Spaces:
Runtime error
Runtime error
Added probabilities properly
Browse files- app.py +4 -2
- test.ipynb +56 -128
app.py
CHANGED
@@ -5,8 +5,10 @@ learn = load_learner('model.pth')
|
|
5 |
|
6 |
def classify_image(img):
|
7 |
celebrity_name,_,probs = learn.predict(img)
|
8 |
-
|
9 |
-
|
|
|
|
|
10 |
|
11 |
image = gr.inputs.Image(shape=(224,224))
|
12 |
label = gr.outputs.Label()
|
|
|
5 |
|
6 |
def classify_image(img):
|
7 |
celebrity_name,_,probs = learn.predict(img)
|
8 |
+
results = dict(zip(list(learn.dls.vocab),probs))
|
9 |
+
best_results = sorted(A, key=A.get, reverse=True)[:5]
|
10 |
+
probabilities = {key: A[key] for key in best_results}
|
11 |
+
return {probabilities}
|
12 |
|
13 |
image = gr.inputs.Image(shape=(224,224))
|
14 |
label = gr.outputs.Label()
|
test.ipynb
CHANGED
@@ -21,7 +21,27 @@
|
|
21 |
},
|
22 |
{
|
23 |
"cell_type": "code",
|
24 |
-
"execution_count":
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
"metadata": {},
|
26 |
"outputs": [
|
27 |
{
|
@@ -64,156 +84,64 @@
|
|
64 |
{
|
65 |
"data": {
|
66 |
"text/plain": [
|
67 |
-
"
|
68 |
-
" tensor(
|
69 |
-
" tensor(
|
70 |
-
"
|
71 |
-
"
|
72 |
-
" 8.8360e-07, 6.3956e-06, 1.2606e-06, 1.0974e-07, 6.6237e-08, 4.8988e-06,\n",
|
73 |
-
" 3.1586e-06, 3.8812e-08, 3.1651e-05, 2.0162e-06, 4.4486e-07, 1.3981e-04,\n",
|
74 |
-
" 9.9433e-01, 9.9587e-08, 4.3374e-06, 7.6844e-08, 1.2182e-06, 9.4450e-07,\n",
|
75 |
-
" 7.8193e-09, 1.0388e-06, 4.0273e-07, 1.4351e-07, 3.5202e-07, 1.0416e-07,\n",
|
76 |
-
" 5.1168e-07, 1.0324e-06, 8.8179e-07, 1.0323e-07, 1.7276e-05, 2.7315e-04,\n",
|
77 |
-
" 3.0939e-07, 2.0662e-06, 8.7540e-08, 2.7940e-06, 7.7654e-05, 2.8455e-03,\n",
|
78 |
-
" 1.5032e-04, 2.5726e-07, 4.8556e-04, 1.2773e-06, 2.7566e-06, 1.0871e-06,\n",
|
79 |
-
" 2.8510e-06, 1.8711e-07, 8.1365e-07, 1.4328e-07, 6.6794e-06, 5.6703e-08,\n",
|
80 |
-
" 9.1386e-09, 6.7899e-04, 2.3621e-07, 2.4263e-05, 1.7181e-07, 2.1496e-06,\n",
|
81 |
-
" 6.3289e-08, 1.0691e-04, 5.2058e-07, 1.4683e-06, 3.1002e-09, 1.3486e-06,\n",
|
82 |
-
" 6.8411e-07, 9.8865e-07, 1.4920e-04, 1.5001e-05, 1.8135e-07, 4.3004e-05,\n",
|
83 |
-
" 2.2933e-07, 1.4821e-06, 1.7363e-05, 5.0178e-07, 2.2853e-07, 4.7195e-08,\n",
|
84 |
-
" 2.7670e-07, 4.9980e-05, 4.5713e-08, 2.0050e-06, 1.3772e-06, 4.8403e-07,\n",
|
85 |
-
" 1.6381e-08, 3.9686e-08, 4.5362e-07]))"
|
86 |
]
|
87 |
},
|
88 |
-
"execution_count":
|
89 |
"metadata": {},
|
90 |
"output_type": "execute_result"
|
91 |
}
|
92 |
],
|
93 |
"source": [
|
94 |
-
"learn.predict('emma_watson.jpg')"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
95 |
]
|
96 |
},
|
97 |
{
|
98 |
"cell_type": "code",
|
99 |
-
"execution_count":
|
100 |
"metadata": {},
|
101 |
"outputs": [
|
102 |
{
|
103 |
"data": {
|
104 |
"text/plain": [
|
105 |
-
"
|
106 |
-
" 'Arnold Schwarzenegger\\n',\n",
|
107 |
-
" 'Jim Carrey\\n',\n",
|
108 |
-
" 'Emma Watson\\n',\n",
|
109 |
-
" 'Robert Downey Jr.\\n',\n",
|
110 |
-
" 'Daniel Radcliffe\\n',\n",
|
111 |
-
" 'Chris Evans\\n',\n",
|
112 |
-
" 'Leonardo DiCaprio\\n',\n",
|
113 |
-
" 'Tom Cruise\\n',\n",
|
114 |
-
" 'Brad Pitt\\n',\n",
|
115 |
-
" 'Charles Chaplin\\n',\n",
|
116 |
-
" 'Morgan Freeman\\n',\n",
|
117 |
-
" 'Tom Hanks\\n',\n",
|
118 |
-
" 'Hugh Jackman\\n',\n",
|
119 |
-
" 'Matt Damon\\n',\n",
|
120 |
-
" 'Sylvester Stallone\\n',\n",
|
121 |
-
" 'Will Smith\\n',\n",
|
122 |
-
" 'Clint Eastwood\\n',\n",
|
123 |
-
" 'Cameron Diaz\\n',\n",
|
124 |
-
" 'George Clooney\\n',\n",
|
125 |
-
" 'Steven Spielberg\\n',\n",
|
126 |
-
" 'Harrison Ford\\n',\n",
|
127 |
-
" 'Robert De Niro\\n',\n",
|
128 |
-
" 'Al Pacino\\n',\n",
|
129 |
-
" 'Robert Downey Jr.\\n',\n",
|
130 |
-
" 'Russell Crowe\\n',\n",
|
131 |
-
" 'Liam Neeson\\n',\n",
|
132 |
-
" 'Kate Winslet\\n',\n",
|
133 |
-
" 'Mark Wahlberg\\n',\n",
|
134 |
-
" 'Natalie Portman\\n',\n",
|
135 |
-
" 'Pierce Brosnan\\n',\n",
|
136 |
-
" 'Sean Connery\\n',\n",
|
137 |
-
" 'Orlando Bloom\\n',\n",
|
138 |
-
" 'Dwayne Johnson\\n',\n",
|
139 |
-
" 'Jackie Chan\\n',\n",
|
140 |
-
" 'Angelina Jolie\\n',\n",
|
141 |
-
" 'Adam Sandler\\n',\n",
|
142 |
-
" 'Scarlett Johansson\\n',\n",
|
143 |
-
" 'Heath Ledger\\n',\n",
|
144 |
-
" 'Anne Hathaway\\n',\n",
|
145 |
-
" 'Jessica Alba\\n',\n",
|
146 |
-
" 'Edward Norton\\n',\n",
|
147 |
-
" 'Keira Knightley\\n',\n",
|
148 |
-
" 'Bradley Cooper\\n',\n",
|
149 |
-
" 'Will Ferrell\\n',\n",
|
150 |
-
" 'Julia Roberts\\n',\n",
|
151 |
-
" 'Nicolas Cage\\n',\n",
|
152 |
-
" 'Daniel Craig\\n',\n",
|
153 |
-
" 'Keanu Reeves\\n',\n",
|
154 |
-
" 'Ian McKellen\\n',\n",
|
155 |
-
" 'Halle Berry\\n',\n",
|
156 |
-
" 'Bruce Willis\\n',\n",
|
157 |
-
" 'Samuel L. Jackson\\n',\n",
|
158 |
-
" 'Ben Stiller\\n',\n",
|
159 |
-
" 'Tommy Lee Jones\\n',\n",
|
160 |
-
" 'Antonio Banderas\\n',\n",
|
161 |
-
" 'Denzel Washington\\n',\n",
|
162 |
-
" 'Steve Carell\\n',\n",
|
163 |
-
" 'Shia LaBeouf\\n',\n",
|
164 |
-
" 'Megan Fox\\n',\n",
|
165 |
-
" 'James Franco\\n',\n",
|
166 |
-
" 'Mel Gibson\\n',\n",
|
167 |
-
" 'Vin Diesel\\n',\n",
|
168 |
-
" 'Tim Allen\\n',\n",
|
169 |
-
" 'Robin Williams\\n',\n",
|
170 |
-
" 'Kevin Spacey\\n',\n",
|
171 |
-
" 'Jason Biggs\\n',\n",
|
172 |
-
" 'Seann William Scott\\n',\n",
|
173 |
-
" 'Jean-Claude Van Damme\\n',\n",
|
174 |
-
" 'Zach Galifianakis\\n',\n",
|
175 |
-
" 'Owen Wilson\\n',\n",
|
176 |
-
" 'Christian Bale\\n',\n",
|
177 |
-
" 'Peter Jackson\\n',\n",
|
178 |
-
" 'Sandra Bullock\\n',\n",
|
179 |
-
" 'Bruce Lee\\n',\n",
|
180 |
-
" 'Drew Barrymore\\n',\n",
|
181 |
-
" 'Macaulay Culkin\\n',\n",
|
182 |
-
" 'Jack Nicholson\\n',\n",
|
183 |
-
" 'Bill Murray\\n',\n",
|
184 |
-
" 'Sigourney Weaver\\n',\n",
|
185 |
-
" 'Jake Gyllenhaal\\n',\n",
|
186 |
-
" 'Jason Statham\\n',\n",
|
187 |
-
" 'Jet Li\\n',\n",
|
188 |
-
" 'Kate Beckinsale\\n',\n",
|
189 |
-
" 'Rowan Atkinson\\n',\n",
|
190 |
-
" 'Marlon Brando\\n',\n",
|
191 |
-
" 'John Travolta\\n',\n",
|
192 |
-
" 'Channing Tatum\\n',\n",
|
193 |
-
" 'Ben Affleck\\n',\n",
|
194 |
-
" 'Shah Rukh Khan\\n',\n",
|
195 |
-
" 'Jennifer Aniston\\n',\n",
|
196 |
-
" 'Emma Stone\\n',\n",
|
197 |
-
" 'Chris Hemsworth\\n',\n",
|
198 |
-
" 'James McAvoy\\n',\n",
|
199 |
-
" 'James Cameron\\n',\n",
|
200 |
-
" 'Amitabh Bachchan\\n',\n",
|
201 |
-
" 'Brendan Fraser\\n',\n",
|
202 |
-
" 'Rachel McAdams\\n',\n",
|
203 |
-
" 'Tom Hiddleston\\n',\n",
|
204 |
-
" 'Aamir Khan\\n']"
|
205 |
]
|
206 |
},
|
207 |
-
"execution_count":
|
208 |
"metadata": {},
|
209 |
"output_type": "execute_result"
|
210 |
}
|
211 |
],
|
212 |
"source": [
|
213 |
-
"
|
214 |
-
" celeb_list = file.readlines()\n",
|
215 |
-
"\n",
|
216 |
-
"celeb_list = [celeb.replace('\\n', '') for celeb in celeb_list]"
|
217 |
]
|
218 |
},
|
219 |
{
|
|
|
21 |
},
|
22 |
{
|
23 |
"cell_type": "code",
|
24 |
+
"execution_count": 24,
|
25 |
+
"metadata": {},
|
26 |
+
"outputs": [
|
27 |
+
{
|
28 |
+
"data": {
|
29 |
+
"text/plain": [
|
30 |
+
"30"
|
31 |
+
]
|
32 |
+
},
|
33 |
+
"execution_count": 24,
|
34 |
+
"metadata": {},
|
35 |
+
"output_type": "execute_result"
|
36 |
+
}
|
37 |
+
],
|
38 |
+
"source": [
|
39 |
+
"list(learn.dls.vocab).index('Emma Watson')"
|
40 |
+
]
|
41 |
+
},
|
42 |
+
{
|
43 |
+
"cell_type": "code",
|
44 |
+
"execution_count": 60,
|
45 |
"metadata": {},
|
46 |
"outputs": [
|
47 |
{
|
|
|
84 |
{
|
85 |
"data": {
|
86 |
"text/plain": [
|
87 |
+
"{'Emma Watson': tensor(0.9943),\n",
|
88 |
+
" 'Kate Beckinsale': tensor(0.0028),\n",
|
89 |
+
" 'Natalie Portman': tensor(0.0007),\n",
|
90 |
+
" 'Keira Knightley': tensor(0.0005),\n",
|
91 |
+
" 'Jessica Alba': tensor(0.0003)}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
92 |
]
|
93 |
},
|
94 |
+
"execution_count": 60,
|
95 |
"metadata": {},
|
96 |
"output_type": "execute_result"
|
97 |
}
|
98 |
],
|
99 |
"source": [
|
100 |
+
"results = dict(zip(list(learn.dls.vocab),learn.predict('emma_watson.jpg')[2]))\n",
|
101 |
+
"best_results = sorted(A, key=A.get, reverse=True)[:5]\n",
|
102 |
+
"probabilities = {key: A[key] for key in best_results}\n",
|
103 |
+
"probabilities"
|
104 |
+
]
|
105 |
+
},
|
106 |
+
{
|
107 |
+
"cell_type": "code",
|
108 |
+
"execution_count": 46,
|
109 |
+
"metadata": {},
|
110 |
+
"outputs": [
|
111 |
+
{
|
112 |
+
"name": "stdout",
|
113 |
+
"output_type": "stream",
|
114 |
+
"text": [
|
115 |
+
"\u001b[0;31mDocstring:\u001b[0m\n",
|
116 |
+
"max(dim=None, keepdim=False) -> Tensor or (Tensor, Tensor)\n",
|
117 |
+
"\n",
|
118 |
+
"See :func:`torch.max`\n",
|
119 |
+
"\u001b[0;31mType:\u001b[0m builtin_function_or_method"
|
120 |
+
]
|
121 |
+
}
|
122 |
+
],
|
123 |
+
"source": [
|
124 |
+
"?pred.max"
|
125 |
]
|
126 |
},
|
127 |
{
|
128 |
"cell_type": "code",
|
129 |
+
"execution_count": 19,
|
130 |
"metadata": {},
|
131 |
"outputs": [
|
132 |
{
|
133 |
"data": {
|
134 |
"text/plain": [
|
135 |
+
"3"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
136 |
]
|
137 |
},
|
138 |
+
"execution_count": 19,
|
139 |
"metadata": {},
|
140 |
"output_type": "execute_result"
|
141 |
}
|
142 |
],
|
143 |
"source": [
|
144 |
+
"celeb_list.index('Emma Watson')"
|
|
|
|
|
|
|
145 |
]
|
146 |
},
|
147 |
{
|