{"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":"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":{"execution":{"iopub.status.busy":"2024-07-09T06:41:03.122586Z","iopub.execute_input":"2024-07-09T06:41:03.122922Z","iopub.status.idle":"2024-07-09T06:41:04.529619Z","shell.execute_reply.started":"2024-07-09T06:41:03.122897Z","shell.execute_reply":"2024-07-09T06:41:04.528624Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels = ['PNEUMONIA', 'NORMAL']\nimg_size = 128\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 in os.listdir(path):\n            try:\n                img_arr = cv2.imread(os.path.join(path, img), cv2.IMREAD_GRAYSCALE)\n                resized_arr = cv2.resize(img_arr, (img_size, img_size))\n                if resized_arr.shape != (img_size, img_size):\n                    print(f\"Ignoring image: {img}. Invalid shape: {resized_arr.shape}\")\n                    continue\n                data.append([resized_arr, class_num])\n            except Exception as e:\n                print(e)\n    return np.array(data, dtype=object)","metadata":{"execution":{"iopub.status.busy":"2024-07-09T06:41:08.752597Z","iopub.execute_input":"2024-07-09T06:41:08.753593Z","iopub.status.idle":"2024-07-09T06:41:08.761541Z","shell.execute_reply.started":"2024-07-09T06:41:08.753558Z","shell.execute_reply":"2024-07-09T06:41:08.760427Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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":{"execution":{"iopub.status.busy":"2024-07-09T06:41:14.080128Z","iopub.execute_input":"2024-07-09T06:41:14.080748Z","iopub.status.idle":"2024-07-09T06:42:46.572613Z","shell.execute_reply.started":"2024-07-09T06:41:14.080713Z","shell.execute_reply":"2024-07-09T06:42:46.571795Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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":{"execution":{"iopub.status.busy":"2024-07-09T06:43:15.959071Z","iopub.execute_input":"2024-07-09T06:43:15.959913Z","iopub.status.idle":"2024-07-09T06:43:15.968377Z","shell.execute_reply.started":"2024-07-09T06:43:15.959881Z","shell.execute_reply":"2024-07-09T06:43:15.967459Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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":{"execution":{"iopub.status.busy":"2024-07-09T06:43:18.658285Z","iopub.execute_input":"2024-07-09T06:43:18.659149Z","iopub.status.idle":"2024-07-09T06:43:21.223944Z","shell.execute_reply.started":"2024-07-09T06:43:18.659116Z","shell.execute_reply":"2024-07-09T06:43:21.222643Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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":{"execution":{"iopub.status.busy":"2024-07-09T06:43:27.290971Z","iopub.execute_input":"2024-07-09T06:43:27.291393Z","iopub.status.idle":"2024-07-09T06:43:27.298211Z","shell.execute_reply.started":"2024-07-09T06:43:27.291361Z","shell.execute_reply":"2024-07-09T06:43:27.296973Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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":{"execution":{"iopub.status.busy":"2024-07-09T06:43:33.519589Z","iopub.execute_input":"2024-07-09T06:43:33.519960Z","iopub.status.idle":"2024-07-09T06:43:36.832989Z","shell.execute_reply.started":"2024-07-09T06:43:33.519935Z","shell.execute_reply":"2024-07-09T06:43:36.831653Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import seaborn as sns\n\nlist_pneumonia_and_normal = []\nfor i in train:\n    if i[1] == 0:\n        list_pneumonia_and_normal.append(\"Pneumonia\")\n    else:\n        list_pneumonia_and_normal.append(\"Normal\")\nsns.countplot(x=list_pneumonia_and_normal)","metadata":{"execution":{"iopub.status.busy":"2024-07-09T06:43:41.830559Z","iopub.execute_input":"2024-07-09T06:43:41.830937Z","iopub.status.idle":"2024-07-09T06:43:42.110664Z","shell.execute_reply.started":"2024-07-09T06:43:41.830906Z","shell.execute_reply":"2024-07-09T06:43:42.109508Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Data Augmentation & Resizing","metadata":{"execution":{"iopub.status.busy":"2024-07-08T20:41:47.137107Z","iopub.execute_input":"2024-07-08T20:41:47.137842Z","iopub.status.idle":"2024-07-08T20:41:47.142339Z","shell.execute_reply.started":"2024-07-08T20:41:47.137796Z","shell.execute_reply":"2024-07-08T20:41:47.141436Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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":{"execution":{"iopub.status.busy":"2024-07-09T06:43:49.764159Z","iopub.execute_input":"2024-07-09T06:43:49.764539Z","iopub.status.idle":"2024-07-09T06:44:02.730526Z","shell.execute_reply.started":"2024-07-09T06:43:49.764510Z","shell.execute_reply":"2024-07-09T06:44:02.729571Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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":{"execution":{"iopub.status.busy":"2024-07-09T06:44:02.732382Z","iopub.execute_input":"2024-07-09T06:44:02.733109Z","iopub.status.idle":"2024-07-09T06:44:02.739549Z","shell.execute_reply.started":"2024-07-09T06:44:02.733054Z","shell.execute_reply":"2024-07-09T06:44:02.738362Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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":{"execution":{"iopub.status.busy":"2024-07-09T06:44:06.975219Z","iopub.execute_input":"2024-07-09T06:44:06.975941Z","iopub.status.idle":"2024-07-09T06:44:08.120918Z","shell.execute_reply.started":"2024-07-09T06:44:06.975910Z","shell.execute_reply":"2024-07-09T06:44:08.119923Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class_labels = train_generator.class_indices","metadata":{"execution":{"iopub.status.busy":"2024-07-09T06:44:11.314211Z","iopub.execute_input":"2024-07-09T06:44:11.315202Z","iopub.status.idle":"2024-07-09T06:44:11.319558Z","shell.execute_reply.started":"2024-07-09T06:44:11.315167Z","shell.execute_reply":"2024-07-09T06:44:11.318468Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class_labels","metadata":{"execution":{"iopub.status.busy":"2024-07-09T06:44:17.821176Z","iopub.execute_input":"2024-07-09T06:44:17.821541Z","iopub.status.idle":"2024-07-09T06:44:17.827892Z","shell.execute_reply.started":"2024-07-09T06:44:17.821515Z","shell.execute_reply":"2024-07-09T06:44:17.826943Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class_name = {value:key for (key, value) in class_labels.items()}","metadata":{"execution":{"iopub.status.busy":"2024-07-09T06:44:33.155899Z","iopub.execute_input":"2024-07-09T06:44:33.156923Z","iopub.status.idle":"2024-07-09T06:44:33.161924Z","shell.execute_reply.started":"2024-07-09T06:44:33.156879Z","shell.execute_reply":"2024-07-09T06:44:33.160750Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class_name","metadata":{"execution":{"iopub.status.busy":"2024-07-09T06:44:33.296397Z","iopub.execute_input":"2024-07-09T06:44:33.297298Z","iopub.status.idle":"2024-07-09T06:44:33.304309Z","shell.execute_reply.started":"2024-07-09T06:44:33.297256Z","shell.execute_reply":"2024-07-09T06:44:33.303129Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# VGG19 CNN Architecture","metadata":{"execution":{"iopub.status.busy":"2024-07-08T20:42:09.909624Z","iopub.execute_input":"2024-07-08T20:42:09.909913Z","iopub.status.idle":"2024-07-08T20:42:09.920310Z","shell.execute_reply.started":"2024-07-08T20:42:09.909891Z","shell.execute_reply":"2024-07-08T20:42:09.919611Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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":{"execution":{"iopub.status.busy":"2024-07-09T06:49:09.410262Z","iopub.execute_input":"2024-07-09T06:49:09.411071Z","iopub.status.idle":"2024-07-09T06:49:09.826118Z","shell.execute_reply.started":"2024-07-09T06:49:09.411041Z","shell.execute_reply":"2024-07-09T06:49:09.825103Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.utils import plot_model\n\nplot_model(model_01)","metadata":{"execution":{"iopub.status.busy":"2024-07-09T06:46:15.601988Z","iopub.execute_input":"2024-07-09T06:46:15.602740Z","iopub.status.idle":"2024-07-09T06:46:16.066295Z","shell.execute_reply.started":"2024-07-09T06:46:15.602708Z","shell.execute_reply":"2024-07-09T06:46:16.065222Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#filepath = \"model.h5\"\nes = EarlyStopping(monitor=\"val_loss\", verbose=1, mode=\"min\", patience=4)\n#cp=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, decay = 1e-6, momentum=0., nesterov = True)\n\nmodel_01.compile(loss=\"categorical_crossentropy\", optimizer=sgd, metrics=['accuracy'])\n","metadata":{"execution":{"iopub.status.busy":"2024-07-08T20:42:12.294838Z","iopub.execute_input":"2024-07-08T20:42:12.295123Z","iopub.status.idle":"2024-07-08T20:42:12.314686Z","shell.execute_reply.started":"2024-07-08T20:42:12.295098Z","shell.execute_reply":"2024-07-08T20:42:12.313785Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history_01 = model_01.fit(train_generator, \n            steps_per_epoch=50,\n            epochs=1, \n            callbacks=[es,lrr],\n            validation_data=valid_generator)","metadata":{"execution":{"iopub.status.busy":"2024-07-08T20:42:12.315889Z","iopub.execute_input":"2024-07-08T20:42:12.316215Z","iopub.status.idle":"2024-07-08T20:43:00.872181Z","shell.execute_reply.started":"2024-07-08T20:42:12.316186Z","shell.execute_reply":"2024-07-08T20:43:00.871302Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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":{"execution":{"iopub.status.busy":"2024-07-08T20:43:00.873657Z","iopub.execute_input":"2024-07-08T20:43:00.873964Z","iopub.status.idle":"2024-07-08T20:43:01.399109Z","shell.execute_reply.started":"2024-07-08T20:43:00.873939Z","shell.execute_reply":"2024-07-08T20:43:01.398290Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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":{"execution":{"iopub.status.busy":"2024-07-08T20:43:01.400221Z","iopub.execute_input":"2024-07-08T20:43:01.400491Z","iopub.status.idle":"2024-07-08T20:43:01.432736Z","shell.execute_reply.started":"2024-07-08T20:43:01.400456Z","shell.execute_reply":"2024-07-08T20:43:01.431919Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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":{"execution":{"iopub.status.busy":"2024-07-08T20:43:01.433791Z","iopub.execute_input":"2024-07-08T20:43:01.434055Z","iopub.status.idle":"2024-07-08T20:43:08.598269Z","shell.execute_reply.started":"2024-07-08T20:43:01.434032Z","shell.execute_reply":"2024-07-08T20:43:08.597464Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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":{"execution":{"iopub.status.busy":"2024-07-08T20:43:08.599549Z","iopub.execute_input":"2024-07-08T20:43:08.599850Z","iopub.status.idle":"2024-07-08T20:43:08.605064Z","shell.execute_reply.started":"2024-07-08T20:43:08.599825Z","shell.execute_reply":"2024-07-08T20:43:08.604236Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"base_model = VGG19(include_top=False, input_shape=(128,128,3), weights = 'imagenet')\n\nbase_model_layer_names = [layer.name for layer in base_model.layers]\n\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_02 = Model(base_model.inputs, output)\n\nset_trainable = False\n#for 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":{"execution":{"iopub.status.busy":"2024-07-08T20:43:08.606460Z","iopub.execute_input":"2024-07-08T20:43:08.606749Z","iopub.status.idle":"2024-07-08T20:43:09.005601Z","shell.execute_reply.started":"2024-07-08T20:43:08.606727Z","shell.execute_reply":"2024-07-08T20:43:09.004630Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Increamental unfreezing & fine tuning","metadata":{"execution":{"iopub.status.busy":"2024-07-08T20:43:09.007005Z","iopub.execute_input":"2024-07-08T20:43:09.007390Z","iopub.status.idle":"2024-07-08T20:43:09.011804Z","shell.execute_reply.started":"2024-07-08T20:43:09.007356Z","shell.execute_reply":"2024-07-08T20:43:09.010918Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"base_model_layer_names","metadata":{"execution":{"iopub.status.busy":"2024-07-08T20:43:09.013111Z","iopub.execute_input":"2024-07-08T20:43:09.013737Z","iopub.status.idle":"2024-07-08T20:43:09.026698Z","shell.execute_reply.started":"2024-07-08T20:43:09.013706Z","shell.execute_reply":"2024-07-08T20:43:09.025620Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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)\n\ntrain_generator = train_datagen.flow_from_directory(\n        # This is the target directory\n        \"/kaggle/input/chest-xray-pneumonia/chest_xray/train\",\n        # All images will be resized to 150x150\n        target_size=(128, 128),\n        batch_size=32,\n        # Since we use binary_crossentropy loss, we need binary labels\n        class_mode='categorical')\n\nvalid_generator = test_datagen.flow_from_directory(\n        \"/kaggle/input/chest-xray-pneumonia/chest_xray/val\",\n        target_size=(128, 128),\n        batch_size=32,\n        class_mode='categorical')","metadata":{"execution":{"iopub.status.busy":"2024-07-08T20:43:29.188471Z","iopub.execute_input":"2024-07-08T20:43:29.189073Z","iopub.status.idle":"2024-07-08T20:43:31.014775Z","shell.execute_reply.started":"2024-07-08T20:43:29.189041Z","shell.execute_reply":"2024-07-08T20:43:31.013898Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sgd = SGD(learning_rate=0.0001, decay = 1e-6, momentum=0., nesterov = True)\n\nmodel_02.compile(loss=\"categorical_crossentropy\", optimizer=sgd, metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2024-07-08T20:43:46.468798Z","iopub.execute_input":"2024-07-08T20:43:46.469157Z","iopub.status.idle":"2024-07-08T20:43:46.479300Z","shell.execute_reply.started":"2024-07-08T20:43:46.469128Z","shell.execute_reply":"2024-07-08T20:43:46.478497Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history_02 = model_02.fit(\n            train_generator, \n            steps_per_epoch=10,                        \n            epochs=10, \n            callbacks=[es, lrr],\n            validation_data=valid_generator,\n             validation_steps = 5 )","metadata":{"execution":{"iopub.status.busy":"2024-07-08T21:08:17.097618Z","iopub.execute_input":"2024-07-08T21:08:17.097992Z","iopub.status.idle":"2024-07-08T21:09:05.074876Z","shell.execute_reply.started":"2024-07-08T21:08:17.097961Z","shell.execute_reply":"2024-07-08T21:09:05.073900Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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":{"execution":{"iopub.status.busy":"2024-07-08T21:03:27.675231Z","iopub.execute_input":"2024-07-08T21:03:27.675691Z","iopub.status.idle":"2024-07-08T21:03:28.204457Z","shell.execute_reply.started":"2024-07-08T21:03:27.675655Z","shell.execute_reply":"2024-07-08T21:03:28.203437Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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":{"execution":{"iopub.status.busy":"2024-07-08T21:04:17.953270Z","iopub.execute_input":"2024-07-08T21:04:17.954174Z","iopub.status.idle":"2024-07-08T21:04:25.650652Z","shell.execute_reply.started":"2024-07-08T21:04:17.954141Z","shell.execute_reply":"2024-07-08T21:04:25.649705Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Unfreezing and fine tuning the entire network","metadata":{},"execution_count":null,"outputs":[]},{"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":{"execution":{"iopub.status.busy":"2024-07-08T21:10:07.872198Z","iopub.execute_input":"2024-07-08T21:10:07.872929Z","iopub.status.idle":"2024-07-08T21:10:08.764375Z","shell.execute_reply.started":"2024-07-08T21:10:07.872897Z","shell.execute_reply":"2024-07-08T21:10:08.763404Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sgd = SGD(learning_rate=0.0001, decay = 1e-6, momentum=0., nesterov = True)\n\nmodel_03.compile(loss=\"categorical_crossentropy\", optimizer=sgd, metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2024-07-08T21:10:28.846382Z","iopub.execute_input":"2024-07-08T21:10:28.847130Z","iopub.status.idle":"2024-07-08T21:10:28.858252Z","shell.execute_reply.started":"2024-07-08T21:10:28.847092Z","shell.execute_reply":"2024-07-08T21:10:28.857313Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history_03 = model_02.fit(train_generator, \n            steps_per_epoch=100,\n            epochs=1, \n            callbacks=[es,lrr],\n            validation_data=valid_generator)","metadata":{"execution":{"iopub.status.busy":"2024-07-08T21:10:47.445715Z","iopub.execute_input":"2024-07-08T21:10:47.446059Z","iopub.status.idle":"2024-07-08T21:11:33.453170Z","shell.execute_reply.started":"2024-07-08T21:10:47.446033Z","shell.execute_reply":"2024-07-08T21:11:33.452246Z"},"trusted":true},"execution_count":null,"outputs":[]}]}