{"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.6","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"gpu","dataSources":[{"sourceId":5441,"databundleVersionId":38425,"sourceType":"competition"},{"sourceId":9900,"sourceType":"datasetVersion","datasetId":6209}],"dockerImageVersionId":11543,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import os, cv2, random\nimport numpy as np\nimport pandas as pd\nimport seaborn as sns\nimport matplotlib.pyplot as plt\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import classification_report\nfrom tqdm import tqdm\nfrom random import shuffle \nfrom IPython.display import SVG\nfrom keras.utils.vis_utils import model_to_dot\nfrom keras.utils import plot_model\nfrom tensorflow.python.keras.applications import ResNet50\nfrom tensorflow.python.keras.models import Sequential\nfrom tensorflow.python.keras.layers import Dense, Flatten, GlobalAveragePooling2D\n%matplotlib inline \n\n\n# Constants\nTEST_SIZE = 0.5\nRANDOM_STATE = 2018\nBATCH_SIZE = 64\nNO_EPOCHS = 5\nNUM_CLASSES = 2\nSAMPLE_SIZE = 20000\nIMG_SIZE = 224\nTRAIN_FOLDER = './train/'\nTEST_FOLDER = './test/'","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-05-22T09:14:55.640797Z","iopub.execute_input":"2024-05-22T09:14:55.641078Z","iopub.status.idle":"2024-05-22T09:14:57.024828Z","shell.execute_reply.started":"2024-05-22T09:14:55.641027Z","shell.execute_reply":"2024-05-22T09:14:57.023971Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"PATH = '/kaggle/input/dogs-vs-cats-redux-kernels-edition/'\ntrain_image_path = os.path.join(PATH, \"train.zip\")\ntest_image_path = os.path.join(PATH, \"test.zip\")","metadata":{"execution":{"iopub.status.busy":"2024-05-22T09:14:57.026673Z","iopub.execute_input":"2024-05-22T09:14:57.026980Z","iopub.status.idle":"2024-05-22T09:14:57.032037Z","shell.execute_reply.started":"2024-05-22T09:14:57.026916Z","shell.execute_reply":"2024-05-22T09:14:57.030995Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import zipfile\nwith zipfile.ZipFile(train_image_path,\"r\") as z:\n    z.extractall(\".\")","metadata":{"execution":{"iopub.status.busy":"2024-05-22T09:14:57.033390Z","iopub.execute_input":"2024-05-22T09:14:57.033671Z","iopub.status.idle":"2024-05-22T09:15:07.660563Z","shell.execute_reply.started":"2024-05-22T09:14:57.033617Z","shell.execute_reply":"2024-05-22T09:15:07.659899Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"with zipfile.ZipFile(test_image_path,\"r\") as z:\n    z.extractall(\".\")","metadata":{"execution":{"iopub.status.busy":"2024-05-22T09:15:07.662486Z","iopub.execute_input":"2024-05-22T09:15:07.662788Z","iopub.status.idle":"2024-05-22T09:15:13.054728Z","shell.execute_reply.started":"2024-05-22T09:15:07.662730Z","shell.execute_reply":"2024-05-22T09:15:13.053867Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_image_list = os.listdir(\"./train/\")[0:SAMPLE_SIZE]\ntest_image_list = os.listdir(\"./test/\")","metadata":{"_uuid":"12d65488fa58182f965485608965ae36f345f183","execution":{"iopub.status.busy":"2024-05-22T09:15:13.055969Z","iopub.execute_input":"2024-05-22T09:15:13.056208Z","iopub.status.idle":"2024-05-22T09:15:13.082709Z","shell.execute_reply.started":"2024-05-22T09:15:13.056159Z","shell.execute_reply":"2024-05-22T09:15:13.081989Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_image_list_count(data_image_list):\n    labels = []\n    for img in data_image_list:\n        labels.append(img.split('.')[-3])\n    sns.countplot(labels)\n    plt.title('Cats and Dogs')\n    \nplot_image_list_count(train_image_list)","metadata":{"_uuid":"2901e1126ebe160a5a5c5a03dd07d5b8792a5290","execution":{"iopub.status.busy":"2024-05-22T09:15:13.083762Z","iopub.execute_input":"2024-05-22T09:15:13.083969Z","iopub.status.idle":"2024-05-22T09:15:13.742544Z","shell.execute_reply.started":"2024-05-22T09:15:13.083932Z","shell.execute_reply":"2024-05-22T09:15:13.741514Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_image_list_count(os.listdir(TRAIN_FOLDER))","metadata":{"_uuid":"1f36b71d110658540e3cbf3e435e00810ebb2d19","execution":{"iopub.status.busy":"2024-05-22T09:15:13.744466Z","iopub.execute_input":"2024-05-22T09:15:13.745171Z","iopub.status.idle":"2024-05-22T09:15:14.028826Z","shell.execute_reply.started":"2024-05-22T09:15:13.745096Z","shell.execute_reply":"2024-05-22T09:15:14.027787Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def process_data(data_image_list, DATA_FOLDER, isTrain=True):\n    data_df = []\n    for img in tqdm(data_image_list):\n        path = os.path.join(DATA_FOLDER, img)\n        if isTrain:\n            label = label_pet_image_one_hot_encoder(img)\n        else:\n            label = img.split('.')[0]\n        img = cv2.imread(path, cv2.IMREAD_COLOR)\n        img = cv2.resize(img, (IMG_SIZE, IMG_SIZE))\n        data_df.append([np.array(img), np.array(label)])\n    shuffle(data_df)\n    return data_df","metadata":{"execution":{"iopub.status.busy":"2024-05-22T09:15:14.030800Z","iopub.execute_input":"2024-05-22T09:15:14.031504Z","iopub.status.idle":"2024-05-22T09:15:14.040502Z","shell.execute_reply.started":"2024-05-22T09:15:14.031429Z","shell.execute_reply":"2024-05-22T09:15:14.039541Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Function to encode labels (0 for cat, 1 for dog)\ndef label_pet_image_one_hot_encoder(img):\n    label = img.split('.')[0]\n    if label == 'cat': return [1, 0]\n    elif label == 'dog': return [0, 1]","metadata":{"execution":{"iopub.status.busy":"2024-05-22T09:17:51.223757Z","iopub.execute_input":"2024-05-22T09:17:51.224035Z","iopub.status.idle":"2024-05-22T09:17:51.228808Z","shell.execute_reply.started":"2024-05-22T09:17:51.223992Z","shell.execute_reply":"2024-05-22T09:17:51.227751Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = process_data(train_image_list, TRAIN_FOLDER)","metadata":{"_uuid":"d1b8ee45b627b58a3a5d04bb0c64fb18d5e8f592","execution":{"iopub.status.busy":"2024-05-22T09:17:52.305235Z","iopub.execute_input":"2024-05-22T09:17:52.305611Z","iopub.status.idle":"2024-05-22T09:18:32.746462Z","shell.execute_reply.started":"2024-05-22T09:17:52.305539Z","shell.execute_reply":"2024-05-22T09:18:32.745749Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def show_images(data, isTest=False):\n    f, ax = plt.subplots(5,5, figsize=(15,15))\n    for i,data in enumerate(data[:25]):\n        img_num = data[1]\n        img_data = data[0]\n        label = np.argmax(img_num)\n        if label  == 1: \n            str_label='Dog'\n        elif label == 0: \n            str_label='Cat'\n        if(isTest):\n            str_label=\"None\"\n        ax[i//5, i%5].imshow(img_data)\n        ax[i//5, i%5].axis('off')\n        ax[i//5, i%5].set_title(\"Label: {}\".format(str_label))\n    plt.show()\n\nshow_images(train)","metadata":{"_uuid":"8a2b305bce44360f693d678a4b7eb9f27b3a7d36","execution":{"iopub.status.busy":"2024-05-22T09:18:32.747520Z","iopub.execute_input":"2024-05-22T09:18:32.747731Z","iopub.status.idle":"2024-05-22T09:18:34.528650Z","shell.execute_reply.started":"2024-05-22T09:18:32.747688Z","shell.execute_reply":"2024-05-22T09:18:34.527031Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test = process_data(test_image_list, TEST_FOLDER, False)","metadata":{"_uuid":"cb0696a3111e5421b3809f91bcb040f32cbde799","execution":{"iopub.status.busy":"2024-05-22T09:18:34.529871Z","iopub.execute_input":"2024-05-22T09:18:34.530117Z","iopub.status.idle":"2024-05-22T09:18:59.599226Z","shell.execute_reply.started":"2024-05-22T09:18:34.530066Z","shell.execute_reply":"2024-05-22T09:18:59.598517Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"show_images(test,True)","metadata":{"_uuid":"485ff5d66200edf7159027b49d4e60619b8b404a","execution":{"iopub.status.busy":"2024-05-22T09:18:59.600601Z","iopub.execute_input":"2024-05-22T09:18:59.600820Z","iopub.status.idle":"2024-05-22T09:19:01.426480Z","shell.execute_reply.started":"2024-05-22T09:18:59.600780Z","shell.execute_reply":"2024-05-22T09:19:01.425584Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = np.array([i[0] for i in train]).reshape(-1,IMG_SIZE,IMG_SIZE,3)\ny = np.array([i[1] for i in train])","metadata":{"_uuid":"7651e9ef5ff8074165a5208257c5db81ab83b75f","execution":{"iopub.status.busy":"2024-05-22T09:19:01.428580Z","iopub.execute_input":"2024-05-22T09:19:01.428858Z","iopub.status.idle":"2024-05-22T09:19:03.637481Z","shell.execute_reply.started":"2024-05-22T09:19:01.428808Z","shell.execute_reply":"2024-05-22T09:19:03.636756Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport numpy as np\nimport cv2\nfrom tqdm import tqdm\nfrom random import shuffle\nfrom sklearn.model_selection import train_test_split\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.applications import ResNet50\nfrom tensorflow.keras.layers import Dense, Lambda\nfrom tensorflow.keras.utils import to_categorical\nfrom tensorflow.keras import backend as K\n\n\nRESNET_WEIGHTS_PATH = '/kaggle/input/resnet50/resnet50_weights_tf_dim_ordering_tf_kernels_notop.h5'\n\n# Define the Hard Swish activation function\ndef hard_swish(x):\n    return x * K.hard_sigmoid(x)\n\n# Split the data into training and validation sets\nX_train, X_val, y_train, y_val = train_test_split(X, y, test_size=TEST_SIZE, random_state=RANDOM_STATE)\n\n# Ensure data shapes\nprint(f'X_train shape: {X_train.shape}')\nprint(f'y_train shape: {y_train.shape}')\nprint(f'X_val shape: {X_val.shape}')\nprint(f'y_val shape: {y_val.shape}')\n\n# Define the model\nmodel = Sequential()\n\n# Add the ResNet50 base model without the top (fully connected) layers\nmodel.add(ResNet50(include_top=False, pooling='max', weights=RESNET_WEIGHTS_PATH))\n\n# Add the fully connected layers with Hard Swish activation\nfor _ in range(10):\n    model.add(Dense(128))\n    model.add(Lambda(hard_swish))  # Apply Hard Swish using a Lambda layer\n\n# Add the final output layer with softmax activation\nmodel.add(Dense(NUM_CLASSES, activation='softmax'))\n\n# Compile the model\nmodel.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])\n\n\n","metadata":{"execution":{"iopub.status.busy":"2024-05-22T09:19:03.638533Z","iopub.execute_input":"2024-05-22T09:19:03.638778Z","iopub.status.idle":"2024-05-22T09:19:17.789031Z","shell.execute_reply.started":"2024-05-22T09:19:03.638730Z","shell.execute_reply":"2024-05-22T09:19:17.788364Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Train the model\ntrain_model = model.fit(X_train, y_train,\n                        batch_size=BATCH_SIZE,\n                        epochs=NO_EPOCHS,\n                        verbose=1,\n                        validation_data=(X_val, y_val))","metadata":{"_uuid":"3e2a3ff8c2dd896fd9ca725625587564165d0727","execution":{"iopub.status.busy":"2024-05-22T09:19:17.790375Z","iopub.execute_input":"2024-05-22T09:19:17.790665Z","iopub.status.idle":"2024-05-22T09:27:50.334750Z","shell.execute_reply.started":"2024-05-22T09:19:17.790610Z","shell.execute_reply":"2024-05-22T09:27:50.334001Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2024-05-22T09:27:50.336366Z","iopub.execute_input":"2024-05-22T09:27:50.336678Z","iopub.status.idle":"2024-05-22T09:27:50.352771Z","shell.execute_reply.started":"2024-05-22T09:27:50.336618Z","shell.execute_reply":"2024-05-22T09:27:50.352097Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_model(model, to_file='model.png')\nSVG(model_to_dot(model).create(prog='dot', format='svg'))","metadata":{"_uuid":"64561287fa8faaa756ba4043a2a3c671810a9dbf","execution":{"iopub.status.busy":"2024-05-22T09:27:50.354453Z","iopub.execute_input":"2024-05-22T09:27:50.354744Z","iopub.status.idle":"2024-05-22T09:27:52.247838Z","shell.execute_reply.started":"2024-05-22T09:27:50.354684Z","shell.execute_reply":"2024-05-22T09:27:52.247016Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_accuracy_and_loss(train_model):\n    hist = train_model.history\n    acc = hist['acc']\n    val_acc = hist['val_acc']\n    loss = hist['loss']\n    val_loss = hist['val_loss']\n    epochs = range(len(acc))\n    f, ax = plt.subplots(1,2, figsize=(14,6))\n    ax[0].plot(epochs, acc, 'g', label='Training accuracy')\n    ax[0].plot(epochs, val_acc, 'r', label='Validation accuracy')\n    ax[0].set_title('Training and validation accuracy')\n    ax[0].legend()\n    ax[1].plot(epochs, loss, 'g', label='Training loss')\n    ax[1].plot(epochs, val_loss, 'r', label='Validation loss')\n    ax[1].set_title('Training and validation loss')\n    ax[1].legend()\n    plt.show()\nplot_accuracy_and_loss(train_model)","metadata":{"_uuid":"5a7b3d7b9494a3a1a75db099d224a18b1bf1d8e3","execution":{"iopub.status.busy":"2024-05-22T09:27:52.249193Z","iopub.execute_input":"2024-05-22T09:27:52.249473Z","iopub.status.idle":"2024-05-22T09:27:52.692794Z","shell.execute_reply.started":"2024-05-22T09:27:52.249424Z","shell.execute_reply":"2024-05-22T09:27:52.691758Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"score = model.evaluate(X_val, y_val, verbose=0)\nprint('Validation loss:', score[0])\nprint('Validation accuracy:', score[1])","metadata":{"_uuid":"0935a9e239a7af382208ecede99bd2afb5098482","execution":{"iopub.status.busy":"2024-05-22T09:28:06.329127Z","iopub.execute_input":"2024-05-22T09:28:06.329545Z","iopub.status.idle":"2024-05-22T09:28:30.457380Z","shell.execute_reply.started":"2024-05-22T09:28:06.329459Z","shell.execute_reply":"2024-05-22T09:28:30.456622Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#get the predictions for the test data\npredicted_classes = model.predict_classes(X_val)\n#get the indices to be plotted\ny_true = np.argmax(y_val,axis=1)","metadata":{"_uuid":"dd52f4c515120a0c9aa2ecd4f1fd7cfaa71a08d0","execution":{"iopub.status.busy":"2024-05-22T09:28:30.458647Z","iopub.execute_input":"2024-05-22T09:28:30.458870Z","iopub.status.idle":"2024-05-22T09:28:55.749026Z","shell.execute_reply.started":"2024-05-22T09:28:30.458831Z","shell.execute_reply":"2024-05-22T09:28:55.748292Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"correct = np.nonzero(predicted_classes==y_true)[0]\nincorrect = np.nonzero(predicted_classes!=y_true)[0]","metadata":{"_uuid":"9da776ea97666c99a141cf26d2e64705cf5d9069","execution":{"iopub.status.busy":"2024-05-22T09:28:55.751014Z","iopub.execute_input":"2024-05-22T09:28:55.751283Z","iopub.status.idle":"2024-05-22T09:28:55.755480Z","shell.execute_reply.started":"2024-05-22T09:28:55.751229Z","shell.execute_reply":"2024-05-22T09:28:55.754722Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"target_names = [\"Class {}:\".format(i) for i in range(NUM_CLASSES)]\nprint(classification_report(y_true, predicted_classes, target_names=target_names))","metadata":{"_uuid":"3a92b2e2751942fd9fff4792ee932ee0d5f9bef2","execution":{"iopub.status.busy":"2024-05-22T09:28:55.756755Z","iopub.execute_input":"2024-05-22T09:28:55.756954Z","iopub.status.idle":"2024-05-22T09:28:55.780617Z","shell.execute_reply.started":"2024-05-22T09:28:55.756918Z","shell.execute_reply":"2024-05-22T09:28:55.779950Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"f, ax = plt.subplots(5,5, figsize=(15,15))\nfor i,data in enumerate(test[:25]):\n    img_num = data[1]\n    img_data = data[0]\n    orig = img_data\n    data = img_data.reshape(-1,IMG_SIZE,IMG_SIZE,3)\n    model_out = model.predict([data])[0]\n    \n    if np.argmax(model_out) == 1: \n        str_predicted='Dog'\n    else: \n        str_predicted='Cat'\n    ax[i//5, i%5].imshow(orig)\n    ax[i//5, i%5].axis('off')\n    ax[i//5, i%5].set_title(\"Predicted:{}\".format(str_predicted))    \nplt.show()","metadata":{"_uuid":"07e788faee58139d33db79be0093cdb0900d8073","execution":{"iopub.status.busy":"2024-05-22T09:28:55.781676Z","iopub.execute_input":"2024-05-22T09:28:55.781917Z","iopub.status.idle":"2024-05-22T09:28:57.913071Z","shell.execute_reply.started":"2024-05-22T09:28:55.781868Z","shell.execute_reply":"2024-05-22T09:28:57.912198Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred_list = []\nimg_list = []\nfor img in tqdm(test):\n    img_data = img[0]\n    img_idx = img[1]\n    data = img_data.reshape(-1,IMG_SIZE,IMG_SIZE,3)\n    predicted = model.predict([data])[0]\n    img_list.append(img_idx)\n    pred_list.append(predicted[1])","metadata":{"_uuid":"15afbef65cdfe94ffa3a2e63ed97f0fdd9a34d89","execution":{"iopub.status.busy":"2024-05-22T09:28:57.914379Z","iopub.execute_input":"2024-05-22T09:28:57.914634Z","iopub.status.idle":"2024-05-22T09:31:16.557938Z","shell.execute_reply.started":"2024-05-22T09:28:57.914579Z","shell.execute_reply":"2024-05-22T09:31:16.557212Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import confusion_matrix","metadata":{"execution":{"iopub.status.busy":"2024-05-22T09:31:16.559241Z","iopub.execute_input":"2024-05-22T09:31:16.559519Z","iopub.status.idle":"2024-05-22T09:31:16.562963Z","shell.execute_reply.started":"2024-05-22T09:31:16.559468Z","shell.execute_reply":"2024-05-22T09:31:16.562248Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cm=confusion_matrix(y_true, predicted_classes)","metadata":{"execution":{"iopub.status.busy":"2024-05-22T09:31:16.564075Z","iopub.execute_input":"2024-05-22T09:31:16.564304Z","iopub.status.idle":"2024-05-22T09:31:16.591480Z","shell.execute_reply.started":"2024-05-22T09:31:16.564263Z","shell.execute_reply":"2024-05-22T09:31:16.590841Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cmap ='Blues'\n\nsns.heatmap(cm,\n                 annot=False,\n                 fmt=\".3f\",\n                 vmin=-0.05,\n                 cmap = cmap)","metadata":{"execution":{"iopub.status.busy":"2024-05-22T09:31:16.592960Z","iopub.execute_input":"2024-05-22T09:31:16.593257Z","iopub.status.idle":"2024-05-22T09:31:17.096213Z","shell.execute_reply.started":"2024-05-22T09:31:16.593200Z","shell.execute_reply":"2024-05-22T09:31:17.095051Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}