ludjan commited on
Commit
a1fb560
1 Parent(s): b6a3c4f
Files changed (4) hide show
  1. app.py +6 -3
  2. cycle-model.pkl +0 -3
  3. images/tricycle2.jpeg +0 -0
  4. notebook.ipynb +28 -51
app.py CHANGED
@@ -1,7 +1,7 @@
1
  # AUTOGENERATED! DO NOT EDIT! File to edit: notebook.ipynb.
2
 
3
  # %% auto 0
4
- __all__ = ['learn', 'categories', 'image', 'label', 'intf', 'classify_image']
5
 
6
  # %% notebook.ipynb 2
7
  from fastai.vision.all import *
@@ -9,7 +9,7 @@ import gradio as gr
9
  import timm
10
 
11
  # %% notebook.ipynb 3
12
- learn = load_learner('cycle-model.pkl')
13
 
14
  # %% notebook.ipynb 4
15
  categories = learn.dls.vocab
@@ -18,7 +18,10 @@ def classify_image(img):
18
  pred,idx,probs = learn.predict(img)
19
  return dict(zip(categories, map(float,probs)))
20
 
21
- # %% notebook.ipynb 6
 
 
 
22
  image = gr.Image()
23
  label = gr.Label()
24
 
 
1
  # AUTOGENERATED! DO NOT EDIT! File to edit: notebook.ipynb.
2
 
3
  # %% auto 0
4
+ __all__ = ['learn', 'categories', 'examples', 'image', 'label', 'intf', 'classify_image']
5
 
6
  # %% notebook.ipynb 2
7
  from fastai.vision.all import *
 
9
  import timm
10
 
11
  # %% notebook.ipynb 3
12
+ learn = load_learner('model.pkl')
13
 
14
  # %% notebook.ipynb 4
15
  categories = learn.dls.vocab
 
18
  pred,idx,probs = learn.predict(img)
19
  return dict(zip(categories, map(float,probs)))
20
 
21
+ # %% notebook.ipynb 5
22
+ examples = ['images/unicycle.jpeg', 'images/bicycle.jpeg', 'images/tricycle.png']
23
+
24
+ # %% notebook.ipynb 7
25
  image = gr.Image()
26
  label = gr.Label()
27
 
cycle-model.pkl DELETED
@@ -1,3 +0,0 @@
1
- version https://git-lfs.github.com/spec/v1
2
- oid sha256:772d8b028992a9b7b0257307849a6b3823f44edefed7d7eaaf86ad42851bc70b
3
- size 46961195
 
 
 
 
images/tricycle2.jpeg ADDED
notebook.ipynb CHANGED
@@ -44,18 +44,18 @@
44
  },
45
  {
46
  "cell_type": "code",
47
- "execution_count": 4,
48
  "id": "f0218bf1-1836-4d7a-8d47-33584471f28b",
49
  "metadata": {},
50
  "outputs": [],
51
  "source": [
52
  "#|export\n",
53
- "learn = load_learner('cycle-model.pkl')"
54
  ]
55
  },
56
  {
57
  "cell_type": "code",
58
- "execution_count": 5,
59
  "id": "168ac2e4-f83b-4ce0-8f23-00999eb5d556",
60
  "metadata": {},
61
  "outputs": [],
@@ -70,9 +70,20 @@
70
  },
71
  {
72
  "cell_type": "code",
73
- "execution_count": 6,
74
  "id": "d343a0d3-40fd-4502-a86b-cb3bac9fdf7f",
75
  "metadata": {},
 
 
 
 
 
 
 
 
 
 
 
76
  "outputs": [
77
  {
78
  "data": {
@@ -163,60 +174,26 @@
163
  ]
164
  },
165
  {
166
- "data": {
167
- "text/html": [
168
- "\n",
169
- "<style>\n",
170
- " /* Turns off some styling */\n",
171
- " progress {\n",
172
- " /* gets rid of default border in Firefox and Opera. */\n",
173
- " border: none;\n",
174
- " /* Needs to be in here for Safari polyfill so background images work as expected. */\n",
175
- " background-size: auto;\n",
176
- " }\n",
177
- " progress:not([value]), progress:not([value])::-webkit-progress-bar {\n",
178
- " background: repeating-linear-gradient(45deg, #7e7e7e, #7e7e7e 10px, #5c5c5c 10px, #5c5c5c 20px);\n",
179
- " }\n",
180
- " .progress-bar-interrupted, .progress-bar-interrupted::-webkit-progress-bar {\n",
181
- " background: #F44336;\n",
182
- " }\n",
183
- "</style>\n"
184
- ],
185
- "text/plain": [
186
- "<IPython.core.display.HTML object>"
187
- ]
188
- },
189
- "metadata": {},
190
- "output_type": "display_data"
191
- },
192
- {
193
- "data": {
194
- "text/html": [],
195
- "text/plain": [
196
- "<IPython.core.display.HTML object>"
197
- ]
198
- },
199
- "metadata": {},
200
- "output_type": "display_data"
201
- },
202
- {
203
- "name": "stdout",
204
- "output_type": "stream",
205
- "text": [
206
- "images/tricycle.png is a tricycle\n"
207
  ]
208
  }
209
  ],
210
  "source": [
211
- "# Upload your own images and link them\n",
212
- "examples = ['images/unicycle.jpeg', 'images/bicycle.jpeg', 'images/tricycle.png']\n",
213
- "\n",
214
  "for example in examples:\n",
215
  " image = PILImage.create(example)\n",
216
  " res_dict = classify_image(image)\n",
217
- " top = max(res_dict, key=res_dict.get)\n",
218
- "\n",
219
- " print(example + ' is a '+ top)"
220
  ]
221
  },
222
  {
 
44
  },
45
  {
46
  "cell_type": "code",
47
+ "execution_count": 6,
48
  "id": "f0218bf1-1836-4d7a-8d47-33584471f28b",
49
  "metadata": {},
50
  "outputs": [],
51
  "source": [
52
  "#|export\n",
53
+ "learn = load_learner('model.pkl')"
54
  ]
55
  },
56
  {
57
  "cell_type": "code",
58
+ "execution_count": 7,
59
  "id": "168ac2e4-f83b-4ce0-8f23-00999eb5d556",
60
  "metadata": {},
61
  "outputs": [],
 
70
  },
71
  {
72
  "cell_type": "code",
73
+ "execution_count": 8,
74
  "id": "d343a0d3-40fd-4502-a86b-cb3bac9fdf7f",
75
  "metadata": {},
76
+ "outputs": [],
77
+ "source": [
78
+ "#|export\n",
79
+ "examples = ['images/unicycle.jpeg', 'images/bicycle.jpeg', 'images/tricycle.png']"
80
+ ]
81
+ },
82
+ {
83
+ "cell_type": "code",
84
+ "execution_count": 10,
85
+ "id": "645eb0ee-b7e5-4ec4-a42e-9f43a163a3a5",
86
+ "metadata": {},
87
  "outputs": [
88
  {
89
  "data": {
 
174
  ]
175
  },
176
  {
177
+ "ename": "UnidentifiedImageError",
178
+ "evalue": "cannot identify image file 'images/tricycle.png'",
179
+ "output_type": "error",
180
+ "traceback": [
181
+ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
182
+ "\u001b[0;31mUnidentifiedImageError\u001b[0m Traceback (most recent call last)",
183
+ "Cell \u001b[0;32mIn[10], line 2\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m example \u001b[38;5;129;01min\u001b[39;00m examples:\n\u001b[0;32m----> 2\u001b[0m image \u001b[38;5;241m=\u001b[39m PILImage\u001b[38;5;241m.\u001b[39mcreate(example)\n\u001b[1;32m 3\u001b[0m res_dict \u001b[38;5;241m=\u001b[39m classify_image(image)\n\u001b[1;32m 4\u001b[0m top_prob_key \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mmax\u001b[39m(res_dict, key\u001b[38;5;241m=\u001b[39mres_dict\u001b[38;5;241m.\u001b[39mget)\n",
184
+ "File \u001b[0;32m~/miniconda3/lib/python3.11/site-packages/fastai/vision/core.py:125\u001b[0m, in \u001b[0;36mPILBase.create\u001b[0;34m(cls, fn, **kwargs)\u001b[0m\n\u001b[1;32m 123\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28misinstance\u001b[39m(fn,\u001b[38;5;28mbytes\u001b[39m): fn \u001b[38;5;241m=\u001b[39m io\u001b[38;5;241m.\u001b[39mBytesIO(fn)\n\u001b[1;32m 124\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28misinstance\u001b[39m(fn,Image\u001b[38;5;241m.\u001b[39mImage): \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28mcls\u001b[39m(fn)\n\u001b[0;32m--> 125\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28mcls\u001b[39m(load_image(fn, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mmerge(\u001b[38;5;28mcls\u001b[39m\u001b[38;5;241m.\u001b[39m_open_args, kwargs)))\n",
185
+ "File \u001b[0;32m~/miniconda3/lib/python3.11/site-packages/fastai/vision/core.py:98\u001b[0m, in \u001b[0;36mload_image\u001b[0;34m(fn, mode)\u001b[0m\n\u001b[1;32m 96\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mload_image\u001b[39m(fn, mode\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mNone\u001b[39;00m):\n\u001b[1;32m 97\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mOpen and load a `PIL.Image` and convert to `mode`\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[0;32m---> 98\u001b[0m im \u001b[38;5;241m=\u001b[39m Image\u001b[38;5;241m.\u001b[39mopen(fn)\n\u001b[1;32m 99\u001b[0m im\u001b[38;5;241m.\u001b[39mload()\n\u001b[1;32m 100\u001b[0m im \u001b[38;5;241m=\u001b[39m im\u001b[38;5;241m.\u001b[39m_new(im\u001b[38;5;241m.\u001b[39mim)\n",
186
+ "File \u001b[0;32m~/miniconda3/lib/python3.11/site-packages/PIL/Image.py:3280\u001b[0m, in \u001b[0;36mopen\u001b[0;34m(fp, mode, formats)\u001b[0m\n\u001b[1;32m 3278\u001b[0m warnings\u001b[38;5;241m.\u001b[39mwarn(message)\n\u001b[1;32m 3279\u001b[0m msg \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mcannot identify image file \u001b[39m\u001b[38;5;132;01m%r\u001b[39;00m\u001b[38;5;124m\"\u001b[39m \u001b[38;5;241m%\u001b[39m (filename \u001b[38;5;28;01mif\u001b[39;00m filename \u001b[38;5;28;01melse\u001b[39;00m fp)\n\u001b[0;32m-> 3280\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m UnidentifiedImageError(msg)\n",
187
+ "\u001b[0;31mUnidentifiedImageError\u001b[0m: cannot identify image file 'images/tricycle.png'"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
188
  ]
189
  }
190
  ],
191
  "source": [
 
 
 
192
  "for example in examples:\n",
193
  " image = PILImage.create(example)\n",
194
  " res_dict = classify_image(image)\n",
195
+ " top_prob_key = max(res_dict, key=res_dict.get)\n",
196
+ " print(example + ' is a '+ top_prob_key)"
 
197
  ]
198
  },
199
  {