{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# **Step 0: Import necessary tools**","metadata":{}},{"cell_type":"code","source":"import numpy as np \nimport pandas as pd \nimport tensorflow as tf\n\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import confusion_matrix\n\nimport matplotlib.pyplot as plt\n%matplotlib inline\nimport seaborn as sns\n\nimport os\nimport datetime\n\nfrom tqdm import tqdm\n\nfrom tensorflow.keras.utils import to_categorical\nfrom tensorflow.keras.models import Sequential, Model\nfrom tensorflow.keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPool2D\nfrom tensorflow.keras.optimizers import RMSprop,Adam\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras.applications import VGG16\nfrom tensorflow.keras import regularizers\n\nimport cv2 as cv","metadata":{"execution":{"iopub.status.busy":"2023-07-30T17:48:04.417872Z","iopub.execute_input":"2023-07-30T17:48:04.418350Z","iopub.status.idle":"2023-07-30T17:48:04.431943Z","shell.execute_reply.started":"2023-07-30T17:48:04.418316Z","shell.execute_reply":"2023-07-30T17:48:04.430534Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Step 1: Data loading**","metadata":{}},{"cell_type":"code","source":"# Data path declaration\n\nDATA_PATH = \"/kaggle/input/dog-breed-identification/\"\nMODELS_PATH = \"/kaggle/working/models/\"\nLOGS_PATH = \"/kaggle/working/logs/\"\nOUTPUT_PATH = \"/kaggle/working/output/\"","metadata":{"execution":{"iopub.status.busy":"2023-07-30T17:48:04.434242Z","iopub.execute_input":"2023-07-30T17:48:04.434726Z","iopub.status.idle":"2023-07-30T17:48:04.449138Z","shell.execute_reply.started":"2023-07-30T17:48:04.434693Z","shell.execute_reply":"2023-07-30T17:48:04.447993Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Verification of directory paths \n\nif not os.path.isdir(MODELS_PATH):\n    os.makedirs(MODELS_PATH)\nif not os.path.isdir(LOGS_PATH):\n    os.makedirs(LOGS_PATH)\nif not os.path.isdir(OUTPUT_PATH):\n    os.makedirs(OUTPUT_PATH)","metadata":{"execution":{"iopub.status.busy":"2023-07-30T17:48:04.451530Z","iopub.execute_input":"2023-07-30T17:48:04.451934Z","iopub.status.idle":"2023-07-30T17:48:04.461169Z","shell.execute_reply.started":"2023-07-30T17:48:04.451900Z","shell.execute_reply":"2023-07-30T17:48:04.460295Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# data loading\n\nlab_df = pd.read_csv(DATA_PATH + \"labels.csv\")","metadata":{"execution":{"iopub.status.busy":"2023-07-30T17:48:04.462251Z","iopub.execute_input":"2023-07-30T17:48:04.462801Z","iopub.status.idle":"2023-07-30T17:48:04.488798Z","shell.execute_reply.started":"2023-07-30T17:48:04.462769Z","shell.execute_reply":"2023-07-30T17:48:04.487853Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Step 2: Understanding data**","metadata":{}},{"cell_type":"code","source":"display(lab_df.head(10))","metadata":{"execution":{"iopub.status.busy":"2023-07-30T17:48:04.491317Z","iopub.execute_input":"2023-07-30T17:48:04.491686Z","iopub.status.idle":"2023-07-30T17:48:04.502423Z","shell.execute_reply.started":"2023-07-30T17:48:04.491655Z","shell.execute_reply":"2023-07-30T17:48:04.501482Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"lab_df.isnull().values.any() # checking for null  values if any","metadata":{"execution":{"iopub.status.busy":"2023-07-30T17:48:04.503879Z","iopub.execute_input":"2023-07-30T17:48:04.504507Z","iopub.status.idle":"2023-07-30T17:48:04.520753Z","shell.execute_reply.started":"2023-07-30T17:48:04.504475Z","shell.execute_reply":"2023-07-30T17:48:04.519820Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"display(lab_df.describe())","metadata":{"execution":{"iopub.status.busy":"2023-07-30T17:48:04.522301Z","iopub.execute_input":"2023-07-30T17:48:04.523152Z","iopub.status.idle":"2023-07-30T17:48:04.552673Z","shell.execute_reply.started":"2023-07-30T17:48:04.523111Z","shell.execute_reply":"2023-07-30T17:48:04.551477Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"lab_df.breed.value_counts().plot.bar(figsize=(20, 10))","metadata":{"execution":{"iopub.status.busy":"2023-07-30T17:48:04.554149Z","iopub.execute_input":"2023-07-30T17:48:04.555129Z","iopub.status.idle":"2023-07-30T17:48:06.116091Z","shell.execute_reply.started":"2023-07-30T17:48:04.555061Z","shell.execute_reply":"2023-07-30T17:48:06.114983Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"i = 0\nimg_height = []\nimg_width = []\nimg=[]\n\nfor f, breed in tqdm(lab_df.values):\n    img = cv.imread(DATA_PATH + 'train/'+ '{}.jpg'.format(f))\n    img_height.append(img.shape[0])\n    img_width.append(img.shape[1])  ","metadata":{"execution":{"iopub.status.busy":"2023-07-30T17:48:06.117676Z","iopub.execute_input":"2023-07-30T17:48:06.118183Z","iopub.status.idle":"2023-07-30T17:49:57.976945Z","shell.execute_reply.started":"2023-07-30T17:48:06.118144Z","shell.execute_reply":"2023-07-30T17:49:57.975933Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(12,5))\nplt.subplot(121)\nplt.xlabel(\"height\")\nsns.histplot(img_height)\n\nplt.figure(figsize=(12,5))\nplt.subplot(122)\nplt.xlabel(\"Width\")\nsns.histplot(img_width)","metadata":{"execution":{"iopub.status.busy":"2023-07-30T17:49:57.978713Z","iopub.execute_input":"2023-07-30T17:49:57.979367Z","iopub.status.idle":"2023-07-30T17:49:59.384909Z","shell.execute_reply.started":"2023-07-30T17:49:57.979326Z","shell.execute_reply":"2023-07-30T17:49:59.383650Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"Width: Max {}, Mean {}, Median {}, Min {}\".format(np.max(img_width),np.mean(img_width),np.median(img_width),np.min(img_width)))\nprint(\"Hight: Max {}, Mean {}, Median {}, Min {}\".format(np.max(img_height),np.mean(img_height),np.median(img_height),np.min(img_height)))","metadata":{"execution":{"iopub.status.busy":"2023-07-30T17:49:59.389078Z","iopub.execute_input":"2023-07-30T17:49:59.389417Z","iopub.status.idle":"2023-07-30T17:49:59.411773Z","shell.execute_reply.started":"2023-07-30T17:49:59.389390Z","shell.execute_reply":"2023-07-30T17:49:59.410664Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Conclusion:**\n\n1. We have 120 breeds with total dataset pf 10222 images\n2. It is clear that the data set is highly skewed.\n3. In both plots normal distribution is found. Effect of outliers is important and shall be omitted. \n4. Effect of orientation (landscape/portrait) shall also be considered. \n\nReserving this scope for fine tuning. For now all images shall be resized to 500 width and 375 height","metadata":{}},{"cell_type":"markdown","source":"# **Step 3: EDA**","metadata":{}},{"cell_type":"markdown","source":"# **Step 3A: Omition of data noise (multi dogs, human face etc.)**","metadata":{}},{"cell_type":"code","source":"face_classifier = cv.CascadeClassifier(cv.data.haarcascades + \"haarcascade_frontalface_default.xml\")\n\n# Single dog images\n\ndog_img=[]\ndog_classes=[]\nD_img_height=[]\nD_img_width=[]\n\n# Image with noise \n\nmulti_img=[]\nmulti_classes=[]\nO_img_height=[]\nO_img_width=[]\n\n#load training images\nfor f, breed in tqdm(lab_df.values):\n  imag = cv.imread(DATA_PATH + 'train/'+ '{}.jpg'.format(f))\n  gray_image = cv.cvtColor(imag, cv.COLOR_BGR2GRAY)  \n  face = face_classifier.detectMultiScale(gray_image, scaleFactor=1.1, minNeighbors=5, minSize=(40, 40))\n  No_of_faces = len(face)\n  if No_of_faces > 1:\n    multi_classes.append(breed)\n    multi_img.append(imag)\n    O_img_height.append(imag.shape[0])\n    O_img_width.append(imag.shape[1])\n    O_img= imag\n  else:\n    dog_classes.append(breed)\n    dog_img.append(imag)\n    D_img_height.append(imag.shape[0])\n    D_img_width.append(imag.shape[1])\n    D_img= imag","metadata":{"execution":{"iopub.status.busy":"2023-07-30T17:49:59.412929Z","iopub.execute_input":"2023-07-30T17:49:59.413247Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Verifying dog image after filter\n\nimg_rgb = cv.cvtColor(dog_img[200], cv.COLOR_BGR2RGB)\nplt.figure(figsize=(20,10))\nplt.imshow(img_rgb)\nplt.title(dog_classes[200])\nplt.axis('off')","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Verifying mixed image after filter\n\nimg_rgb = cv.cvtColor(multi_img[250], cv.COLOR_BGR2RGB)\nplt.figure(figsize=(20,10))\nplt.imshow(img_rgb)\nplt.title(multi_classes[250])\nplt.axis('off')","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Statestical insight on filtered dog image\n\nplt.figure(figsize=(12,5))\nplt.subplot(121)\nplt.xlabel(\"height {Dog data}\")\nsns.histplot(D_img_height)\n\nplt.figure(figsize=(12,5))\nplt.subplot(122)\nplt.xlabel(\"Width {Dog data}\")\nsns.histplot(D_img_width)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Statestical insight on filtered mixed image\n\nplt.figure(figsize=(12,5))\nplt.subplot(121)\nplt.xlabel(\"height {Other data}\")\nsns.histplot(O_img_height)\n\nplt.figure(figsize=(12,5))\nplt.subplot(122)\nplt.xlabel(\"Width {Other data}\")\nsns.histplot(O_img_width)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Conclusion:**\n\nTotal 264 images are identified where in, human interaction or multiple dogs are identified. \n\nSuch images are dropped from dataset.","metadata":{}},{"cell_type":"markdown","source":"# **Step 3B: Outlier treatment for size**","metadata":{}},{"cell_type":"code","source":"dfh = pd.DataFrame(D_img_height, columns = ['height'])\n\nP25_h = dfh['height'].quantile(0.25)\nP75_h = dfh['height'].quantile(0.75)\nIQR_h = P75_h - P25_h\n\nUL_h = P75_h + 1.5 * IQR_h\nLL_h = P25_h - 1.5 * IQR_h\n\ndfw= pd.DataFrame(D_img_width, columns = ['width'])\n\nP25_w = dfw['width'].quantile(0.25)\nP75_w = dfw['width'].quantile(0.75)\nIQR_w = P75_w - P25_w\n\nUL_w = P75_w + 1.5 * IQR_w\nLL_w = P25_w - 1.5 * IQR_w\n\nprint(\"Height: 25 percetile limit {}, 75 percetile limit limit {}, IQR {}, Upper limit {}, Lower limit {}\".format(P25_h, P75_h, IQR_h, UL_h, LL_h))\nprint(\"Width: 25 percetile limit {}, 75 percetile limit limit {}, IQR {}, Upper limit {}, Lower limit {}\".format(P25_w, P75_w, IQR_w, UL_w, LL_w))\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Omition of outliers from analysis\n\ndog_img_F=[]\ndog_classes_F=[]\nD_img_height_F=[]\nD_img_width_F=[]\n\nfor i in tqdm(D_img_height):\n  if ((D_img_height[i]<=UL_h) and (D_img_height[i]>=LL_h) and (D_img_width[i]<=UL_w) and (D_img_width[i]>=LL_w)):\n    dog_img_F.append(dog_img[i])\n    dog_classes_F.append(dog_classes[i])\n    D_img_height_F.append(D_img_height[i])\n    D_img_width_F.append(D_img_width[i])","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Statestical insight after omition of outliers\n\nplt.figure(figsize=(12,5))\nplt.subplot(121)\nplt.xlabel(\"height\")\nsns.histplot(D_img_height_F)\n\nplt.figure(figsize=(12,5))\nplt.subplot(122)\nplt.xlabel(\"Width\")\nsns.histplot(D_img_width_F)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Step 3C: Resize & Normalise image**\n\nDue to computation limitation, sample database is limited to 1000 randomly chosen images is created from processed database. these are random chosen datapoints.","metadata":{}},{"cell_type":"code","source":"import random\nrandom_index = random.sample(range(0,len(dog_classes_F)), 1000)\nprint(random_index)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dog_img_sample=[]\ndog_classes_sample=[]\n\nfor i in tqdm(range(1000)):\n  j=random_index[i]\n  dog_img_sample.append(dog_img_F[j])\n  dog_classes_sample.append(dog_classes_F[j])","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Image preprocessing routine\n\ndef image_preprocessing(image, W, H):\n\n    # Load Image\n    readFlag=cv.COLOR_BGR2GRAY\n    image_gray = cv.cvtColor(image, readFlag)\n    \n    # Crop Image\n    mid_row = int(image_gray.shape[0]/2)\n    mid_col = int(image_gray.shape[1]/2)\n    if image_gray.shape[0]>image_gray.shape[1]:\n        image_cropped = image_gray[mid_row-mid_col:mid_row+mid_col,\n                                   0:image_gray.shape[1]]\n    else:\n        image_cropped = image_gray[0:image_gray.shape[0],\n                                   mid_col-mid_row:mid_col+mid_row]\n    \n    # Rescale Image\n    image_rescale = cv.resize(image_cropped,\n                               dsize=(W, H),\n                               interpolation=cv.INTER_AREA)\n    return image_rescale","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = np.empty((len(dog_classes_sample), 224, 224), dtype=np.uint8)\n\nfor i in tqdm(range(1000)):\n  image_rescaled = image_preprocessing(dog_img_sample[i], 224, 224) # due to computation limitation, need to drop image size to 224 * 224\n  X[i, :, :] = image_rescaled\n\nX = X.astype('float32')/255","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Y = pd.get_dummies(dog_classes_sample)\nX = X.reshape(-1,224,224,1)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('\\nTrain Images shape: ',X.shape,' size: {:,}'.format(X.size))\nprint('One-hot encoded output shape: ',Y.shape,' size: {:,}'.format(Y.size))","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Step 4: Model building**","metadata":{}},{"cell_type":"markdown","source":"# **Step 4a: Train test split**","metadata":{}},{"cell_type":"code","source":"X_train, X_val, Y_train, Y_val = train_test_split(X, Y, test_size = 0.2)\n\nprint('Shape X train data:', X_train.shape)\nprint('Shape Y train data:', Y_train.shape)\nprint('Shape X val data:', X_val.shape)\nprint('Shape Y val data:', Y_val.shape) ","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Step 4b: Model building and Tensor board recording**","metadata":{}},{"cell_type":"code","source":"%load_ext tensorboard","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Function to build a TensorBoard callback\ndef create_tensorboard_callback():\n    # Create a log directory for storing TensorBoard logs\n    logdir = os.path.join(LOGS_PATH, datetime.datetime.now().strftime(\"%Y%m%d-%H%M%S\"))\n    # make it so the logs get tracked whenever we run an experiment\n    return tf.keras.callbacks.TensorBoard(logdir)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Create Early Stopping callback\nearly_stopping = tf.keras.callbacks.EarlyStopping(monitor=\"val_accuracy\", patience=5)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Building the Model\nmodel = Sequential()\n\nmodel.add(Conv2D(filters = 64, kernel_size = (5,5), activation ='relu', input_shape = (224,224,1)))\nmodel.add(MaxPool2D(pool_size=(2,2)))\n\nmodel.add(Conv2D(filters = 32, kernel_size = (5,5), activation ='relu', kernel_regularizer = 'l2'))\nmodel.add(MaxPool2D(pool_size=(2,2))) #3 , 3\n\nmodel.add(Conv2D(filters = 16, kernel_size = (5,5), activation ='relu', kernel_regularizer = 'l2'))\nmodel.add(MaxPool2D(pool_size=(2,2))) # 7,7\n\nmodel.add(Conv2D(filters = 8, kernel_size = (5,5), activation ='relu', kernel_regularizer = 'l2'))\nmodel.add(MaxPool2D(pool_size=(2,2)))\n\nmodel.add(Flatten())\nmodel.add(Dense(128, activation = \"relu\", kernel_regularizer = 'l2'))\nmodel.add(Dense(64, activation = \"relu\", kernel_regularizer = 'l2'))\nmodel.add(Dense(Y_train.shape[1], activation = \"softmax\"))\n\nmodel.compile(loss = 'categorical_crossentropy', optimizer = Adam(0.001),metrics=['accuracy'])\n\nmodel.summary()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Step 4c: Model training**","metadata":{}},{"cell_type":"code","source":"# Create a new TensorBoard session everytime we train a model\ntensorboard = create_tensorboard_callback()\n\n# Training the model\nepochs = 25\nbatch_size = 50\n\nhistory = model.fit(X_train, Y_train, batch_size = batch_size, epochs = epochs, validation_data = (X_val, Y_val),callbacks=[tensorboard, early_stopping])","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Step 5: Result validation**","metadata":{}},{"cell_type":"markdown","source":"# **Step 5a: Test result analysis**","metadata":{}},{"cell_type":"code","source":"%tensorboard --logdir LOGS_PATH","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_loss = model.history.history['loss']\nval_loss = model.history.history['val_loss']\ntrain_accuracy = model.history.history['accuracy']\nvalidation_accuracy = model.history.history['val_accuracy']","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(train_loss)\nplt.plot(val_loss)\nplt.xlabel(\"Epoch\")\nplt.ylabel(\"Loss\")\nplt.title(\"LOSS CURVE\")\nplt.legend(['Train', 'Validation'])\nplt.grid()\nplt.show()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(train_accuracy)\nplt.plot(validation_accuracy)\nplt.xlabel(\"Epoch\")\nplt.ylabel(\"Accuracy\")\nplt.title(\"ACCURACY CURVE\")\nplt.legend(['Train', 'Validation'])\nplt.grid()\nplt.show()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Step 5b: Model validation (fresh 1000 samples)**","metadata":{}},{"cell_type":"code","source":"random_test_index = random.sample(range(0,len(dog_classes_F)), 1000)\nprint(random_test_index)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dog_img_test_sample=[]\ndog_classes_test_sample=[]\n\nfor i in tqdm(range(1000)):\n  j=random_test_index[i]\n  dog_img_test_sample.append(dog_img_F[j])\n  dog_classes_test_sample.append(dog_classes_F[j])","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_test = np.empty((len(dog_classes_test_sample), 224, 224), dtype=np.uint8)\n\nfor i in tqdm(range(1000)):\n  image_rescaled = image_preprocessing(dog_img_test_sample[i], 224, 224) # due to computation limitation, need to drop image size to 224 * 224\n  X_test[i, :, :] = image_rescaled\n\nX_test = X_test.astype('float32')/255","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Y_test = pd.get_dummies(dog_classes_test_sample)\nX_test = X_test.reshape(-1,224,224,1)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('\\nTest Images shape: ',X_test.shape,' size: {:,}'.format(X_test.size))\nprint('One-hot encoded output shape: ',Y_test.shape,' size: {:,}'.format(Y_test.size))","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Predisction for 1000 random test samples\n\npredictions = model.predict(X_test, verbose=1)\npredictions","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def image_predict(index):\n    \n    # Result visualisation for individual sample\n    \n    Prob_mat = predictions[index] \n    Pred_label = Y_train.columns[np.argmax(predictions[index])]\n    True_label = dog_classes_test_sample[index]\n\n    # Find the top 5 prediction confidence indexes\n    Top_5_pred_indexes = Prob_mat.argsort()[-5:][::-1]\n\n    # Find the prediction connfidence values of top 5\n    Top_5_pred_values = Prob_mat[Top_5_pred_indexes]\n\n    # Find the prediction labels of top 5\n    Top_5_pred_labels = Y_train.columns[Top_5_pred_indexes]\n\n    # Setup plot\n    top_plot = plt.bar(np.arange(len(Top_5_pred_labels)),Top_5_pred_values, color=\"grey\")\n\n    plt.xticks(np.arange(len(Top_5_pred_labels)),labels=Top_5_pred_labels, rotation=\"vertical\")\n\n    # Change color of true label\n    if np.isin(True_label, Top_5_pred_labels):\n      top_plot[np.argmax(Top_5_pred_labels == True_label)].set_color(\"green\")\n    ","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_predict(52)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Val_Pred_label = []\nVal_True_label = []\n\nfor i in tqdm(range(1000)): \n    Val_Pred_label.append(Y_train.columns[np.argmax(predictions[i])])\n    Val_True_label.append(dog_classes_test_sample[i])","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_result = pd.DataFrame()\ndf_result['Val_True'] = Val_True_label\ndf_result['Val_Pred'] = Val_True_label","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_result","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_result.to_excel(\"output.xlsx\")","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}