jph00 commited on
Commit
d3f0b6e
1 Parent(s): b45a4be
Files changed (3) hide show
  1. app.ipynb +217 -129
  2. app.py +1 -1
  3. model.pkl +2 -2
app.ipynb CHANGED
@@ -2,12 +2,22 @@
2
  "cells": [
3
  {
4
  "cell_type": "code",
5
- "execution_count": 2,
 
 
 
 
 
 
 
 
 
 
6
  "id": "44eb0ad3",
7
  "metadata": {},
8
  "outputs": [],
9
  "source": [
10
- "#export\n",
11
  "from fastai.vision.all import *\n",
12
  "import gradio as gr\n",
13
  "\n",
@@ -16,7 +26,7 @@
16
  },
17
  {
18
  "cell_type": "code",
19
- "execution_count": 3,
20
  "id": "d838c0b3",
21
  "metadata": {},
22
  "outputs": [],
@@ -31,7 +41,7 @@
31
  },
32
  {
33
  "cell_type": "code",
34
- "execution_count": 4,
35
  "id": "c107f724",
36
  "metadata": {},
37
  "outputs": [
@@ -75,10 +85,10 @@
75
  " <tbody>\n",
76
  " <tr>\n",
77
  " <td>0</td>\n",
78
- " <td>0.217549</td>\n",
79
- " <td>0.094998</td>\n",
80
- " <td>0.026387</td>\n",
81
- " <td>00:05</td>\n",
82
  " </tr>\n",
83
  " </tbody>\n",
84
  "</table>"
@@ -130,24 +140,24 @@
130
  " <tbody>\n",
131
  " <tr>\n",
132
  " <td>0</td>\n",
133
- " <td>0.095330</td>\n",
134
- " <td>0.042852</td>\n",
135
- " <td>0.013532</td>\n",
136
- " <td>00:06</td>\n",
137
  " </tr>\n",
138
  " <tr>\n",
139
  " <td>1</td>\n",
140
- " <td>0.036632</td>\n",
141
- " <td>0.046426</td>\n",
142
- " <td>0.016915</td>\n",
143
- " <td>00:05</td>\n",
144
  " </tr>\n",
145
  " <tr>\n",
146
  " <td>2</td>\n",
147
- " <td>0.019293</td>\n",
148
- " <td>0.038611</td>\n",
149
- " <td>0.013532</td>\n",
150
- " <td>00:05</td>\n",
151
  " </tr>\n",
152
  " </tbody>\n",
153
  "</table>"
@@ -167,7 +177,158 @@
167
  },
168
  {
169
  "cell_type": "code",
170
- "execution_count": 5,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
171
  "id": "5171c7fc",
172
  "metadata": {},
173
  "outputs": [],
@@ -177,7 +338,7 @@
177
  },
178
  {
179
  "cell_type": "code",
180
- "execution_count": 6,
181
  "id": "3295ef11",
182
  "metadata": {},
183
  "outputs": [
@@ -188,7 +349,7 @@
188
  "PILImage mode=RGB size=192x191"
189
  ]
190
  },
191
- "execution_count": 6,
192
  "metadata": {},
193
  "output_type": "execute_result"
194
  }
@@ -201,22 +362,20 @@
201
  },
202
  {
203
  "cell_type": "code",
204
- "execution_count": 7,
205
  "id": "ae2bc6ac",
206
  "metadata": {},
207
  "outputs": [],
208
  "source": [
209
- "#export\n",
210
  "learn = load_learner('model.pkl')"
211
  ]
212
  },
213
  {
214
  "cell_type": "code",
215
- "execution_count": 8,
216
  "id": "6e0bf9da",
217
- "metadata": {
218
- "scrolled": false
219
- },
220
  "outputs": [
221
  {
222
  "data": {
@@ -255,10 +414,10 @@
255
  {
256
  "data": {
257
  "text/plain": [
258
- "('False', TensorBase(0), TensorBase([9.9993e-01, 6.6811e-05]))"
259
  ]
260
  },
261
- "execution_count": 8,
262
  "metadata": {},
263
  "output_type": "execute_result"
264
  }
@@ -269,12 +428,12 @@
269
  },
270
  {
271
  "cell_type": "code",
272
- "execution_count": 9,
273
  "id": "0419ed3a",
274
  "metadata": {},
275
  "outputs": [],
276
  "source": [
277
- "#export\n",
278
  "categories = ('Dog', 'Cat')\n",
279
  "\n",
280
  "def classify_image(img):\n",
@@ -284,7 +443,7 @@
284
  },
285
  {
286
  "cell_type": "code",
287
- "execution_count": 10,
288
  "id": "762dec00",
289
  "metadata": {},
290
  "outputs": [
@@ -325,10 +484,10 @@
325
  {
326
  "data": {
327
  "text/plain": [
328
- "{'Dog': 0.9999332427978516, 'Cat': 6.681094237137586e-05}"
329
  ]
330
  },
331
- "execution_count": 10,
332
  "metadata": {},
333
  "output_type": "execute_result"
334
  }
@@ -339,11 +498,9 @@
339
  },
340
  {
341
  "cell_type": "code",
342
- "execution_count": 10,
343
  "id": "0518a30a",
344
- "metadata": {
345
- "collapsed": true
346
- },
347
  "outputs": [
348
  {
349
  "name": "stdout",
@@ -354,113 +511,69 @@
354
  "To create a public link, set `share=True` in `launch()`.\n"
355
  ]
356
  },
357
- {
358
- "data": {
359
- "text/html": [
360
- "\n",
361
- " <iframe\n",
362
- " width=\"900\"\n",
363
- " height=\"500\"\n",
364
- " src=\"http://127.0.0.1:7860/\"\n",
365
- " frameborder=\"0\"\n",
366
- " allowfullscreen\n",
367
- " \n",
368
- " ></iframe>\n",
369
- " "
370
- ],
371
- "text/plain": [
372
- "<IPython.lib.display.IFrame at 0x7f98552d6340>"
373
- ]
374
- },
375
- "metadata": {},
376
- "output_type": "display_data"
377
- },
378
  {
379
  "data": {
380
  "text/plain": [
381
- "(<fastapi.applications.FastAPI at 0x7f986cf492b0>,\n",
382
  " 'http://127.0.0.1:7860/',\n",
383
  " None)"
384
  ]
385
  },
386
- "execution_count": 10,
387
  "metadata": {},
388
  "output_type": "execute_result"
389
  }
390
  ],
391
  "source": [
392
- "#export\n",
393
  "image = gr.inputs.Image(shape=(192, 192))\n",
394
  "label = gr.outputs.Label()\n",
395
  "examples = ['dog.jpg', 'cat.jpg', 'dunno.jpg']\n",
396
  "\n",
397
  "intf = gr.Interface(fn=classify_image, inputs=image, outputs=label, examples=examples)\n",
398
- "intf.launch()"
399
  ]
400
  },
401
  {
402
- "cell_type": "code",
403
- "execution_count": 11,
404
- "id": "103be39f",
405
  "metadata": {},
406
- "outputs": [],
407
  "source": [
408
- "import requests,base64\n",
409
- "from PIL import Image\n",
410
- "from io import BytesIO"
411
  ]
412
  },
413
  {
414
  "cell_type": "code",
415
- "execution_count": 12,
416
- "id": "fd962acc",
417
  "metadata": {},
418
  "outputs": [],
419
  "source": [
420
- "def data_url(filename, size=(192,192)):\n",
421
- " image = PILImage.create(filename)\n",
422
- " image.thumbnail(size)\n",
423
- " buff = BytesIO()\n",
424
- " image.save(buff, format=\"JPEG\")\n",
425
- " prefix = f'data:image/{Path(filename).suffix[1:]};base64,'\n",
426
- " return prefix + base64.b64encode(buff.getvalue()).decode('utf-8')"
427
  ]
428
  },
429
  {
430
  "cell_type": "code",
431
- "execution_count": 13,
432
- "id": "a55f921b",
433
- "metadata": {
434
- "scrolled": true
435
- },
436
  "outputs": [
437
  {
438
- "data": {
439
- "text/plain": [
440
- "{'data': [{'label': 'Cat',\n",
441
- " 'confidences': [{'label': 'Cat', 'confidence': 1.0},\n",
442
- " {'label': 'Dog', 'confidence': 2.655391640078719e-13}]}],\n",
443
- " 'flag_index': None,\n",
444
- " 'updated_state': None,\n",
445
- " 'durations': [0.0977640151977539],\n",
446
- " 'avg_durations': [0.0977640151977539]}"
447
- ]
448
- },
449
- "execution_count": 13,
450
- "metadata": {},
451
- "output_type": "execute_result"
452
  }
453
  ],
454
  "source": [
455
- "data = {\"data\": [data_url('cat.jpg')]}\n",
456
- "res = requests.post(url='https://hf.space/embed/jph00/testing/+/api/predict/', json=data).json()\n",
457
- "res"
458
  ]
459
  },
460
  {
461
  "cell_type": "code",
462
  "execution_count": null,
463
- "id": "82774c08",
464
  "metadata": {},
465
  "outputs": [],
466
  "source": []
@@ -471,31 +584,6 @@
471
  "display_name": "Python 3 (ipykernel)",
472
  "language": "python",
473
  "name": "python3"
474
- },
475
- "language_info": {
476
- "codemirror_mode": {
477
- "name": "ipython",
478
- "version": 3
479
- },
480
- "file_extension": ".py",
481
- "mimetype": "text/x-python",
482
- "name": "python",
483
- "nbconvert_exporter": "python",
484
- "pygments_lexer": "ipython3",
485
- "version": "3.9.5"
486
- },
487
- "toc": {
488
- "base_numbering": 1,
489
- "nav_menu": {},
490
- "number_sections": false,
491
- "sideBar": true,
492
- "skip_h1_title": false,
493
- "title_cell": "Table of Contents",
494
- "title_sidebar": "Contents",
495
- "toc_cell": false,
496
- "toc_position": {},
497
- "toc_section_display": true,
498
- "toc_window_display": false
499
  }
500
  },
501
  "nbformat": 4,
 
2
  "cells": [
3
  {
4
  "cell_type": "code",
5
+ "execution_count": null,
6
+ "id": "18acb717",
7
+ "metadata": {},
8
+ "outputs": [],
9
+ "source": [
10
+ "#|default_exp app"
11
+ ]
12
+ },
13
+ {
14
+ "cell_type": "code",
15
+ "execution_count": null,
16
  "id": "44eb0ad3",
17
  "metadata": {},
18
  "outputs": [],
19
  "source": [
20
+ "#|export\n",
21
  "from fastai.vision.all import *\n",
22
  "import gradio as gr\n",
23
  "\n",
 
26
  },
27
  {
28
  "cell_type": "code",
29
+ "execution_count": null,
30
  "id": "d838c0b3",
31
  "metadata": {},
32
  "outputs": [],
 
41
  },
42
  {
43
  "cell_type": "code",
44
+ "execution_count": null,
45
  "id": "c107f724",
46
  "metadata": {},
47
  "outputs": [
 
85
  " <tbody>\n",
86
  " <tr>\n",
87
  " <td>0</td>\n",
88
+ " <td>0.209574</td>\n",
89
+ " <td>0.081121</td>\n",
90
+ " <td>0.022327</td>\n",
91
+ " <td>00:24</td>\n",
92
  " </tr>\n",
93
  " </tbody>\n",
94
  "</table>"
 
140
  " <tbody>\n",
141
  " <tr>\n",
142
  " <td>0</td>\n",
143
+ " <td>0.090262</td>\n",
144
+ " <td>0.056602</td>\n",
145
+ " <td>0.017591</td>\n",
146
+ " <td>00:23</td>\n",
147
  " </tr>\n",
148
  " <tr>\n",
149
  " <td>1</td>\n",
150
+ " <td>0.035389</td>\n",
151
+ " <td>0.037754</td>\n",
152
+ " <td>0.014208</td>\n",
153
+ " <td>00:22</td>\n",
154
  " </tr>\n",
155
  " <tr>\n",
156
  " <td>2</td>\n",
157
+ " <td>0.013607</td>\n",
158
+ " <td>0.038817</td>\n",
159
+ " <td>0.012179</td>\n",
160
+ " <td>00:22</td>\n",
161
  " </tr>\n",
162
  " </tbody>\n",
163
  "</table>"
 
177
  },
178
  {
179
  "cell_type": "code",
180
+ "execution_count": null,
181
+ "id": "bed928f3",
182
+ "metadata": {},
183
+ "outputs": [],
184
+ "source": [
185
+ "path = untar_data(URLs.PETS)/'images'\n",
186
+ "\n",
187
+ "dls = ImageDataLoaders.from_name_func('.',\n",
188
+ " get_image_files(path), valid_pct=0.2, seed=42,\n",
189
+ " label_func=is_cat,\n",
190
+ " item_tfms=Resize(192))"
191
+ ]
192
+ },
193
+ {
194
+ "cell_type": "code",
195
+ "execution_count": null,
196
+ "id": "7e56b200",
197
+ "metadata": {},
198
+ "outputs": [
199
+ {
200
+ "data": {
201
+ "text/html": [
202
+ "\n",
203
+ "<style>\n",
204
+ " /* Turns off some styling */\n",
205
+ " progress {\n",
206
+ " /* gets rid of default border in Firefox and Opera. */\n",
207
+ " border: none;\n",
208
+ " /* Needs to be in here for Safari polyfill so background images work as expected. */\n",
209
+ " background-size: auto;\n",
210
+ " }\n",
211
+ " .progress-bar-interrupted, .progress-bar-interrupted::-webkit-progress-bar {\n",
212
+ " background: #F44336;\n",
213
+ " }\n",
214
+ "</style>\n"
215
+ ],
216
+ "text/plain": [
217
+ "<IPython.core.display.HTML object>"
218
+ ]
219
+ },
220
+ "metadata": {},
221
+ "output_type": "display_data"
222
+ },
223
+ {
224
+ "data": {
225
+ "text/html": [
226
+ "<table border=\"1\" class=\"dataframe\">\n",
227
+ " <thead>\n",
228
+ " <tr style=\"text-align: left;\">\n",
229
+ " <th>epoch</th>\n",
230
+ " <th>train_loss</th>\n",
231
+ " <th>valid_loss</th>\n",
232
+ " <th>error_rate</th>\n",
233
+ " <th>time</th>\n",
234
+ " </tr>\n",
235
+ " </thead>\n",
236
+ " <tbody>\n",
237
+ " <tr>\n",
238
+ " <td>0</td>\n",
239
+ " <td>0.184049</td>\n",
240
+ " <td>0.038403</td>\n",
241
+ " <td>0.010825</td>\n",
242
+ " <td>00:21</td>\n",
243
+ " </tr>\n",
244
+ " </tbody>\n",
245
+ "</table>"
246
+ ],
247
+ "text/plain": [
248
+ "<IPython.core.display.HTML object>"
249
+ ]
250
+ },
251
+ "metadata": {},
252
+ "output_type": "display_data"
253
+ },
254
+ {
255
+ "data": {
256
+ "text/html": [
257
+ "\n",
258
+ "<style>\n",
259
+ " /* Turns off some styling */\n",
260
+ " progress {\n",
261
+ " /* gets rid of default border in Firefox and Opera. */\n",
262
+ " border: none;\n",
263
+ " /* Needs to be in here for Safari polyfill so background images work as expected. */\n",
264
+ " background-size: auto;\n",
265
+ " }\n",
266
+ " .progress-bar-interrupted, .progress-bar-interrupted::-webkit-progress-bar {\n",
267
+ " background: #F44336;\n",
268
+ " }\n",
269
+ "</style>\n"
270
+ ],
271
+ "text/plain": [
272
+ "<IPython.core.display.HTML object>"
273
+ ]
274
+ },
275
+ "metadata": {},
276
+ "output_type": "display_data"
277
+ },
278
+ {
279
+ "data": {
280
+ "text/html": [
281
+ "<table border=\"1\" class=\"dataframe\">\n",
282
+ " <thead>\n",
283
+ " <tr style=\"text-align: left;\">\n",
284
+ " <th>epoch</th>\n",
285
+ " <th>train_loss</th>\n",
286
+ " <th>valid_loss</th>\n",
287
+ " <th>error_rate</th>\n",
288
+ " <th>time</th>\n",
289
+ " </tr>\n",
290
+ " </thead>\n",
291
+ " <tbody>\n",
292
+ " <tr>\n",
293
+ " <td>0</td>\n",
294
+ " <td>0.075693</td>\n",
295
+ " <td>0.042666</td>\n",
296
+ " <td>0.013532</td>\n",
297
+ " <td>00:24</td>\n",
298
+ " </tr>\n",
299
+ " <tr>\n",
300
+ " <td>1</td>\n",
301
+ " <td>0.038955</td>\n",
302
+ " <td>0.018082</td>\n",
303
+ " <td>0.006089</td>\n",
304
+ " <td>00:22</td>\n",
305
+ " </tr>\n",
306
+ " <tr>\n",
307
+ " <td>2</td>\n",
308
+ " <td>0.016343</td>\n",
309
+ " <td>0.018480</td>\n",
310
+ " <td>0.004736</td>\n",
311
+ " <td>00:24</td>\n",
312
+ " </tr>\n",
313
+ " </tbody>\n",
314
+ "</table>"
315
+ ],
316
+ "text/plain": [
317
+ "<IPython.core.display.HTML object>"
318
+ ]
319
+ },
320
+ "metadata": {},
321
+ "output_type": "display_data"
322
+ }
323
+ ],
324
+ "source": [
325
+ "learn = vision_learner(dls, resnet18, metrics=error_rate).to_fp16()\n",
326
+ "learn.fine_tune(3)"
327
+ ]
328
+ },
329
+ {
330
+ "cell_type": "code",
331
+ "execution_count": null,
332
  "id": "5171c7fc",
333
  "metadata": {},
334
  "outputs": [],
 
338
  },
339
  {
340
  "cell_type": "code",
341
+ "execution_count": null,
342
  "id": "3295ef11",
343
  "metadata": {},
344
  "outputs": [
 
349
  "PILImage mode=RGB size=192x191"
350
  ]
351
  },
352
+ "execution_count": null,
353
  "metadata": {},
354
  "output_type": "execute_result"
355
  }
 
362
  },
363
  {
364
  "cell_type": "code",
365
+ "execution_count": null,
366
  "id": "ae2bc6ac",
367
  "metadata": {},
368
  "outputs": [],
369
  "source": [
370
+ "#|export\n",
371
  "learn = load_learner('model.pkl')"
372
  ]
373
  },
374
  {
375
  "cell_type": "code",
376
+ "execution_count": null,
377
  "id": "6e0bf9da",
378
+ "metadata": {},
 
 
379
  "outputs": [
380
  {
381
  "data": {
 
414
  {
415
  "data": {
416
  "text/plain": [
417
+ "('False', TensorBase(0), TensorBase([9.9999e-01, 8.4523e-06]))"
418
  ]
419
  },
420
+ "execution_count": null,
421
  "metadata": {},
422
  "output_type": "execute_result"
423
  }
 
428
  },
429
  {
430
  "cell_type": "code",
431
+ "execution_count": null,
432
  "id": "0419ed3a",
433
  "metadata": {},
434
  "outputs": [],
435
  "source": [
436
+ "#|export\n",
437
  "categories = ('Dog', 'Cat')\n",
438
  "\n",
439
  "def classify_image(img):\n",
 
443
  },
444
  {
445
  "cell_type": "code",
446
+ "execution_count": null,
447
  "id": "762dec00",
448
  "metadata": {},
449
  "outputs": [
 
484
  {
485
  "data": {
486
  "text/plain": [
487
+ "{'Dog': 0.9999915361404419, 'Cat': 8.452258043689653e-06}"
488
  ]
489
  },
490
+ "execution_count": null,
491
  "metadata": {},
492
  "output_type": "execute_result"
493
  }
 
498
  },
499
  {
500
  "cell_type": "code",
501
+ "execution_count": null,
502
  "id": "0518a30a",
503
+ "metadata": {},
 
 
504
  "outputs": [
505
  {
506
  "name": "stdout",
 
511
  "To create a public link, set `share=True` in `launch()`.\n"
512
  ]
513
  },
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
514
  {
515
  "data": {
516
  "text/plain": [
517
+ "(<fastapi.applications.FastAPI at 0x7fa03ba47670>,\n",
518
  " 'http://127.0.0.1:7860/',\n",
519
  " None)"
520
  ]
521
  },
522
+ "execution_count": null,
523
  "metadata": {},
524
  "output_type": "execute_result"
525
  }
526
  ],
527
  "source": [
528
+ "#|export\n",
529
  "image = gr.inputs.Image(shape=(192, 192))\n",
530
  "label = gr.outputs.Label()\n",
531
  "examples = ['dog.jpg', 'cat.jpg', 'dunno.jpg']\n",
532
  "\n",
533
  "intf = gr.Interface(fn=classify_image, inputs=image, outputs=label, examples=examples)\n",
534
+ "intf.launch(inline=False)"
535
  ]
536
  },
537
  {
538
+ "cell_type": "markdown",
539
+ "id": "0d1e90ce",
 
540
  "metadata": {},
 
541
  "source": [
542
+ "## end -"
 
 
543
  ]
544
  },
545
  {
546
  "cell_type": "code",
547
+ "execution_count": null,
548
+ "id": "82774c08",
549
  "metadata": {},
550
  "outputs": [],
551
  "source": [
552
+ "from nbdev.export import notebook2script"
 
 
 
 
 
 
553
  ]
554
  },
555
  {
556
  "cell_type": "code",
557
+ "execution_count": null,
558
+ "id": "7a880da1",
559
+ "metadata": {},
 
 
560
  "outputs": [
561
  {
562
+ "name": "stdout",
563
+ "output_type": "stream",
564
+ "text": [
565
+ "Converted app.ipynb.\n"
566
+ ]
 
 
 
 
 
 
 
 
 
567
  }
568
  ],
569
  "source": [
570
+ "notebook2script('app.ipynb')"
 
 
571
  ]
572
  },
573
  {
574
  "cell_type": "code",
575
  "execution_count": null,
576
+ "id": "1a349335",
577
  "metadata": {},
578
  "outputs": [],
579
  "source": []
 
584
  "display_name": "Python 3 (ipykernel)",
585
  "language": "python",
586
  "name": "python3"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
587
  }
588
  },
589
  "nbformat": 4,
app.py CHANGED
@@ -24,4 +24,4 @@ label = gr.outputs.Label()
24
  examples = ['dog.jpg', 'cat.jpg', 'dunno.jpg']
25
 
26
  intf = gr.Interface(fn=classify_image, inputs=image, outputs=label, examples=examples)
27
- intf.launch()
 
24
  examples = ['dog.jpg', 'cat.jpg', 'dunno.jpg']
25
 
26
  intf = gr.Interface(fn=classify_image, inputs=image, outputs=label, examples=examples)
27
+ intf.launch(inline=False)
model.pkl CHANGED
@@ -1,3 +1,3 @@
1
  version https://git-lfs.github.com/spec/v1
2
- oid sha256:656a52c3cd69a13f3a23f8c1eefa1333a0b7f0509adc60e1bec6e4f9662a4e22
3
- size 47062571
 
1
  version https://git-lfs.github.com/spec/v1
2
+ oid sha256:46eee60a5eac1402f8402ce1915230e6ca75d8d05c35c4e9500eadf2e39c525a
3
+ size 47062827