File size: 24,715 Bytes
8edd63e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "l8RrpoC0T1CR",
    "outputId": "5cd7fc04-e75a-4e79-d40d-3bbab858b821"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Collecting gradio\n",
      "  Downloading gradio-3.44.3-py3-none-any.whl (20.2 MB)\n",
      "     ---------------------------------------- 20.2/20.2 MB 5.7 MB/s eta 0:00:00\n",
      "Requirement already satisfied: pandas<3.0,>=1.0 in c:\\users\\admin\\anaconda3\\lib\\site-packages (from gradio) (1.5.3)\n",
      "Requirement already satisfied: numpy~=1.0 in c:\\users\\admin\\anaconda3\\lib\\site-packages (from gradio) (1.23.5)\n",
      "Collecting pydantic!=1.8,!=1.8.1,!=2.0.0,!=2.0.1,<3.0.0,>=1.7.4\n",
      "  Downloading pydantic-2.3.0-py3-none-any.whl (374 kB)\n",
      "     -------------------------------------- 374.5/374.5 kB 5.8 MB/s eta 0:00:00\n",
      "Collecting httpx\n",
      "  Downloading httpx-0.25.0-py3-none-any.whl (75 kB)\n",
      "     ---------------------------------------- 75.7/75.7 kB 4.1 MB/s eta 0:00:00\n",
      "Collecting ffmpy\n",
      "  Downloading ffmpy-0.3.1.tar.gz (5.5 kB)\n",
      "  Preparing metadata (setup.py): started\n",
      "  Preparing metadata (setup.py): finished with status 'done'\n",
      "Collecting uvicorn>=0.14.0\n",
      "  Downloading uvicorn-0.23.2-py3-none-any.whl (59 kB)\n",
      "     ---------------------------------------- 59.5/59.5 kB 1.5 MB/s eta 0:00:00\n",
      "Collecting huggingface-hub>=0.14.0\n",
      "  Downloading huggingface_hub-0.17.1-py3-none-any.whl (294 kB)\n",
      "     -------------------------------------- 294.8/294.8 kB 6.1 MB/s eta 0:00:00\n",
      "Collecting python-multipart\n",
      "  Downloading python_multipart-0.0.6-py3-none-any.whl (45 kB)\n",
      "     ---------------------------------------- 45.7/45.7 kB 2.4 MB/s eta 0:00:00\n",
      "Requirement already satisfied: requests~=2.0 in c:\\users\\admin\\anaconda3\\lib\\site-packages (from gradio) (2.28.1)\n",
      "Requirement already satisfied: typing-extensions~=4.0 in c:\\users\\admin\\anaconda3\\lib\\site-packages (from gradio) (4.4.0)\n",
      "Requirement already satisfied: jinja2<4.0 in c:\\users\\admin\\anaconda3\\lib\\site-packages (from gradio) (3.1.2)\n",
      "Requirement already satisfied: matplotlib~=3.0 in c:\\users\\admin\\anaconda3\\lib\\site-packages (from gradio) (3.7.0)\n",
      "Collecting semantic-version~=2.0\n",
      "  Downloading semantic_version-2.10.0-py2.py3-none-any.whl (15 kB)\n",
      "Collecting aiofiles<24.0,>=22.0\n",
      "  Downloading aiofiles-23.2.1-py3-none-any.whl (15 kB)\n",
      "Requirement already satisfied: packaging in c:\\users\\admin\\anaconda3\\lib\\site-packages (from gradio) (22.0)\n",
      "Collecting websockets<12.0,>=10.0\n",
      "  Downloading websockets-11.0.3-cp310-cp310-win_amd64.whl (124 kB)\n",
      "     -------------------------------------- 124.7/124.7 kB 7.2 MB/s eta 0:00:00\n",
      "Collecting fastapi\n",
      "  Downloading fastapi-0.103.1-py3-none-any.whl (66 kB)\n",
      "     ---------------------------------------- 66.2/66.2 kB ? eta 0:00:00\n",
      "Collecting pydub\n",
      "  Downloading pydub-0.25.1-py2.py3-none-any.whl (32 kB)\n",
      "Requirement already satisfied: pillow<11.0,>=8.0 in c:\\users\\admin\\anaconda3\\lib\\site-packages (from gradio) (9.4.0)\n",
      "Collecting importlib-resources<7.0,>=1.3\n",
      "  Downloading importlib_resources-6.0.1-py3-none-any.whl (34 kB)\n",
      "Requirement already satisfied: pyyaml<7.0,>=5.0 in c:\\users\\admin\\anaconda3\\lib\\site-packages (from gradio) (6.0)\n",
      "Collecting altair<6.0,>=4.2.0\n",
      "  Downloading altair-5.1.1-py3-none-any.whl (520 kB)\n",
      "     -------------------------------------- 520.6/520.6 kB 4.1 MB/s eta 0:00:00\n",
      "Collecting gradio-client==0.5.0\n",
      "  Downloading gradio_client-0.5.0-py3-none-any.whl (298 kB)\n",
      "     -------------------------------------- 298.2/298.2 kB 6.1 MB/s eta 0:00:00\n",
      "Collecting orjson~=3.0\n",
      "  Downloading orjson-3.9.7-cp310-none-win_amd64.whl (134 kB)\n",
      "     -------------------------------------- 134.8/134.8 kB 2.7 MB/s eta 0:00:00\n",
      "Requirement already satisfied: markupsafe~=2.0 in c:\\users\\admin\\anaconda3\\lib\\site-packages (from gradio) (2.1.1)\n",
      "Requirement already satisfied: fsspec in c:\\users\\admin\\anaconda3\\lib\\site-packages (from gradio-client==0.5.0->gradio) (2022.11.0)\n",
      "Requirement already satisfied: jsonschema>=3.0 in c:\\users\\admin\\anaconda3\\lib\\site-packages (from altair<6.0,>=4.2.0->gradio) (4.17.3)\n",
      "Requirement already satisfied: toolz in c:\\users\\admin\\anaconda3\\lib\\site-packages (from altair<6.0,>=4.2.0->gradio) (0.12.0)\n",
      "Requirement already satisfied: tqdm>=4.42.1 in c:\\users\\admin\\anaconda3\\lib\\site-packages (from huggingface-hub>=0.14.0->gradio) (4.64.1)\n",
      "Requirement already satisfied: filelock in c:\\users\\admin\\anaconda3\\lib\\site-packages (from huggingface-hub>=0.14.0->gradio) (3.9.0)\n",
      "Requirement already satisfied: pyparsing>=2.3.1 in c:\\users\\admin\\anaconda3\\lib\\site-packages (from matplotlib~=3.0->gradio) (3.0.9)\n",
      "Requirement already satisfied: fonttools>=4.22.0 in c:\\users\\admin\\anaconda3\\lib\\site-packages (from matplotlib~=3.0->gradio) (4.25.0)\n",
      "Requirement already satisfied: python-dateutil>=2.7 in c:\\users\\admin\\anaconda3\\lib\\site-packages (from matplotlib~=3.0->gradio) (2.8.2)\n",
      "Requirement already satisfied: kiwisolver>=1.0.1 in c:\\users\\admin\\anaconda3\\lib\\site-packages (from matplotlib~=3.0->gradio) (1.4.4)\n",
      "Requirement already satisfied: contourpy>=1.0.1 in c:\\users\\admin\\anaconda3\\lib\\site-packages (from matplotlib~=3.0->gradio) (1.0.5)\n",
      "Requirement already satisfied: cycler>=0.10 in c:\\users\\admin\\anaconda3\\lib\\site-packages (from matplotlib~=3.0->gradio) (0.11.0)\n",
      "Requirement already satisfied: pytz>=2020.1 in c:\\users\\admin\\anaconda3\\lib\\site-packages (from pandas<3.0,>=1.0->gradio) (2022.7)\n",
      "Collecting typing-extensions~=4.0\n",
      "  Downloading typing_extensions-4.7.1-py3-none-any.whl (33 kB)\n",
      "Collecting annotated-types>=0.4.0\n",
      "  Downloading annotated_types-0.5.0-py3-none-any.whl (11 kB)\n",
      "Collecting pydantic-core==2.6.3\n",
      "  Downloading pydantic_core-2.6.3-cp310-none-win_amd64.whl (1.7 MB)\n",
      "     ---------------------------------------- 1.7/1.7 MB 5.0 MB/s eta 0:00:00\n",
      "Requirement already satisfied: urllib3<1.27,>=1.21.1 in c:\\users\\admin\\anaconda3\\lib\\site-packages (from requests~=2.0->gradio) (1.26.14)\n",
      "Requirement already satisfied: certifi>=2017.4.17 in c:\\users\\admin\\anaconda3\\lib\\site-packages (from requests~=2.0->gradio) (2022.12.7)\n",
      "Requirement already satisfied: charset-normalizer<3,>=2 in c:\\users\\admin\\anaconda3\\lib\\site-packages (from requests~=2.0->gradio) (2.0.4)\n",
      "Requirement already satisfied: idna<4,>=2.5 in c:\\users\\admin\\anaconda3\\lib\\site-packages (from requests~=2.0->gradio) (3.4)\n",
      "Requirement already satisfied: click>=7.0 in c:\\users\\admin\\anaconda3\\lib\\site-packages (from uvicorn>=0.14.0->gradio) (8.0.4)\n",
      "Collecting h11>=0.8\n",
      "  Downloading h11-0.14.0-py3-none-any.whl (58 kB)\n",
      "     ---------------------------------------- 58.3/58.3 kB ? eta 0:00:00\n",
      "Collecting starlette<0.28.0,>=0.27.0\n",
      "  Downloading starlette-0.27.0-py3-none-any.whl (66 kB)\n",
      "     ---------------------------------------- 67.0/67.0 kB ? eta 0:00:00\n",
      "Collecting anyio<4.0.0,>=3.7.1\n",
      "  Downloading anyio-3.7.1-py3-none-any.whl (80 kB)\n",
      "     ---------------------------------------- 80.9/80.9 kB ? eta 0:00:00\n",
      "Requirement already satisfied: sniffio in c:\\users\\admin\\anaconda3\\lib\\site-packages (from httpx->gradio) (1.2.0)\n",
      "Collecting httpcore<0.19.0,>=0.18.0\n",
      "  Downloading httpcore-0.18.0-py3-none-any.whl (76 kB)\n",
      "     ---------------------------------------- 76.0/76.0 kB ? eta 0:00:00\n",
      "Collecting exceptiongroup\n",
      "  Downloading exceptiongroup-1.1.3-py3-none-any.whl (14 kB)\n",
      "Requirement already satisfied: colorama in c:\\users\\admin\\anaconda3\\lib\\site-packages (from click>=7.0->uvicorn>=0.14.0->gradio) (0.4.6)\n",
      "Requirement already satisfied: attrs>=17.4.0 in c:\\users\\admin\\anaconda3\\lib\\site-packages (from jsonschema>=3.0->altair<6.0,>=4.2.0->gradio) (22.1.0)\n",
      "Requirement already satisfied: pyrsistent!=0.17.0,!=0.17.1,!=0.17.2,>=0.14.0 in c:\\users\\admin\\anaconda3\\lib\\site-packages (from jsonschema>=3.0->altair<6.0,>=4.2.0->gradio) (0.18.0)\n",
      "Requirement already satisfied: six>=1.5 in c:\\users\\admin\\anaconda3\\lib\\site-packages (from python-dateutil>=2.7->matplotlib~=3.0->gradio) (1.16.0)\n",
      "Building wheels for collected packages: ffmpy\n",
      "  Building wheel for ffmpy (setup.py): started\n",
      "  Building wheel for ffmpy (setup.py): finished with status 'done'\n",
      "  Created wheel for ffmpy: filename=ffmpy-0.3.1-py3-none-any.whl size=5601 sha256=43e9a0a9285c8ed00438117305d007a6856228395941847f06649adf16cc1338\n",
      "  Stored in directory: c:\\users\\admin\\appdata\\local\\pip\\cache\\wheels\\25\\a7\\dd\\c513d5a67b9610660774187483d20314b63e406dd6687e4b4a\n",
      "Successfully built ffmpy\n",
      "Installing collected packages: pydub, ffmpy, websockets, typing-extensions, semantic-version, python-multipart, orjson, importlib-resources, h11, exceptiongroup, annotated-types, aiofiles, uvicorn, pydantic-core, huggingface-hub, anyio, starlette, pydantic, httpcore, altair, httpx, fastapi, gradio-client, gradio\n",
      "  Attempting uninstall: typing-extensions\n",
      "    Found existing installation: typing_extensions 4.4.0\n",
      "    Uninstalling typing_extensions-4.4.0:\n",
      "      Successfully uninstalled typing_extensions-4.4.0\n",
      "  Attempting uninstall: huggingface-hub\n",
      "    Found existing installation: huggingface-hub 0.10.1\n",
      "    Uninstalling huggingface-hub-0.10.1:\n",
      "      Successfully uninstalled huggingface-hub-0.10.1\n",
      "  Attempting uninstall: anyio\n",
      "    Found existing installation: anyio 3.5.0\n",
      "    Uninstalling anyio-3.5.0:\n",
      "      Successfully uninstalled anyio-3.5.0\n",
      "Successfully installed aiofiles-23.2.1 altair-5.1.1 annotated-types-0.5.0 anyio-3.7.1 exceptiongroup-1.1.3 fastapi-0.103.1 ffmpy-0.3.1 gradio-3.44.3 gradio-client-0.5.0 h11-0.14.0 httpcore-0.18.0 httpx-0.25.0 huggingface-hub-0.17.1 importlib-resources-6.0.1 orjson-3.9.7 pydantic-2.3.0 pydantic-core-2.6.3 pydub-0.25.1 python-multipart-0.0.6 semantic-version-2.10.0 starlette-0.27.0 typing-extensions-4.7.1 uvicorn-0.23.2 websockets-11.0.3\n"
     ]
    }
   ],
   "source": [
    "!pip install gradio"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "id": "STRRMSZCVpt9"
   },
   "outputs": [
    {
     "ename": "ModuleNotFoundError",
     "evalue": "No module named 'tensorflow'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mModuleNotFoundError\u001b[0m                       Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[2], line 5\u001b[0m\n\u001b[0;32m      3\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;21;01mseaborn\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m \u001b[38;5;21;01msns\u001b[39;00m\n\u001b[0;32m      4\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mPIL\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m Image\n\u001b[1;32m----> 5\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;21;01mtensorflow\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m \u001b[38;5;21;01mtf\u001b[39;00m\n\u001b[0;32m      6\u001b[0m tf\u001b[38;5;241m.\u001b[39mrandom\u001b[38;5;241m.\u001b[39mset_seed(\u001b[38;5;241m3\u001b[39m)\n\u001b[0;32m      7\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mtensorflow\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m keras\n",
      "\u001b[1;31mModuleNotFoundError\u001b[0m: No module named 'tensorflow'"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "import seaborn as sns\n",
    "from PIL import Image\n",
    "import tensorflow as tf\n",
    "tf.random.set_seed(3)\n",
    "from tensorflow import keras\n",
    "from keras.datasets import mnist\n",
    "from tensorflow.math import confusion_matrix"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "6iPPGLvvaMGU"
   },
   "source": [
    "1. Data understanding:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "e48RTLHEV6d-"
   },
   "outputs": [],
   "source": [
    "(x_train,y_train), (x_test,y_test) = mnist.load_data()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "xPZiE4UqV8N8",
    "outputId": "5550b2d7-7d35-4f5f-fb3f-4dd12815e88f"
   },
   "outputs": [],
   "source": [
    "type(x_train)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "CTDRcRsFXaX0",
    "outputId": "bed93371-bfb4-49c5-be69-4c6223896bd2"
   },
   "outputs": [],
   "source": [
    "# shape of the numpy arrays\n",
    "print(x_train.shape, y_train.shape, x_test.shape, y_test.shape)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "rr9jdm31nBxK"
   },
   "source": [
    "Training data = 60,000 Images\n",
    "\n",
    "Test data = 10,000 Images"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "Kpu_bnGYX_TC",
    "outputId": "230759a2-2bf8-4f6c-80f9-50a64f2e68f8"
   },
   "outputs": [],
   "source": [
    "# printing the 10th image\n",
    "print(x_train[10])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "AKBkeVeTaajW",
    "outputId": "f3f07e79-6e5c-4b72-8060-d776e9153e3c"
   },
   "outputs": [],
   "source": [
    "# unique values in Y_train\n",
    "print(np.unique(y_train))\n",
    "\n",
    "# unique values in Y_test\n",
    "print(np.unique(y_test))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 443
    },
    "id": "RgGYRF-RX_ZS",
    "outputId": "a415eefd-0a0c-4510-fb5d-0d8c7d71160b"
   },
   "outputs": [],
   "source": [
    "plt.xticks([])\n",
    "plt.yticks([])\n",
    "plt.xlabel([y_train[10]])\n",
    "plt.imshow(x_train[10],cmap=plt.cm.binary)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 443
    },
    "id": "ZdJAi1Gwm38K",
    "outputId": "a8fbc8be-69b9-432d-8128-39897a5685b5"
   },
   "outputs": [],
   "source": [
    "plt.xticks([])\n",
    "plt.yticks([])\n",
    "plt.xlabel([y_test[10]])\n",
    "plt.imshow(x_test[10],cmap=plt.cm.binary)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "FP_-vUHTaf3e"
   },
   "source": [
    "2. Data prepocessing"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "_UAPumftLfLi"
   },
   "outputs": [],
   "source": [
    "def pixel_normalization(image):\n",
    "    return (image / 255)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "llckMTh-aA-5"
   },
   "outputs": [],
   "source": [
    "from keras.src.utils.np_utils import to_categorical\n",
    "x_train = pixel_normalization(x_train)\n",
    "x_test = pixel_normalization(x_test)\n",
    "y_train = to_categorical(y_train)\n",
    "y_test = to_categorical(y_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "OpybamzzbAMJ"
   },
   "outputs": [],
   "source": [
    "x_train = np.expand_dims(x_train, axis=3)\n",
    "x_test = np.expand_dims(x_test, axis=3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "njgeqOFAbUOw",
    "outputId": "add1008c-e9fe-4e80-de6b-2c1980034b20"
   },
   "outputs": [],
   "source": [
    "x_train.shape"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "8PCTOFTVcJ_3"
   },
   "source": [
    "3. Data modeling and evaluation"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "TOaAB5O-cvyg"
   },
   "outputs": [],
   "source": [
    "from tensorflow.keras.models import Sequential, save_model, load_model\n",
    "from tensorflow.keras.layers import Conv2D, MaxPool2D, Flatten, Dense, Dropout\n",
    "from tensorflow.keras.utils import to_categorical, plot_model\n",
    "from tensorflow.keras.losses import CategoricalCrossentropy\n",
    "from tensorflow.keras.optimizers import Adam\n",
    "from tensorflow.keras.metrics import CategoricalAccuracy\n",
    "from tensorflow.keras.callbacks import ModelCheckpoint, ReduceLROnPlateau\n",
    "import warnings"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 1000
    },
    "id": "j1pps_dycSOo",
    "outputId": "dc42ec5d-0d08-4987-df71-e7a7201f5f34"
   },
   "outputs": [],
   "source": [
    "\n",
    "cnn = Sequential()\n",
    "cnn.add(Conv2D(filters=32, kernel_size=(3, 3), padding='same', strides=(1,1), activation='relu', input_shape=(28, 28, 1)))\n",
    "cnn.add(Dropout(.2))\n",
    "cnn.add(MaxPool2D(pool_size=(2, 2), strides=(2, 2)))\n",
    "cnn.add(Conv2D(filters=64, kernel_size=(3, 3), padding='same', strides=(1,1), activation='relu'))\n",
    "cnn.add(Dropout(.2))\n",
    "cnn.add(MaxPool2D(pool_size=(2, 2), strides=(2, 2)))\n",
    "cnn.add(Flatten())\n",
    "cnn.add(Dense(units=128, activation='relu'))\n",
    "cnn.add(Dropout(.5))\n",
    "cnn.add(Dense(units=10, activation='softmax'))\n",
    "\n",
    "plot_model(cnn, show_shapes=True, show_layer_activations=True, show_layer_names=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "QumZFXdt0L0W"
   },
   "outputs": [],
   "source": [
    "cnn.compile(loss='sparse_categorical_crossentropy',\n",
    "             optimizer='adam',\n",
    "             metrics=['accuracy'])\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "XPtSkHm2dTJ_",
    "outputId": "aab669a0-e6bb-4665-cd9a-f8ce2e596eec"
   },
   "outputs": [],
   "source": [
    "cnn.compile(optimizer=Adam(learning_rate=0.001),\n",
    "            loss=CategoricalCrossentropy(from_logits=True),\n",
    "            metrics=CategoricalAccuracy())\n",
    "\n",
    "reduce_lr = ReduceLROnPlateau(monitor='val_loss', factor=0.2, patience=3, min_lr=0.00001)\n",
    "checkpoint = ModelCheckpoint(filepath='/content/modelCNN.h5', monitor='val_categorical_accuracy', save_best_only=True)\n",
    "history = cnn.fit(x_train, y_train, epochs=10, batch_size=200, validation_data=(x_test, y_test), callbacks=[checkpoint, reduce_lr])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "Frx0P5XGl9ex",
    "outputId": "e4db9493-2a35-4998-9edc-4d9237c48311"
   },
   "outputs": [],
   "source": [
    "# Evaluate the model on test set\n",
    "score = cnn.evaluate(x_test, y_test, verbose=0)\n",
    "# Print test accuracy\n",
    "print('\\n', 'Test accuracy:', score[1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "t-xECl76mFCp",
    "outputId": "fb5c590a-b578-4cd7-d585-871c001fcd61"
   },
   "outputs": [],
   "source": [
    "y_pred = cnn.predict(x_test)\n",
    "y_pred[10]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "xru7Tc7fmaxE",
    "outputId": "2b5001f6-d823-40bb-98c7-2d679bb19c43"
   },
   "outputs": [],
   "source": [
    "np.argmax(y_pred[10])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "ac9ChxzKoNla"
   },
   "outputs": [],
   "source": [
    "predictions = []\n",
    "for instance in y_pred:\n",
    "    predictions.append(np.argmax(instance))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 687
    },
    "id": "yP7wrcg9oZBZ",
    "outputId": "15dc3ce3-36da-4e24-924f-7a390758716b"
   },
   "outputs": [],
   "source": [
    "plt.figure(figsize=(9, 9))\n",
    "plt.plot(history.history['loss'], color='blue', label='loss');\n",
    "plt.plot(history.history['val_loss'], color='red', label='val_loss');\n",
    "plt.legend();\n",
    "plt.title('Loss vs Validation Loss');\n",
    "plt.tight_layout()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 687
    },
    "id": "nev_C6ujoriT",
    "outputId": "03cb7757-a390-40fc-f8cb-99e99209a685"
   },
   "outputs": [],
   "source": [
    "plt.figure(figsize=(9, 9))\n",
    "plt.plot(history.history['categorical_accuracy'], color='blue', label='accuracy');\n",
    "plt.plot(history.history['val_categorical_accuracy'], color='red', label='val_accuracy');\n",
    "plt.legend();\n",
    "plt.title('Accuracy vs Validation Accuracy');\n",
    "plt.tight_layout()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "fD6XSaK-pP9Y"
   },
   "source": [
    "4. Bulid an Interface app by using gradio"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "e2WFs3QqpqDR"
   },
   "outputs": [],
   "source": [
    "import gradio as gr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "O0BAm6XtqFJI"
   },
   "outputs": [],
   "source": [
    "loaded_CNN = keras.models.load_model('/content/modelCNN.h5')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 611
    },
    "id": "C0dt-NchpZTR",
    "outputId": "e5ae8672-ad8d-4674-e74a-ce197579fa09"
   },
   "outputs": [],
   "source": [
    "def predict(img):\n",
    "    img_array = np.array(img)\n",
    "    img_array = img_array.reshape(1, 28, 28)\n",
    "    img_array = img_array/255\n",
    "    pred = loaded_CNN.predict(img_array)\n",
    "    print(pred)\n",
    "    return np.argmax(pred)\n",
    "\n",
    "iface = gr.Interface(predict, inputs = 'sketchpad',\n",
    "                     outputs = 'text',\n",
    "                     allow_flagging = 'never',\n",
    "                     description = 'Draw a Digit Below... (Draw in the centre for best results)')\n",
    "iface.launch(share = True, width = 300, height = 500)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "colab": {
   "provenance": []
  },
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}