jeffreymjohnson commited on
Commit
ca286eb
·
1 Parent(s): 3f8ce24

Add notebook and model

Browse files
Deadpool-Detector.ipynb ADDED
@@ -0,0 +1,399 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "markdown",
5
+ "id": "4b48236b-3e6f-4dec-ac45-5bb8c5f7ff45",
6
+ "metadata": {},
7
+ "source": [
8
+ "<div style=\"font-size: 50px; text-align: center\" >Deadpool™️ Detector</div>\n",
9
+ "\n"
10
+ ]
11
+ },
12
+ {
13
+ "cell_type": "markdown",
14
+ "id": "731ffedc-3131-4d20-bed6-ecfcf0681d73",
15
+ "metadata": {},
16
+ "source": [
17
+ "# Description\n",
18
+ "First development app using deep learning techniques. Stepping stone in my journey.\n",
19
+ "\n",
20
+ "Given an image infer if it contains Deadpool in it."
21
+ ]
22
+ },
23
+ {
24
+ "cell_type": "markdown",
25
+ "id": "1afbc8dd-8b83-4603-8ab8-3724fca03fa3",
26
+ "metadata": {},
27
+ "source": [
28
+ "# Train a model"
29
+ ]
30
+ },
31
+ {
32
+ "cell_type": "markdown",
33
+ "id": "3e2cfb0c-6ac4-4cd3-b66e-1eed809bb88d",
34
+ "metadata": {},
35
+ "source": [
36
+ "## Training Data\n",
37
+ "Using [SH2022 Dataset](https://www.kaggle.com/datasets/muhammadhananasghar/sh2022-dataset) there is a labeled set of Deadpool images."
38
+ ]
39
+ },
40
+ {
41
+ "cell_type": "code",
42
+ "execution_count": null,
43
+ "id": "61850308-6410-412b-a0a3-2a9c8c5ac982",
44
+ "metadata": {},
45
+ "outputs": [],
46
+ "source": [
47
+ "#|default_exp app"
48
+ ]
49
+ },
50
+ {
51
+ "cell_type": "code",
52
+ "execution_count": null,
53
+ "id": "3e665c52-32ba-40a7-b100-480e60d128f6",
54
+ "metadata": {},
55
+ "outputs": [],
56
+ "source": [
57
+ "from fastai.vision.all import *"
58
+ ]
59
+ },
60
+ {
61
+ "cell_type": "code",
62
+ "execution_count": null,
63
+ "id": "6697efd8-df96-4db1-834a-9136c6d71ebb",
64
+ "metadata": {},
65
+ "outputs": [],
66
+ "source": [
67
+ "# copy test data to project if not there already\n",
68
+ "data_path = Path(\"/home/zeus/data/SH2022/\")\n",
69
+ "data_path.ls()"
70
+ ]
71
+ },
72
+ {
73
+ "cell_type": "markdown",
74
+ "id": "04538e6b-bc2e-4ef6-b5b7-3134185ba01c",
75
+ "metadata": {},
76
+ "source": [
77
+ "Load the data"
78
+ ]
79
+ },
80
+ {
81
+ "cell_type": "code",
82
+ "execution_count": null,
83
+ "id": "7e40d0c6-7118-4b31-8304-f0fdb1f3c490",
84
+ "metadata": {},
85
+ "outputs": [],
86
+ "source": [
87
+ "train_files = get_image_files(data_path/\"train\")\n",
88
+ "len(train_files)"
89
+ ]
90
+ },
91
+ {
92
+ "cell_type": "code",
93
+ "execution_count": null,
94
+ "id": "f3e72099-446d-4e7b-8a6d-a6111f6c8556",
95
+ "metadata": {},
96
+ "outputs": [],
97
+ "source": [
98
+ "test_files = get_image_files(data_path/\"test/Deadpool\")\n",
99
+ "len(test_files)"
100
+ ]
101
+ },
102
+ {
103
+ "cell_type": "markdown",
104
+ "id": "2549da81-e133-4f5d-93e2-e754885d9d99",
105
+ "metadata": {},
106
+ "source": [
107
+ "Add data to a `DataLoaders` object and display a batch"
108
+ ]
109
+ },
110
+ {
111
+ "cell_type": "code",
112
+ "execution_count": null,
113
+ "id": "d9fa3c5f-c90f-44bb-bf95-cfbc9698c97e",
114
+ "metadata": {},
115
+ "outputs": [],
116
+ "source": [
117
+ "dls = ImageDataLoaders.from_folder(\n",
118
+ " path=data_path,\n",
119
+ " train='train',\n",
120
+ " valid='test/Deadpool',\n",
121
+ " item_tfms=Resize(224),\n",
122
+ " batch_tfms=aug_transforms()\n",
123
+ ")\n",
124
+ "dls.show_batch()"
125
+ ]
126
+ },
127
+ {
128
+ "cell_type": "markdown",
129
+ "id": "f4b6eb81-25b6-449c-8f48-f418c6e1cb7a",
130
+ "metadata": {},
131
+ "source": [
132
+ "## Training"
133
+ ]
134
+ },
135
+ {
136
+ "cell_type": "code",
137
+ "execution_count": null,
138
+ "id": "54353c83-2bf3-419d-a7f3-24eb6feef718",
139
+ "metadata": {},
140
+ "outputs": [],
141
+ "source": [
142
+ "learn = vision_learner(dls, resnet34, metrics=error_rate)\n",
143
+ "learn.fine_tune(3)"
144
+ ]
145
+ },
146
+ {
147
+ "cell_type": "markdown",
148
+ "id": "1f5afdb5-998b-4b12-b56f-0c89077ae5c2",
149
+ "metadata": {},
150
+ "source": [
151
+ "## Export Trained Model"
152
+ ]
153
+ },
154
+ {
155
+ "cell_type": "code",
156
+ "execution_count": null,
157
+ "id": "22ff7ee5-efc0-4c71-88e6-c1a43b94edba",
158
+ "metadata": {},
159
+ "outputs": [],
160
+ "source": [
161
+ "learn.export('/home/zeus/projects/Deadpool-Detector/deadpool-detection-model.pkl')"
162
+ ]
163
+ },
164
+ {
165
+ "cell_type": "markdown",
166
+ "id": "61b3019d-cd37-49b4-bced-503499796d6c",
167
+ "metadata": {},
168
+ "source": [
169
+ "# Create App"
170
+ ]
171
+ },
172
+ {
173
+ "cell_type": "code",
174
+ "execution_count": 1,
175
+ "id": "f678e4ce-6b99-45b2-ae12-3e3a6978fe0a",
176
+ "metadata": {},
177
+ "outputs": [],
178
+ "source": [
179
+ "#|export\n",
180
+ "from fastai.vision.all import *"
181
+ ]
182
+ },
183
+ {
184
+ "cell_type": "code",
185
+ "execution_count": 2,
186
+ "id": "cebedb5a-32ed-483d-90ce-9ccf101bda42",
187
+ "metadata": {},
188
+ "outputs": [],
189
+ "source": [
190
+ "#|export\n",
191
+ "learn = load_learner('/home/zeus/projects/Deadpool-Detector/deadpool-detection-model.pkl')"
192
+ ]
193
+ },
194
+ {
195
+ "cell_type": "code",
196
+ "execution_count": 3,
197
+ "id": "cc0fa8a2-51fe-4591-9056-e84e0fec9af7",
198
+ "metadata": {},
199
+ "outputs": [],
200
+ "source": [
201
+ "#|export\n",
202
+ "labels = learn.dls.vocab\n",
203
+ "def predict(img):\n",
204
+ " img = PILImage.create(img)\n",
205
+ " pred,pred_idx,probs = learn.predict(img)\n",
206
+ " return {labels[i]: float(probs[i]) for i in range(len(labels))}"
207
+ ]
208
+ },
209
+ {
210
+ "cell_type": "code",
211
+ "execution_count": 7,
212
+ "id": "3cec4455-8159-457b-9563-7e41aa202dc1",
213
+ "metadata": {},
214
+ "outputs": [
215
+ {
216
+ "name": "stdout",
217
+ "output_type": "stream",
218
+ "text": [
219
+ "Running on local URL: http://127.0.0.1:7863\n",
220
+ "Running on public URL: https://40a72959264a859fdc.gradio.live\n",
221
+ "\n",
222
+ "This share link expires in 72 hours. For free permanent hosting and GPU upgrades (NEW!), check out Spaces: https://huggingface.co/spaces\n"
223
+ ]
224
+ },
225
+ {
226
+ "data": {
227
+ "text/html": [
228
+ "<div><iframe src=\"https://40a72959264a859fdc.gradio.live\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
229
+ ],
230
+ "text/plain": [
231
+ "<IPython.core.display.HTML object>"
232
+ ]
233
+ },
234
+ "metadata": {},
235
+ "output_type": "display_data"
236
+ },
237
+ {
238
+ "data": {
239
+ "text/html": [
240
+ "\n",
241
+ "<style>\n",
242
+ " /* Turns off some styling */\n",
243
+ " progress {\n",
244
+ " /* gets rid of default border in Firefox and Opera. */\n",
245
+ " border: none;\n",
246
+ " /* Needs to be in here for Safari polyfill so background images work as expected. */\n",
247
+ " background-size: auto;\n",
248
+ " }\n",
249
+ " progress:not([value]), progress:not([value])::-webkit-progress-bar {\n",
250
+ " background: repeating-linear-gradient(45deg, #7e7e7e, #7e7e7e 10px, #5c5c5c 10px, #5c5c5c 20px);\n",
251
+ " }\n",
252
+ " .progress-bar-interrupted, .progress-bar-interrupted::-webkit-progress-bar {\n",
253
+ " background: #F44336;\n",
254
+ " }\n",
255
+ "</style>\n"
256
+ ],
257
+ "text/plain": [
258
+ "<IPython.core.display.HTML object>"
259
+ ]
260
+ },
261
+ "metadata": {},
262
+ "output_type": "display_data"
263
+ },
264
+ {
265
+ "data": {
266
+ "text/html": [],
267
+ "text/plain": [
268
+ "<IPython.core.display.HTML object>"
269
+ ]
270
+ },
271
+ "metadata": {},
272
+ "output_type": "display_data"
273
+ },
274
+ {
275
+ "data": {
276
+ "text/html": [
277
+ "\n",
278
+ "<style>\n",
279
+ " /* Turns off some styling */\n",
280
+ " progress {\n",
281
+ " /* gets rid of default border in Firefox and Opera. */\n",
282
+ " border: none;\n",
283
+ " /* Needs to be in here for Safari polyfill so background images work as expected. */\n",
284
+ " background-size: auto;\n",
285
+ " }\n",
286
+ " progress:not([value]), progress:not([value])::-webkit-progress-bar {\n",
287
+ " background: repeating-linear-gradient(45deg, #7e7e7e, #7e7e7e 10px, #5c5c5c 10px, #5c5c5c 20px);\n",
288
+ " }\n",
289
+ " .progress-bar-interrupted, .progress-bar-interrupted::-webkit-progress-bar {\n",
290
+ " background: #F44336;\n",
291
+ " }\n",
292
+ "</style>\n"
293
+ ],
294
+ "text/plain": [
295
+ "<IPython.core.display.HTML object>"
296
+ ]
297
+ },
298
+ "metadata": {},
299
+ "output_type": "display_data"
300
+ },
301
+ {
302
+ "data": {
303
+ "text/html": [],
304
+ "text/plain": [
305
+ "<IPython.core.display.HTML object>"
306
+ ]
307
+ },
308
+ "metadata": {},
309
+ "output_type": "display_data"
310
+ },
311
+ {
312
+ "data": {
313
+ "text/html": [
314
+ "\n",
315
+ "<style>\n",
316
+ " /* Turns off some styling */\n",
317
+ " progress {\n",
318
+ " /* gets rid of default border in Firefox and Opera. */\n",
319
+ " border: none;\n",
320
+ " /* Needs to be in here for Safari polyfill so background images work as expected. */\n",
321
+ " background-size: auto;\n",
322
+ " }\n",
323
+ " progress:not([value]), progress:not([value])::-webkit-progress-bar {\n",
324
+ " background: repeating-linear-gradient(45deg, #7e7e7e, #7e7e7e 10px, #5c5c5c 10px, #5c5c5c 20px);\n",
325
+ " }\n",
326
+ " .progress-bar-interrupted, .progress-bar-interrupted::-webkit-progress-bar {\n",
327
+ " background: #F44336;\n",
328
+ " }\n",
329
+ "</style>\n"
330
+ ],
331
+ "text/plain": [
332
+ "<IPython.core.display.HTML object>"
333
+ ]
334
+ },
335
+ "metadata": {},
336
+ "output_type": "display_data"
337
+ },
338
+ {
339
+ "data": {
340
+ "text/html": [],
341
+ "text/plain": [
342
+ "<IPython.core.display.HTML object>"
343
+ ]
344
+ },
345
+ "metadata": {},
346
+ "output_type": "display_data"
347
+ }
348
+ ],
349
+ "source": [
350
+ "#|export\n",
351
+ "import gradio as gr\n",
352
+ "\n",
353
+ "demo = gr.Interface(\n",
354
+ " title=\"Deadpool™️ Detector\",\n",
355
+ " fn=predict,\n",
356
+ " inputs=gr.Image(shape=(200,200)),\n",
357
+ " outputs=gr.Label(num_top_classes=-3),\n",
358
+ " examples=[\"examples/deadpool_example1.jpg\", \"examples/deadpool_example2.jpg\", \"examples/deadpool_example3.jpg\", \"examples/spiderman.jpg\",]\n",
359
+ ").launch(share=true)"
360
+ ]
361
+ },
362
+ {
363
+ "cell_type": "code",
364
+ "execution_count": null,
365
+ "id": "018d3761-beac-4999-b915-32e61297db7c",
366
+ "metadata": {},
367
+ "outputs": [],
368
+ "source": []
369
+ }
370
+ ],
371
+ "metadata": {
372
+ "kernelspec": {
373
+ "display_name": "Python 3 (ipykernel)",
374
+ "language": "python",
375
+ "name": "python3"
376
+ },
377
+ "language_info": {
378
+ "codemirror_mode": {
379
+ "name": "ipython",
380
+ "version": 3
381
+ },
382
+ "file_extension": ".py",
383
+ "mimetype": "text/x-python",
384
+ "name": "python",
385
+ "nbconvert_exporter": "python",
386
+ "pygments_lexer": "ipython3",
387
+ "version": "3.9.16"
388
+ },
389
+ "widgets": {
390
+ "application/vnd.jupyter.widget-state+json": {
391
+ "state": {},
392
+ "version_major": 2,
393
+ "version_minor": 0
394
+ }
395
+ }
396
+ },
397
+ "nbformat": 4,
398
+ "nbformat_minor": 5
399
+ }
deadpool-detection-model.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:8c561ef25820955f456c82e9f355b9676c0d610bc151b5d95b0630ebe7f475cc
3
+ size 88830229