noshot 12.0.0__py3-none-any.whl → 13.0.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- noshot/data/DLE FSD BDA/DLE/DLE 2 (tim stan s)/1.1 DNN (Pytorch).ipynb +164 -0
- noshot/data/DLE FSD BDA/DLE/DLE 2 (tim stan s)/1.2 DNN (Tensorflow).ipynb +94 -0
- noshot/data/DLE FSD BDA/DLE/DLE 2 (tim stan s)/1.3 DNN (Image Classification).ipynb +134 -0
- noshot/data/DLE FSD BDA/DLE/DLE 2 (tim stan s)/2.1 DNN vs CNN.ipynb +127 -0
- noshot/data/DLE FSD BDA/DLE/DLE 2 (tim stan s)/2.2 DNN vs CNN.ipynb +123 -0
- noshot/data/DLE FSD BDA/DLE/DLE 2 (tim stan s)/4. FCNN (Image Segmentation).ipynb +108 -0
- noshot/data/DLE FSD BDA/DLE/DLE 2 (tim stan s)/Lab Excercise (Training DNN).ipynb +646 -0
- noshot/data/DLE FSD BDA/DLE/DLE 2 (tim stan s)/Load-Images.ipynb +553 -0
- noshot/data/DLE FSD BDA/DLE/DLE 3 (sonic boy)/Ex1.ipynb +216 -0
- noshot/data/DLE FSD BDA/DLE/DLE 3 (sonic boy)/Ex2.ipynb +195 -0
- noshot/data/DLE FSD BDA/DLE/DLE 3 (sonic boy)/Ex3.ipynb +427 -0
- noshot/data/DLE FSD BDA/DLE/DLE 3 (sonic boy)/Ex4.ipynb +186 -0
- noshot/data/DLE FSD BDA/DLE/DLE 4 (senior)/Exp01/DNN Ex No 1.ipynb +398 -0
- noshot/data/DLE FSD BDA/DLE/DLE 4 (senior)/Exp01/Ex No 1 Build in dataset.ipynb +171 -0
- noshot/data/DLE FSD BDA/DLE/DLE 4 (senior)/Exp01/Exp1-Short-DL_ANN_ImageClassification.ipynb +401 -0
- noshot/data/DLE FSD BDA/DLE/DLE 4 (senior)/Exp01/OR GATE .ipynb +8511 -0
- noshot/data/DLE FSD BDA/DLE/DLE 4 (senior)/Exp02/Exp2-Short-DL_CNN_ImageClassification.ipynb +737 -0
- noshot/data/DLE FSD BDA/DLE/DLE 4 (senior)/Exp03/DL-Ex3-RNN.ipynb +591 -0
- noshot/data/DLE FSD BDA/DLE/DLE 4 (senior)/Exp04/Ex no 4.ipynb +551 -0
- {noshot-12.0.0.dist-info → noshot-13.0.0.dist-info}/METADATA +1 -1
- noshot-13.0.0.dist-info/RECORD +32 -0
- noshot-12.0.0.dist-info/RECORD +0 -13
- /noshot/data/DLE FSD BDA/DLE/{1. DNN (Image Classification).ipynb → DLE 1 (Json)/1. DNN (Image Classification).ipynb} +0 -0
- /noshot/data/DLE FSD BDA/DLE/{2. DNN vs CNN.ipynb → DLE 1 (Json)/2. DNN vs CNN.ipynb} +0 -0
- /noshot/data/DLE FSD BDA/DLE/{3. CNN (Object Detecrion).ipynb → DLE 1 (Json)/3. CNN (Object Detecrion).ipynb} +0 -0
- /noshot/data/DLE FSD BDA/DLE/{4. FCN (Image Segmentaion).ipynb → DLE 1 (Json)/4. FCN (Image Segmentaion).ipynb} +0 -0
- {noshot-12.0.0.dist-info → noshot-13.0.0.dist-info}/WHEEL +0 -0
- {noshot-12.0.0.dist-info → noshot-13.0.0.dist-info}/licenses/LICENSE.txt +0 -0
- {noshot-12.0.0.dist-info → noshot-13.0.0.dist-info}/top_level.txt +0 -0
@@ -0,0 +1,737 @@
|
|
1
|
+
{
|
2
|
+
"cells": [
|
3
|
+
{
|
4
|
+
"cell_type": "markdown",
|
5
|
+
"id": "6b08756f",
|
6
|
+
"metadata": {},
|
7
|
+
"source": [
|
8
|
+
"# Apply convolutional neural network for the same image classification dataset and compare DNN and CNN in terms of parameters and performance"
|
9
|
+
]
|
10
|
+
},
|
11
|
+
{
|
12
|
+
"cell_type": "code",
|
13
|
+
"execution_count": null,
|
14
|
+
"id": "a0bfab06-b12f-46b6-818f-a9ccddb17f74",
|
15
|
+
"metadata": {
|
16
|
+
"id": "a0bfab06-b12f-46b6-818f-a9ccddb17f74",
|
17
|
+
"outputId": "7fce260d-6542-45ae-9331-ef2fbd3276ed"
|
18
|
+
},
|
19
|
+
"outputs": [],
|
20
|
+
"source": [
|
21
|
+
"!pip install Kaggle\n",
|
22
|
+
"!pip install opendatasets\n",
|
23
|
+
"import opendatasets as od\n",
|
24
|
+
"import pandas\n",
|
25
|
+
"od.download(\n",
|
26
|
+
" \"https://www.kaggle.com/datasets/prakharrastogi534/bean-leaf-dataset/data\",force=True)"
|
27
|
+
]
|
28
|
+
},
|
29
|
+
{
|
30
|
+
"cell_type": "code",
|
31
|
+
"execution_count": null,
|
32
|
+
"id": "83ef5252-c7dc-480c-b6e1-999465440421",
|
33
|
+
"metadata": {
|
34
|
+
"id": "83ef5252-c7dc-480c-b6e1-999465440421",
|
35
|
+
"outputId": "a53c12e9-9ec1-4dfc-846b-1fbb826ab20a"
|
36
|
+
},
|
37
|
+
"outputs": [],
|
38
|
+
"source": [
|
39
|
+
"!pip install tensorflow"
|
40
|
+
]
|
41
|
+
},
|
42
|
+
{
|
43
|
+
"cell_type": "code",
|
44
|
+
"execution_count": null,
|
45
|
+
"id": "08d18c5b-917f-44eb-8051-d23126b7770a",
|
46
|
+
"metadata": {
|
47
|
+
"id": "08d18c5b-917f-44eb-8051-d23126b7770a",
|
48
|
+
"outputId": "151c6f85-1b99-479b-a14b-8201af5f547f"
|
49
|
+
},
|
50
|
+
"outputs": [],
|
51
|
+
"source": [
|
52
|
+
"import tensorflow as tf\n",
|
53
|
+
"import tensorflow as tf\n",
|
54
|
+
"from tensorflow.keras import layers, models\n",
|
55
|
+
"from tensorflow.keras.preprocessing.image import ImageDataGenerator\n",
|
56
|
+
"import matplotlib.pyplot as plt\n",
|
57
|
+
"import seaborn as sns\n",
|
58
|
+
"import numpy as np\n",
|
59
|
+
"from sklearn.metrics import classification_report, confusion_matrix"
|
60
|
+
]
|
61
|
+
},
|
62
|
+
{
|
63
|
+
"cell_type": "code",
|
64
|
+
"execution_count": null,
|
65
|
+
"id": "d0f0c066-4d98-43f4-9ece-f12da6c4ff94",
|
66
|
+
"metadata": {
|
67
|
+
"id": "d0f0c066-4d98-43f4-9ece-f12da6c4ff94"
|
68
|
+
},
|
69
|
+
"outputs": [],
|
70
|
+
"source": [
|
71
|
+
"train_dir=r'C:\\Users\\User\\Documents\\Jupyternotebookprgs\\bean-leaf-dataset\\train\\train'\n",
|
72
|
+
"validation_dir=r'C:\\Users\\User\\Documents\\Jupyternotebookprgs\\bean-leaf-dataset\\validation\\validation'\n",
|
73
|
+
"test_dir=r'C:\\Users\\User\\Documents\\Jupyternotebookprgs\\bean-leaf-dataset\\test\\test'"
|
74
|
+
]
|
75
|
+
},
|
76
|
+
{
|
77
|
+
"cell_type": "code",
|
78
|
+
"execution_count": null,
|
79
|
+
"id": "be68a49f-7284-4481-a776-742218a276a7",
|
80
|
+
"metadata": {
|
81
|
+
"id": "be68a49f-7284-4481-a776-742218a276a7"
|
82
|
+
},
|
83
|
+
"outputs": [],
|
84
|
+
"source": [
|
85
|
+
"train_datagen = ImageDataGenerator(\n",
|
86
|
+
" rescale=1./255,\n",
|
87
|
+
" rotation_range=40,\n",
|
88
|
+
" width_shift_range=0.2,\n",
|
89
|
+
" height_shift_range=0.2,\n",
|
90
|
+
" shear_range=0.2,\n",
|
91
|
+
" zoom_range=0.2,\n",
|
92
|
+
" horizontal_flip=True,\n",
|
93
|
+
" fill_mode='nearest'\n",
|
94
|
+
")"
|
95
|
+
]
|
96
|
+
},
|
97
|
+
{
|
98
|
+
"cell_type": "code",
|
99
|
+
"execution_count": null,
|
100
|
+
"id": "25f04809-f073-4748-a87c-0771b6b89f9a",
|
101
|
+
"metadata": {
|
102
|
+
"id": "25f04809-f073-4748-a87c-0771b6b89f9a"
|
103
|
+
},
|
104
|
+
"outputs": [],
|
105
|
+
"source": [
|
106
|
+
"validation_datagen = ImageDataGenerator(rescale=1./255)\n",
|
107
|
+
"test_datagen = ImageDataGenerator(rescale=1./255)"
|
108
|
+
]
|
109
|
+
},
|
110
|
+
{
|
111
|
+
"cell_type": "code",
|
112
|
+
"execution_count": null,
|
113
|
+
"id": "36a6ed4a-e961-47e8-9225-292e7298cb8d",
|
114
|
+
"metadata": {
|
115
|
+
"id": "36a6ed4a-e961-47e8-9225-292e7298cb8d",
|
116
|
+
"outputId": "ae5a6d17-8f5e-4536-86ce-c52fb059f097"
|
117
|
+
},
|
118
|
+
"outputs": [],
|
119
|
+
"source": [
|
120
|
+
"train_generator = train_datagen.flow_from_directory(\n",
|
121
|
+
" train_dir, target_size=(150, 150), batch_size=32, class_mode='sparse'\n",
|
122
|
+
")"
|
123
|
+
]
|
124
|
+
},
|
125
|
+
{
|
126
|
+
"cell_type": "code",
|
127
|
+
"execution_count": null,
|
128
|
+
"id": "a0e150d9-8057-4c07-be06-966bbe6e26a3",
|
129
|
+
"metadata": {
|
130
|
+
"id": "a0e150d9-8057-4c07-be06-966bbe6e26a3",
|
131
|
+
"outputId": "a77a76ff-fd24-451f-a18e-5e0122b86bfc"
|
132
|
+
},
|
133
|
+
"outputs": [],
|
134
|
+
"source": [
|
135
|
+
"validation_generator = validation_datagen.flow_from_directory(\n",
|
136
|
+
" validation_dir, target_size=(150, 150), batch_size=32, class_mode='sparse'\n",
|
137
|
+
")"
|
138
|
+
]
|
139
|
+
},
|
140
|
+
{
|
141
|
+
"cell_type": "code",
|
142
|
+
"execution_count": null,
|
143
|
+
"id": "4cef37dc-3af1-49c3-9f7d-134687400a01",
|
144
|
+
"metadata": {
|
145
|
+
"id": "4cef37dc-3af1-49c3-9f7d-134687400a01",
|
146
|
+
"outputId": "25405fdf-d8c0-4b68-9159-707f94ce98d0"
|
147
|
+
},
|
148
|
+
"outputs": [],
|
149
|
+
"source": [
|
150
|
+
"test_generator = test_datagen.flow_from_directory(\n",
|
151
|
+
" test_dir, target_size=(150, 150), batch_size=32, class_mode='sparse', shuffle=False\n",
|
152
|
+
")\n"
|
153
|
+
]
|
154
|
+
},
|
155
|
+
{
|
156
|
+
"cell_type": "code",
|
157
|
+
"execution_count": null,
|
158
|
+
"id": "5139b795",
|
159
|
+
"metadata": {
|
160
|
+
"id": "5139b795",
|
161
|
+
"outputId": "67351a99-3612-4fee-d795-791b25efe8bc"
|
162
|
+
},
|
163
|
+
"outputs": [],
|
164
|
+
"source": [
|
165
|
+
"class_names = list(test_generator.class_indices.keys())\n",
|
166
|
+
"class_names"
|
167
|
+
]
|
168
|
+
},
|
169
|
+
{
|
170
|
+
"cell_type": "code",
|
171
|
+
"execution_count": null,
|
172
|
+
"id": "2ee841f4-6d52-4aeb-8db4-1ccd907d7b7a",
|
173
|
+
"metadata": {
|
174
|
+
"id": "2ee841f4-6d52-4aeb-8db4-1ccd907d7b7a",
|
175
|
+
"outputId": "bdba5bd7-949e-4031-8e00-0c2ddb03e1e1"
|
176
|
+
},
|
177
|
+
"outputs": [],
|
178
|
+
"source": [
|
179
|
+
"model = models.Sequential([\n",
|
180
|
+
" layers.Flatten(input_shape=(150, 150, 3)), # Flatten the input (150, 150, 3) to 150*150*3 = 67500\n",
|
181
|
+
" layers.Dense(512, activation='relu'),\n",
|
182
|
+
" layers.Dense(256, activation='relu'),\n",
|
183
|
+
" layers.Dense(128, activation='relu'),\n",
|
184
|
+
" layers.Dense(len(class_names), activation='softmax')\n",
|
185
|
+
"])"
|
186
|
+
]
|
187
|
+
},
|
188
|
+
{
|
189
|
+
"cell_type": "code",
|
190
|
+
"execution_count": null,
|
191
|
+
"id": "7cb22179-4433-448e-9335-9690bfb2bf1e",
|
192
|
+
"metadata": {
|
193
|
+
"id": "7cb22179-4433-448e-9335-9690bfb2bf1e",
|
194
|
+
"outputId": "f180ed05-2cd1-4de1-ff18-c906bb84f4d4"
|
195
|
+
},
|
196
|
+
"outputs": [],
|
197
|
+
"source": [
|
198
|
+
"model.compile(\n",
|
199
|
+
" optimizer='adam',\n",
|
200
|
+
" loss='sparse_categorical_crossentropy',\n",
|
201
|
+
" metrics=['accuracy']\n",
|
202
|
+
")"
|
203
|
+
]
|
204
|
+
},
|
205
|
+
{
|
206
|
+
"cell_type": "code",
|
207
|
+
"execution_count": null,
|
208
|
+
"id": "5750b6e9",
|
209
|
+
"metadata": {
|
210
|
+
"id": "5750b6e9",
|
211
|
+
"outputId": "b2d7aef0-9570-461a-bf6c-abf8fefe4f5e"
|
212
|
+
},
|
213
|
+
"outputs": [],
|
214
|
+
"source": [
|
215
|
+
"history = model.fit(\n",
|
216
|
+
" train_generator,\n",
|
217
|
+
" epochs=5,\n",
|
218
|
+
" validation_data=validation_generator,\n",
|
219
|
+
"\n",
|
220
|
+
")"
|
221
|
+
]
|
222
|
+
},
|
223
|
+
{
|
224
|
+
"cell_type": "code",
|
225
|
+
"execution_count": null,
|
226
|
+
"id": "5a313b75",
|
227
|
+
"metadata": {
|
228
|
+
"id": "5a313b75",
|
229
|
+
"outputId": "a76bd934-a9d3-4f5f-927d-0d177cb5f309"
|
230
|
+
},
|
231
|
+
"outputs": [],
|
232
|
+
"source": [
|
233
|
+
"test_loss, test_acc = model.evaluate(test_generator, steps=test_generator.samples // test_generator.batch_size)\n",
|
234
|
+
"print(f'Test accuracy: {test_acc}')"
|
235
|
+
]
|
236
|
+
},
|
237
|
+
{
|
238
|
+
"cell_type": "code",
|
239
|
+
"execution_count": null,
|
240
|
+
"id": "3cb43468",
|
241
|
+
"metadata": {
|
242
|
+
"id": "3cb43468",
|
243
|
+
"outputId": "90b54c16-a4b9-40ce-daa1-5246caa8e3ed"
|
244
|
+
},
|
245
|
+
"outputs": [],
|
246
|
+
"source": [
|
247
|
+
"Y_pred = model.predict(test_generator, steps=test_generator.samples // test_generator.batch_size + 1)\n",
|
248
|
+
"y_pred = np.argmax(Y_pred, axis=1)"
|
249
|
+
]
|
250
|
+
},
|
251
|
+
{
|
252
|
+
"cell_type": "code",
|
253
|
+
"execution_count": null,
|
254
|
+
"id": "6e03f8b6",
|
255
|
+
"metadata": {
|
256
|
+
"id": "6e03f8b6",
|
257
|
+
"outputId": "0239f1fa-e1fd-46dc-ea30-3465080b2749"
|
258
|
+
},
|
259
|
+
"outputs": [],
|
260
|
+
"source": [
|
261
|
+
"print(classification_report(y_true, y_pred, target_names=class_names))"
|
262
|
+
]
|
263
|
+
},
|
264
|
+
{
|
265
|
+
"cell_type": "code",
|
266
|
+
"execution_count": null,
|
267
|
+
"id": "648bda44",
|
268
|
+
"metadata": {
|
269
|
+
"id": "648bda44"
|
270
|
+
},
|
271
|
+
"outputs": [],
|
272
|
+
"source": []
|
273
|
+
},
|
274
|
+
{
|
275
|
+
"cell_type": "code",
|
276
|
+
"execution_count": null,
|
277
|
+
"id": "7eb184d5",
|
278
|
+
"metadata": {
|
279
|
+
"id": "7eb184d5"
|
280
|
+
},
|
281
|
+
"outputs": [],
|
282
|
+
"source": [
|
283
|
+
"CNN_model = models.Sequential([\n",
|
284
|
+
" layers.Conv2D(32, (3, 3), activation='relu', input_shape=(150, 150, 3)),\n",
|
285
|
+
" layers.MaxPooling2D((2, 2)),\n",
|
286
|
+
" layers.Conv2D(64, (3, 3), activation='relu'),\n",
|
287
|
+
" layers.MaxPooling2D((2, 2)),\n",
|
288
|
+
" layers.Conv2D(128, (3, 3), activation='relu'),\n",
|
289
|
+
" layers.MaxPooling2D((2, 2)),\n",
|
290
|
+
" layers.Conv2D(128, (3, 3), activation='relu'),\n",
|
291
|
+
" layers.MaxPooling2D((2, 2)),\n",
|
292
|
+
" layers.Flatten(),\n",
|
293
|
+
" layers.Dense(512, activation='relu'),\n",
|
294
|
+
" layers.Dense(len(class_names), activation='softmax')\n",
|
295
|
+
"])\n"
|
296
|
+
]
|
297
|
+
},
|
298
|
+
{
|
299
|
+
"cell_type": "markdown",
|
300
|
+
"id": "d391e18f",
|
301
|
+
"metadata": {
|
302
|
+
"id": "d391e18f"
|
303
|
+
},
|
304
|
+
"source": [
|
305
|
+
"Visualize the Feature Map"
|
306
|
+
]
|
307
|
+
},
|
308
|
+
{
|
309
|
+
"cell_type": "code",
|
310
|
+
"execution_count": null,
|
311
|
+
"id": "fa0a18bc",
|
312
|
+
"metadata": {
|
313
|
+
"id": "fa0a18bc"
|
314
|
+
},
|
315
|
+
"outputs": [],
|
316
|
+
"source": [
|
317
|
+
"from tensorflow.keras.models import Model\n",
|
318
|
+
"\n",
|
319
|
+
"# Specify the layers to visualize\n",
|
320
|
+
"layer_names = [\n",
|
321
|
+
" 'conv2d', # 1st Conv Layer\n",
|
322
|
+
" 'max_pooling2d', # 1st MaxPooling Layer\n",
|
323
|
+
" 'conv2d_1', # 2nd Conv Layer\n",
|
324
|
+
" 'max_pooling2d_1', # 2nd MaxPooling Layer\n",
|
325
|
+
" 'conv2d_2', # 3rd Conv Layer\n",
|
326
|
+
" 'max_pooling2d_2', # 3rd MaxPooling Layer\n",
|
327
|
+
" 'conv2d_3', # 4th Conv Layer\n",
|
328
|
+
" 'max_pooling2d_3' # 4th MaxPooling Layer\n",
|
329
|
+
"]\n",
|
330
|
+
"\n",
|
331
|
+
"# Create a new model that outputs the feature maps\n",
|
332
|
+
"layer_outputs = [CNN_model.get_layer(name).output for name in layer_names]\n",
|
333
|
+
"feature_map_model = Model(inputs=CNN_model.input, outputs=layer_outputs)\n"
|
334
|
+
]
|
335
|
+
},
|
336
|
+
{
|
337
|
+
"cell_type": "code",
|
338
|
+
"execution_count": null,
|
339
|
+
"id": "7ae275da",
|
340
|
+
"metadata": {
|
341
|
+
"id": "7ae275da"
|
342
|
+
},
|
343
|
+
"outputs": [],
|
344
|
+
"source": [
|
345
|
+
"from tensorflow.keras.preprocessing import image\n",
|
346
|
+
"import numpy as np\n",
|
347
|
+
"\n",
|
348
|
+
"img_path = r'C:\\Users\\User\\Documents\\Jupyternotebookprgs\\bean-leaf-dataset\\train\\train\\angular_leaf_spot\\angular_leaf_spot_train.0.jpg' # Update this path\n",
|
349
|
+
"img = image.load_img(img_path, target_size=(150, 150))\n",
|
350
|
+
"img_array = image.img_to_array(img)\n",
|
351
|
+
"img_array = np.expand_dims(img_array, axis=0) # Add batch dimension\n",
|
352
|
+
"img_array /= 255.0 # Normalize the image\n"
|
353
|
+
]
|
354
|
+
},
|
355
|
+
{
|
356
|
+
"cell_type": "code",
|
357
|
+
"execution_count": null,
|
358
|
+
"id": "9a989eba",
|
359
|
+
"metadata": {
|
360
|
+
"id": "9a989eba",
|
361
|
+
"outputId": "cb4d70d0-204b-4ca2-93e9-b3e1cc713dbe"
|
362
|
+
},
|
363
|
+
"outputs": [],
|
364
|
+
"source": [
|
365
|
+
"feature_maps = feature_map_model.predict(img_array)\n"
|
366
|
+
]
|
367
|
+
},
|
368
|
+
{
|
369
|
+
"cell_type": "code",
|
370
|
+
"execution_count": null,
|
371
|
+
"id": "7fb737e9",
|
372
|
+
"metadata": {
|
373
|
+
"id": "7fb737e9",
|
374
|
+
"outputId": "2d9eb44e-cd14-4dfc-f7ee-e8b6f82ff8cb"
|
375
|
+
},
|
376
|
+
"outputs": [],
|
377
|
+
"source": [
|
378
|
+
"import matplotlib.pyplot as plt\n",
|
379
|
+
"\n",
|
380
|
+
"def plot_feature_maps(feature_maps, layer_names):\n",
|
381
|
+
" n_layers = len(layer_names)\n",
|
382
|
+
" fig, axes = plt.subplots(n_layers, 10, figsize=(20, 20))\n",
|
383
|
+
"\n",
|
384
|
+
" for i, layer_name in enumerate(layer_names):\n",
|
385
|
+
" fmap = feature_maps[i]\n",
|
386
|
+
" n_features = fmap.shape[-1]\n",
|
387
|
+
" for j in range(min(n_features, 10)):\n",
|
388
|
+
" ax = axes[i, j]\n",
|
389
|
+
" ax.imshow(fmap[0, :, :, j], cmap='viridis')\n",
|
390
|
+
" ax.axis('off')\n",
|
391
|
+
" axes[i, 0].set_title(f'Layer: {layer_name}')\n",
|
392
|
+
"\n",
|
393
|
+
" plt.tight_layout()\n",
|
394
|
+
" plt.show()\n",
|
395
|
+
"\n",
|
396
|
+
"plot_feature_maps(feature_maps, layer_names)\n"
|
397
|
+
]
|
398
|
+
},
|
399
|
+
{
|
400
|
+
"cell_type": "code",
|
401
|
+
"execution_count": null,
|
402
|
+
"id": "c0645049-c2f6-4f1a-adce-55f1b743eed3",
|
403
|
+
"metadata": {
|
404
|
+
"id": "c0645049-c2f6-4f1a-adce-55f1b743eed3"
|
405
|
+
},
|
406
|
+
"outputs": [],
|
407
|
+
"source": [
|
408
|
+
"CNN_model.compile(\n",
|
409
|
+
" optimizer='adam',\n",
|
410
|
+
" loss='sparse_categorical_crossentropy',\n",
|
411
|
+
" metrics=['accuracy']\n",
|
412
|
+
")"
|
413
|
+
]
|
414
|
+
},
|
415
|
+
{
|
416
|
+
"cell_type": "code",
|
417
|
+
"execution_count": null,
|
418
|
+
"id": "c56323f2-3524-424c-b232-a0db433b72ed",
|
419
|
+
"metadata": {
|
420
|
+
"id": "c56323f2-3524-424c-b232-a0db433b72ed",
|
421
|
+
"outputId": "6a387ae7-dd10-4b21-8d88-747a8324849d"
|
422
|
+
},
|
423
|
+
"outputs": [],
|
424
|
+
"source": [
|
425
|
+
"CNN_history = CNN_model.fit(\n",
|
426
|
+
" train_generator,\n",
|
427
|
+
" epochs=5,\n",
|
428
|
+
" validation_data=validation_generator,\n",
|
429
|
+
"\n",
|
430
|
+
")"
|
431
|
+
]
|
432
|
+
},
|
433
|
+
{
|
434
|
+
"cell_type": "code",
|
435
|
+
"execution_count": null,
|
436
|
+
"id": "1e5d4cc3-7550-469e-9d80-5b914d895b63",
|
437
|
+
"metadata": {
|
438
|
+
"id": "1e5d4cc3-7550-469e-9d80-5b914d895b63",
|
439
|
+
"outputId": "ce3e721a-14d1-42c9-ecf6-c0a1729b14fb"
|
440
|
+
},
|
441
|
+
"outputs": [],
|
442
|
+
"source": [
|
443
|
+
"test_loss, test_acc = CNN_model.evaluate(test_generator, steps=test_generator.samples // test_generator.batch_size)\n",
|
444
|
+
"print(f'Test accuracy: {test_acc}')"
|
445
|
+
]
|
446
|
+
},
|
447
|
+
{
|
448
|
+
"cell_type": "code",
|
449
|
+
"execution_count": null,
|
450
|
+
"id": "64f25696-1315-43e3-825c-d9e852fd2783",
|
451
|
+
"metadata": {
|
452
|
+
"id": "64f25696-1315-43e3-825c-d9e852fd2783",
|
453
|
+
"outputId": "07fdb09f-20cc-48aa-cea8-0e65145ccb0a"
|
454
|
+
},
|
455
|
+
"outputs": [],
|
456
|
+
"source": [
|
457
|
+
"CNN_Y_pred = CNN_model.predict(test_generator, steps=test_generator.samples // test_generator.batch_size + 1)\n",
|
458
|
+
"CNN_y_pred = np.argmax(CNN_Y_pred, axis=1)\n"
|
459
|
+
]
|
460
|
+
},
|
461
|
+
{
|
462
|
+
"cell_type": "code",
|
463
|
+
"execution_count": null,
|
464
|
+
"id": "f33d6e62-5f49-4962-8ba9-69abda6976ea",
|
465
|
+
"metadata": {
|
466
|
+
"id": "f33d6e62-5f49-4962-8ba9-69abda6976ea"
|
467
|
+
},
|
468
|
+
"outputs": [],
|
469
|
+
"source": [
|
470
|
+
"y_true = test_generator.classes\n"
|
471
|
+
]
|
472
|
+
},
|
473
|
+
{
|
474
|
+
"cell_type": "code",
|
475
|
+
"execution_count": null,
|
476
|
+
"id": "31a7fa08-1081-4a9c-8370-908671bc5505",
|
477
|
+
"metadata": {
|
478
|
+
"id": "31a7fa08-1081-4a9c-8370-908671bc5505",
|
479
|
+
"outputId": "ed71b9c1-4765-42f8-bb87-a72c3b0b9ba0"
|
480
|
+
},
|
481
|
+
"outputs": [],
|
482
|
+
"source": [
|
483
|
+
"print(classification_report(y_true, CNN_y_pred , target_names=class_names))\n"
|
484
|
+
]
|
485
|
+
},
|
486
|
+
{
|
487
|
+
"cell_type": "code",
|
488
|
+
"execution_count": null,
|
489
|
+
"id": "a137ffdc-a0ff-4e82-a0b5-ed62ef2c564f",
|
490
|
+
"metadata": {
|
491
|
+
"id": "a137ffdc-a0ff-4e82-a0b5-ed62ef2c564f"
|
492
|
+
},
|
493
|
+
"outputs": [],
|
494
|
+
"source": [
|
495
|
+
"def plot_confusion_matrix(y_true, y_pred, class_names):\n",
|
496
|
+
" cm = confusion_matrix(y_true, y_pred)\n",
|
497
|
+
" print(cm)\n",
|
498
|
+
"\n",
|
499
|
+
" plt.figure(figsize=(5, 5))\n",
|
500
|
+
" sns.heatmap(cm, annot=True, cmap='Blues', fmt='g', xticklabels=class_names, yticklabels=class_names)\n",
|
501
|
+
" plt.xlabel('Predicted')\n",
|
502
|
+
" plt.ylabel('True')\n",
|
503
|
+
" plt.title('Confusion Matrix')\n",
|
504
|
+
" plt.show()"
|
505
|
+
]
|
506
|
+
},
|
507
|
+
{
|
508
|
+
"cell_type": "code",
|
509
|
+
"execution_count": null,
|
510
|
+
"id": "4a0db27f-b2f9-46e8-9dc3-15df1bb1a38d",
|
511
|
+
"metadata": {
|
512
|
+
"id": "4a0db27f-b2f9-46e8-9dc3-15df1bb1a38d",
|
513
|
+
"outputId": "d2529d1d-ef92-4e7f-8639-7a375657228f"
|
514
|
+
},
|
515
|
+
"outputs": [],
|
516
|
+
"source": [
|
517
|
+
"plot_confusion_matrix(y_true, CNN_y_pred, class_names)\n"
|
518
|
+
]
|
519
|
+
},
|
520
|
+
{
|
521
|
+
"cell_type": "code",
|
522
|
+
"execution_count": null,
|
523
|
+
"id": "a4e7c2cc-16b5-4427-b808-ae32ac7b95e6",
|
524
|
+
"metadata": {
|
525
|
+
"id": "a4e7c2cc-16b5-4427-b808-ae32ac7b95e6"
|
526
|
+
},
|
527
|
+
"outputs": [],
|
528
|
+
"source": [
|
529
|
+
"def plot_training_history(history):\n",
|
530
|
+
" acc = history.history['accuracy']\n",
|
531
|
+
" val_acc = history.history['val_accuracy']\n",
|
532
|
+
" loss = history.history['loss']\n",
|
533
|
+
" val_loss = history.history['val_loss']\n",
|
534
|
+
" epochs = range(len(acc))\n",
|
535
|
+
"\n",
|
536
|
+
" plt.figure(figsize=(12, 4))\n",
|
537
|
+
"\n",
|
538
|
+
" plt.subplot(1, 2, 1)\n",
|
539
|
+
" plt.plot(epochs, acc, 'b', label='Training accuracy')\n",
|
540
|
+
" plt.plot(epochs, val_acc, 'r', label='Validation accuracy')\n",
|
541
|
+
" plt.title('Training and validation accuracy')\n",
|
542
|
+
" plt.legend()\n",
|
543
|
+
"\n",
|
544
|
+
" plt.subplot(1, 2, 2)\n",
|
545
|
+
" plt.plot(epochs, loss, 'b', label='Training loss')\n",
|
546
|
+
" plt.plot(epochs, val_loss, 'r', label='Validation loss')\n",
|
547
|
+
" plt.title('Training and validation loss')\n",
|
548
|
+
" plt.legend()\n",
|
549
|
+
"\n",
|
550
|
+
" plt.show()"
|
551
|
+
]
|
552
|
+
},
|
553
|
+
{
|
554
|
+
"cell_type": "code",
|
555
|
+
"execution_count": null,
|
556
|
+
"id": "3c474de3-ed19-47ab-92ef-b6484baaaecb",
|
557
|
+
"metadata": {
|
558
|
+
"id": "3c474de3-ed19-47ab-92ef-b6484baaaecb",
|
559
|
+
"outputId": "9c9f2f48-9891-43de-ab77-60e192e16df3"
|
560
|
+
},
|
561
|
+
"outputs": [],
|
562
|
+
"source": [
|
563
|
+
"plot_training_history(CNN_history)"
|
564
|
+
]
|
565
|
+
},
|
566
|
+
{
|
567
|
+
"cell_type": "code",
|
568
|
+
"execution_count": null,
|
569
|
+
"id": "53ec029b-123d-4e01-b6f3-92d705f64689",
|
570
|
+
"metadata": {
|
571
|
+
"id": "53ec029b-123d-4e01-b6f3-92d705f64689"
|
572
|
+
},
|
573
|
+
"outputs": [],
|
574
|
+
"source": [
|
575
|
+
"def plot_predictions(generator, model, class_names):\n",
|
576
|
+
" x, y_true = next(generator)\n",
|
577
|
+
" y_pred_prob = CNN_model.predict(x)\n",
|
578
|
+
" y_pred = np.argmax(y_pred_prob, axis=1)\n",
|
579
|
+
"\n",
|
580
|
+
" plt.figure(figsize=(20, 10))\n",
|
581
|
+
" for i in range(12):\n",
|
582
|
+
" plt.subplot(3, 4, i + 1)\n",
|
583
|
+
" plt.imshow(x[i])\n",
|
584
|
+
" plt.title(f'True: {class_names[int(y_true[i])]}, Pred: {class_names[y_pred[i]]}\\nProb: {y_pred_prob[i][y_pred[i]]:.2f}')\n",
|
585
|
+
" plt.axis('off')\n",
|
586
|
+
" plt.show()"
|
587
|
+
]
|
588
|
+
},
|
589
|
+
{
|
590
|
+
"cell_type": "code",
|
591
|
+
"execution_count": null,
|
592
|
+
"id": "53913002-8093-4ff9-a9e7-121669333dec",
|
593
|
+
"metadata": {
|
594
|
+
"id": "53913002-8093-4ff9-a9e7-121669333dec",
|
595
|
+
"outputId": "37dba8c0-8af9-486a-8d46-879b28b34c04"
|
596
|
+
},
|
597
|
+
"outputs": [],
|
598
|
+
"source": [
|
599
|
+
"plot_predictions(test_generator, CNN_model, class_names)\n"
|
600
|
+
]
|
601
|
+
},
|
602
|
+
{
|
603
|
+
"cell_type": "code",
|
604
|
+
"execution_count": null,
|
605
|
+
"id": "991a0720",
|
606
|
+
"metadata": {
|
607
|
+
"id": "991a0720"
|
608
|
+
},
|
609
|
+
"outputs": [],
|
610
|
+
"source": [
|
611
|
+
"from sklearn.metrics import precision_score, recall_score, f1_score, accuracy_score\n",
|
612
|
+
"import numpy as np\n"
|
613
|
+
]
|
614
|
+
},
|
615
|
+
{
|
616
|
+
"cell_type": "code",
|
617
|
+
"execution_count": null,
|
618
|
+
"id": "be5dd19c",
|
619
|
+
"metadata": {
|
620
|
+
"id": "be5dd19c"
|
621
|
+
},
|
622
|
+
"outputs": [],
|
623
|
+
"source": [
|
624
|
+
"precision_dnn = precision_score(y_true, y_pred, average='weighted')\n",
|
625
|
+
"recall_dnn = recall_score(y_true, y_pred, average='weighted')\n",
|
626
|
+
"f1_dnn = f1_score(y_true, y_pred, average='weighted')\n",
|
627
|
+
"accuracy_dnn = accuracy_score(y_true, y_pred)\n",
|
628
|
+
"\n",
|
629
|
+
"# Calculate metrics for CNN model\n",
|
630
|
+
"precision_cnn = precision_score(y_true, CNN_y_pred, average='weighted')\n",
|
631
|
+
"recall_cnn = recall_score(y_true, CNN_y_pred, average='weighted')\n",
|
632
|
+
"f1_cnn = f1_score(y_true, CNN_y_pred, average='weighted')\n",
|
633
|
+
"accuracy_cnn = accuracy_score(y_true, CNN_y_pred)\n",
|
634
|
+
"\n"
|
635
|
+
]
|
636
|
+
},
|
637
|
+
{
|
638
|
+
"cell_type": "code",
|
639
|
+
"execution_count": null,
|
640
|
+
"id": "89e25117-c92e-4121-9719-d2dcd0e5906a",
|
641
|
+
"metadata": {
|
642
|
+
"id": "89e25117-c92e-4121-9719-d2dcd0e5906a",
|
643
|
+
"outputId": "4d4a6fac-5b2a-459d-830b-3f5536d79f63"
|
644
|
+
},
|
645
|
+
"outputs": [],
|
646
|
+
"source": [
|
647
|
+
"import matplotlib.pyplot as plt\n",
|
648
|
+
"import numpy as np\n",
|
649
|
+
"import pandas as pd\n",
|
650
|
+
"\n",
|
651
|
+
"# Metrics for DNN\n",
|
652
|
+
"dnn_metrics = {\n",
|
653
|
+
" 'Precision': precision_dnn,\n",
|
654
|
+
" 'Recall': recall_dnn,\n",
|
655
|
+
" 'F1 Score': f1_dnn,\n",
|
656
|
+
" 'Accuracy': accuracy_dnn,\n",
|
657
|
+
" 'Loss': 1.6507\n",
|
658
|
+
"}\n",
|
659
|
+
"\n",
|
660
|
+
"# Metrics for CNN\n",
|
661
|
+
"cnn_metrics = {\n",
|
662
|
+
" 'Precision': precision_cnn,\n",
|
663
|
+
" 'Recall': recall_cnn,\n",
|
664
|
+
" 'F1 Score': f1_cnn,\n",
|
665
|
+
" 'Accuracy': accuracy_cnn,\n",
|
666
|
+
" 'Loss': 0.6988\n",
|
667
|
+
"}\n",
|
668
|
+
"\n",
|
669
|
+
"# Convert to DataFrame for easy plotting\n",
|
670
|
+
"metrics_df = pd.DataFrame({\n",
|
671
|
+
" 'Metric': list(dnn_metrics.keys()) + list(cnn_metrics.keys()),\n",
|
672
|
+
" 'Value': list(dnn_metrics.values()) + list(cnn_metrics.values()),\n",
|
673
|
+
" 'Model': ['DNN']*len(dnn_metrics) + ['CNN']*len(cnn_metrics)\n",
|
674
|
+
"})\n",
|
675
|
+
"\n",
|
676
|
+
"# Set up the plot\n",
|
677
|
+
"plt.figure(figsize=(5,5))\n",
|
678
|
+
"\n",
|
679
|
+
"# Bar positions\n",
|
680
|
+
"x = np.arange(len(dnn_metrics)) # the label locations\n",
|
681
|
+
"width = 0.35 # the width of the bars\n",
|
682
|
+
"\n",
|
683
|
+
"# Plotting bars\n",
|
684
|
+
"bars1 = plt.bar(x - width/2, list(dnn_metrics.values()), width, label='DNN', color='blue')\n",
|
685
|
+
"bars2 = plt.bar(x + width/2, list(cnn_metrics.values()), width, label='CNN', color='orange')\n",
|
686
|
+
"\n",
|
687
|
+
"# Adding labels, title, and legend\n",
|
688
|
+
"plt.xlabel('Metrics', fontsize=12)\n",
|
689
|
+
"plt.ylabel('Scores', fontsize=12)\n",
|
690
|
+
"plt.title('Comparison of Model Performance', fontsize=14)\n",
|
691
|
+
"plt.xticks(x, list(dnn_metrics.keys()), rotation=45)\n",
|
692
|
+
"plt.legend()\n",
|
693
|
+
"\n",
|
694
|
+
"# Add value labels on top of the bars\n",
|
695
|
+
"def add_labels(bars):\n",
|
696
|
+
" for bar in bars:\n",
|
697
|
+
" height = bar.get_height()\n",
|
698
|
+
" plt.annotate(f'{height:.2f}',\n",
|
699
|
+
" xy=(bar.get_x() + bar.get_width() / 2, height),\n",
|
700
|
+
" xytext=(0, 3), # 3 points vertical offset\n",
|
701
|
+
" textcoords=\"offset points\",\n",
|
702
|
+
" ha='center', va='bottom')\n",
|
703
|
+
"\n",
|
704
|
+
"add_labels(bars1)\n",
|
705
|
+
"add_labels(bars2)\n",
|
706
|
+
"\n",
|
707
|
+
"# Display the plot\n",
|
708
|
+
"plt.tight_layout()\n",
|
709
|
+
"plt.show()\n"
|
710
|
+
]
|
711
|
+
}
|
712
|
+
],
|
713
|
+
"metadata": {
|
714
|
+
"colab": {
|
715
|
+
"provenance": []
|
716
|
+
},
|
717
|
+
"kernelspec": {
|
718
|
+
"display_name": "Python 3 (ipykernel)",
|
719
|
+
"language": "python",
|
720
|
+
"name": "python3"
|
721
|
+
},
|
722
|
+
"language_info": {
|
723
|
+
"codemirror_mode": {
|
724
|
+
"name": "ipython",
|
725
|
+
"version": 3
|
726
|
+
},
|
727
|
+
"file_extension": ".py",
|
728
|
+
"mimetype": "text/x-python",
|
729
|
+
"name": "python",
|
730
|
+
"nbconvert_exporter": "python",
|
731
|
+
"pygments_lexer": "ipython3",
|
732
|
+
"version": "3.12.4"
|
733
|
+
}
|
734
|
+
},
|
735
|
+
"nbformat": 4,
|
736
|
+
"nbformat_minor": 5
|
737
|
+
}
|