quangtohe commited on
Commit
8edd63e
·
1 Parent(s): f381c8d

Upload Untitled8.ipynb

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