{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":3362,"databundleVersionId":31148,"sourceType":"competition"}],"dockerImageVersionId":30746,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"## Convolution Neural Network for image classification","metadata":{}},{"cell_type":"markdown","source":"Problem Statement: This is an image classification problem. The task is to classify images as Cat or Dog, hence its Binary classification. \n\nSolution: This can be reloved using Deep Learning techniques. Lets try with Convolutional Neural Network with Augmentation. Augmentation allows us to create copy of images in different angles, brightness, zoom level and so on. This helps the model to get trained better and be robust.\n\nAfter trying on CNN, we can upgrade to transfer learning using VG16. The VG16 is a pre trained model and can helps to achieve better accuracy","metadata":{}},{"cell_type":"markdown","source":"## Import Libraries","metadata":{}},{"cell_type":"code","source":"import warnings\nwarnings.filterwarnings('ignore')\n\nimport os\nfrom os import makedirs\nfrom os import listdir\nfrom shutil import copyfile\n\nimport pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nfrom matplotlib.image import imread\nfrom PIL import Image\nimport seaborn as sns\nimport random\n\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import classification_report,confusion_matrix,ConfusionMatrixDisplay\n\nimport tensorflow as tf\nfrom tensorflow.keras.models import Sequential,load_model,Model,save_model\nfrom tensorflow.keras import layers\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras.layers import Dense, MaxPooling2D, Dropout,Flatten, Conv2D,RandomContrast ,RandomFlip,RandomRotation, RandomZoom, RandomHeight, RandomWidth,BatchNormalization\nfrom tensorflow.keras.callbacks import EarlyStopping,ReduceLROnPlateau,ModelCheckpoint\nfrom tensorflow.keras.utils import image_dataset_from_directory\nfrom functools import partial\nfrom tensorflow.keras import applications, optimizers\n\n#Seed\nseed = 0\nnp.random.seed(seed)\ntf.random.set_seed(42)","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-08-07T16:05:30.149945Z","iopub.execute_input":"2024-08-07T16:05:30.150736Z","iopub.status.idle":"2024-08-07T16:05:30.185585Z","shell.execute_reply.started":"2024-08-07T16:05:30.150701Z","shell.execute_reply":"2024-08-07T16:05:30.184485Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Unzip and Load the files to a Dataframe","metadata":{}},{"cell_type":"code","source":"foldernames = [name.split('.')[0] for name in os.listdir('/kaggle/input/dogs-vs-cats') if name.split('.')[1] == 'zip']\nprint('File Names:',foldernames)","metadata":{"execution":{"iopub.status.busy":"2024-08-07T13:50:27.356034Z","iopub.execute_input":"2024-08-07T13:50:27.357057Z","iopub.status.idle":"2024-08-07T13:50:27.363974Z","shell.execute_reply.started":"2024-08-07T13:50:27.357008Z","shell.execute_reply":"2024-08-07T13:50:27.363103Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import zipfile\n\nfor name in foldernames:\n    with zipfile.ZipFile('../input/dogs-vs-cats/{}.zip'.format(name),'r') as z:\n        z.extractall('.')\n        print('The {} is unzipped and is available in working folder'.format(name))","metadata":{"execution":{"iopub.status.busy":"2024-08-07T13:50:27.365896Z","iopub.execute_input":"2024-08-07T13:50:27.366372Z","iopub.status.idle":"2024-08-07T13:50:42.069386Z","shell.execute_reply.started":"2024-08-07T13:50:27.366346Z","shell.execute_reply":"2024-08-07T13:50:42.068388Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_filenames = [name for name in os.listdir('/kaggle/working/train'.format(name)) ]\ntest_filenames = [name for name in os.listdir('/kaggle/working/test1'.format(name)) ]\nprint('Total files in train folder', len(train_filenames))\nprint('Total files in test folder', len(test_filenames))","metadata":{"execution":{"iopub.status.busy":"2024-08-07T13:50:42.072025Z","iopub.execute_input":"2024-08-07T13:50:42.072350Z","iopub.status.idle":"2024-08-07T13:50:42.099840Z","shell.execute_reply.started":"2024-08-07T13:50:42.072324Z","shell.execute_reply":"2024-08-07T13:50:42.098660Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def to_df(filenames,name):\n    files = []\n    label = []\n    for name in filenames:\n        files.append(name)\n        label.append(name[:3]) # Use first 2 characters to create label name\n    df = pd.DataFrame({'filename':files,'label':label})\n    return df\ndf_train = to_df(train_filenames,name) \ndf_test = to_df(test_filenames,name) ","metadata":{"execution":{"iopub.status.busy":"2024-08-07T13:50:42.101180Z","iopub.execute_input":"2024-08-07T13:50:42.101640Z","iopub.status.idle":"2024-08-07T13:50:42.122593Z","shell.execute_reply.started":"2024-08-07T13:50:42.101606Z","shell.execute_reply":"2024-08-07T13:50:42.121623Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.head(5)","metadata":{"execution":{"iopub.status.busy":"2024-08-07T13:50:42.123956Z","iopub.execute_input":"2024-08-07T13:50:42.124415Z","iopub.status.idle":"2024-08-07T13:50:42.148188Z","shell.execute_reply.started":"2024-08-07T13:50:42.124388Z","shell.execute_reply":"2024-08-07T13:50:42.147240Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Check the Data distribution\n\nThis is to ensure that we dont give any bias to the model. Here the target is equally balanced","metadata":{}},{"cell_type":"code","source":"print('Data Distribution',df_train['label'].value_counts())\n\ndf_train['label'].value_counts().plot(kind='pie',autopct='%0.f%%',title='Types of Images')","metadata":{"execution":{"iopub.status.busy":"2024-08-07T13:50:42.149242Z","iopub.execute_input":"2024-08-07T13:50:42.149521Z","iopub.status.idle":"2024-08-07T13:50:42.331065Z","shell.execute_reply.started":"2024-08-07T13:50:42.149498Z","shell.execute_reply":"2024-08-07T13:50:42.329808Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Generate Random Images for Cat and Dogs","metadata":{}},{"cell_type":"code","source":"cat_files = [name for i,name in enumerate(df_train['filename']) if df_train.iloc[i,1] == 'cat']\ndog_files = [name for i,name in enumerate(df_train['filename']) if df_train.iloc[i,1] == 'dog']","metadata":{"execution":{"iopub.status.busy":"2024-08-07T13:50:42.333231Z","iopub.execute_input":"2024-08-07T13:50:42.333752Z","iopub.status.idle":"2024-08-07T13:50:43.664580Z","shell.execute_reply.started":"2024-08-07T13:50:42.333716Z","shell.execute_reply":"2024-08-07T13:50:43.663712Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i,name in enumerate(np.random.randint(0,len(cat_files),5)):\n    plt.subplot(1,5,i+1)   \n    filename = 'train/' + cat_files[name]\n    image = imread(filename)\n    plt.imshow(image)\n    plt.axis('off')    ","metadata":{"execution":{"iopub.status.busy":"2024-08-07T13:50:43.665889Z","iopub.execute_input":"2024-08-07T13:50:43.666389Z","iopub.status.idle":"2024-08-07T13:50:44.142345Z","shell.execute_reply.started":"2024-08-07T13:50:43.666355Z","shell.execute_reply":"2024-08-07T13:50:44.141282Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i,name in enumerate(np.random.randint(0,len(dog_files),5)):\n    plt.subplot(1,5,i+1)          \n    filename = 'train/' + dog_files[name]\n    image = imread(filename)\n    plt.imshow(image)    \n    plt.axis('off')    ","metadata":{"execution":{"iopub.status.busy":"2024-08-07T13:50:44.147343Z","iopub.execute_input":"2024-08-07T13:50:44.147991Z","iopub.status.idle":"2024-08-07T13:50:44.678550Z","shell.execute_reply.started":"2024-08-07T13:50:44.147961Z","shell.execute_reply":"2024-08-07T13:50:44.677470Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Split the data for training and Testing","metadata":{}},{"cell_type":"code","source":"X_train, X_test = train_test_split(df_train,test_size =0.2,random_state = 42)","metadata":{"execution":{"iopub.status.busy":"2024-08-07T13:50:44.679859Z","iopub.execute_input":"2024-08-07T13:50:44.680945Z","iopub.status.idle":"2024-08-07T13:50:44.693258Z","shell.execute_reply.started":"2024-08-07T13:50:44.680907Z","shell.execute_reply":"2024-08-07T13:50:44.692187Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train.shape, X_test.shape","metadata":{"execution":{"iopub.status.busy":"2024-08-07T13:50:44.694648Z","iopub.execute_input":"2024-08-07T13:50:44.695099Z","iopub.status.idle":"2024-08-07T13:50:44.702700Z","shell.execute_reply.started":"2024-08-07T13:50:44.695064Z","shell.execute_reply":"2024-08-07T13:50:44.701606Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Target balance for Train and Test Data","metadata":{}},{"cell_type":"code","source":"plt.subplot(1,2,1)\nprint('Train Data Distribution',X_train['label'].value_counts())\n\nX_train['label'].value_counts().plot(kind='pie',autopct='%0.f%%',title='Train Data Distribution')\n\nplt.subplot(1,2,2)\nprint('Test Data Distribution',X_test['label'].value_counts())\n\nX_test['label'].value_counts().plot(kind='pie',autopct='%0.f%%',title='Test Data Distribution')","metadata":{"execution":{"iopub.status.busy":"2024-08-07T13:50:44.704378Z","iopub.execute_input":"2024-08-07T13:50:44.704749Z","iopub.status.idle":"2024-08-07T13:50:44.910203Z","shell.execute_reply.started":"2024-08-07T13:50:44.704716Z","shell.execute_reply":"2024-08-07T13:50:44.909175Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Define the constants for  configutation variables","metadata":{}},{"cell_type":"code","source":"HEIGHT,WIDTH = 150,150\nBATCH_SIZE = 32","metadata":{"execution":{"iopub.status.busy":"2024-08-07T13:50:44.911558Z","iopub.execute_input":"2024-08-07T13:50:44.912026Z","iopub.status.idle":"2024-08-07T13:50:44.917302Z","shell.execute_reply.started":"2024-08-07T13:50:44.911989Z","shell.execute_reply":"2024-08-07T13:50:44.916299Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Generate images from Dataframes using the Augmentation we visualized\n\nThey create a copy of image for each augmentation generated for model traning. Please note there is no physical copy of these augmented images stored, hence does not increase space","metadata":{}},{"cell_type":"code","source":"train_datagenerator = ImageDataGenerator(rotation_range=15, \n                                  rescale=1./255, \n                                  shear_range=0.1,\n                                  zoom_range=0.2,\n                                  horizontal_flip=True,\n                                  width_shift_range=0.1,\n                                  height_shift_range=0.1)\n\ntraining_data = train_datagenerator.flow_from_dataframe(dataframe=X_train, \n                                                        directory='./train',\n                                                       x_col='filename',\n                                                       y_col='label',\n                                                       target_size=(WIDTH, HEIGHT),\n                                                       class_mode='categorical',\n                                                       batch_size=BATCH_SIZE)","metadata":{"execution":{"iopub.status.busy":"2024-08-07T13:50:44.918569Z","iopub.execute_input":"2024-08-07T13:50:44.920966Z","iopub.status.idle":"2024-08-07T13:50:45.136859Z","shell.execute_reply.started":"2024-08-07T13:50:44.920940Z","shell.execute_reply":"2024-08-07T13:50:45.135856Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_datagenerator = ImageDataGenerator(rescale=1./255)\n\ntest_data = test_datagenerator.flow_from_dataframe(dataframe=X_test, \n                                                        directory='./train',\n                                                       x_col='filename',\n                                                       y_col='label',\n                                                       target_size=(WIDTH, HEIGHT),\n                                                       class_mode='categorical',\n                                                       batch_size=BATCH_SIZE)","metadata":{"execution":{"iopub.status.busy":"2024-08-07T13:50:45.138102Z","iopub.execute_input":"2024-08-07T13:50:45.138437Z","iopub.status.idle":"2024-08-07T13:50:45.198170Z","shell.execute_reply.started":"2024-08-07T13:50:45.138412Z","shell.execute_reply":"2024-08-07T13:50:45.197196Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Types of Augmentation","metadata":{}},{"cell_type":"markdown","source":"### 1. Flip","metadata":{}},{"cell_type":"code","source":"img_gen = ImageDataGenerator(horizontal_flip=True, vertical_flip=True)\n\nbatch = img_gen.flow_from_dataframe(dataframe = X_train,directory='train/',\n                                       x_col ='filename',y_col='label',\n                                       has_ext =True,seed=42,\n                                       class_mode='binary',\n                                       target_size=(WIDTH,HEIGHT),batch_size=1)\n\nfig, ax = plt.subplots(nrows=1, ncols=3, figsize=(8,8))\n\nfor i in range(3):\n    \n    # convert to unsigned integers \n    image_batch, label_batch = next(batch)\n    image = image_batch[0].astype('uint8')\n   \n    ax[i].imshow(image)\n    ax[i].axis('off')","metadata":{"execution":{"iopub.status.busy":"2024-08-07T13:50:45.199183Z","iopub.execute_input":"2024-08-07T13:50:45.199479Z","iopub.status.idle":"2024-08-07T13:50:45.658036Z","shell.execute_reply.started":"2024-08-07T13:50:45.199453Z","shell.execute_reply":"2024-08-07T13:50:45.657060Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### 2. Rotation","metadata":{}},{"cell_type":"code","source":"img_gen = ImageDataGenerator(rotation_range=30, fill_mode='nearest')\n\nbatch = img_gen.flow_from_dataframe(dataframe = X_train,directory='train/',\n                                       x_col ='filename',y_col='label',\n                                       has_ext =True,seed=42,\n                                       class_mode='binary',\n                                       target_size=(WIDTH,HEIGHT),batch_size=1)\n\nfig, ax = plt.subplots(nrows=1, ncols=3, figsize=(8,8))\n\nfor i in range(3):\n    \n    # convert to unsigned integers \n    image_batch, label_batch = next(batch)\n    image = image_batch[0].astype('uint8')\n   \n    ax[i].imshow(image)\n    ax[i].axis('off')","metadata":{"execution":{"iopub.status.busy":"2024-08-07T13:50:45.659431Z","iopub.execute_input":"2024-08-07T13:50:45.659819Z","iopub.status.idle":"2024-08-07T13:50:46.348410Z","shell.execute_reply.started":"2024-08-07T13:50:45.659783Z","shell.execute_reply":"2024-08-07T13:50:46.347367Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### 3. Shift","metadata":{}},{"cell_type":"code","source":"img_gen = ImageDataGenerator(width_shift_range=0.2, height_shift_range=0.2)\n\nbatch = img_gen.flow_from_dataframe(dataframe = X_train,directory='train/',\n                                       x_col ='filename',y_col='label',\n                                       has_ext =True,seed=42,\n                                       class_mode='binary',\n                                       target_size=(WIDTH,HEIGHT),batch_size=1)\n\nfig, ax = plt.subplots(nrows=1, ncols=3, figsize=(8,8))\n\nfor i in range(3):\n    \n    # convert to unsigned integers \n    image_batch, label_batch = next(batch)\n    image = image_batch[0].astype('uint8')\n   \n    ax[i].imshow(image)\n    ax[i].axis('off')","metadata":{"execution":{"iopub.status.busy":"2024-08-07T13:50:46.349547Z","iopub.execute_input":"2024-08-07T13:50:46.349828Z","iopub.status.idle":"2024-08-07T13:50:46.795281Z","shell.execute_reply.started":"2024-08-07T13:50:46.349805Z","shell.execute_reply":"2024-08-07T13:50:46.794162Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### 4. Zoom","metadata":{}},{"cell_type":"code","source":"img_gen = ImageDataGenerator(zoom_range=0.2, shear_range = 0.1)\n\nbatch = img_gen.flow_from_dataframe(dataframe = X_train,directory='train/',\n                                       x_col ='filename',y_col='label',\n                                       has_ext =True,seed=42,\n                                       class_mode='binary',\n                                       target_size=(WIDTH,HEIGHT),batch_size=1)\n\nfig, ax = plt.subplots(nrows=1, ncols=3, figsize=(8,8))\n\nfor i in range(3):\n    \n    # convert to unsigned integers \n    image_batch, label_batch = next(batch)\n    image = image_batch[0].astype('uint8')\n   \n    ax[i].imshow(image)\n    ax[i].axis('off')","metadata":{"execution":{"iopub.status.busy":"2024-08-07T13:50:46.796538Z","iopub.execute_input":"2024-08-07T13:50:46.796864Z","iopub.status.idle":"2024-08-07T13:50:47.250893Z","shell.execute_reply.started":"2024-08-07T13:50:46.796839Z","shell.execute_reply":"2024-08-07T13:50:47.249873Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### 5.Brightness","metadata":{}},{"cell_type":"code","source":"img_gen = ImageDataGenerator(brightness_range=[0.4,1.5])\n\nbatch = img_gen.flow_from_dataframe(dataframe = X_train,directory='train/',\n                                       x_col ='filename',y_col='label',\n                                       has_ext =True,seed=42,\n                                       class_mode='binary',\n                                       target_size=(WIDTH,HEIGHT),batch_size=1)\n\nfig, ax = plt.subplots(nrows=1, ncols=3, figsize=(8,8))\n\nfor i in range(3):\n    \n    # convert to unsigned integers \n    image_batch, label_batch = next(batch)\n    image = image_batch[0].astype('uint8')\n   \n    ax[i].imshow(image)\n    ax[i].axis('off')","metadata":{"execution":{"iopub.status.busy":"2024-08-07T13:50:47.252072Z","iopub.execute_input":"2024-08-07T13:50:47.252386Z","iopub.status.idle":"2024-08-07T13:50:47.664596Z","shell.execute_reply.started":"2024-08-07T13:50:47.252361Z","shell.execute_reply":"2024-08-07T13:50:47.663463Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Model 1: CNN Model\n\n1. Input Layer\n2. One or more Convolution Layers\n3. Max Pooling Layer\n4. Fully Connected Layer\n5. Output Layer using Softmax ","metadata":{}},{"cell_type":"code","source":"default_layer = partial(layers.Conv2D, kernel_size=3, activation='relu', padding=\"SAME\")\n\ncnn_model = Sequential()\n\n#Input Layer\ncnn_model.add(default_layer(filters=32, kernel_size=5, input_shape=(WIDTH, HEIGHT, 3)))\ncnn_model.add(MaxPooling2D(pool_size=2))\n\n#Hidden Layer\ncnn_model.add(default_layer(filters=64))\ncnn_model.add(default_layer(filters=64))\ncnn_model.add(MaxPooling2D(pool_size=2))\n\ncnn_model.add(default_layer(filters=128))\ncnn_model.add(default_layer(filters=128))\ncnn_model.add(MaxPooling2D(pool_size=2))\n\n#Fully Connect Layer\ncnn_model.add(Flatten())\ncnn_model.add(Dense(64, activation='relu'))\ncnn_model.add(Dropout(0.2))\n\ncnn_model.add(Dense(32, activation='relu'))\ncnn_model.add(Dropout(0.2))\n\n#Output Layer\ncnn_model.add(Dense(2, activation='softmax'))\n\ncnn_model.summary()","metadata":{"execution":{"iopub.status.busy":"2024-08-07T13:50:47.666098Z","iopub.execute_input":"2024-08-07T13:50:47.666907Z","iopub.status.idle":"2024-08-07T13:50:47.916244Z","shell.execute_reply.started":"2024-08-07T13:50:47.666864Z","shell.execute_reply":"2024-08-07T13:50:47.915332Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Check points","metadata":{}},{"cell_type":"code","source":"checkpoint_filepath = '/kaggle/working/cnn_model_final.model.keras'\nearly_stoping = EarlyStopping(patience= 5,restore_best_weights=True)\ncheck_point = ModelCheckpoint(filepath=checkpoint_filepath ,monitor='val_accuracy', mode='max',save_best_only=True)","metadata":{"execution":{"iopub.status.busy":"2024-08-07T13:50:47.917888Z","iopub.execute_input":"2024-08-07T13:50:47.918432Z","iopub.status.idle":"2024-08-07T13:50:47.923444Z","shell.execute_reply.started":"2024-08-07T13:50:47.918394Z","shell.execute_reply":"2024-08-07T13:50:47.922447Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Compile","metadata":{}},{"cell_type":"code","source":"cnn_model.compile(optimizer='SGD',loss='categorical_crossentropy',metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2024-08-07T13:50:47.924994Z","iopub.execute_input":"2024-08-07T13:50:47.925316Z","iopub.status.idle":"2024-08-07T13:50:47.943571Z","shell.execute_reply.started":"2024-08-07T13:50:47.925291Z","shell.execute_reply":"2024-08-07T13:50:47.942727Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Fit","metadata":{}},{"cell_type":"code","source":"history = cnn_model.fit(training_data,epochs=15,\n                   validation_data=test_data, callbacks=[check_point,early_stoping],\n                     validation_steps=10, \n                     steps_per_epoch=10)","metadata":{"execution":{"iopub.status.busy":"2024-08-07T13:50:47.945174Z","iopub.execute_input":"2024-08-07T13:50:47.945465Z","iopub.status.idle":"2024-08-07T13:53:33.589755Z","shell.execute_reply.started":"2024-08-07T13:50:47.945441Z","shell.execute_reply":"2024-08-07T13:53:33.588797Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Checking the Saved Model","metadata":{}},{"cell_type":"code","source":"my_model = load_model(checkpoint_filepath)\nmy_model.summary()","metadata":{"execution":{"iopub.status.busy":"2024-08-07T13:53:33.591087Z","iopub.execute_input":"2024-08-07T13:53:33.591431Z","iopub.status.idle":"2024-08-07T13:53:33.828809Z","shell.execute_reply.started":"2024-08-07T13:53:33.591403Z","shell.execute_reply":"2024-08-07T13:53:33.827874Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Accuracy","metadata":{}},{"cell_type":"code","source":"loss,accuracy = cnn_model.evaluate(test_data,batch_size = BATCH_SIZE, verbose = 1)\nprint('Loss for the Model:',loss)\nprint('Accuracy for the Model:',accuracy)","metadata":{"execution":{"iopub.status.busy":"2024-08-07T13:53:33.829884Z","iopub.execute_input":"2024-08-07T13:53:33.830195Z","iopub.status.idle":"2024-08-07T13:54:44.692864Z","shell.execute_reply.started":"2024-08-07T13:53:33.830170Z","shell.execute_reply":"2024-08-07T13:54:44.691839Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Model 2: Transfer Learning using VG16","metadata":{}},{"cell_type":"markdown","source":"### Define Model","metadata":{}},{"cell_type":"code","source":"vg_model = applications.vgg16.VGG16(weights='imagenet', include_top = False, input_shape=(WIDTH,HEIGHT,3))","metadata":{"execution":{"iopub.status.busy":"2024-08-07T13:54:44.698424Z","iopub.execute_input":"2024-08-07T13:54:44.698753Z","iopub.status.idle":"2024-08-07T13:54:48.534248Z","shell.execute_reply.started":"2024-08-07T13:54:44.698727Z","shell.execute_reply":"2024-08-07T13:54:48.533390Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"vg_model.summary()","metadata":{"execution":{"iopub.status.busy":"2024-08-07T13:54:48.535380Z","iopub.execute_input":"2024-08-07T13:54:48.535681Z","iopub.status.idle":"2024-08-07T13:54:48.569032Z","shell.execute_reply.started":"2024-08-07T13:54:48.535657Z","shell.execute_reply":"2024-08-07T13:54:48.568164Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cnn_model = Sequential()\n\ncnn_model.add(vg_model)\n\ncnn_model.add(Flatten())\ncnn_model.add(Dense(128,activation='relu'))\ncnn_model.add(Dropout(0.2))\n\ncnn_model.add(Dense(2,activation='softmax'))","metadata":{"execution":{"iopub.status.busy":"2024-08-07T13:54:48.570207Z","iopub.execute_input":"2024-08-07T13:54:48.570503Z","iopub.status.idle":"2024-08-07T13:54:48.582059Z","shell.execute_reply.started":"2024-08-07T13:54:48.570477Z","shell.execute_reply":"2024-08-07T13:54:48.581279Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Callbacks","metadata":{}},{"cell_type":"code","source":"checkpoint_filepath = '/kaggle/working/vg16_model_final.model.keras'\ncheck_point = ModelCheckpoint(filepath=checkpoint_filepath ,monitor='val_accuracy', mode='max',save_best_only=True)\nearly_stoping = EarlyStopping(patience= 5,restore_best_weights=True)\nreduce_lr = ReduceLROnPlateau(monitor='val_loss', factor=0.2,patience=5, min_lr=0.001)","metadata":{"execution":{"iopub.status.busy":"2024-08-07T15:48:55.521203Z","iopub.execute_input":"2024-08-07T15:48:55.521620Z","iopub.status.idle":"2024-08-07T15:48:55.527837Z","shell.execute_reply.started":"2024-08-07T15:48:55.521590Z","shell.execute_reply":"2024-08-07T15:48:55.526615Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Compile","metadata":{}},{"cell_type":"code","source":"optimizer_final = optimizers.SGD(learning_rate=0.001, momentum=0.9)\ncnn_model.compile(optimizer=optimizer_final,loss='categorical_crossentropy',metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2024-08-07T15:48:57.458346Z","iopub.execute_input":"2024-08-07T15:48:57.459156Z","iopub.status.idle":"2024-08-07T15:48:57.469812Z","shell.execute_reply.started":"2024-08-07T15:48:57.459100Z","shell.execute_reply":"2024-08-07T15:48:57.468842Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Fit","metadata":{}},{"cell_type":"code","source":"histroy = cnn_model.fit(training_data,epochs=30,validation_data=test_data, \n                        validation_steps=10,\n                        steps_per_epoch=10, \n                        callbacks=[early_stoping,check_point,reduce_lr],\n#                        validation_steps = X_test.shape[0]//BATCH_SIZE,\n#                        steps_per_epoch = X_train.shape[0]//BATCH_SIZE\n                       )","metadata":{"execution":{"iopub.status.busy":"2024-08-07T15:48:59.752676Z","iopub.execute_input":"2024-08-07T15:48:59.753605Z","iopub.status.idle":"2024-08-07T15:51:36.696842Z","shell.execute_reply.started":"2024-08-07T15:48:59.753570Z","shell.execute_reply":"2024-08-07T15:51:36.695706Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"loss,accuracy = cnn_model.evaluate(test_data,steps = len(test_data) ,verbose = 1)\nprint('Loss for the Model:',loss)\nprint('Accuracy for the Model:',accuracy)","metadata":{"execution":{"iopub.status.busy":"2024-08-07T15:47:35.172882Z","iopub.execute_input":"2024-08-07T15:47:35.173299Z","iopub.status.idle":"2024-08-07T15:47:35.177488Z","shell.execute_reply.started":"2024-08-07T15:47:35.173267Z","shell.execute_reply":"2024-08-07T15:47:35.176508Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"acc = history.history['accuracy']\nval_acc = history.history['val_accuracy']\nloss = history.history['loss']\nval_loss = history.history['val_loss']\n\nepochs = range(len(acc))\n\nplt.figure(figsize=(15,5))\nplt.subplot(1,2,1)\nplt.plot(epochs, acc, 'g', label='Training accuracy')\nplt.plot(epochs, val_acc, 'b', label='Validation accuracy')\nplt.title('Training and validation accuracy')\nplt.xlabel('Epoch')\nplt.ylabel('Accuracy')\nplt.legend()\n\n\nplt.subplot(1,2,2)\nplt.plot(epochs, loss, 'g', label='Training Loss')\nplt.plot(epochs, val_loss, 'b', label='Validation Loss')\nplt.title('Training and validation loss')\nplt.xlabel('Epoch')\nplt.ylabel('Loss')\nplt.legend()\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-08-07T16:13:28.560243Z","iopub.execute_input":"2024-08-07T16:13:28.561055Z","iopub.status.idle":"2024-08-07T16:13:29.071385Z","shell.execute_reply.started":"2024-08-07T16:13:28.561017Z","shell.execute_reply":"2024-08-07T16:13:29.070164Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Prediction","metadata":{}},{"cell_type":"code","source":"pred = cnn_model.predict(test_data,verbose = 1)\ny_pred = np.argmax(pred,axis=1)","metadata":{"execution":{"iopub.status.busy":"2024-08-07T16:13:51.227310Z","iopub.execute_input":"2024-08-07T16:13:51.227692Z","iopub.status.idle":"2024-08-07T16:22:32.955269Z","shell.execute_reply.started":"2024-08-07T16:13:51.227666Z","shell.execute_reply":"2024-08-07T16:22:32.953954Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_true= test_data.labels","metadata":{"execution":{"iopub.status.busy":"2024-08-07T16:26:19.148093Z","iopub.execute_input":"2024-08-07T16:26:19.148892Z","iopub.status.idle":"2024-08-07T16:26:19.153447Z","shell.execute_reply.started":"2024-08-07T16:26:19.148858Z","shell.execute_reply":"2024-08-07T16:26:19.152219Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels = ['Cat','Dog']\nprint(classification_report(y_true,y_pred,target_names= labels))","metadata":{"execution":{"iopub.status.busy":"2024-08-07T16:26:22.532005Z","iopub.execute_input":"2024-08-07T16:26:22.532521Z","iopub.status.idle":"2024-08-07T16:26:22.557729Z","shell.execute_reply.started":"2024-08-07T16:26:22.532482Z","shell.execute_reply":"2024-08-07T16:26:22.556768Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"confusion_mtx = confusion_matrix(y_true,y_pred) \n\nfig,ax = plt.subplots(figsize = (8,4) )\nsns.heatmap(confusion_mtx, annot=True, linewidths=0.1, cmap = \"cividis\",\n            linecolor=\"black\", fmt='.0f', ax=ax,cbar=False, xticklabels=labels, yticklabels=labels)\n\nplt.xlabel(\"Predicted Label\",fontsize=10)\nplt.ylabel(\"True Label\",fontsize=10)\nplt.title(\"Confusion Matrix\",fontsize=13)\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-08-07T16:27:08.517029Z","iopub.execute_input":"2024-08-07T16:27:08.517961Z","iopub.status.idle":"2024-08-07T16:27:08.646785Z","shell.execute_reply.started":"2024-08-07T16:27:08.517929Z","shell.execute_reply":"2024-08-07T16:27:08.645540Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Prediction Test Data using saved Model","metadata":{}},{"cell_type":"code","source":"test_datagenerator = ImageDataGenerator(rescale=1./255)\n\ntest_data = test_datagenerator.flow_from_dataframe(dataframe=df_test, \n                                                        directory='./test1',\n                                                       x_col='filename',\n                                                       y_col='label',\n                                                       target_size=(WIDTH, HEIGHT),\n                                                       class_mode='categorical',\n                                                       batch_size=1)","metadata":{"execution":{"iopub.status.busy":"2024-08-07T16:27:23.456726Z","iopub.execute_input":"2024-08-07T16:27:23.457499Z","iopub.status.idle":"2024-08-07T16:27:23.644718Z","shell.execute_reply.started":"2024-08-07T16:27:23.457464Z","shell.execute_reply":"2024-08-07T16:27:23.643607Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred = cnn_model.predict(test_data)\ny_pred = np.argmax(pred,axis=1)","metadata":{"execution":{"iopub.status.busy":"2024-08-07T16:27:35.992441Z","iopub.execute_input":"2024-08-07T16:27:35.992817Z","iopub.status.idle":"2024-08-07T16:50:47.624205Z","shell.execute_reply.started":"2024-08-07T16:27:35.992788Z","shell.execute_reply":"2024-08-07T16:50:47.623020Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(y_pred)","metadata":{"execution":{"iopub.status.busy":"2024-08-07T16:53:17.171758Z","iopub.execute_input":"2024-08-07T16:53:17.172214Z","iopub.status.idle":"2024-08-07T16:53:17.178769Z","shell.execute_reply.started":"2024-08-07T16:53:17.172182Z","shell.execute_reply":"2024-08-07T16:53:17.177631Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub_file = pd.read_csv('../input/dogs-vs-cats/sampleSubmission.csv')\nfinal_result = sub_file.copy()\nfinal_result['id'] = final_result['id'].apply(lambda x : str(x)+'.jpg')","metadata":{"execution":{"iopub.status.busy":"2024-08-07T16:53:19.246711Z","iopub.execute_input":"2024-08-07T16:53:19.247096Z","iopub.status.idle":"2024-08-07T16:53:19.276078Z","shell.execute_reply.started":"2024-08-07T16:53:19.247065Z","shell.execute_reply":"2024-08-07T16:53:19.274969Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub_file['label'] = y_pred\nsub_file['label'] = sub_file['label'].replace({1:'dog',0:'cat'})\nsub_file.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2024-08-07T16:53:21.060834Z","iopub.execute_input":"2024-08-07T16:53:21.061990Z","iopub.status.idle":"2024-08-07T16:53:21.081990Z","shell.execute_reply.started":"2024-08-07T16:53:21.061951Z","shell.execute_reply":"2024-08-07T16:53:21.081172Z"},"trusted":true},"execution_count":null,"outputs":[]}]}