{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"gpu","dataSources":[{"sourceId":5441,"databundleVersionId":38425,"sourceType":"competition"},{"sourceId":9900,"sourceType":"datasetVersion","datasetId":6209}],"dockerImageVersionId":30558,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"<center>\n    <a>\n        <img src=\"https://willowcreek.co.za/wp-content/uploads/2020/03/WC-Pets-Article-Feature-Image.png\"\n             border=\"0\" \n             width=430\n             height=420  \n             class=\"center\">\n    </a>\n","metadata":{}},{"cell_type":"markdown","source":"# <p> <center style=\"background-color:#fffff;font-family:Gabriola;color:black;font-size:250%;text-align:center;border-radius:0px\"> Cats Vs Dogs Image Classification </center></p>\n","metadata":{}},{"cell_type":"markdown","source":"# <b style=\"background-color:#fffff;font-family:'Comic Sans MS';color:black;font-size:100%;text-align:left;border-radius:0px\"> Table of Content </b>\n\n<table border=\"0\" align=\"left\" style=\"text-align:left;font-family:'Comic Sans MS'; border-collapse: collapse;\">\n  <colgroup>\n    <col style=\"width:800px;height:75px;\">\n  </colgroup>\n  <tr>\n    <th style=\"text-align:left;font-size:16px;background-color:white; border: none;\">\n      <a href=\"#1\" style=\"text-decoration:none;\"><b>1. Unzipping the train and test files</b></a>\n    </th>\n  </tr>\n    \n  <tr>\n    <th style=\"text-align:left;font-size:16px;background-color:white; border: none;\">\n      <a href=\"#2\" style=\"text-decoration:none;\"><b>2. Creating dataframes of image paths and labels</b></a>\n    </th>\n  </tr>\n  <tr>\n    <th style=\"text-align:left;font-size:16px;background-color:white; border: none;\">\n      <a href=\"#3\" style=\"text-decoration:none;\"><b>3. Exploring The Data</b></a>\n    </th>\n  </tr>\n  <tr>\n    <th style=\"text-align:left;font-size:16px;background-color:white; border: none;\">\n      <a href=\"#4\" style=\"text-decoration:none;\"><b>4. ImageDataGenerator</b></a>\n    </th>\n  </tr>\n  <tr>\n    <th style=\"text-align:left;font-size:16px;background-color:white; border: none;\">\n      <a href=\"#5\" style=\"text-decoration:none;\"><b>5. ConvNN Sequential Model</b></a>\n    </th>\n  </tr>\n    <tr>\n    <th style=\"text-align:left;font-size:16px;background-color:white; border: none;\">\n      <a href=\"#6\" style=\"text-decoration:none;\"><b>6. Transfer Learning: VGG16</b></a>\n    </th>\n  </tr>\n    \n</table>\n\n","metadata":{}},{"cell_type":"markdown","source":"<a id=\"1\"></a>\n\n# <b style=\"background-color:#fffff;font-family:'Comic Sans MS';color:black;font-size:100%;text-align:left;border-radius:0px\"> Unzipping the train and test files </b>","metadata":{}},{"cell_type":"code","source":"FAST_RUN = False","metadata":{"execution":{"iopub.status.busy":"2024-05-22T03:05:14.218085Z","iopub.execute_input":"2024-05-22T03:05:14.218759Z","iopub.status.idle":"2024-05-22T03:05:14.227913Z","shell.execute_reply.started":"2024-05-22T03:05:14.218731Z","shell.execute_reply":"2024-05-22T03:05:14.227048Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nfrom zipfile import ZipFile\n\n# printing all files-paths\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\npath = '/kaggle/input/dogs-vs-cats-redux-kernels-edition/'\nfiles = ['train.zip', 'test.zip']\n\n# unzipping the train and test files\nfor file in files:\n    with ZipFile(path + file, 'r') as zip_file:\n        zip_file.extractall(path = '/kaggle/working/')","metadata":{"execution":{"iopub.status.busy":"2024-05-22T03:05:26.847754Z","iopub.execute_input":"2024-05-22T03:05:26.848333Z","iopub.status.idle":"2024-05-22T03:05:41.738581Z","shell.execute_reply.started":"2024-05-22T03:05:26.848304Z","shell.execute_reply":"2024-05-22T03:05:41.737737Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id=\"2\"></a>\n\n# <b style=\"background-color:#fffff;font-family:'Comic Sans MS';color:black;font-size:100%;text-align:left;border-radius:0px\"> Creating dataframes of image paths and labels </b>\n\n**The Training dataframe consists of two columns :**\n- the file name for each image\n- the label for each iamge \"0 for cat, 1 for dogs\"","metadata":{}},{"cell_type":"code","source":"import pandas as pd\n\ntrain_path = '/kaggle/working/train/'\n\n# reading all filenames in train directory\nfilenames = os.listdir(train_path)\nprint(\"first filenames in the training images:\", filenames[:4], sep = \"\\n\")\n\n# extracting the label ('dog', 'cat') from filenames\nlabels = [int(x.split('.')[0] == 'dog') for x in filenames]\nprint(\"labels :\", labels[:4], sep = \"\\n\")\n\n# creating the train dataframe with filenames, labels\ntrain_df = pd.DataFrame({'filename' : filenames, 'label' : labels})\nprint('training set shape', train_df.shape, sep = \"\\n\")\n\ntrain_df.head()","metadata":{"execution":{"iopub.status.busy":"2024-05-22T03:05:41.740114Z","iopub.execute_input":"2024-05-22T03:05:41.740395Z","iopub.status.idle":"2024-05-22T03:05:42.081378Z","shell.execute_reply.started":"2024-05-22T03:05:41.740369Z","shell.execute_reply":"2024-05-22T03:05:42.080515Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**The Testing dataframe with only one column `filename` and sorting it by id**","metadata":{}},{"cell_type":"code","source":"test_path = '/kaggle/working/test/'\n\n# reading all filenames in test directory\nfilenames = os.listdir(test_path)\nprint(\"first filenames in the test images:\", filenames[:4], sep = \"\\n\")\n\n# extracting the id from filename\nids = [int(x.split('.')[0]) for x in filenames]\nprint(\"ids :\", ids[:4], sep = \"\\n\")\n\n# creating the dataframe with ids, filenames\ntest = pd.DataFrame({'id' : ids, 'filename' : filenames})\nprint('test-set shape:', test.shape, sep = \"\\n\")\n\n# sorting the test dataframe by the id and reseting index\ntest.sort_values(by = ['id'], axis = 0, inplace = True)\ntest.reset_index(inplace = True, drop = True)\n\n# droping the id column as it's no longer useful\ntest.drop(columns = ['id'], inplace = True)\n\ntest.head()","metadata":{"execution":{"iopub.status.busy":"2024-05-22T03:05:42.082562Z","iopub.execute_input":"2024-05-22T03:05:42.083031Z","iopub.status.idle":"2024-05-22T03:05:42.126023Z","shell.execute_reply.started":"2024-05-22T03:05:42.083003Z","shell.execute_reply":"2024-05-22T03:05:42.125185Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id=\"3\"></a>\n\n# <b style=\"background-color:#fffff;font-family:'Comic Sans MS';color:black;font-size:100%;text-align:left;border-radius:0px\"> Explore the Data</b>","metadata":{}},{"cell_type":"markdown","source":"**Spliting the data into train and validation**","metadata":{}},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\n\ntrain, validation = train_test_split(train_df, test_size = 0.2,\n                                         stratify = train_df['label'], random_state = 42)\n\nprint(\"Train-set shape:\", train.shape)\nprint(\"Validation-set shape:\", validation.shape)","metadata":{"execution":{"iopub.status.busy":"2024-05-22T03:05:42.128254Z","iopub.execute_input":"2024-05-22T03:05:42.128866Z","iopub.status.idle":"2024-05-22T03:05:42.663641Z","shell.execute_reply.started":"2024-05-22T03:05:42.128831Z","shell.execute_reply":"2024-05-22T03:05:42.662700Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Importing visualization librarie and Setting up rcParams**","metadata":{}},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport seaborn as sns\n%matplotlib inline\n\nplt.style.use('seaborn-v0_8-muted')\n\nplt.rcParams['font.size'] = 10\nplt.rcParams['legend.fontsize'] = 10\nplt.rcParams['xtick.labelsize'] = 8\nplt.rcParams['ytick.labelsize'] = 8","metadata":{"execution":{"iopub.status.busy":"2024-05-22T03:05:42.664814Z","iopub.execute_input":"2024-05-22T03:05:42.665165Z","iopub.status.idle":"2024-05-22T03:05:42.934101Z","shell.execute_reply.started":"2024-05-22T03:05:42.665126Z","shell.execute_reply":"2024-05-22T03:05:42.933124Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Cats and Dogs Classes are balanced**","metadata":{}},{"cell_type":"code","source":"fig, ax = plt.subplots(1, 2, figsize=(8, 3), sharey = True)\n\nsns.countplot(x = 'label', data = train, ax=ax[0])\nax[0].set_title('Training dataset')\nax[0].set_xticks(ticks=[0, 1], labels=['Cat', 'Dog'])\n\nsns.countplot(x = 'label', data = validation, ax=ax[1])\nax[1].set_title('Validation dataset')\nax[1].set_xticks(ticks=[0, 1], labels=['Cat', 'Dog'])\n\nplt.suptitle('Cats and Dogs Classes are balanced', y = -0.04, fontsize=18)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-05-22T03:05:42.935324Z","iopub.execute_input":"2024-05-22T03:05:42.935694Z","iopub.status.idle":"2024-05-22T03:05:43.294748Z","shell.execute_reply.started":"2024-05-22T03:05:42.935659Z","shell.execute_reply":"2024-05-22T03:05:43.293814Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**looking at a sample**","metadata":{}},{"cell_type":"code","source":"from tensorflow.keras.preprocessing.image import load_img\n\nload_img((train_path + train.loc[0, 'filename']))","metadata":{"execution":{"iopub.status.busy":"2024-05-22T03:05:57.198213Z","iopub.execute_input":"2024-05-22T03:05:57.199353Z","iopub.status.idle":"2024-05-22T03:06:04.855837Z","shell.execute_reply.started":"2024-05-22T03:05:57.199305Z","shell.execute_reply":"2024-05-22T03:06:04.854867Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id=\"4\"></a>\n\n# <b style=\"background-color:#fffff;font-family:'Comic Sans MS';color:black;font-size:100%;text-align:left;border-radius:0px\"> ImageDataGenerator </b>","metadata":{}},{"cell_type":"markdown","source":"**defining ImageDataGenerator object to load images**","metadata":{}},{"cell_type":"code","source":"IMAGE_WIDTH = 360\nIMAGE_HEIGHT = 360\nIMAGE_SIZE = (IMAGE_WIDTH, IMAGE_HEIGHT)\nIMAGE_CHANNELS = 3\n\nBATCH_SIZE = 32","metadata":{"execution":{"iopub.status.busy":"2024-05-22T03:06:07.886595Z","iopub.execute_input":"2024-05-22T03:06:07.887710Z","iopub.status.idle":"2024-05-22T03:06:07.891943Z","shell.execute_reply.started":"2024-05-22T03:06:07.887675Z","shell.execute_reply":"2024-05-22T03:06:07.890906Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.preprocessing.image import ImageDataGenerator\n\ntrain_datagen = ImageDataGenerator( rescale = 1.0/255, \n                                    rotation_range = 15.0,\n                                    width_shift_range = 0.1,\n                                    height_shift_range = 0.1,\n                                    shear_range = 0.1,\n                                    zoom_range = 0.1,\n                                    horizontal_flip= True,\n                                    fill_mode = 'nearest',)\n\ntrain_datagen = train_datagen.flow_from_dataframe(\n                                              # dataframe to get paths from\n                                                dataframe = train,\n                                              # directory path images are in\n                                                directory = train_path,\n                                              # column name file paths are in\n                                                x_col = 'filename',\n                                              # column name output targets are in\n                                                y_col = 'label',\n                                              # size to rescale the output images to\n                                                target_size = IMAGE_SIZE,\n                                              # size of the batch\n                                                batch_size = BATCH_SIZE,\n                                              # mode \"row\" output targets is array of y_col as it is\n                                                class_mode = 'raw',\n                                            )","metadata":{"execution":{"iopub.status.busy":"2024-05-22T03:06:13.754373Z","iopub.execute_input":"2024-05-22T03:06:13.755256Z","iopub.status.idle":"2024-05-22T03:06:13.964190Z","shell.execute_reply.started":"2024-05-22T03:06:13.755223Z","shell.execute_reply":"2024-05-22T03:06:13.963413Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Visualizing a sample from training images**","metadata":{}},{"cell_type":"code","source":"fig, ax = plt.subplots(4, 6, figsize = (12, 8))\n\ni, j = 0, 0\nfor images_batch, labels_batch in train_datagen:\n    ax[i][j].imshow(images_batch[0])\n    ax[i][j].set_title(['Cat', 'Dog'][labels_batch[0]])\n    ax[i][j].axis('off')\n    i += 1\n    if i == 4: i, j = 0, j+1\n    if j == 6: break\n        \nfig.tight_layout(pad=0.001)\nplt.suptitle('Training Dataset', fontsize=18, y = 1.05)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-05-22T03:06:18.096385Z","iopub.execute_input":"2024-05-22T03:06:18.096776Z","iopub.status.idle":"2024-05-22T03:06:40.436656Z","shell.execute_reply.started":"2024-05-22T03:06:18.096743Z","shell.execute_reply":"2024-05-22T03:06:40.434680Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"validation_datagen = ImageDataGenerator(rescale = 1.0/255,)\n\nvalidation_datagen = validation_datagen.flow_from_dataframe(\n    dataframe = validation,\n    directory = train_path,\n    x_col = 'filename',\n    y_col = 'label',\n    target_size = IMAGE_SIZE,\n    batch_size = BATCH_SIZE,\n    class_mode = 'raw',\n    \n)","metadata":{"execution":{"iopub.status.busy":"2024-05-22T03:06:40.438287Z","iopub.execute_input":"2024-05-22T03:06:40.438606Z","iopub.status.idle":"2024-05-22T03:06:40.498308Z","shell.execute_reply.started":"2024-05-22T03:06:40.438576Z","shell.execute_reply":"2024-05-22T03:06:40.497469Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_datagen = ImageDataGenerator(rescale=1./255)\ntest_datagen = test_datagen.flow_from_dataframe(\n                                test, \n                                test_path, \n                                x_col='filename',\n                                y_col=None,\n                                class_mode=None,\n                                target_size= IMAGE_SIZE,\n                                batch_size= BATCH_SIZE,\n                                shuffle=False\n                            )","metadata":{"execution":{"iopub.status.busy":"2024-05-22T03:06:40.499617Z","iopub.execute_input":"2024-05-22T03:06:40.499977Z","iopub.status.idle":"2024-05-22T03:06:40.631189Z","shell.execute_reply.started":"2024-05-22T03:06:40.499941Z","shell.execute_reply":"2024-05-22T03:06:40.630447Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Visualizing a sample from testing images**","metadata":{}},{"cell_type":"code","source":"fig, ax = plt.subplots(4, 6, figsize = (13, 9))\nplt.subplots_adjust(hspace=0.05, wspace=0.05)\n\ni, j = 0, 0\nfor images_batch in test_datagen:\n    ax[i][j].imshow(images_batch[0])\n    ax[i][j].axis('off')\n    i += 1\n    if i == 4: i, j = 0, j+1\n    if j == 6: break\n\nfig.tight_layout(pad=0.001)\nplt.suptitle('Testing Dataset', fontsize=18, y = 1.05)\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-05-22T03:06:40.633117Z","iopub.execute_input":"2024-05-22T03:06:40.633392Z","iopub.status.idle":"2024-05-22T03:06:44.758677Z","shell.execute_reply.started":"2024-05-22T03:06:40.633366Z","shell.execute_reply":"2024-05-22T03:06:44.757563Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id=\"5\"></a>\n\n# <b style=\"background-color:#fffff;font-family:'Comic Sans MS';color:black;font-size:100%;text-align:left;border-radius:0px\"> ConvNN Sequential Model </b>","metadata":{}},{"cell_type":"markdown","source":"**building model structure**","metadata":{}},{"cell_type":"code","source":"!pip install visualkeras","metadata":{"_kg_hide-input":true,"_kg_hide-output":true,"execution":{"iopub.status.busy":"2024-05-22T03:06:44.760083Z","iopub.execute_input":"2024-05-22T03:06:44.760382Z","iopub.status.idle":"2024-05-22T03:06:57.642730Z","shell.execute_reply.started":"2024-05-22T03:06:44.760353Z","shell.execute_reply":"2024-05-22T03:06:57.641656Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\nfrom keras.models import Sequential\nfrom keras.layers import Dense, Conv2D, MaxPooling2D, Dropout, Flatten, Activation, BatchNormalization\nimport visualkeras\n\ntf.keras.backend.clear_session()\ntf.random.set_seed(42)\n\n\ndef build_model():\n    model = Sequential()\n    \n    model.add(Conv2D(32, (3, 3), activation='relu',\n                     input_shape=(IMAGE_WIDTH, IMAGE_HEIGHT, IMAGE_CHANNELS, )))\n    model.add(Conv2D(32, (3, 3), activation='relu'))\n    model.add(BatchNormalization())\n    model.add(MaxPooling2D(pool_size=(2, 2)))\n    model.add(Dropout(0.5))\n\n    model.add(Conv2D(64, (3, 3), activation='relu'))\n    model.add(Conv2D(64, (3, 3), activation='relu'))\n    model.add(BatchNormalization())\n    model.add(MaxPooling2D(pool_size=(2, 2)))\n    model.add(Dropout(0.5))\n\n    model.add(Conv2D(128, (3, 3), activation='relu'))\n    model.add(Conv2D(128, (3, 3), activation='relu'))\n    model.add(BatchNormalization())\n    model.add(MaxPooling2D(pool_size=(2, 2)))\n    model.add(Dropout(0.5))\n\n    model.add(Flatten())\n    \n    model.add(Dense(512, activation='relu'))\n    model.add(BatchNormalization())\n    model.add(Dropout(0.5))\n    model.add(Dense(1, activation='sigmoid'))\n    \n    return model\n\nmodel = build_model()\nmodel.summary()\nvisualkeras.layered_view(model, legend = True, scale_xy=1, scale_z=0.001, spacing=15)","metadata":{"_kg_hide-input":false,"_kg_hide-output":false,"execution":{"iopub.status.busy":"2024-05-22T03:06:57.644594Z","iopub.execute_input":"2024-05-22T03:06:57.645012Z","iopub.status.idle":"2024-05-22T03:06:58.945337Z","shell.execute_reply.started":"2024-05-22T03:06:57.644961Z","shell.execute_reply":"2024-05-22T03:06:58.944433Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Compiling  model**","metadata":{}},{"cell_type":"code","source":"adam_opt = tf.keras.optimizers.Adam(learning_rate=0.001)\nmodel.compile(optimizer = adam_opt, loss = 'binary_crossentropy', metrics = ['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2024-05-22T03:06:58.946832Z","iopub.execute_input":"2024-05-22T03:06:58.947201Z","iopub.status.idle":"2024-05-22T03:06:58.965619Z","shell.execute_reply.started":"2024-05-22T03:06:58.947166Z","shell.execute_reply":"2024-05-22T03:06:58.964866Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Call-backs**","metadata":{}},{"cell_type":"code","source":"from keras.callbacks import EarlyStopping, ReduceLROnPlateau, ModelCheckpoint\n\n# Stop training when 'validation loss' has stopped improving.\nearly_stopping = EarlyStopping(monitor = 'val_loss', min_delta = 1e-4, \n                                                       patience = 5,  verbose = 1)\n\n# Reduce learning rate when 'validation loss' has stopped improving.\nreduce_learning_rate = ReduceLROnPlateau(monitor = 'val_loss', factor = 0.5,\n                                                         patience = 2, verbose = 1, min_lr = 1e-5)\n\n\n#saving best model weights on the validation-set\ncheckpoint_filepath = '/kaggle/working/checkpoint.weights.h5'\nmodel_checkpoint = ModelCheckpoint(\n                            filepath=checkpoint_filepath,\n                            save_weights_only=True,\n                            monitor='val_loss',\n                            mode='max',\n                            save_best_only=True\n                        )\n\n\n\ncall_backs = [reduce_learning_rate, early_stopping, model_checkpoint]","metadata":{"execution":{"iopub.status.busy":"2024-05-22T03:06:58.966839Z","iopub.execute_input":"2024-05-22T03:06:58.967185Z","iopub.status.idle":"2024-05-22T03:06:58.974033Z","shell.execute_reply.started":"2024-05-22T03:06:58.967151Z","shell.execute_reply":"2024-05-22T03:06:58.972923Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"epochs = 3 if FAST_RUN else 20\nhistory = model.fit(train_datagen,\n                      steps_per_epoch= train.shape[0]//BATCH_SIZE,\n                      epochs = epochs,\n                      validation_data= validation_datagen, \n                      validation_steps= validation.shape[0]// BATCH_SIZE,\n                      callbacks = call_backs,\n                   )","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2024-05-21T16:49:14.820806Z","iopub.execute_input":"2024-05-21T16:49:14.821122Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_history(history):\n    acc = history.history['accuracy']\n    val_acc = history.history['val_accuracy']\n\n    loss = history.history['loss']\n    val_loss = history.history['val_loss']\n    \n    fig, ax = plt.subplots(2, 1, figsize = (8, 10))\n\n    epochs = range(1, len(acc) + 1)\n    ax[0].plot(epochs, acc, '--', label='Training acc')\n    ax[0].plot(epochs, val_acc, label='Validation acc')\n    ax[0].set_title('Training and validation accuracy')\n    ax[0].set_ylim([0.0, 1.0])\n    ax[0].legend()\n\n    ax[1].plot(epochs, loss, '--', label='Training loss')\n    ax[1].plot(epochs, val_loss, label='Validation loss')\n    ax[1].set_title('Training and validation loss')\n    ax[1].legend()\n    \n    plt.show()\n","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2024-05-22T03:07:34.287023Z","iopub.execute_input":"2024-05-22T03:07:34.287982Z","iopub.status.idle":"2024-05-22T03:07:34.296359Z","shell.execute_reply.started":"2024-05-22T03:07:34.287941Z","shell.execute_reply":"2024-05-22T03:07:34.295233Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Model Learning Curve**","metadata":{}},{"cell_type":"code","source":"plot_history(history)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Making Predictions**","metadata":{}},{"cell_type":"code","source":"import numpy as np\n\nmodel.load_weights(checkpoint_filepath)\npredict = model.predict(test_datagen, steps= np.ceil(test.shape[0]/BATCH_SIZE))\npredict","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub = test.copy()\n\nsub['label'] = predict\nsub['id'] = sub['filename'].str.split('.').apply(lambda x: x[0])\nsub = sub[['id', 'label']]\nsub.to_csv('/kaggle/working/sub0.csv', index = False)\n\nsub.head()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id=\"6\"></a>\n\n# <b style=\"background-color:#fffff;font-family:'Comic Sans MS';color:black;font-size:100%;text-align:left;border-radius:0px\"> Transfer Learning: VGG16 </b>","metadata":{}},{"cell_type":"code","source":"from keras.applications.vgg16 import VGG16\n\nvgg16=VGG16(\n    include_top=False,\n    pooling='max',\n    input_shape =(IMAGE_WIDTH, IMAGE_HEIGHT, IMAGE_CHANNELS), \n    weights='imagenet',\n)\nvgg16.trainable = False\n\nmodel = Sequential()\nmodel.add(vgg16)\nmodel.add(Dropout(0.5))\nmodel.add(Flatten())\nmodel.add(Dense(512, activation='relu'))\nmodel.add(BatchNormalization())\nmodel.add(Dropout(0.5))\nmodel.add(Dense(1, activation='sigmoid'))\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2024-05-22T03:08:39.004261Z","iopub.execute_input":"2024-05-22T03:08:39.005198Z","iopub.status.idle":"2024-05-22T03:08:39.476869Z","shell.execute_reply.started":"2024-05-22T03:08:39.005161Z","shell.execute_reply":"2024-05-22T03:08:39.475800Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Compile model**","metadata":{}},{"cell_type":"code","source":"adam_opt = tf.keras.optimizers.Adam(learning_rate=0.001)\nmodel.compile(optimizer = adam_opt, loss = 'binary_crossentropy', metrics = ['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2024-05-22T03:07:56.638676Z","iopub.execute_input":"2024-05-22T03:07:56.639368Z","iopub.status.idle":"2024-05-22T03:07:56.651589Z","shell.execute_reply.started":"2024-05-22T03:07:56.639333Z","shell.execute_reply":"2024-05-22T03:07:56.650647Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Stop training when 'validation loss' has stopped improving.\nearly_stopping = EarlyStopping(monitor = 'val_loss', min_delta = 1e-4, \n                                                       patience = 5,  verbose = 1)\n\n# Reduce learning rate when 'validation loss' has stopped improving.\nreduce_learning_rate = ReduceLROnPlateau(monitor = 'val_loss', factor = 0.5,\n                                                         patience = 2, verbose = 1, min_lr = 1e-5)\n\n#saving best model weights on the validation-set\ncheckpoint_filepath = '/kaggle/working/checkpoint.weights.h5'\nmodel_checkpoint = ModelCheckpoint(\n                            filepath=checkpoint_filepath,\n                            save_weights_only=True,\n                            monitor='val_loss',\n                            mode='max',\n                            save_best_only=True\n                        )\n\n\n\ncall_backs = [reduce_learning_rate, early_stopping, model_checkpoint]","metadata":{"execution":{"iopub.status.busy":"2024-05-22T03:09:49.858913Z","iopub.execute_input":"2024-05-22T03:09:49.859297Z","iopub.status.idle":"2024-05-22T03:09:49.865939Z","shell.execute_reply.started":"2024-05-22T03:09:49.859266Z","shell.execute_reply":"2024-05-22T03:09:49.864874Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Training Model**","metadata":{}},{"cell_type":"code","source":"epochs = 3 if FAST_RUN else 20\nhistory = model.fit(train_datagen,\n                      steps_per_epoch= train.shape[0]//BATCH_SIZE,\n                      epochs = epochs,\n                      validation_data= validation_datagen, \n                      validation_steps= validation.shape[0]// BATCH_SIZE,\n                      callbacks = call_backs\n                   )","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_history(history)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\n\nmodel.load_weights(checkpoint_filepath)\npredict = model.predict(test_datagen, steps= np.ceil(test.shape[0]/BATCH_SIZE))\n\nsub = test.copy()\n\nsub['label'] = predict\nsub['id'] = sub['filename'].str.split('.').apply(lambda x: x[0])\nsub = sub[['id', 'label']]\nsub.to_csv('/kaggle/working/sub1.csv', index = False)\n\nsub.head()","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}