{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","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":3362,"databundleVersionId":31148,"sourceType":"competition"}],"dockerImageVersionId":30699,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-05-15T18:16:07.638470Z","iopub.execute_input":"2024-05-15T18:16:07.638792Z","iopub.status.idle":"2024-05-15T18:16:07.661790Z","shell.execute_reply.started":"2024-05-15T18:16:07.638767Z","shell.execute_reply":"2024-05-15T18:16:07.660863Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nfrom os import makedirs # make directory\nfrom os import listdir # list directories\nfrom shutil import copyfile\nfrom random import seed\nfrom random import random\nimport numpy as np\nimport pandas as pd\n\n# visuals\nimport seaborn as sns\nimport matplotlib.pyplot as plt\nfrom matplotlib.image import imread # Used to read images\nfrom PIL import Image # Image Visulization\n\n# Scikit-learn\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import classification_report,confusion_matrix,ConfusionMatrixDisplay\n\n# Tensorflow\nimport tensorflow as tf\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras.layers import Dense,MaxPooling2D,Dropout,Flatten,BatchNormalization,Conv2D\nfrom tensorflow.keras.callbacks import ReduceLROnPlateau,EarlyStopping,ModelCheckpoint\nfrom tensorflow.keras.utils import image_dataset_from_directory\nfrom tensorflow.keras.layers import Dense, Conv2D, MaxPooling2D, Flatten, BatchNormalization, Dropout\nfrom tensorflow.keras.layers import RandomFlip, RandomRotation, RandomZoom, RandomBrightness, Rescaling, RandomTranslation, RandomWidth, RandomHeight\nfrom tensorflow.keras.models import Sequential, Model\n# from tensorflow.keras.callbacks import EarlyStopping, ModelCheckpoint\nfrom tensorflow.keras.regularizers import L2\nfrom tensorflow.keras.applications import VGG16\nfrom tensorflow.keras.applications.vgg16 import preprocess_input\nfrom tensorflow.keras.preprocessing import image\nfrom tensorflow.keras.optimizers import Adamax","metadata":{"execution":{"iopub.status.busy":"2024-05-15T18:16:07.663451Z","iopub.execute_input":"2024-05-15T18:16:07.663745Z","iopub.status.idle":"2024-05-15T18:16:07.673737Z","shell.execute_reply.started":"2024-05-15T18:16:07.663720Z","shell.execute_reply":"2024-05-15T18:16:07.673048Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Data Importing","metadata":{}},{"cell_type":"code","source":"import zipfile\nwith zipfile.ZipFile(\"/kaggle/input/dogs-vs-cats/train.zip\", 'r') as zip_ref:\n  zip_ref.extractall(\"/kaggle/working/\")\nwith zipfile.ZipFile(\"/kaggle/input/dogs-vs-cats/test1.zip\", 'r') as zip_ref:\n  zip_ref.extractall(\"/kaggle/working/\")","metadata":{"execution":{"iopub.status.busy":"2024-05-15T18:16:07.674846Z","iopub.execute_input":"2024-05-15T18:16:07.675256Z","iopub.status.idle":"2024-05-15T18:16:19.218577Z","shell.execute_reply.started":"2024-05-15T18:16:07.675224Z","shell.execute_reply":"2024-05-15T18:16:19.217552Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_dir = \"../working/train/\"\n\nfilenames = os.listdir(image_dir)\nlabels = [x.split(\".\")[0] for x in filenames]\n\ndata = pd.DataFrame({\"filename\": filenames, \"label\": labels})\n\ndata.head()","metadata":{"execution":{"iopub.status.busy":"2024-05-15T18:16:19.219861Z","iopub.execute_input":"2024-05-15T18:16:19.220157Z","iopub.status.idle":"2024-05-15T18:16:19.254940Z","shell.execute_reply.started":"2024-05-15T18:16:19.220133Z","shell.execute_reply":"2024-05-15T18:16:19.254090Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"The shape of the data is:\",data.shape)","metadata":{"execution":{"iopub.status.busy":"2024-05-15T18:16:19.256909Z","iopub.execute_input":"2024-05-15T18:16:19.257214Z","iopub.status.idle":"2024-05-15T18:16:19.716638Z","shell.execute_reply.started":"2024-05-15T18:16:19.257189Z","shell.execute_reply":"2024-05-15T18:16:19.715655Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Spliting the data","metadata":{}},{"cell_type":"code","source":"labels = data['label']\n\n# 20% split\n# 80%.    20%\nX_train, X_temp = train_test_split(data, test_size=0.2, stratify=labels, random_state = 42)\n\nlabel_test_val = X_temp['label']\n\n# 10%.   10%\nX_test, X_val = train_test_split(X_temp, test_size=0.5, stratify=label_test_val, random_state = 42)\n\nprint('The shape of train data set is:',X_train.shape)\nprint('The shape of test data set is:',X_test.shape)\nprint('The shape of validation data set is:',X_val.shape)","metadata":{"execution":{"iopub.status.busy":"2024-05-15T18:16:19.717794Z","iopub.execute_input":"2024-05-15T18:16:19.718078Z","iopub.status.idle":"2024-05-15T18:16:19.780354Z","shell.execute_reply.started":"2024-05-15T18:16:19.718054Z","shell.execute_reply":"2024-05-15T18:16:19.779509Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# create directories\ndataset_home = 'dataset_dogs_vs_cats/'\nsubdirs = ['train/', 'test/']\n\nfor subdir in subdirs:\n    # create label subdirectories\n    labeldirs = ['dogs/', 'cats/']\n    for labldir in labeldirs:\n        newdir = dataset_home + subdir + labldir\n        makedirs(newdir, exist_ok=True) # Create 4 directories\n# seed random number generator\nseed(1)\n# define ratio of pictures to use for validation\nval_ratio = 0.2\n# copy training dataset images into subdirectories\nsrc_directory = 'train/'\nfor file in listdir(src_directory):\n        src = src_directory + '/' + file\n        dst_dir = 'train/'\n        if random() < val_ratio:\n            dst_dir = 'test/'\n        if file.startswith('cat'):\n            dst = dataset_home + dst_dir + 'cats/' + file\n            copyfile(src, dst)\n        elif file.startswith('dog'):\n            dst = dataset_home + dst_dir + 'dogs/' + file\n            copyfile(src, dst)\n\n            \npath1 = \"/kaggle/working/dataset_dogs_vs_cats/train/cats\"\npath2 = \"/kaggle/working/dataset_dogs_vs_cats/train/dogs\"\npath3 = \"/kaggle/working/dataset_dogs_vs_cats/test/cats\"\npath4 = \"/kaggle/working/dataset_dogs_vs_cats/test/dogs\"\n\n\nprint('Then number of cat images in training data is' ,len(os.listdir(path1)))\nprint('Then number of dog images in training data is' ,len(os.listdir(path2)))\nprint('Then number of cat images in validation data is' ,len(os.listdir(path3)))\nprint('Then number of dog images in validation data is' ,len(os.listdir(path4)))","metadata":{"execution":{"iopub.status.busy":"2024-05-15T18:16:19.781317Z","iopub.execute_input":"2024-05-15T18:16:19.781566Z","iopub.status.idle":"2024-05-15T18:16:23.326717Z","shell.execute_reply.started":"2024-05-15T18:16:19.781545Z","shell.execute_reply":"2024-05-15T18:16:23.325743Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_size = 128 # Size of the image\nimage_channel = 3 # Colour scale (RGB)\nbat_size = 32 # Number of files/images processed at once","metadata":{"execution":{"iopub.status.busy":"2024-05-15T18:17:04.046868Z","iopub.execute_input":"2024-05-15T18:17:04.047227Z","iopub.status.idle":"2024-05-15T18:17:04.051894Z","shell.execute_reply.started":"2024-05-15T18:17:04.047199Z","shell.execute_reply":"2024-05-15T18:17:04.050940Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_datagen = ImageDataGenerator(rescale=1./255)\n\ntest_datagen = ImageDataGenerator(rescale=1./255)","metadata":{"execution":{"iopub.status.busy":"2024-05-15T18:17:05.951189Z","iopub.execute_input":"2024-05-15T18:17:05.952014Z","iopub.status.idle":"2024-05-15T18:17:05.956433Z","shell.execute_reply.started":"2024-05-15T18:17:05.951983Z","shell.execute_reply":"2024-05-15T18:17:05.955382Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Applying image data gernerator to train and test data\n\ntrain_generator = train_datagen.flow_from_dataframe(X_train,\n                                                    directory = 'train/',\n                                                    x_col= 'filename',\n                                                    y_col= 'label',\n                                                    batch_size = bat_size,\n                                                    target_size = (image_size,image_size)\n                                                   )\nval_generator = test_datagen.flow_from_dataframe(X_val, \n                                                 directory = 'train/',\n                                                 x_col= 'filename',\n                                                 y_col= 'label',\n                                                 batch_size = bat_size,\n                                                 target_size = (image_size,image_size),\n                                                 shuffle=False\n                                                )\n\ntest_generator = test_datagen.flow_from_dataframe(X_test, \n                                                  directory = 'train/',\n                                                  x_col= 'filename',\n                                                  y_col= 'label',\n                                                  batch_size = bat_size,\n                                                  target_size = (image_size,image_size),\n                                                  shuffle=False\n                                                 )","metadata":{"execution":{"iopub.status.busy":"2024-05-15T18:17:07.516422Z","iopub.execute_input":"2024-05-15T18:17:07.517145Z","iopub.status.idle":"2024-05-15T18:17:07.813249Z","shell.execute_reply.started":"2024-05-15T18:17:07.517114Z","shell.execute_reply":"2024-05-15T18:17:07.812406Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_gen = train_datagen.flow_from_directory('/kaggle/working/dataset_dogs_vs_cats/train/', \n                                              class_mode='binary',\n                                              target_size = (image_size,image_size),\n                                              batch_size = bat_size,\n                                             )\n\nval_gen = test_datagen.flow_from_directory('/kaggle/working/dataset_dogs_vs_cats/test/', \n                                          class_mode='binary',\n                                          batch_size = bat_size,\n                                          target_size = (image_size,image_size),\n                                          shuffle = False\n                                         )","metadata":{"execution":{"iopub.status.busy":"2024-05-15T18:17:08.431114Z","iopub.execute_input":"2024-05-15T18:17:08.431406Z","iopub.status.idle":"2024-05-15T18:17:09.089619Z","shell.execute_reply.started":"2024-05-15T18:17:08.431382Z","shell.execute_reply":"2024-05-15T18:17:09.088878Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Convolutional Neural Network","metadata":{}},{"cell_type":"code","source":"from tensorflow.keras.models import Model\nfrom tensorflow.keras.layers import Input, Conv2D, MaxPooling2D, Flatten, Dense\n\nfrom tensorflow.keras.models import Model\nfrom tensorflow.keras.layers import Input, Conv2D, MaxPooling2D, Flatten, Dense\n\ndef VGG16_one(input_shape=(224, 224, 3)):\n    \"\"\"Instantiates the VGG16 architecture for binary classification.\n\n    # Arguments\n        input_shape: optional shape tuple, only to be specified\n            if `include_top` is False (otherwise the input shape\n            has to be `(224, 224, 3)` (with `channels_last` data format)\n            or `(3, 224, 244)` (with `channels_first` data format).\n            It should have exactly 3 inputs channels,\n            and width and height should be no smaller than 48.\n            E.g. `(200, 200, 3)` would be one valid value.\n\n    # Returns\n        A Keras model instance.\n    \"\"\"\n    img_input = Input(shape=input_shape)\n\n    # Block 1\n    x = Conv2D(64, (3, 3), activation='relu', padding='same', name='block1_conv1')(img_input)\n    x = Conv2D(64, (3, 3), activation='relu', padding='same', name='block1_conv2')(x)\n    x = MaxPooling2D((2, 2), strides=(2, 2), name='block1_pool')(x)\n\n    # Block 2\n    x = Conv2D(128, (3, 3), activation='relu', padding='same', name='block2_conv1')(x)\n    x = Conv2D(128, (3, 3), activation='relu', padding='same', name='block2_conv2')(x)\n    \n    x = MaxPooling2D((2, 2), strides=(2, 2), name='block2_pool')(x)\n\n    # Block 3\n    x = Conv2D(256, (3, 3), activation='relu', padding='same', name='block3_conv1')(x)\n    x = Conv2D(256, (3, 3), activation='relu', padding='same', name='block3_conv2')(x)\n    x = Conv2D(256, (3, 3), activation='relu', padding='same', name='block3_conv3')(x)\n    x = MaxPooling2D((2, 2), strides=(2, 2), name='block3_pool')(x)\n\n    # Block 4\n    x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block4_conv1')(x)\n    x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block4_conv2')(x)\n    x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block4_conv3')(x)\n    x = MaxPooling2D((2, 2), strides=(2, 2), name='block4_pool')(x)\n\n    # Block 5\n    x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block5_conv1')(x)\n    x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block5_conv2')(x)\n    x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block5_conv3')(x)\n    x = MaxPooling2D((2, 2), strides=(2, 2), name='block5_pool')(x)\n\n    # Classification block\n    x = Flatten(name='flatten')(x)\n    x = Dense(4096, activation='relu', name='fc1')(x)\n    x = Dense(4096, activation='relu', name='fc2')(x)\n    output = Dense(2, activation='softmax', name='predictions')(x)\n\n    # Create model\n    model = Model(img_input, output, name='vgg16_binary')\n\n    return model","metadata":{"execution":{"iopub.status.busy":"2024-05-15T18:17:09.712752Z","iopub.execute_input":"2024-05-15T18:17:09.713057Z","iopub.status.idle":"2024-05-15T18:17:09.729712Z","shell.execute_reply.started":"2024-05-15T18:17:09.713011Z","shell.execute_reply":"2024-05-15T18:17:09.728694Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_one = VGG16_one(input_shape=(image_size, image_size, image_channel))\nmodel_one.summary()","metadata":{"execution":{"iopub.status.busy":"2024-05-15T18:17:10.165743Z","iopub.execute_input":"2024-05-15T18:17:10.166048Z","iopub.status.idle":"2024-05-15T18:17:11.535511Z","shell.execute_reply.started":"2024-05-15T18:17:10.166004Z","shell.execute_reply":"2024-05-15T18:17:11.534589Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"1. **Convolutional layers:** The model uses convolutional layers with 3x3 filters to extract features from the input images. The number of filters in each layer increases as you move from the first block to the last block.\n        \n2. **Pooling layers:** The model uses max-pooling layers with a pool size of 2x2 to downsample the feature maps and reduce the spatial dimensions of the input images.\n3. **Flattening:** The output of the convolutional and pooling layers is flattened to prepare the data for the fully connected layers.\n4. **Fully connected layers:** The model uses two fully connected layers with ReLU activation functions to transform the flattened output into a higher-dimensional space.\n5. **Softmax output:** The final output layer uses a softmax activation function to produce a probability distribution over two classes.\n6. **Binary classification:** The model is designed for binary classification tasks, where the output is a probability distribution over two classes.","metadata":{}},{"cell_type":"code","source":"learning_rate_reduction = ReduceLROnPlateau(monitor = 'val_accuracy',\n                                            patience=2,\n                                            factor=0.5,\n                                            min_lr = 0.00001,\n                                            verbose = 1)","metadata":{"execution":{"iopub.status.busy":"2024-05-15T18:17:11.537128Z","iopub.execute_input":"2024-05-15T18:17:11.537858Z","iopub.status.idle":"2024-05-15T18:17:11.542328Z","shell.execute_reply.started":"2024-05-15T18:17:11.537823Z","shell.execute_reply":"2024-05-15T18:17:11.541301Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_one.compile(optimizer='adam',loss='binary_crossentropy',metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2024-05-15T18:17:11.986006Z","iopub.execute_input":"2024-05-15T18:17:11.986322Z","iopub.status.idle":"2024-05-15T18:17:12.000149Z","shell.execute_reply.started":"2024-05-15T18:17:11.986297Z","shell.execute_reply":"2024-05-15T18:17:11.999343Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_one_logs = model_one.fit(train_generator,\n                    validation_data = val_generator, \n                    callbacks=[learning_rate_reduction],\n                    epochs = 5)","metadata":{"execution":{"iopub.status.busy":"2024-05-15T18:17:12.554681Z","iopub.execute_input":"2024-05-15T18:17:12.554988Z","iopub.status.idle":"2024-05-15T18:25:22.747243Z","shell.execute_reply.started":"2024-05-15T18:17:12.554961Z","shell.execute_reply":"2024-05-15T18:25:22.746322Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n\n# Plotting function\ndef plot_history(history):\n    plt.figure(figsize=(12, 6))\n    plt.subplot(1, 2, 1)\n    plt.plot(history.history['accuracy'], label='Training Accuracy')\n    plt.plot(history.history['val_accuracy'], label='Validation Accuracy')\n    plt.title('Accuracy')\n    plt.xlabel('Epoch')\n    plt.ylabel('Accuracy')\n    plt.legend()\n    plt.grid(True)\n\n    plt.subplot(1, 2, 2)\n    plt.plot(history.history['loss'], label='Training Loss')\n    plt.plot(history.history['val_loss'], label='Validation Loss')\n    plt.title('Loss')\n    plt.xlabel('Epoch')\n    plt.ylabel('Loss')\n    plt.legend()\n    plt.grid(True)\n\n    plt.tight_layout()\n    plt.show()\n\n# Plotting\nplot_history(model_one_logs)\n","metadata":{"execution":{"iopub.status.busy":"2024-05-15T18:53:39.288136Z","iopub.execute_input":"2024-05-15T18:53:39.288531Z","iopub.status.idle":"2024-05-15T18:53:40.016058Z","shell.execute_reply.started":"2024-05-15T18:53:39.288501Z","shell.execute_reply":"2024-05-15T18:53:40.015101Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def VGG16_two(input_shape=(224, 224, 3), num_classes=2):\n    model = Sequential()\n\n    # Block 1\n    model.add(Conv2D(64, (3, 3), activation='relu', padding='same', input_shape=input_shape))\n    model.add(BatchNormalization())\n    model.add(MaxPooling2D(pool_size=(2, 2)))\n    model.add(Dropout(0.2))\n\n    # Block 2\n    model.add(Conv2D(128, (3, 3), activation='relu', padding='same'))\n    model.add(BatchNormalization())\n    model.add(MaxPooling2D(pool_size=(2, 2)))\n    model.add(Dropout(0.2))\n\n    # Block 3\n    model.add(Conv2D(256, (3, 3), activation='relu', padding='same'))\n    model.add(Conv2D(256, (3, 3), activation='relu', padding='same'))\n    model.add(MaxPooling2D(pool_size=(2, 2)))\n\n    # Block 4\n    model.add(Conv2D(512, (3, 3), activation='relu', padding='same'))\n    model.add(Conv2D(512, (3, 3), activation='relu', padding='same'))\n    model.add(MaxPooling2D(pool_size=(2, 2)))\n\n    # Block 5\n    model.add(Conv2D(512, (3, 3), activation='relu', padding='same'))\n    model.add(Conv2D(512, (3, 3), activation='relu', padding='same'))\n    model.add(BatchNormalization())\n    model.add(MaxPooling2D(pool_size=(2, 2)))\n    model.add(Dropout(0.2))\n\n    # Fully Connected layers\n    model.add(Flatten())\n    model.add(Dense(512, activation='relu'))\n    model.add(BatchNormalization())\n    model.add(Dropout(0.2))\n\n    # Output layer\n    model.add(Dense(num_classes, activation='softmax'))\n\n    return model\n","metadata":{"execution":{"iopub.status.busy":"2024-05-15T18:25:22.749236Z","iopub.execute_input":"2024-05-15T18:25:22.749522Z","iopub.status.idle":"2024-05-15T18:25:22.761891Z","shell.execute_reply.started":"2024-05-15T18:25:22.749497Z","shell.execute_reply":"2024-05-15T18:25:22.760974Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_two = VGG16_two(input_shape=(image_size, image_size, image_channel))\nmodel_two.summary()","metadata":{"execution":{"iopub.status.busy":"2024-05-15T18:25:22.763111Z","iopub.execute_input":"2024-05-15T18:25:22.763443Z","iopub.status.idle":"2024-05-15T18:25:23.120860Z","shell.execute_reply.started":"2024-05-15T18:25:22.763408Z","shell.execute_reply":"2024-05-15T18:25:23.119982Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_two.compile(optimizer='adam',loss='binary_crossentropy',metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2024-05-15T18:25:23.122811Z","iopub.execute_input":"2024-05-15T18:25:23.123098Z","iopub.status.idle":"2024-05-15T18:25:23.130754Z","shell.execute_reply.started":"2024-05-15T18:25:23.123073Z","shell.execute_reply":"2024-05-15T18:25:23.129827Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_two_logs = model_two.fit(train_generator,\n                    validation_data = val_generator, \n                    callbacks=[learning_rate_reduction],\n                    epochs = 5)","metadata":{"execution":{"iopub.status.busy":"2024-05-15T18:25:23.131657Z","iopub.execute_input":"2024-05-15T18:25:23.131893Z","iopub.status.idle":"2024-05-15T18:29:44.397941Z","shell.execute_reply.started":"2024-05-15T18:25:23.131872Z","shell.execute_reply":"2024-05-15T18:29:44.397152Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n\n# Plotting function\ndef plot_history(history):\n    plt.figure(figsize=(12, 6))\n    plt.subplot(1, 2, 1)\n    plt.plot(history.history['accuracy'], label='Training Accuracy')\n    plt.plot(history.history['val_accuracy'], label='Validation Accuracy')\n    plt.title('Accuracy')\n    plt.xlabel('Epoch')\n    plt.ylabel('Accuracy')\n    plt.legend()\n    plt.grid(True)\n\n    plt.subplot(1, 2, 2)\n    plt.plot(history.history['loss'], label='Training Loss')\n    plt.plot(history.history['val_loss'], label='Validation Loss')\n    plt.title('Loss')\n    plt.xlabel('Epoch')\n    plt.ylabel('Loss')\n    plt.legend()\n    plt.grid(True)\n\n    plt.tight_layout()\n    plt.show()\n\n# Plotting\nplot_history(model_two_logs)\n","metadata":{"execution":{"iopub.status.busy":"2024-05-15T18:53:09.407946Z","iopub.execute_input":"2024-05-15T18:53:09.408709Z","iopub.status.idle":"2024-05-15T18:53:10.102631Z","shell.execute_reply.started":"2024-05-15T18:53:09.408674Z","shell.execute_reply":"2024-05-15T18:53:10.101620Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### VGG Model Three\nAdding batch norm and dropout to block two, keeping neurons same","metadata":{}},{"cell_type":"code","source":"def VGG16_three(input_shape=(224, 224, 3)):\n    \"\"\"Instantiates the VGG16 architecture for binary classification.\n\n    # Arguments\n        input_shape: optional shape tuple, only to be specified\n            if `include_top` is False (otherwise the input shape\n            has to be `(224, 224, 3)` (with `channels_last` data format)\n            or `(3, 224, 244)` (with `channels_first` data format).\n            It should have exactly 3 inputs channels,\n            and width and height should be no smaller than 48.\n            E.g. `(200, 200, 3)` would be one valid value.\n\n    # Returns\n        A Keras model instance.\n    \"\"\"\n    img_input = Input(shape=input_shape)\n\n    # Block 1\n    x = Conv2D(64, (3, 3), activation='relu', padding='same', name='block1_conv1')(img_input)\n    x = Conv2D(64, (3, 3), activation='relu', padding='same', name='block1_conv2')(x)\n    x = MaxPooling2D((2, 2), strides=(2, 2), name='block1_pool')(x)\n\n    # Block 2\n    x = Conv2D(128, (3, 3), activation='relu', padding='same', name='block2_conv1')(x)\n    x = Conv2D(128, (3, 3), activation='relu', padding='same', name='block2_conv2')(x)\n    x = BatchNormalization()(x)\n    x = MaxPooling2D((2, 2), strides=(2, 2), name='block2_pool')(x)\n    x = Dropout(0.2)(x)\n\n    # Block 3\n    x = Conv2D(256, (3, 3), activation='relu', padding='same', name='block3_conv1')(x)\n    x = Conv2D(256, (3, 3), activation='relu', padding='same', name='block3_conv2')(x)\n    x = Conv2D(256, (3, 3), activation='relu', padding='same', name='block3_conv3')(x)\n    x = MaxPooling2D((2, 2), strides=(2, 2), name='block3_pool')(x)\n\n    # Block 4\n    x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block4_conv1')(x)\n    x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block4_conv2')(x)\n    x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block4_conv3')(x)\n    x = MaxPooling2D((2, 2), strides=(2, 2), name='block4_pool')(x)\n\n    # Block 5\n    x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block5_conv1')(x)\n    x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block5_conv2')(x)\n    x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block5_conv3')(x)\n    x = MaxPooling2D((2, 2), strides=(2, 2), name='block5_pool')(x)\n\n    # Classification block\n    x = Flatten(name='flatten')(x)\n    x = Dense(4096, activation='relu', name='fc1')(x)\n    x = Dense(4096, activation='relu', name='fc2')(x)\n    output = Dense(2, activation='softmax', name='predictions')(x)\n\n    # Create model\n    model = Model(img_input, output, name='vgg16_three')\n\n    return model","metadata":{"execution":{"iopub.status.busy":"2024-05-15T18:29:44.399398Z","iopub.execute_input":"2024-05-15T18:29:44.399688Z","iopub.status.idle":"2024-05-15T18:29:44.415836Z","shell.execute_reply.started":"2024-05-15T18:29:44.399662Z","shell.execute_reply":"2024-05-15T18:29:44.414878Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_three = VGG16_three(input_shape=(image_size, image_size, image_channel))\nmodel_three.summary()","metadata":{"execution":{"iopub.status.busy":"2024-05-15T18:29:44.416910Z","iopub.execute_input":"2024-05-15T18:29:44.417252Z","iopub.status.idle":"2024-05-15T18:29:44.561994Z","shell.execute_reply.started":"2024-05-15T18:29:44.417221Z","shell.execute_reply":"2024-05-15T18:29:44.561068Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_three.compile(optimizer='adam',loss='binary_crossentropy',metrics=['accuracy'])\nmodel_three_logs = model_three.fit(train_generator,\n                    validation_data = val_generator, \n                    callbacks=[learning_rate_reduction],\n                    epochs = 5)","metadata":{"execution":{"iopub.status.busy":"2024-05-15T18:29:44.563122Z","iopub.execute_input":"2024-05-15T18:29:44.563443Z","iopub.status.idle":"2024-05-15T18:37:29.837310Z","shell.execute_reply.started":"2024-05-15T18:29:44.563412Z","shell.execute_reply":"2024-05-15T18:37:29.836506Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n\n# Plotting function\ndef plot_history(history):\n    plt.figure(figsize=(12, 6))\n    plt.subplot(1, 2, 1)\n    plt.plot(history.history['accuracy'], label='Training Accuracy')\n    plt.plot(history.history['val_accuracy'], label='Validation Accuracy')\n    plt.title('Accuracy')\n    plt.xlabel('Epoch')\n    plt.ylabel('Accuracy')\n    plt.legend()\n    plt.grid(True)\n\n    plt.subplot(1, 2, 2)\n    plt.plot(history.history['loss'], label='Training Loss')\n    plt.plot(history.history['val_loss'], label='Validation Loss')\n    plt.title('Loss')\n    plt.xlabel('Epoch')\n    plt.ylabel('Loss')\n    plt.legend()\n    plt.grid(True)\n\n    plt.tight_layout()\n    plt.show()\n\n# Plotting\nplot_history(model_three_logs)\n","metadata":{"execution":{"iopub.status.busy":"2024-05-15T18:52:44.543026Z","iopub.execute_input":"2024-05-15T18:52:44.543461Z","iopub.status.idle":"2024-05-15T18:52:45.303205Z","shell.execute_reply.started":"2024-05-15T18:52:44.543429Z","shell.execute_reply":"2024-05-15T18:52:45.302188Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# VGG Model Four","metadata":{}},{"cell_type":"code","source":"def VGG16_four(input_shape=(224, 224, 3)):\n    \"\"\"Instantiates the VGG16 architecture for binary classification.\n\n    # Arguments\n        input_shape: optional shape tuple, only to be specified\n            if `include_top` is False (otherwise the input shape\n            has to be `(224, 224, 3)` (with `channels_last` data format)\n            or `(3, 224, 244)` (with `channels_first` data format).\n            It should have exactly 3 inputs channels,\n            and width and height should be no smaller than 48.\n            E.g. `(200, 200, 3)` would be one valid value.\n\n    # Returns\n        A Keras model instance.\n    \"\"\"\n    img_input = Input(shape=input_shape)\n\n    # Block 1\n    x = Conv2D(64, (3, 3), activation='relu', padding='same', name='block1_conv1')(img_input)\n    x = MaxPooling2D((2, 2), strides=(2, 2), name='block1_pool')(x)\n\n    # Block 2\n    x = Conv2D(128, (3, 3), activation='relu', padding='same', name='block2_conv1')(x)\n    x = MaxPooling2D((2, 2), strides=(2, 2), name='block2_pool')(x)\n\n    # Block 3\n    x = Conv2D(256, (3, 3), activation='relu', padding='same', name='block3_conv1')(x)\n    x = MaxPooling2D((2, 2), strides=(2, 2), name='block3_pool')(x)\n\n    # Block 4\n    x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block4_conv1')(x)\n    x = MaxPooling2D((2, 2), strides=(2, 2), name='block4_pool')(x)\n\n    # Block 5\n    x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block5_conv1')(x)\n    x = MaxPooling2D((2, 2), strides=(2, 2), name='block5_pool')(x)\n\n    # Classification block\n    x = Flatten(name='flatten')(x)\n    x = Dense(4096, activation='relu', name='fc2')(x)\n    output = Dense(2, activation='softmax', name='predictions')(x)\n\n    # Create model\n    model = Model(img_input, output, name='vgg16_four')\n\n    return model","metadata":{"execution":{"iopub.status.busy":"2024-05-15T18:37:29.838572Z","iopub.execute_input":"2024-05-15T18:37:29.838859Z","iopub.status.idle":"2024-05-15T18:37:29.850177Z","shell.execute_reply.started":"2024-05-15T18:37:29.838834Z","shell.execute_reply":"2024-05-15T18:37:29.849325Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_four = VGG16_four(input_shape=(image_size, image_size, image_channel))\nmodel_four.summary()","metadata":{"execution":{"iopub.status.busy":"2024-05-15T18:37:29.853434Z","iopub.execute_input":"2024-05-15T18:37:29.853664Z","iopub.status.idle":"2024-05-15T18:37:29.937709Z","shell.execute_reply.started":"2024-05-15T18:37:29.853644Z","shell.execute_reply":"2024-05-15T18:37:29.936890Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_four.compile(optimizer='adam',loss='binary_crossentropy',metrics=['accuracy'])\nmodel_four_logs = model_four.fit(train_generator,\n                    validation_data = val_generator, \n                    callbacks=[learning_rate_reduction],\n                    epochs = 5)","metadata":{"execution":{"iopub.status.busy":"2024-05-15T18:41:57.486266Z","iopub.execute_input":"2024-05-15T18:41:57.487009Z","iopub.status.idle":"2024-05-15T18:44:48.880677Z","shell.execute_reply.started":"2024-05-15T18:41:57.486972Z","shell.execute_reply":"2024-05-15T18:44:48.879817Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n\n# Plotting function\ndef plot_history(history):\n    plt.figure(figsize=(12, 6))\n    plt.subplot(1, 2, 1)\n    plt.plot(history.history['accuracy'], label='Training Accuracy')\n    plt.plot(history.history['val_accuracy'], label='Validation Accuracy')\n    plt.title('Accuracy')\n    plt.xlabel('Epoch')\n    plt.ylabel('Accuracy')\n    plt.legend()\n    plt.grid(True)\n\n    plt.subplot(1, 2, 2)\n    plt.plot(history.history['loss'], label='Training Loss')\n    plt.plot(history.history['val_loss'], label='Validation Loss')\n    plt.title('Loss')\n    plt.xlabel('Epoch')\n    plt.ylabel('Loss')\n    plt.legend()\n    plt.grid(True)\n\n    plt.tight_layout()\n    plt.show()\n\n# Plotting\nplot_history(model_four_logs)\n","metadata":{"execution":{"iopub.status.busy":"2024-05-15T18:51:38.968180Z","iopub.execute_input":"2024-05-15T18:51:38.968563Z","iopub.status.idle":"2024-05-15T18:51:39.803766Z","shell.execute_reply.started":"2024-05-15T18:51:38.968533Z","shell.execute_reply":"2024-05-15T18:51:39.802783Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# VGG Five","metadata":{}},{"cell_type":"code","source":"def VGG16_five(input_shape=(224, 224, 3)):\n    \"\"\"Instantiates the VGG16 architecture for binary classification.\n\n    # Arguments\n        input_shape: optional shape tuple, only to be specified\n            if `include_top` is False (otherwise the input shape\n            has to be `(224, 224, 3)` (with `channels_last` data format)\n            or `(3, 224, 244)` (with `channels_first` data format).\n            It should have exactly 3 inputs channels,\n            and width and height should be no smaller than 48.\n            E.g. `(200, 200, 3)` would be one valid value.\n\n    # Returns\n        A Keras model instance.\n    \"\"\"\n    img_input = Input(shape=input_shape)\n\n    # Block 1\n    x = Conv2D(64, (3, 3), activation='relu', padding='same', name='block1_conv1')(img_input)\n    x = MaxPooling2D((2, 2), strides=(2, 2), name='block1_pool')(x)\n\n    # Block 2\n    x = Conv2D(128, (3, 3), activation='relu', padding='same', name='block2_conv1')(x)\n    x = MaxPooling2D((2, 2), strides=(2, 2), name='block2_pool')(x)\n\n    # Block 3\n    x = Conv2D(256, (3, 3), activation='relu', padding='same', name='block3_conv1')(x)\n    x = MaxPooling2D((2, 2), strides=(2, 2), name='block3_pool')(x)\n\n    # Block 4\n    x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block4_conv1')(x)\n    x = MaxPooling2D((2, 2), strides=(2, 2), name='block4_pool')(x)\n\n    # Block 5\n    x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block5_conv1')(x)\n    x = MaxPooling2D((2, 2), strides=(2, 2), name='block5_pool')(x)\n\n    # Classification block\n    x = Flatten(name='flatten')(x)\n    x = Dense(1024, activation='relu', name='fc2')(x)\n    output = Dense(2, activation='softmax', name='predictions')(x)\n\n    # Create model\n    model = Model(img_input, output, name='vgg16_five')\n\n    return model","metadata":{"execution":{"iopub.status.busy":"2024-05-15T18:37:29.938732Z","iopub.execute_input":"2024-05-15T18:37:29.938982Z","iopub.status.idle":"2024-05-15T18:37:29.949483Z","shell.execute_reply.started":"2024-05-15T18:37:29.938960Z","shell.execute_reply":"2024-05-15T18:37:29.948561Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_five = VGG16_five(input_shape=(image_size, image_size, image_channel))\nmodel_five.summary()","metadata":{"execution":{"iopub.status.busy":"2024-05-15T18:37:29.950703Z","iopub.execute_input":"2024-05-15T18:37:29.951419Z","iopub.status.idle":"2024-05-15T18:37:30.035333Z","shell.execute_reply.started":"2024-05-15T18:37:29.951388Z","shell.execute_reply":"2024-05-15T18:37:30.034513Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_five.compile(optimizer='adam',loss='binary_crossentropy',metrics=['accuracy'])\nmodel_five_logs = model_five.fit(train_generator,\n                    validation_data = val_generator, \n                    callbacks=[learning_rate_reduction],\n                    epochs = 5)","metadata":{"execution":{"iopub.status.busy":"2024-05-15T18:37:30.036344Z","iopub.execute_input":"2024-05-15T18:37:30.036597Z","iopub.status.idle":"2024-05-15T18:40:20.937993Z","shell.execute_reply.started":"2024-05-15T18:37:30.036575Z","shell.execute_reply":"2024-05-15T18:40:20.937068Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n\n# Plotting function\ndef plot_history(history):\n    plt.figure(figsize=(12, 6))\n    plt.subplot(1, 2, 1)\n    plt.plot(history.history['accuracy'], label='Training Accuracy')\n    plt.plot(history.history['val_accuracy'], label='Validation Accuracy')\n    plt.title('Accuracy')\n    plt.xlabel('Epoch')\n    plt.ylabel('Accuracy')\n    plt.legend()\n    plt.grid(True)\n\n    plt.subplot(1, 2, 2)\n    plt.plot(history.history['loss'], label='Training Loss')\n    plt.plot(history.history['val_loss'], label='Validation Loss')\n    plt.title('Loss')\n    plt.xlabel('Epoch')\n    plt.ylabel('Loss')\n    plt.legend()\n    plt.grid(True)\n\n    plt.tight_layout()\n    plt.show()\n\n# Plotting\nplot_history(model_five_logs)\n","metadata":{"execution":{"iopub.status.busy":"2024-05-15T18:54:08.912986Z","iopub.execute_input":"2024-05-15T18:54:08.913821Z","iopub.status.idle":"2024-05-15T18:54:09.968857Z","shell.execute_reply.started":"2024-05-15T18:54:08.913788Z","shell.execute_reply":"2024-05-15T18:54:09.967863Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}