{"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"nvidiaTeslaT4","dataSources":[{"sourceId":10338,"databundleVersionId":862042,"sourceType":"competition"},{"sourceId":482,"sourceType":"datasetVersion","datasetId":228},{"sourceId":7773,"sourceType":"datasetVersion","datasetId":4667},{"sourceId":18613,"sourceType":"datasetVersion","datasetId":5839},{"sourceId":20797,"sourceType":"datasetVersion","datasetId":15700},{"sourceId":23812,"sourceType":"datasetVersion","datasetId":17810},{"sourceId":477177,"sourceType":"datasetVersion","datasetId":216167},{"sourceId":519715,"sourceType":"datasetVersion","datasetId":246422},{"sourceId":951996,"sourceType":"datasetVersion","datasetId":516716},{"sourceId":1019494,"sourceType":"datasetVersion","datasetId":560711},{"sourceId":1022626,"sourceType":"datasetVersion","datasetId":562468},{"sourceId":1157383,"sourceType":"datasetVersion","datasetId":548681},{"sourceId":1166777,"sourceType":"datasetVersion","datasetId":661308},{"sourceId":1426603,"sourceType":"datasetVersion","datasetId":835414},{"sourceId":1432479,"sourceType":"datasetVersion","datasetId":839140},{"sourceId":1494905,"sourceType":"datasetVersion","datasetId":724418},{"sourceId":2047221,"sourceType":"datasetVersion","datasetId":1226038},{"sourceId":2332307,"sourceType":"datasetVersion","datasetId":891819},{"sourceId":7079240,"sourceType":"datasetVersion","datasetId":4077867}],"dockerImageVersionId":30698,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# Import Libraries & Reading Dataset","metadata":{}},{"cell_type":"code","source":"import os, shutil\nimport random\nimport numpy as np\nimport pandas as pd\nimport cv2\nimport skimage\nimport matplotlib.pyplot as plt\nimport skimage.segmentation\nimport seaborn as sns\n%matplotlib inline\nplt.style.use('ggplot')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-28T11:52:34.417153Z","iopub.execute_input":"2025-05-28T11:52:34.417914Z","iopub.status.idle":"2025-05-28T11:52:34.423972Z","shell.execute_reply.started":"2025-05-28T11:52:34.417886Z","shell.execute_reply":"2025-05-28T11:52:34.423025Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"img_size = 128\nlabels = ['PNEUMONIA', 'NORMAL']\n\ndef get_data(data_dir):\n    data = []\n    for label in labels:\n        path = os.path.join(data_dir, label)\n        class_num = labels.index(label)\n        for img_name in os.listdir(path):\n            try:\n                img_arr = cv2.imread(os.path.join(path, img_name), cv2.IMREAD_GRAYSCALE)\n                if img_arr is None:\n                    continue  # skip unreadable images\n                resized_arr = cv2.resize(img_arr, (img_size, img_size))\n                data.append((resized_arr, class_num))  # tuple, not list\n            except Exception as e:\n                print(f\"Error loading image {img_name}: {e}\")\n    return data  # list of (image, label) tuples","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-28T11:56:49.595142Z","iopub.execute_input":"2025-05-28T11:56:49.595980Z","iopub.status.idle":"2025-05-28T11:56:49.601581Z","shell.execute_reply.started":"2025-05-28T11:56:49.595950Z","shell.execute_reply":"2025-05-28T11:56:49.600790Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train = get_data(\"/kaggle/input/chest-xray-pneumonia/chest_xray/train\")\ntest = get_data(\"/kaggle/input/chest-xray-pneumonia/chest_xray/test\")\nval = get_data(\"/kaggle/input/chest-xray-pneumonia/chest_xray/val\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-28T11:44:31.774146Z","iopub.execute_input":"2025-05-28T11:44:31.774518Z","iopub.status.idle":"2025-05-28T11:45:16.050582Z","shell.execute_reply.started":"2025-05-28T11:44:31.774494Z","shell.execute_reply":"2025-05-28T11:45:16.049303Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pneumonia = os.listdir(\"/kaggle/input/chest-xray-pneumonia/chest_xray/train/PNEUMONIA\")\npenomina_dir = \"/kaggle/input/chest-xray-pneumonia/chest_xray/train/PNEUMONIA\"","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-28T11:57:56.844231Z","iopub.execute_input":"2025-05-28T11:57:56.844890Z","iopub.status.idle":"2025-05-28T11:57:56.852300Z","shell.execute_reply.started":"2025-05-28T11:57:56.844860Z","shell.execute_reply":"2025-05-28T11:57:56.851550Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.figure(figsize=(20,10))\n\nfor i in range(9):\n    plt.subplot(3,3, i+1)\n    img = plt.imread(os.path.join(penomina_dir, pneumonia[i]))\n    plt.imshow(img, cmap='gray')\n    plt.axis(\"off\")\n    plt.title(\"Pneumonia X-ray\")\nplt.tight_layout()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-28T11:57:58.678903Z","iopub.execute_input":"2025-05-28T11:57:58.679534Z","iopub.status.idle":"2025-05-28T11:58:00.904942Z","shell.execute_reply.started":"2025-05-28T11:57:58.679508Z","shell.execute_reply":"2025-05-28T11:58:00.904065Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"normal = os.listdir(\"/kaggle/input/chest-xray-pneumonia/chest_xray/train/NORMAL\")\nnormal_dir = \"/kaggle/input/chest-xray-pneumonia/chest_xray/train/NORMAL\"","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-28T11:58:01.300633Z","iopub.execute_input":"2025-05-28T11:58:01.300953Z","iopub.status.idle":"2025-05-28T11:58:01.306064Z","shell.execute_reply.started":"2025-05-28T11:58:01.300930Z","shell.execute_reply":"2025-05-28T11:58:01.305134Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.figure(figsize=(20,10))\n\nfor i in range(9):\n    plt.subplot(3,3, i+1)\n    img = plt.imread(os.path.join(normal_dir, normal[i]))\n    plt.imshow(img, cmap='gray')\n    plt.axis(\"off\")\n    plt.title(\"Normal X-ray\")\nplt.tight_layout()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-28T11:58:04.064915Z","iopub.execute_input":"2025-05-28T11:58:04.065618Z","iopub.status.idle":"2025-05-28T11:58:06.862923Z","shell.execute_reply.started":"2025-05-28T11:58:04.065574Z","shell.execute_reply":"2025-05-28T11:58:06.862112Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import seaborn as sns\nimport matplotlib.pyplot as plt\n\nlistx = []\nfor i in train:\n    if i[1] == 0:\n        listx.append(\"Pneumonia\")\n    else:\n        listx.append(\"Normal\")\n\nsns.countplot(x=listx)\nplt.title(\"Class Distribution in Training Set\")\nplt.xlabel(\"Class\")\nplt.ylabel(\"Count\")\nplt.show()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-28T11:58:55.975451Z","iopub.execute_input":"2025-05-28T11:58:55.976326Z","iopub.status.idle":"2025-05-28T11:58:56.242833Z","shell.execute_reply.started":"2025-05-28T11:58:55.976291Z","shell.execute_reply":"2025-05-28T11:58:56.242003Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Data Augmentation & Resizing","metadata":{}},{"cell_type":"code","source":"import tensorflow as tf\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras.models import Model\nfrom tensorflow.keras.models import load_model\nfrom tensorflow.keras.layers import Input, Dense, Flatten, Conv2D,Dropout\nfrom tensorflow.keras.applications.vgg19 import VGG19\nfrom tensorflow.keras.optimizers import SGD, RMSprop, Adam\nfrom tensorflow.keras.callbacks import ModelCheckpoint, EarlyStopping, ReduceLROnPlateau","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-28T11:59:01.096159Z","iopub.execute_input":"2025-05-28T11:59:01.096884Z","iopub.status.idle":"2025-05-28T11:59:15.201163Z","shell.execute_reply.started":"2025-05-28T11:59:01.096859Z","shell.execute_reply":"2025-05-28T11:59:15.200447Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_datagen = ImageDataGenerator(rescale = 1. / 255, \n                  horizontal_flip=0.4,\n                  vertical_flip=0.4,\n                  rotation_range=40,\n                  shear_range=0.2,\n                  width_shift_range=0.4,\n                  height_shift_range=0.4,\n                  fill_mode=\"nearest\")\nvalid_datagen = ImageDataGenerator(rescale = 1./255)\ntest_datagen = ImageDataGenerator(rescale = 1./255)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-28T11:59:20.507629Z","iopub.execute_input":"2025-05-28T11:59:20.508489Z","iopub.status.idle":"2025-05-28T11:59:20.512856Z","shell.execute_reply.started":"2025-05-28T11:59:20.508462Z","shell.execute_reply":"2025-05-28T11:59:20.511886Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_generator = train_datagen.flow_from_directory(\"/kaggle/input/chest-xray-pneumonia/chest_xray/train\",\n                                 batch_size = 32,\n                                 target_size=(128,128),\n                                 class_mode = 'categorical',\n                                 shuffle=True,\n                                 seed = 42,\n                                 color_mode = 'rgb')\nvalid_generator = valid_datagen.flow_from_directory(\"/kaggle/input/chest-xray-pneumonia/chest_xray/val\",\n                                 batch_size = 32,\n                                 target_size=(128,128),\n                                 class_mode = 'categorical',\n                                 shuffle=True,\n                                 seed = 42,\n                                 color_mode = 'rgb')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-28T11:59:49.209973Z","iopub.execute_input":"2025-05-28T11:59:49.210556Z","iopub.status.idle":"2025-05-28T11:59:50.892746Z","shell.execute_reply.started":"2025-05-28T11:59:49.210529Z","shell.execute_reply":"2025-05-28T11:59:50.892041Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"class_labels = train_generator.class_indices","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-28T11:59:56.509526Z","iopub.execute_input":"2025-05-28T11:59:56.510281Z","iopub.status.idle":"2025-05-28T11:59:56.513954Z","shell.execute_reply.started":"2025-05-28T11:59:56.510255Z","shell.execute_reply":"2025-05-28T11:59:56.512942Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"class_labels","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-28T11:59:59.661508Z","iopub.execute_input":"2025-05-28T11:59:59.662262Z","iopub.status.idle":"2025-05-28T11:59:59.667487Z","shell.execute_reply.started":"2025-05-28T11:59:59.662235Z","shell.execute_reply":"2025-05-28T11:59:59.666579Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"class_name = {value:key for (key, value) in class_labels.items()}","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-28T12:00:05.101368Z","iopub.execute_input":"2025-05-28T12:00:05.101721Z","iopub.status.idle":"2025-05-28T12:00:05.105946Z","shell.execute_reply.started":"2025-05-28T12:00:05.101695Z","shell.execute_reply":"2025-05-28T12:00:05.104962Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"class_name","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-28T12:00:07.476838Z","iopub.execute_input":"2025-05-28T12:00:07.477512Z","iopub.status.idle":"2025-05-28T12:00:07.482517Z","shell.execute_reply.started":"2025-05-28T12:00:07.477487Z","shell.execute_reply":"2025-05-28T12:00:07.481657Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# VGG19 CNN Architecture","metadata":{}},{"cell_type":"code","source":"base_model = VGG19(input_shape = (128,128,3),\n                     include_top = False,\n                     weights = 'imagenet')\nfor layer in base_model.layers:\n    layer.trainable = False\n\nx = base_model.output\nflat = Flatten()(x)\n\n\nclass_1 = Dense(4608, activation = 'relu')(flat)\ndropout = Dropout(0.2)(class_1)\nclass_2 = Dense(1152, activation = 'relu')(dropout)\noutput = Dense(2, activation = 'softmax')(class_2)\n\nmodel_01 = Model(base_model.inputs, output)\nmodel_01.summary()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-28T12:00:14.055827Z","iopub.execute_input":"2025-05-28T12:00:14.056472Z","iopub.status.idle":"2025-05-28T12:00:15.921513Z","shell.execute_reply.started":"2025-05-28T12:00:14.056447Z","shell.execute_reply":"2025-05-28T12:00:15.920716Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"filepath = \"model.keras\"\nes = EarlyStopping(monitor=\"val_loss\", verbose=1, mode=\"min\", patience=4)\ncp = ModelCheckpoint(filepath, monitor=\"val_loss\", save_best_only=True, save_weights_only=False, mode=\"auto\", save_freq=\"epoch\")\nlrr = ReduceLROnPlateau(monitor=\"val_accuracy\", patience=3, verbose=1, factor=0.5, min_lr=0.0001)\n\nsgd = SGD(learning_rate=0.0001, momentum=0.0, nesterov=True)\n\nmodel_01.compile(loss=\"categorical_crossentropy\", optimizer=sgd, metrics=['accuracy'])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-28T12:02:15.999262Z","iopub.execute_input":"2025-05-28T12:02:15.999894Z","iopub.status.idle":"2025-05-28T12:02:16.011672Z","shell.execute_reply.started":"2025-05-28T12:02:15.999866Z","shell.execute_reply":"2025-05-28T12:02:16.010663Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"history_01 = model_01.fit(train_generator, \n            steps_per_epoch=50,\n            epochs=1, \n            callbacks=[es, cp, lrr],\n            validation_data=valid_generator)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-28T12:02:19.147075Z","iopub.execute_input":"2025-05-28T12:02:19.147683Z","iopub.status.idle":"2025-05-28T12:03:06.454183Z","shell.execute_reply.started":"2025-05-28T12:02:19.147654Z","shell.execute_reply":"2025-05-28T12:03:06.453562Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"if not os.path.isdir('model_weights/'):\n    os.mkdir(\"model_weights/\")\nmodel_01.save(filepath = \"model_weights/vgg19_model_01.h5\", overwrite=True)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-28T12:04:03.161344Z","iopub.execute_input":"2025-05-28T12:04:03.161809Z","iopub.status.idle":"2025-05-28T12:04:03.677209Z","shell.execute_reply.started":"2025-05-28T12:04:03.161783Z","shell.execute_reply":"2025-05-28T12:04:03.676253Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_generator = test_datagen.flow_from_directory(\"/kaggle/input/chest-xray-pneumonia/chest_xray/test\",\n                                 batch_size = 32,\n                                 target_size=(128,128),\n                                 class_mode = 'categorical',\n                                 shuffle=True,\n                                 seed = 42,\n                                 color_mode = 'rgb')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-28T12:04:43.571006Z","iopub.execute_input":"2025-05-28T12:04:43.571574Z","iopub.status.idle":"2025-05-28T12:04:44.412665Z","shell.execute_reply.started":"2025-05-28T12:04:43.571549Z","shell.execute_reply":"2025-05-28T12:04:44.411817Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model_01.load_weights(\"model_weights/vgg19_model_01.h5\")\n\nvgg_val_eval_01 = model_01.evaluate(valid_generator)\nvgg_test_eval_01 = model_01.evaluate(test_generator)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-28T12:04:49.581522Z","iopub.execute_input":"2025-05-28T12:04:49.581857Z","iopub.status.idle":"2025-05-28T12:04:56.006686Z","shell.execute_reply.started":"2025-05-28T12:04:49.581835Z","shell.execute_reply":"2025-05-28T12:04:56.005952Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(f\"Validation Loss: {vgg_val_eval_01[0]}\")\nprint(f\"Validation Accuarcy: {vgg_val_eval_01[1]}\")\nprint(f\"Test Loss: {vgg_test_eval_01[0]}\")\nprint(f\"Test Accuarcy: {vgg_test_eval_01[1]}\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-28T12:05:05.717409Z","iopub.execute_input":"2025-05-28T12:05:05.717726Z","iopub.status.idle":"2025-05-28T12:05:05.722129Z","shell.execute_reply.started":"2025-05-28T12:05:05.717704Z","shell.execute_reply":"2025-05-28T12:05:05.721166Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Increamental unfreezing & fine tuning","metadata":{}},{"cell_type":"code","source":"base_model = VGG19(include_top=False, input_shape=(128,128,3))\nbase_model_layer_names = [layer.name for layer in base_model.layers]\n\nx = base_model.output\nflat = Flatten()(x)\n\n\nclass_1 = Dense(4608, activation = 'relu')(flat)\ndropout = Dropout(0.2)(class_1)\nclass_2 = Dense(1152, activation = 'relu')(dropout)\noutput = Dense(2, activation = 'softmax')(class_2)\n\nmodel_02 = Model(base_model.inputs, output)\nmodel_02.load_weights(\"model_weights/vgg19_model_01.h5\")\n\nset_trainable = False\nfor layer in base_model.layers:\n    if layer.name in [ 'block5_conv3','block5_conv4']:\n        set_trainable=True\n    if set_trainable:\n        set_trainable=True\n    else:\n        set_trainable=False\nprint(model_02.summary())","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-28T12:05:14.427652Z","iopub.execute_input":"2025-05-28T12:05:14.428226Z","iopub.status.idle":"2025-05-28T12:05:15.247257Z","shell.execute_reply.started":"2025-05-28T12:05:14.428199Z","shell.execute_reply":"2025-05-28T12:05:15.246447Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"base_model_layer_names","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-28T12:05:21.164326Z","iopub.execute_input":"2025-05-28T12:05:21.165030Z","iopub.status.idle":"2025-05-28T12:05:21.170733Z","shell.execute_reply.started":"2025-05-28T12:05:21.164986Z","shell.execute_reply":"2025-05-28T12:05:21.169770Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"sgd = SGD(learning_rate=0.0001, momentum=0.0, nesterov=True)\n\nmodel_02.compile(loss=\"categorical_crossentropy\", optimizer=sgd, metrics=['accuracy'])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-28T12:05:57.328492Z","iopub.execute_input":"2025-05-28T12:05:57.329350Z","iopub.status.idle":"2025-05-28T12:05:57.337481Z","shell.execute_reply.started":"2025-05-28T12:05:57.329321Z","shell.execute_reply":"2025-05-28T12:05:57.336654Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"history_02 = model_02.fit(train_generator, \n            steps_per_epoch=10,\n            epochs=1, \n            callbacks=[es, cp, lrr],\n            validation_data=valid_generator)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-28T12:05:59.715438Z","iopub.execute_input":"2025-05-28T12:05:59.716261Z","iopub.status.idle":"2025-05-28T12:06:33.755333Z","shell.execute_reply.started":"2025-05-28T12:05:59.716230Z","shell.execute_reply":"2025-05-28T12:06:33.754659Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"if not os.path.isdir('model_weights/'):\n    os.mkdir(\"model_weights/\")\nmodel_02.save(filepath = \"model_weights/vgg19_model_02.h5\", overwrite=True)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-28T12:06:39.314973Z","iopub.execute_input":"2025-05-28T12:06:39.315728Z","iopub.status.idle":"2025-05-28T12:06:39.779822Z","shell.execute_reply.started":"2025-05-28T12:06:39.315696Z","shell.execute_reply":"2025-05-28T12:06:39.779102Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model_02.load_weights(\"model_weights/vgg19_model_02.h5\")\n\nvgg_val_eval_02 = model_02.evaluate(valid_generator)\nvgg_test_eval_02 = model_02.evaluate(test_generator)\n\nprint(f\"Validation Loss: {vgg_val_eval_02[0]}\")\nprint(f\"Validation Accuarcy: {vgg_val_eval_02[1]}\")\nprint(f\"Test Loss: {vgg_test_eval_02[0]}\")\nprint(f\"Test Accuarcy: {vgg_test_eval_02[1]}\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-28T12:06:42.952388Z","iopub.execute_input":"2025-05-28T12:06:42.953104Z","iopub.status.idle":"2025-05-28T12:06:49.144786Z","shell.execute_reply.started":"2025-05-28T12:06:42.953075Z","shell.execute_reply":"2025-05-28T12:06:49.143945Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Unfreezing and fine tuning the entire network","metadata":{}},{"cell_type":"code","source":"base_model = VGG19(include_top=False, input_shape=(128,128,3))\n\nx = base_model.output\nflat = Flatten()(x)\n\nclass_1 = Dense(4608, activation = 'relu')(flat)\ndropout = Dropout(0.2)(class_1)\nclass_2 = Dense(1152, activation = 'relu')(dropout)\noutput = Dense(2, activation = 'softmax')(class_2)\n\nmodel_03 = Model(base_model.inputs, output)\nmodel_03.load_weights(\"model_weights/vgg19_model_01.h5\")\n\nprint(model_03.summary())","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-28T12:06:55.292709Z","iopub.execute_input":"2025-05-28T12:06:55.293006Z","iopub.status.idle":"2025-05-28T12:06:56.079241Z","shell.execute_reply.started":"2025-05-28T12:06:55.292987Z","shell.execute_reply":"2025-05-28T12:06:56.078410Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"sgd = SGD(learning_rate=0.0001, momentum=0.0, nesterov=True)\n\nmodel_03.compile(loss=\"categorical_crossentropy\", optimizer=sgd, metrics=['accuracy'])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-28T12:07:04.866462Z","iopub.execute_input":"2025-05-28T12:07:04.867220Z","iopub.status.idle":"2025-05-28T12:07:04.875199Z","shell.execute_reply.started":"2025-05-28T12:07:04.867190Z","shell.execute_reply":"2025-05-28T12:07:04.874455Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"history_03 = model_02.fit(train_generator, \n            steps_per_epoch=100,\n            epochs=10, \n            callbacks=[es, cp, lrr],\n            validation_data=valid_generator)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-28T12:10:25.588147Z","iopub.execute_input":"2025-05-28T12:10:25.588495Z","iopub.status.idle":"2025-05-28T12:14:26.817571Z","shell.execute_reply.started":"2025-05-28T12:10:25.588470Z","shell.execute_reply":"2025-05-28T12:14:26.816659Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"if not os.path.isdir('model_weights/'):\n    os.mkdir(\"model_weights/\")\nmodel_02.save(filepath = \"model_weights/vgg19_model_03.h5\", overwrite=True)","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model_03.load_weights(\"model_weights/vgg19_model_03.h5\")\n\nvgg_val_eval_02 = model_02.evaluate(valid_generator)\nvgg_test_eval_02 = model_02.evaluate(test_generator)\n\nprint(f\"Validation Loss: {vgg_val_eval_02[0]}\")\nprint(f\"Validation Accuarcy: {vgg_val_eval_02[1]}\")\nprint(f\"Test Loss: {vgg_test_eval_02[0]}\")\nprint(f\"Test Accuarcy: {vgg_test_eval_02[1]}\")","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}