{"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":"<div style=\"padding:20px;color:white;margin:0;font-size:200%;text-align:center;display:fill;border-radius:5px;background-color:#C85C8E;overflow:hidden;font-weight:500\">🐶🐶 Dogs 🐕🐕 vs.🐱🐱 Cats 🐈🐈 Classification Using VGG16</div>\n\n\n\n","metadata":{"papermill":{"duration":0.008658,"end_time":"2023-03-08T09:28:18.893766","exception":false,"start_time":"2023-03-08T09:28:18.885108","status":"completed"},"tags":[]}},{"cell_type":"markdown","source":"# **Table of Contents**\n\n1.\t[Introduction](#1)\n2.\t[Data Preprocessing](#2)\n3.\t[Model Training](#3)\n4.\t[Evaluation](#4)\n5.\t[Preparing Test Data](#5)\n6.\t[Submission](#6)\n","metadata":{"papermill":{"duration":0.008639,"end_time":"2023-03-08T09:28:18.910179","exception":false,"start_time":"2023-03-08T09:28:18.901540","status":"completed"},"tags":[]}},{"cell_type":"markdown","source":"<div id='1'\n     style = \"display: fill;\n              border-radius: 10px;\n              background-color: #B7CADB;\">\n    <h1 style = \"padding: 15px; \n                 color: white;\n                 text-align: left;\n                 font-family: Trebuchet MS;\">Introduction\n    </h1>\n</div>","metadata":{"papermill":{"duration":0.007417,"end_time":"2023-03-08T09:28:18.925684","exception":false,"start_time":"2023-03-08T09:28:18.918267","status":"completed"},"tags":[]}},{"cell_type":"markdown","source":"<div id='1'\n     style = \"display: fill;\n              border-radius: 10px;\n              background-color: #AD8B73;\">\n    <h3 style = \"padding: 15px; \n                 color: white;\n                 text-align: left;\n                 font-family: Trebuchet MS;\">Importing Libraries\n    </h3>\n</div>","metadata":{"papermill":{"duration":0.00734,"end_time":"2023-03-08T09:28:18.940938","exception":false,"start_time":"2023-03-08T09:28:18.933598","status":"completed"},"tags":[]}},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport zipfile\nimport numpy as np\nfrom PIL import Image\nfrom keras.preprocessing.image import ImageDataGenerator\nfrom keras.utils import to_categorical\nfrom keras.models import Sequential\nfrom keras import layers\nfrom keras.layers import Conv2D, MaxPooling2D , Dropout, Flatten, Dense, Activation, GlobalMaxPooling2D\nfrom keras.applications import VGG16\nfrom keras import optimizers\nfrom keras.models import Model\nimport seaborn as sns\n\nfrom sklearn.model_selection import train_test_split\nimport matplotlib.pyplot as plt\nimport random\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\nimport warnings \nwarnings.filterwarnings('ignore')","metadata":{"_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","papermill":{"duration":1.297321,"end_time":"2023-03-08T09:28:20.245959","exception":false,"start_time":"2023-03-08T09:28:18.948638","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-03-21T03:03:10.517300Z","iopub.execute_input":"2023-03-21T03:03:10.517803Z","iopub.status.idle":"2023-03-21T03:03:10.547872Z","shell.execute_reply.started":"2023-03-21T03:03:10.517759Z","shell.execute_reply":"2023-03-21T03:03:10.543934Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<div id='1'\n     style = \"display: fill;\n              border-radius: 10px;\n              background-color: #AD8B73;\">\n    <h3 style = \"padding: 15px; \n                 color: white;\n                 text-align: left;\n                 font-family: Trebuchet MS;\">UnZipping The Train.zip and Test1.zio file\n    </h3>\n</div>","metadata":{"papermill":{"duration":0.007824,"end_time":"2023-03-08T09:28:20.432292","exception":false,"start_time":"2023-03-08T09:28:20.424468","status":"completed"},"tags":[]}},{"cell_type":"code","source":"path_zip = '/kaggle/input/dogs-vs-cats/'\npath = '/kaggle/working/'\n\nwith zipfile.ZipFile(path_zip+'train.zip','r') as zip_ref:\n    zip_ref.extractall(path+'train')\nwith zipfile.ZipFile(path_zip+'test1.zip','r') as zip_ref:\n    zip_ref.extractall(path+'test')\n","metadata":{"papermill":{"duration":0.045144,"end_time":"2023-03-08T09:28:20.299355","exception":false,"start_time":"2023-03-08T09:28:20.254211","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-03-21T03:03:10.552792Z","iopub.execute_input":"2023-03-21T03:03:10.553164Z","iopub.status.idle":"2023-03-21T03:03:22.639567Z","shell.execute_reply.started":"2023-03-21T03:03:10.553127Z","shell.execute_reply":"2023-03-21T03:03:22.638476Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<div id='1'\n     style = \"display: fill;\n              border-radius: 10px;\n              background-color: #AD8B73;\">\n    <h3 style = \"padding: 15px; \n                 color: white;\n                 text-align: left;\n                 font-family: Trebuchet MS;\">Creating a dataframe with two columns one is filename and other is the category i.e category of the file\n    </h3>\n</div>","metadata":{"papermill":{"duration":0.007824,"end_time":"2023-03-08T09:28:20.432292","exception":false,"start_time":"2023-03-08T09:28:20.424468","status":"completed"},"tags":[]}},{"cell_type":"code","source":"filenames = os.listdir(\"/kaggle/working/train/train\")\ncategories=[]\n\nfor filename in filenames:\n    category = filename.split('.')[0]\n    \n    if category == 'dog':\n        categories.append(1)\n    else:\n        categories.append(0)\n\ndf = pd.DataFrame({\n    'filename' : filenames,\n    'category' : categories\n})\n\ndf.head()","metadata":{"execution":{"iopub.status.busy":"2023-03-21T03:03:22.643194Z","iopub.execute_input":"2023-03-21T03:03:22.643493Z","iopub.status.idle":"2023-03-21T03:03:22.684008Z","shell.execute_reply.started":"2023-03-21T03:03:22.643463Z","shell.execute_reply":"2023-03-21T03:03:22.682968Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.dtypes","metadata":{"execution":{"iopub.status.busy":"2023-03-21T03:03:22.685393Z","iopub.execute_input":"2023-03-21T03:03:22.685824Z","iopub.status.idle":"2023-03-21T03:03:22.695421Z","shell.execute_reply.started":"2023-03-21T03:03:22.685787Z","shell.execute_reply":"2023-03-21T03:03:22.694168Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<div id='2'\n     style = \"display: fill;\n              border-radius: 10px;\n              background-color: #B7CADB;\">\n    <h1 style = \"padding: 15px; \n                 color: white;\n                 text-align: left;\n                 font-family: Trebuchet MS;\">Data Preprocessing\n    </h1>\n</div>","metadata":{"papermill":{"duration":0.007417,"end_time":"2023-03-08T09:28:18.925684","exception":false,"start_time":"2023-03-08T09:28:18.918267","status":"completed"},"tags":[]}},{"cell_type":"code","source":"# See Total In Count\ndf['category'].value_counts().plot.bar()","metadata":{"execution":{"iopub.status.busy":"2023-03-21T03:03:22.698688Z","iopub.execute_input":"2023-03-21T03:03:22.699118Z","iopub.status.idle":"2023-03-21T03:03:22.883999Z","shell.execute_reply.started":"2023-03-21T03:03:22.699062Z","shell.execute_reply":"2023-03-21T03:03:22.882975Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<div id='1'\n     style = \"display: fill;\n              border-radius: 10px;\n              background-color: #AD8B73;\">\n    <h3 style = \"padding: 15px; \n                 color: white;\n                 text-align: left;\n                 font-family: Trebuchet MS;\">Sample Image\n    </h3>\n</div>","metadata":{"papermill":{"duration":0.007824,"end_time":"2023-03-08T09:28:20.432292","exception":false,"start_time":"2023-03-08T09:28:20.424468","status":"completed"},"tags":[]}},{"cell_type":"code","source":"sample =random.choice(filenames)\nimg = np.asarray(Image.open(\"/kaggle/working/train/train/\"+sample))\n# image = load_img()\nplt.imshow(img)","metadata":{"execution":{"iopub.status.busy":"2023-03-21T03:03:22.886182Z","iopub.execute_input":"2023-03-21T03:03:22.886798Z","iopub.status.idle":"2023-03-21T03:03:23.167073Z","shell.execute_reply.started":"2023-03-21T03:03:22.886761Z","shell.execute_reply":"2023-03-21T03:03:23.166087Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<div id='1'\n     style = \"display: fill;\n              border-radius: 10px;\n              background-color: #AD8B73;\">\n    <h3 style = \"padding: 15px; \n                 color: white;\n                 text-align: left;\n                 font-family: Trebuchet MS;\"> Prepare Test and Train Data\n    </h3>\n</div>","metadata":{"papermill":{"duration":0.008357,"end_time":"2023-03-08T09:28:20.622243","exception":false,"start_time":"2023-03-08T09:28:20.613886","status":"completed"},"tags":[]}},{"cell_type":"code","source":"# df[\"category\"] = df[\"category\"].replace({0: 'cat', 1: 'dog'}) ","metadata":{"execution":{"iopub.status.busy":"2023-03-21T03:03:23.168643Z","iopub.execute_input":"2023-03-21T03:03:23.169327Z","iopub.status.idle":"2023-03-21T03:03:23.173667Z","shell.execute_reply.started":"2023-03-21T03:03:23.169289Z","shell.execute_reply":"2023-03-21T03:03:23.172362Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df, validate_df = train_test_split(df, test_size=0.2)\ntrain_df = train_df.reset_index()\nvalidate_df = validate_df.reset_index()\n\n\ntotal_train = train_df.shape[0]\ntotal_validate = validate_df.shape[0]","metadata":{"execution":{"iopub.status.busy":"2023-03-21T03:03:23.175437Z","iopub.execute_input":"2023-03-21T03:03:23.176150Z","iopub.status.idle":"2023-03-21T03:03:23.191013Z","shell.execute_reply.started":"2023-03-21T03:03:23.176086Z","shell.execute_reply":"2023-03-21T03:03:23.190076Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"validate_df","metadata":{"execution":{"iopub.status.busy":"2023-03-21T03:03:23.193529Z","iopub.execute_input":"2023-03-21T03:03:23.193941Z","iopub.status.idle":"2023-03-21T03:03:23.205642Z","shell.execute_reply.started":"2023-03-21T03:03:23.193905Z","shell.execute_reply":"2023-03-21T03:03:23.204619Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df['category']=train_df['category'].astype(str)\nvalidate_df['category']=validate_df['category'].astype(str)\n# # train_df\n","metadata":{"execution":{"iopub.status.busy":"2023-03-21T03:03:23.207087Z","iopub.execute_input":"2023-03-21T03:03:23.208222Z","iopub.status.idle":"2023-03-21T03:03:23.227463Z","shell.execute_reply.started":"2023-03-21T03:03:23.208184Z","shell.execute_reply":"2023-03-21T03:03:23.226516Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<div id='1'\n     style = \"display: fill;\n              border-radius: 10px;\n              background-color: #AD8B73;\">\n    <h3 style = \"padding: 15px; \n                 color: white;\n                 text-align: left;\n                 font-family: Trebuchet MS;\"> Training Image Generator\n    </h3>\n</div>","metadata":{"papermill":{"duration":0.008357,"end_time":"2023-03-08T09:28:20.622243","exception":false,"start_time":"2023-03-08T09:28:20.613886","status":"completed"},"tags":[]}},{"cell_type":"code","source":"\nimage_size = 224\ninput_shape = (image_size, image_size, 3)\n\nepochs = 10\nbatch_size = 16","metadata":{"execution":{"iopub.status.busy":"2023-03-21T03:03:23.229048Z","iopub.execute_input":"2023-03-21T03:03:23.229484Z","iopub.status.idle":"2023-03-21T03:03:23.236700Z","shell.execute_reply.started":"2023-03-21T03:03:23.229448Z","shell.execute_reply":"2023-03-21T03:03:23.235589Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_datagen = ImageDataGenerator(\n    rotation_range=15,\n    rescale=1./255,\n    shear_range=0.2,\n    zoom_range=0.2,\n    horizontal_flip=True,\n    fill_mode='nearest',\n    width_shift_range=0.1,\n    height_shift_range=0.1\n)\n\ntrain_generator = train_datagen.flow_from_dataframe(\n    train_df, \n    \"/kaggle/working/train/train/\", \n    x_col='filename',\n    y_col='category',\n    class_mode='binary',\n    target_size=(image_size, image_size),\n    batch_size=batch_size\n)","metadata":{"execution":{"iopub.status.busy":"2023-03-21T03:03:23.238281Z","iopub.execute_input":"2023-03-21T03:03:23.238790Z","iopub.status.idle":"2023-03-21T03:03:23.427682Z","shell.execute_reply.started":"2023-03-21T03:03:23.238755Z","shell.execute_reply":"2023-03-21T03:03:23.426642Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<div id='1'\n     style = \"display: fill;\n              border-radius: 10px;\n              background-color: #AD8B73;\">\n    <h3 style = \"padding: 15px; \n                 color: white;\n                 text-align: left;\n                 font-family: Trebuchet MS;\"> Validation Image Generator\n    </h3>\n</div>","metadata":{"papermill":{"duration":0.008357,"end_time":"2023-03-08T09:28:20.622243","exception":false,"start_time":"2023-03-08T09:28:20.613886","status":"completed"},"tags":[]}},{"cell_type":"code","source":"validation_datagen = ImageDataGenerator(rescale=1./255)\nvalidation_generator = validation_datagen.flow_from_dataframe(\n    validate_df, \n    \"/kaggle/working/train/train/\", \n    x_col='filename',\n    y_col='category',\n    class_mode='binary',\n    target_size=(image_size, image_size),\n    batch_size=batch_size\n)","metadata":{"execution":{"iopub.status.busy":"2023-03-21T03:03:23.431007Z","iopub.execute_input":"2023-03-21T03:03:23.431303Z","iopub.status.idle":"2023-03-21T03:03:23.487809Z","shell.execute_reply.started":"2023-03-21T03:03:23.431276Z","shell.execute_reply":"2023-03-21T03:03:23.486903Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(validation_generator.class_indices)\nprint(validation_generator.n)\nprint(len(validation_generator))\n# print(validation_generator)\n# validation_generator","metadata":{"execution":{"iopub.status.busy":"2023-03-21T03:03:23.492340Z","iopub.execute_input":"2023-03-21T03:03:23.492609Z","iopub.status.idle":"2023-03-21T03:03:23.498882Z","shell.execute_reply.started":"2023-03-21T03:03:23.492584Z","shell.execute_reply":"2023-03-21T03:03:23.497789Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<div id='1'\n     style = \"display: fill;\n              border-radius: 10px;\n              background-color: #AD8B73;\">\n    <h3 style = \"padding: 15px; \n                 color: white;\n                 text-align: left;\n                 font-family: Trebuchet MS;\"> Sample Images Generated\n    </h3>\n</div>","metadata":{"papermill":{"duration":0.008374,"end_time":"2023-03-08T09:28:20.744696","exception":false,"start_time":"2023-03-08T09:28:20.736322","status":"completed"},"tags":[]}},{"cell_type":"code","source":"example_df = train_df.sample(n=1).reset_index(drop = True)\nexample_generator = train_datagen.flow_from_dataframe(\n    example_df,\n    \"/kaggle/working/train/train/\",\n    x_col='filename',\n    y_col='category',\n    class_mode='categorical'\n)\nplt.figure(figsize=(12, 12))\nfor i in range(0, 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        break\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-03-21T03:03:23.500437Z","iopub.execute_input":"2023-03-21T03:03:23.501121Z","iopub.status.idle":"2023-03-21T03:03:25.203042Z","shell.execute_reply.started":"2023-03-21T03:03:23.501069Z","shell.execute_reply":"2023-03-21T03:03:25.202160Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<div id='3'\n     style = \"display: fill;\n              border-radius: 10px;\n              background-color: #B7CADB;\">\n    <h1 style = \"padding: 15px; \n                 color: white;\n                 text-align: left;\n                 font-family: Trebuchet MS;\">Model Training\n    </h1>\n</div>","metadata":{"papermill":{"duration":0.008166,"end_time":"2023-03-08T09:28:20.691678","exception":false,"start_time":"2023-03-08T09:28:20.683512","status":"completed"},"tags":[]}},{"cell_type":"code","source":"\n\npre_trained_model = VGG16(input_shape=input_shape, include_top=False, weights=\"imagenet\")\n    \nfor layer in pre_trained_model.layers[:15]:\n    layer.trainable = False\n\nfor layer in pre_trained_model.layers[15:]:\n    layer.trainable = True\n    \nlast_layer = pre_trained_model.get_layer('block5_pool')\nlast_output = last_layer.output\n    \n# Flatten the output layer to 1 dimension\nx = GlobalMaxPooling2D()(last_output)\n# Add a fully connected layer with 512 hidden units and ReLU activation\nx = Dense(512, activation='relu')(x)\n# Add a dropout rate of 0.5\nx = Dropout(0.5)(x)\n# Add a final sigmoid layer for classification\nx = layers.Dense(1, activation='sigmoid')(x)\n\nmodel = Model(pre_trained_model.input, x)\n\nmodel.compile(loss='binary_crossentropy',\n              optimizer=optimizers.SGD(lr=1e-4, momentum=0.9),\n              metrics=['accuracy'])\n\nmodel.summary()","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2023-03-21T03:03:25.204597Z","iopub.execute_input":"2023-03-21T03:03:25.205250Z","iopub.status.idle":"2023-03-21T03:03:25.623397Z","shell.execute_reply.started":"2023-03-21T03:03:25.205210Z","shell.execute_reply":"2023-03-21T03:03:25.622583Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.utils import plot_model\nplot_model(model, show_shapes = True,expand_nested = True,dpi = 80)","metadata":{"execution":{"iopub.status.busy":"2023-03-21T03:03:25.624462Z","iopub.execute_input":"2023-03-21T03:03:25.625026Z","iopub.status.idle":"2023-03-21T03:03:25.904775Z","shell.execute_reply.started":"2023-03-21T03:03:25.624996Z","shell.execute_reply":"2023-03-21T03:03:25.903412Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install visualkeras\nimport visualkeras\nvisualkeras.layered_view(model, legend=True) ","metadata":{"execution":{"iopub.status.busy":"2023-03-21T03:03:25.906889Z","iopub.execute_input":"2023-03-21T03:03:25.907518Z","iopub.status.idle":"2023-03-21T03:03:35.535527Z","shell.execute_reply.started":"2023-03-21T03:03:25.907465Z","shell.execute_reply":"2023-03-21T03:03:35.534312Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<div id='1'\n     style = \"display: fill;\n              border-radius: 10px;\n              background-color: #AD8B73;\">\n    <h3 style = \"padding: 15px; \n                 color: white;\n                 text-align: left;\n                 font-family: Trebuchet MS;\"> FIT MODEL\n    </h3>\n</div>","metadata":{"papermill":{"duration":0.008357,"end_time":"2023-03-08T09:28:20.622243","exception":false,"start_time":"2023-03-08T09:28:20.613886","status":"completed"},"tags":[]}},{"cell_type":"code","source":"# fine-tune the model\nhistory = model.fit(\n    train_generator,\n    epochs=epochs,\n    validation_data=validation_generator,\n    validation_steps=total_validate//batch_size,\n    steps_per_epoch=total_train//batch_size)\n","metadata":{"papermill":{"duration":5.833109,"end_time":"2023-03-08T09:28:26.586661","exception":false,"start_time":"2023-03-08T09:28:20.753552","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-03-21T03:03:35.537131Z","iopub.execute_input":"2023-03-21T03:03:35.538092Z","iopub.status.idle":"2023-03-21T03:51:06.685580Z","shell.execute_reply.started":"2023-03-21T03:03:35.538042Z","shell.execute_reply":"2023-03-21T03:51:06.684593Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<div id='4'\n     style = \"display: fill;\n              border-radius: 10px;\n              background-color: #B7CADB;\">\n    <h1 style = \"padding: 15px; \n                 color: white;\n                 text-align: left;\n                 font-family: Trebuchet MS;\">Evaluation\n    </h1>\n</div>","metadata":{"papermill":{"duration":0.009154,"end_time":"2023-03-08T09:28:26.605771","exception":false,"start_time":"2023-03-08T09:28:26.596617","status":"completed"},"tags":[]}},{"cell_type":"markdown","source":"<div id='1'\n     style = \"display: fill;\n              border-radius: 10px;\n              background-color: #AD8B73;\">\n    <h3 style = \"padding: 15px; \n                 color: white;\n                 text-align: left;\n                 font-family: Trebuchet MS;\">Accuracy\n    </h3>\n</div>","metadata":{"papermill":{"duration":0.009338,"end_time":"2023-03-08T09:28:26.624742","exception":false,"start_time":"2023-03-08T09:28:26.615404","status":"completed"},"tags":[]}},{"cell_type":"code","source":"loss, accuracy = model.evaluate(train_generator, verbose=1)\nprint(\"Train : accuracy = %f  ;  loss = %f\" % (accuracy, loss))\n\nloss_v, accuracy_v = model.evaluate(validation_generator, verbose=1)\nprint(\"Validation : accuracy = %f  ;  loss_v = %f\" % (accuracy_v, loss_v))\n\nmodel.save(\"finalmodel.h5\")","metadata":{"papermill":{"duration":0.023282,"end_time":"2023-03-08T09:28:26.657449","exception":false,"start_time":"2023-03-08T09:28:26.634167","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-03-21T03:51:06.687291Z","iopub.execute_input":"2023-03-21T03:51:06.687654Z","iopub.status.idle":"2023-03-21T03:55:49.578419Z","shell.execute_reply.started":"2023-03-21T03:51:06.687601Z","shell.execute_reply":"2023-03-21T03:55:49.577286Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, ax = plt.subplots(2,1)\nax[0].plot(history.history['loss'], color='b', label=\"Training loss\")\nax[0].plot(history.history['val_loss'], color='r', label=\"validation loss\",axes =ax[0])\nlegend = ax[0].legend(loc='best', shadow=True)\n\nax[1].plot(history.history['accuracy'], color='b', label=\"Training accuracy\")\nax[1].plot(history.history['val_accuracy'], color='r',label=\"Validation accuracy\")\nlegend = ax[1].legend(loc='best', shadow=True)","metadata":{"execution":{"iopub.status.busy":"2023-03-21T03:55:49.580875Z","iopub.execute_input":"2023-03-21T03:55:49.581963Z","iopub.status.idle":"2023-03-21T03:55:49.921085Z","shell.execute_reply.started":"2023-03-21T03:55:49.581923Z","shell.execute_reply":"2023-03-21T03:55:49.920043Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<div id='5'\n     style = \"display: fill;\n              border-radius: 10px;\n              background-color: #B7CADB;\">\n    <h1 style = \"padding: 15px; \n                 color: white;\n                 text-align: left;\n                 font-family: Trebuchet MS;\">Prepare Testing Data\n    </h1>\n</div>","metadata":{"papermill":{"duration":0.00958,"end_time":"2023-03-08T09:28:27.407615","exception":false,"start_time":"2023-03-08T09:28:27.398035","status":"completed"},"tags":[]}},{"cell_type":"code","source":"test_filenames = os.listdir(\"/kaggle/working/test/test1\")\ntest_df = pd.DataFrame({\n    'filename': test_filenames\n})\nnb_samples = test_df.shape[0]","metadata":{"papermill":{"duration":0.467943,"end_time":"2023-03-08T09:28:27.885481","exception":false,"start_time":"2023-03-08T09:28:27.417538","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-03-21T03:55:49.922855Z","iopub.execute_input":"2023-03-21T03:55:49.923247Z","iopub.status.idle":"2023-03-21T03:55:49.937866Z","shell.execute_reply.started":"2023-03-21T03:55:49.923207Z","shell.execute_reply":"2023-03-21T03:55:49.936977Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<div id='1'\n     style = \"display: fill;\n              border-radius: 10px;\n              background-color: #AD8B73;\">\n    <h3 style = \"padding: 15px; \n                 color: white;\n                 text-align: left;\n                 font-family: Trebuchet MS;\"> Test Image Generator\n    </h3>\n</div>","metadata":{"papermill":{"duration":0.008357,"end_time":"2023-03-08T09:28:20.622243","exception":false,"start_time":"2023-03-08T09:28:20.613886","status":"completed"},"tags":[]}},{"cell_type":"code","source":"test_gen = ImageDataGenerator(rescale=1./255)\ntest_generator = test_gen.flow_from_dataframe(\n    test_df, \n    \"/kaggle/working/test/test1\", \n    x_col='filename',\n    y_col=None,\n    class_mode=None,\n    batch_size=batch_size,\n    target_size=(image_size, image_size),\n    shuffle=False\n)","metadata":{"execution":{"iopub.status.busy":"2023-03-21T03:55:49.939341Z","iopub.execute_input":"2023-03-21T03:55:49.939690Z","iopub.status.idle":"2023-03-21T03:55:50.044185Z","shell.execute_reply.started":"2023-03-21T03:55:49.939654Z","shell.execute_reply":"2023-03-21T03:55:50.043256Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<div id='1'\n     style = \"display: fill;\n              border-radius: 10px;\n              background-color: #AD8B73;\">\n    <h3 style = \"padding: 15px; \n                 color: white;\n                 text-align: left;\n                 font-family: Trebuchet MS;\"> Test Image Generator\n    </h3>\n</div>","metadata":{"papermill":{"duration":0.008357,"end_time":"2023-03-08T09:28:20.622243","exception":false,"start_time":"2023-03-08T09:28:20.613886","status":"completed"},"tags":[]}},{"cell_type":"code","source":"predict = model.predict_generator(test_generator, steps=np.ceil(nb_samples/batch_size))\nthreshold = 0.5\ntest_df['category'] = np.where(predict > threshold, 1,0)","metadata":{"execution":{"iopub.status.busy":"2023-03-21T03:55:50.045562Z","iopub.execute_input":"2023-03-21T03:55:50.046016Z","iopub.status.idle":"2023-03-21T03:56:36.070858Z","shell.execute_reply.started":"2023-03-21T03:55:50.045978Z","shell.execute_reply":"2023-03-21T03:56:36.069773Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<div id='6'\n     style = \"display: fill;\n              border-radius: 10px;\n              background-color: #B7CADB;\">\n    <h1 style = \"padding: 15px; \n                 color: white;\n                 text-align: left;\n                 font-family: Trebuchet MS;\">Submission\n    </h1>\n</div>","metadata":{"papermill":{"duration":0.00958,"end_time":"2023-03-08T09:28:27.407615","exception":false,"start_time":"2023-03-08T09:28:27.398035","status":"completed"},"tags":[]}},{"cell_type":"code","source":"sample_test = test_df.sample(n=9).reset_index()\nsample_test.head()\nplt.figure(figsize=(12, 12))\nfor index, row in sample_test.iterrows():\n    filename = row['filename']\n    category = 'Cat'if row['category']==0 else 'Dog'\n    img = np.asarray(Image.open(\"/kaggle/working/test/test1/\"+filename))\n    plt.subplot(3, 3, index+1)\n    plt.imshow(img)\n    plt.xlabel(filename + '  Category: (' + \"{}\".format(category) + ')')\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-03-21T03:56:36.072484Z","iopub.execute_input":"2023-03-21T03:56:36.072853Z","iopub.status.idle":"2023-03-21T03:56:38.038066Z","shell.execute_reply.started":"2023-03-21T03:56:36.072813Z","shell.execute_reply":"2023-03-21T03:56:38.037162Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_df = test_df.copy()\nsubmission_df['id'] = submission_df['filename'].str.split('.').str[0]\nsubmission_df['label'] = submission_df['category']\nsubmission_df.drop(['filename', 'category'], axis=1, inplace=True)\nsubmission_df.to_csv('submission_1.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2023-03-21T03:56:38.039540Z","iopub.execute_input":"2023-03-21T03:56:38.040152Z","iopub.status.idle":"2023-03-21T03:56:38.094642Z","shell.execute_reply.started":"2023-03-21T03:56:38.040113Z","shell.execute_reply":"2023-03-21T03:56:38.093654Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<div id='5'\n     style = \"display: fill;\n              border-radius: 10px;\n              background-color: #0E8388;\">\n    <h1 style = \"padding: 30px; \n                 color: white;\n                 font-weight:600;\n                 font-size:50px;\n                 text-align: CENTER;\n                 font-family: Tilt Warp;\">PLEASE UPVOTE IF IT HELPED\n    </h1>\n</div>","metadata":{"papermill":{"duration":0.009917,"end_time":"2023-03-08T09:28:27.905871","exception":false,"start_time":"2023-03-08T09:28:27.895954","status":"completed"},"tags":[]}}]}