{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.11.11","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"nvidiaTeslaT4","dataSources":[{"sourceId":3362,"databundleVersionId":31148,"sourceType":"competition"},{"sourceId":5441,"databundleVersionId":38425,"sourceType":"competition"},{"sourceId":791828,"sourceType":"datasetVersion","datasetId":119698},{"sourceId":1003830,"sourceType":"datasetVersion","datasetId":550917}],"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# Basic\nimport os\nfrom os import makedirs\nfrom os import listdir\nfrom shutil import copyfile\nfrom random import seed\nfrom random import random\nimport numpy as np\nimport pandas as pd\n\n# visuals\nimport seaborn as sns\nimport matplotlib.pyplot as plt\nfrom matplotlib.image import imread\nfrom PIL import Image\n\n# Scikit-learn\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import classification_report,confusion_matrix,ConfusionMatrixDisplay\n\n# Tensorflow\nimport tensorflow as tf\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras.layers import Dense,MaxPooling2D,Dropout,Flatten,BatchNormalization,Conv2D\nfrom tensorflow.keras.callbacks import ReduceLROnPlateau,EarlyStopping","metadata":{"execution":{"iopub.status.busy":"2023-09-05T14:35:56.251814Z","iopub.execute_input":"2023-09-05T14:35:56.252235Z","iopub.status.idle":"2023-09-05T14:35:56.260866Z","shell.execute_reply.started":"2023-09-05T14:35:56.252204Z","shell.execute_reply":"2023-09-05T14:35:56.259533Z"},"_kg_hide-output":true,"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_path = \"../input/dogs-vs-cats/test1.zip\"\ntest_path = \"../input/dogs-vs-cats/train.zip\"\n\nfiles = \"/kaggle/working/\"\n\nimport zipfile\n\nwith zipfile.ZipFile(train_path, 'r') as zipp:\n    zipp.extractall(files)\n    \nwith zipfile.ZipFile(test_path, 'r') as zipp:\n    zipp.extractall(files)","metadata":{"execution":{"iopub.status.busy":"2023-09-05T13:47:45.979689Z","iopub.execute_input":"2023-09-05T13:47:45.980955Z","iopub.status.idle":"2023-09-05T13:48:03.919766Z","shell.execute_reply.started":"2023-09-05T13:47:45.980919Z","shell.execute_reply":"2023-09-05T13:48:03.918726Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"### Loading Images in a Dataframe","metadata":{}},{"cell_type":"code","source":"image_dir = \"../working/train/\"\n\nfilenames = os.listdir(image_dir)\nlabels = [x.split(\".\")[0] for x in filenames]\n\ndata = pd.DataFrame({\"filename\": filenames, \"label\": labels})\n\ndata.head()","metadata":{"execution":{"iopub.status.busy":"2023-09-05T13:48:09.144563Z","iopub.execute_input":"2023-09-05T13:48:09.144927Z","iopub.status.idle":"2023-09-05T13:48:09.1954Z","shell.execute_reply.started":"2023-09-05T13:48:09.144897Z","shell.execute_reply":"2023-09-05T13:48:09.194456Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"<a id='5'></a>\n<div class=\"alert alert-block alert-info\"><h2 style=\"text-align:center;color:black\"> Data Exploration </h2> </div>","metadata":{}},{"cell_type":"markdown","source":"First, we start by visualizing the variable of interest.\n\nLet's view more images in a grid format.\n\n<h5 style=\"text-align:center;color:Green\">We visualize few images of Dogs. </h5>","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(20,20)) # specifying the overall grid size\nplt.subplots_adjust(hspace=0.4)\n\n\nfor i in range(10):\n    \n    plt.subplot(1,10,i+1)    # the number of images in the grid is 10*10 (100)\n    filename = 'train/' + 'dog.' + str(i) + '.jpg'\n    image = imread(filename)\n    plt.imshow(image)\n    plt.title('Dog',fontsize=12)\n    plt.axis('off')\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-09-05T13:48:18.729817Z","iopub.execute_input":"2023-09-05T13:48:18.730492Z","iopub.status.idle":"2023-09-05T13:48:19.746436Z","shell.execute_reply.started":"2023-09-05T13:48:18.730458Z","shell.execute_reply":"2023-09-05T13:48:19.745511Z"},"_kg_hide-input":true,"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"<h5 style=\"text-align:center;color:Red\">We visualize few images of Cats. </h5>","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(20,20)) # specifying the overall grid size\nplt.subplots_adjust(hspace=0.4)\n\n\nfor i in range(10):\n    \n    plt.subplot(1,10,i+1)    # the number of images in the grid is 10*10 (100)\n    filename = 'train/' + 'cat.' + str(i) + '.jpg'\n    image = imread(filename)\n    plt.imshow(image)\n    plt.title('Cat',fontsize=12)\n    plt.axis('off')\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-09-05T13:48:24.109937Z","iopub.execute_input":"2023-09-05T13:48:24.110358Z","iopub.status.idle":"2023-09-05T13:48:25.068551Z","shell.execute_reply.started":"2023-09-05T13:48:24.110307Z","shell.execute_reply":"2023-09-05T13:48:25.06771Z"},"_kg_hide-input":true,"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"Images are of varying size.\n\nEvery time the cell is run different set of images will be displayed, one can scan the images of both categories. Presence of humans in some images could be a challenge for the model to classify.\n\nSome images have more than one cats or dogs respectively.","metadata":{}},{"cell_type":"markdown","source":"<a id='6'></a>\n<div class=\"alert alert-block alert-info\"><h2 style=\"text-align:center;color:black\"> Train Test Split </h2> </div>","metadata":{}},{"cell_type":"markdown","source":"<a id='7'></a>\n<div class=\"alert alert-block alert-warning\"><h4 style=\"text-align:center;color:black\"> Using Dataframe </h4> </div>","metadata":{}},{"cell_type":"code","source":"# train test split using dataframe\n\nlabels = data['label']\n\nX_train, X_temp = train_test_split(data, test_size=0.2, stratify=labels, random_state = 42)\n\nlabel_test_val = X_temp['label']\n\nX_test, X_val = train_test_split(X_temp, test_size=0.5, stratify=label_test_val, random_state = 42)\n\nprint('The shape of train data',X_train.shape)\nprint('The shape of test data',X_test.shape)\nprint('The shape of validation data',X_val.shape)","metadata":{"execution":{"iopub.status.busy":"2023-09-05T13:48:32.965079Z","iopub.execute_input":"2023-09-05T13:48:32.965438Z","iopub.status.idle":"2023-09-05T13:48:33.034261Z","shell.execute_reply.started":"2023-09-05T13:48:32.965409Z","shell.execute_reply":"2023-09-05T13:48:33.033108Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"Now we will Create a barplot to see the class distrubtion in trainting dataset.","metadata":{}},{"cell_type":"code","source":"labels = ['Cat','Dog']\n\nlabel1,count1 = np.unique(X_train.label,return_counts=True)\nlabel2,count2 = np.unique(X_val.label,return_counts=True)\nlabel3,count3 = np.unique(X_test.label,return_counts=True)\n\nuni1 = pd.DataFrame(data=count1,index=labels,columns=['Count1'])\nuni2 = pd.DataFrame(data=count2,index=labels,columns=['Count2'])\nuni3 = pd.DataFrame(data=count3,index=labels,columns=['Count3'])\n\n\nplt.figure(figsize=(20,6),dpi=200)\nsns.set_style('darkgrid')\n\nplt.subplot(131)\nsns.barplot(data=uni1,x=uni1.index,y='Count1',palette='icefire',width=0.2).set_title('Class distribution in Training set',fontsize=15)\nplt.xlabel('Labels',fontsize=12)\nplt.ylabel('Count',fontsize=12)\n\nplt.subplot(132)\nsns.barplot(data=uni2,x=uni2.index,y='Count2',palette='icefire',width=0.2).set_title('Class distribution in validation set',fontsize=15)\nplt.xlabel('Labels',fontsize=12)\nplt.ylabel('Count',fontsize=12)\n\n\nplt.subplot(133)\nsns.barplot(data=uni3,x=uni3.index,y='Count3',palette='icefire',width=0.2).set_title('Class distribution in Testing set',fontsize=15)\nplt.xlabel('Labels',fontsize=12)\nplt.ylabel('Count',fontsize=12)\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-09-05T13:48:37.209453Z","iopub.execute_input":"2023-09-05T13:48:37.210153Z","iopub.status.idle":"2023-09-05T13:48:38.03665Z","shell.execute_reply.started":"2023-09-05T13:48:37.210118Z","shell.execute_reply":"2023-09-05T13:48:38.035696Z"},"_kg_hide-input":true,"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"<a id='8'></a>\n<div class=\"alert alert-block alert-warning\"><h4 style=\"text-align:center;color:black\"> Using Directory </h4> </div>","metadata":{}},{"cell_type":"code","source":"# create directories\ndataset_home = 'dataset_dogs_vs_cats/'\nsubdirs = ['train/', 'test/']\n\nfor subdir in subdirs:\n    # create label subdirectories\n    labeldirs = ['dogs/', 'cats/']\n    for labldir in labeldirs:\n        newdir = dataset_home + subdir + labldir\n        makedirs(newdir, exist_ok=True)\n# seed random number generator\nseed(1)\n# define ratio of pictures to use for validation\nval_ratio = 0.2\n# copy training dataset images into subdirectories\nsrc_directory = 'train/'\nfor file in listdir(src_directory):\n        src = src_directory + '/' + file\n        dst_dir = 'train/'\n        if random() < val_ratio:\n            dst_dir = 'test/'\n        if file.startswith('cat'):\n            dst = dataset_home + dst_dir + 'cats/' + file\n            copyfile(src, dst)\n        elif file.startswith('dog'):\n            dst = dataset_home + dst_dir + 'dogs/' + file\n            copyfile(src, dst)\n\n            \npath1 = \"/kaggle/working/dataset_dogs_vs_cats/train/cats\"\npath2 = \"/kaggle/working/dataset_dogs_vs_cats/train/dogs\"\npath3 = \"/kaggle/working/dataset_dogs_vs_cats/test/cats\"\npath4 = \"/kaggle/working/dataset_dogs_vs_cats/test/dogs\"\n\n\nprint('Then number of cat images in training data is' ,len(os.listdir(path1)))\nprint('Then number of dog images in training data is' ,len(os.listdir(path2)))\nprint('Then number of cat images in validation data is' ,len(os.listdir(path3)))\nprint('Then number of dog images in validation data is' ,len(os.listdir(path4)))\n","metadata":{"execution":{"iopub.status.busy":"2023-09-05T13:48:46.839843Z","iopub.execute_input":"2023-09-05T13:48:46.840217Z","iopub.status.idle":"2023-09-05T13:48:49.361872Z","shell.execute_reply.started":"2023-09-05T13:48:46.840183Z","shell.execute_reply":"2023-09-05T13:48:49.360796Z"},"_kg_hide-input":true,"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"<a id='9'></a>\n<div class=\"alert alert-block alert-info\"><h2 style=\"text-align:center;color:black\"> Data Preparation </h2> </div>\n\nfirstly, we will list out all the important parameters and respective values.","metadata":{}},{"cell_type":"code","source":"# parameters\nimage_size = 128\nimage_channel = 3\nbat_size = 32","metadata":{"execution":{"iopub.status.busy":"2023-09-05T13:48:56.364574Z","iopub.execute_input":"2023-09-05T13:48:56.364952Z","iopub.status.idle":"2023-09-05T13:48:56.369879Z","shell.execute_reply.started":"2023-09-05T13:48:56.36492Z","shell.execute_reply":"2023-09-05T13:48:56.368791Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"<a id='10'></a>\n<div class=\"alert alert-block alert-danger\"><h3 style=\"text-align:center;color:black\">Image Data Generator </h3> </div>\n\n* The data for will used by flow_from_dataframe and flow_from_directory.\n* The batch size is 32 and the image size is (128,128).\n","metadata":{}},{"cell_type":"code","source":"# Creating image data generator\ntrain_datagen = ImageDataGenerator(rescale=1./255,\n                                    rotation_range = 15,\n                                    horizontal_flip = True,\n                                    zoom_range = 0.2,\n                                    shear_range = 0.1,\n                                    fill_mode = 'reflect',\n                                    width_shift_range = 0.1,\n                                    height_shift_range = 0.1)\n\ntest_datagen = ImageDataGenerator(rescale=1./255)","metadata":{"execution":{"iopub.status.busy":"2023-09-05T13:48:59.919701Z","iopub.execute_input":"2023-09-05T13:48:59.920071Z","iopub.status.idle":"2023-09-05T13:48:59.926486Z","shell.execute_reply.started":"2023-09-05T13:48:59.92004Z","shell.execute_reply":"2023-09-05T13:48:59.925241Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"<a id='11'></a>\n<div class=\"alert alert-block alert-warning\"><h4 style=\"text-align:center;color:black\"> Using Dataframe </h4> </div>","metadata":{}},{"cell_type":"code","source":"# Applying image data gernerator to train and test data\n\ntrain_generator = train_datagen.flow_from_dataframe(X_train,\n                                                    directory = 'train/',\n                                                    x_col= 'filename',\n                                                    y_col= 'label',\n                                                    batch_size = bat_size,\n                                                    target_size = (image_size,image_size)\n                                                   )\nval_generator = test_datagen.flow_from_dataframe(X_val, \n                                                 directory = 'train/',\n                                                 x_col= 'filename',\n                                                 y_col= 'label',\n                                                 batch_size = bat_size,\n                                                 target_size = (image_size,image_size),\n                                                 shuffle=False\n                                                )\n\ntest_generator = test_datagen.flow_from_dataframe(X_test, \n                                                  directory = 'train/',\n                                                  x_col= 'filename',\n                                                  y_col= 'label',\n                                                  batch_size = bat_size,\n                                                  target_size = (image_size,image_size),\n                                                  shuffle=False\n                                                 )","metadata":{"execution":{"iopub.status.busy":"2023-09-05T13:49:03.174663Z","iopub.execute_input":"2023-09-05T13:49:03.175028Z","iopub.status.idle":"2023-09-05T13:49:03.477142Z","shell.execute_reply.started":"2023-09-05T13:49:03.174999Z","shell.execute_reply":"2023-09-05T13:49:03.476055Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"<a id='12'></a>\n<div class=\"alert alert-block alert-warning\"><h4 style=\"text-align:center;color:black\"> Using Directory </h4> </div>","metadata":{}},{"cell_type":"code","source":"train_gen = train_datagen.flow_from_directory('/kaggle/working/dataset_dogs_vs_cats/train/', \n                                              class_mode='binary',\n                                              target_size = (image_size,image_size),\n                                              batch_size = bat_size,\n                                             )\n\nval_gen = test_datagen.flow_from_directory('/kaggle/working/dataset_dogs_vs_cats/test/', \n                                          class_mode='binary',\n                                          batch_size = bat_size,\n                                          target_size = (image_size,image_size),\n                                          shuffle = False\n                                         )","metadata":{"execution":{"iopub.status.busy":"2023-09-05T15:05:32.785369Z","iopub.execute_input":"2023-09-05T15:05:32.785738Z","iopub.status.idle":"2023-09-05T15:05:33.461229Z","shell.execute_reply.started":"2023-09-05T15:05:32.785707Z","shell.execute_reply":"2023-09-05T15:05:33.460326Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"<a id='13'></a>\n<div class=\"alert alert-block alert-info\"><h2 style=\"text-align:center;color:black\"> Deep Learning Model </h2> </div>","metadata":{}},{"cell_type":"code","source":"model = Sequential()\n\n# Input Layer\nmodel.add(Conv2D(32,(3,3),activation='relu',input_shape = (image_size,image_size,image_channel))) \nmodel.add(BatchNormalization())\nmodel.add(MaxPooling2D(pool_size=(2,2)))\nmodel.add(Dropout(0.2))\n\n# Bloack 1 \nmodel.add(Conv2D(64,(3,3),activation='relu'))\nmodel.add(BatchNormalization())\nmodel.add(MaxPooling2D(pool_size=(2,2)))\nmodel.add(Dropout(0.2))\n# Block 2\nmodel.add(Conv2D(128,(3,3),activation='relu'))\nmodel.add(BatchNormalization())\nmodel.add(MaxPooling2D(pool_size=(2,2)))\nmodel.add(Dropout(0.2))\n# Block 3\nmodel.add(Conv2D(256,(3,3),activation='relu'))\nmodel.add(BatchNormalization())\nmodel.add(MaxPooling2D(pool_size=(2,2)))\nmodel.add(Dropout(0.2))\n\n# Fully Connected layers \nmodel.add(Flatten())\nmodel.add(Dense(512,activation='relu'))\nmodel.add(BatchNormalization())\nmodel.add(Dropout(0.2))\n\n# Output layer\nmodel.add(Dense(2,activation='softmax'))\n\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2023-09-05T13:49:38.42484Z","iopub.execute_input":"2023-09-05T13:49:38.425198Z","iopub.status.idle":"2023-09-05T13:49:38.645106Z","shell.execute_reply.started":"2023-09-05T13:49:38.425169Z","shell.execute_reply":"2023-09-05T13:49:38.644348Z"},"_kg_hide-input":true,"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"<a id='15'></a>\n<div class=\"alert alert-block alert-danger\"><h3 style=\"text-align:center;color:black\"> Callbacks </h3> </div>\nwe will be using two callbacks -\n\n* **ReduceLROnPlateau :** Reduce learning rate when a metric has stopped improving.\n* **EarlyStopping :** Stop training when a monitored metric has stopped improving.","metadata":{}},{"cell_type":"code","source":"learning_rate_reduction = ReduceLROnPlateau(monitor = 'val_accuracy',\n                                            patience=2,\n                                            factor=0.5,\n                                            min_lr = 0.00001,\n                                            verbose = 1)\n\nearly_stoping = EarlyStopping(monitor='val_loss',patience= 3,restore_best_weights=True,verbose=0)","metadata":{"execution":{"iopub.status.busy":"2023-09-05T13:50:35.345069Z","iopub.execute_input":"2023-09-05T13:50:35.34543Z","iopub.status.idle":"2023-09-05T13:50:35.352108Z","shell.execute_reply.started":"2023-09-05T13:50:35.345401Z","shell.execute_reply":"2023-09-05T13:50:35.350825Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model.compile(optimizer='adam',loss='binary_crossentropy',metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2023-09-05T13:50:00.299663Z","iopub.execute_input":"2023-09-05T13:50:00.300547Z","iopub.status.idle":"2023-09-05T13:50:00.320395Z","shell.execute_reply.started":"2023-09-05T13:50:00.300512Z","shell.execute_reply":"2023-09-05T13:50:00.31947Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"cat_dog = model.fit(train_generator,\n                    validation_data = val_generator, \n                    callbacks=[early_stoping,learning_rate_reduction],\n                    epochs = 30,\n                    # steps_per_epoch = len(train_generator),\n                    # validation_steps = len(val_generaotor),\n                   )","metadata":{"execution":{"iopub.status.busy":"2023-09-05T13:50:38.996717Z","iopub.execute_input":"2023-09-05T13:50:38.997099Z","iopub.status.idle":"2023-09-05T14:20:52.678296Z","shell.execute_reply.started":"2023-09-05T13:50:38.997067Z","shell.execute_reply":"2023-09-05T14:20:52.677203Z"},"_kg_hide-output":true,"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# plots for accuracy and Loss with epochs\n\nerror = pd.DataFrame(cat_dog.history)\n\nplt.figure(figsize=(18,5),dpi=200)\nsns.set_style('darkgrid')\n\nplt.subplot(121)\nplt.title('Cross Entropy Loss',fontsize=15)\nplt.xlabel('Epochs',fontsize=12)\nplt.ylabel('Loss',fontsize=12)\nplt.plot(error['loss'])\nplt.plot(error['val_loss'])\n\nplt.subplot(122)\nplt.title('Classification Accuracy',fontsize=15)\nplt.xlabel('Epochs',fontsize=12)\nplt.ylabel('Accuracy',fontsize=12)\nplt.plot(error['accuracy'])\nplt.plot(error['val_accuracy'])\n\nplt.show()","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2023-09-05T14:23:31.710594Z","iopub.execute_input":"2023-09-05T14:23:31.711004Z","iopub.status.idle":"2023-09-05T14:24:22.711667Z","shell.execute_reply.started":"2023-09-05T14:23:31.710969Z","shell.execute_reply":"2023-09-05T14:24:22.710688Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Evaluvate for train generator\nloss,acc = model.evaluate(train_generator,batch_size = bat_size, verbose = 0)\n\nprint('The accuracy of the model for training data is:',acc*100)\nprint('The Loss of the model for training data is:',loss)\n\n# Evaluvate for validation generator\nloss,acc = model.evaluate(val_generator,batch_size = bat_size, verbose = 0)\n\nprint('The accuracy of the model for validation data is:',acc*100)\nprint('The Loss of the model for validation data is:',loss)","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2023-09-05T14:24:58.829815Z","iopub.execute_input":"2023-09-05T14:24:58.830198Z","iopub.status.idle":"2023-09-05T14:26:42.954357Z","shell.execute_reply.started":"2023-09-05T14:24:58.830167Z","shell.execute_reply":"2023-09-05T14:26:42.952944Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"Finally, we will save the model for future use.","metadata":{}},{"cell_type":"code","source":"# Save the Model\nmodel.save(\"model.h5\")","metadata":{"execution":{"iopub.status.busy":"2023-09-05T14:26:42.957417Z","iopub.execute_input":"2023-09-05T14:26:42.958445Z","iopub.status.idle":"2023-09-05T14:26:43.107784Z","shell.execute_reply.started":"2023-09-05T14:26:42.958258Z","shell.execute_reply":"2023-09-05T14:26:43.10678Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"<a id='19'></a>\n<div class=\"alert alert-block alert-info\"><h2 style=\"text-align:center;color:black\"> Prediction </h2> </div>\n\nNow, we will predict the model on test dataset.","metadata":{}},{"cell_type":"code","source":"# prediction\nresult = model.predict(test_generator,batch_size = bat_size,verbose = 0)\n\ny_pred = np.argmax(result, axis = 1)\n\ny_true = test_generator.labels\n\n# Evaluvate\nloss,acc = model.evaluate(test_generator, batch_size = bat_size, verbose = 0)\n\nprint('The accuracy of the model for testing data is:',acc*100)\nprint('The Loss of the model for testing data is:',loss)","metadata":{"execution":{"iopub.status.busy":"2023-09-05T14:27:06.299747Z","iopub.execute_input":"2023-09-05T14:27:06.300335Z","iopub.status.idle":"2023-09-05T14:27:13.998495Z","shell.execute_reply.started":"2023-09-05T14:27:06.300301Z","shell.execute_reply":"2023-09-05T14:27:13.997454Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"Classification report","metadata":{}},{"cell_type":"code","source":"labels =['Cat','Dog']\nprint(classification_report(y_true, y_pred,target_names=labels))","metadata":{"execution":{"iopub.status.busy":"2023-09-05T14:27:19.159733Z","iopub.execute_input":"2023-09-05T14:27:19.160747Z","iopub.status.idle":"2023-09-05T14:27:19.180458Z","shell.execute_reply.started":"2023-09-05T14:27:19.160712Z","shell.execute_reply":"2023-09-05T14:27:19.179448Z"},"_kg_hide-input":true,"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"confusioin matrix","metadata":{}},{"cell_type":"code","source":"confusion_mtx = confusion_matrix(y_true,y_pred) \n\nf,ax = plt.subplots(figsize = (8,4),dpi=200)\nsns.heatmap(confusion_mtx, annot=True, linewidths=0.1, cmap = \"gist_yarg_r\", linecolor=\"black\", fmt='.0f', ax=ax,cbar=False, xticklabels=labels, yticklabels=labels)\n\nplt.xlabel(\"Predicted Label\",fontsize=10)\nplt.ylabel(\"True Label\",fontsize=10)\nplt.title(\"Confusion Matrix\",fontsize=13)\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-09-05T14:27:22.841287Z","iopub.execute_input":"2023-09-05T14:27:22.84164Z","iopub.status.idle":"2023-09-05T14:27:23.106554Z","shell.execute_reply.started":"2023-09-05T14:27:22.841611Z","shell.execute_reply":"2023-09-05T14:27:23.105668Z"},"_kg_hide-input":true,"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"<a id='20'></a>\n<div class=\"alert alert-block alert-info\"><h2 style=\"text-align:center;color:black\"> submission </h2> </div>\nMake predictions on kaggle test data for submission.","metadata":{}},{"cell_type":"code","source":"size =(128,128)\n\n# loading into dataframe\ntest_dir = \"../working/test1/\"\nfilenames = os.listdir(test_dir)\ntest_data = pd.DataFrame({\"filename\": filenames})\ntest_data['label'] = 'unknown'\n\n# Create data genenerator for test data\ntest1_idg =  test_datagen.flow_from_dataframe(test_data, \n                                     \"test1/\", \n                                     x_col= \"filename\",\n                                     y_col = 'label',\n                                     batch_size = bat_size,\n                                     target_size=size, \n                                     shuffle = False)\n\n# Test Prediction\ntest1_predict = model.predict(test1_idg,verbose = 0)\n\ntest1_predict_argmax = np.argmax(test1_predict, axis=1)\n\ny_test_pred = test1_predict_argmax\n\ntest_data['label'] = y_test_pred\n\n# mapping\nlabel_mapping = {0: 'cat', 1: 'dog'}\ntest_data['label'] = test_data['label'].map(label_mapping)\ntest_data.head()\n\n# csv file output for submission\nsub = pd.read_csv('/kaggle/input/dogs-vs-cats/sampleSubmission.csv',index_col='id')\n\nsub['label'] = y_test_pred\n\nsub.to_csv('submission.csv',index=True)","metadata":{"execution":{"iopub.status.busy":"2023-09-05T14:41:38.289739Z","iopub.execute_input":"2023-09-05T14:41:38.290138Z","iopub.status.idle":"2023-09-05T14:41:56.482153Z","shell.execute_reply.started":"2023-09-05T14:41:38.290107Z","shell.execute_reply":"2023-09-05T14:41:56.481092Z"},"_kg_hide-input":true,"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"<a id='21'></a>\n<div class=\"alert alert-block alert-danger\"><h3 style=\"text-align:center;color:black\"> Visualize Classified Images  </h3> </div>","metadata":{}},{"cell_type":"code","source":"fig, axes = plt.subplots(1, 10, figsize=(20, 4))\nfor idx in range(10):\n    image_path = os.path.join(test_dir, test_data.iloc[idx]['filename'])\n    image = Image.open(image_path)\n    axes[idx].imshow(image)\n    axes[idx].set_title(\"Label: \" + test_data.iloc[idx]['label'])\n    axes[idx].axis('off')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-09-05T14:41:56.484409Z","iopub.execute_input":"2023-09-05T14:41:56.484822Z","iopub.status.idle":"2023-09-05T14:41:57.990679Z","shell.execute_reply.started":"2023-09-05T14:41:56.484785Z","shell.execute_reply":"2023-09-05T14:41:57.989632Z"},"_kg_hide-input":true,"trusted":true},"outputs":[],"execution_count":null}]}