{"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":"nvidiaTeslaT4","dataSources":[{"sourceId":3362,"databundleVersionId":31148,"sourceType":"competition"}],"dockerImageVersionId":30698,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\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":"2024-05-14T10:39:06.855973Z","iopub.execute_input":"2024-05-14T10:39:06.856857Z","iopub.status.idle":"2024-05-14T10:39:07.239472Z","shell.execute_reply.started":"2024-05-14T10:39:06.856821Z","shell.execute_reply":"2024-05-14T10:39:07.238593Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# basic librareis\nimport zipfile\nimport glob\nimport os\nimport pandas as pd\nimport numpy as np\nfrom PIL import Image\n\n# visualization libraries\nimport matplotlib\nfrom matplotlib import pyplot as plt\nimport seaborn as sns\npd.options.mode.chained_assignment = None\n\n# preprocessing\n\nfrom sklearn.model_selection import train_test_split\n\n# metrics\nfrom sklearn.metrics import (confusion_matrix, \n                             classification_report, \n                             accuracy_score, \n                             f1_score, \n                             roc_auc_score)\n# modeling\nimport tensorflow as tf\nfrom keras.models import Model,Sequential\nfrom keras.layers import Conv2D, MaxPooling2D, Dropout, Dense,Flatten\n\nfrom keras.applications import resnet50\nfrom keras.applications.resnet50 import preprocess_input\nfrom keras import optimizers\nfrom keras import regularizers\nfrom keras.callbacks import EarlyStopping,LearningRateScheduler\n\nfrom keras.preprocessing.image import load_img\nfrom sklearn.model_selection import train_test_split\nfrom keras.src.legacy.preprocessing.image import ImageDataGenerator\n\nfrom keras import backend as K\nK.clear_session()\n\n# model plotting\nfrom IPython.display import SVG\n#from keras.utils.vis_utils import model_to_dot\nfrom keras.utils import plot_model\n\n# mesc\nfrom sklearn.utils import shuffle","metadata":{"execution":{"iopub.status.busy":"2024-05-14T10:39:21.156338Z","iopub.execute_input":"2024-05-14T10:39:21.156854Z","iopub.status.idle":"2024-05-14T10:39:21.529201Z","shell.execute_reply.started":"2024-05-14T10:39:21.156827Z","shell.execute_reply":"2024-05-14T10:39:21.528229Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Set colors\nsns.set_palette([\"#1E90FF\",\"#C0C0C0\"])\ncustom_cmap = matplotlib.colors.LinearSegmentedColormap.from_list(\"\",[\"#1E90FF\",\"#C0C0C0\"])\ncolor = '#000080'","metadata":{"execution":{"iopub.status.busy":"2024-05-14T10:39:21.531316Z","iopub.execute_input":"2024-05-14T10:39:21.531630Z","iopub.status.idle":"2024-05-14T10:39:21.537050Z","shell.execute_reply.started":"2024-05-14T10:39:21.531588Z","shell.execute_reply":"2024-05-14T10:39:21.536198Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"\n<h2 style=\"background-color: #1E90FF; padding:15px;border-radius:10px\" align=\"left\"><font color= #FFF >Problem Definition:</font></h2>\n    \nThe Dogs & Cats is a foundational problem for a basic CNN(convolutional neural network) model which involves classifying images as a dog or a cat.The dataset can be used for learning how to develop,evaluate and use convolutional deep learning neural networks for classification of images. This includes how to develop a robust test harness for estimating the performance of the model, exploring improvements for the model by changing the paramters of the model, saving and loading the model to make predicitions on new data.                    \n","metadata":{}},{"cell_type":"markdown","source":"<h2 style=\"background-color: #1E90FF; padding:15px;border-radius:10px\" align=\"left\"><font color= #FFF >Dataset Description:</font></h2>\nThe training archive contains 25,000 images of dogs and cats.\n\nTrain your algorithm on these files and predict the labels for test1.zip.\n\n* **1 = dog**\n* **0 = cat**","metadata":{}},{"cell_type":"markdown","source":"<h2 style=\"background-color: #1E90FF; padding:15px;border-radius:10px\" align=\"left\"><font color= #FFF >Dataset Retrieval:</font></h2>","metadata":{}},{"cell_type":"code","source":"# extraction of train and test data from zipfiles to data folder\nprint('Extracting the data from dataset.....'+'\\n')\nzip_files = glob.glob('/kaggle/input/dogs-vs-cats/*.zip')\n\nprint('{} files found in the input directory'.format(str(len(zip_files))) +'\\n')\nfor file in zip_files:\n    with zipfile.ZipFile(file, 'r') as Z:\n        Z.extractall('data')\n    print ('{} is extracted'.format(file.split('/')[-1]) + '\\n')\n      \nprint('Extraction is completed'+'\\n')","metadata":{"execution":{"iopub.status.busy":"2024-05-14T10:39:21.537974Z","iopub.execute_input":"2024-05-14T10:39:21.538227Z","iopub.status.idle":"2024-05-14T10:39:36.100050Z","shell.execute_reply.started":"2024-05-14T10:39:21.538199Z","shell.execute_reply":"2024-05-14T10:39:36.099102Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Total number of images in train and test datasets\ntrain_dir = '/kaggle/working/data/train/'\ntest_dir  = '/kaggle/working/data/test1/'\nprint(\"Total Images in Train, and Test Data...\")\nprint('\\n' + 'No. of Train Images: '+ str(len(os.listdir(train_dir))))\nprint('No. of Test Images: '+ str(len(os.listdir(test_dir)))+ '\\n')  ","metadata":{"execution":{"iopub.status.busy":"2024-05-14T10:39:36.101157Z","iopub.execute_input":"2024-05-14T10:39:36.101431Z","iopub.status.idle":"2024-05-14T10:39:36.128558Z","shell.execute_reply.started":"2024-05-14T10:39:36.101408Z","shell.execute_reply":"2024-05-14T10:39:36.127729Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**NOTE** :\n\\We not gonna use test","metadata":{}},{"cell_type":"code","source":"# category and filepath extraction helper functions\n\ndef category(path): \n    return [file.split('.')[0] for file in os.listdir(path)]\n\ndef filename(path):\n    return [file for file in os.listdir(path)]\n\n# image names and labels\nx_train_imgname = filename(train_dir) \nx_test_imgname = filename(test_dir)\ny_train_label = category(train_dir)\n\n#creation of total dataframe and submission dataframe\nprint('Image data is storing into dataframes...'+ '\\n')\ntrain_image_df = pd.DataFrame({ 'filename': x_train_imgname, 'category': y_train_label})\n\nprint('Training image names and labels are read to train_image_df'+ '\\n')","metadata":{"execution":{"iopub.status.busy":"2024-05-14T10:39:36.129712Z","iopub.execute_input":"2024-05-14T10:39:36.129970Z","iopub.status.idle":"2024-05-14T10:39:36.184727Z","shell.execute_reply.started":"2024-05-14T10:39:36.129948Z","shell.execute_reply":"2024-05-14T10:39:36.183931Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_image_df.head()","metadata":{"execution":{"iopub.status.busy":"2024-05-14T10:39:36.185742Z","iopub.execute_input":"2024-05-14T10:39:36.185994Z","iopub.status.idle":"2024-05-14T10:39:36.206520Z","shell.execute_reply.started":"2024-05-14T10:39:36.185973Z","shell.execute_reply":"2024-05-14T10:39:36.205661Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<h2 style=\"background-color: #1E90FF; padding:15px;border-radius:10px\" align=\"left\"><font color= #FFF >Images extraction and Image visualizations:</font></h2>","metadata":{}},{"cell_type":"code","source":"print('Helper funtions for image path,id, category data extractions and Image visualizations' +'\\n')\n# helper functions \n\ndef img_path(directory):\n    ''' \n    This function extracts image ids, category,and image paths from directory.\n    input:\n    directiory: Path to location of images\n    Return:\n    ID_no: list of image ids\n    Paths: list of image paths\n    cate: list of category\n    '''\n    paths = []\n    cate = []\n    ID_no = []\n    for file in os.listdir(directory):\n        path = os.path.join(directory, file)\n        paths.append(path)\n        cate.append(file.split('.')[0])\n        ID_no.append(file.split('.')[1])\n    return ID_no, paths, cate\n\n\n\n\ndef showImages(data,num_row  = 3,num_col =  3, name = 'any', subtitle = 'off'):\n    \"\"\" This function creates a grid of images from  dataset.\n    Shuffled images will be displayed.\n    \n    Input: \n    num_row: default: 3, no. of rows in a grid\n    num_col: default:3, no. of columns in grid\n    data: Dataframe of paths \n    name:  default 'any', takes: cat, dog, any or something else would give both\n    subtitle: display id number for each image, defalult: 'off', takes: 'on' and 'off'\n    Return: None\n    \n    \"\"\"\n    \n    # little data sorting\n    cat_df,dog_df = data[data['Category'] == 'cat'], data[data['Category'] == 'dog']\n\n    \n    if name == 'dog':\n        X, Y  = dog_df['img_paths'], dog_df['ID_no']\n    elif name == 'cat':\n        X, Y  = cat_df['img_paths'], cat_df['ID_no']     \n    else:\n        X, Y  = data['img_paths'], data['ID_no']     # could use try and except but lets stick to minimal code\n\n    (X_rand, Y_rand) = shuffle(X, Y)\n    \n    # showing images on matplotlib \n    \n    fig, ax = plt.subplots(num_row,num_col,figsize = (12,12), dpi = 100)\n    axes = ax.ravel()\n    \n    for idx,ax  in enumerate(axes):\n        x = load_img(X_rand.iloc[idx],target_size= (125, 125))\n        ax.imshow(x)\n        if subtitle == 'on':\n            ax.set_title(\"{}\".format(Y_rand.iloc[idx]))\n        else:\n            ax.set_title('')\n        ax.axis('off')\n        plt.subplots_adjust(wspace =0)\n        del x\n    #fig.tight_layout()\n    \n    fig.text(0.1,0.93, 'CAT OR DOG?: {}s examples from Training Data'.format(name.capitalize()),{'fontfamily':'serif','size':18,'weight':'bold'})\n    \n    return None","metadata":{"execution":{"iopub.status.busy":"2024-05-14T10:39:36.207679Z","iopub.execute_input":"2024-05-14T10:39:36.207998Z","iopub.status.idle":"2024-05-14T10:39:36.221933Z","shell.execute_reply.started":"2024-05-14T10:39:36.207967Z","shell.execute_reply":"2024-05-14T10:39:36.221052Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Implementing above function over train data set\nID_no, img_paths, train_images = img_path(train_dir)\n\nprint('Dataframe is creating for training image visualization in a grid...'+'\\n' )\n#creating new dataframe for data visulaization\n\nvisual_df = pd.DataFrame({'ID_no':ID_no,'Category':train_images, 'img_paths': img_paths})\n\nprint(visual_df.head(5))\n\nprint('Done!')","metadata":{"execution":{"iopub.status.busy":"2024-05-14T10:39:36.224678Z","iopub.execute_input":"2024-05-14T10:39:36.224935Z","iopub.status.idle":"2024-05-14T10:39:36.323504Z","shell.execute_reply.started":"2024-05-14T10:39:36.224914Z","shell.execute_reply":"2024-05-14T10:39:36.322671Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"showImages(visual_df,5,5, name = 'dog', subtitle = 'off')","metadata":{"execution":{"iopub.status.busy":"2024-05-14T10:39:36.324452Z","iopub.execute_input":"2024-05-14T10:39:36.324769Z","iopub.status.idle":"2024-05-14T10:39:37.893495Z","shell.execute_reply.started":"2024-05-14T10:39:36.324737Z","shell.execute_reply":"2024-05-14T10:39:37.892447Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"showImages(visual_df,5,5, name = 'cat', subtitle = 'off')","metadata":{"execution":{"iopub.status.busy":"2024-05-14T10:39:37.895067Z","iopub.execute_input":"2024-05-14T10:39:37.895377Z","iopub.status.idle":"2024-05-14T10:39:39.632433Z","shell.execute_reply.started":"2024-05-14T10:39:37.895350Z","shell.execute_reply":"2024-05-14T10:39:39.631306Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"showImages(visual_df,5,5, name = 'Dogs and Cat', subtitle = 'off')","metadata":{"execution":{"iopub.status.busy":"2024-05-14T10:39:39.633677Z","iopub.execute_input":"2024-05-14T10:39:39.633966Z","iopub.status.idle":"2024-05-14T10:39:41.093379Z","shell.execute_reply.started":"2024-05-14T10:39:39.633942Z","shell.execute_reply":"2024-05-14T10:39:41.092333Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<h2 style=\"background-color: #1E90FF; padding:15px;border-radius:10px\" align=\"left\"><font color= #FFF >Spliting data:</font></h2>","metadata":{}},{"cell_type":"code","source":"train_image_df.head()","metadata":{"execution":{"iopub.status.busy":"2024-05-14T11:29:05.587224Z","iopub.execute_input":"2024-05-14T11:29:05.588017Z","iopub.status.idle":"2024-05-14T11:29:05.596893Z","shell.execute_reply.started":"2024-05-14T11:29:05.587983Z","shell.execute_reply":"2024-05-14T11:29:05.595923Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Data split into train data and validation data\ntrain_valid_df, test_df = train_test_split(train_image_df, test_size = 0.04)\ntrain_df, valid_df = train_test_split(train_valid_df, test_size = 0.2)\n\ntrain_images = train_df.shape[0]\nvalid_images = valid_df.shape[0]\nholdon_images = test_df.shape[0]\n\nprint('Number of Training Images: ' + str(train_images))\nprint('Number of Validating Images: ' + str(valid_images))\nprint('Number of testing Images: ' +str(holdon_images))","metadata":{"execution":{"iopub.status.busy":"2024-05-14T10:40:58.709723Z","iopub.execute_input":"2024-05-14T10:40:58.710116Z","iopub.status.idle":"2024-05-14T10:40:58.725061Z","shell.execute_reply.started":"2024-05-14T10:40:58.710085Z","shell.execute_reply":"2024-05-14T10:40:58.724013Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<h2 style=\"background-color: #1E90FF; padding:15px;border-radius:10px\" align=\"left\"><font color= #FFF >Image Augmentation:</font></h2>\n\n**Image augmentation** is a technique used in computer vision to artificially expand training datasets by applying transformations like rotations, flips, or scaling to original images. This helps **improve model generalization** by exposing it to variations in data, reducing overfitting, and enhancing performance in real-world scenarios.","metadata":{}},{"cell_type":"code","source":"img_size = 224\nbatch_size = 128","metadata":{"execution":{"iopub.status.busy":"2024-05-14T10:39:41.113834Z","iopub.execute_input":"2024-05-14T10:39:41.114124Z","iopub.status.idle":"2024-05-14T10:39:41.118558Z","shell.execute_reply.started":"2024-05-14T10:39:41.114101Z","shell.execute_reply":"2024-05-14T10:39:41.117678Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Trying image augmentation\nprint('Helper funtions for Image Agumentation visualizations')\n\ndef data_argumentation_show(n, grid_size):\n    sample_aug_map = ImageDataGenerator(\n            rotation_range = 25,\n            horizontal_flip = True,\n            height_shift_range =0.2,\n            width_shift_range = 0.2,\n            fill_mode='nearest',\n            rescale = 1/255)\n    sample_data = sample_aug_map.flow_from_dataframe(\n            (train_df.sample(n)),\n            train_dir,\n            x_col = 'filename',\n            y_col = 'category',\n            target_size = (img_size, img_size),\n            class_mode = 'categorical')\n  \n  #subplot grid \n    fig = plt.figure(figsize = (10,10))\n    for i in range(0,grid_size*grid_size):\n        plt.subplot(grid_size,grid_size, i+1)\n        for x,y in sample_data:\n            img = x[0]\n            plt.imshow(img)\n            plt.axis('off')\n            break\n            plt.tight_layout()\n            del img\n    fig.show()\n\n     \n    fig.text(0.1,0.93, 'CAT OR DOG?: image Augmentation sample examples'.format(),{'fontfamily':'serif','size':18,'weight':'bold'})\n    \n    return None","metadata":{"execution":{"iopub.status.busy":"2024-05-14T10:46:03.019850Z","iopub.execute_input":"2024-05-14T10:46:03.020546Z","iopub.status.idle":"2024-05-14T10:46:03.029372Z","shell.execute_reply.started":"2024-05-14T10:46:03.020517Z","shell.execute_reply":"2024-05-14T10:46:03.028457Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# To visulalize the effect of data argumentation \n#select number of samples to argument----> n = \n# total number of argumentation is grid_Size**2\n\ndata_argumentation_show(1, 5)","metadata":{"execution":{"iopub.status.busy":"2024-05-14T11:37:47.734226Z","iopub.execute_input":"2024-05-14T11:37:47.734666Z","iopub.status.idle":"2024-05-14T11:37:49.675212Z","shell.execute_reply.started":"2024-05-14T11:37:47.734598Z","shell.execute_reply":"2024-05-14T11:37:49.674285Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_argumentation_show(2, 5)","metadata":{"execution":{"iopub.status.busy":"2024-05-14T10:46:10.803764Z","iopub.execute_input":"2024-05-14T10:46:10.804388Z","iopub.status.idle":"2024-05-14T10:46:12.614647Z","shell.execute_reply.started":"2024-05-14T10:46:10.804360Z","shell.execute_reply":"2024-05-14T10:46:12.613754Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# adding augmented images to train images\n# res_train_data  = augmented Images + train Images\ntrain_aug_map = ImageDataGenerator(\n                    rotation_range=10,\n                    horizontal_flip=True,\n                    fill_mode='nearest',\n                    preprocessing_function = preprocess_input)\n\nres_train_data = train_aug_map.flow_from_dataframe(\n            train_df, train_dir,\n            x_col = 'filename',\n            y_col = 'category',\n            target_size = (img_size, img_size),\n            batch_size = batch_size,\n            class_mode = 'categorical')\n\n#one should validate the generality of model on the actcual target images\n#so not supposed agumentation\nvalid_aug_map = ImageDataGenerator(preprocessing_function = preprocess_input)\n\nres_valid_data = valid_aug_map.flow_from_dataframe(\n             valid_df, train_dir,\n             x_col = 'filename',\n             y_col = 'category',\n             target_size = (img_size, img_size),\n             batch_size = batch_size,\n             class_mode = 'categorical')\n\n#test data rescaling images\n\ntest_aug_map = ImageDataGenerator(preprocessing_function = preprocess_input)\n\nres_test_data = test_aug_map.flow_from_dataframe(\n             test_df, train_dir,\n             x_col = 'filename',\n             y_col = None,\n             class_mode = None,\n             target_size = (img_size, img_size),\n             shuffle = False)","metadata":{"execution":{"iopub.status.busy":"2024-05-14T10:50:53.924094Z","iopub.execute_input":"2024-05-14T10:50:53.924424Z","iopub.status.idle":"2024-05-14T10:50:54.229273Z","shell.execute_reply.started":"2024-05-14T10:50:53.924398Z","shell.execute_reply":"2024-05-14T10:50:54.228371Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Setting learning rate schedule to start with large steps and end with small steps\nprint('\\nSetting a decay learning rate for learning rate schedule\\n')\nepoch = 50\nlearning_rate = 3e-5 \nlr_start = 0.00000001\nlr_min = 0.000001\nlr_max = 3e-5 \nlr_rampup_epochs = 1\nlr_sustain_epochs = 1\nlr_exp_decay = .8\n\ndef lrfn(epoch):\n    if epoch < lr_rampup_epochs:\n        lr = (lr_max - lr_start) / lr_rampup_epochs * epoch + lr_start\n    elif epoch < lr_rampup_epochs + lr_sustain_epochs:\n        lr = lr_max\n    else:\n        lr = (lr_max - lr_min) * lr_exp_decay**(epoch - lr_rampup_epochs - lr_sustain_epochs) + lr_min\n    return lr\n    \nprint(\"Done!\")","metadata":{"execution":{"iopub.status.busy":"2024-05-14T10:47:04.825914Z","iopub.execute_input":"2024-05-14T10:47:04.826546Z","iopub.status.idle":"2024-05-14T10:47:04.833484Z","shell.execute_reply.started":"2024-05-14T10:47:04.826515Z","shell.execute_reply":"2024-05-14T10:47:04.832655Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"base_model = tf.keras.applications.ResNet50(weights='imagenet', include_top=False, input_shape=(224, 224, 3))\n\n# Freezing layers\nbase_model.trainable = False\n\n# Adding custom classifier on top of ResNet50\n# to make ResNet 50 to predict cats and dogs only\nflatten_layer = tf.keras.layers.Flatten()\ndense_layer = tf.keras.layers.Dense(1024, activation='relu')\ndropout_layer = tf.keras.layers.Dropout(0.4)\n# last layer contatins only two neurons to predict cats or dogs\noutput_layer = tf.keras.layers.Dense(2, activation='softmax')\n\ninputs = tf.keras.Input(shape=(224, 224, 3))\nx = base_model(inputs, training=False)\nx = flatten_layer(x)\nx = dense_layer(x)\nx = dropout_layer(x)\noutputs = output_layer(x)\n\n# Creating model\nresNet_model = tf.keras.Model(inputs, outputs)\n\n# Compiling model\noptimizer = optimizers.Adam(learning_rate=1e-5)\nprint('\\nResNet based Transfer learning Model layers and output shapes with params...\\n')\n\nresNet_model.summary()","metadata":{"execution":{"iopub.status.busy":"2024-05-14T10:54:32.782144Z","iopub.execute_input":"2024-05-14T10:54:32.782538Z","iopub.status.idle":"2024-05-14T10:54:35.949376Z","shell.execute_reply.started":"2024-05-14T10:54:32.782510Z","shell.execute_reply":"2024-05-14T10:54:35.948489Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('\\nSetting early stopping factor and learning rate schedule\\n')\n\nearlystop = EarlyStopping(patience= 5)\n    \nlr_callback = LearningRateScheduler(lrfn, verbose = True)\n\ncallbacks = [earlystop, lr_callback]\nprint('Done!')","metadata":{"execution":{"iopub.status.busy":"2024-05-14T10:54:35.951110Z","iopub.execute_input":"2024-05-14T10:54:35.951780Z","iopub.status.idle":"2024-05-14T10:54:35.956692Z","shell.execute_reply.started":"2024-05-14T10:54:35.951747Z","shell.execute_reply":"2024-05-14T10:54:35.955782Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"resNet_model.compile(optimizer = optimizer,\n             loss = 'categorical_crossentropy',\n             metrics = ['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2024-05-14T10:54:39.506584Z","iopub.execute_input":"2024-05-14T10:54:39.506939Z","iopub.status.idle":"2024-05-14T10:54:39.515784Z","shell.execute_reply.started":"2024-05-14T10:54:39.506911Z","shell.execute_reply":"2024-05-14T10:54:39.514956Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('Training on ResNet50 has started ....\\n')\n\nresnet_history = resNet_model.fit(res_train_data, epochs = 15,\n                          validation_data = res_valid_data,\n                          validation_steps= 2,\n                          steps_per_epoch= 2,\n                          callbacks = callbacks)\nprint('Done!\\n')","metadata":{"execution":{"iopub.status.busy":"2024-05-14T12:21:40.371462Z","iopub.execute_input":"2024-05-14T12:21:40.371870Z","iopub.status.idle":"2024-05-14T12:22:32.404658Z","shell.execute_reply.started":"2024-05-14T12:21:40.371841Z","shell.execute_reply":"2024-05-14T12:22:32.403688Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Plot training & validation loss values\nplt.figure(figsize=(12, 4))\n\nplt.subplot(1, 2, 1)\nplt.plot(resnet_history.history['loss'], label='Train Loss')\nplt.plot(resnet_history.history['val_loss'], label='Validation Loss')\nplt.title('Model Loss')\nplt.xlabel('Epoch')\nplt.ylabel('Loss')\nplt.legend(loc='upper right')\n\n# Plot training & validation accuracy values\nplt.subplot(1, 2, 2)\nplt.plot(resnet_history.history['accuracy'], label='Train Accuracy')\nplt.plot(resnet_history.history['val_accuracy'], label='Validation Accuracy')\nplt.title('Model Accuracy')\nplt.xlabel('Epoch')\nplt.ylabel('Accuracy')\nplt.legend(loc='lower right')\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-05-14T12:24:06.301259Z","iopub.execute_input":"2024-05-14T12:24:06.302025Z","iopub.status.idle":"2024-05-14T12:24:06.716636Z","shell.execute_reply.started":"2024-05-14T12:24:06.301995Z","shell.execute_reply":"2024-05-14T12:24:06.715675Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Predicting test dataset\nres_pred = resNet_model.predict(res_test_data)\ntest_df['res_pred'] = np.argmax(res_pred, axis = -1)\nlabels = dict((v,k) for k,v in res_train_data.class_indices.items())\n\ntest_df['res_pred'] = test_df['res_pred'].map(labels)","metadata":{"execution":{"iopub.status.busy":"2024-05-14T10:56:31.263259Z","iopub.execute_input":"2024-05-14T10:56:31.264197Z","iopub.status.idle":"2024-05-14T10:56:50.561847Z","shell.execute_reply.started":"2024-05-14T10:56:31.264147Z","shell.execute_reply":"2024-05-14T10:56:50.560810Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df.head()","metadata":{"execution":{"iopub.status.busy":"2024-05-14T12:15:21.773093Z","iopub.execute_input":"2024-05-14T12:15:21.774009Z","iopub.status.idle":"2024-05-14T12:15:21.783321Z","shell.execute_reply.started":"2024-05-14T12:15:21.773976Z","shell.execute_reply":"2024-05-14T12:15:21.782339Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"confusion_mtx = confusion_matrix(test_df['category'],test_df['res_pred'])\n\nlabels = [ 'True Neg','False Pos','False Neg','True Pos']\ncategories = ['Cat', 'Dog']\n\nf,ax = plt.subplots(figsize = (4,4),dpi=200)\nsns.heatmap(confusion_mtx,\n            annot=True,\n            linewidths=0.1,\n            cmap= custom_cmap,\n            fmt='.0f',\n            ax=ax,cbar=False,\n            xticklabels=categories,\n            yticklabels=categories)\n\n# Adding custom text annotations\nfor i, label in enumerate(labels):\n    ax.text(i % 2 + 0.5, i // 2 + 0.3, label, ha='center', va='center', fontsize=8)\n    \nplt.xlabel(\"Predicted Label\",fontsize=10)\nplt.ylabel(\"True Label\",fontsize=10)\nplt.title(\"Confusion Matrix For Test Data\",fontsize=13)\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-05-14T10:56:50.563739Z","iopub.execute_input":"2024-05-14T10:56:50.564329Z","iopub.status.idle":"2024-05-14T10:56:50.796236Z","shell.execute_reply.started":"2024-05-14T10:56:50.564293Z","shell.execute_reply":"2024-05-14T10:56:50.794657Z"},"trusted":true},"execution_count":null,"outputs":[]}]}