Kabil007 commited on
Commit
00a2395
·
1 Parent(s): 01fb0f3

Upload Kabil_(EfficientNetB5_model).ipynb

Browse files
Files changed (1) hide show
  1. Kabil_(EfficientNetB5_model).ipynb +1078 -0
Kabil_(EfficientNetB5_model).ipynb ADDED
@@ -0,0 +1,1078 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "nbformat": 4,
3
+ "nbformat_minor": 0,
4
+ "metadata": {
5
+ "colab": {
6
+ "provenance": [],
7
+ "gpuType": "T4"
8
+ },
9
+ "kernelspec": {
10
+ "name": "python3",
11
+ "display_name": "Python 3"
12
+ },
13
+ "language_info": {
14
+ "name": "python"
15
+ },
16
+ "accelerator": "GPU"
17
+ },
18
+ "cells": [
19
+ {
20
+ "cell_type": "markdown",
21
+ "source": [
22
+ "# EfficientNet B5\n",
23
+ "## Let's Begin...."
24
+ ],
25
+ "metadata": {
26
+ "id": "DGOlpli75Z_c"
27
+ }
28
+ },
29
+ {
30
+ "cell_type": "code",
31
+ "execution_count": 1,
32
+ "metadata": {
33
+ "id": "OylMWw9Sh3b3"
34
+ },
35
+ "outputs": [],
36
+ "source": [
37
+ "# Import Neccessary Lib...\n",
38
+ "import pandas as pd\n",
39
+ "import numpy as np\n",
40
+ "from matplotlib import pyplot as plt\n",
41
+ "import seaborn as sns\n",
42
+ "\n",
43
+ "\n",
44
+ "import os\n",
45
+ "import random\n",
46
+ "\n",
47
+ "from sklearn.model_selection import train_test_split\n",
48
+ "from sklearn.metrics import confusion_matrix, classification_report\n",
49
+ "import cv2\n",
50
+ "\n",
51
+ "import tensorflow as tf\n",
52
+ "from tensorflow.keras.preprocessing.image import ImageDataGenerator\n",
53
+ "from tensorflow.keras.callbacks import EarlyStopping, ModelCheckpoint\n",
54
+ "from tensorflow.keras.applications import VGG19\n",
55
+ "from tensorflow.keras.optimizers import Adam, Adamax\n",
56
+ "from tensorflow.keras.models import Sequential\n",
57
+ "from tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense, Activation, Dropout, BatchNormalization\n",
58
+ "from tensorflow.keras import regularizers\n",
59
+ "from tensorflow.keras.regularizers import l1, l2"
60
+ ]
61
+ },
62
+ {
63
+ "cell_type": "code",
64
+ "source": [
65
+ "# Directory paths\n",
66
+ "train_dir = 'drive/MyDrive/LungCancer-IITM/Data/train'\n",
67
+ "test_dir = 'drive/MyDrive/LungCancer-IITM/Data/test'\n",
68
+ "valid_dir = 'drive/MyDrive/LungCancer-IITM/Data/valid'"
69
+ ],
70
+ "metadata": {
71
+ "id": "4DHOnXmTh8a_"
72
+ },
73
+ "execution_count": 2,
74
+ "outputs": []
75
+ },
76
+ {
77
+ "cell_type": "code",
78
+ "source": [
79
+ "\n",
80
+ "\n",
81
+ "\n"
82
+ ],
83
+ "metadata": {
84
+ "colab": {
85
+ "base_uri": "https://localhost:8080/"
86
+ },
87
+ "id": "PTGmzmm_iEqc",
88
+ "outputId": "6ecc5f01-8a51-4ef8-e672-ef60d5668eab"
89
+ },
90
+ "execution_count": 3,
91
+ "outputs": [
92
+ {
93
+ "output_type": "stream",
94
+ "name": "stdout",
95
+ "text": [
96
+ "Train DataFrame:\n",
97
+ " Image_Path \\\n",
98
+ "0 drive/MyDrive/LungCancer-IITM/Data/train/adeno... \n",
99
+ "1 drive/MyDrive/LungCancer-IITM/Data/train/adeno... \n",
100
+ "2 drive/MyDrive/LungCancer-IITM/Data/train/adeno... \n",
101
+ "3 drive/MyDrive/LungCancer-IITM/Data/train/adeno... \n",
102
+ "4 drive/MyDrive/LungCancer-IITM/Data/train/adeno... \n",
103
+ "\n",
104
+ " Label \n",
105
+ "0 adenocarcinoma_left.lower.lobe_T2_N0_M0_Ib \n",
106
+ "1 adenocarcinoma_left.lower.lobe_T2_N0_M0_Ib \n",
107
+ "2 adenocarcinoma_left.lower.lobe_T2_N0_M0_Ib \n",
108
+ "3 adenocarcinoma_left.lower.lobe_T2_N0_M0_Ib \n",
109
+ "4 adenocarcinoma_left.lower.lobe_T2_N0_M0_Ib \n"
110
+ ]
111
+ }
112
+ ]
113
+ },
114
+ {
115
+ "cell_type": "code",
116
+ "source": [
117
+ "import os\n",
118
+ "import pandas as pd\n",
119
+ "\n",
120
+ "# Function to create a DataFrame from image files in a folder\n",
121
+ "def create_dataframe(folder_path):\n",
122
+ " # Initialize an empty dictionary to store image paths and labels\n",
123
+ " data = {'Image_Path': [], 'Label': []}\n",
124
+ "\n",
125
+ " # List all subdirectories (labels) in the given folder\n",
126
+ " labels = os.listdir(folder_path)\n",
127
+ "\n",
128
+ " # Loop through each label\n",
129
+ " for label in labels:\n",
130
+ " # Construct the full path to the label folder\n",
131
+ " label_path = os.path.join(folder_path, label)\n",
132
+ "\n",
133
+ " # Check if the path is a directory\n",
134
+ " if os.path.isdir(label_path):\n",
135
+ " # List all image files in the label folder\n",
136
+ " images = os.listdir(label_path)\n",
137
+ "\n",
138
+ " # Loop through each image\n",
139
+ " for image in images:\n",
140
+ " # Construct the full path to the image\n",
141
+ " image_path = os.path.join(label_path, image)\n",
142
+ "\n",
143
+ " # Append image path and label to the dictionary\n",
144
+ " data['Image_Path'].append(image_path)\n",
145
+ " data['Label'].append(label)\n",
146
+ "\n",
147
+ " # Create a DataFrame from the dictionary\n",
148
+ " df = pd.DataFrame(data)\n",
149
+ " return df\n",
150
+ "\n",
151
+ "# Provide the path to your 'data' folder\n",
152
+ "data_folder = 'drive/MyDrive/LungCancer-IITM/Data'\n",
153
+ "\n",
154
+ "# Create DataFrames for train, test, and valid using the create_dataframe function\n",
155
+ "train_df = create_dataframe(os.path.join(data_folder, 'train'))\n",
156
+ "test_df = create_dataframe(os.path.join(data_folder, 'test'))\n",
157
+ "valid_df = create_dataframe(os.path.join(data_folder, 'valid'))\n",
158
+ "\n",
159
+ "# Print the created DataFrames for inspection\n",
160
+ "print(\"Train DataFrame:\")\n",
161
+ "print(train_df.head())"
162
+ ],
163
+ "metadata": {
164
+ "colab": {
165
+ "base_uri": "https://localhost:8080/"
166
+ },
167
+ "outputId": "67d41380-a800-446f-e017-98e22cb99872",
168
+ "id": "U-4wnr0O8dvF"
169
+ },
170
+ "execution_count": null,
171
+ "outputs": [
172
+ {
173
+ "output_type": "stream",
174
+ "name": "stdout",
175
+ "text": [
176
+ "Train DataFrame:\n",
177
+ " Image_Path \\\n",
178
+ "0 drive/MyDrive/LungCancer-IITM/Data/train/adeno... \n",
179
+ "1 drive/MyDrive/LungCancer-IITM/Data/train/adeno... \n",
180
+ "2 drive/MyDrive/LungCancer-IITM/Data/train/adeno... \n",
181
+ "3 drive/MyDrive/LungCancer-IITM/Data/train/adeno... \n",
182
+ "4 drive/MyDrive/LungCancer-IITM/Data/train/adeno... \n",
183
+ "\n",
184
+ " Label \n",
185
+ "0 adenocarcinoma_left.lower.lobe_T2_N0_M0_Ib \n",
186
+ "1 adenocarcinoma_left.lower.lobe_T2_N0_M0_Ib \n",
187
+ "2 adenocarcinoma_left.lower.lobe_T2_N0_M0_Ib \n",
188
+ "3 adenocarcinoma_left.lower.lobe_T2_N0_M0_Ib \n",
189
+ "4 adenocarcinoma_left.lower.lobe_T2_N0_M0_Ib \n"
190
+ ]
191
+ }
192
+ ]
193
+ },
194
+ {
195
+ "cell_type": "code",
196
+ "source": [
197
+ "print(\"\\nTest DataFrame:\")\n",
198
+ "print(test_df.head())"
199
+ ],
200
+ "metadata": {
201
+ "colab": {
202
+ "base_uri": "https://localhost:8080/"
203
+ },
204
+ "id": "c6iuI9JXiLQd",
205
+ "outputId": "e360f402-86ec-4aba-d390-be7e5ded6110"
206
+ },
207
+ "execution_count": 4,
208
+ "outputs": [
209
+ {
210
+ "output_type": "stream",
211
+ "name": "stdout",
212
+ "text": [
213
+ "\n",
214
+ "Test DataFrame:\n",
215
+ " Image_Path Label\n",
216
+ "0 drive/MyDrive/LungCancer-IITM/Data/test/large.... large.cell.carcinoma\n",
217
+ "1 drive/MyDrive/LungCancer-IITM/Data/test/large.... large.cell.carcinoma\n",
218
+ "2 drive/MyDrive/LungCancer-IITM/Data/test/large.... large.cell.carcinoma\n",
219
+ "3 drive/MyDrive/LungCancer-IITM/Data/test/large.... large.cell.carcinoma\n",
220
+ "4 drive/MyDrive/LungCancer-IITM/Data/test/large.... large.cell.carcinoma\n"
221
+ ]
222
+ }
223
+ ]
224
+ },
225
+ {
226
+ "cell_type": "code",
227
+ "source": [
228
+ "print(\"\\nValid DataFrame:\")\n",
229
+ "print(valid_df.head())"
230
+ ],
231
+ "metadata": {
232
+ "colab": {
233
+ "base_uri": "https://localhost:8080/"
234
+ },
235
+ "id": "0TX-BeALiOEZ",
236
+ "outputId": "68839461-8585-426f-c4e7-dce922db48bb"
237
+ },
238
+ "execution_count": 5,
239
+ "outputs": [
240
+ {
241
+ "output_type": "stream",
242
+ "name": "stdout",
243
+ "text": [
244
+ "\n",
245
+ "Valid DataFrame:\n",
246
+ " Image_Path \\\n",
247
+ "0 drive/MyDrive/LungCancer-IITM/Data/valid/large... \n",
248
+ "1 drive/MyDrive/LungCancer-IITM/Data/valid/large... \n",
249
+ "2 drive/MyDrive/LungCancer-IITM/Data/valid/large... \n",
250
+ "3 drive/MyDrive/LungCancer-IITM/Data/valid/large... \n",
251
+ "4 drive/MyDrive/LungCancer-IITM/Data/valid/large... \n",
252
+ "\n",
253
+ " Label \n",
254
+ "0 large.cell.carcinoma_left.hilum_T2_N2_M0_IIIa \n",
255
+ "1 large.cell.carcinoma_left.hilum_T2_N2_M0_IIIa \n",
256
+ "2 large.cell.carcinoma_left.hilum_T2_N2_M0_IIIa \n",
257
+ "3 large.cell.carcinoma_left.hilum_T2_N2_M0_IIIa \n",
258
+ "4 large.cell.carcinoma_left.hilum_T2_N2_M0_IIIa \n"
259
+ ]
260
+ }
261
+ ]
262
+ },
263
+ {
264
+ "cell_type": "code",
265
+ "source": [
266
+ "# Calculate the number of unique classes (labels) in the 'Label' column of the training DataFrame\n",
267
+ "num_classes = len(train_df['Label'].unique())\n",
268
+ "\n",
269
+ "# Print the number of classes in the dataset\n",
270
+ "print(f\"We have {num_classes} classes\")\n",
271
+ "\n",
272
+ "# Print the total number of images in the training DataFrame (total rows)\n",
273
+ "print(f\"We have {train_df.shape[0]} images\")"
274
+ ],
275
+ "metadata": {
276
+ "colab": {
277
+ "base_uri": "https://localhost:8080/"
278
+ },
279
+ "id": "dKwwZ0aXiS8Y",
280
+ "outputId": "17a1d131-9684-4e97-eb98-d836de207eb6"
281
+ },
282
+ "execution_count": 6,
283
+ "outputs": [
284
+ {
285
+ "output_type": "stream",
286
+ "name": "stdout",
287
+ "text": [
288
+ "We have 4 classes\n",
289
+ "We have 613 images\n"
290
+ ]
291
+ }
292
+ ]
293
+ },
294
+ {
295
+ "cell_type": "code",
296
+ "source": [
297
+ "# Calculate the number of unique classes (labels) in the 'Label' column of the test DataFrame\n",
298
+ "num_classes = len(test_df['Label'].unique())\n",
299
+ "\n",
300
+ "# Print the number of classes in the dataset\n",
301
+ "print(f\"We have {num_classes} classes\")\n",
302
+ "\n",
303
+ "# Print the total number of images in the test DataFrame (total rows)\n",
304
+ "print(f\"We have {test_df.shape[0]} images\")"
305
+ ],
306
+ "metadata": {
307
+ "colab": {
308
+ "base_uri": "https://localhost:8080/"
309
+ },
310
+ "id": "C8DIiGIwijaq",
311
+ "outputId": "6e7a4904-4c58-4dcc-84d6-4bd02b00df6a"
312
+ },
313
+ "execution_count": 7,
314
+ "outputs": [
315
+ {
316
+ "output_type": "stream",
317
+ "name": "stdout",
318
+ "text": [
319
+ "We have 4 classes\n",
320
+ "We have 315 images\n"
321
+ ]
322
+ }
323
+ ]
324
+ },
325
+ {
326
+ "cell_type": "code",
327
+ "source": [
328
+ "# Calculate the number of unique classes (labels) in the 'Label' column of the valid DataFrame\n",
329
+ "num_classes = len(valid_df['Label'].unique())\n",
330
+ "\n",
331
+ "# Print the number of classes in the dataset\n",
332
+ "print(f\"We have {num_classes} classes\")\n",
333
+ "\n",
334
+ "# Print the total number of images in the valid DataFrame (total rows)\n",
335
+ "print(f\"We have {valid_df.shape[0]} images\")"
336
+ ],
337
+ "metadata": {
338
+ "colab": {
339
+ "base_uri": "https://localhost:8080/"
340
+ },
341
+ "id": "OhbkmbZqinKY",
342
+ "outputId": "4ca3a84a-3ef7-41cd-dca5-024b6afe66fd"
343
+ },
344
+ "execution_count": 8,
345
+ "outputs": [
346
+ {
347
+ "output_type": "stream",
348
+ "name": "stdout",
349
+ "text": [
350
+ "We have 4 classes\n",
351
+ "We have 72 images\n"
352
+ ]
353
+ }
354
+ ]
355
+ },
356
+ {
357
+ "cell_type": "code",
358
+ "source": [
359
+ "# Define the size of the input images\n",
360
+ "img_size = (224, 224)\n",
361
+ "\n",
362
+ "# Specify the number of color channels in the images (3 for RGB)\n",
363
+ "channels = 3\n",
364
+ "\n",
365
+ "# Specify the color representation ('rgb' for red, green, blue)\n",
366
+ "color = 'rgb'\n",
367
+ "\n",
368
+ "# Define the shape of the input images based on size, channels, and color representation\n",
369
+ "img_shape = (img_size[0], img_size[1], channels)\n",
370
+ "\n",
371
+ "# Specify the batch size for training\n",
372
+ "batch_size = 32\n",
373
+ "\n",
374
+ "# Get the length of the test DataFrame\n",
375
+ "ts_length = len(test_df)\n",
376
+ "\n",
377
+ "# Determine an optimal test batch size that evenly divides the length of the test DataFrame\n",
378
+ "test_batch_size = max(sorted([ts_length // n for n in range(1, ts_length + 1) if ts_length % n == 0 and ts_length / n <= 80]))\n",
379
+ "\n",
380
+ "# Calculate the number of steps needed to cover the entire test dataset\n",
381
+ "test_steps = ts_length // test_batch_size\n",
382
+ "\n",
383
+ "# Define a function 'scalar' that takes an image as input (placeholder, no implementation provided)\n",
384
+ "def scalar(img):\n",
385
+ " return img\n"
386
+ ],
387
+ "metadata": {
388
+ "id": "7H00Xv0riwXL"
389
+ },
390
+ "execution_count": 9,
391
+ "outputs": []
392
+ },
393
+ {
394
+ "cell_type": "code",
395
+ "source": [
396
+ "tr_gen = ImageDataGenerator(preprocessing_function= scalar,\n",
397
+ " horizontal_flip= True)\n",
398
+ "\n",
399
+ "# Create an ImageDataGenerator for training with specified preprocessing and augmentation settings\n",
400
+ "tr_gen = ImageDataGenerator(preprocessing_function=scalar, horizontal_flip=True)\n",
401
+ "\n",
402
+ "# Create an ImageDataGenerator for testing with specified preprocessing settings\n",
403
+ "ts_gen = ImageDataGenerator(preprocessing_function=scalar)\n",
404
+ "\n",
405
+ "# Generate a flow from DataFrame for training data\n",
406
+ "train_gen = tr_gen.flow_from_dataframe(\n",
407
+ " train_df,\n",
408
+ " x_col='Image_Path',\n",
409
+ " y_col='Label',\n",
410
+ " target_size=img_size,\n",
411
+ " class_mode='categorical',\n",
412
+ " color_mode=color,\n",
413
+ " shuffle=True,\n",
414
+ " batch_size=batch_size\n",
415
+ ")\n",
416
+ "\n",
417
+ "# Generate a flow from DataFrame for validation data\n",
418
+ "valid_gen = ts_gen.flow_from_dataframe(\n",
419
+ " valid_df,\n",
420
+ " x_col='Image_Path',\n",
421
+ " y_col='Label',\n",
422
+ " target_size=img_size,\n",
423
+ " class_mode='categorical',\n",
424
+ " color_mode=color,\n",
425
+ " shuffle=True,\n",
426
+ " batch_size=batch_size\n",
427
+ ")\n",
428
+ "\n",
429
+ "# Generate a flow from DataFrame for test data\n",
430
+ "test_gen = ts_gen.flow_from_dataframe(\n",
431
+ " test_df,\n",
432
+ " x_col='Image_Path',\n",
433
+ " y_col='Label',\n",
434
+ " target_size=img_size,\n",
435
+ " class_mode='categorical',\n",
436
+ " color_mode=color,\n",
437
+ " shuffle=False,\n",
438
+ " batch_size=test_batch_size\n",
439
+ ")\n"
440
+ ],
441
+ "metadata": {
442
+ "colab": {
443
+ "base_uri": "https://localhost:8080/"
444
+ },
445
+ "id": "QqSOiLrxjjOD",
446
+ "outputId": "e562f193-cc5c-439f-c7b9-18bad8e76fe2"
447
+ },
448
+ "execution_count": 10,
449
+ "outputs": [
450
+ {
451
+ "output_type": "stream",
452
+ "name": "stdout",
453
+ "text": [
454
+ "Found 613 validated image filenames belonging to 4 classes.\n",
455
+ "Found 72 validated image filenames belonging to 4 classes.\n",
456
+ "Found 315 validated image filenames belonging to 4 classes.\n"
457
+ ]
458
+ }
459
+ ]
460
+ },
461
+ {
462
+ "cell_type": "code",
463
+ "source": [
464
+ "# Using the EfficientNetB5 pre-trained model as a base model (without the fully connected layers)\n",
465
+ "base_model = tf.keras.applications.efficientnet.EfficientNetB5(\n",
466
+ " include_top=False, # Exclude the fully connected layers\n",
467
+ " weights=\"imagenet\", # Load pre-trained ImageNet weights\n",
468
+ " input_shape=img_shape, # Specify the input shape for the model\n",
469
+ " pooling='max' # Use global max pooling as the final pooling layer\n",
470
+ ")\n",
471
+ "\n",
472
+ "# Constructing the complete model using Sequential API\n",
473
+ "model = Sequential([\n",
474
+ " base_model, # EfficientNetB5 as the base model\n",
475
+ " BatchNormalization(axis=-1, momentum=0.99, epsilon=0.001), # Batch normalization layer\n",
476
+ " Dense(256,\n",
477
+ " kernel_regularizer=regularizers.l2(l=0.016),\n",
478
+ " activity_regularizer=regularizers.l1(0.006),\n",
479
+ " bias_regularizer=regularizers.l1(0.006),\n",
480
+ " activation='relu'), # Dense layer with regularization and ReLU activation\n",
481
+ " Dropout(rate=0.45, seed=123), # Dropout layer for regularization\n",
482
+ " Dense(4, activation='softmax') # Output layer with softmax activation for multi-class classification\n",
483
+ "])\n",
484
+ "\n",
485
+ "# Compile the model with specified optimizer, loss function, and evaluation metric\n",
486
+ "model.compile(\n",
487
+ " optimizer=Adamax(learning_rate=0.001),\n",
488
+ " loss='categorical_crossentropy',\n",
489
+ " metrics=['accuracy']\n",
490
+ ")\n",
491
+ "\n",
492
+ "# Display a summary of the model architecture\n",
493
+ "model.summary()\n"
494
+ ],
495
+ "metadata": {
496
+ "colab": {
497
+ "base_uri": "https://localhost:8080/"
498
+ },
499
+ "id": "h2iZBYVFkm0n",
500
+ "outputId": "76e92170-c977-4a26-d134-b261838ef813"
501
+ },
502
+ "execution_count": 11,
503
+ "outputs": [
504
+ {
505
+ "output_type": "stream",
506
+ "name": "stdout",
507
+ "text": [
508
+ "Model: \"sequential\"\n",
509
+ "_________________________________________________________________\n",
510
+ " Layer (type) Output Shape Param # \n",
511
+ "=================================================================\n",
512
+ " efficientnetb5 (Functional (None, 2048) 28513527 \n",
513
+ " ) \n",
514
+ " \n",
515
+ " batch_normalization (Batch (None, 2048) 8192 \n",
516
+ " Normalization) \n",
517
+ " \n",
518
+ " dense (Dense) (None, 256) 524544 \n",
519
+ " \n",
520
+ " dropout (Dropout) (None, 256) 0 \n",
521
+ " \n",
522
+ " dense_1 (Dense) (None, 4) 1028 \n",
523
+ " \n",
524
+ "=================================================================\n",
525
+ "Total params: 29047291 (110.81 MB)\n",
526
+ "Trainable params: 28870452 (110.13 MB)\n",
527
+ "Non-trainable params: 176839 (690.78 KB)\n",
528
+ "_________________________________________________________________\n"
529
+ ]
530
+ }
531
+ ]
532
+ },
533
+ {
534
+ "cell_type": "code",
535
+ "source": [
536
+ "# Retrieve the configuration of the optimizer used in the EfficientNetB5 base model\n",
537
+ "model.optimizer.get_config()"
538
+ ],
539
+ "metadata": {
540
+ "colab": {
541
+ "base_uri": "https://localhost:8080/"
542
+ },
543
+ "id": "FhylF03qk8dp",
544
+ "outputId": "b9b8515d-048c-4a1f-829a-78906413760b"
545
+ },
546
+ "execution_count": 13,
547
+ "outputs": [
548
+ {
549
+ "output_type": "execute_result",
550
+ "data": {
551
+ "text/plain": [
552
+ "{'name': 'Adamax',\n",
553
+ " 'weight_decay': None,\n",
554
+ " 'clipnorm': None,\n",
555
+ " 'global_clipnorm': None,\n",
556
+ " 'clipvalue': None,\n",
557
+ " 'use_ema': False,\n",
558
+ " 'ema_momentum': 0.99,\n",
559
+ " 'ema_overwrite_frequency': None,\n",
560
+ " 'jit_compile': True,\n",
561
+ " 'is_legacy_optimizer': False,\n",
562
+ " 'learning_rate': 0.001,\n",
563
+ " 'beta_1': 0.9,\n",
564
+ " 'beta_2': 0.999,\n",
565
+ " 'epsilon': 1e-07}"
566
+ ]
567
+ },
568
+ "metadata": {},
569
+ "execution_count": 13
570
+ }
571
+ ]
572
+ },
573
+ {
574
+ "cell_type": "code",
575
+ "source": [
576
+ "# Define early stopping to halt training if the validation loss doesn't improve for 'patience' consecutive epochs\n",
577
+ "early_stop = EarlyStopping(monitor='val_loss',\n",
578
+ " patience=5,\n",
579
+ " verbose=1)\n",
580
+ "# Define model checkpoint to save the best weights during training based on validation loss\n",
581
+ "checkpoint = ModelCheckpoint('model_weights_efficient_B5_2.h5',\n",
582
+ " monitor='val_loss',\n",
583
+ " save_best_only=True,\n",
584
+ " save_weights_only=True,\n",
585
+ " mode='min',\n",
586
+ " verbose=1)\n",
587
+ "\n",
588
+ "# Train the EfficientNetB5 base model on the training data with validation using the generator\n",
589
+ "# - x: Training generator\n",
590
+ "# - steps_per_epoch: Number of batches to process in each epoch\n",
591
+ "# - epochs: Number of training epochs\n",
592
+ "# - callbacks: List of callbacks to apply during training (early stopping and model checkpoint)\n",
593
+ "# - validation_data: Validation generator for evaluating the model's performance on a separate dataset\n",
594
+ "\n",
595
+ "history = model.fit(x= train_gen,\n",
596
+ " steps_per_epoch = 20,\n",
597
+ " epochs= 100,\n",
598
+ " callbacks=[early_stop, checkpoint],\n",
599
+ " validation_data = valid_gen)"
600
+ ],
601
+ "metadata": {
602
+ "colab": {
603
+ "base_uri": "https://localhost:8080/"
604
+ },
605
+ "id": "Ymbza2MYlB2j",
606
+ "outputId": "d8eea6ac-dc3e-4e0c-8525-cdfa875f115f"
607
+ },
608
+ "execution_count": 14,
609
+ "outputs": [
610
+ {
611
+ "output_type": "stream",
612
+ "name": "stdout",
613
+ "text": [
614
+ "Epoch 1/100\n",
615
+ "20/20 [==============================] - ETA: 0s - loss: 8.9467 - accuracy: 0.6525\n",
616
+ "Epoch 1: val_loss improved from inf to 13.82872, saving model to model_weights_efficient_B5_2.h5\n",
617
+ "20/20 [==============================] - 330s 12s/step - loss: 8.9467 - accuracy: 0.6525 - val_loss: 13.8287 - val_accuracy: 0.4861\n",
618
+ "Epoch 2/100\n",
619
+ "20/20 [==============================] - ETA: 0s - loss: 7.9310 - accuracy: 0.8222\n",
620
+ "Epoch 2: val_loss improved from 13.82872 to 9.65489, saving model to model_weights_efficient_B5_2.h5\n",
621
+ "20/20 [==============================] - 19s 935ms/step - loss: 7.9310 - accuracy: 0.8222 - val_loss: 9.6549 - val_accuracy: 0.5000\n",
622
+ "Epoch 3/100\n",
623
+ "20/20 [==============================] - ETA: 0s - loss: 7.1907 - accuracy: 0.9086\n",
624
+ "Epoch 3: val_loss improved from 9.65489 to 8.90058, saving model to model_weights_efficient_B5_2.h5\n",
625
+ "20/20 [==============================] - 19s 947ms/step - loss: 7.1907 - accuracy: 0.9086 - val_loss: 8.9006 - val_accuracy: 0.5833\n",
626
+ "Epoch 4/100\n",
627
+ "20/20 [==============================] - ETA: 0s - loss: 6.6951 - accuracy: 0.9478\n",
628
+ "Epoch 4: val_loss improved from 8.90058 to 7.97767, saving model to model_weights_efficient_B5_2.h5\n",
629
+ "20/20 [==============================] - 19s 932ms/step - loss: 6.6951 - accuracy: 0.9478 - val_loss: 7.9777 - val_accuracy: 0.5833\n",
630
+ "Epoch 5/100\n",
631
+ "20/20 [==============================] - ETA: 0s - loss: 6.2736 - accuracy: 0.9755\n",
632
+ "Epoch 5: val_loss improved from 7.97767 to 7.08031, saving model to model_weights_efficient_B5_2.h5\n",
633
+ "20/20 [==============================] - 19s 932ms/step - loss: 6.2736 - accuracy: 0.9755 - val_loss: 7.0803 - val_accuracy: 0.6528\n",
634
+ "Epoch 6/100\n",
635
+ "20/20 [==============================] - ETA: 0s - loss: 5.9248 - accuracy: 0.9641\n",
636
+ "Epoch 6: val_loss improved from 7.08031 to 6.62661, saving model to model_weights_efficient_B5_2.h5\n",
637
+ "20/20 [==============================] - 19s 951ms/step - loss: 5.9248 - accuracy: 0.9641 - val_loss: 6.6266 - val_accuracy: 0.7500\n",
638
+ "Epoch 7/100\n",
639
+ "20/20 [==============================] - ETA: 0s - loss: 5.6432 - accuracy: 0.9739\n",
640
+ "Epoch 7: val_loss improved from 6.62661 to 6.26470, saving model to model_weights_efficient_B5_2.h5\n",
641
+ "20/20 [==============================] - 19s 932ms/step - loss: 5.6432 - accuracy: 0.9739 - val_loss: 6.2647 - val_accuracy: 0.6667\n",
642
+ "Epoch 8/100\n",
643
+ "20/20 [==============================] - ETA: 0s - loss: 5.4284 - accuracy: 0.9739\n",
644
+ "Epoch 8: val_loss improved from 6.26470 to 5.88624, saving model to model_weights_efficient_B5_2.h5\n",
645
+ "20/20 [==============================] - 20s 975ms/step - loss: 5.4284 - accuracy: 0.9739 - val_loss: 5.8862 - val_accuracy: 0.7361\n",
646
+ "Epoch 9/100\n",
647
+ "20/20 [==============================] - ETA: 0s - loss: 5.1599 - accuracy: 0.9821\n",
648
+ "Epoch 9: val_loss improved from 5.88624 to 5.53767, saving model to model_weights_efficient_B5_2.h5\n",
649
+ "20/20 [==============================] - 19s 933ms/step - loss: 5.1599 - accuracy: 0.9821 - val_loss: 5.5377 - val_accuracy: 0.8472\n",
650
+ "Epoch 10/100\n",
651
+ "20/20 [==============================] - ETA: 0s - loss: 4.9567 - accuracy: 0.9788\n",
652
+ "Epoch 10: val_loss improved from 5.53767 to 5.29575, saving model to model_weights_efficient_B5_2.h5\n",
653
+ "20/20 [==============================] - 19s 934ms/step - loss: 4.9567 - accuracy: 0.9788 - val_loss: 5.2957 - val_accuracy: 0.8750\n",
654
+ "Epoch 11/100\n",
655
+ "20/20 [==============================] - ETA: 0s - loss: 4.7625 - accuracy: 0.9804\n",
656
+ "Epoch 11: val_loss improved from 5.29575 to 5.10167, saving model to model_weights_efficient_B5_2.h5\n",
657
+ "20/20 [==============================] - 19s 948ms/step - loss: 4.7625 - accuracy: 0.9804 - val_loss: 5.1017 - val_accuracy: 0.8750\n",
658
+ "Epoch 12/100\n",
659
+ "20/20 [==============================] - ETA: 0s - loss: 4.6141 - accuracy: 0.9788\n",
660
+ "Epoch 12: val_loss improved from 5.10167 to 4.96450, saving model to model_weights_efficient_B5_2.h5\n",
661
+ "20/20 [==============================] - 19s 957ms/step - loss: 4.6141 - accuracy: 0.9788 - val_loss: 4.9645 - val_accuracy: 0.8750\n",
662
+ "Epoch 13/100\n",
663
+ "20/20 [==============================] - ETA: 0s - loss: 4.4517 - accuracy: 0.9902\n",
664
+ "Epoch 13: val_loss improved from 4.96450 to 4.89537, saving model to model_weights_efficient_B5_2.h5\n",
665
+ "20/20 [==============================] - 19s 938ms/step - loss: 4.4517 - accuracy: 0.9902 - val_loss: 4.8954 - val_accuracy: 0.8750\n",
666
+ "Epoch 14/100\n",
667
+ "20/20 [==============================] - ETA: 0s - loss: 4.3521 - accuracy: 0.9788\n",
668
+ "Epoch 14: val_loss improved from 4.89537 to 4.61144, saving model to model_weights_efficient_B5_2.h5\n",
669
+ "20/20 [==============================] - 19s 941ms/step - loss: 4.3521 - accuracy: 0.9788 - val_loss: 4.6114 - val_accuracy: 0.8611\n",
670
+ "Epoch 15/100\n",
671
+ "20/20 [==============================] - ETA: 0s - loss: 4.1907 - accuracy: 0.9837\n",
672
+ "Epoch 15: val_loss improved from 4.61144 to 4.47061, saving model to model_weights_efficient_B5_2.h5\n",
673
+ "20/20 [==============================] - 20s 980ms/step - loss: 4.1907 - accuracy: 0.9837 - val_loss: 4.4706 - val_accuracy: 0.8611\n",
674
+ "Epoch 16/100\n",
675
+ "20/20 [==============================] - ETA: 0s - loss: 4.0591 - accuracy: 0.9821\n",
676
+ "Epoch 16: val_loss improved from 4.47061 to 4.35734, saving model to model_weights_efficient_B5_2.h5\n",
677
+ "20/20 [==============================] - 19s 930ms/step - loss: 4.0591 - accuracy: 0.9821 - val_loss: 4.3573 - val_accuracy: 0.8750\n",
678
+ "Epoch 17/100\n",
679
+ "20/20 [==============================] - ETA: 0s - loss: 3.9479 - accuracy: 0.9837\n",
680
+ "Epoch 17: val_loss improved from 4.35734 to 4.19360, saving model to model_weights_efficient_B5_2.h5\n",
681
+ "20/20 [==============================] - 19s 940ms/step - loss: 3.9479 - accuracy: 0.9837 - val_loss: 4.1936 - val_accuracy: 0.8750\n",
682
+ "Epoch 18/100\n",
683
+ "20/20 [==============================] - ETA: 0s - loss: 3.8014 - accuracy: 0.9951\n",
684
+ "Epoch 18: val_loss improved from 4.19360 to 4.07113, saving model to model_weights_efficient_B5_2.h5\n",
685
+ "20/20 [==============================] - 20s 977ms/step - loss: 3.8014 - accuracy: 0.9951 - val_loss: 4.0711 - val_accuracy: 0.8750\n",
686
+ "Epoch 19/100\n",
687
+ "20/20 [==============================] - ETA: 0s - loss: 3.7042 - accuracy: 0.9918\n",
688
+ "Epoch 19: val_loss improved from 4.07113 to 4.02841, saving model to model_weights_efficient_B5_2.h5\n",
689
+ "20/20 [==============================] - 19s 940ms/step - loss: 3.7042 - accuracy: 0.9918 - val_loss: 4.0284 - val_accuracy: 0.8472\n",
690
+ "Epoch 20/100\n",
691
+ "20/20 [==============================] - ETA: 0s - loss: 3.6051 - accuracy: 0.9918\n",
692
+ "Epoch 20: val_loss improved from 4.02841 to 3.87404, saving model to model_weights_efficient_B5_2.h5\n",
693
+ "20/20 [==============================] - 19s 943ms/step - loss: 3.6051 - accuracy: 0.9918 - val_loss: 3.8740 - val_accuracy: 0.9028\n",
694
+ "Epoch 21/100\n",
695
+ "20/20 [==============================] - ETA: 0s - loss: 3.5299 - accuracy: 0.9902\n",
696
+ "Epoch 21: val_loss improved from 3.87404 to 3.76933, saving model to model_weights_efficient_B5_2.h5\n",
697
+ "20/20 [==============================] - 19s 947ms/step - loss: 3.5299 - accuracy: 0.9902 - val_loss: 3.7693 - val_accuracy: 0.9028\n",
698
+ "Epoch 22/100\n",
699
+ "20/20 [==============================] - ETA: 0s - loss: 3.4325 - accuracy: 0.9902\n",
700
+ "Epoch 22: val_loss improved from 3.76933 to 3.64684, saving model to model_weights_efficient_B5_2.h5\n",
701
+ "20/20 [==============================] - 20s 964ms/step - loss: 3.4325 - accuracy: 0.9902 - val_loss: 3.6468 - val_accuracy: 0.8889\n",
702
+ "Epoch 23/100\n",
703
+ "20/20 [==============================] - ETA: 0s - loss: 3.3194 - accuracy: 0.9967\n",
704
+ "Epoch 23: val_loss improved from 3.64684 to 3.55495, saving model to model_weights_efficient_B5_2.h5\n",
705
+ "20/20 [==============================] - 19s 934ms/step - loss: 3.3194 - accuracy: 0.9967 - val_loss: 3.5549 - val_accuracy: 0.8889\n",
706
+ "Epoch 24/100\n",
707
+ "20/20 [==============================] - ETA: 0s - loss: 3.2151 - accuracy: 0.9935\n",
708
+ "Epoch 24: val_loss improved from 3.55495 to 3.47809, saving model to model_weights_efficient_B5_2.h5\n",
709
+ "20/20 [==============================] - 20s 1s/step - loss: 3.2151 - accuracy: 0.9935 - val_loss: 3.4781 - val_accuracy: 0.8889\n",
710
+ "Epoch 25/100\n",
711
+ "20/20 [==============================] - ETA: 0s - loss: 3.1480 - accuracy: 0.9869\n",
712
+ "Epoch 25: val_loss improved from 3.47809 to 3.46385, saving model to model_weights_efficient_B5_2.h5\n",
713
+ "20/20 [==============================] - 19s 937ms/step - loss: 3.1480 - accuracy: 0.9869 - val_loss: 3.4639 - val_accuracy: 0.8889\n",
714
+ "Epoch 26/100\n",
715
+ "20/20 [==============================] - ETA: 0s - loss: 3.0889 - accuracy: 0.9837\n",
716
+ "Epoch 26: val_loss improved from 3.46385 to 3.30259, saving model to model_weights_efficient_B5_2.h5\n",
717
+ "20/20 [==============================] - 19s 935ms/step - loss: 3.0889 - accuracy: 0.9837 - val_loss: 3.3026 - val_accuracy: 0.8889\n",
718
+ "Epoch 27/100\n",
719
+ "20/20 [==============================] - ETA: 0s - loss: 2.9959 - accuracy: 0.9902\n",
720
+ "Epoch 27: val_loss improved from 3.30259 to 3.23432, saving model to model_weights_efficient_B5_2.h5\n",
721
+ "20/20 [==============================] - 19s 977ms/step - loss: 2.9959 - accuracy: 0.9902 - val_loss: 3.2343 - val_accuracy: 0.9167\n",
722
+ "Epoch 28/100\n",
723
+ "20/20 [==============================] - ETA: 0s - loss: 2.8889 - accuracy: 0.9967\n",
724
+ "Epoch 28: val_loss improved from 3.23432 to 3.13419, saving model to model_weights_efficient_B5_2.h5\n",
725
+ "20/20 [==============================] - 19s 952ms/step - loss: 2.8889 - accuracy: 0.9967 - val_loss: 3.1342 - val_accuracy: 0.9028\n",
726
+ "Epoch 29/100\n",
727
+ "20/20 [==============================] - ETA: 0s - loss: 2.8285 - accuracy: 0.9918\n",
728
+ "Epoch 29: val_loss improved from 3.13419 to 3.05611, saving model to model_weights_efficient_B5_2.h5\n",
729
+ "20/20 [==============================] - 20s 969ms/step - loss: 2.8285 - accuracy: 0.9918 - val_loss: 3.0561 - val_accuracy: 0.9167\n",
730
+ "Epoch 30/100\n",
731
+ "20/20 [==============================] - ETA: 0s - loss: 2.7386 - accuracy: 0.9967\n",
732
+ "Epoch 30: val_loss improved from 3.05611 to 2.98006, saving model to model_weights_efficient_B5_2.h5\n",
733
+ "20/20 [==============================] - 19s 930ms/step - loss: 2.7386 - accuracy: 0.9967 - val_loss: 2.9801 - val_accuracy: 0.9167\n",
734
+ "Epoch 31/100\n",
735
+ "20/20 [==============================] - ETA: 0s - loss: 2.6883 - accuracy: 0.9935\n",
736
+ "Epoch 31: val_loss improved from 2.98006 to 2.91081, saving model to model_weights_efficient_B5_2.h5\n",
737
+ "20/20 [==============================] - 19s 942ms/step - loss: 2.6883 - accuracy: 0.9935 - val_loss: 2.9108 - val_accuracy: 0.9167\n",
738
+ "Epoch 32/100\n",
739
+ "20/20 [==============================] - ETA: 0s - loss: 2.6405 - accuracy: 0.9788\n",
740
+ "Epoch 32: val_loss did not improve from 2.91081\n",
741
+ "20/20 [==============================] - 18s 901ms/step - loss: 2.6405 - accuracy: 0.9788 - val_loss: 2.9625 - val_accuracy: 0.8611\n",
742
+ "Epoch 33/100\n",
743
+ "20/20 [==============================] - ETA: 0s - loss: 2.5627 - accuracy: 0.9886\n",
744
+ "Epoch 33: val_loss improved from 2.91081 to 2.88892, saving model to model_weights_efficient_B5_2.h5\n",
745
+ "20/20 [==============================] - 19s 938ms/step - loss: 2.5627 - accuracy: 0.9886 - val_loss: 2.8889 - val_accuracy: 0.9028\n",
746
+ "Epoch 34/100\n",
747
+ "20/20 [==============================] - ETA: 0s - loss: 2.5646 - accuracy: 0.9804\n",
748
+ "Epoch 34: val_loss did not improve from 2.88892\n",
749
+ "20/20 [==============================] - 18s 901ms/step - loss: 2.5646 - accuracy: 0.9804 - val_loss: 2.9084 - val_accuracy: 0.8611\n",
750
+ "Epoch 35/100\n",
751
+ "20/20 [==============================] - ETA: 0s - loss: 2.4740 - accuracy: 0.9935\n",
752
+ "Epoch 35: val_loss improved from 2.88892 to 2.79603, saving model to model_weights_efficient_B5_2.h5\n",
753
+ "20/20 [==============================] - 19s 955ms/step - loss: 2.4740 - accuracy: 0.9935 - val_loss: 2.7960 - val_accuracy: 0.9028\n",
754
+ "Epoch 36/100\n",
755
+ "20/20 [==============================] - ETA: 0s - loss: 2.4113 - accuracy: 0.9853\n",
756
+ "Epoch 36: val_loss improved from 2.79603 to 2.72169, saving model to model_weights_efficient_B5_2.h5\n",
757
+ "20/20 [==============================] - 19s 965ms/step - loss: 2.4113 - accuracy: 0.9853 - val_loss: 2.7217 - val_accuracy: 0.8333\n",
758
+ "Epoch 37/100\n",
759
+ "20/20 [==============================] - ETA: 0s - loss: 2.3420 - accuracy: 0.9869\n",
760
+ "Epoch 37: val_loss improved from 2.72169 to 2.62496, saving model to model_weights_efficient_B5_2.h5\n",
761
+ "20/20 [==============================] - 19s 932ms/step - loss: 2.3420 - accuracy: 0.9869 - val_loss: 2.6250 - val_accuracy: 0.8611\n",
762
+ "Epoch 38/100\n",
763
+ "20/20 [==============================] - ETA: 0s - loss: 2.2655 - accuracy: 0.9951\n",
764
+ "Epoch 38: val_loss improved from 2.62496 to 2.49132, saving model to model_weights_efficient_B5_2.h5\n",
765
+ "20/20 [==============================] - 20s 980ms/step - loss: 2.2655 - accuracy: 0.9951 - val_loss: 2.4913 - val_accuracy: 0.9167\n",
766
+ "Epoch 39/100\n",
767
+ "20/20 [==============================] - ETA: 0s - loss: 2.2046 - accuracy: 0.9967\n",
768
+ "Epoch 39: val_loss improved from 2.49132 to 2.45171, saving model to model_weights_efficient_B5_2.h5\n",
769
+ "20/20 [==============================] - 19s 935ms/step - loss: 2.2046 - accuracy: 0.9967 - val_loss: 2.4517 - val_accuracy: 0.9028\n",
770
+ "Epoch 40/100\n",
771
+ "20/20 [==============================] - ETA: 0s - loss: 2.1569 - accuracy: 0.9935\n",
772
+ "Epoch 40: val_loss improved from 2.45171 to 2.36931, saving model to model_weights_efficient_B5_2.h5\n",
773
+ "20/20 [==============================] - 19s 934ms/step - loss: 2.1569 - accuracy: 0.9935 - val_loss: 2.3693 - val_accuracy: 0.9306\n",
774
+ "Epoch 41/100\n",
775
+ "20/20 [==============================] - ETA: 0s - loss: 2.0928 - accuracy: 0.9967\n",
776
+ "Epoch 41: val_loss improved from 2.36931 to 2.30855, saving model to model_weights_efficient_B5_2.h5\n",
777
+ "20/20 [==============================] - 20s 979ms/step - loss: 2.0928 - accuracy: 0.9967 - val_loss: 2.3086 - val_accuracy: 0.9306\n",
778
+ "Epoch 42/100\n",
779
+ "20/20 [==============================] - ETA: 0s - loss: 2.0393 - accuracy: 0.9967\n",
780
+ "Epoch 42: val_loss improved from 2.30855 to 2.24363, saving model to model_weights_efficient_B5_2.h5\n",
781
+ "20/20 [==============================] - 19s 937ms/step - loss: 2.0393 - accuracy: 0.9967 - val_loss: 2.2436 - val_accuracy: 0.9306\n",
782
+ "Epoch 43/100\n",
783
+ "20/20 [==============================] - ETA: 0s - loss: 1.9881 - accuracy: 0.9984\n",
784
+ "Epoch 43: val_loss improved from 2.24363 to 2.19355, saving model to model_weights_efficient_B5_2.h5\n",
785
+ "20/20 [==============================] - 19s 979ms/step - loss: 1.9881 - accuracy: 0.9984 - val_loss: 2.1935 - val_accuracy: 0.9167\n",
786
+ "Epoch 44/100\n",
787
+ "20/20 [==============================] - ETA: 0s - loss: 1.9369 - accuracy: 1.0000\n",
788
+ "Epoch 44: val_loss improved from 2.19355 to 2.13765, saving model to model_weights_efficient_B5_2.h5\n",
789
+ "20/20 [==============================] - 19s 963ms/step - loss: 1.9369 - accuracy: 1.0000 - val_loss: 2.1376 - val_accuracy: 0.9306\n",
790
+ "Epoch 45/100\n",
791
+ "20/20 [==============================] - ETA: 0s - loss: 1.8963 - accuracy: 0.9967\n",
792
+ "Epoch 45: val_loss improved from 2.13765 to 2.11182, saving model to model_weights_efficient_B5_2.h5\n",
793
+ "20/20 [==============================] - 19s 934ms/step - loss: 1.8963 - accuracy: 0.9967 - val_loss: 2.1118 - val_accuracy: 0.9306\n",
794
+ "Epoch 46/100\n",
795
+ "20/20 [==============================] - ETA: 0s - loss: 1.8555 - accuracy: 0.9967\n",
796
+ "Epoch 46: val_loss improved from 2.11182 to 2.08817, saving model to model_weights_efficient_B5_2.h5\n",
797
+ "20/20 [==============================] - 19s 939ms/step - loss: 1.8555 - accuracy: 0.9967 - val_loss: 2.0882 - val_accuracy: 0.9306\n",
798
+ "Epoch 47/100\n",
799
+ "20/20 [==============================] - ETA: 0s - loss: 1.8443 - accuracy: 0.9869\n",
800
+ "Epoch 47: val_loss improved from 2.08817 to 2.08034, saving model to model_weights_efficient_B5_2.h5\n",
801
+ "20/20 [==============================] - 20s 978ms/step - loss: 1.8443 - accuracy: 0.9869 - val_loss: 2.0803 - val_accuracy: 0.9306\n",
802
+ "Epoch 48/100\n",
803
+ "20/20 [==============================] - ETA: 0s - loss: 1.7713 - accuracy: 0.9984\n",
804
+ "Epoch 48: val_loss improved from 2.08034 to 1.98731, saving model to model_weights_efficient_B5_2.h5\n",
805
+ "20/20 [==============================] - 19s 936ms/step - loss: 1.7713 - accuracy: 0.9984 - val_loss: 1.9873 - val_accuracy: 0.9306\n",
806
+ "Epoch 49/100\n",
807
+ "20/20 [==============================] - ETA: 0s - loss: 1.7160 - accuracy: 0.9984\n",
808
+ "Epoch 49: val_loss improved from 1.98731 to 1.93409, saving model to model_weights_efficient_B5_2.h5\n",
809
+ "20/20 [==============================] - 19s 935ms/step - loss: 1.7160 - accuracy: 0.9984 - val_loss: 1.9341 - val_accuracy: 0.9306\n",
810
+ "Epoch 50/100\n",
811
+ "20/20 [==============================] - ETA: 0s - loss: 1.6692 - accuracy: 0.9967\n",
812
+ "Epoch 50: val_loss improved from 1.93409 to 1.88645, saving model to model_weights_efficient_B5_2.h5\n",
813
+ "20/20 [==============================] - 19s 953ms/step - loss: 1.6692 - accuracy: 0.9967 - val_loss: 1.8864 - val_accuracy: 0.9306\n",
814
+ "Epoch 51/100\n",
815
+ "20/20 [==============================] - ETA: 0s - loss: 1.6335 - accuracy: 0.9967\n",
816
+ "Epoch 51: val_loss improved from 1.88645 to 1.87095, saving model to model_weights_efficient_B5_2.h5\n",
817
+ "20/20 [==============================] - 19s 937ms/step - loss: 1.6335 - accuracy: 0.9967 - val_loss: 1.8709 - val_accuracy: 0.9306\n",
818
+ "Epoch 52/100\n",
819
+ "20/20 [==============================] - ETA: 0s - loss: 1.6030 - accuracy: 0.9935\n",
820
+ "Epoch 52: val_loss improved from 1.87095 to 1.81230, saving model to model_weights_efficient_B5_2.h5\n",
821
+ "20/20 [==============================] - 19s 934ms/step - loss: 1.6030 - accuracy: 0.9935 - val_loss: 1.8123 - val_accuracy: 0.9306\n",
822
+ "Epoch 53/100\n",
823
+ "20/20 [==============================] - ETA: 0s - loss: 1.5754 - accuracy: 0.9951\n",
824
+ "Epoch 53: val_loss improved from 1.81230 to 1.80875, saving model to model_weights_efficient_B5_2.h5\n",
825
+ "20/20 [==============================] - 19s 943ms/step - loss: 1.5754 - accuracy: 0.9951 - val_loss: 1.8088 - val_accuracy: 0.9306\n",
826
+ "Epoch 54/100\n",
827
+ "20/20 [==============================] - ETA: 0s - loss: 1.5490 - accuracy: 0.9902\n",
828
+ "Epoch 54: val_loss improved from 1.80875 to 1.77187, saving model to model_weights_efficient_B5_2.h5\n",
829
+ "20/20 [==============================] - 20s 973ms/step - loss: 1.5490 - accuracy: 0.9902 - val_loss: 1.7719 - val_accuracy: 0.9167\n",
830
+ "Epoch 55/100\n",
831
+ "20/20 [==============================] - ETA: 0s - loss: 1.4940 - accuracy: 0.9967\n",
832
+ "Epoch 55: val_loss improved from 1.77187 to 1.72648, saving model to model_weights_efficient_B5_2.h5\n",
833
+ "20/20 [==============================] - 19s 943ms/step - loss: 1.4940 - accuracy: 0.9967 - val_loss: 1.7265 - val_accuracy: 0.9167\n",
834
+ "Epoch 56/100\n",
835
+ "20/20 [==============================] - ETA: 0s - loss: 1.4628 - accuracy: 0.9951\n",
836
+ "Epoch 56: val_loss improved from 1.72648 to 1.66311, saving model to model_weights_efficient_B5_2.h5\n",
837
+ "20/20 [==============================] - 19s 932ms/step - loss: 1.4628 - accuracy: 0.9951 - val_loss: 1.6631 - val_accuracy: 0.9306\n",
838
+ "Epoch 57/100\n",
839
+ "20/20 [==============================] - ETA: 0s - loss: 1.4283 - accuracy: 0.9951\n",
840
+ "Epoch 57: val_loss improved from 1.66311 to 1.58719, saving model to model_weights_efficient_B5_2.h5\n",
841
+ "20/20 [==============================] - 19s 956ms/step - loss: 1.4283 - accuracy: 0.9951 - val_loss: 1.5872 - val_accuracy: 0.9306\n",
842
+ "Epoch 58/100\n",
843
+ "20/20 [==============================] - ETA: 0s - loss: 1.4072 - accuracy: 0.9967\n",
844
+ "Epoch 58: val_loss improved from 1.58719 to 1.56380, saving model to model_weights_efficient_B5_2.h5\n",
845
+ "20/20 [==============================] - 19s 932ms/step - loss: 1.4072 - accuracy: 0.9967 - val_loss: 1.5638 - val_accuracy: 0.9306\n",
846
+ "Epoch 59/100\n",
847
+ "20/20 [==============================] - ETA: 0s - loss: 1.3953 - accuracy: 0.9902\n",
848
+ "Epoch 59: val_loss did not improve from 1.56380\n",
849
+ "20/20 [==============================] - 18s 935ms/step - loss: 1.3953 - accuracy: 0.9902 - val_loss: 1.5837 - val_accuracy: 0.9306\n",
850
+ "Epoch 60/100\n",
851
+ "20/20 [==============================] - ETA: 0s - loss: 1.3637 - accuracy: 0.9902\n",
852
+ "Epoch 60: val_loss improved from 1.56380 to 1.55265, saving model to model_weights_efficient_B5_2.h5\n",
853
+ "20/20 [==============================] - 19s 941ms/step - loss: 1.3637 - accuracy: 0.9902 - val_loss: 1.5526 - val_accuracy: 0.9444\n",
854
+ "Epoch 61/100\n",
855
+ "20/20 [==============================] - ETA: 0s - loss: 1.3116 - accuracy: 0.9918\n",
856
+ "Epoch 61: val_loss improved from 1.55265 to 1.48927, saving model to model_weights_efficient_B5_2.h5\n",
857
+ "20/20 [==============================] - 19s 955ms/step - loss: 1.3116 - accuracy: 0.9918 - val_loss: 1.4893 - val_accuracy: 0.9444\n",
858
+ "Epoch 62/100\n",
859
+ "20/20 [==============================] - ETA: 0s - loss: 1.2852 - accuracy: 0.9951\n",
860
+ "Epoch 62: val_loss improved from 1.48927 to 1.46638, saving model to model_weights_efficient_B5_2.h5\n",
861
+ "20/20 [==============================] - 19s 947ms/step - loss: 1.2852 - accuracy: 0.9951 - val_loss: 1.4664 - val_accuracy: 0.9306\n",
862
+ "Epoch 63/100\n",
863
+ "20/20 [==============================] - ETA: 0s - loss: 1.2581 - accuracy: 0.9935\n",
864
+ "Epoch 63: val_loss improved from 1.46638 to 1.45661, saving model to model_weights_efficient_B5_2.h5\n",
865
+ "20/20 [==============================] - 19s 934ms/step - loss: 1.2581 - accuracy: 0.9935 - val_loss: 1.4566 - val_accuracy: 0.9306\n",
866
+ "Epoch 64/100\n",
867
+ "20/20 [==============================] - ETA: 0s - loss: 1.2234 - accuracy: 0.9967\n",
868
+ "Epoch 64: val_loss improved from 1.45661 to 1.42951, saving model to model_weights_efficient_B5_2.h5\n",
869
+ "20/20 [==============================] - 19s 932ms/step - loss: 1.2234 - accuracy: 0.9967 - val_loss: 1.4295 - val_accuracy: 0.9306\n",
870
+ "Epoch 65/100\n",
871
+ "20/20 [==============================] - ETA: 0s - loss: 1.1978 - accuracy: 0.9967\n",
872
+ "Epoch 65: val_loss improved from 1.42951 to 1.40270, saving model to model_weights_efficient_B5_2.h5\n",
873
+ "20/20 [==============================] - 20s 972ms/step - loss: 1.1978 - accuracy: 0.9967 - val_loss: 1.4027 - val_accuracy: 0.9306\n",
874
+ "Epoch 66/100\n",
875
+ "20/20 [==============================] - ETA: 0s - loss: 1.1966 - accuracy: 0.9935\n",
876
+ "Epoch 66: val_loss did not improve from 1.40270\n",
877
+ "20/20 [==============================] - 18s 896ms/step - loss: 1.1966 - accuracy: 0.9935 - val_loss: 1.4201 - val_accuracy: 0.9167\n",
878
+ "Epoch 67/100\n",
879
+ "20/20 [==============================] - ETA: 0s - loss: 1.1682 - accuracy: 0.9984\n",
880
+ "Epoch 67: val_loss did not improve from 1.40270\n",
881
+ "20/20 [==============================] - 19s 931ms/step - loss: 1.1682 - accuracy: 0.9984 - val_loss: 1.4158 - val_accuracy: 0.9306\n",
882
+ "Epoch 68/100\n",
883
+ "20/20 [==============================] - ETA: 0s - loss: 1.1414 - accuracy: 0.9918\n",
884
+ "Epoch 68: val_loss improved from 1.40270 to 1.36896, saving model to model_weights_efficient_B5_2.h5\n",
885
+ "20/20 [==============================] - 19s 939ms/step - loss: 1.1414 - accuracy: 0.9918 - val_loss: 1.3690 - val_accuracy: 0.9306\n",
886
+ "Epoch 69/100\n",
887
+ "20/20 [==============================] - ETA: 0s - loss: 1.1103 - accuracy: 0.9984\n",
888
+ "Epoch 69: val_loss improved from 1.36896 to 1.32771, saving model to model_weights_efficient_B5_2.h5\n",
889
+ "20/20 [==============================] - 20s 977ms/step - loss: 1.1103 - accuracy: 0.9984 - val_loss: 1.3277 - val_accuracy: 0.9306\n",
890
+ "Epoch 70/100\n",
891
+ "20/20 [==============================] - ETA: 0s - loss: 1.1277 - accuracy: 0.9886\n",
892
+ "Epoch 70: val_loss did not improve from 1.32771\n",
893
+ "20/20 [==============================] - 18s 897ms/step - loss: 1.1277 - accuracy: 0.9886 - val_loss: 1.3546 - val_accuracy: 0.9167\n",
894
+ "Epoch 71/100\n",
895
+ "20/20 [==============================] - ETA: 0s - loss: 1.0964 - accuracy: 0.9902\n",
896
+ "Epoch 71: val_loss improved from 1.32771 to 1.27567, saving model to model_weights_efficient_B5_2.h5\n",
897
+ "20/20 [==============================] - 19s 938ms/step - loss: 1.0964 - accuracy: 0.9902 - val_loss: 1.2757 - val_accuracy: 0.9306\n",
898
+ "Epoch 72/100\n",
899
+ "20/20 [==============================] - ETA: 0s - loss: 1.0688 - accuracy: 0.9886\n",
900
+ "Epoch 72: val_loss did not improve from 1.27567\n",
901
+ "20/20 [==============================] - 18s 907ms/step - loss: 1.0688 - accuracy: 0.9886 - val_loss: 1.3252 - val_accuracy: 0.9028\n",
902
+ "Epoch 73/100\n",
903
+ "20/20 [==============================] - ETA: 0s - loss: 1.0428 - accuracy: 0.9984\n",
904
+ "Epoch 73: val_loss did not improve from 1.27567\n",
905
+ "20/20 [==============================] - 18s 901ms/step - loss: 1.0428 - accuracy: 0.9984 - val_loss: 1.3001 - val_accuracy: 0.9028\n",
906
+ "Epoch 74/100\n",
907
+ "20/20 [==============================] - ETA: 0s - loss: 1.0202 - accuracy: 0.9951\n",
908
+ "Epoch 74: val_loss did not improve from 1.27567\n",
909
+ "20/20 [==============================] - 18s 895ms/step - loss: 1.0202 - accuracy: 0.9951 - val_loss: 1.4571 - val_accuracy: 0.8472\n",
910
+ "Epoch 75/100\n",
911
+ "20/20 [==============================] - ETA: 0s - loss: 1.0117 - accuracy: 0.9902\n",
912
+ "Epoch 75: val_loss did not improve from 1.27567\n",
913
+ "20/20 [==============================] - 19s 930ms/step - loss: 1.0117 - accuracy: 0.9902 - val_loss: 1.2980 - val_accuracy: 0.8889\n",
914
+ "Epoch 76/100\n",
915
+ "20/20 [==============================] - ETA: 0s - loss: 1.0119 - accuracy: 0.9918\n",
916
+ "Epoch 76: val_loss did not improve from 1.27567\n",
917
+ "20/20 [==============================] - 19s 968ms/step - loss: 1.0119 - accuracy: 0.9918 - val_loss: 1.2769 - val_accuracy: 0.9028\n",
918
+ "Epoch 76: early stopping\n"
919
+ ]
920
+ }
921
+ ]
922
+ },
923
+ {
924
+ "cell_type": "code",
925
+ "source": [
926
+ "# Calculate the total number of samples in the test dataset\n",
927
+ "ts_length = len(test_df)\n",
928
+ "# Determine the optimal test batch size within a reasonable range (1 to 80)\n",
929
+ "test_batch_size = max(sorted([ts_length // n for n in range(1, ts_length + 1) if ts_length%n == 0 and ts_length/n <= 80]))\n",
930
+ "# Calculate the number of steps to cover the entire test dataset using the determined test batch size\n",
931
+ "test_steps = ts_length // test_batch_size\n",
932
+ "\n",
933
+ "# Evaluate the EfficientNetB5base model on the training dataset and print the results\n",
934
+ "train_score = model.evaluate(train_gen, steps= test_steps, verbose= 1)\n",
935
+ "# Evaluate the EfficientNetB5 base model on the validation dataset and print the results\n",
936
+ "valid_score = model.evaluate(valid_gen, steps= test_steps, verbose= 1)\n",
937
+ "# Evaluate the EfficientNetB5 base model on the test dataset and print the results\n",
938
+ "test_score = model.evaluate(test_gen, steps= test_steps, verbose= 1)\n",
939
+ "\n",
940
+ "# Print the evaluation results for the training dataset\n",
941
+ "print(\"Train Loss: \", train_score[0])\n",
942
+ "print(\"Train Accuracy: \", train_score[1])\n",
943
+ "print('-' * 20)\n",
944
+ "\n",
945
+ "# Print the evaluation results for the validation dataset\n",
946
+ "print(\"Validation Loss: \", valid_score[0])\n",
947
+ "print(\"Validation Accuracy: \", valid_score[1])\n",
948
+ "print('-' * 20)\n",
949
+ "\n",
950
+ "# Print the evaluation results for the test dataset\n",
951
+ "print(\"Test Loss: \", test_score[0])\n",
952
+ "print(\"Test Loss: \", test_score[0])\n",
953
+ "print(\"Test Accuracy: \", test_score[1])"
954
+ ],
955
+ "metadata": {
956
+ "colab": {
957
+ "base_uri": "https://localhost:8080/"
958
+ },
959
+ "id": "mf1mrDrLpGXF",
960
+ "outputId": "85ada66f-d5a3-4cd5-b1ad-b6ab756c89bf"
961
+ },
962
+ "execution_count": 15,
963
+ "outputs": [
964
+ {
965
+ "output_type": "stream",
966
+ "name": "stdout",
967
+ "text": [
968
+ "5/5 [==============================] - 2s 276ms/step - loss: 0.9960 - accuracy: 1.0000\n",
969
+ "3/5 [=================>............] - ETA: 0s - loss: 1.2769 - accuracy: 0.9028"
970
+ ]
971
+ },
972
+ {
973
+ "output_type": "stream",
974
+ "name": "stderr",
975
+ "text": [
976
+ "WARNING:tensorflow:Your input ran out of data; interrupting training. Make sure that your dataset or generator can generate at least `steps_per_epoch * epochs` batches (in this case, 5 batches). You may need to use the repeat() function when building your dataset.\n"
977
+ ]
978
+ },
979
+ {
980
+ "output_type": "stream",
981
+ "name": "stdout",
982
+ "text": [
983
+ "5/5 [==============================] - 1s 213ms/step - loss: 1.2769 - accuracy: 0.9028\n",
984
+ "5/5 [==============================] - 150s 37s/step - loss: 1.2655 - accuracy: 0.9111\n",
985
+ "Train Loss: 0.9959659576416016\n",
986
+ "Train Accuracy: 1.0\n",
987
+ "--------------------\n",
988
+ "Validation Loss: 1.2768819332122803\n",
989
+ "Validation Accuracy: 0.9027777910232544\n",
990
+ "--------------------\n",
991
+ "Test Loss: 1.2654653787612915\n",
992
+ "Test Accuracy: 0.9111111164093018\n"
993
+ ]
994
+ }
995
+ ]
996
+ },
997
+ {
998
+ "cell_type": "markdown",
999
+ "source": [
1000
+ "# EfficientNet B5\n",
1001
+ "## (The Above model is EfficientNetB5 which shows best accuracy compare to other models)\n",
1002
+ "## Train Accuracy: 100%\n",
1003
+ "## Validation Accuracy: 90.2%\n",
1004
+ "## Test Accuracy: 91.11%"
1005
+ ],
1006
+ "metadata": {
1007
+ "id": "3aYDXYnm71Wd"
1008
+ }
1009
+ },
1010
+ {
1011
+ "cell_type": "markdown",
1012
+ "source": [
1013
+ "# VGG19\n",
1014
+ "## Train Accuracy: 100%\n",
1015
+ "## Validation Accuracy: 80.56%\n",
1016
+ "## Test Accuracy: 79.05%"
1017
+ ],
1018
+ "metadata": {
1019
+ "id": "av1hgCOj-VLh"
1020
+ }
1021
+ },
1022
+ {
1023
+ "cell_type": "markdown",
1024
+ "source": [
1025
+ "# VGG16\n",
1026
+ "## Train Accuracy: 100%\n",
1027
+ "## Validation Accuracy: 79.16%\n",
1028
+ "## Test Accuracy: 76.19%"
1029
+ ],
1030
+ "metadata": {
1031
+ "id": "shJGEpmM-iSU"
1032
+ }
1033
+ },
1034
+ {
1035
+ "cell_type": "code",
1036
+ "source": [
1037
+ "import shutil\n",
1038
+ "\n",
1039
+ "# Source path\n",
1040
+ "source_path = \"content/model_weights_efficient_B5_2.h5\"\n",
1041
+ "\n",
1042
+ "# Destination path (Data folder)\n",
1043
+ "destination_path = \"drive/MyDrive/LungCancer-IITM/Data/model_weights_efficient_B5_2.h5\"\n",
1044
+ "\n",
1045
+ "# Move the file\n",
1046
+ "shutil.move(source_path, destination_path)\n",
1047
+ "\n",
1048
+ "print(f\"File moved from {source_path} to {destination_path}\")"
1049
+ ],
1050
+ "metadata": {
1051
+ "id": "nF-O7RYjEFCi"
1052
+ },
1053
+ "execution_count": 22,
1054
+ "outputs": []
1055
+ },
1056
+ {
1057
+ "cell_type": "code",
1058
+ "source": [
1059
+ "# EfficientNetB5 model link:-\n",
1060
+ "google_drive_link = \"https://drive.google.com/file/d/1ppJ_h5jE3tr2-n0x1TBzx8CEfCdAg9TD/view?usp=drive_link\""
1061
+ ],
1062
+ "metadata": {
1063
+ "id": "qXQfOXJGEo9R"
1064
+ },
1065
+ "execution_count": null,
1066
+ "outputs": []
1067
+ },
1068
+ {
1069
+ "cell_type": "markdown",
1070
+ "source": [
1071
+ "#Thank You..."
1072
+ ],
1073
+ "metadata": {
1074
+ "id": "F1XWcOHaE8gc"
1075
+ }
1076
+ }
1077
+ ]
1078
+ }