{"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"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import pandas as pd \nimport numpy as np\nimport matplotlib.pyplot as plt \nfrom matplotlib.pyplot import imread\nimport os \nimport random\nfrom IPython.display import  Image, display\n\nimport tensorflow as tf\nfrom keras import layers \nfrom sklearn.model_selection import train_test_split","metadata":{"execution":{"iopub.status.busy":"2023-09-10T09:46:12.953320Z","iopub.execute_input":"2023-09-10T09:46:12.953818Z","iopub.status.idle":"2023-09-10T09:46:12.960997Z","shell.execute_reply.started":"2023-09-10T09:46:12.953783Z","shell.execute_reply":"2023-09-10T09:46:12.960022Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.listdir(\"/kaggle/input/dog-breed-identification/\")","metadata":{"execution":{"iopub.status.busy":"2023-09-10T09:46:12.967046Z","iopub.execute_input":"2023-09-10T09:46:12.967667Z","iopub.status.idle":"2023-09-10T09:46:12.986354Z","shell.execute_reply.started":"2023-09-10T09:46:12.967635Z","shell.execute_reply":"2023-09-10T09:46:12.985071Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_images_path = \"/kaggle/input/dog-breed-identification/train\"\ntest_images_path = \"/kaggle/input/dog-breed-identification/test\"","metadata":{"execution":{"iopub.status.busy":"2023-09-10T09:46:12.989014Z","iopub.execute_input":"2023-09-10T09:46:12.989738Z","iopub.status.idle":"2023-09-10T09:46:12.995664Z","shell.execute_reply.started":"2023-09-10T09:46:12.989697Z","shell.execute_reply":"2023-09-10T09:46:12.994275Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels_df = pd.read_csv(\"/kaggle/input/dog-breed-identification/labels.csv\")\nlabels_df.head()","metadata":{"execution":{"iopub.status.busy":"2023-09-10T09:46:12.997314Z","iopub.execute_input":"2023-09-10T09:46:12.997687Z","iopub.status.idle":"2023-09-10T09:46:13.033825Z","shell.execute_reply.started":"2023-09-10T09:46:12.997637Z","shell.execute_reply":"2023-09-10T09:46:13.033017Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels_df.describe()","metadata":{"execution":{"iopub.status.busy":"2023-09-10T09:46:13.036188Z","iopub.execute_input":"2023-09-10T09:46:13.036870Z","iopub.status.idle":"2023-09-10T09:46:13.061381Z","shell.execute_reply.started":"2023-09-10T09:46:13.036819Z","shell.execute_reply":"2023-09-10T09:46:13.060216Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def is_images_same(target_dir, target_df): \n    \"\"\"\n    That function going to check images in target_dir are equal to\n    image list in target_df \n    \"\"\"\n    len_target_dir = len(os.listdir(target_dir))\n    len_target_df = len(target_df)\n    if len_target_dir == len_target_df: \n        print(f\"Both are having same no of images:{len_target_dir}\")\n    else: \n        print(f\"Target dir having {len_target_dir} images while Target DF having {len_target_df}\")\n        \nis_images_same(target_dir = train_images_path, target_df = labels_df)","metadata":{"execution":{"iopub.status.busy":"2023-09-10T09:46:13.063177Z","iopub.execute_input":"2023-09-10T09:46:13.064042Z","iopub.status.idle":"2023-09-10T09:46:13.076566Z","shell.execute_reply.started":"2023-09-10T09:46:13.064003Z","shell.execute_reply":"2023-09-10T09:46:13.075684Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from IPython.display import  Image, display\nimport random\n\nexample = random.choice(os.listdir(train_images_path))\nexample = train_images_path + \"/\" + example\n\nImage(example)","metadata":{"execution":{"iopub.status.busy":"2023-09-10T09:46:13.077702Z","iopub.execute_input":"2023-09-10T09:46:13.078255Z","iopub.status.idle":"2023-09-10T09:46:13.093487Z","shell.execute_reply.started":"2023-09-10T09:46:13.078216Z","shell.execute_reply":"2023-09-10T09:46:13.092212Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels_df.columns","metadata":{"execution":{"iopub.status.busy":"2023-09-10T09:46:13.095461Z","iopub.execute_input":"2023-09-10T09:46:13.096474Z","iopub.status.idle":"2023-09-10T09:46:13.104764Z","shell.execute_reply.started":"2023-09-10T09:46:13.096433Z","shell.execute_reply":"2023-09-10T09:46:13.103910Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels_df[\"breed\"].value_counts().plot.bar(figsize=(40,20))\nprint(f\"Average Images per breed:{int(labels_df['breed'].value_counts().sum()/len(labels_df['breed'].unique()))}\")\nprint(f\"Total no of breeds:{len(labels_df['breed'].unique())}\")","metadata":{"execution":{"iopub.status.busy":"2023-09-10T09:46:13.107584Z","iopub.execute_input":"2023-09-10T09:46:13.108344Z","iopub.status.idle":"2023-09-10T09:46:15.053827Z","shell.execute_reply.started":"2023-09-10T09:46:13.108311Z","shell.execute_reply":"2023-09-10T09:46:15.052562Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fileNames = [train_images_path + \"/\" + fName + \".jpg\" for fName in labels_df[\"id\"]]\nfileNames[:10]","metadata":{"execution":{"iopub.status.busy":"2023-09-10T09:46:15.055478Z","iopub.execute_input":"2023-09-10T09:46:15.055818Z","iopub.status.idle":"2023-09-10T09:46:15.071174Z","shell.execute_reply.started":"2023-09-10T09:46:15.055790Z","shell.execute_reply":"2023-09-10T09:46:15.069933Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"classNames = labels_df[\"breed\"].unique()\nclassNames[:5]\n\ntarget_labels = [breed for breed in labels_df[\"breed\"]]\ntarget_labels_encoded = [label == np.array(classNames) for label in target_labels]","metadata":{"execution":{"iopub.status.busy":"2023-09-10T09:46:15.074131Z","iopub.execute_input":"2023-09-10T09:46:15.074874Z","iopub.status.idle":"2023-09-10T09:46:15.208907Z","shell.execute_reply.started":"2023-09-10T09:46:15.074828Z","shell.execute_reply":"2023-09-10T09:46:15.207598Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"NUM_IMAGE = 2000\n\nX_train, X_val, Y_train, Y_val = train_test_split(fileNames[:NUM_IMAGE], target_labels_encoded[:NUM_IMAGE], test_size=0.2, random_state=42)\nlen(X_train), len(X_val), len(Y_train), len(Y_val)","metadata":{"execution":{"iopub.status.busy":"2023-09-10T09:46:15.210711Z","iopub.execute_input":"2023-09-10T09:46:15.211146Z","iopub.status.idle":"2023-09-10T09:46:15.223803Z","shell.execute_reply.started":"2023-09-10T09:46:15.211107Z","shell.execute_reply":"2023-09-10T09:46:15.222467Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train[0], Y_train[0]","metadata":{"execution":{"iopub.status.busy":"2023-09-10T09:46:15.225259Z","iopub.execute_input":"2023-09-10T09:46:15.225628Z","iopub.status.idle":"2023-09-10T09:46:15.237313Z","shell.execute_reply.started":"2023-09-10T09:46:15.225599Z","shell.execute_reply":"2023-09-10T09:46:15.236150Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img = imread(example)\nplt.imshow(img)\n\nprint(f\"Image shape: {img.shape}\")","metadata":{"execution":{"iopub.status.busy":"2023-09-10T09:46:15.238739Z","iopub.execute_input":"2023-09-10T09:46:15.239170Z","iopub.status.idle":"2023-09-10T09:46:15.715165Z","shell.execute_reply.started":"2023-09-10T09:46:15.239133Z","shell.execute_reply":"2023-09-10T09:46:15.713924Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tf.constant(img)","metadata":{"execution":{"iopub.status.busy":"2023-09-10T09:46:15.716752Z","iopub.execute_input":"2023-09-10T09:46:15.717184Z","iopub.status.idle":"2023-09-10T09:46:15.727650Z","shell.execute_reply.started":"2023-09-10T09:46:15.717149Z","shell.execute_reply":"2023-09-10T09:46:15.726486Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tf.image.convert_image_dtype(img, tf.float32)","metadata":{"execution":{"iopub.status.busy":"2023-09-10T09:46:15.732810Z","iopub.execute_input":"2023-09-10T09:46:15.733904Z","iopub.status.idle":"2023-09-10T09:46:15.746613Z","shell.execute_reply.started":"2023-09-10T09:46:15.733836Z","shell.execute_reply":"2023-09-10T09:46:15.745530Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"IMAGE_SIZE = 224\n\ndef process_image(image_path): \n    img = tf.io.read_file(image_path)\n    # Turn the jpeg image into numerical Tensor with 3 colour channels (Red, Green, Blue)\n    img = tf.io.decode_image(img, channels =3)\n    # Convert the colour channel values from 0-225 values to 0-1 values\n    img = tf.image.convert_image_dtype(img, tf.float32)\n    # Resize the image to our desired size (224, 244)\n    img = tf.image.resize_with_crop_or_pad(img, 224, 224)\n    return img","metadata":{"execution":{"iopub.status.busy":"2023-09-10T09:46:15.748104Z","iopub.execute_input":"2023-09-10T09:46:15.748468Z","iopub.status.idle":"2023-09-10T09:46:15.755138Z","shell.execute_reply.started":"2023-09-10T09:46:15.748431Z","shell.execute_reply":"2023-09-10T09:46:15.754338Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_image_label(image_path, label):\n    image = process_image(image_path)\n    return image, label\n\nget_image_label(X_train[10], Y_train[10])","metadata":{"execution":{"iopub.status.busy":"2023-09-10T09:46:15.756709Z","iopub.execute_input":"2023-09-10T09:46:15.757027Z","iopub.status.idle":"2023-09-10T09:46:15.791111Z","shell.execute_reply.started":"2023-09-10T09:46:15.757001Z","shell.execute_reply":"2023-09-10T09:46:15.790259Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"BATCH_SIZE = 32\n\ndef create_data_batches(X, y=None, batch_size = BATCH_SIZE, valid_data= False, test_data=False): \n    if test_data: \n        test_data = tf.data.Dataset.from_tensor_slices(tf.constant(X))\n        test_data = test_data.map(process_image).batch(BATCH_SIZE) \n        return test_data \n    \n    if valid_data: \n        valid_data = tf.data.Dataset.from_tensor_slices((tf.constant(X),tf.constant(y)))\n        valid_data = valid_data.map(get_image_label).batch(BATCH_SIZE)\n        return valid_data\n    \n    #shuffle and create training data\n    else:  \n        train_data = tf.data.Dataset.from_tensor_slices((tf.constant(X),tf.constant(y))).shuffle(buffer_size = len(X))\n        train_data = train_data.map(get_image_label).batch(BATCH_SIZE) \n        return train_data ","metadata":{"execution":{"iopub.status.busy":"2023-09-10T09:46:15.792412Z","iopub.execute_input":"2023-09-10T09:46:15.792935Z","iopub.status.idle":"2023-09-10T09:46:15.802084Z","shell.execute_reply.started":"2023-09-10T09:46:15.792899Z","shell.execute_reply":"2023-09-10T09:46:15.800952Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data = create_data_batches(X_train, Y_train)\nvalid_data = create_data_batches(X_val, Y_val, valid_data=True)","metadata":{"execution":{"iopub.status.busy":"2023-09-10T09:46:15.803759Z","iopub.execute_input":"2023-09-10T09:46:15.804419Z","iopub.status.idle":"2023-09-10T09:46:16.243420Z","shell.execute_reply.started":"2023-09-10T09:46:15.804387Z","shell.execute_reply":"2023-09-10T09:46:16.242229Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample = next(iter(train_data))\nsample[0][0]","metadata":{"execution":{"iopub.status.busy":"2023-09-10T09:46:16.245134Z","iopub.execute_input":"2023-09-10T09:46:16.245556Z","iopub.status.idle":"2023-09-10T09:46:16.388520Z","shell.execute_reply.started":"2023-09-10T09:46:16.245514Z","shell.execute_reply":"2023-09-10T09:46:16.387445Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def show_images(images, label):\n    plt.figure(figsize = (12,12))\n    \n    for i in range(0,16): \n        ax = plt.subplot(4, 4, i+1)\n        plt.imshow(images[i])\n        plt.title(classNames[tf.argmax(label[i])])\n        plt.axis(\"off\")","metadata":{"execution":{"iopub.status.busy":"2023-09-10T09:46:16.390841Z","iopub.execute_input":"2023-09-10T09:46:16.391295Z","iopub.status.idle":"2023-09-10T09:46:16.400960Z","shell.execute_reply.started":"2023-09-10T09:46:16.391256Z","shell.execute_reply":"2023-09-10T09:46:16.399650Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_images, train_labels = next(train_data.as_numpy_iterator())\nshow_images(train_images, train_labels)","metadata":{"execution":{"iopub.status.busy":"2023-09-10T09:46:16.402291Z","iopub.execute_input":"2023-09-10T09:46:16.402984Z","iopub.status.idle":"2023-09-10T09:46:18.902274Z","shell.execute_reply.started":"2023-09-10T09:46:16.402950Z","shell.execute_reply":"2023-09-10T09:46:18.901130Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"val_images, val_labels = next(valid_data.as_numpy_iterator())\nshow_images(val_images, val_labels)","metadata":{"execution":{"iopub.status.busy":"2023-09-10T09:46:18.903463Z","iopub.execute_input":"2023-09-10T09:46:18.904528Z","iopub.status.idle":"2023-09-10T09:46:21.165599Z","shell.execute_reply.started":"2023-09-10T09:46:18.904494Z","shell.execute_reply":"2023-09-10T09:46:21.164315Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras import layers \n\ndef create_model():\n    base_model = tf.keras.applications.mobilenet_v2.MobileNetV2(include_top = False, \n                                                     classes = len(classNames)) \n    base_model.trainable = False \n\n    inputs = layers.Input(shape = (224,224,3))\n    x = base_model(inputs, training = False) \n    x = tf.keras.layers.GlobalAveragePooling2D(name= \"global_average_pooling\")(x)\n    x = layers.Dropout(0.2)(x)\n    outputs = tf.keras.layers.Dense(len(classNames), activation=\"softmax\")(x)\n\n\n    ModelDogBreed = tf.keras.Model(inputs, outputs) \n\n    ModelDogBreed.compile(loss = \"categorical_crossentropy\", \n                         optimizer = tf.keras.optimizers.Adam(), \n                         metrics=[\"accuracy\"]) \n\n    return ModelDogBreed","metadata":{"execution":{"iopub.status.busy":"2023-09-10T09:46:21.167582Z","iopub.execute_input":"2023-09-10T09:46:21.167948Z","iopub.status.idle":"2023-09-10T09:46:21.177018Z","shell.execute_reply.started":"2023-09-10T09:46:21.167918Z","shell.execute_reply":"2023-09-10T09:46:21.175938Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = create_model()\n\nEarlyStoppingCallbacks = tf.keras.callbacks.EarlyStopping(\n    monitor='val_loss', patience=2, baseline=None, restore_best_weights=True\n)","metadata":{"execution":{"iopub.status.busy":"2023-09-10T09:46:21.178435Z","iopub.execute_input":"2023-09-10T09:46:21.178759Z","iopub.status.idle":"2023-09-10T09:46:23.213329Z","shell.execute_reply.started":"2023-09-10T09:46:21.178731Z","shell.execute_reply":"2023-09-10T09:46:23.212205Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ModelDogBreed_History = model.fit(train_data, \n                                         steps_per_epoch = len(train_data),\n                                         epochs = 10, \n                                         validation_data= valid_data, \n                                         validation_steps = len(valid_data),\n                                         callbacks = [EarlyStoppingCallbacks])","metadata":{"execution":{"iopub.status.busy":"2023-09-10T09:46:23.214738Z","iopub.execute_input":"2023-09-10T09:46:23.215162Z","iopub.status.idle":"2023-09-10T09:54:25.125227Z","shell.execute_reply.started":"2023-09-10T09:46:23.215113Z","shell.execute_reply":"2023-09-10T09:54:25.123773Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.evaluate(valid_data)","metadata":{"execution":{"iopub.status.busy":"2023-09-10T09:54:25.127394Z","iopub.execute_input":"2023-09-10T09:54:25.127737Z","iopub.status.idle":"2023-09-10T09:54:34.682494Z","shell.execute_reply.started":"2023-09-10T09:54:25.127699Z","shell.execute_reply":"2023-09-10T09:54:34.681264Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = ModelDogBreed_History\n\nloss = history.history['loss']\nval_loss = history.history['val_loss']\naccuracy = history.history['accuracy']\nval_accuracy = history.history['val_accuracy']\n\nepochs = range(len(history.history['loss']))\n\nplt.plot(epochs, loss, label='training_loss')\nplt.plot(epochs, val_loss, label='val_loss')\nplt.title('Loss')\nplt.xlabel('Epochs')\nplt.legend()\n\nplt.figure()\nplt.plot(epochs, accuracy, label='training_accuracy')\nplt.plot(epochs, val_accuracy, label='val_accuracy')\nplt.title('Accuracy')\nplt.xlabel('Epochs')\nplt.legend()","metadata":{"execution":{"iopub.status.busy":"2023-09-10T09:54:34.684262Z","iopub.execute_input":"2023-09-10T09:54:34.684692Z","iopub.status.idle":"2023-09-10T09:54:35.398676Z","shell.execute_reply.started":"2023-09-10T09:54:34.684654Z","shell.execute_reply":"2023-09-10T09:54:35.397504Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = model.predict(valid_data)\npredictions","metadata":{"execution":{"iopub.status.busy":"2023-09-10T09:54:35.400265Z","iopub.execute_input":"2023-09-10T09:54:35.400680Z","iopub.status.idle":"2023-09-10T09:54:46.641093Z","shell.execute_reply.started":"2023-09-10T09:54:35.400641Z","shell.execute_reply":"2023-09-10T09:54:46.639904Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_pred_label(prediction_probabilities):\n    \"\"\"\n    Turns an array of prediction probabilities into a label.\n    \"\"\"\n    return classNames[np.argmax(prediction_probabilities)]\n\npred_label = get_pred_label(predictions[0])\nprint(pred_label)","metadata":{"execution":{"iopub.status.busy":"2023-09-10T09:54:46.646972Z","iopub.execute_input":"2023-09-10T09:54:46.647588Z","iopub.status.idle":"2023-09-10T09:54:46.654833Z","shell.execute_reply.started":"2023-09-10T09:54:46.647546Z","shell.execute_reply":"2023-09-10T09:54:46.653695Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def unbatchify(data):\n  \"\"\"\n  Takes a batched dataset of (image, label) Tensors and returns separate arrays\n  of images and labels.\n  \"\"\"\n  images = []\n  labels = []\n  \n  for image, label in data.unbatch().as_numpy_iterator():\n    images.append(image)\n    labels.append(classNames[np.argmax(label)])\n  return images, labels\n\nval_images, val_labels = unbatchify(valid_data)\nval_images[0], val_labels[0]","metadata":{"execution":{"iopub.status.busy":"2023-09-10T09:54:46.656512Z","iopub.execute_input":"2023-09-10T09:54:46.657800Z","iopub.status.idle":"2023-09-10T09:54:47.403019Z","shell.execute_reply.started":"2023-09-10T09:54:46.657767Z","shell.execute_reply":"2023-09-10T09:54:47.402150Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_pred(prediction_probabilities, labels, images, n=1):\n  \"\"\"\n  View the prediction, ground truth label and image for sample n.\n  \"\"\"\n  pred_prob, true_label, image = prediction_probabilities[n], labels[n], images[n]\n  pred_label = get_pred_label(pred_prob)\n  \n  plt.imshow(image)\n  plt.xticks([])\n  plt.yticks([])\n    \n  if pred_label == true_label:\n    color = \"green\"\n  else:\n    color = \"red\"\n\n  plt.title(\"{} {:2.0f}% ({})\".format(pred_label,\n                                      np.max(pred_prob)*100,\n                                      true_label),\n                                      color=color)","metadata":{"execution":{"iopub.status.busy":"2023-09-10T09:54:47.406684Z","iopub.execute_input":"2023-09-10T09:54:47.407454Z","iopub.status.idle":"2023-09-10T09:54:47.415171Z","shell.execute_reply.started":"2023-09-10T09:54:47.407423Z","shell.execute_reply":"2023-09-10T09:54:47.413875Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_pred(prediction_probabilities=predictions,\n          labels=val_labels,\n          images=val_images)","metadata":{"execution":{"iopub.status.busy":"2023-09-10T09:54:47.416509Z","iopub.execute_input":"2023-09-10T09:54:47.416813Z","iopub.status.idle":"2023-09-10T09:54:47.704087Z","shell.execute_reply.started":"2023-09-10T09:54:47.416787Z","shell.execute_reply":"2023-09-10T09:54:47.702782Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_pred_conf(prediction_probabilities, labels, n=1):\n  \"\"\"\n  Plots the top 10 highest prediction confidences along with\n  the truth label for sample n.\n  \"\"\"\n  pred_prob, true_label = prediction_probabilities[n], labels[n]\n  pred_label = get_pred_label(pred_prob)\n\n  top_10_pred_indexes = pred_prob.argsort()[-10:][::-1]\n  top_10_pred_values = pred_prob[top_10_pred_indexes]\n  top_10_pred_labels = classNames[top_10_pred_indexes]\n\n  top_plot = plt.bar(np.arange(len(top_10_pred_labels)), \n                     top_10_pred_values, \n                     color=\"grey\")\n  plt.xticks(np.arange(len(top_10_pred_labels)),\n             labels=top_10_pred_labels,\n             rotation=\"vertical\")\n\n  if np.isin(true_label, top_10_pred_labels):\n    top_plot[np.argmax(top_10_pred_labels == true_label)].set_color(\"green\")\n  else:\n    pass","metadata":{"execution":{"iopub.status.busy":"2023-09-10T09:54:47.706281Z","iopub.execute_input":"2023-09-10T09:54:47.707118Z","iopub.status.idle":"2023-09-10T09:54:47.715342Z","shell.execute_reply.started":"2023-09-10T09:54:47.707081Z","shell.execute_reply":"2023-09-10T09:54:47.714465Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_pred_conf(prediction_probabilities=predictions,\n               labels=val_labels,\n               n=9)","metadata":{"execution":{"iopub.status.busy":"2023-09-10T09:54:47.716679Z","iopub.execute_input":"2023-09-10T09:54:47.717838Z","iopub.status.idle":"2023-09-10T09:54:48.038517Z","shell.execute_reply.started":"2023-09-10T09:54:47.717804Z","shell.execute_reply":"2023-09-10T09:54:48.037143Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"i_multiplier = 0\nnum_rows = 3\nnum_cols = 2\nnum_images = num_rows*num_cols\nplt.figure(figsize=(5*2*num_cols, 5*num_rows))\nfor i in range(num_images):\n  plt.subplot(num_rows, 2*num_cols, 2*i+1)\n  plot_pred(prediction_probabilities=predictions,\n            labels=val_labels,\n            images=val_images,\n            n=i+i_multiplier)\n  plt.subplot(num_rows, 2*num_cols, 2*i+2)\n  plot_pred_conf(prediction_probabilities=predictions,\n                labels=val_labels,\n                n=i+i_multiplier)\nplt.tight_layout(h_pad=1.0)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-09-10T09:54:48.040177Z","iopub.execute_input":"2023-09-10T09:54:48.040626Z","iopub.status.idle":"2023-09-10T09:54:50.842303Z","shell.execute_reply.started":"2023-09-10T09:54:48.040522Z","shell.execute_reply":"2023-09-10T09:54:50.841032Z"},"trusted":true},"execution_count":null,"outputs":[]}],"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"}}