{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"The aim of this notebook is to create a decent vanilla Convolutional Neural Network that is capable of distinguishing between a Dog and Cat.\n\nPlease note that it is advisable to use pre-trained models for image classification tasks, but i'll be creating my own model to solidify my understanding of DNNs and CNNs.\n","metadata":{}},{"cell_type":"markdown","source":"## Importing Libraries","metadata":{}},{"cell_type":"code","source":"import numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\nimport tensorflow as tf\nfrom tensorflow import keras\nfrom keras.preprocessing.image import ImageDataGenerator, load_img, img_to_array\nfrom keras.utils import to_categorical\nfrom sklearn.model_selection import train_test_split\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport random\n\nimport os\nprint(os.listdir(\"../input/dogs-vs-cats\"))","metadata":{"execution":{"iopub.status.busy":"2021-08-22T03:58:03.426168Z","iopub.execute_input":"2021-08-22T03:58:03.426506Z","iopub.status.idle":"2021-08-22T03:58:08.273166Z","shell.execute_reply.started":"2021-08-22T03:58:03.426477Z","shell.execute_reply":"2021-08-22T03:58:08.272251Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Unzipping the files\n\nimport zipfile\n\nwith zipfile.ZipFile(\"../input/dogs-vs-cats/train.zip\",\"r\") as z:\n    z.extractall(\".\")\n    \nwith zipfile.ZipFile(\"../input/dogs-vs-cats/test1.zip\",\"r\") as z:\n    z.extractall(\".\")  ","metadata":{"execution":{"iopub.status.busy":"2021-08-22T03:58:08.274613Z","iopub.execute_input":"2021-08-22T03:58:08.275076Z","iopub.status.idle":"2021-08-22T03:58:25.863301Z","shell.execute_reply.started":"2021-08-22T03:58:08.27504Z","shell.execute_reply":"2021-08-22T03:58:25.862483Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Retrieving a list of directories in each folder\n\nDIR_TRAIN = \"/kaggle/working/train/\"\nDIR_TEST = \"/kaggle/working/test1\"\n\ntrain_imgs = os.listdir(DIR_TRAIN)\ntest_imgs = os.listdir(DIR_TEST)","metadata":{"execution":{"iopub.status.busy":"2021-08-22T03:58:25.865009Z","iopub.execute_input":"2021-08-22T03:58:25.865335Z","iopub.status.idle":"2021-08-22T03:58:25.890498Z","shell.execute_reply.started":"2021-08-22T03:58:25.865306Z","shell.execute_reply":"2021-08-22T03:58:25.889755Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_imgs[:5]","metadata":{"execution":{"iopub.status.busy":"2021-08-22T03:58:25.892156Z","iopub.execute_input":"2021-08-22T03:58:25.892454Z","iopub.status.idle":"2021-08-22T03:58:25.900001Z","shell.execute_reply.started":"2021-08-22T03:58:25.892429Z","shell.execute_reply":"2021-08-22T03:58:25.898816Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Loading a sample image","metadata":{}},{"cell_type":"code","source":"sample = random.choice(train_imgs)\nimage = load_img(\"/kaggle/working/train/\"+sample)\nplt.imshow(image)\nplt.axis(\"off\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-08-22T03:58:25.901584Z","iopub.execute_input":"2021-08-22T03:58:25.902027Z","iopub.status.idle":"2021-08-22T03:58:26.034734Z","shell.execute_reply.started":"2021-08-22T03:58:25.901955Z","shell.execute_reply":"2021-08-22T03:58:26.033874Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Creating a DataFrame for our train set\n\ncategory = [x.split(\".\")[0] for x in train_imgs]\ndf = pd.DataFrame({\"Filename\":train_imgs, \"Category\":category})\ndf.head()","metadata":{"execution":{"iopub.status.busy":"2021-08-22T03:58:26.035765Z","iopub.execute_input":"2021-08-22T03:58:26.036059Z","iopub.status.idle":"2021-08-22T03:58:26.07201Z","shell.execute_reply.started":"2021-08-22T03:58:26.036026Z","shell.execute_reply":"2021-08-22T03:58:26.071282Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Visualizing the constituents of our train set\n\nplt.figure(figsize=(6,6))\nplt.pie(df['Category'].value_counts(), explode=[0.01,0.02], \n       autopct=\"%.2f%%\", textprops={'color':'white', 'size':12,\n                                   'weight':'bold'},\n       startangle=45, colors = ['#947867', '#D49034'])\nplt.legend([\"Dogs\",\"Cats\"])\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-08-22T03:58:26.073184Z","iopub.execute_input":"2021-08-22T03:58:26.07354Z","iopub.status.idle":"2021-08-22T03:58:26.221024Z","shell.execute_reply.started":"2021-08-22T03:58:26.073504Z","shell.execute_reply":"2021-08-22T03:58:26.220081Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Splitting the data into train & validation","metadata":{}},{"cell_type":"code","source":"# Splitting the train set, into a train & validation set with equal categories\n\ndf_train, df_valid = train_test_split(df, test_size = 5000, \n                                     stratify=df['Category'],\n                                     random_state=42)","metadata":{"execution":{"iopub.status.busy":"2021-08-22T03:58:26.223768Z","iopub.execute_input":"2021-08-22T03:58:26.224121Z","iopub.status.idle":"2021-08-22T03:58:26.277254Z","shell.execute_reply.started":"2021-08-22T03:58:26.224084Z","shell.execute_reply":"2021-08-22T03:58:26.27654Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Checking if categories are equal\n\ndf_train['Category'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2021-08-22T03:58:26.279175Z","iopub.execute_input":"2021-08-22T03:58:26.279529Z","iopub.status.idle":"2021-08-22T03:58:26.290939Z","shell.execute_reply.started":"2021-08-22T03:58:26.279494Z","shell.execute_reply":"2021-08-22T03:58:26.289858Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.reset_index(drop=True, inplace=True)\ndf_valid.reset_index(drop=True, inplace=True)","metadata":{"execution":{"iopub.status.busy":"2021-08-22T03:58:26.292488Z","iopub.execute_input":"2021-08-22T03:58:26.29288Z","iopub.status.idle":"2021-08-22T03:58:26.298092Z","shell.execute_reply.started":"2021-08-22T03:58:26.292845Z","shell.execute_reply":"2021-08-22T03:58:26.297228Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 2.0 PROCESSING THE DATA","metadata":{}},{"cell_type":"markdown","source":"### Data Augmentation","metadata":{}},{"cell_type":"code","source":"# Creating an Augmentation generator for the train set\n\ntrain_datagen = ImageDataGenerator(\n    rotation_range = 15,\n    rescale = 1.0/255.0,\n    zoom_range = 0.2,\n    horizontal_flip = True,\n    width_shift_range = 0.1,\n    height_shift_range = 0.1,\n)\n\ntrain_generator = train_datagen.flow_from_dataframe(df_train, \n                                  directory = \"/kaggle/working/train/\",\n                                  x_col = 'Filename',\n                                  y_col = 'Category',\n                                  target_size = (224, 224),\n                                  class_mode = 'categorical',\n                                  batch_size = 32\n                                 )","metadata":{"execution":{"iopub.status.busy":"2021-08-22T03:58:26.299599Z","iopub.execute_input":"2021-08-22T03:58:26.300257Z","iopub.status.idle":"2021-08-22T03:58:26.5092Z","shell.execute_reply.started":"2021-08-22T03:58:26.300218Z","shell.execute_reply":"2021-08-22T03:58:26.508363Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Creating the Augmentation generator for the valid set\n\nvalid_datagen = ImageDataGenerator(rescale = 1.0/255.0)\n\nvalid_generator = valid_datagen.flow_from_dataframe(df_valid, \n                                  directory = \"/kaggle/working/train/\",\n                                  x_col = 'Filename',\n                                  y_col = 'Category',\n                                  target_size = (224, 224),\n                                  class_mode = 'categorical',\n                                  batch_size = 32\n                                 )","metadata":{"execution":{"iopub.status.busy":"2021-08-22T03:58:26.510455Z","iopub.execute_input":"2021-08-22T03:58:26.510783Z","iopub.status.idle":"2021-08-22T03:58:26.569215Z","shell.execute_reply.started":"2021-08-22T03:58:26.510755Z","shell.execute_reply":"2021-08-22T03:58:26.568294Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Let's see how well our Generator works","metadata":{}},{"cell_type":"code","source":"df_example = df_train.sample(1)\n\nexample_generator = train_datagen.flow_from_dataframe(df_example,\n                                  directory = \"/kaggle/working/train/\",\n                                  x_col = 'Filename',\n                                  y_col = 'Category',\n                                  target_size = (224, 224),\n                                  class_mode = 'categorical')","metadata":{"execution":{"iopub.status.busy":"2021-08-22T03:58:26.570471Z","iopub.execute_input":"2021-08-22T03:58:26.570802Z","iopub.status.idle":"2021-08-22T03:58:26.580552Z","shell.execute_reply.started":"2021-08-22T03:58:26.570769Z","shell.execute_reply":"2021-08-22T03:58:26.579543Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(12,12))\n\nfor i in range(9):\n    plt.subplot(3, 3, i+1)\n    for X_batch, Y_batch in example_generator:\n        image = X_batch[0]\n        plt.imshow(image)\n        plt.axis(\"off\")\n        break\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-08-22T03:58:26.58201Z","iopub.execute_input":"2021-08-22T03:58:26.582413Z","iopub.status.idle":"2021-08-22T03:58:27.698864Z","shell.execute_reply.started":"2021-08-22T03:58:26.582369Z","shell.execute_reply":"2021-08-22T03:58:27.698102Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Works as we had hoped !!","metadata":{}},{"cell_type":"markdown","source":"# 3.0 CREATING THE MODEL","metadata":{}},{"cell_type":"markdown","source":"### Creating the sequential vanilla model","metadata":{}},{"cell_type":"code","source":"from functools import partial\n\nkeras.backend.clear_session()\n\n\nDefaultConv = partial(keras.layers.Conv2D, kernel_size = 3, strides = 1,\n                     padding = 'same', activation = 'relu')\n\nmodel = keras.models.Sequential([\n    DefaultConv(filters = 32, kernel_size = 7, strides=2, input_shape=[224,224,3]),\n    keras.layers.BatchNormalization(),\n    keras.layers.MaxPool2D(pool_size=2),\n\n    DefaultConv(filters = 64),\n    DefaultConv(filters = 64),\n    keras.layers.BatchNormalization(),\n    keras.layers.Dropout(0.25),\n    keras.layers.MaxPool2D(pool_size=2),\n    \n    DefaultConv(filters = 128),\n    DefaultConv(filters = 128),\n    keras.layers.BatchNormalization(),\n    keras.layers.Dropout(0.25),\n    keras.layers.MaxPool2D(pool_size=2),\n    \n    DefaultConv(filters = 256),\n    DefaultConv(filters = 256),\n    keras.layers.BatchNormalization(),\n    keras.layers.Dropout(0.25),\n    keras.layers.MaxPool2D(pool_size=2),\n    \n    DefaultConv(filters = 512),\n    DefaultConv(filters = 512),\n    keras.layers.BatchNormalization(),\n    keras.layers.Dropout(0.4),\n    keras.layers.MaxPool2D(pool_size=2),\n    \n    keras.layers.Flatten(),\n    keras.layers.Dense(300, activation = 'relu', use_bias = False),\n    keras.layers.Dropout(0.5),\n    keras.layers.Dense(2, activation = 'sigmoid')\n])\n\nmodel.compile(loss = \"binary_crossentropy\", optimizer = 'nadam',\n             metrics = ['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2021-08-22T03:58:27.700086Z","iopub.execute_input":"2021-08-22T03:58:27.700629Z","iopub.status.idle":"2021-08-22T03:58:29.818659Z","shell.execute_reply.started":"2021-08-22T03:58:27.700577Z","shell.execute_reply":"2021-08-22T03:58:29.817832Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2021-08-22T03:58:29.819853Z","iopub.execute_input":"2021-08-22T03:58:29.82018Z","iopub.status.idle":"2021-08-22T03:58:29.836194Z","shell.execute_reply.started":"2021-08-22T03:58:29.820146Z","shell.execute_reply":"2021-08-22T03:58:29.835258Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### callbacks","metadata":{}},{"cell_type":"code","source":"Checkpoint_cb = keras.callbacks.ModelCheckpoint(\"model.h5\", save_best_only=True)\nEarlystopping_cb = keras.callbacks.EarlyStopping(patience=10)\n\ncallback = [Checkpoint_cb, Earlystopping_cb]","metadata":{"execution":{"iopub.status.busy":"2021-08-22T03:58:29.838315Z","iopub.execute_input":"2021-08-22T03:58:29.838686Z","iopub.status.idle":"2021-08-22T03:58:29.844368Z","shell.execute_reply.started":"2021-08-22T03:58:29.838651Z","shell.execute_reply":"2021-08-22T03:58:29.843242Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Fitting the model","metadata":{}},{"cell_type":"code","source":"history = model.fit(\n    train_generator,\n    epochs = 50,\n    validation_data = valid_generator,\n    validation_steps = len(df_valid)/32,\n    steps_per_epoch = len(df_train)/32,\n    callbacks=callback\n)","metadata":{"execution":{"iopub.status.busy":"2021-08-22T03:58:29.845848Z","iopub.execute_input":"2021-08-22T03:58:29.846474Z","iopub.status.idle":"2021-08-22T05:35:28.092089Z","shell.execute_reply.started":"2021-08-22T03:58:29.84642Z","shell.execute_reply":"2021-08-22T05:35:28.091243Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Visualizing the Training","metadata":{}},{"cell_type":"code","source":"# Train and Validation Loss\n\nplt.figure(figsize=(12,6))\nplt.plot(history.history['loss'][1:], \"ro-\", label = \"Train Loss\")\nplt.plot(history.history['val_loss'][1:], \"b--\", lw=3, label = \"Validation Loss\")\nplt.legend(loc=\"upper right\", fontsize=12)\nplt.xlabel(\"epochs\")\nplt.ylabel(\"Loss\")\nplt.title(\"Train & Validation Loss (50 epochs)\", fontsize=16)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-08-22T05:35:28.095216Z","iopub.execute_input":"2021-08-22T05:35:28.0955Z","iopub.status.idle":"2021-08-22T05:35:28.268495Z","shell.execute_reply.started":"2021-08-22T05:35:28.095472Z","shell.execute_reply":"2021-08-22T05:35:28.267709Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Train and Validation Accuracy\n\nplt.figure(figsize=(12,6))\nplt.plot(history.history['accuracy'], \"ro-\", label = \"Train Accuracy\")\nplt.plot(history.history['val_accuracy'], \"b--\", lw=3, label = \"Validation Accuracy\")\nplt.legend(loc=\"lower right\", fontsize=12)\nplt.xlabel(\"epochs\")\nplt.ylabel(\"Accuracy\")\nplt.title(\"Train & Validation Accuracy (50 epochs)\", fontsize=16)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-08-22T05:35:28.271429Z","iopub.execute_input":"2021-08-22T05:35:28.271705Z","iopub.status.idle":"2021-08-22T05:35:28.43117Z","shell.execute_reply.started":"2021-08-22T05:35:28.271679Z","shell.execute_reply":"2021-08-22T05:35:28.430224Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"We can see from our chart that during training, we experienced some cases of overfitting. In general, the model performed well due to the addition of regularization and sensitivity techniques like `DataAugmentaion`, `BatchNormalization` and `DropOut`.","metadata":{}},{"cell_type":"markdown","source":"## PROCESSING THE TEST DATA","metadata":{}},{"cell_type":"code","source":"df_test = pd.DataFrame({'Filename':test_imgs})\ndf_test.head()","metadata":{"execution":{"iopub.status.busy":"2021-08-22T05:35:28.432587Z","iopub.execute_input":"2021-08-22T05:35:28.43291Z","iopub.status.idle":"2021-08-22T05:35:28.445303Z","shell.execute_reply.started":"2021-08-22T05:35:28.432875Z","shell.execute_reply":"2021-08-22T05:35:28.444533Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Creating the Generator","metadata":{}},{"cell_type":"code","source":"test_datagen = ImageDataGenerator(rescale=1.0/255.0)\n\ntest_generator = test_datagen.flow_from_dataframe(\n    df_test,\n    directory = \"/kaggle/working/test1/\",\n    x_col = 'Filename',\n    y_col = None,\n    class_mode = None,\n    target_size = (224, 224),\n    batch_size = 32,\n    shuffle = False\n)","metadata":{"execution":{"iopub.status.busy":"2021-08-22T05:35:28.44681Z","iopub.execute_input":"2021-08-22T05:35:28.447427Z","iopub.status.idle":"2021-08-22T05:35:28.554295Z","shell.execute_reply.started":"2021-08-22T05:35:28.447388Z","shell.execute_reply":"2021-08-22T05:35:28.553454Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# loading the best model\n\nmodel = keras.models.load_model(\"model.h5\")","metadata":{"execution":{"iopub.status.busy":"2021-08-22T05:35:28.557589Z","iopub.execute_input":"2021-08-22T05:35:28.557839Z","iopub.status.idle":"2021-08-22T05:35:28.910031Z","shell.execute_reply.started":"2021-08-22T05:35:28.557814Z","shell.execute_reply":"2021-08-22T05:35:28.909203Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2021-08-22T05:35:28.911747Z","iopub.execute_input":"2021-08-22T05:35:28.9121Z","iopub.status.idle":"2021-08-22T05:35:28.931476Z","shell.execute_reply.started":"2021-08-22T05:35:28.912063Z","shell.execute_reply":"2021-08-22T05:35:28.930497Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Prediction","metadata":{}},{"cell_type":"code","source":"pred = model.predict(test_generator, \n                     steps = np.ceil(df_test.shape[0]/32))","metadata":{"execution":{"iopub.status.busy":"2021-08-22T05:35:28.932729Z","iopub.execute_input":"2021-08-22T05:35:28.933241Z","iopub.status.idle":"2021-08-22T05:36:06.30294Z","shell.execute_reply.started":"2021-08-22T05:35:28.933197Z","shell.execute_reply":"2021-08-22T05:36:06.302021Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"The model returns the probability an instace belongs to each category.","metadata":{}},{"cell_type":"code","source":"np.set_printoptions(suppress=True)\n\npred[:5]","metadata":{"execution":{"iopub.status.busy":"2021-08-22T05:36:06.306513Z","iopub.execute_input":"2021-08-22T05:36:06.308317Z","iopub.status.idle":"2021-08-22T05:36:06.317417Z","shell.execute_reply.started":"2021-08-22T05:36:06.308286Z","shell.execute_reply":"2021-08-22T05:36:06.31645Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"We will add a category to our dataframe based on our predictions. If the first probabilty on each row is greater than 0.5, we assign it as `cat`, else `dog`.","metadata":{}},{"cell_type":"code","source":"category = []\nfor x in pred[:,0]:\n    category.append(\"cat\" if x > 0.5 else \"dog\")\n    \ndf_test['Category'] = category\n\ndf_test.head()","metadata":{"execution":{"iopub.status.busy":"2021-08-22T05:36:06.321032Z","iopub.execute_input":"2021-08-22T05:36:06.322807Z","iopub.status.idle":"2021-08-22T05:36:06.373818Z","shell.execute_reply.started":"2021-08-22T05:36:06.322777Z","shell.execute_reply":"2021-08-22T05:36:06.373007Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Visualizing our predictions","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(6,6))\nplt.pie(df_test['Category'].value_counts(), explode=[0.01,0.02], \n       autopct=\"%.2f%%\", textprops={'color':'white', 'size':12,\n                                   'weight':'bold'},\n       startangle=45, colors = ['#947867', '#D49034'])\nplt.legend([\"Dogs\",\"Cats\"])\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-08-22T07:37:20.271306Z","iopub.execute_input":"2021-08-22T07:37:20.271836Z","iopub.status.idle":"2021-08-22T07:37:20.3979Z","shell.execute_reply.started":"2021-08-22T07:37:20.271737Z","shell.execute_reply":"2021-08-22T07:37:20.393065Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_example = df_test.sample(50).reset_index(drop=True)\n\nplt.figure(figsize=(18,12))\n\nfor i in range(50):\n    plt.subplot(5,10,i+1)\n    filename = df_example['Filename'][i]\n    category = df_example['Category'][i]\n    image = load_img('/kaggle/working/test1/'+filename)\n    plt.imshow(image)\n    plt.title(f\"Prediction: {category}\")\n    plt.axis(\"off\")\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-08-22T01:46:08.748706Z","iopub.execute_input":"2021-08-22T01:46:08.749055Z","iopub.status.idle":"2021-08-22T01:46:12.124218Z","shell.execute_reply.started":"2021-08-22T01:46:08.749023Z","shell.execute_reply":"2021-08-22T01:46:12.123442Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"For the submission, we are to provide the probabilty that an image is a dog. Therefore, i will be extracting the second column of our `pred` variable for this purpose.","metadata":{}},{"cell_type":"code","source":"df_submission = df_test.copy()\ndf_submission['Category'] = pred[:,1]\ndf_submission['id'] = df_submission['Filename'].str.split('.').str[0]\ndf_submission['label'] = df_submission['Category']\ndf_submission.drop(['Filename', 'Category'], axis = 1, inplace = True)\ndf_submission.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2021-08-22T02:47:07.306087Z","iopub.execute_input":"2021-08-22T02:47:07.306423Z","iopub.status.idle":"2021-08-22T02:47:07.370673Z","shell.execute_reply.started":"2021-08-22T02:47:07.30639Z","shell.execute_reply":"2021-08-22T02:47:07.369897Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_submission","metadata":{"execution":{"iopub.status.busy":"2021-08-22T02:47:16.008503Z","iopub.execute_input":"2021-08-22T02:47:16.008837Z","iopub.status.idle":"2021-08-22T02:47:16.022833Z","shell.execute_reply.started":"2021-08-22T02:47:16.008808Z","shell.execute_reply":"2021-08-22T02:47:16.021897Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# $The$ $End!$","metadata":{}},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}