{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.11.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"gpu","dataSources":[{"sourceId":3362,"databundleVersionId":31148,"sourceType":"competition"}],"dockerImageVersionId":31089,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\nimport warnings\nwarnings.filterwarnings(\"ignore\")\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2025-07-15T09:31:06.33204Z","iopub.execute_input":"2025-07-15T09:31:06.332245Z","iopub.status.idle":"2025-07-15T09:31:08.319843Z","shell.execute_reply.started":"2025-07-15T09:31:06.33222Z","shell.execute_reply":"2025-07-15T09:31:08.319165Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import zipfile\nwith zipfile.ZipFile(\"/kaggle/input/dogs-vs-cats/train.zip\",\"r\") as zip_ref:\n    zip_ref.extractall(\"/kaggle/working/train\") #You choose where you want the contents to go:\n                                                #here it is in /kaggle/working/train \n\nwith zipfile.ZipFile(\"/kaggle/input/dogs-vs-cats/test1.zip\", \"r\") as zip_ref:\n    zip_ref.extractall(\"/kaggle/working/test1\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-15T09:31:08.321353Z","iopub.execute_input":"2025-07-15T09:31:08.321677Z","iopub.status.idle":"2025-07-15T09:31:24.21339Z","shell.execute_reply.started":"2025-07-15T09:31:08.321654Z","shell.execute_reply":"2025-07-15T09:31:24.212855Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"zip_ref.namelist()[:10]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-15T09:31:24.214037Z","iopub.execute_input":"2025-07-15T09:31:24.214234Z","iopub.status.idle":"2025-07-15T09:31:24.220402Z","shell.execute_reply.started":"2025-07-15T09:31:24.214217Z","shell.execute_reply":"2025-07-15T09:31:24.219834Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import os\ntrain_dir = \"/kaggle/working/train/train\"\ntest_dir = '/kaggle/working/test1/test1'\n\n# Create a list of filenames\nfilenames = os.listdir(train_dir)\n\n# create labels based on filenames\ncategories = []\nfor fname in filenames:\n    if fname.startswith(\"cat\"):\n        categories.append(\"0\")  # cat:0\n    else:\n        categories.append(\"1\")  # dog : 1\n\n\n\nfilename1 = os.listdir(test_dir)\ntest_df = pd.DataFrame({\n    \"Filename1\" : filename1,\n})\ntest_df.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-15T09:31:24.221065Z","iopub.execute_input":"2025-07-15T09:31:24.221462Z","iopub.status.idle":"2025-07-15T09:31:24.278349Z","shell.execute_reply.started":"2025-07-15T09:31:24.221443Z","shell.execute_reply":"2025-07-15T09:31:24.277677Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df = pd.DataFrame({\n    \"Filename\": filenames,\n    \"Category\": categories\n})\n\ndf.sample(5)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-15T09:31:24.279268Z","iopub.execute_input":"2025-07-15T09:31:24.279509Z","iopub.status.idle":"2025-07-15T09:31:24.296774Z","shell.execute_reply.started":"2025-07-15T09:31:24.279482Z","shell.execute_reply":"2025-07-15T09:31:24.296109Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Count of each category\ndf['Category'].value_counts()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-15T09:31:24.297488Z","iopub.execute_input":"2025-07-15T09:31:24.297755Z","iopub.status.idle":"2025-07-15T09:31:24.310201Z","shell.execute_reply.started":"2025-07-15T09:31:24.297732Z","shell.execute_reply":"2025-07-15T09:31:24.309516Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Lets see random image\nimport matplotlib.pyplot as plt\nfrom tensorflow.keras.preprocessing import image\nimg = image.load_img(\"/kaggle/working/train/train/cat.8355.jpg\")\nplt.imshow(img)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-15T09:31:24.312326Z","iopub.execute_input":"2025-07-15T09:31:24.312547Z","iopub.status.idle":"2025-07-15T09:31:40.620841Z","shell.execute_reply.started":"2025-07-15T09:31:24.312531Z","shell.execute_reply":"2025-07-15T09:31:40.62007Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"img2 = image.load_img(\"/kaggle/working/train/train/dog.7808.jpg\")\nplt.imshow(img2)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-15T09:31:40.621589Z","iopub.execute_input":"2025-07-15T09:31:40.622138Z","iopub.status.idle":"2025-07-15T09:31:40.904801Z","shell.execute_reply.started":"2025-07-15T09:31:40.622117Z","shell.execute_reply":"2025-07-15T09:31:40.90402Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Use image datagenerator flow from dataframe\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\n\ndatagen = ImageDataGenerator(rescale = 1./255,\n                             validation_split = 0.2, \nrotation_range = 40, # random rotaio\n# width_shift_range = 0.2,\n# height_shift_range = 0.2,\nshear_range = 0.2,\nzoom_range = 0.2,\nhorizontal_flip = True)\n\n# fill_mode = 'nearest')\n\ntrain_gen = datagen.flow_from_dataframe(df, \n                                        directory = train_dir,\n                                        x_col = 'Filename', \n                                        y_col = 'Category',\n                                        target_size = (150,150), #This resizes all images to 150x150 pixels \n                                        class_mode = 'binary',\n                                        subset = 'training', \n                                        batch_size = 32)\n\nvalidation_gen = datagen.flow_from_dataframe(df,\n                                             directory = train_dir,\n                                             x_col = 'Filename',\n                                             y_col = 'Category',\n                                             target_size = (150,150),\n                                             class_mode = 'binary',\n                                             subset = 'validation', # take validation data from the set\n                                             batch_size = 32)\n\n\ntest_gen = ImageDataGenerator(rescale = 1./255)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-15T09:31:40.905756Z","iopub.execute_input":"2025-07-15T09:31:40.906246Z","iopub.status.idle":"2025-07-15T09:31:41.275756Z","shell.execute_reply.started":"2025-07-15T09:31:40.906224Z","shell.execute_reply":"2025-07-15T09:31:41.275182Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Model building","metadata":{}},{"cell_type":"code","source":"from tensorflow.keras import Sequential\nfrom tensorflow.keras.layers import Conv2D,MaxPooling2D,Flatten, Dense,Dropout\n\nmodel = Sequential()\n\n\n# 32 Filter Convolution: Each filter is of size 3,3\nmodel.add(Conv2D(32, kernel_size = (3,3), activation = 'relu', padding = 'valid', input_shape = (150,150,3)))\nmodel.add(Dropout(0.4))\nmodel.add(MaxPooling2D(pool_size = (2,2), strides = 2, padding = 'valid'))\n\n\n# filter 2\nmodel.add(Conv2D(64, kernel_size = (3,3), activation = 'relu', padding = 'valid'))\nmodel.add(Dropout(0.5))\nmodel.add(MaxPooling2D(pool_size = (2,2), strides = 2, padding = 'valid'))\n\nmodel.add(Conv2D(128, kernel_size = (3,3), activation = 'relu', padding = 'valid'))\nmodel.add(Dropout(0.5))\nmodel.add(MaxPooling2D(pool_size = (2,2), strides = 2, padding = 'valid'))\n\n\n# flatten \nmodel.add(Flatten()) # flattens all filters 17*17*128 =36,992  \n\n\n# fully connected layer\n# hidden layer\nmodel.add(Dense(128, activation = 'relu'))\n# model.add(Dropout(0.5))\n\n# Output layer\nmodel.add(Dense(1, activation = 'sigmoid')) # output layer 128*1 + 1=129  \n\n\n# summary\nmodel.summary()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-15T09:31:41.276348Z","iopub.execute_input":"2025-07-15T09:31:41.276534Z","iopub.status.idle":"2025-07-15T09:31:43.577233Z","shell.execute_reply.started":"2025-07-15T09:31:41.276518Z","shell.execute_reply":"2025-07-15T09:31:43.576727Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# compilation\nmodel.compile(loss = 'binary_crossentropy', \n                optimizer = 'rmsprop', \n                metrics = ['accuracy'])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-15T09:31:43.577914Z","iopub.execute_input":"2025-07-15T09:31:43.578178Z","iopub.status.idle":"2025-07-15T09:31:43.59105Z","shell.execute_reply.started":"2025-07-15T09:31:43.578154Z","shell.execute_reply":"2025-07-15T09:31:43.590463Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"history = model.fit(train_gen, \n                    validation_data = validation_gen, \n                    epochs = 5)\nhistory","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-15T09:31:43.591833Z","iopub.execute_input":"2025-07-15T09:31:43.592053Z","iopub.status.idle":"2025-07-15T09:42:48.773032Z","shell.execute_reply.started":"2025-07-15T09:31:43.59203Z","shell.execute_reply":"2025-07-15T09:42:48.772167Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Visualising accuracy \nplt.plot(history.history['accuracy'], label = 'Train accuracy')\nplt.plot(history.history['val_accuracy'], label = 'Validation accuracy')\nplt.xlabel(\"Epoch\")\nplt.ylabel(\"accuracy\")\nplt.legend()\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-15T09:42:48.773999Z","iopub.execute_input":"2025-07-15T09:42:48.774287Z","iopub.status.idle":"2025-07-15T09:42:48.948297Z","shell.execute_reply.started":"2025-07-15T09:42:48.77426Z","shell.execute_reply":"2025-07-15T09:42:48.947564Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Lets see loss visually\nplt.plot(history.history['loss'], label = 'training loss')\nplt.plot(history.history['val_loss'], label = 'val loss')\nplt.xlabel(\"Epoch\")\nplt.ylabel(\"Loss\")\nplt.legend()\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-15T09:42:48.948996Z","iopub.execute_input":"2025-07-15T09:42:48.949206Z","iopub.status.idle":"2025-07-15T09:42:49.092282Z","shell.execute_reply.started":"2025-07-15T09:42:48.949186Z","shell.execute_reply":"2025-07-15T09:42:49.091526Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Feature extraction","metadata":{}},{"cell_type":"code","source":"from keras.applications.vgg16 import VGG16","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-15T09:42:49.093065Z","iopub.execute_input":"2025-07-15T09:42:49.093253Z","iopub.status.idle":"2025-07-15T09:42:49.098049Z","shell.execute_reply.started":"2025-07-15T09:42:49.093238Z","shell.execute_reply":"2025-07-15T09:42:49.097307Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"conv_base = VGG16(\n    weights = 'imagenet',\n    include_top = False,     # means dense layer\n    input_shape = (150,150,3)\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-15T09:42:49.098998Z","iopub.execute_input":"2025-07-15T09:42:49.099261Z","iopub.status.idle":"2025-07-15T09:42:51.634391Z","shell.execute_reply.started":"2025-07-15T09:42:49.099238Z","shell.execute_reply":"2025-07-15T09:42:51.633828Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"conv_base.summary()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-15T09:42:51.635203Z","iopub.execute_input":"2025-07-15T09:42:51.635424Z","iopub.status.idle":"2025-07-15T09:42:51.658006Z","shell.execute_reply.started":"2025-07-15T09:42:51.635407Z","shell.execute_reply":"2025-07-15T09:42:51.657451Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from tensorflow.keras import Sequential\nfrom tensorflow.keras.layers import Conv2D,MaxPooling2D,Flatten, Dense,Dropout\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-15T09:42:51.658741Z","iopub.execute_input":"2025-07-15T09:42:51.658982Z","iopub.status.idle":"2025-07-15T09:42:51.662589Z","shell.execute_reply.started":"2025-07-15T09:42:51.658961Z","shell.execute_reply":"2025-07-15T09:42:51.661832Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\nmodel = Sequential()\nmodel.add(conv_base)\nmodel.add(Flatten())\nmodel.add(Dense(256, activation = 'relu'))\nmodel.add(Dense(1, activation = 'sigmoid'))\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-15T09:42:51.663434Z","iopub.execute_input":"2025-07-15T09:42:51.663688Z","iopub.status.idle":"2025-07-15T09:42:51.707881Z","shell.execute_reply.started":"2025-07-15T09:42:51.663663Z","shell.execute_reply":"2025-07-15T09:42:51.707323Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model.summary()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-15T09:42:51.708637Z","iopub.execute_input":"2025-07-15T09:42:51.708903Z","iopub.status.idle":"2025-07-15T09:42:51.72152Z","shell.execute_reply.started":"2025-07-15T09:42:51.708882Z","shell.execute_reply":"2025-07-15T09:42:51.720988Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"conv_base.trainable = False","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-15T09:42:51.722173Z","iopub.execute_input":"2025-07-15T09:42:51.722419Z","iopub.status.idle":"2025-07-15T09:42:51.731205Z","shell.execute_reply.started":"2025-07-15T09:42:51.722395Z","shell.execute_reply":"2025-07-15T09:42:51.730656Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model.compile(optimizer = 'adam', \n             loss = 'binary_crossentropy',\n             metrics = ['accuracy'])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-15T09:42:51.73406Z","iopub.execute_input":"2025-07-15T09:42:51.734243Z","iopub.status.idle":"2025-07-15T09:42:51.756064Z","shell.execute_reply.started":"2025-07-15T09:42:51.734229Z","shell.execute_reply":"2025-07-15T09:42:51.755491Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"history = model.fit(train_gen, \n                    validation_data = validation_gen, \n                    epochs = 10)\nhistory","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-15T09:47:01.958881Z","iopub.execute_input":"2025-07-15T09:47:01.959482Z","iopub.status.idle":"2025-07-15T10:09:48.415166Z","shell.execute_reply.started":"2025-07-15T09:47:01.959457Z","shell.execute_reply":"2025-07-15T10:09:48.414402Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nplt.plot(history.history['accuracy'], label ='Train accuracy')\nplt.plot(history.history['val_accuracy'], label = 'val accuracy')\nplt.legend()\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-15T10:09:48.416452Z","iopub.execute_input":"2025-07-15T10:09:48.4167Z","iopub.status.idle":"2025-07-15T10:09:48.545684Z","shell.execute_reply.started":"2025-07-15T10:09:48.416684Z","shell.execute_reply":"2025-07-15T10:09:48.544861Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.plot(history.history['loss'], label = 'train loss')\nplt.plot(history.history['val_loss'], label = 'val loss')\nplt.legend()\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-15T10:09:48.546246Z","iopub.execute_input":"2025-07-15T10:09:48.546414Z","iopub.status.idle":"2025-07-15T10:09:48.699119Z","shell.execute_reply.started":"2025-07-15T10:09:48.546401Z","shell.execute_reply":"2025-07-15T10:09:48.698512Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Fine tuning","metadata":{}},{"cell_type":"code","source":"conv_base.trainable = True\nset_trainable = False\nfor layer in conv_base.layers:\n    if layer.name == 'block5_conv1':\n        set_trainable = True\n    if set_trainable:\n        layer.trainable = True\n    else:\n        layer.trainable = False\nfor layer in conv_base.layers:\n    print(layer.name, layer.trainable)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-15T10:09:48.700656Z","iopub.execute_input":"2025-07-15T10:09:48.700878Z","iopub.status.idle":"2025-07-15T10:09:48.705989Z","shell.execute_reply.started":"2025-07-15T10:09:48.700862Z","shell.execute_reply":"2025-07-15T10:09:48.705441Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model = Sequential()\nmodel.add(conv_base)\nmodel.add(Flatten())\nmodel.add(Dense(256, activation = 'relu'))\nmodel.add(Dense(1, activation = 'sigmoid'))\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-15T10:09:48.706633Z","iopub.execute_input":"2025-07-15T10:09:48.706851Z","iopub.status.idle":"2025-07-15T10:09:48.752011Z","shell.execute_reply.started":"2025-07-15T10:09:48.706828Z","shell.execute_reply":"2025-07-15T10:09:48.751281Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import tensorflow\nfrom tensorflow import keras\nmodel.compile(optimizer = keras.optimizers.RMSprop(learning_rate=1e-5),\n    loss = 'binary_crossentropy',\n    metrics = ['accuracy'])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-15T10:09:48.752954Z","iopub.execute_input":"2025-07-15T10:09:48.75319Z","iopub.status.idle":"2025-07-15T10:09:48.76226Z","shell.execute_reply.started":"2025-07-15T10:09:48.753167Z","shell.execute_reply":"2025-07-15T10:09:48.761408Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"history = model.fit(train_gen, epochs =10, validation_data = validation_gen)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-15T10:09:48.763293Z","iopub.execute_input":"2025-07-15T10:09:48.763552Z","iopub.status.idle":"2025-07-15T10:32:41.822412Z","shell.execute_reply.started":"2025-07-15T10:09:48.763529Z","shell.execute_reply":"2025-07-15T10:32:41.821856Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.plot(history.history['accuracy'], label = 'train accuracy', color = 'red')\nplt.plot(history.history['accuracy'], label='val accuracy', color = 'yellow')\nplt.legend()\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-15T10:32:41.823395Z","iopub.execute_input":"2025-07-15T10:32:41.823684Z","iopub.status.idle":"2025-07-15T10:32:41.956539Z","shell.execute_reply.started":"2025-07-15T10:32:41.823655Z","shell.execute_reply":"2025-07-15T10:32:41.95584Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.plot(history.history['loss'], label = 'train loss')\nplt.plot(history.history['val_loss'], label = 'val loss')\nplt.legend()\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-15T10:32:41.957285Z","iopub.execute_input":"2025-07-15T10:32:41.957507Z","iopub.status.idle":"2025-07-15T10:32:42.098339Z","shell.execute_reply.started":"2025-07-15T10:32:41.957489Z","shell.execute_reply":"2025-07-15T10:32:42.097654Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Model Evaluation on test data","metadata":{}},{"cell_type":"code","source":"test_gen = datagen.flow_from_dataframe(\n    test_df,\n    directory = test_dir,\n    x_col = 'Filename1',\n    y_col = None,\n    class_mode = None,\n    shuffle = False,\n    target_size = (150,150),\n    batch_size= 32\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-15T10:32:42.100023Z","iopub.execute_input":"2025-07-15T10:32:42.100227Z","iopub.status.idle":"2025-07-15T10:32:42.185805Z","shell.execute_reply.started":"2025-07-15T10:32:42.100212Z","shell.execute_reply":"2025-07-15T10:32:42.184883Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pred = model.predict(test_gen, verbose = 1)\npred","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-15T10:32:42.186798Z","iopub.execute_input":"2025-07-15T10:32:42.187023Z","iopub.status.idle":"2025-07-15T10:33:52.811019Z","shell.execute_reply.started":"2025-07-15T10:32:42.186996Z","shell.execute_reply":"2025-07-15T10:33:52.810364Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# convert probabilities to labels\npred_label = (pred>0.5).astype(int).flatten()\npred_label[:10]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-15T10:33:52.811785Z","iopub.execute_input":"2025-07-15T10:33:52.812055Z","iopub.status.idle":"2025-07-15T10:33:52.817358Z","shell.execute_reply.started":"2025-07-15T10:33:52.812032Z","shell.execute_reply":"2025-07-15T10:33:52.816826Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Save predcitions\ntest_df['Category'] = pred_label\ntest_df","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-15T10:33:52.818094Z","iopub.execute_input":"2025-07-15T10:33:52.818327Z","iopub.status.idle":"2025-07-15T10:33:52.841451Z","shell.execute_reply.started":"2025-07-15T10:33:52.818302Z","shell.execute_reply":"2025-07-15T10:33:52.840927Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_df.to_csv(\"submission.csv\", index = False)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-15T10:33:52.84222Z","iopub.execute_input":"2025-07-15T10:33:52.842451Z","iopub.status.idle":"2025-07-15T10:33:52.874278Z","shell.execute_reply.started":"2025-07-15T10:33:52.842435Z","shell.execute_reply":"2025-07-15T10:33:52.873761Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Check submission file\nsub = pd.read_csv(\"submission.csv\")\nsub.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-15T10:33:52.874977Z","iopub.execute_input":"2025-07-15T10:33:52.875197Z","iopub.status.idle":"2025-07-15T10:33:52.893584Z","shell.execute_reply.started":"2025-07-15T10:33:52.875175Z","shell.execute_reply":"2025-07-15T10:33:52.893053Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"n = 8  # number of images\nplt.figure(figsize=(15, 10))\nfor i in range(n):\n    img_name = test_df.iloc[i]['Filename1']\n    pred = pred_label[i]\n    label = 'Dog' if pred == 1 else 'Cat'\n\n    img_path = os.path.join(test_dir, img_name)\n    img= image.load_img(img_path, target_size = (150,150))\n    plt.subplot(2, 4, i+1)\n    plt.imshow(img)\n    plt.title(f\"Predicted {label}\")\nplt.tight_layout()\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-15T10:33:52.894453Z","iopub.execute_input":"2025-07-15T10:33:52.895002Z","iopub.status.idle":"2025-07-15T10:33:54.483091Z","shell.execute_reply.started":"2025-07-15T10:33:52.894976Z","shell.execute_reply":"2025-07-15T10:33:54.482122Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}