{"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-09-21T13:42:15.342485Z","iopub.execute_input":"2021-09-21T13:42:15.342785Z","iopub.status.idle":"2021-09-21T13:42:20.474017Z","shell.execute_reply.started":"2021-09-21T13:42:15.342705Z","shell.execute_reply":"2021-09-21T13:42:20.472578Z"},"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-09-21T13:42:33.662074Z","iopub.execute_input":"2021-09-21T13:42:33.662436Z","iopub.status.idle":"2021-09-21T13:42:50.541415Z","shell.execute_reply.started":"2021-09-21T13:42:33.662393Z","shell.execute_reply":"2021-09-21T13:42:50.540514Z"},"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-09-21T13:42:59.574782Z","iopub.execute_input":"2021-09-21T13:42:59.575090Z","iopub.status.idle":"2021-09-21T13:42:59.599915Z","shell.execute_reply.started":"2021-09-21T13:42:59.575062Z","shell.execute_reply":"2021-09-21T13:42:59.599178Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_imgs[:5]","metadata":{"execution":{"iopub.status.busy":"2021-09-21T13:43:37.591067Z","iopub.execute_input":"2021-09-21T13:43:37.591407Z","iopub.status.idle":"2021-09-21T13:43:37.598581Z","shell.execute_reply.started":"2021-09-21T13:43:37.591377Z","shell.execute_reply":"2021-09-21T13:43:37.597725Z"},"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-09-21T13:44:11.804954Z","iopub.execute_input":"2021-09-21T13:44:11.805366Z","iopub.status.idle":"2021-09-21T13:44:12.005739Z","shell.execute_reply.started":"2021-09-21T13:44:11.805330Z","shell.execute_reply":"2021-09-21T13:44:12.004665Z"},"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-09-21T13:45:31.082743Z","iopub.execute_input":"2021-09-21T13:45:31.083080Z","iopub.status.idle":"2021-09-21T13:45:31.119345Z","shell.execute_reply.started":"2021-09-21T13:45:31.083052Z","shell.execute_reply":"2021-09-21T13:45:31.118209Z"},"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-09-21T13:45:47.068342Z","iopub.execute_input":"2021-09-21T13:45:47.068704Z","iopub.status.idle":"2021-09-21T13:45:47.216612Z","shell.execute_reply.started":"2021-09-21T13:45:47.068675Z","shell.execute_reply":"2021-09-21T13:45:47.215627Z"},"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-09-21T13:46:05.578355Z","iopub.execute_input":"2021-09-21T13:46:05.578728Z","iopub.status.idle":"2021-09-21T13:46:05.633803Z","shell.execute_reply.started":"2021-09-21T13:46:05.578699Z","shell.execute_reply":"2021-09-21T13:46:05.633025Z"},"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-09-21T13:46:12.699358Z","iopub.execute_input":"2021-09-21T13:46:12.700014Z","iopub.status.idle":"2021-09-21T13:46:12.722276Z","shell.execute_reply.started":"2021-09-21T13:46:12.699969Z","shell.execute_reply":"2021-09-21T13:46:12.721221Z"},"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-09-21T13:46:15.891158Z","iopub.execute_input":"2021-09-21T13:46:15.891504Z","iopub.status.idle":"2021-09-21T13:46:15.896479Z","shell.execute_reply.started":"2021-09-21T13:46:15.891472Z","shell.execute_reply":"2021-09-21T13:46:15.895311Z"},"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-09-21T13:46:22.652302Z","iopub.execute_input":"2021-09-21T13:46:22.652648Z","iopub.status.idle":"2021-09-21T13:46:22.872972Z","shell.execute_reply.started":"2021-09-21T13:46:22.652618Z","shell.execute_reply":"2021-09-21T13:46:22.872171Z"},"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-09-21T13:46:27.554929Z","iopub.execute_input":"2021-09-21T13:46:27.555285Z","iopub.status.idle":"2021-09-21T13:46:27.615156Z","shell.execute_reply.started":"2021-09-21T13:46:27.555235Z","shell.execute_reply":"2021-09-21T13:46:27.614062Z"},"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-09-21T13:46:35.106606Z","iopub.execute_input":"2021-09-21T13:46:35.106931Z","iopub.status.idle":"2021-09-21T13:46:35.121746Z","shell.execute_reply.started":"2021-09-21T13:46:35.106904Z","shell.execute_reply":"2021-09-21T13:46:35.120581Z"},"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-09-21T13:46:40.498688Z","iopub.execute_input":"2021-09-21T13:46:40.499003Z","iopub.status.idle":"2021-09-21T13:46:41.651362Z","shell.execute_reply.started":"2021-09-21T13:46:40.498975Z","shell.execute_reply":"2021-09-21T13:46:41.650551Z"},"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-09-21T13:46:54.840806Z","iopub.execute_input":"2021-09-21T13:46:54.841154Z","iopub.status.idle":"2021-09-21T13:46:57.086806Z","shell.execute_reply.started":"2021-09-21T13:46:54.841123Z","shell.execute_reply":"2021-09-21T13:46:57.085647Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2021-09-21T13:47:04.265903Z","iopub.execute_input":"2021-09-21T13:47:04.266226Z","iopub.status.idle":"2021-09-21T13:47:04.285228Z","shell.execute_reply.started":"2021-09-21T13:47:04.266196Z","shell.execute_reply":"2021-09-21T13:47:04.284475Z"},"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-09-21T13:47:13.433342Z","iopub.execute_input":"2021-09-21T13:47:13.433707Z","iopub.status.idle":"2021-09-21T13:47:13.439125Z","shell.execute_reply.started":"2021-09-21T13:47:13.433676Z","shell.execute_reply":"2021-09-21T13:47:13.438168Z"},"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-09-21T13:47:16.095577Z","iopub.execute_input":"2021-09-21T13:47:16.095909Z","iopub.status.idle":"2021-09-21T13:51:53.244872Z","shell.execute_reply.started":"2021-09-21T13:47:16.095878Z","shell.execute_reply":"2021-09-21T13:51:53.241355Z"},"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-09-21T13:51:53.246311Z","iopub.status.idle":"2021-09-21T13:51:53.247023Z"},"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":[]}]}