{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"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\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","execution":{"iopub.status.busy":"2023-09-07T16:51:09.451677Z","iopub.execute_input":"2023-09-07T16:51:09.452668Z","iopub.status.idle":"2023-09-07T16:51:09.463343Z","shell.execute_reply.started":"2023-09-07T16:51:09.452632Z","shell.execute_reply":"2023-09-07T16:51:09.462229Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Cats vs Dogs Image Classification using Transfer Learning Model","metadata":{}},{"cell_type":"markdown","source":"### Loading Necessary Libraries","metadata":{}},{"cell_type":"code","source":"import numpy as np\nimport seaborn as sns\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport tensorflow as tf\nimport os\nimport zipfile","metadata":{"execution":{"iopub.status.busy":"2023-09-07T16:51:10.143982Z","iopub.execute_input":"2023-09-07T16:51:10.144995Z","iopub.status.idle":"2023-09-07T16:51:10.152957Z","shell.execute_reply.started":"2023-09-07T16:51:10.144956Z","shell.execute_reply":"2023-09-07T16:51:10.151801Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"folders = os.listdir('/kaggle/input')[0]","metadata":{"execution":{"iopub.status.busy":"2023-09-07T16:51:10.343138Z","iopub.execute_input":"2023-09-07T16:51:10.343507Z","iopub.status.idle":"2023-09-07T16:51:10.348841Z","shell.execute_reply.started":"2023-09-07T16:51:10.343477Z","shell.execute_reply":"2023-09-07T16:51:10.347654Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"folders","metadata":{"execution":{"iopub.status.busy":"2023-09-07T16:51:11.783952Z","iopub.execute_input":"2023-09-07T16:51:11.784966Z","iopub.status.idle":"2023-09-07T16:51:11.791556Z","shell.execute_reply.started":"2023-09-07T16:51:11.784933Z","shell.execute_reply":"2023-09-07T16:51:11.790409Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"folders = os.listdir('/kaggle/input/dogs-vs-cats-redux-kernels-edition/')","metadata":{"execution":{"iopub.status.busy":"2023-09-07T16:51:11.793844Z","iopub.execute_input":"2023-09-07T16:51:11.794420Z","iopub.status.idle":"2023-09-07T16:51:11.801680Z","shell.execute_reply.started":"2023-09-07T16:51:11.794367Z","shell.execute_reply":"2023-09-07T16:51:11.800735Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"folders","metadata":{"execution":{"iopub.status.busy":"2023-09-07T16:51:13.700740Z","iopub.execute_input":"2023-09-07T16:51:13.701424Z","iopub.status.idle":"2023-09-07T16:51:13.708411Z","shell.execute_reply.started":"2023-09-07T16:51:13.701384Z","shell.execute_reply":"2023-09-07T16:51:13.707327Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import re\n\nfolder_to_unzip = [folder for folder in folders if not re.search('.csv', folder)]","metadata":{"execution":{"iopub.status.busy":"2023-09-07T16:51:13.911793Z","iopub.execute_input":"2023-09-07T16:51:13.912160Z","iopub.status.idle":"2023-09-07T16:51:13.917221Z","shell.execute_reply.started":"2023-09-07T16:51:13.912130Z","shell.execute_reply":"2023-09-07T16:51:13.916120Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"folder_to_unzip","metadata":{"execution":{"iopub.status.busy":"2023-09-07T16:51:14.142666Z","iopub.execute_input":"2023-09-07T16:51:14.143331Z","iopub.status.idle":"2023-09-07T16:51:14.149355Z","shell.execute_reply.started":"2023-09-07T16:51:14.143299Z","shell.execute_reply":"2023-09-07T16:51:14.148356Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"base_dir = \"/kaggle/input/dogs-vs-cats-redux-kernels-edition/\"","metadata":{"execution":{"iopub.status.busy":"2023-09-07T16:51:14.353574Z","iopub.execute_input":"2023-09-07T16:51:14.356020Z","iopub.status.idle":"2023-09-07T16:51:14.360520Z","shell.execute_reply.started":"2023-09-07T16:51:14.355987Z","shell.execute_reply":"2023-09-07T16:51:14.359408Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for folder in folder_to_unzip:\n    with zipfile.ZipFile(os.path.join(base_dir, folder), 'r') as file:\n        file.extractall('.')","metadata":{"execution":{"iopub.status.busy":"2023-09-07T16:51:14.551954Z","iopub.execute_input":"2023-09-07T16:51:14.553595Z","iopub.status.idle":"2023-09-07T16:51:32.260786Z","shell.execute_reply.started":"2023-09-07T16:51:14.553557Z","shell.execute_reply":"2023-09-07T16:51:32.259744Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"base_dir_working = '/kaggle/working'\n\nfolders_in_working_dir = os.listdir(base_dir_working) ","metadata":{"execution":{"iopub.status.busy":"2023-09-07T16:51:32.264551Z","iopub.execute_input":"2023-09-07T16:51:32.264839Z","iopub.status.idle":"2023-09-07T16:51:32.271665Z","shell.execute_reply.started":"2023-09-07T16:51:32.264815Z","shell.execute_reply":"2023-09-07T16:51:32.270761Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"folders_in_working_dir","metadata":{"execution":{"iopub.status.busy":"2023-09-07T16:51:32.274783Z","iopub.execute_input":"2023-09-07T16:51:32.275609Z","iopub.status.idle":"2023-09-07T16:51:32.284424Z","shell.execute_reply.started":"2023-09-07T16:51:32.275576Z","shell.execute_reply":"2023-09-07T16:51:32.283453Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dir = f\"{base_dir_working}/train\"\ntest_dir = f\"{base_dir_working}/test\"","metadata":{"execution":{"iopub.status.busy":"2023-09-07T16:51:32.286962Z","iopub.execute_input":"2023-09-07T16:51:32.287347Z","iopub.status.idle":"2023-09-07T16:51:32.294719Z","shell.execute_reply.started":"2023-09-07T16:51:32.287316Z","shell.execute_reply":"2023-09-07T16:51:32.293793Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(f\"The train set has {len(os.listdir(train_dir))} number of image files\")","metadata":{"execution":{"iopub.status.busy":"2023-09-07T16:51:32.296317Z","iopub.execute_input":"2023-09-07T16:51:32.296731Z","iopub.status.idle":"2023-09-07T16:51:32.320111Z","shell.execute_reply.started":"2023-09-07T16:51:32.296699Z","shell.execute_reply":"2023-09-07T16:51:32.319063Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(f\"The test set has {len(os.listdir(test_dir))} number of image files\")","metadata":{"execution":{"iopub.status.busy":"2023-09-07T16:51:32.321424Z","iopub.execute_input":"2023-09-07T16:51:32.324500Z","iopub.status.idle":"2023-09-07T16:51:32.337888Z","shell.execute_reply.started":"2023-09-07T16:51:32.324474Z","shell.execute_reply":"2023-09-07T16:51:32.337020Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Data Exploration","metadata":{}},{"cell_type":"code","source":"import random\nimport pandas as pd\nfrom PIL import Image\nimport cv2\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import classification_report","metadata":{"execution":{"iopub.status.busy":"2023-09-07T16:51:32.338959Z","iopub.execute_input":"2023-09-07T16:51:32.339232Z","iopub.status.idle":"2023-09-07T16:51:32.344235Z","shell.execute_reply.started":"2023-09-07T16:51:32.339209Z","shell.execute_reply":"2023-09-07T16:51:32.343266Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Creating a DataFrame containing each file image and their respective labels","metadata":{}},{"cell_type":"code","source":"filenames = os.listdir(train_dir)\nlabels = [file.split(\".\")[0] for file in filenames]\ndf = pd.DataFrame({\"filename\": filenames, \"label\": labels})","metadata":{"execution":{"iopub.status.busy":"2023-09-07T16:51:32.345555Z","iopub.execute_input":"2023-09-07T16:51:32.346117Z","iopub.status.idle":"2023-09-07T16:51:32.383442Z","shell.execute_reply.started":"2023-09-07T16:51:32.346069Z","shell.execute_reply":"2023-09-07T16:51:32.382570Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head()","metadata":{"execution":{"iopub.status.busy":"2023-09-07T16:51:32.386386Z","iopub.execute_input":"2023-09-07T16:51:32.386648Z","iopub.status.idle":"2023-09-07T16:51:32.398325Z","shell.execute_reply.started":"2023-09-07T16:51:32.386626Z","shell.execute_reply":"2023-09-07T16:51:32.397133Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# check if the Dataset is balanced \n\ndf.label.value_counts()","metadata":{"execution":{"iopub.status.busy":"2023-09-07T16:51:32.402677Z","iopub.execute_input":"2023-09-07T16:51:32.402931Z","iopub.status.idle":"2023-09-07T16:51:32.414473Z","shell.execute_reply.started":"2023-09-07T16:51:32.402909Z","shell.execute_reply":"2023-09-07T16:51:32.413314Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"categories = df.groupby(\"label\")\nnum_images_from_each_categories = 5\n\nfig, axes = plt.subplots(len(categories), num_images_from_each_categories, figsize=(15, 15))\nfor i, (category, group) in enumerate(categories):\n    random_selection = random.sample(range(len(group)), num_images_from_each_categories)\n    for j, index in enumerate(random_selection):\n        filename = group.iloc[index][\"filename\"]\n        label = group.iloc[index]['label']\n        image_path = os.path.join(train_dir, filename)\n        image = Image.open(image_path)\n\n        axes[i, j].imshow(image)\n        axes[i, j].set_title(\"Label: \" + label, fontsize = 30)\n\n\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-09-07T16:51:32.416189Z","iopub.execute_input":"2023-09-07T16:51:32.416564Z","iopub.status.idle":"2023-09-07T16:51:36.256659Z","shell.execute_reply.started":"2023-09-07T16:51:32.416530Z","shell.execute_reply":"2023-09-07T16:51:36.255726Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# From the axis of the images we could see that the images have varying size(height, width)\n# So checking the height, width on average for all images\n\nimage_height = []\nimage_width = []\n\nimage_paths = [os.path.join(train_dir, file) for file in os.listdir(train_dir)]\n\nfor image_path in image_paths:\n    image = cv2.imread(image_path)\n    height, width, _ = image.shape\n    \n    image_height.append(height)\n    image_width.append(width)\n    \nheight = np.median(image_height)\nwidth = np.median(image_width)\n\nprint(height, width)","metadata":{"execution":{"iopub.status.busy":"2023-09-07T16:51:36.258058Z","iopub.execute_input":"2023-09-07T16:51:36.258696Z","iopub.status.idle":"2023-09-07T16:52:20.053752Z","shell.execute_reply.started":"2023-09-07T16:51:36.258662Z","shell.execute_reply":"2023-09-07T16:52:20.052601Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# So I will be using the image size as (370, 370)","metadata":{"execution":{"iopub.status.busy":"2023-09-07T16:52:20.055167Z","iopub.execute_input":"2023-09-07T16:52:20.055613Z","iopub.status.idle":"2023-09-07T16:52:20.060443Z","shell.execute_reply.started":"2023-09-07T16:52:20.055578Z","shell.execute_reply":"2023-09-07T16:52:20.059259Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Create train, validation and test datasets for Modeling","metadata":{}},{"cell_type":"code","source":"df.head(10)","metadata":{"execution":{"iopub.status.busy":"2023-09-07T16:52:20.062137Z","iopub.execute_input":"2023-09-07T16:52:20.062479Z","iopub.status.idle":"2023-09-07T16:52:20.078009Z","shell.execute_reply.started":"2023-09-07T16:52:20.062447Z","shell.execute_reply":"2023-09-07T16:52:20.077097Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels = df['label']\nX_train, X_temp = train_test_split(df, test_size=0.2, stratify=labels, random_state = 25)\n\nlabel_test_val = X_temp['label']\nX_test, X_val = train_test_split(X_temp, test_size=0.5, stratify=label_test_val, random_state = 25)\n\nprint ('X_train:', X_train['label'].value_counts())\n\nprint ('X_val:', X_val['label'].value_counts())\n\nprint ('X_test:', X_test['label'].value_counts())","metadata":{"execution":{"iopub.status.busy":"2023-09-07T16:52:20.079610Z","iopub.execute_input":"2023-09-07T16:52:20.079950Z","iopub.status.idle":"2023-09-07T16:52:20.148636Z","shell.execute_reply.started":"2023-09-07T16:52:20.079919Z","shell.execute_reply":"2023-09-07T16:52:20.147612Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train.shape","metadata":{"execution":{"iopub.status.busy":"2023-09-07T16:52:20.152957Z","iopub.execute_input":"2023-09-07T16:52:20.153250Z","iopub.status.idle":"2023-09-07T16:52:20.161699Z","shell.execute_reply.started":"2023-09-07T16:52:20.153225Z","shell.execute_reply":"2023-09-07T16:52:20.160677Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_val.shape","metadata":{"execution":{"iopub.status.busy":"2023-09-07T16:52:20.162779Z","iopub.execute_input":"2023-09-07T16:52:20.163169Z","iopub.status.idle":"2023-09-07T16:52:20.171812Z","shell.execute_reply.started":"2023-09-07T16:52:20.163137Z","shell.execute_reply":"2023-09-07T16:52:20.170579Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_test.shape","metadata":{"execution":{"iopub.status.busy":"2023-09-07T16:52:20.173037Z","iopub.execute_input":"2023-09-07T16:52:20.174204Z","iopub.status.idle":"2023-09-07T16:52:20.184419Z","shell.execute_reply.started":"2023-09-07T16:52:20.174139Z","shell.execute_reply":"2023-09-07T16:52:20.183369Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train.head()","metadata":{"execution":{"iopub.status.busy":"2023-09-07T16:52:20.185731Z","iopub.execute_input":"2023-09-07T16:52:20.186484Z","iopub.status.idle":"2023-09-07T16:52:20.199520Z","shell.execute_reply.started":"2023-09-07T16:52:20.186453Z","shell.execute_reply":"2023-09-07T16:52:20.198397Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"From the code and output above all three splits of data has both categories in the same proportion (equally distributed) as that in the original data. ","metadata":{}},{"cell_type":"markdown","source":"## Model Development \n- **VVG16**\n\n- **ResNet50**","metadata":{}},{"cell_type":"code","source":"from tensorflow.keras.layers import Input, Flatten, Dense, Dropout, GlobalAveragePooling2D, Conv2D, MaxPooling2D\nfrom tensorflow.keras.applications import ResNet50, InceptionV3\nfrom tensorflow.keras.models import Model\nfrom tensorflow.keras.optimizers import Adam, RMSprop\nfrom sklearn.model_selection import KFold\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator","metadata":{"execution":{"iopub.status.busy":"2023-09-07T16:52:20.200892Z","iopub.execute_input":"2023-09-07T16:52:20.201922Z","iopub.status.idle":"2023-09-07T16:52:20.208647Z","shell.execute_reply.started":"2023-09-07T16:52:20.201886Z","shell.execute_reply":"2023-09-07T16:52:20.207894Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Setting the size of the image and the batch size\nbatch_size = 64\nsize = (370, 370)\n\ndatagen = tf.keras.preprocessing.image.ImageDataGenerator(\n                                 preprocessing_function = tf.keras.applications.vgg16.preprocess_input)\n\ntrain_generator = datagen.flow_from_dataframe(X_train, \"train/\", x_col= \"filename\", y_col= \"label\",\n                                    batch_size = batch_size,\n                                    target_size=size,\n                                    class_mode='categorical')\n\nvalidation_generator = datagen.flow_from_dataframe(X_val, \"train/\", x_col=\"filename\", y_col=\"label\",\n                                  batch_size = batch_size,\n                                  target_size = size,\n                                  class_mode='categorical',\n                                  shuffle = False)\n\ntest_generator = datagen.flow_from_dataframe(X_test, \"train/\", x_col= \"filename\", y_col= \"label\",\n                                    batch_size = batch_size,\n                                    target_size=size, shuffle = False)","metadata":{"execution":{"iopub.status.busy":"2023-09-07T14:32:27.561057Z","iopub.execute_input":"2023-09-07T14:32:27.561575Z","iopub.status.idle":"2023-09-07T14:32:27.853350Z","shell.execute_reply.started":"2023-09-07T14:32:27.561543Z","shell.execute_reply":"2023-09-07T14:32:27.852447Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Download vgg16 model but ignoring the weights","metadata":{}},{"cell_type":"code","source":"vgg16_model = tf.keras.applications.vgg16.VGG16(include_top=False, input_shape=(370, 370, 3))","metadata":{"execution":{"iopub.status.busy":"2023-09-07T14:32:27.854689Z","iopub.execute_input":"2023-09-07T14:32:27.855020Z","iopub.status.idle":"2023-09-07T14:32:33.065816Z","shell.execute_reply.started":"2023-09-07T14:32:27.854990Z","shell.execute_reply":"2023-09-07T14:32:33.064876Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"vgg16_model.summary()","metadata":{"execution":{"iopub.status.busy":"2023-09-07T14:32:33.067450Z","iopub.execute_input":"2023-09-07T14:32:33.067780Z","iopub.status.idle":"2023-09-07T14:32:33.108199Z","shell.execute_reply.started":"2023-09-07T14:32:33.067751Z","shell.execute_reply":"2023-09-07T14:32:33.107488Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for layer in vgg16_model.layers:\n  layer.trainable = False","metadata":{"execution":{"iopub.status.busy":"2023-09-07T14:32:33.109119Z","iopub.execute_input":"2023-09-07T14:32:33.109423Z","iopub.status.idle":"2023-09-07T14:32:33.114679Z","shell.execute_reply.started":"2023-09-07T14:32:33.109392Z","shell.execute_reply":"2023-09-07T14:32:33.113975Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x = vgg16_model.output\nx = tf.keras.layers.Flatten() (x)\nx = tf.keras.layers.Dropout(0.5, name=\"Dropout1\") (x)\nx = tf.keras.layers.Dense(256, activation=\"relu\") (x)\nx = tf.keras.layers.Dropout(0.5, name=\"Dropout2\")(x)\noutput = tf.keras.layers.Dense(2, activation=\"softmax\") (x)\n\nfinal_model = tf.keras.models.Model(inputs=vgg16_model.input, outputs=output)","metadata":{"execution":{"iopub.status.busy":"2023-09-07T14:32:33.115774Z","iopub.execute_input":"2023-09-07T14:32:33.116109Z","iopub.status.idle":"2023-09-07T14:32:33.187225Z","shell.execute_reply.started":"2023-09-07T14:32:33.116063Z","shell.execute_reply":"2023-09-07T14:32:33.186391Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Plot the final model","metadata":{}},{"cell_type":"code","source":"tf.keras.utils.plot_model(final_model, show_shapes = True, show_layer_names=True)","metadata":{"execution":{"iopub.status.busy":"2023-09-07T14:32:33.188558Z","iopub.execute_input":"2023-09-07T14:32:33.188891Z","iopub.status.idle":"2023-09-07T14:32:33.727761Z","shell.execute_reply.started":"2023-09-07T14:32:33.188862Z","shell.execute_reply":"2023-09-07T14:32:33.726947Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"final_model.compile(optimizer='adam',\n                    loss=tf.keras.losses.categorical_crossentropy,\n                    metrics = [\"acc\"])","metadata":{"execution":{"iopub.status.busy":"2023-09-07T14:32:33.729325Z","iopub.execute_input":"2023-09-07T14:32:33.729942Z","iopub.status.idle":"2023-09-07T14:32:33.749451Z","shell.execute_reply.started":"2023-09-07T14:32:33.729912Z","shell.execute_reply":"2023-09-07T14:32:33.748579Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Define callback for saving the best model weights\ncheckpoint = tf.keras.callbacks.ModelCheckpoint(\"vgg16_1.h5\",\n                                                monitor=\"val_loss\",\n                                                save_best_only=True)\n\n# Define callback for early stopping\nearly = tf.keras.callbacks.EarlyStopping(\n    monitor='val_accuracy',\n    min_delta=0,\n    patience=20,\n    verbose=1,\n    mode='auto'\n)","metadata":{"execution":{"iopub.status.busy":"2023-09-07T14:32:33.750851Z","iopub.execute_input":"2023-09-07T14:32:33.751198Z","iopub.status.idle":"2023-09-07T14:32:33.757046Z","shell.execute_reply.started":"2023-09-07T14:32:33.751167Z","shell.execute_reply":"2023-09-07T14:32:33.755819Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"The ModelCheckpoint callback will save the best model during training based on the validation loss. \n\nThe saved model will be stored with the prefix \"vgg16_1.h5\".","metadata":{}},{"cell_type":"markdown","source":"# Train the model","metadata":{}},{"cell_type":"code","source":"history = final_model.fit(train_generator, \n                          batch_size=batch_size, \n                          validation_data=validation_generator, \n                          epochs = 10, callbacks=[checkpoint, early])","metadata":{"execution":{"iopub.status.busy":"2023-09-07T14:32:33.758720Z","iopub.execute_input":"2023-09-07T14:32:33.759057Z","iopub.status.idle":"2023-09-07T15:21:55.848007Z","shell.execute_reply.started":"2023-09-07T14:32:33.759025Z","shell.execute_reply":"2023-09-07T15:21:55.847128Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Evaluate accuracy and loss of the model","metadata":{}},{"cell_type":"code","source":"acc = history.history['acc']\nval_acc = history.history['val_acc']\n\nloss = history.history['loss']\nval_loss = history.history['val_loss']\n\nepochs_range = range(10)\n\nplt.figure(figsize=(6, 5))\nplt.subplot(1, 2, 1)\nplt.plot(epochs_range, acc, label='Training Accuracy')\nplt.plot(epochs_range, val_acc, label = 'Validation Accuracy')\nplt.legend(loc='lower right')\nplt.title('Training and Validation Accuracy')\n\nplt.subplot(1, 2, 2)\nplt.plot(epochs_range, loss, label='Training Loss')\nplt.plot(epochs_range, val_loss, label='Validation Loss')\nplt.legend(loc='upper right')\nplt.title('Training and Validation Loss')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-09-07T15:21:55.849425Z","iopub.execute_input":"2023-09-07T15:21:55.849775Z","iopub.status.idle":"2023-09-07T15:21:56.393214Z","shell.execute_reply.started":"2023-09-07T15:21:55.849742Z","shell.execute_reply":"2023-09-07T15:21:56.392146Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Load the best model based on validation loss\nbest_model = tf.keras.models.load_model(\"vgg16_1.h5\")\n\n# Evaluate the best model on the test data\ntest_loss, test_accuracy = best_model.evaluate(test_generator, verbose=1)\nprint(f\"Test accuracy: {test_accuracy}\")","metadata":{"execution":{"iopub.status.busy":"2023-09-07T15:21:56.394595Z","iopub.execute_input":"2023-09-07T15:21:56.395569Z","iopub.status.idle":"2023-09-07T15:22:39.181773Z","shell.execute_reply.started":"2023-09-07T15:21:56.395530Z","shell.execute_reply":"2023-09-07T15:22:39.180623Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"best_model.save(\"vvg16.h5\")","metadata":{"execution":{"iopub.status.busy":"2023-09-07T15:22:39.183389Z","iopub.execute_input":"2023-09-07T15:22:39.183747Z","iopub.status.idle":"2023-09-07T15:22:39.663832Z","shell.execute_reply.started":"2023-09-07T15:22:39.183716Z","shell.execute_reply":"2023-09-07T15:22:39.662847Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"output = best_model.predict(test_generator)\n\nresult_argmax = np.argmax(output, axis=1)\nresult_argmax[:10]","metadata":{"execution":{"iopub.status.busy":"2023-09-07T15:22:39.667099Z","iopub.execute_input":"2023-09-07T15:22:39.667529Z","iopub.status.idle":"2023-09-07T15:23:09.685003Z","shell.execute_reply.started":"2023-09-07T15:22:39.667496Z","shell.execute_reply":"2023-09-07T15:23:09.683995Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_true = test_generator.labels\n\ny_pred = result_argmax\n\naccuracy = (y_pred == y_true).mean()\n\nprint(\"Test Accuracy:\", accuracy)","metadata":{"execution":{"iopub.status.busy":"2023-09-07T15:23:09.686328Z","iopub.execute_input":"2023-09-07T15:23:09.686662Z","iopub.status.idle":"2023-09-07T15:23:09.694045Z","shell.execute_reply.started":"2023-09-07T15:23:09.686631Z","shell.execute_reply":"2023-09-07T15:23:09.693135Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**The VVG16 model did well having 98.08% accuracy**","metadata":{}},{"cell_type":"code","source":"print(classification_report(y_true, y_pred))","metadata":{"execution":{"iopub.status.busy":"2023-09-07T15:23:09.695535Z","iopub.execute_input":"2023-09-07T15:23:09.696216Z","iopub.status.idle":"2023-09-07T15:23:09.718316Z","shell.execute_reply.started":"2023-09-07T15:23:09.696180Z","shell.execute_reply":"2023-09-07T15:23:09.717470Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import confusion_matrix\n\nconfusion = confusion_matrix(y_pred, y_true)\n\nplt.figure(figsize=(8, 6))\nsns.heatmap(confusion, annot=True, fmt=\"d\", cmap=\"Blues\",\n            xticklabels=[\"Class 0\", \"Class 1\"],\n            yticklabels=[\"Class 0\", \"Class 1\"])\nplt.xlabel(\"Predicted\")\nplt.ylabel(\"Actual\")\nplt.title(\"Confusion Matrix\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-09-07T15:23:09.719647Z","iopub.execute_input":"2023-09-07T15:23:09.719989Z","iopub.status.idle":"2023-09-07T15:23:09.997873Z","shell.execute_reply.started":"2023-09-07T15:23:09.719958Z","shell.execute_reply":"2023-09-07T15:23:09.996963Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Make predictions on real test data for submission.","metadata":{}},{"cell_type":"code","source":"test_dir = f\"{base_dir_working}/test\"","metadata":{"execution":{"iopub.status.busy":"2023-09-07T15:23:09.999283Z","iopub.execute_input":"2023-09-07T15:23:10.000333Z","iopub.status.idle":"2023-09-07T15:23:10.004894Z","shell.execute_reply.started":"2023-09-07T15:23:10.000289Z","shell.execute_reply":"2023-09-07T15:23:10.003749Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"filenames = os.listdir(test_dir)\ntest_data = pd.DataFrame({\"filename\": filenames})\ntest_data['label'] = 'unknown'\ntest_data.head()\n\n\nreal_test_generator =  datagen.flow_from_dataframe(test_data, \"test/\", x_col= \"filename\",y_col = 'label',\n                                    batch_size = batch_size,\n                                    target_size=size, shuffle = False)","metadata":{"execution":{"iopub.status.busy":"2023-09-07T15:23:10.006248Z","iopub.execute_input":"2023-09-07T15:23:10.006791Z","iopub.status.idle":"2023-09-07T15:23:10.165119Z","shell.execute_reply.started":"2023-09-07T15:23:10.006759Z","shell.execute_reply":"2023-09-07T15:23:10.164224Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nreal_test_generator =  datagen.flow_from_dataframe(test_data, \"test/\", x_col= \"filename\",y_col = 'label',\n                                    batch_size = batch_size,\n                                    target_size=size, shuffle = False)","metadata":{"execution":{"iopub.status.busy":"2023-09-07T15:23:10.166584Z","iopub.execute_input":"2023-09-07T15:23:10.166936Z","iopub.status.idle":"2023-09-07T15:23:10.311336Z","shell.execute_reply.started":"2023-09-07T15:23:10.166904Z","shell.execute_reply":"2023-09-07T15:23:10.310390Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_predict = best_model.predict(real_test_generator)\n\ntest_predict_argmax = np.argmax(test_predict, axis=1)\n\ny_test_pred = test_predict_argmax\n\ntest_data['label'] = y_test_pred\n\ntest_data.head()","metadata":{"execution":{"iopub.status.busy":"2023-09-07T15:23:10.312693Z","iopub.execute_input":"2023-09-07T15:23:10.313141Z","iopub.status.idle":"2023-09-07T15:25:43.514752Z","shell.execute_reply.started":"2023-09-07T15:23:10.313108Z","shell.execute_reply":"2023-09-07T15:25:43.513760Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_generator.class_indices","metadata":{"execution":{"iopub.status.busy":"2023-09-07T15:25:43.516342Z","iopub.execute_input":"2023-09-07T15:25:43.516746Z","iopub.status.idle":"2023-09-07T15:25:43.524034Z","shell.execute_reply.started":"2023-09-07T15:25:43.516713Z","shell.execute_reply":"2023-09-07T15:25:43.523087Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"label_mapping = {0: 'cat', 1: 'dog'}\ntest_data['label'] = test_data['label'].map(label_mapping)\ntest_data.head()","metadata":{"execution":{"iopub.status.busy":"2023-09-07T15:25:43.525476Z","iopub.execute_input":"2023-09-07T15:25:43.526096Z","iopub.status.idle":"2023-09-07T15:25:43.541227Z","shell.execute_reply.started":"2023-09-07T15:25:43.526047Z","shell.execute_reply":"2023-09-07T15:25:43.540333Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Lets extend beyond 5 images \n\nfig, axes = plt.subplots(4, 5, figsize=(20, 15))  # 2 rows, 5 columns\n\nfor idx in range(20):  # Display 10 images\n    row = idx // 5  # Calculate the row index\n    col = idx % 5   # Calculate the column index\n\n    image_path = os.path.join(test_dir, test_data.iloc[idx]['filename'])\n    image = Image.open(image_path)\n    \n    axes[row, col].imshow(image)\n    axes[row, col].set_title(\"Label: \" + test_data.iloc[idx]['label'])\n    axes[row, col].axis('off')\n\nplt.subplots_adjust(wspace=0.3, hspace=0.3)  # Add spacing between subplots\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-09-07T15:25:43.542676Z","iopub.execute_input":"2023-09-07T15:25:43.543567Z","iopub.status.idle":"2023-09-07T15:25:45.713354Z","shell.execute_reply.started":"2023-09-07T15:25:43.543536Z","shell.execute_reply":"2023-09-07T15:25:45.711162Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Does quite well with the predictions","metadata":{}},{"cell_type":"markdown","source":"# Downloading ResNet50 model but ignoring the weights","metadata":{}},{"cell_type":"code","source":"# Setting the size of the image and the batch size\nbatch_size = 64\nsize = (370, 370)","metadata":{"execution":{"iopub.status.busy":"2023-09-07T16:52:20.209827Z","iopub.execute_input":"2023-09-07T16:52:20.210750Z","iopub.status.idle":"2023-09-07T16:52:20.218847Z","shell.execute_reply.started":"2023-09-07T16:52:20.210717Z","shell.execute_reply":"2023-09-07T16:52:20.217632Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"datagen = tf.keras.preprocessing.image.ImageDataGenerator(\n    preprocessing_function=tf.keras.applications.resnet50.preprocess_input\n)\n\ntrain_generator = datagen.flow_from_dataframe(X_train, \"train/\", x_col= \"filename\", y_col= \"label\",\n                                    batch_size = batch_size,\n                                    target_size=size)\n\nvalidation_generator = datagen.flow_from_dataframe(X_val, \"train/\", x_col=\"filename\", y_col=\"label\",\n                                  batch_size = batch_size,\n                                  target_size = size,\n                                  shuffle = False)\n\ntest_generator = datagen.flow_from_dataframe(X_test, \"train/\", x_col= \"filename\", y_col= \"label\",\n                                    batch_size = batch_size,\n                                    target_size=size, shuffle = False)","metadata":{"execution":{"iopub.status.busy":"2023-09-07T16:52:20.221610Z","iopub.execute_input":"2023-09-07T16:52:20.222000Z","iopub.status.idle":"2023-09-07T16:52:20.520089Z","shell.execute_reply.started":"2023-09-07T16:52:20.221966Z","shell.execute_reply":"2023-09-07T16:52:20.519010Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.layers import Input\n\n# Define the model architecture (same as before)\nresnet50_model = tf.keras.applications.ResNet50(weights='imagenet', include_top=False, input_tensor=Input(shape=(370, 370, 3)))\n\nresnet50_model.summary()","metadata":{"execution":{"iopub.status.busy":"2023-09-07T16:52:20.521631Z","iopub.execute_input":"2023-09-07T16:52:20.522269Z","iopub.status.idle":"2023-09-07T16:52:23.041768Z","shell.execute_reply.started":"2023-09-07T16:52:20.522232Z","shell.execute_reply":"2023-09-07T16:52:23.040663Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for layer in resnet50_model.layers:\n  layer.trainable = False","metadata":{"execution":{"iopub.status.busy":"2023-09-07T16:52:23.042962Z","iopub.execute_input":"2023-09-07T16:52:23.043380Z","iopub.status.idle":"2023-09-07T16:52:23.099423Z","shell.execute_reply.started":"2023-09-07T16:52:23.043343Z","shell.execute_reply":"2023-09-07T16:52:23.082629Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x = tf.keras.layers.Flatten()(resnet50_model.output)\nx = tf.keras.layers.Dropout(0.5, name=\"Dropout1\")(x)\nx = tf.keras.layers.Dense(512, activation='relu')(x)\nx = tf.keras.layers.Dropout(0.5, name=\"Dropout2\")(x)\noutput = tf.keras.layers.Dense(2, activation='softmax')(x)\nmodel = tf.keras.models.Model(inputs=[resnet50_model.input], outputs=[output])","metadata":{"execution":{"iopub.status.busy":"2023-09-07T16:52:23.101018Z","iopub.execute_input":"2023-09-07T16:52:23.101355Z","iopub.status.idle":"2023-09-07T16:52:23.253310Z","shell.execute_reply.started":"2023-09-07T16:52:23.101327Z","shell.execute_reply":"2023-09-07T16:52:23.252422Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Plot the final model\ntf.keras.utils.plot_model(model, show_shapes = True, show_layer_names=True)","metadata":{"execution":{"iopub.status.busy":"2023-09-07T16:52:23.254481Z","iopub.execute_input":"2023-09-07T16:52:23.254964Z","iopub.status.idle":"2023-09-07T16:52:25.223347Z","shell.execute_reply.started":"2023-09-07T16:52:23.254927Z","shell.execute_reply":"2023-09-07T16:52:25.222175Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Compile the model\nmodel.compile(optimizer=Adam(lr=0.0001), \n              loss=tf.keras.losses.categorical_crossentropy, \n              metrics=['accuracy'])\n\n# Define callback for saving the best model weights\ncheckpoint = tf.keras.callbacks.ModelCheckpoint(\"ResNet50_1.h5\",\n                                                monitor=\"val_loss\",\n                                                save_best_only=True)\n\n# Define callback for early stopping\nearly = tf.keras.callbacks.EarlyStopping(\n    monitor='val_accuracy',\n    min_delta=0,\n    patience=20,\n    verbose=1,\n    mode='auto'\n)\n\n# Train the model using your manually split datasets\nhistory = model.fit(train_generator, \n                    batch_size=batch_size, \n                    validation_data=validation_generator, \n                    epochs=10, callbacks=[checkpoint, early])","metadata":{"execution":{"iopub.status.busy":"2023-09-07T16:52:25.224976Z","iopub.execute_input":"2023-09-07T16:52:25.226001Z","iopub.status.idle":"2023-09-07T17:31:46.909489Z","shell.execute_reply.started":"2023-09-07T16:52:25.225964Z","shell.execute_reply":"2023-09-07T17:31:46.908522Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"acc = history.history['accuracy']\nval_acc = history.history['val_accuracy']\n\nloss = history.history['loss']\nval_loss = history.history['val_loss']\n\nepochs_range = range(10)\n\nplt.figure(figsize=(6, 5))\nplt.subplot(1, 2, 1)\nplt.plot(epochs_range, acc, label='Training Accuracy')\nplt.plot(epochs_range, val_acc, label = 'Validation Accuracy')\nplt.legend(loc='lower right')\nplt.title('Training and Validation Accuracy')\n\nplt.subplot(1, 2, 2)\nplt.plot(epochs_range, loss, label='Training Loss')\nplt.plot(epochs_range, val_loss, label='Validation Loss')\nplt.legend(loc='upper right')\nplt.title('Training and Validation Loss')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-09-07T17:31:46.911199Z","iopub.execute_input":"2023-09-07T17:31:46.911564Z","iopub.status.idle":"2023-09-07T17:31:47.368787Z","shell.execute_reply.started":"2023-09-07T17:31:46.911531Z","shell.execute_reply":"2023-09-07T17:31:47.367867Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Load the best model based on validation loss\nbest_model = tf.keras.models.load_model(\"ResNet50_1.h5\")\n\n# Evaluate the best model on the test data\ntest_loss, test_accuracy = best_model.evaluate(test_generator, verbose=1)\nprint(f\"Test accuracy: {test_accuracy}\")","metadata":{"execution":{"iopub.status.busy":"2023-09-07T17:31:47.370208Z","iopub.execute_input":"2023-09-07T17:31:47.370870Z","iopub.status.idle":"2023-09-07T17:32:22.856769Z","shell.execute_reply.started":"2023-09-07T17:31:47.370829Z","shell.execute_reply":"2023-09-07T17:32:22.855759Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"output = best_model.predict(test_generator)\n\nresult_argmax = np.argmax(output, axis=1)\n\ny_true = test_generator.labels\n\ny_pred = result_argmax\n\naccuracy = (y_pred == y_true).mean()\n\nprint(\"Test Accuracy:\", accuracy)","metadata":{"execution":{"iopub.status.busy":"2023-09-07T17:42:40.254471Z","iopub.execute_input":"2023-09-07T17:42:40.254835Z","iopub.status.idle":"2023-09-07T17:43:03.801057Z","shell.execute_reply.started":"2023-09-07T17:42:40.254806Z","shell.execute_reply":"2023-09-07T17:43:03.800122Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**ResNet50 rock my world 99.04 percent accurate on the test generator**","metadata":{}},{"cell_type":"code","source":"print(classification_report(y_true, y_pred))","metadata":{"execution":{"iopub.status.busy":"2023-09-07T17:43:09.281834Z","iopub.execute_input":"2023-09-07T17:43:09.282542Z","iopub.status.idle":"2023-09-07T17:43:09.302727Z","shell.execute_reply.started":"2023-09-07T17:43:09.282509Z","shell.execute_reply":"2023-09-07T17:43:09.301762Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import confusion_matrix\n\nconfusion = confusion_matrix(y_pred, y_true)\n\nplt.figure(figsize=(8, 6))\nsns.heatmap(confusion, annot=True, fmt=\"d\", cmap=\"Blues\",\n            xticklabels=[\"Cat\", \"Dog\"],\n            yticklabels=[\"Cat\", \"Dog\"])\nplt.xlabel(\"Predicted\")\nplt.ylabel(\"Actual\")\nplt.title(\"Confusion Matrix\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-09-07T17:43:14.200006Z","iopub.execute_input":"2023-09-07T17:43:14.200664Z","iopub.status.idle":"2023-09-07T17:43:14.490037Z","shell.execute_reply.started":"2023-09-07T17:43:14.200633Z","shell.execute_reply":"2023-09-07T17:43:14.489015Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Checking our model on the real Test Data","metadata":{}},{"cell_type":"code","source":"test_dir = f\"{base_dir_working}/test\"","metadata":{"execution":{"iopub.status.busy":"2023-09-07T17:43:29.062380Z","iopub.execute_input":"2023-09-07T17:43:29.062743Z","iopub.status.idle":"2023-09-07T17:43:29.067498Z","shell.execute_reply.started":"2023-09-07T17:43:29.062714Z","shell.execute_reply":"2023-09-07T17:43:29.066464Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"filenames = os.listdir(test_dir)\ntest_data = pd.DataFrame({\"filename\": filenames})\ntest_data['label'] = 'unknown'\ntest_data.head()","metadata":{"execution":{"iopub.status.busy":"2023-09-07T17:43:31.007779Z","iopub.execute_input":"2023-09-07T17:43:31.008494Z","iopub.status.idle":"2023-09-07T17:43:31.030017Z","shell.execute_reply.started":"2023-09-07T17:43:31.008460Z","shell.execute_reply":"2023-09-07T17:43:31.029069Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"real_test_generator =  datagen.flow_from_dataframe(test_data, \"test/\", x_col= \"filename\",y_col = 'label',\n                                    batch_size = batch_size,\n                                    target_size=size, shuffle = False)","metadata":{"execution":{"iopub.status.busy":"2023-09-07T17:43:31.826156Z","iopub.execute_input":"2023-09-07T17:43:31.826516Z","iopub.status.idle":"2023-09-07T17:43:31.971877Z","shell.execute_reply.started":"2023-09-07T17:43:31.826486Z","shell.execute_reply":"2023-09-07T17:43:31.970984Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_predict = best_model.predict(real_test_generator)\n\ntest_predict_argmax = np.argmax(test_predict, axis=1)\n\ny_test_pred = test_predict_argmax\n\ntest_data['label'] = y_test_pred\n\ntest_data.head()","metadata":{"execution":{"iopub.status.busy":"2023-09-07T17:43:32.332986Z","iopub.execute_input":"2023-09-07T17:43:32.333662Z","iopub.status.idle":"2023-09-07T17:45:22.514100Z","shell.execute_reply.started":"2023-09-07T17:43:32.333630Z","shell.execute_reply":"2023-09-07T17:45:22.513114Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data.shape","metadata":{"execution":{"iopub.status.busy":"2023-09-07T17:45:36.240740Z","iopub.execute_input":"2023-09-07T17:45:36.241115Z","iopub.status.idle":"2023-09-07T17:45:36.247839Z","shell.execute_reply.started":"2023-09-07T17:45:36.241064Z","shell.execute_reply":"2023-09-07T17:45:36.246699Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data[test_data.filename == '1368.jpg']","metadata":{"execution":{"iopub.status.busy":"2023-09-07T17:45:37.638207Z","iopub.execute_input":"2023-09-07T17:45:37.638564Z","iopub.status.idle":"2023-09-07T17:45:37.652640Z","shell.execute_reply.started":"2023-09-07T17:45:37.638535Z","shell.execute_reply":"2023-09-07T17:45:37.651456Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data[test_data.filename == '5386.jpg']","metadata":{"execution":{"iopub.status.busy":"2023-09-07T17:45:38.613178Z","iopub.execute_input":"2023-09-07T17:45:38.614051Z","iopub.status.idle":"2023-09-07T17:45:38.625666Z","shell.execute_reply.started":"2023-09-07T17:45:38.614017Z","shell.execute_reply":"2023-09-07T17:45:38.624583Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data[test_data.filename == '475.jpg']","metadata":{"execution":{"iopub.status.busy":"2023-09-07T17:45:39.854145Z","iopub.execute_input":"2023-09-07T17:45:39.854510Z","iopub.status.idle":"2023-09-07T17:45:39.865932Z","shell.execute_reply.started":"2023-09-07T17:45:39.854479Z","shell.execute_reply":"2023-09-07T17:45:39.864960Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data[test_data.filename == '5519.jpg']","metadata":{"execution":{"iopub.status.busy":"2023-09-07T17:45:42.113762Z","iopub.execute_input":"2023-09-07T17:45:42.114148Z","iopub.status.idle":"2023-09-07T17:45:42.125946Z","shell.execute_reply.started":"2023-09-07T17:45:42.114113Z","shell.execute_reply":"2023-09-07T17:45:42.124940Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data[test_data.filename == '9584.jpg']","metadata":{"execution":{"iopub.status.busy":"2023-09-07T17:45:43.247516Z","iopub.execute_input":"2023-09-07T17:45:43.249374Z","iopub.status.idle":"2023-09-07T17:45:43.262135Z","shell.execute_reply.started":"2023-09-07T17:45:43.249323Z","shell.execute_reply":"2023-09-07T17:45:43.260883Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_generator.class_indices","metadata":{"execution":{"iopub.status.busy":"2023-09-07T17:45:44.698029Z","iopub.execute_input":"2023-09-07T17:45:44.698948Z","iopub.status.idle":"2023-09-07T17:45:44.705300Z","shell.execute_reply.started":"2023-09-07T17:45:44.698906Z","shell.execute_reply":"2023-09-07T17:45:44.704269Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"label_mapping = {0: 'cat', 1: 'dog'}\ntest_data['label'] = test_data['label'].map(label_mapping)\ntest_data.head()","metadata":{"execution":{"iopub.status.busy":"2023-09-07T17:45:45.632602Z","iopub.execute_input":"2023-09-07T17:45:45.633331Z","iopub.status.idle":"2023-09-07T17:45:45.646587Z","shell.execute_reply.started":"2023-09-07T17:45:45.633294Z","shell.execute_reply":"2023-09-07T17:45:45.645505Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Lets extend beyond 5 images \n\nfig, axes = plt.subplots(4, 5, figsize=(25, 15))  # 2 rows, 5 columns\n\nfor idx in range(20):  # Display 10 images\n    row = idx // 5  # Calculate the row index\n    col = idx % 5   # Calculate the column index\n\n    image_path = os.path.join(test_dir, test_data.iloc[idx]['filename'])\n    image = Image.open(image_path)\n    \n    axes[row, col].imshow(image)\n    axes[row, col].set_title(\"Label: \" + test_data.iloc[idx]['label'])\n    axes[row, col].axis('off')\n\nplt.subplots_adjust(wspace=0.3, hspace=0.3)  # Add spacing between subplots\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-09-07T17:45:48.012059Z","iopub.execute_input":"2023-09-07T17:45:48.013630Z","iopub.status.idle":"2023-09-07T17:45:50.597598Z","shell.execute_reply.started":"2023-09-07T17:45:48.013589Z","shell.execute_reply":"2023-09-07T17:45:50.595183Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### Our model was very decent in predicting the image classes correctly","metadata":{}},{"cell_type":"code","source":"os.listdir('/kaggle/input/dogs-vs-cats-redux-kernels-edition')","metadata":{"execution":{"iopub.status.busy":"2023-09-07T17:47:34.478598Z","iopub.execute_input":"2023-09-07T17:47:34.478955Z","iopub.status.idle":"2023-09-07T17:47:34.486190Z","shell.execute_reply.started":"2023-09-07T17:47:34.478928Z","shell.execute_reply":"2023-09-07T17:47:34.485138Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_sample = pd.read_csv('/kaggle/input/dogs-vs-cats-redux-kernels-edition/sample_submission.csv')","metadata":{"execution":{"iopub.status.busy":"2023-09-07T17:48:00.724173Z","iopub.execute_input":"2023-09-07T17:48:00.724545Z","iopub.status.idle":"2023-09-07T17:48:00.750709Z","shell.execute_reply.started":"2023-09-07T17:48:00.724517Z","shell.execute_reply":"2023-09-07T17:48:00.749807Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2023-09-07T17:50:50.879590Z","iopub.execute_input":"2023-09-07T17:50:50.879960Z","iopub.status.idle":"2023-09-07T17:50:50.917267Z","shell.execute_reply.started":"2023-09-07T17:50:50.879930Z","shell.execute_reply":"2023-09-07T17:50:50.916208Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pd.read_csv('/kaggle/working/submission.csv').head(10)","metadata":{"execution":{"iopub.status.busy":"2023-09-07T17:51:21.995231Z","iopub.execute_input":"2023-09-07T17:51:21.995639Z","iopub.status.idle":"2023-09-07T17:51:22.017929Z","shell.execute_reply.started":"2023-09-07T17:51:21.995608Z","shell.execute_reply":"2023-09-07T17:51:22.016041Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}],"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"}}