{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"gpu","dataSources":[{"sourceId":7327,"databundleVersionId":861871,"sourceType":"competition"}],"dockerImageVersionId":30588,"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","_kg_hide-input":true,"_kg_hide-output":true,"execution":{"iopub.status.busy":"2023-12-13T17:28:26.879472Z","iopub.execute_input":"2023-12-13T17:28:26.880324Z","iopub.status.idle":"2023-12-13T17:28:57.600969Z","shell.execute_reply.started":"2023-12-13T17:28:26.880292Z","shell.execute_reply":"2023-12-13T17:28:57.599837Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Import Necessary Libraries","metadata":{}},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport tensorflow as tf\nfrom tensorflow.keras.models import Sequential\nfrom keras.layers import Dense, Conv2D, Flatten,Input,Dropout,MaxPooling2D\nfrom tensorflow.keras import datasets, layers, models\nfrom tensorflow.math import confusion_matrix\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.preprocessing import LabelEncoder\nfrom tensorflow.keras.utils import to_categorical\nfrom tensorflow.keras.applications import EfficientNetB4, Xception, ResNet50\nfrom tensorflow.keras.layers import GlobalAveragePooling2D, concatenate\nfrom tensorflow.keras.models import Model\nfrom tensorflow.keras.callbacks import EarlyStopping, ModelCheckpoint\nfrom tensorflow.keras.models import load_model\nfrom keras.preprocessing.image import ImageDataGenerator\nimport random\nimport os\nimport cv2\nimport warnings\nwarnings.filterwarnings('ignore')","metadata":{"execution":{"iopub.status.busy":"2023-12-13T17:28:57.602625Z","iopub.execute_input":"2023-12-13T17:28:57.603128Z","iopub.status.idle":"2023-12-13T17:29:10.322242Z","shell.execute_reply.started":"2023-12-13T17:28:57.603098Z","shell.execute_reply":"2023-12-13T17:29:10.321349Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Data Import","metadata":{}},{"cell_type":"code","source":"df = pd.read_csv('/kaggle/input/dog-breed-identification/labels.csv')\ndf.head()","metadata":{"execution":{"iopub.status.busy":"2023-12-13T17:29:20.298645Z","iopub.execute_input":"2023-12-13T17:29:20.300075Z","iopub.status.idle":"2023-12-13T17:29:20.365009Z","shell.execute_reply.started":"2023-12-13T17:29:20.300025Z","shell.execute_reply":"2023-12-13T17:29:20.364010Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Data Analyse","metadata":{}},{"cell_type":"code","source":"df.shape","metadata":{"execution":{"iopub.status.busy":"2023-12-13T17:29:22.392726Z","iopub.execute_input":"2023-12-13T17:29:22.393459Z","iopub.status.idle":"2023-12-13T17:29:22.399329Z","shell.execute_reply.started":"2023-12-13T17:29:22.393423Z","shell.execute_reply":"2023-12-13T17:29:22.398327Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df[\"breed\"].nunique()","metadata":{"execution":{"iopub.status.busy":"2023-12-13T17:29:23.466889Z","iopub.execute_input":"2023-12-13T17:29:23.467674Z","iopub.status.idle":"2023-12-13T17:29:23.479826Z","shell.execute_reply.started":"2023-12-13T17:29:23.467637Z","shell.execute_reply":"2023-12-13T17:29:23.478931Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df[\"breed\"].unique()","metadata":{"execution":{"iopub.status.busy":"2023-12-13T17:29:24.552463Z","iopub.execute_input":"2023-12-13T17:29:24.553387Z","iopub.status.idle":"2023-12-13T17:29:24.561386Z","shell.execute_reply.started":"2023-12-13T17:29:24.553352Z","shell.execute_reply":"2023-12-13T17:29:24.560324Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.isna().sum()","metadata":{"execution":{"iopub.status.busy":"2023-12-13T17:29:27.053434Z","iopub.execute_input":"2023-12-13T17:29:27.054162Z","iopub.status.idle":"2023-12-13T17:29:27.064863Z","shell.execute_reply.started":"2023-12-13T17:29:27.054127Z","shell.execute_reply":"2023-12-13T17:29:27.063901Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.info()","metadata":{"execution":{"iopub.status.busy":"2023-12-13T17:29:28.469511Z","iopub.execute_input":"2023-12-13T17:29:28.469937Z","iopub.status.idle":"2023-12-13T17:29:28.493891Z","shell.execute_reply.started":"2023-12-13T17:29:28.469903Z","shell.execute_reply":"2023-12-13T17:29:28.492623Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(15, 8))\nsns.countplot(x='breed', data=df, order=df['breed'].value_counts().index,palette=\"YlGnBu_r\")\nplt.title('Counts of Dog Breeds')\nplt.xlabel('Count')\nplt.xticks(rotation=90)\nplt.ylabel('Dog Breeds')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-12-13T17:33:05.643219Z","iopub.execute_input":"2023-12-13T17:33:05.644402Z","iopub.status.idle":"2023-12-13T17:33:07.220117Z","shell.execute_reply.started":"2023-12-13T17:33:05.644352Z","shell.execute_reply":"2023-12-13T17:33:07.218993Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Data splitting based on 'breed' counts**","metadata":{}},{"cell_type":"code","source":"breed_counts = df['breed'].value_counts()\nincluded_breeds = breed_counts[breed_counts > 100].index.tolist()\ndf = df[df['breed'].isin(included_breeds)].reset_index(drop=True)","metadata":{"execution":{"iopub.status.busy":"2023-12-13T17:33:47.952442Z","iopub.execute_input":"2023-12-13T17:33:47.953602Z","iopub.status.idle":"2023-12-13T17:33:47.964584Z","shell.execute_reply.started":"2023-12-13T17:33:47.953564Z","shell.execute_reply":"2023-12-13T17:33:47.963250Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.shape","metadata":{"execution":{"iopub.status.busy":"2023-12-13T17:33:48.991993Z","iopub.execute_input":"2023-12-13T17:33:48.992389Z","iopub.status.idle":"2023-12-13T17:33:48.999142Z","shell.execute_reply.started":"2023-12-13T17:33:48.992358Z","shell.execute_reply":"2023-12-13T17:33:48.998167Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head()","metadata":{"execution":{"iopub.status.busy":"2023-12-13T17:33:49.966397Z","iopub.execute_input":"2023-12-13T17:33:49.967112Z","iopub.status.idle":"2023-12-13T17:33:49.977204Z","shell.execute_reply.started":"2023-12-13T17:33:49.967079Z","shell.execute_reply":"2023-12-13T17:33:49.976153Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Image Prep","metadata":{}},{"cell_type":"markdown","source":"**Function to read and resize images**","metadata":{}},{"cell_type":"code","source":"def read_and_resize_image_rgb(file_path):\n    img = cv2.imread(file_path)\n    img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) \n    img_rgb_resized = cv2.resize(img_rgb, (224, 224))\n    return img_rgb_resized","metadata":{"execution":{"iopub.status.busy":"2023-12-13T17:33:52.514801Z","iopub.execute_input":"2023-12-13T17:33:52.515687Z","iopub.status.idle":"2023-12-13T17:33:52.520821Z","shell.execute_reply.started":"2023-12-13T17:33:52.515649Z","shell.execute_reply":"2023-12-13T17:33:52.519829Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Directory containing images**","metadata":{}},{"cell_type":"code","source":"image_dir = '/kaggle/input/dog-breed-identification/train'","metadata":{"execution":{"iopub.status.busy":"2023-12-13T17:33:54.193640Z","iopub.execute_input":"2023-12-13T17:33:54.194604Z","iopub.status.idle":"2023-12-13T17:33:54.199176Z","shell.execute_reply.started":"2023-12-13T17:33:54.194561Z","shell.execute_reply":"2023-12-13T17:33:54.198011Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Read and resize images**","metadata":{}},{"cell_type":"code","source":"resized_images = []\nfor idx, filename in enumerate(df['id']):\n    img_path = os.path.join(image_dir, filename + '.jpg')\n    resized_img = read_and_resize_image_rgb(img_path)\n    resized_images.append(resized_img)\nresized_images_array = np.array(resized_images)","metadata":{"execution":{"iopub.status.busy":"2023-12-13T17:33:55.790491Z","iopub.execute_input":"2023-12-13T17:33:55.790910Z","iopub.status.idle":"2023-12-13T17:34:13.400039Z","shell.execute_reply.started":"2023-12-13T17:33:55.790877Z","shell.execute_reply":"2023-12-13T17:34:13.399150Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Display first 20 images**","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(10, 10))\nfor i in range(20):\n    plt.subplot(4, 5, i + 1)\n    plt.imshow(resized_images_array[i])\n    plt.axis('off')\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-12-13T17:34:21.700422Z","iopub.execute_input":"2023-12-13T17:34:21.701316Z","iopub.status.idle":"2023-12-13T17:34:23.074930Z","shell.execute_reply.started":"2023-12-13T17:34:21.701282Z","shell.execute_reply":"2023-12-13T17:34:23.073595Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Extract unique class names**","metadata":{}},{"cell_type":"code","source":"class_names = df['breed'].unique()\nclass_names[1:10]","metadata":{"execution":{"iopub.status.busy":"2023-12-13T17:34:27.363587Z","iopub.execute_input":"2023-12-13T17:34:27.364036Z","iopub.status.idle":"2023-12-13T17:34:27.372481Z","shell.execute_reply.started":"2023-12-13T17:34:27.364000Z","shell.execute_reply":"2023-12-13T17:34:27.371421Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Preprocess labels**","metadata":{}},{"cell_type":"code","source":"label_encoder = LabelEncoder()\nlabel_encoded = label_encoder.fit_transform(df['breed'])\nnew_labels = to_categorical(label_encoded)","metadata":{"execution":{"iopub.status.busy":"2023-12-13T17:34:35.213523Z","iopub.execute_input":"2023-12-13T17:34:35.214290Z","iopub.status.idle":"2023-12-13T17:34:35.220371Z","shell.execute_reply.started":"2023-12-13T17:34:35.214255Z","shell.execute_reply":"2023-12-13T17:34:35.219320Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Assign data and labels**","metadata":{}},{"cell_type":"code","source":"X = resized_images_array\ny = new_labels","metadata":{"execution":{"iopub.status.busy":"2023-12-13T17:34:37.206570Z","iopub.execute_input":"2023-12-13T17:34:37.207736Z","iopub.status.idle":"2023-12-13T17:34:37.211667Z","shell.execute_reply.started":"2023-12-13T17:34:37.207702Z","shell.execute_reply":"2023-12-13T17:34:37.210684Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Train Test Split","metadata":{}},{"cell_type":"code","source":"X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.15, random_state=30)","metadata":{"execution":{"iopub.status.busy":"2023-12-13T17:34:39.171925Z","iopub.execute_input":"2023-12-13T17:34:39.172815Z","iopub.status.idle":"2023-12-13T17:34:39.299565Z","shell.execute_reply.started":"2023-12-13T17:34:39.172758Z","shell.execute_reply":"2023-12-13T17:34:39.298527Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train.shape","metadata":{"execution":{"iopub.status.busy":"2023-12-13T17:34:40.726575Z","iopub.execute_input":"2023-12-13T17:34:40.726985Z","iopub.status.idle":"2023-12-13T17:34:40.733364Z","shell.execute_reply.started":"2023-12-13T17:34:40.726931Z","shell.execute_reply":"2023-12-13T17:34:40.732348Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_test.shape","metadata":{"execution":{"iopub.status.busy":"2023-12-13T17:34:41.670164Z","iopub.execute_input":"2023-12-13T17:34:41.670992Z","iopub.status.idle":"2023-12-13T17:34:41.677152Z","shell.execute_reply.started":"2023-12-13T17:34:41.670935Z","shell.execute_reply":"2023-12-13T17:34:41.676194Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train.shape","metadata":{"execution":{"iopub.status.busy":"2023-12-13T17:34:42.488864Z","iopub.execute_input":"2023-12-13T17:34:42.489273Z","iopub.status.idle":"2023-12-13T17:34:42.495469Z","shell.execute_reply.started":"2023-12-13T17:34:42.489243Z","shell.execute_reply":"2023-12-13T17:34:42.494530Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_test.shape","metadata":{"execution":{"iopub.status.busy":"2023-12-13T17:34:42.803210Z","iopub.execute_input":"2023-12-13T17:34:42.803588Z","iopub.status.idle":"2023-12-13T17:34:42.809903Z","shell.execute_reply.started":"2023-12-13T17:34:42.803549Z","shell.execute_reply":"2023-12-13T17:34:42.808860Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Data Augmentation","metadata":{}},{"cell_type":"code","source":"train_datagen = ImageDataGenerator(\n    rotation_range=30,\n    width_shift_range=0.2,\n    height_shift_range=0.2,\n    rescale=1./255,\n    shear_range=0.2,\n    zoom_range=0.2,\n    horizontal_flip=True,\n    fill_mode='nearest')\n\ntest_datagen=ImageDataGenerator(rescale=1./255)","metadata":{"execution":{"iopub.status.busy":"2023-12-13T17:34:45.139925Z","iopub.execute_input":"2023-12-13T17:34:45.140344Z","iopub.status.idle":"2023-12-13T17:34:45.146475Z","shell.execute_reply.started":"2023-12-13T17:34:45.140311Z","shell.execute_reply":"2023-12-13T17:34:45.145119Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Plot augmented images**","metadata":{}},{"cell_type":"code","source":"augmented_images = train_datagen.flow(X_train, y=y_train, shuffle=True)\nplt.figure(figsize=(10, 10))\nfor i in range(20):\n    augmented_image = augmented_images.next()[0][0] \n    plt.subplot(4, 5, i + 1)\n    plt.imshow(augmented_image)\n    plt.axis('off')\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-12-13T17:34:47.544608Z","iopub.execute_input":"2023-12-13T17:34:47.545047Z","iopub.status.idle":"2023-12-13T17:34:55.608854Z","shell.execute_reply.started":"2023-12-13T17:34:47.544997Z","shell.execute_reply":"2023-12-13T17:34:55.607825Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"training_set=train_datagen.flow(X_train,y=y_train,batch_size=64)\ntesting_set=test_datagen.flow(X_test,y=y_test,batch_size=64)","metadata":{"execution":{"iopub.status.busy":"2023-12-13T17:35:44.594791Z","iopub.execute_input":"2023-12-13T17:35:44.595846Z","iopub.status.idle":"2023-12-13T17:35:44.981113Z","shell.execute_reply.started":"2023-12-13T17:35:44.595806Z","shell.execute_reply":"2023-12-13T17:35:44.979827Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Convolutional Neural Network","metadata":{}},{"cell_type":"markdown","source":"**Define the model**","metadata":{}},{"cell_type":"code","source":"model = Sequential([\n    Input(shape=(224, 224, 3)),\n    Conv2D(8, (3, 3), activation='relu', padding='same'),\n    MaxPooling2D((2, 2)),\n    Conv2D(16, (3, 3), activation='relu', padding='same'),\n    MaxPooling2D((2, 2)),\n    Conv2D(32, (3, 3), activation='relu', padding='same'),\n    MaxPooling2D((2, 2)),\n    Flatten(),\n    Dense(64, activation='sigmoid'),\n    Dropout(0.2),\n    Dense(20, activation='softmax')\n])","metadata":{"execution":{"iopub.status.busy":"2023-12-13T17:35:47.452444Z","iopub.execute_input":"2023-12-13T17:35:47.452923Z","iopub.status.idle":"2023-12-13T17:35:50.631865Z","shell.execute_reply.started":"2023-12-13T17:35:47.452880Z","shell.execute_reply":"2023-12-13T17:35:50.630962Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Compile the model**","metadata":{}},{"cell_type":"code","source":"model.compile(optimizer=\"adam\", loss='categorical_crossentropy', metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2023-12-13T17:35:52.658650Z","iopub.execute_input":"2023-12-13T17:35:52.659096Z","iopub.status.idle":"2023-12-13T17:35:52.680127Z","shell.execute_reply.started":"2023-12-13T17:35:52.659061Z","shell.execute_reply":"2023-12-13T17:35:52.678849Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Summary**","metadata":{}},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2023-12-13T17:35:55.440658Z","iopub.execute_input":"2023-12-13T17:35:55.441483Z","iopub.status.idle":"2023-12-13T17:35:55.477244Z","shell.execute_reply.started":"2023-12-13T17:35:55.441448Z","shell.execute_reply":"2023-12-13T17:35:55.476314Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Train the CNN model**","metadata":{}},{"cell_type":"code","source":"history=model.fit_generator(training_set,validation_data = testing_set,epochs = 10)","metadata":{"execution":{"iopub.status.busy":"2023-12-13T17:35:57.592040Z","iopub.execute_input":"2023-12-13T17:35:57.592429Z","iopub.status.idle":"2023-12-13T17:39:54.158852Z","shell.execute_reply.started":"2023-12-13T17:35:57.592397Z","shell.execute_reply":"2023-12-13T17:39:54.157706Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Transfer Learning (EfficientNetB4, Xception, ResNet50)","metadata":{}},{"cell_type":"code","source":"df = pd.read_csv('/kaggle/input/dog-breed-identification/labels.csv')\ndf.shape","metadata":{"execution":{"iopub.status.busy":"2023-12-13T17:41:20.763856Z","iopub.execute_input":"2023-12-13T17:41:20.764643Z","iopub.status.idle":"2023-12-13T17:41:20.788546Z","shell.execute_reply.started":"2023-12-13T17:41:20.764607Z","shell.execute_reply":"2023-12-13T17:41:20.787277Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def read_and_resize_image_rgb(file_path):\n    img = cv2.imread(file_path)\n    img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) \n    img_rgb_resized = cv2.resize(img_rgb, (224, 224))\n    return img_rgb_resized","metadata":{"execution":{"iopub.status.busy":"2023-12-13T17:41:22.914381Z","iopub.execute_input":"2023-12-13T17:41:22.914807Z","iopub.status.idle":"2023-12-13T17:41:22.920263Z","shell.execute_reply.started":"2023-12-13T17:41:22.914775Z","shell.execute_reply":"2023-12-13T17:41:22.919253Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"resized_images = []\nfor idx, filename in enumerate(df['id']):\n    img_path = os.path.join(image_dir, filename + '.jpg')\n    resized_img = read_and_resize_image_rgb(img_path)\n    resized_images.append(resized_img)\nresized_images_array = np.array(resized_images)","metadata":{"execution":{"iopub.status.busy":"2023-12-13T17:41:24.394731Z","iopub.execute_input":"2023-12-13T17:41:24.395762Z","iopub.status.idle":"2023-12-13T17:42:37.754939Z","shell.execute_reply.started":"2023-12-13T17:41:24.395723Z","shell.execute_reply":"2023-12-13T17:42:37.753852Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class_names = df['breed'].unique()","metadata":{"execution":{"iopub.status.busy":"2023-12-13T17:42:56.778566Z","iopub.execute_input":"2023-12-13T17:42:56.779088Z","iopub.status.idle":"2023-12-13T17:42:56.784879Z","shell.execute_reply.started":"2023-12-13T17:42:56.779057Z","shell.execute_reply":"2023-12-13T17:42:56.783890Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"label_encoder = LabelEncoder()\nlabel_encoded = label_encoder.fit_transform(df['breed'])\nnew_labels = to_categorical(label_encoded)","metadata":{"execution":{"iopub.status.busy":"2023-12-13T17:42:57.786607Z","iopub.execute_input":"2023-12-13T17:42:57.787369Z","iopub.status.idle":"2023-12-13T17:42:57.797531Z","shell.execute_reply.started":"2023-12-13T17:42:57.787336Z","shell.execute_reply":"2023-12-13T17:42:57.796295Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = resized_images_array\ny = new_labels","metadata":{"execution":{"iopub.status.busy":"2023-12-13T17:42:59.069409Z","iopub.execute_input":"2023-12-13T17:42:59.070150Z","iopub.status.idle":"2023-12-13T17:42:59.075380Z","shell.execute_reply.started":"2023-12-13T17:42:59.070118Z","shell.execute_reply":"2023-12-13T17:42:59.074204Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Function to extract features using pre-trained models**","metadata":{}},{"cell_type":"code","source":"def get_features(model_name, model_preprocessor, input_size, data):\n    input_layer = Input(input_size)\n    preprocessor = model_preprocessor(input_layer)\n    base_model = model_name(weights='imagenet', include_top=False, input_shape=input_size)(preprocessor)\n    avg = GlobalAveragePooling2D()(base_model)\n    feature_extractor = Model(inputs=input_layer, outputs=avg)\n    feature_maps = feature_extractor.predict(data, verbose=1)\n    print('Feature maps shape:', feature_maps.shape)\n    return feature_maps","metadata":{"execution":{"iopub.status.busy":"2023-12-13T17:43:01.255039Z","iopub.execute_input":"2023-12-13T17:43:01.256080Z","iopub.status.idle":"2023-12-13T17:43:01.262297Z","shell.execute_reply.started":"2023-12-13T17:43:01.256042Z","shell.execute_reply":"2023-12-13T17:43:01.261214Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Extract features**","metadata":{}},{"cell_type":"code","source":"effnet_preprocessor = tf.keras.applications.efficientnet.preprocess_input\neffnet_features = get_features(EfficientNetB4, effnet_preprocessor, (224, 224, 3), X)\n\nxception_preprocessor = tf.keras.applications.xception.preprocess_input\nxception_features = get_features(Xception, xception_preprocessor, (224, 224, 3), X)\n\nresnet_preprocessor = tf.keras.applications.resnet50.preprocess_input\nresnet_features = get_features(ResNet50, resnet_preprocessor, (224, 224, 3), X)","metadata":{"execution":{"iopub.status.busy":"2023-12-13T17:43:03.104509Z","iopub.execute_input":"2023-12-13T17:43:03.105201Z","iopub.status.idle":"2023-12-13T17:44:38.412483Z","shell.execute_reply.started":"2023-12-13T17:43:03.105166Z","shell.execute_reply":"2023-12-13T17:44:38.411387Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Concatenate extracted features**","metadata":{}},{"cell_type":"code","source":"final_features = np.concatenate([effnet_features, xception_features, resnet_features], axis=-1)\nprint('Final feature maps shape:', final_features.shape)","metadata":{"execution":{"iopub.status.busy":"2023-12-13T17:51:35.161910Z","iopub.execute_input":"2023-12-13T17:51:35.162937Z","iopub.status.idle":"2023-12-13T17:51:35.252118Z","shell.execute_reply.started":"2023-12-13T17:51:35.162896Z","shell.execute_reply":"2023-12-13T17:51:35.250998Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Create a new model for classification using concatenated features**","metadata":{}},{"cell_type":"code","source":"model = Sequential()\nmodel.add(Dropout(0.5, input_shape=(final_features.shape[1],)))\nmodel.add(Dense(120, activation='softmax'))\nmodel.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2023-12-13T17:53:33.328917Z","iopub.execute_input":"2023-12-13T17:53:33.329329Z","iopub.status.idle":"2023-12-13T17:53:33.362355Z","shell.execute_reply.started":"2023-12-13T17:53:33.329297Z","shell.execute_reply":"2023-12-13T17:53:33.361528Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Define callbacks for model training**","metadata":{}},{"cell_type":"code","source":"checkpoint = ModelCheckpoint('best_model.h5', monitor='val_accuracy', verbose=1, save_best_only=True, mode='max')\nearly_stopping = EarlyStopping(monitor='val_accuracy', patience=5, restore_best_weights=True)","metadata":{"execution":{"iopub.status.busy":"2023-12-13T17:53:35.050545Z","iopub.execute_input":"2023-12-13T17:53:35.051291Z","iopub.status.idle":"2023-12-13T17:53:35.056109Z","shell.execute_reply.started":"2023-12-13T17:53:35.051254Z","shell.execute_reply":"2023-12-13T17:53:35.055164Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Train the new model using extracted features**","metadata":{}},{"cell_type":"code","source":"history = model.fit(final_features, y,\n                    batch_size=128,\n                    epochs=50,\n                    validation_split=0.15,\n                    callbacks=[checkpoint, early_stopping])","metadata":{"execution":{"iopub.status.busy":"2023-12-13T17:53:36.772781Z","iopub.execute_input":"2023-12-13T17:53:36.773870Z","iopub.status.idle":"2023-12-13T17:53:42.752757Z","shell.execute_reply.started":"2023-12-13T17:53:36.773824Z","shell.execute_reply":"2023-12-13T17:53:42.751709Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(12, 5))\nplt.subplot(1, 2, 1)\nplt.plot(history.history['loss'], label='Training Loss')\nplt.plot(history.history['val_loss'], label='Validation Loss')\nplt.title('Training and Validation Loss')\nplt.xlabel('Epoch')\nplt.ylabel('Loss')\nplt.legend()\n\nplt.subplot(1, 2, 2)\nplt.plot(history.history['accuracy'], label='Training Accuracy')\nplt.plot(history.history['val_accuracy'], label='Validation Accuracy')\nplt.title('Training and Validation Accuracy')\nplt.xlabel('Epoch')\nplt.ylabel('Accuracy')\nplt.legend()\n\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-12-13T17:53:46.462569Z","iopub.execute_input":"2023-12-13T17:53:46.463276Z","iopub.status.idle":"2023-12-13T17:53:48.412521Z","shell.execute_reply.started":"2023-12-13T17:53:46.463241Z","shell.execute_reply":"2023-12-13T17:53:48.411621Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Plot Predictions","metadata":{}},{"cell_type":"markdown","source":"**Load the best model saved during training**","metadata":{}},{"cell_type":"code","source":"best_model = load_model('best_model.h5')","metadata":{"execution":{"iopub.status.busy":"2023-12-13T17:54:04.422070Z","iopub.execute_input":"2023-12-13T17:54:04.422470Z","iopub.status.idle":"2023-12-13T17:54:04.483358Z","shell.execute_reply.started":"2023-12-13T17:54:04.422436Z","shell.execute_reply":"2023-12-13T17:54:04.482527Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Making predictions**","metadata":{}},{"cell_type":"code","source":"all_predictions = best_model.predict(final_features)\nall_predicted_labels = np.argmax(all_predictions, axis=1)\ntrue_labels = np.argmax(y, axis=1)","metadata":{"execution":{"iopub.status.busy":"2023-12-13T17:54:06.477906Z","iopub.execute_input":"2023-12-13T17:54:06.478888Z","iopub.status.idle":"2023-12-13T17:54:07.804777Z","shell.execute_reply.started":"2023-12-13T17:54:06.478848Z","shell.execute_reply":"2023-12-13T17:54:07.803736Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Randomly select 20 predicted photos**","metadata":{}},{"cell_type":"code","source":"random.seed(6)\nrandom_indices = random.sample(range(len(resized_images)), 20)","metadata":{"execution":{"iopub.status.busy":"2023-12-13T17:54:10.283460Z","iopub.execute_input":"2023-12-13T17:54:10.283845Z","iopub.status.idle":"2023-12-13T17:54:10.288796Z","shell.execute_reply.started":"2023-12-13T17:54:10.283812Z","shell.execute_reply":"2023-12-13T17:54:10.287728Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Function to plot images with their predicted and true labels (matching in green, non-matching in red)**","metadata":{}},{"cell_type":"code","source":"def plot_images(images, predicted_labels, true_labels, class_names):\n    plt.figure(figsize=(15, 10))\n    for i in range(20):\n        plt.subplot(4, 5, i + 1)\n        plt.imshow(images[i])\n        pred_label = predicted_labels[i]\n        true_label = true_labels[i]\n        if pred_label == true_label:\n            title_color = 'green'\n        else:\n            title_color = 'red'\n        plt.title(f\"Predicted: {pred_label}\\nTrue: {true_label}\", color=title_color)\n        plt.axis('off')\n    plt.tight_layout()\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2023-12-13T17:54:12.755396Z","iopub.execute_input":"2023-12-13T17:54:12.755792Z","iopub.status.idle":"2023-12-13T17:54:12.765418Z","shell.execute_reply.started":"2023-12-13T17:54:12.755761Z","shell.execute_reply":"2023-12-13T17:54:12.764464Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Extract random images, predicted labels, and true labels**","metadata":{}},{"cell_type":"code","source":"images_random_20 = [resized_images[i] for i in random_indices]\npredicted_labels_random_20 = all_predicted_labels[random_indices]\ntrue_labels_random_20 = true_labels[random_indices]","metadata":{"execution":{"iopub.status.busy":"2023-12-13T17:54:15.241488Z","iopub.execute_input":"2023-12-13T17:54:15.241875Z","iopub.status.idle":"2023-12-13T17:54:15.247436Z","shell.execute_reply.started":"2023-12-13T17:54:15.241842Z","shell.execute_reply":"2023-12-13T17:54:15.246498Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Inverse transform the encoded labels to get actual dog breed names**","metadata":{}},{"cell_type":"code","source":"predicted_labels_names = label_encoder.inverse_transform(predicted_labels_random_20)\ntrue_labels_names = label_encoder.inverse_transform(true_labels_random_20)","metadata":{"execution":{"iopub.status.busy":"2023-12-13T17:54:17.427451Z","iopub.execute_input":"2023-12-13T17:54:17.427814Z","iopub.status.idle":"2023-12-13T17:54:17.434140Z","shell.execute_reply.started":"2023-12-13T17:54:17.427787Z","shell.execute_reply":"2023-12-13T17:54:17.433038Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Plot the random 20 predicted photos with actual dog breed names**","metadata":{}},{"cell_type":"code","source":"plot_images(images_random_20, predicted_labels_names, true_labels_names, class_names)","metadata":{"execution":{"iopub.status.busy":"2023-12-13T17:54:19.604919Z","iopub.execute_input":"2023-12-13T17:54:19.605334Z","iopub.status.idle":"2023-12-13T17:54:22.449869Z","shell.execute_reply.started":"2023-12-13T17:54:19.605302Z","shell.execute_reply":"2023-12-13T17:54:22.448821Z"},"trusted":true},"execution_count":null,"outputs":[]}]}