{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.14","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":9238397,"sourceType":"datasetVersion","datasetId":5588187}],"dockerImageVersionId":30761,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"!pip install visualkeras","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2024-09-02T16:33:51.307069Z","iopub.execute_input":"2024-09-02T16:33:51.307733Z","iopub.status.idle":"2024-09-02T16:34:06.366369Z","shell.execute_reply.started":"2024-09-02T16:33:51.307682Z","shell.execute_reply":"2024-09-02T16:34:06.365338Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Dependencies","metadata":{}},{"cell_type":"markdown","source":"# Import Dataset","metadata":{}},{"cell_type":"code","source":"# Data wrangling\nimport zipfile\nimport numpy as np\nimport pandas as pd\nfrom sklearn.model_selection import train_test_split\n\n# General\nimport os\nimport random\nfrom shutil import copyfile\nfrom collections import Counter\n\n# Image visualisation\nimport cv2\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport plotly.express as px\nfrom PIL import Image\n\n# Image Data Processing\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras.preprocessing.image import load_img\n\n# Building the model\nimport tensorflow as tf\nfrom tensorflow.keras.models import Sequential\nfrom keras.utils import plot_model\nfrom tensorflow.keras.layers import MaxPooling2D, Conv2D, Dense, Dropout, Flatten, BatchNormalization\nfrom tensorflow.keras.callbacks import EarlyStopping, ReduceLROnPlateau\nfrom visualkeras import layered_view\n\n# Model Evaluation\nfrom sklearn.metrics import classification_report,confusion_matrix\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-09-02T16:34:06.368177Z","iopub.execute_input":"2024-09-02T16:34:06.368513Z","iopub.status.idle":"2024-09-02T16:34:22.491656Z","shell.execute_reply.started":"2024-09-02T16:34:06.368477Z","shell.execute_reply":"2024-09-02T16:34:22.490864Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Get Zip","metadata":{}},{"cell_type":"markdown","source":"os.listdir(image_dir) gives us = \n\n['dog.8711.jpg',\n 'cat.11569.jpg',\n 'dog.11560.jpg',\n 'cat.2932.jpg',\n 'cat.5351.jpg'....]","metadata":{}},{"cell_type":"code","source":"# Initialize zip paths\ntrain_zip_path = \"/kaggle/input/dogs-vs-cats/train.zip\"\ntest_zip_path = \"/kaggle/input/dogs-vs-cats/test1.zip\"\n\n# Address extract location for zip files\nextract_dir = \"/kaggle/working/\"\n\n# Unzip train dataset\nwith zipfile.ZipFile(train_zip_path, \"r\") as zip_ref:\n    zip_ref.extractall(extract_dir)\n    \n# Unzip test dataset\nwith zipfile.ZipFile(test_zip_path, \"r\") as zip_ref:\n    zip_ref.extractall(extract_dir)","metadata":{"execution":{"iopub.status.busy":"2024-09-02T16:34:22.492867Z","iopub.execute_input":"2024-09-02T16:34:22.493485Z","iopub.status.idle":"2024-09-02T16:34:37.768095Z","shell.execute_reply.started":"2024-09-02T16:34:22.493448Z","shell.execute_reply":"2024-09-02T16:34:37.767220Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Loop through each image in train folder and put it to a DataFrame\nimage_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":"2024-09-02T16:34:37.771040Z","iopub.execute_input":"2024-09-02T16:34:37.771568Z","iopub.status.idle":"2024-09-02T16:34:37.823243Z","shell.execute_reply.started":"2024-09-02T16:34:37.771516Z","shell.execute_reply":"2024-09-02T16:34:37.822307Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Data Exploration","metadata":{}},{"cell_type":"code","source":"# Data exploration, Image Visualization\ndef get_images_from_folder(directory):\n    images = []\n    labels = []\n    for filename in os.listdir(directory):\n        img_path = os.path.join(directory, filename)\n        image = load_img(img_path)\n        images.append(image)\n\n        # Assuming label extraction is done here\n        label = filename.split(\".\")[0]\n        labels.append(label)\n        \n    return images, labels\n\ndata_images, data_labels = get_images_from_folder(\"/kaggle/working/train\")\n","metadata":{"execution":{"iopub.status.busy":"2024-09-02T16:34:37.824367Z","iopub.execute_input":"2024-09-02T16:34:37.824673Z","iopub.status.idle":"2024-09-02T16:34:41.566537Z","shell.execute_reply.started":"2024-09-02T16:34:37.824618Z","shell.execute_reply":"2024-09-02T16:34:41.565669Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# See stock images from the dataset\ndef display_images(images, labels, num_images=5):\n    plt.figure(figsize=(10, 5))\n    for i in range(num_images):\n        plt.subplot(1, num_images, i + 1)\n        plt.imshow(images[i])\n        plt.title(labels[i])\n        plt.axis('off')\n    plt.show()\n\ndisplay_images(data_images, data_labels)","metadata":{"execution":{"iopub.status.busy":"2024-09-02T16:34:41.567813Z","iopub.execute_input":"2024-09-02T16:34:41.568190Z","iopub.status.idle":"2024-09-02T16:34:42.083139Z","shell.execute_reply.started":"2024-09-02T16:34:41.568147Z","shell.execute_reply":"2024-09-02T16:34:42.082229Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Load an image using OpenCV\nimg = cv2.imread('/kaggle/working/train/cat.1.jpg')\n\n# Convert the image from BGR to RGB (since OpenCV loads images in BGR format)\nimg_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\n\n# Split the image into R, G, B channels\nR, G, B = cv2.split(img_rgb)\n\n# Plotting the original and the R, G, B channels\nfig, axes = plt.subplots(1, 4, figsize=(20, 5))\n\n# Original Image\naxes[0].imshow(img_rgb)\naxes[0].set_title('Original Image')\naxes[0].axis('off')\n\n# Red Channel\naxes[1].imshow(R, cmap='Reds')\naxes[1].set_title('Red Channel')\naxes[1].axis('off')\n\n# Green Channel\naxes[2].imshow(G, cmap='Greens')\naxes[2].set_title('Green Channel')\naxes[2].axis('off')\n\n# Blue Channel\naxes[3].imshow(B, cmap='Blues')\naxes[3].set_title('Blue Channel')\naxes[3].axis('off')\n\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2024-09-02T16:34:42.084443Z","iopub.execute_input":"2024-09-02T16:34:42.084853Z","iopub.status.idle":"2024-09-02T16:34:42.760126Z","shell.execute_reply.started":"2024-09-02T16:34:42.084807Z","shell.execute_reply":"2024-09-02T16:34:42.759159Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Analyzing Image Dimensions","metadata":{}},{"cell_type":"code","source":"# Get only the first two shape of the images\ndef analyze_image_shapes(directory):\n    shapes = []\n\n    # Loop through all images in the directory\n    for filename in os.listdir(directory):\n        img_path = os.path.join(directory, filename)\n        img = cv2.imread(img_path)\n        \n        if img is not None: # If image exists\n            shapes.append(img.shape[:2])\n\n    return shapes\n\n# Load and analyze image shapes\nimage_shapes = analyze_image_shapes('/kaggle/working/train')","metadata":{"execution":{"iopub.status.busy":"2024-09-02T16:34:42.761604Z","iopub.execute_input":"2024-09-02T16:34:42.762005Z","iopub.status.idle":"2024-09-02T16:35:02.113355Z","shell.execute_reply.started":"2024-09-02T16:34:42.761963Z","shell.execute_reply":"2024-09-02T16:35:02.112516Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Turn into Numpy for Analysis","metadata":{}},{"cell_type":"code","source":"# Convert shapes to a NumPy array \nimage_shapes_np = np.array(image_shapes)\n\n# Calculate median shape\nmedian_shape = np.median(image_shapes_np, axis=0)\n\n# Calculate mode shape\nshape_counter = Counter([tuple(shape) for shape in image_shapes])\nmode_shape = shape_counter.most_common(1)[0][0]\n\n# Which shapes are the most common?\nshape_counter.most_common(5)","metadata":{"execution":{"iopub.status.busy":"2024-09-02T16:35:02.114434Z","iopub.execute_input":"2024-09-02T16:35:02.114737Z","iopub.status.idle":"2024-09-02T16:35:02.149116Z","shell.execute_reply.started":"2024-09-02T16:35:02.114706Z","shell.execute_reply":"2024-09-02T16:35:02.148239Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(f\"Median shape: {median_shape}\")\nprint(f\"Mode shape: {mode_shape}\")","metadata":{"execution":{"iopub.status.busy":"2024-09-02T16:35:02.152348Z","iopub.execute_input":"2024-09-02T16:35:02.152699Z","iopub.status.idle":"2024-09-02T16:35:02.157707Z","shell.execute_reply.started":"2024-09-02T16:35:02.152659Z","shell.execute_reply":"2024-09-02T16:35:02.156766Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Load a simple test image\ntest_image_path = \"/kaggle/input/cat-dog/train/dog/dog.375.jpg\"\ntest_image = cv2.imread(test_image_path)\n\nif test_image is not None:\n    # Display the image using matplotlib\n    plt.imshow(cv2.cvtColor(test_image, cv2.COLOR_BGR2RGB))\n    plt.axis('off')\n    plt.show()\nelse:\n    print(f\"Failed to load test image at {test_image_path}.\")","metadata":{"execution":{"iopub.status.busy":"2024-09-02T16:35:02.158997Z","iopub.execute_input":"2024-09-02T16:35:02.159368Z","iopub.status.idle":"2024-09-02T16:35:02.427318Z","shell.execute_reply.started":"2024-09-02T16:35:02.159317Z","shell.execute_reply":"2024-09-02T16:35:02.426438Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Load the image size\nimage = cv2.imread(test_image_path)\n\n# Get the image dimensions (height, width, and number of channels)\nheight, width, channels = image.shape\n\nprint(f\"Height: {height}, Width: {width}, Channels: {channels}\")","metadata":{"execution":{"iopub.status.busy":"2024-09-02T16:35:02.428350Z","iopub.execute_input":"2024-09-02T16:35:02.428680Z","iopub.status.idle":"2024-09-02T16:35:02.435600Z","shell.execute_reply.started":"2024-09-02T16:35:02.428615Z","shell.execute_reply":"2024-09-02T16:35:02.434688Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Train Test Split","metadata":{}},{"cell_type":"markdown","source":"### Get Train, Validation and Test Sets","metadata":{}},{"cell_type":"code","source":"labels = 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":"2024-09-02T16:35:02.436918Z","iopub.execute_input":"2024-09-02T16:35:02.437240Z","iopub.status.idle":"2024-09-02T16:35:02.498642Z","shell.execute_reply.started":"2024-09-02T16:35:02.437207Z","shell.execute_reply":"2024-09-02T16:35:02.497690Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Analyze the distribution between sets for each class","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":"2024-09-02T16:35:02.499702Z","iopub.execute_input":"2024-09-02T16:35:02.500008Z","iopub.status.idle":"2024-09-02T16:35:03.305388Z","shell.execute_reply.started":"2024-09-02T16:35:02.499975Z","shell.execute_reply":"2024-09-02T16:35:03.304451Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Creating Directories","metadata":{}},{"cell_type":"code","source":"# Define dataset home directory and subdirectories\ndataset_home = '/kaggle/working/dataset_dogs_vs_cats'\nsubdirs = ['train/', 'test/']\n\n\n\n# Create the main directories and subdirectories\nfor subdir in subdirs:\n    # Create label subdirectories\n    labeldirs = ['dogs/', 'cats/']\n    for labldir in labeldirs:\n        newdir = os.path.join(dataset_home, subdir, labldir)\n        os.makedirs(newdir, exist_ok=True)\n\n# Seed random number generator for reproducibility\nrandom.seed(1)\n\n# Define the ratio of pictures to use for validation\nval_ratio = 0.2\n\n\n# Source directory where the original images are located\nsrc_directory = '/kaggle/working/train'\n\n# Loop through each file in the source directory\nfor file in os.listdir(src_directory):\n    src = os.path.join(src_directory, file)\n    dst_dir = 'train/'  # Default destination is train directory\n    \n    # Randomly assign some images to the test set based on val_ratio\n    if random.random() < val_ratio:\n        dst_dir = 'test/'\n    \n    # Determine the correct subdirectory based on the filename\n    if file.startswith('cat'):\n        dst = os.path.join(dataset_home, dst_dir, 'cats/', file)\n    elif file.startswith('dog'):\n        dst = os.path.join(dataset_home, dst_dir, 'dogs/', file)\n    \n    # Copy the file to the appropriate destination\n    copyfile(src, dst)","metadata":{"execution":{"iopub.status.busy":"2024-09-02T16:35:03.306674Z","iopub.execute_input":"2024-09-02T16:35:03.307036Z","iopub.status.idle":"2024-09-02T16:35:05.983581Z","shell.execute_reply.started":"2024-09-02T16:35:03.306997Z","shell.execute_reply":"2024-09-02T16:35:05.982786Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Check the directories!","metadata":{}},{"cell_type":"code","source":"# Check the number of files in each directory\nfor subdir in subdirs:\n    for labldir in labeldirs:\n        dir_path = os.path.join(dataset_home, subdir, labldir)\n        print(f'{dir_path}: {len(os.listdir(dir_path))} images')","metadata":{"execution":{"iopub.status.busy":"2024-09-02T16:35:05.984745Z","iopub.execute_input":"2024-09-02T16:35:05.985038Z","iopub.status.idle":"2024-09-02T16:35:06.004540Z","shell.execute_reply.started":"2024-09-02T16:35:05.985007Z","shell.execute_reply":"2024-09-02T16:35:06.003676Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Check the number of files in each directory\ncheck_path = '/kaggle/working/dataset_dogs_vs_cats'\n\nsubdirs = [\"train/cats\", \"train/dogs\", \"test/cats\", \"test/dogs\"]\n\nfor subdir in subdirs:\n        path = os.path.join(check_path, subdir)\n        image_count = len(os.listdir(path))\n        print(f\"Number of images in {subdir}: {image_count}\")","metadata":{"execution":{"iopub.status.busy":"2024-09-02T16:35:06.005669Z","iopub.execute_input":"2024-09-02T16:35:06.005938Z","iopub.status.idle":"2024-09-02T16:35:06.025404Z","shell.execute_reply.started":"2024-09-02T16:35:06.005908Z","shell.execute_reply":"2024-09-02T16:35:06.024551Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Data Augmentation","metadata":{}},{"cell_type":"code","source":"# parameters\nimage_size = 128\nimage_channel = 3\nbat_size = 32","metadata":{"execution":{"iopub.status.busy":"2024-09-02T16:35:06.026554Z","iopub.execute_input":"2024-09-02T16:35:06.026945Z","iopub.status.idle":"2024-09-02T16:35:06.031425Z","shell.execute_reply.started":"2024-09-02T16:35:06.026898Z","shell.execute_reply":"2024-09-02T16:35:06.030480Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# # AAAA Creating image data generators\n# train_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\n# test_datagen = ImageDataGenerator(rescale=1./255)\n\n# # Applying image data generator to train and validation data\n# train_generator = train_datagen.flow_from_dataframe(\n#     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#     class_mode='binary'  # Binary classification problem !!\n# )\n\n# val_generator = test_datagen.flow_from_dataframe(\n#     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#     class_mode='binary'\n# )","metadata":{"execution":{"iopub.status.busy":"2024-09-02T16:35:06.032506Z","iopub.execute_input":"2024-09-02T16:35:06.032812Z","iopub.status.idle":"2024-09-02T16:35:06.040912Z","shell.execute_reply.started":"2024-09-02T16:35:06.032779Z","shell.execute_reply":"2024-09-02T16:35:06.039963Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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":"2024-09-02T16:35:06.042235Z","iopub.execute_input":"2024-09-02T16:35:06.042606Z","iopub.status.idle":"2024-09-02T16:35:06.054503Z","shell.execute_reply.started":"2024-09-02T16:35:06.042556Z","shell.execute_reply":"2024-09-02T16:35:06.053601Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Defining data set generators","metadata":{}},{"cell_type":"code","source":"# root directory for the generator images\ndirectory = \"train/\"\n\n# Applying image data gernerator to train and test data\n\ntrain_generator = train_datagen.flow_from_dataframe(X_train,\n                                                    directory = directory,\n                                                    x_col= \"filename\",\n                                                    y_col= \"label\",\n                                                    batch_size = bat_size,\n                                                    target_size = (image_size,image_size),\n                                                    class_mode=\"binary\"\n                                                   )\nval_generator = test_datagen.flow_from_dataframe(X_val, \n                                                 directory = directory,\n                                                 x_col= \"filename\",\n                                                 y_col= \"label\",\n                                                 batch_size = bat_size,\n                                                 target_size = (image_size,image_size),\n                                                 shuffle=False,\n                                                 class_mode=\"binary\"\n                                                )\n\ntest_generator = test_datagen.flow_from_dataframe(X_test, \n                                                  directory = directory,\n                                                  x_col= \"filename\",\n                                                  y_col= \"label\",\n                                                  batch_size = bat_size,\n                                                  target_size = (image_size,image_size),\n                                                  shuffle=False,\n                                                  class_mode=\"binary\"\n                                                 )","metadata":{"execution":{"iopub.status.busy":"2024-09-02T16:35:06.055728Z","iopub.execute_input":"2024-09-02T16:35:06.055986Z","iopub.status.idle":"2024-09-02T16:35:06.366273Z","shell.execute_reply.started":"2024-09-02T16:35:06.055957Z","shell.execute_reply":"2024-09-02T16:35:06.365394Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Visualize the Generator and Labels","metadata":{}},{"cell_type":"code","source":"# Get a batch of images and labels from the train generator\nimages, labels = next(train_generator)\n\n# Limit to 16 images for display\nnum_images = min(len(images), 16)\n\n# Plot the images along with their labels\nplt.figure(figsize=(12, 12))\nfor i in range(num_images):\n    plt.subplot(4, 4, i + 1)\n    plt.imshow(images[i])\n    plt.title(f\"Label: {labels[i]}\")\n    plt.axis('off')\n\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2024-09-02T17:42:02.017684Z","iopub.execute_input":"2024-09-02T17:42:02.018438Z","iopub.status.idle":"2024-09-02T17:42:04.773228Z","shell.execute_reply.started":"2024-09-02T17:42:02.018395Z","shell.execute_reply":"2024-09-02T17:42:04.772262Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Model","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(1,activation='sigmoid'))\n\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2024-09-02T16:35:07.037662Z","iopub.execute_input":"2024-09-02T16:35:07.037976Z","iopub.status.idle":"2024-09-02T16:35:08.514675Z","shell.execute_reply.started":"2024-09-02T16:35:07.037943Z","shell.execute_reply":"2024-09-02T16:35:08.513824Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Model Visualization","metadata":{}},{"cell_type":"code","source":"layered_view(model, legend=True, max_xy=300,)","metadata":{"execution":{"iopub.status.busy":"2024-09-02T16:35:08.515838Z","iopub.execute_input":"2024-09-02T16:35:08.516173Z","iopub.status.idle":"2024-09-02T16:35:08.581444Z","shell.execute_reply.started":"2024-09-02T16:35:08.516137Z","shell.execute_reply":"2024-09-02T16:35:08.580319Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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":"2024-09-02T16:35:08.582896Z","iopub.execute_input":"2024-09-02T16:35:08.583271Z","iopub.status.idle":"2024-09-02T16:35:08.589794Z","shell.execute_reply.started":"2024-09-02T16:35:08.583234Z","shell.execute_reply":"2024-09-02T16:35:08.588183Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer='adam',loss='binary_crossentropy',metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2024-09-02T16:35:08.591070Z","iopub.execute_input":"2024-09-02T16:35:08.591375Z","iopub.status.idle":"2024-09-02T16:35:08.614966Z","shell.execute_reply.started":"2024-09-02T16:35:08.591341Z","shell.execute_reply":"2024-09-02T16:35:08.614070Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Fit","metadata":{}},{"cell_type":"code","source":"history = 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                   )\n","metadata":{"execution":{"iopub.status.busy":"2024-09-02T16:35:08.616263Z","iopub.execute_input":"2024-09-02T16:35:08.617244Z","iopub.status.idle":"2024-09-02T17:25:45.462341Z","shell.execute_reply.started":"2024-09-02T16:35:08.617200Z","shell.execute_reply":"2024-09-02T17:25:45.461465Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Model Evaluation","metadata":{}},{"cell_type":"code","source":"error = pd.DataFrame(history.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":{"execution":{"iopub.status.busy":"2024-09-02T17:25:45.468140Z","iopub.execute_input":"2024-09-02T17:25:45.468489Z","iopub.status.idle":"2024-09-02T17:25:46.299069Z","shell.execute_reply.started":"2024-09-02T17:25:45.468452Z","shell.execute_reply":"2024-09-02T17:25:46.298171Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# My Test Set Prediction","metadata":{}},{"cell_type":"code","source":"# prediction\nresult = model.predict(test_generator,batch_size = bat_size,verbose = 0)\n\ny_pred = (result > 0.5).astype(int).flatten()\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":"2024-09-02T17:47:08.505104Z","iopub.execute_input":"2024-09-02T17:47:08.505583Z","iopub.status.idle":"2024-09-02T17:47:15.203319Z","shell.execute_reply.started":"2024-09-02T17:47:08.505540Z","shell.execute_reply":"2024-09-02T17:47:15.202356Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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":"2024-09-02T17:47:18.760612Z","iopub.execute_input":"2024-09-02T17:47:18.761052Z","iopub.status.idle":"2024-09-02T17:47:18.776466Z","shell.execute_reply.started":"2024-09-02T17:47:18.761013Z","shell.execute_reply":"2024-09-02T17:47:18.775551Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Confusion Matrix","metadata":{}},{"cell_type":"code","source":"cm = confusion_matrix(y_true, y_pred)\n\nplt.figure(figsize=(6, 4))\nsns.heatmap(cm, annot=True, fmt=\"d\", cmap=\"Blues\", cbar=False)\nplt.title(\"Confusion Matrix\")\nplt.xlabel(\"Predicted Label\")\nplt.ylabel(\"True Label\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-09-02T17:49:40.002914Z","iopub.execute_input":"2024-09-02T17:49:40.003770Z","iopub.status.idle":"2024-09-02T17:49:40.213782Z","shell.execute_reply.started":"2024-09-02T17:49:40.003727Z","shell.execute_reply":"2024-09-02T17:49:40.212815Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Make predictions on the test set\npredictions = model.predict(val_generator)\npredicted_labels = np.argmax(predictions, axis=1)\ntrue_labels = val_generator.classes\n\n# Identify misclassified images\nmisclassified_indices = np.where(predicted_labels  != true_labels)[0]\n\n# Visualize misclassified images\nfig, axes = plt.subplots(1, 10, figsize=(20, 4))  # Adjust the number 10 as needed\nfor idx, ax in enumerate(axes):\n    if idx >= len(misclassified_indices):\n        break\n    misclassified_idx = misclassified_indices[idx]\n    image_path = val_generator.filepaths[misclassified_idx]\n    image = Image.open(image_path)\n    ax.imshow(image)\n    ax.set_title(f\"True: {true_labels[misclassified_idx]}, Pred: {predicted_labels[misclassified_idx]}\")\n    ax.axis('off')\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2024-09-02T17:49:53.051005Z","iopub.execute_input":"2024-09-02T17:49:53.051958Z","iopub.status.idle":"2024-09-02T17:49:57.813307Z","shell.execute_reply.started":"2024-09-02T17:49:53.051898Z","shell.execute_reply":"2024-09-02T17:49:57.812358Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Test1 Prediction","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 = (test1_predict > 0.5).astype(int).flatten()\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":"2024-09-02T18:41:42.805980Z","iopub.execute_input":"2024-09-02T18:41:42.806353Z","iopub.status.idle":"2024-09-02T18:41:59.470725Z","shell.execute_reply.started":"2024-09-02T18:41:42.806319Z","shell.execute_reply":"2024-09-02T18:41:59.469920Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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":"2024-09-02T18:41:59.472528Z","iopub.execute_input":"2024-09-02T18:41:59.473029Z","iopub.status.idle":"2024-09-02T18:42:01.379997Z","shell.execute_reply.started":"2024-09-02T18:41:59.472982Z","shell.execute_reply":"2024-09-02T18:42:01.379119Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}