{"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":"none","dataSources":[{"sourceId":5441,"databundleVersionId":38425,"sourceType":"competition"}],"dockerImageVersionId":30786,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"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-11-02T03:15:25.276036Z","iopub.execute_input":"2024-11-02T03:15:25.276526Z","iopub.status.idle":"2024-11-02T03:15:26.491300Z","shell.execute_reply.started":"2024-11-02T03:15:25.276480Z","shell.execute_reply":"2024-11-02T03:15:26.489801Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!unzip -qq /kaggle/input/dogs-vs-cats-redux-kernels-edition/train.zip\n!unzip -qq /kaggle/input/dogs-vs-cats-redux-kernels-edition/test.zip","metadata":{"execution":{"iopub.status.busy":"2024-11-02T03:25:41.915943Z","iopub.execute_input":"2024-11-02T03:25:41.916374Z","iopub.status.idle":"2024-11-02T03:25:55.063321Z","shell.execute_reply.started":"2024-11-02T03:25:41.916334Z","shell.execute_reply":"2024-11-02T03:25:55.061391Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport seaborn as sns","metadata":{"execution":{"iopub.status.busy":"2024-11-02T03:27:06.857521Z","iopub.execute_input":"2024-11-02T03:27:06.858523Z","iopub.status.idle":"2024-11-02T03:27:06.864719Z","shell.execute_reply.started":"2024-11-02T03:27:06.858472Z","shell.execute_reply":"2024-11-02T03:27:06.863032Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow import keras\nfrom keras import models\nfrom keras import layers\nfrom keras import optimizers\nfrom keras import callbacks\nfrom keras import backend as K\n\nfrom keras.preprocessing import image\nfrom keras.datasets import fashion_mnist\nfrom keras.utils import to_categorical\n\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import confusion_matrix, classification_report\nfrom keras.layers import  MaxPooling2D\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator","metadata":{"execution":{"iopub.status.busy":"2024-11-02T03:34:30.335449Z","iopub.execute_input":"2024-11-02T03:34:30.335954Z","iopub.status.idle":"2024-11-02T03:34:30.435276Z","shell.execute_reply.started":"2024-11-02T03:34:30.335911Z","shell.execute_reply":"2024-11-02T03:34:30.433817Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def file_to_df(file_names):\n    files, labels = list(), list()\n    for file in file_names:\n        files.append(file)\n        labels.append(file[:3])\n    df = pd.DataFrame({'filename':files,'label':labels})\n    return df\n\ndef cat_dog_df(dir):\n    file_names = os.listdir(f'/kaggle/working/{dir}')\n    df = file_to_df(file_names)\n\n    return df\n\ntrain_df = cat_dog_df('train')\ntest_df = cat_dog_df('test')\ntest_df['label'] = 0.5","metadata":{"execution":{"iopub.status.busy":"2024-11-02T04:59:59.022965Z","iopub.execute_input":"2024-11-02T04:59:59.024383Z","iopub.status.idle":"2024-11-02T04:59:59.088918Z","shell.execute_reply.started":"2024-11-02T04:59:59.024333Z","shell.execute_reply":"2024-11-02T04:59:59.087646Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Overview","metadata":{}},{"cell_type":"code","source":"pd.crosstab(train_df['label'], columns='N')","metadata":{"execution":{"iopub.status.busy":"2024-11-02T03:30:19.522691Z","iopub.execute_input":"2024-11-02T03:30:19.523210Z","iopub.status.idle":"2024-11-02T03:30:19.566788Z","shell.execute_reply.started":"2024-11-02T03:30:19.523167Z","shell.execute_reply":"2024-11-02T03:30:19.565287Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cat = [file for file in os.listdir('/kaggle/working/train') if file.startswith('cat')]\ndog = [file for file in os.listdir('/kaggle/working/train') if file.startswith('dog')]","metadata":{"execution":{"iopub.status.busy":"2024-11-02T03:29:31.430427Z","iopub.execute_input":"2024-11-02T03:29:31.430919Z","iopub.status.idle":"2024-11-02T03:29:31.485251Z","shell.execute_reply.started":"2024-11-02T03:29:31.430870Z","shell.execute_reply":"2024-11-02T03:29:31.484023Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, ax = plt.subplots(2, 2, figsize=(10, 10))\n\nfor i, c in enumerate(np.random.randint(0, len(cat), 4), start=1):\n    plt.subplot(2, 2, i)\n    image = plt.imread(f'/kaggle/working/train/{cat[c]}')\n    print(image.shape)\n    plt.imshow(image)","metadata":{"execution":{"iopub.status.busy":"2024-11-02T03:29:51.436039Z","iopub.execute_input":"2024-11-02T03:29:51.436480Z","iopub.status.idle":"2024-11-02T03:29:52.682275Z","shell.execute_reply.started":"2024-11-02T03:29:51.436435Z","shell.execute_reply":"2024-11-02T03:29:52.680749Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, ax = plt.subplots(2, 2, figsize=(10, 10))\n\nfor i, c in enumerate(np.random.randint(0, len(cat), 4), start=1):\n    plt.subplot(2, 2, i)\n    image = plt.imread(f'/kaggle/working/train/{dog[c]}')\n    print(image.shape)\n    plt.imshow(image)","metadata":{"execution":{"iopub.status.busy":"2024-11-02T03:30:29.083807Z","iopub.execute_input":"2024-11-02T03:30:29.084313Z","iopub.status.idle":"2024-11-02T03:30:30.327455Z","shell.execute_reply.started":"2024-11-02T03:30:29.084272Z","shell.execute_reply":"2024-11-02T03:30:30.326077Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Data Preparation","metadata":{}},{"cell_type":"code","source":"train, valid = train_test_split(train_df, test_size=0.2, stratify=train_df['label'], random_state=42)\nprint(train.shape, valid.shape)","metadata":{"execution":{"iopub.status.busy":"2024-11-02T03:31:09.484289Z","iopub.execute_input":"2024-11-02T03:31:09.484741Z","iopub.status.idle":"2024-11-02T03:31:09.547322Z","shell.execute_reply.started":"2024-11-02T03:31:09.484694Z","shell.execute_reply":"2024-11-02T03:31:09.545914Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, ax = plt.subplots(1, 2, figsize=(8, 4))\n\nplt.subplot(1, 2, 1)\nsns.countplot(x=train['label'], hue=train['label'])\nplt.title('Train')\n\nplt.subplot(1, 2, 2)\nsns.countplot(x=valid['label'], hue=valid['label'])\nplt.title('Validation')","metadata":{"execution":{"iopub.status.busy":"2024-11-02T03:33:43.079379Z","iopub.execute_input":"2024-11-02T03:33:43.079934Z","iopub.status.idle":"2024-11-02T03:33:45.041223Z","shell.execute_reply.started":"2024-11-02T03:33:43.079881Z","shell.execute_reply":"2024-11-02T03:33:45.040011Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Data Augmentation","metadata":{}},{"cell_type":"code","source":"width, height = 128, 128\nbatch_size = 32","metadata":{"execution":{"iopub.status.busy":"2024-11-02T03:34:05.008214Z","iopub.execute_input":"2024-11-02T03:34:05.008689Z","iopub.status.idle":"2024-11-02T03:34:05.015588Z","shell.execute_reply.started":"2024-11-02T03:34:05.008643Z","shell.execute_reply":"2024-11-02T03:34:05.013999Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# use for preprocessing train and validation data\ntrain_datagen = ImageDataGenerator(rescale=1./255, #rescaling the pixels\n                               horizontal_flip=True,\n                               rotation_range=15,\n                               zoom_range=0.2,\n                               shear_range=0.1,\n                               width_shift_range=0.2,\n                               height_shift_range=0.2,\n                               brightness_range=(0.5, 1)\n                               )\n\ntest_datagen = ImageDataGenerator(rescale=1./255)","metadata":{"execution":{"iopub.status.busy":"2024-11-02T03:34:36.029470Z","iopub.execute_input":"2024-11-02T03:34:36.030018Z","iopub.status.idle":"2024-11-02T03:34:36.037784Z","shell.execute_reply.started":"2024-11-02T03:34:36.029969Z","shell.execute_reply":"2024-11-02T03:34:36.036394Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data = train_datagen.flow_from_dataframe(dataframe=train,\n                                               directory='/kaggle/working/train',\n                                               x_col ='filename',\n                                               y_col='label',\n                                               target_size=(width,height),\n                                               class_mode='categorical',\n                                               batch_size=batch_size)\n\nvalidation_data = test_datagen.flow_from_dataframe(valid,\n                                                   directory='/kaggle/working/train',\n                                                   x_col ='filename',\n                                                   y_col='label',\n                                                   target_size=(width,height),\n                                                   class_mode='categorical',\n                                                   batch_size=batch_size,\n                                                   shuffle=False)\n\ntrain_data.class_indices","metadata":{"execution":{"iopub.status.busy":"2024-11-02T04:26:26.984362Z","iopub.execute_input":"2024-11-02T04:26:26.984929Z","iopub.status.idle":"2024-11-02T04:26:27.388493Z","shell.execute_reply.started":"2024-11-02T04:26:26.984879Z","shell.execute_reply":"2024-11-02T04:26:27.387027Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Transfer Learning","metadata":{}},{"cell_type":"code","source":"base_model = keras.applications.ResNet50V2(weights='imagenet', include_top=False, \n                                           input_shape=(width, height, 3))\nbase_model.trainable=False\n# base_model.summary()","metadata":{"execution":{"iopub.status.busy":"2024-11-02T03:47:50.088853Z","iopub.execute_input":"2024-11-02T03:47:50.089349Z","iopub.status.idle":"2024-11-02T03:47:51.594538Z","shell.execute_reply.started":"2024-11-02T03:47:50.089306Z","shell.execute_reply":"2024-11-02T03:47:51.593062Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"base_model(keras.Input(shape=(width, height, 3)))\n\nresNet = models.Sequential()\nresNet.add(base_model)\nresNet.add(layers.Flatten())\nresNet.add(layers.Dense(64, activation='relu'))\nresNet.add(layers.Dropout(0.2))\nresNet.add(layers.Dense(2, activation='softmax', name='output'))\n\nresNet.summary()","metadata":{"execution":{"iopub.status.busy":"2024-11-02T03:38:31.521525Z","iopub.execute_input":"2024-11-02T03:38:31.522007Z","iopub.status.idle":"2024-11-02T03:38:31.571247Z","shell.execute_reply.started":"2024-11-02T03:38:31.521963Z","shell.execute_reply":"2024-11-02T03:38:31.570050Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"resNet.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])\n\nearly_stopping = callbacks.EarlyStopping(monitor='val_loss', patience=3, restore_best_weights=True)\n\nhistory = resNet.fit(train_data,\n                     steps_per_epoch=len(train_data)//10,\n                     validation_data=validation_data,\n                     validation_steps=len(validation_data)//batch_size,\n                     epochs=10,\n                     callbacks=early_stopping)\nresNet.save('resNet50V2_itr10_callbacks.keras')","metadata":{"execution":{"iopub.status.busy":"2024-11-02T04:07:30.857286Z","iopub.execute_input":"2024-11-02T04:07:30.857808Z","iopub.status.idle":"2024-11-02T04:19:07.760707Z","shell.execute_reply.started":"2024-11-02T04:07:30.857760Z","shell.execute_reply":"2024-11-02T04:19:07.759293Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Evaluation","metadata":{}},{"cell_type":"code","source":"y_pred = resNet.predict(validation_data).argmax(axis=1)\ny_true = validation_data.labels\n\nresult = resNet.evaluate(validation_data, verbose=False)\nprint(f\"Accuracy: {result[1]} Loss: {result[0]}\")","metadata":{"execution":{"iopub.status.busy":"2024-11-02T04:26:34.259436Z","iopub.execute_input":"2024-11-02T04:26:34.259928Z","iopub.status.idle":"2024-11-02T04:31:27.623012Z","shell.execute_reply.started":"2024-11-02T04:26:34.259884Z","shell.execute_reply":"2024-11-02T04:31:27.621639Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# confusion matrix\nplt.figure(figsize=(4, 3))\nsns.heatmap(confusion_matrix(y_true, y_pred), annot=True, cmap='Blues', fmt='d')\nplt.show()\n\n# Show result and report\nprint(classification_report(y_true, y_pred, target_names=['Cat', 'Dog']))","metadata":{"execution":{"iopub.status.busy":"2024-11-02T04:31:27.625274Z","iopub.execute_input":"2024-11-02T04:31:27.625664Z","iopub.status.idle":"2024-11-02T04:31:27.920736Z","shell.execute_reply.started":"2024-11-02T04:31:27.625624Z","shell.execute_reply":"2024-11-02T04:31:27.919135Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_summary(history):\n    acc = history.history['accuracy']\n    val_acc = history.history['val_accuracy']\n    loss = history.history['loss']\n    val_loss = history.history['val_loss']\n\n    print(f'Last train accuracy: {acc[-1]}')\n    print(f'Last validation accuracy: {val_acc[-1]}')\n\n    fig, ax = plt.subplots(1, 2, figsize=(10, 4))\n\n    ax[0].plot(acc, label='acc')\n    ax[0].plot(val_acc, label='val_acc')\n    ax[0].set_title('Model accuracy')\n    ax[0].set_ylabel('acc')\n    ax[0].set_xlabel('epoch')\n    ax[0].legend()\n\n    ax[1].plot(loss, label='loss')\n    ax[1].plot(val_loss, label='val_loss')\n    ax[1].set_title('Model loss')\n    ax[1].set_ylabel('loss')\n    ax[1].set_xlabel('epoch')\n    ax[1].legend()\n\n\nplot_summary(history)","metadata":{"execution":{"iopub.status.busy":"2024-11-02T04:31:27.922657Z","iopub.execute_input":"2024-11-02T04:31:27.923181Z","iopub.status.idle":"2024-11-02T04:31:28.574720Z","shell.execute_reply.started":"2024-11-02T04:31:27.923126Z","shell.execute_reply":"2024-11-02T04:31:28.573321Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Submission","metadata":{}},{"cell_type":"code","source":"test_data = test_datagen.flow_from_dataframe(test_df,\n                                           directory='/kaggle/working/test',\n                                           x_col ='filename',\n                                           y_col='label',\n                                           target_size=(width,height),\n                                           class_mode='categorical',\n                                           batch_size=batch_size,\n                                           shuffle=False)","metadata":{"execution":{"iopub.status.busy":"2024-11-02T05:00:33.680624Z","iopub.execute_input":"2024-11-02T05:00:33.681948Z","iopub.status.idle":"2024-11-02T05:00:33.876415Z","shell.execute_reply.started":"2024-11-02T05:00:33.681893Z","shell.execute_reply":"2024-11-02T05:00:33.874802Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_pred = resNet.predict(test_data).argmax(axis=1)\ntest_pred.shape","metadata":{"execution":{"iopub.status.busy":"2024-11-02T04:41:06.096010Z","iopub.execute_input":"2024-11-02T04:41:06.096458Z","iopub.status.idle":"2024-11-02T04:47:07.727075Z","shell.execute_reply.started":"2024-11-02T04:41:06.096417Z","shell.execute_reply":"2024-11-02T04:47:07.725573Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df['label'] = test_pred\ntest_df['label'] = test_df['label'].map({0: 'cat', 1:'dog'})\ntest_df['class'] = [0 if label == 'dog' else 1 for label in test_df['label']]","metadata":{"execution":{"iopub.status.busy":"2024-11-02T05:00:13.538919Z","iopub.execute_input":"2024-11-02T05:00:13.539415Z","iopub.status.idle":"2024-11-02T05:00:13.560392Z","shell.execute_reply.started":"2024-11-02T05:00:13.539370Z","shell.execute_reply":"2024-11-02T05:00:13.559019Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import re\n\npattern  =r'.jpg'\nfor ind, fname in enumerate(test_df['filename']):\n    test_df.loc[ind, 'id'] = re.sub(pattern, '', fname)","metadata":{"execution":{"iopub.status.busy":"2024-11-02T05:00:14.872332Z","iopub.execute_input":"2024-11-02T05:00:14.873380Z","iopub.status.idle":"2024-11-02T05:00:17.368314Z","shell.execute_reply.started":"2024-11-02T05:00:14.873327Z","shell.execute_reply":"2024-11-02T05:00:17.366938Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df['id'] = test_df['id'].astype('int')\ntest_df = test_df.sort_values('id').reset_index(drop=True)\ntest_df.head()","metadata":{"execution":{"iopub.status.busy":"2024-11-02T05:00:49.315492Z","iopub.execute_input":"2024-11-02T05:00:49.316232Z","iopub.status.idle":"2024-11-02T05:00:49.336078Z","shell.execute_reply.started":"2024-11-02T05:00:49.316171Z","shell.execute_reply":"2024-11-02T05:00:49.334717Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.read_csv('/kaggle/input/dogs-vs-cats-redux-kernels-edition/sample_submission.csv')\nsubmission['label'] = test_df['class']\nsubmission.head()","metadata":{"execution":{"iopub.status.busy":"2024-11-02T05:00:54.273940Z","iopub.execute_input":"2024-11-02T05:00:54.274599Z","iopub.status.idle":"2024-11-02T05:00:54.297120Z","shell.execute_reply.started":"2024-11-02T05:00:54.274548Z","shell.execute_reply":"2024-11-02T05:00:54.295651Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2024-11-02T05:01:07.748245Z","iopub.execute_input":"2024-11-02T05:01:07.748716Z","iopub.status.idle":"2024-11-02T05:01:07.775674Z","shell.execute_reply.started":"2024-11-02T05:01:07.748673Z","shell.execute_reply":"2024-11-02T05:01:07.774352Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}