{"metadata":{"kernelspec":{"name":"python3","display_name":"Python 3","language":"python"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":7327,"databundleVersionId":861871,"sourceType":"competition"},{"sourceId":8699920,"sourceType":"datasetVersion","datasetId":5217872}],"dockerImageVersionId":30732,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"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 ydata_profiling import ProfileReport\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nimport random\nimport os\nimport cv2\nimport warnings\nwarnings.filterwarnings('ignore')\nprint(tf.config.list_physical_devices())\nprint(tf.__version__)","metadata":{"execution":{"iopub.status.busy":"2024-06-15T16:54:51.943527Z","iopub.execute_input":"2024-06-15T16:54:51.944335Z","iopub.status.idle":"2024-06-15T16:55:10.731992Z","shell.execute_reply.started":"2024-06-15T16:54:51.944240Z","shell.execute_reply":"2024-06-15T16:55:10.730825Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Data Analysis","metadata":{}},{"cell_type":"code","source":"df = pd.read_csv('/kaggle/input/dog-breed-identification/labels.csv')\ndf.head()","metadata":{"editable":true,"slideshow":{"slide_type":""},"tags":[],"execution":{"iopub.status.busy":"2024-06-15T16:55:19.082006Z","iopub.execute_input":"2024-06-15T16:55:19.083431Z","iopub.status.idle":"2024-06-15T16:55:19.135782Z","shell.execute_reply.started":"2024-06-15T16:55:19.083371Z","shell.execute_reply":"2024-06-15T16:55:19.134427Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Labels_Profile = ProfileReport(df, progress_bar= False)\nLabels_Profile.to_notebook_iframe()","metadata":{"execution":{"iopub.status.busy":"2024-06-15T16:55:22.146955Z","iopub.execute_input":"2024-06-15T16:55:22.147693Z","iopub.status.idle":"2024-06-15T16:55:33.175023Z","shell.execute_reply.started":"2024-06-15T16:55:22.147659Z","shell.execute_reply":"2024-06-15T16:55:33.173874Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\nimport matplotlib.pyplot as plt\nimport seaborn as sns\n\n# Ensure inline plotting in Jupyter notebooks\n%matplotlib inline\n\n# Load the dataset\ndf = pd.read_csv('/kaggle/input/dog-breed-identification/labels.csv')\n\n# Count the number of photographs for each breed\nbreed_counts = df['breed'].value_counts()\n\n# Plot the counts\nplt.figure(figsize=(20, 10))\nsns.barplot(x=breed_counts.index, y=breed_counts.values)\nplt.xticks(rotation=90)\nplt.xlabel('Breed')\nplt.ylabel('Number of Photographs')\nplt.title('Number of Photographs per Dog Breed')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-06-15T16:57:08.427064Z","iopub.execute_input":"2024-06-15T16:57:08.427502Z","iopub.status.idle":"2024-06-15T16:57:09.836538Z","shell.execute_reply.started":"2024-06-15T16:57:08.427470Z","shell.execute_reply":"2024-06-15T16:57:09.835380Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Count the number of photographs for each breed\nbreed_counts = df['breed'].value_counts()\n\n# Print the counts\nprint(breed_counts)","metadata":{"execution":{"iopub.status.busy":"2024-06-15T16:57:14.745158Z","iopub.execute_input":"2024-06-15T16:57:14.745683Z","iopub.status.idle":"2024-06-15T16:57:14.756446Z","shell.execute_reply.started":"2024-06-15T16:57:14.745643Z","shell.execute_reply":"2024-06-15T16:57:14.755165Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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":"2024-06-15T16:57:25.964210Z","iopub.execute_input":"2024-06-15T16:57:25.965517Z","iopub.status.idle":"2024-06-15T16:57:25.973706Z","shell.execute_reply.started":"2024-06-15T16:57:25.965468Z","shell.execute_reply":"2024-06-15T16:57:25.972451Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.shape\n","metadata":{"execution":{"iopub.status.busy":"2024-06-15T16:57:28.757900Z","iopub.execute_input":"2024-06-15T16:57:28.758317Z","iopub.status.idle":"2024-06-15T16:57:28.765271Z","shell.execute_reply.started":"2024-06-15T16:57:28.758285Z","shell.execute_reply":"2024-06-15T16:57:28.764033Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_dir = '/kaggle/input/dog-breed-identification/train'\ntest_dir = '/kaggle/input/dog-breed-identification/test'","metadata":{"execution":{"iopub.status.busy":"2024-06-15T16:57:59.556304Z","iopub.execute_input":"2024-06-15T16:57:59.556704Z","iopub.status.idle":"2024-06-15T16:57:59.561309Z","shell.execute_reply.started":"2024-06-15T16:57:59.556674Z","shell.execute_reply":"2024-06-15T16:57:59.560165Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 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":"2024-06-15T16:58:02.389426Z","iopub.execute_input":"2024-06-15T16:58:02.389843Z","iopub.status.idle":"2024-06-15T16:58:02.395626Z","shell.execute_reply.started":"2024-06-15T16:58:02.389807Z","shell.execute_reply":"2024-06-15T16:58:02.394462Z"},"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":"2024-06-15T16:58:05.747780Z","iopub.execute_input":"2024-06-15T16:58:05.748533Z","iopub.status.idle":"2024-06-15T16:58:25.656022Z","shell.execute_reply.started":"2024-06-15T16:58:05.748498Z","shell.execute_reply":"2024-06-15T16:58:25.654626Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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":"2024-06-15T17:00:01.270722Z","iopub.execute_input":"2024-06-15T17:00:01.271135Z","iopub.status.idle":"2024-06-15T17:00:02.620446Z","shell.execute_reply.started":"2024-06-15T17:00:01.271098Z","shell.execute_reply":"2024-06-15T17:00:02.619290Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"unique_breeds = df['breed'].unique()\nprint(unique_breeds)","metadata":{"execution":{"iopub.status.busy":"2024-06-15T17:00:40.116107Z","iopub.execute_input":"2024-06-15T17:00:40.117098Z","iopub.status.idle":"2024-06-15T17:00:40.122513Z","shell.execute_reply.started":"2024-06-15T17:00:40.117059Z","shell.execute_reply":"2024-06-15T17:00:40.121484Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Label Encoding**","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":"2024-06-15T17:01:42.757135Z","iopub.execute_input":"2024-06-15T17:01:42.758189Z","iopub.status.idle":"2024-06-15T17:01:42.764479Z","shell.execute_reply.started":"2024-06-15T17:01:42.758148Z","shell.execute_reply":"2024-06-15T17:01:42.763271Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = resized_images_array\ny = new_labels","metadata":{"execution":{"iopub.status.busy":"2024-06-15T17:02:36.554378Z","iopub.execute_input":"2024-06-15T17:02:36.554899Z","iopub.status.idle":"2024-06-15T17:02:36.559759Z","shell.execute_reply.started":"2024-06-15T17:02:36.554867Z","shell.execute_reply":"2024-06-15T17:02:36.558613Z"},"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.2, random_state=30)","metadata":{"execution":{"iopub.status.busy":"2024-06-15T17:02:39.151157Z","iopub.execute_input":"2024-06-15T17:02:39.151708Z","iopub.status.idle":"2024-06-15T17:02:39.313508Z","shell.execute_reply.started":"2024-06-15T17:02:39.151665Z","shell.execute_reply":"2024-06-15T17:02:39.312254Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(X_train.shape)\nprint(X_test.shape)\nprint(y_train.shape)\nprint(y_test.shape)","metadata":{"execution":{"iopub.status.busy":"2024-06-15T17:02:41.583479Z","iopub.execute_input":"2024-06-15T17:02:41.584507Z","iopub.status.idle":"2024-06-15T17:02:41.590791Z","shell.execute_reply.started":"2024-06-15T17:02:41.584462Z","shell.execute_reply":"2024-06-15T17:02:41.589519Z"},"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":"2024-06-15T17:03:32.353812Z","iopub.execute_input":"2024-06-15T17:03:32.354199Z","iopub.status.idle":"2024-06-15T17:03:32.360017Z","shell.execute_reply.started":"2024-06-15T17:03:32.354172Z","shell.execute_reply":"2024-06-15T17:03:32.358883Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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 = next(augmented_images)[0][0]  # Use `next` function\n    plt.subplot(4, 5, i + 1)\n    plt.imshow(augmented_image)\n    plt.axis('off')\nplt.tight_layout()\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2024-06-15T17:05:24.265456Z","iopub.execute_input":"2024-06-15T17:05:24.265884Z","iopub.status.idle":"2024-06-15T17:05:33.586095Z","shell.execute_reply.started":"2024-06-15T17:05:24.265852Z","shell.execute_reply":"2024-06-15T17:05:33.584960Z"},"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":"2024-06-15T17:05:39.102530Z","iopub.execute_input":"2024-06-15T17:05:39.102930Z","iopub.status.idle":"2024-06-15T17:05:39.639024Z","shell.execute_reply.started":"2024-06-15T17:05:39.102900Z","shell.execute_reply":"2024-06-15T17:05:39.637971Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Model Training**","metadata":{}},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport os\nimport cv2\nimport tensorflow as tf\nfrom tensorflow.keras.models import Sequential, Model\nfrom tensorflow.keras.layers import Dense, Conv2D, Flatten, Input, Dropout, MaxPooling2D, BatchNormalization, Activation, GlobalAveragePooling2D\nfrom tensorflow.keras.optimizers import Adam\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras.applications import EfficientNetB0  # Import EfficientNetB0\nfrom tensorflow.keras.utils import to_categorical\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.preprocessing import LabelEncoder\n","metadata":{"execution":{"iopub.status.busy":"2024-06-15T17:06:02.125854Z","iopub.execute_input":"2024-06-15T17:06:02.126717Z","iopub.status.idle":"2024-06-15T17:06:02.135378Z","shell.execute_reply.started":"2024-06-15T17:06:02.126677Z","shell.execute_reply":"2024-06-15T17:06:02.134344Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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":"2024-06-15T17:09:48.994756Z","iopub.execute_input":"2024-06-15T17:09:48.995798Z","iopub.status.idle":"2024-06-15T17:09:49.002069Z","shell.execute_reply.started":"2024-06-15T17:09:48.995760Z","shell.execute_reply":"2024-06-15T17:09:49.000985Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Feature Extraction**","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\n# xception_preprocessor = tf.keras.applications.xception.preprocess_input\n# xception_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":{"scrolled":true,"execution":{"iopub.status.busy":"2024-06-15T17:09:52.664122Z","iopub.execute_input":"2024-06-15T17:09:52.664886Z","iopub.status.idle":"2024-06-15T17:16:37.746667Z","shell.execute_reply.started":"2024-06-15T17:09:52.664848Z","shell.execute_reply":"2024-06-15T17:16:37.744700Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"final_features = np.concatenate([effnet_features, resnet_features], axis=-1)\n# test_xception_features\nprint('Final feature maps shape:', final_features.shape)","metadata":{"execution":{"iopub.status.busy":"2024-06-15T17:16:48.515669Z","iopub.execute_input":"2024-06-15T17:16:48.516353Z","iopub.status.idle":"2024-06-15T17:16:48.537068Z","shell.execute_reply.started":"2024-06-15T17:16:48.516319Z","shell.execute_reply":"2024-06-15T17:16:48.533538Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = Sequential()\nmodel.add(Dropout(0.7, input_shape=(final_features.shape[1],)))\nmodel.add(Dense(20, activation='softmax'))\nmodel.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])","metadata":{"editable":true,"slideshow":{"slide_type":""},"tags":[],"execution":{"iopub.status.busy":"2024-06-15T17:16:57.132862Z","iopub.execute_input":"2024-06-15T17:16:57.133782Z","iopub.status.idle":"2024-06-15T17:16:57.172157Z","shell.execute_reply.started":"2024-06-15T17:16:57.133747Z","shell.execute_reply":"2024-06-15T17:16:57.171168Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"checkpoint = ModelCheckpoint('best_model.h5.keras', 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":"2024-06-15T17:17:01.937009Z","iopub.execute_input":"2024-06-15T17:17:01.937428Z","iopub.status.idle":"2024-06-15T17:17:01.942758Z","shell.execute_reply.started":"2024-06-15T17:17:01.937397Z","shell.execute_reply":"2024-06-15T17:17:01.941799Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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":{"editable":true,"slideshow":{"slide_type":""},"tags":[],"execution":{"iopub.status.busy":"2024-06-15T17:17:05.991616Z","iopub.execute_input":"2024-06-15T17:17:05.992523Z","iopub.status.idle":"2024-06-15T17:17:08.580482Z","shell.execute_reply.started":"2024-06-15T17:17:05.992487Z","shell.execute_reply":"2024-06-15T17:17:08.579532Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Extract features using EfficientNetB4\ntest_effnet_features = get_features(EfficientNetB4, effnet_preprocessor, (224, 224, 3), X_test)\n\n# Extract features using ResNet50\ntest_resnet_features = get_features(ResNet50, resnet_preprocessor, (224, 224, 3), X_test)\n\n# test_xception_features = get_features(Xception, xception_preprocessor, (224,224, 3) , X_test)\n\n# Concatenate the features\ntest_final_features = np.concatenate([test_effnet_features, test_resnet_features], axis=-1)\n\nprint('Final test feature maps shape:', test_final_features.shape)\n","metadata":{"execution":{"iopub.status.busy":"2024-06-15T17:17:25.436983Z","iopub.execute_input":"2024-06-15T17:17:25.437522Z","iopub.status.idle":"2024-06-15T17:18:58.864483Z","shell.execute_reply.started":"2024-06-15T17:17:25.437487Z","shell.execute_reply":"2024-06-15T17:18:58.863281Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Predict on test data\n# Ensure you load the best model\nbest_model = load_model('best_model.h5.keras')\n\n\ntest_predictions = best_model.predict(test_final_features)\ntest_predicted_labels = np.argmax(test_predictions, axis=1)\ntrue_test_labels = np.argmax(y_test, axis=1)\n","metadata":{"execution":{"iopub.status.busy":"2024-06-15T17:30:14.469518Z","iopub.execute_input":"2024-06-15T17:30:14.469922Z","iopub.status.idle":"2024-06-15T17:30:14.665751Z","shell.execute_reply.started":"2024-06-15T17:30:14.469891Z","shell.execute_reply":"2024-06-15T17:30:14.664653Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import accuracy_score, classification_report, confusion_matrix\nimport seaborn as sns\nimport matplotlib.pyplot as plt\n\n# Accuracy\naccuracy = accuracy_score(true_test_labels, test_predicted_labels)\nprint(f'Accuracy: {accuracy * 100:.2f}%')\n\n# Classification Report\nclass_report = classification_report(true_test_labels, test_predicted_labels, target_names=label_encoder.classes_)\nprint(class_report)\n\n# Confusion Matrix\nconf_matrix = confusion_matrix(true_test_labels, test_predicted_labels)\n\nplt.figure(figsize=(12, 10))\nsns.heatmap(conf_matrix, annot=True, fmt='d', cmap='Blues', xticklabels=label_encoder.classes_, yticklabels=label_encoder.classes_)\nplt.title('Confusion Matrix')\nplt.xlabel('Predicted Label')\nplt.ylabel('True Label')\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2024-06-15T17:30:19.029148Z","iopub.execute_input":"2024-06-15T17:30:19.029557Z","iopub.status.idle":"2024-06-15T17:30:20.284689Z","shell.execute_reply.started":"2024-06-15T17:30:19.029526Z","shell.execute_reply":"2024-06-15T17:30:20.283632Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Prediction","metadata":{}},{"cell_type":"code","source":"correctly_classified_indices = np.where(true_test_labels == test_predicted_labels)[0]\n\nplt.figure(figsize=(12, 12))\nfor i, idx in enumerate(correctly_classified_indices[:20]):\n    plt.subplot(4, 5, i + 1)\n    plt.imshow(X_test[idx])\n    plt.title(f'True: {label_encoder.classes_[true_test_labels[idx]]}\\nPred: {label_encoder.classes_[test_predicted_labels[idx]]}')\n    plt.axis('off')\nplt.tight_layout()\nplt.show()\n","metadata":{"editable":true,"slideshow":{"slide_type":""},"tags":[],"execution":{"iopub.status.busy":"2024-06-15T17:30:29.858097Z","iopub.execute_input":"2024-06-15T17:30:29.858524Z","iopub.status.idle":"2024-06-15T17:30:32.256562Z","shell.execute_reply.started":"2024-06-15T17:30:29.858492Z","shell.execute_reply":"2024-06-15T17:30:32.255287Z"},"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":"2024-06-15T17:30:39.349862Z","iopub.execute_input":"2024-06-15T17:30:39.350229Z","iopub.status.idle":"2024-06-15T17:30:39.931326Z","shell.execute_reply.started":"2024-06-15T17:30:39.350201Z","shell.execute_reply":"2024-06-15T17:30:39.930292Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Identify misclassified indices\nmisclassified_indices = np.where(true_test_labels != test_predicted_labels)[0]\n\n# Plot misclassified examples\nplt.figure(figsize=(12, 12))\nfor i, idx in enumerate(misclassified_indices[:20]):\n    plt.subplot(4, 5, i + 1)\n    plt.imshow(X_test[idx])\n    plt.title(f'True: {label_encoder.classes_[true_test_labels[idx]]}\\nPred: {label_encoder.classes_[test_predicted_labels[idx]]}')\n    plt.axis('off')\nplt.tight_layout()\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2024-06-15T17:30:53.207907Z","iopub.execute_input":"2024-06-15T17:30:53.208278Z","iopub.status.idle":"2024-06-15T17:30:55.468264Z","shell.execute_reply.started":"2024-06-15T17:30:53.208249Z","shell.execute_reply":"2024-06-15T17:30:55.466787Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"markdown","source":"# **External Image For Testing that The Model is Working Expected Or Not!**","metadata":{}},{"cell_type":"code","source":"\nfrom keras.preprocessing import image\nimport numpy as np\nfrom tensorflow.keras.preprocessing import image\nfrom tensorflow.keras.applications import EfficientNetB4, ResNet50\nfrom tensorflow.keras.applications.efficientnet import preprocess_input as effnet_preprocess_input\nfrom tensorflow.keras.applications.resnet50 import preprocess_input as resnet_preprocess_input\nfrom tensorflow.keras.layers import Input, GlobalAveragePooling2D\nfrom tensorflow.keras.models import Model, load_model\nfrom sklearn.preprocessing import LabelEncoder\n\n# Function to load and preprocess the image\ndef load_and_preprocess_image(img_path):\n    img = image.load_img(img_path, target_size=(224, 224))\n    img_tensor = image.img_to_array(img)  # Convert the image to numpy array\n    img_tensor = np.expand_dims(img_tensor, axis=0)  # Add batch dimension\n    return img_tensor\n\n# Path to the specific image\nspecific_img_path = '/kaggle/input/external-dog-image-for-classifying/TibetanTerrier.jpg'\n\n# Load and preprocess the image\nimg_tensor = load_and_preprocess_image(specific_img_path)\n\n# Apply preprocessing functions\nimg_tensor_effnet = effnet_preprocess_input(img_tensor.copy())\nimg_tensor_resnet = resnet_preprocess_input(img_tensor.copy())\n\n# Extract features using EfficientNetB4\ntest_effnet_features = get_features(EfficientNetB4, effnet_preprocess_input, (224, 224, 3), img_tensor_effnet)\n\n# Extract features using ResNet50\ntest_resnet_features = get_features(ResNet50, resnet_preprocess_input, (224, 224, 3), img_tensor_resnet)\n\n# Concatenate the features\ntest_final_features = np.concatenate([test_effnet_features, test_resnet_features], axis=-1)\n\nprint(\"EfficientNet features shape:\", test_effnet_features.shape)\nprint(\"ResNet features shape:\", test_resnet_features.shape)\nprint(\"Concatenated features shape:\", test_final_features.shape)\n\n# Load the best model\nbest_model = load_model('best_model.h5.keras')\n\n# Make prediction\npredictions = best_model.predict(test_final_features)\nlabel_index = np.argmax(predictions[0])\n\n# Create a dictionary to map class indices to class labels\nclass_labels = {index: label for index, label in enumerate(label_encoder.classes_)}\npredicted_label = class_labels[label_index]\n\nprint(f\"Predicted label: {predicted_label}\")","metadata":{"editable":true,"slideshow":{"slide_type":""},"tags":[],"execution":{"iopub.status.busy":"2024-06-15T18:01:34.600027Z","iopub.execute_input":"2024-06-15T18:01:34.600449Z","iopub.status.idle":"2024-06-15T18:01:45.335083Z","shell.execute_reply.started":"2024-06-15T18:01:34.600417Z","shell.execute_reply":"2024-06-15T18:01:45.333881Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**I hope this notebook will helpful! I want to learn that if my steps and practices are true or not. Please give me a comment! I'm pretty new at this.**","metadata":{}}]}