{"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":"## Dog Breed Identification\n\nIn this competition, given an image of a dog we are asked to predict a probability for each of the different 120 breeds of the dogs.\n\nNotebook summary:\n* The Notebook is created on Dog Breed Dataset.\n* As there are 120 Different Breeds of Dog present in the Dataset it becomes difficult for a single pretrained model to give Good results.\n* Here we used different pretrained models to extract features from the images and combined them and then trained a DNN model on these features.\n\nSo let's get started.","metadata":{}},{"cell_type":"code","source":"import tensorflow as tf\nprint(tf.__version__)","metadata":{"execution":{"iopub.status.busy":"2021-10-05T09:42:40.692450Z","iopub.execute_input":"2021-10-05T09:42:40.693024Z","iopub.status.idle":"2021-10-05T09:42:44.985492Z","shell.execute_reply.started":"2021-10-05T09:42:40.692933Z","shell.execute_reply":"2021-10-05T09:42:44.984250Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# !pip install livelossplot","metadata":{"scrolled":true,"execution":{"iopub.status.busy":"2021-10-05T09:42:44.987030Z","iopub.execute_input":"2021-10-05T09:42:44.987389Z","iopub.status.idle":"2021-10-05T09:42:44.991736Z","shell.execute_reply.started":"2021-10-05T09:42:44.987353Z","shell.execute_reply":"2021-10-05T09:42:44.990861Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport matplotlib.pyplot as plt\nfrom tensorflow import keras\n\nimport cv2\nimport PIL\nimport os\nimport pathlib\nimport shutil\nfrom IPython.display import Image, display\n\n# Plotly for the interactive viewer (see last section)\n\nimport plotly.graph_objs as go\nimport plotly.graph_objects as go\nfrom sklearn.metrics import cohen_kappa_score\nfrom sklearn.model_selection import train_test_split\nfrom tensorflow.keras.models import Sequential, Model,load_model\nfrom tensorflow.keras.applications import vgg16 \nfrom tensorflow.keras.applications import resnet50\nfrom tensorflow.keras.applications import xception\nfrom tensorflow.keras.applications import inception_v3\nfrom tensorflow.keras.applications import inception_resnet_v2\nfrom tensorflow.keras.applications import resnet_v2\nfrom tensorflow.keras.applications import nasnet\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator,load_img, img_to_array\nfrom tensorflow.keras import Input\nfrom tensorflow.keras.layers import Conv2D, MaxPooling2D, Dense, Dropout, Flatten,BatchNormalization,Activation\nfrom tensorflow.keras.layers import GlobalAveragePooling2D, Concatenate\nfrom tensorflow.keras.optimizers import Adam, SGD, RMSprop\nfrom tensorflow.keras.callbacks import ModelCheckpoint, Callback, EarlyStopping, LearningRateScheduler\nfrom tensorflow.keras.utils import to_categorical\nfrom tensorflow.keras.preprocessing import image_dataset_from_directory\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras.layers.experimental import preprocessing\n\nimport gc\nimport skimage.io\n# from livelossplot import PlotLossesKeras","metadata":{"execution":{"iopub.status.busy":"2021-10-05T09:42:44.993153Z","iopub.execute_input":"2021-10-05T09:42:44.993573Z","iopub.status.idle":"2021-10-05T09:42:45.860992Z","shell.execute_reply.started":"2021-10-05T09:42:44.993539Z","shell.execute_reply":"2021-10-05T09:42:45.860287Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Load Data","metadata":{}},{"cell_type":"code","source":"train_dir = '../input/dog-breed-dataset-with-subdirectories-by-class/data/train'\ntest_dir = '../input/dog-breed-dataset-with-subdirectories-by-class/data/test'\n\ntrain_labels = pd.read_csv('../input/dog-breed-identification/labels.csv', index_col = 'id')\nsubmission=pd.read_csv('../input/dog-breed-identification/sample_submission.csv')","metadata":{"execution":{"iopub.status.busy":"2021-10-05T09:42:45.863058Z","iopub.execute_input":"2021-10-05T09:42:45.863383Z","iopub.status.idle":"2021-10-05T09:42:46.385645Z","shell.execute_reply.started":"2021-10-05T09:42:45.863346Z","shell.execute_reply":"2021-10-05T09:42:46.384925Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_size = len(os.listdir(train_dir))\ntest_size = len(os.listdir(test_dir))\n\nprint(train_size,test_size)\nprint(train_labels.shape)\nprint(submission.shape)","metadata":{"execution":{"iopub.status.busy":"2021-10-05T09:42:46.386895Z","iopub.execute_input":"2021-10-05T09:42:46.387153Z","iopub.status.idle":"2021-10-05T09:42:46.414407Z","shell.execute_reply.started":"2021-10-05T09:42:46.387122Z","shell.execute_reply":"2021-10-05T09:42:46.413616Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"target, dog_breeds = pd.factorize(train_labels['breed'], sort = True)\ntrain_labels['target'] = target\n\nprint(dog_breeds)","metadata":{"execution":{"iopub.status.busy":"2021-10-05T09:42:46.415711Z","iopub.execute_input":"2021-10-05T09:42:46.415975Z","iopub.status.idle":"2021-10-05T09:42:46.428681Z","shell.execute_reply.started":"2021-10-05T09:42:46.415943Z","shell.execute_reply":"2021-10-05T09:42:46.428061Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"display(train_labels.head())\ndisplay(submission.head())","metadata":{"execution":{"iopub.status.busy":"2021-10-05T09:42:46.429552Z","iopub.execute_input":"2021-10-05T09:42:46.429741Z","iopub.status.idle":"2021-10-05T09:42:46.469502Z","shell.execute_reply.started":"2021-10-05T09:42:46.429719Z","shell.execute_reply":"2021-10-05T09:42:46.468870Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_labels['breed'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2021-10-05T09:42:46.471825Z","iopub.execute_input":"2021-10-05T09:42:46.472010Z","iopub.status.idle":"2021-10-05T09:42:46.485428Z","shell.execute_reply.started":"2021-10-05T09:42:46.471989Z","shell.execute_reply":"2021-10-05T09:42:46.484665Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(15, 10))\ntrain_labels['breed'].value_counts().plot(kind='bar')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-10-05T09:42:46.486790Z","iopub.execute_input":"2021-10-05T09:42:46.487109Z","iopub.status.idle":"2021-10-05T09:42:48.369295Z","shell.execute_reply.started":"2021-10-05T09:42:46.487077Z","shell.execute_reply":"2021-10-05T09:42:48.368494Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Global Variables","metadata":{}},{"cell_type":"code","source":"N_EPOCHS = 50\nBATCH_SIZE = 128\nIMG_HEIGHT = 331\nIMG_WIDTH = 331","metadata":{"execution":{"iopub.status.busy":"2021-10-05T09:42:48.372028Z","iopub.execute_input":"2021-10-05T09:42:48.372470Z","iopub.status.idle":"2021-10-05T09:42:48.376338Z","shell.execute_reply.started":"2021-10-05T09:42:48.372433Z","shell.execute_reply":"2021-10-05T09:42:48.375487Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Prepare the Datasets","metadata":{}},{"cell_type":"code","source":"train_ds = image_dataset_from_directory(\n  directory = train_dir,\n  labels = 'inferred',\n  label_mode='int',\n  batch_size=BATCH_SIZE,\n  image_size=(IMG_HEIGHT, IMG_WIDTH),\n  shuffle = True,\n  seed=1234,\n  validation_split=0.1,\n  subset=\"training\",\n)","metadata":{"execution":{"iopub.status.busy":"2021-10-05T09:42:48.377620Z","iopub.execute_input":"2021-10-05T09:42:48.378062Z","iopub.status.idle":"2021-10-05T09:42:51.167817Z","shell.execute_reply.started":"2021-10-05T09:42:48.377974Z","shell.execute_reply":"2021-10-05T09:42:51.167075Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class_names = train_ds.class_names\nprint(len(class_names))\nprint(class_names)","metadata":{"scrolled":true,"execution":{"iopub.status.busy":"2021-10-05T09:42:51.169203Z","iopub.execute_input":"2021-10-05T09:42:51.169473Z","iopub.status.idle":"2021-10-05T09:42:51.174609Z","shell.execute_reply.started":"2021-10-05T09:42:51.169441Z","shell.execute_reply":"2021-10-05T09:42:51.173930Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"val_ds = image_dataset_from_directory(\n  directory = train_dir,\n  labels = 'inferred',\n  label_mode='int',\n  batch_size=BATCH_SIZE,\n  image_size=(IMG_HEIGHT, IMG_WIDTH),\n  shuffle = True,\n  seed=1234,\n  validation_split=0.1,\n  subset=\"validation\",\n)","metadata":{"execution":{"iopub.status.busy":"2021-10-05T09:42:51.176096Z","iopub.execute_input":"2021-10-05T09:42:51.176577Z","iopub.status.idle":"2021-10-05T09:42:51.823334Z","shell.execute_reply.started":"2021-10-05T09:42:51.176524Z","shell.execute_reply":"2021-10-05T09:42:51.822585Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_ds = image_dataset_from_directory(\n  directory = test_dir,\n  label_mode= None,\n  batch_size=BATCH_SIZE,\n  image_size=(IMG_HEIGHT, IMG_WIDTH),\n  shuffle = False,\n  seed=1234\n)","metadata":{"execution":{"iopub.status.busy":"2021-10-05T09:42:51.824387Z","iopub.execute_input":"2021-10-05T09:42:51.824887Z","iopub.status.idle":"2021-10-05T09:42:59.110015Z","shell.execute_reply.started":"2021-10-05T09:42:51.824850Z","shell.execute_reply":"2021-10-05T09:42:59.109301Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"del class_names","metadata":{"execution":{"iopub.status.busy":"2021-10-05T09:42:59.111334Z","iopub.execute_input":"2021-10-05T09:42:59.111585Z","iopub.status.idle":"2021-10-05T09:42:59.115778Z","shell.execute_reply.started":"2021-10-05T09:42:59.111553Z","shell.execute_reply":"2021-10-05T09:42:59.114942Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Visualize the Data","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(20, 20))\n\nfor images, labels in train_ds.take(1):\n#   print(labels)\n  for i in range(16):\n    ax = plt.subplot(4, 4, i + 1)\n    plt.imshow(images[i].numpy().astype(\"uint8\"))\n    plt.title(dog_breeds[labels[i]])\n    plt.axis(\"off\")","metadata":{"execution":{"iopub.status.busy":"2021-10-05T09:42:59.117282Z","iopub.execute_input":"2021-10-05T09:42:59.117564Z","iopub.status.idle":"2021-10-05T09:43:08.164623Z","shell.execute_reply.started":"2021-10-05T09:42:59.117523Z","shell.execute_reply":"2021-10-05T09:43:08.163829Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(20, 20))\nfor images, labels in val_ds.take(1):\n#   print(labels)\n  for i in range(16):\n    ax = plt.subplot(4, 4, i + 1)\n    plt.imshow(images[i].numpy().astype(\"uint8\"))\n    plt.title(dog_breeds[labels[i]])\n    plt.axis(\"off\")","metadata":{"execution":{"iopub.status.busy":"2021-10-05T09:43:08.165674Z","iopub.execute_input":"2021-10-05T09:43:08.166373Z","iopub.status.idle":"2021-10-05T09:43:16.044437Z","shell.execute_reply.started":"2021-10-05T09:43:08.166338Z","shell.execute_reply":"2021-10-05T09:43:16.040305Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(20, 20))\nfor images in test_ds.take(1):\n#   print(labels)\n  for i in range(16):\n    ax = plt.subplot(4, 4, i + 1)\n    plt.imshow(images[i].numpy().astype(\"uint8\"))\n#     plt.title(dog_breeds[labels[i]])\n    plt.axis(\"off\")","metadata":{"execution":{"iopub.status.busy":"2021-10-05T09:43:16.045605Z","iopub.execute_input":"2021-10-05T09:43:16.045825Z","iopub.status.idle":"2021-10-05T09:43:18.734667Z","shell.execute_reply.started":"2021-10-05T09:43:16.045798Z","shell.execute_reply":"2021-10-05T09:43:18.732506Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Configure the dataset for performance\n\nLet's make sure to use buffered prefetching, so we can yield data from disk without having I/O become blocking. These are two important methods we should use when loading data.\n\n.cache() keeps the images in memory after they're loaded off disk during the first epoch. \n\n.prefetch() overlaps data preprocessing and model execution while training.","metadata":{}},{"cell_type":"code","source":"AUTOTUNE = tf.data.AUTOTUNE\n\n# Without Caching\ntrain_ds = train_ds.prefetch(buffer_size=AUTOTUNE)\nval_ds = val_ds.prefetch(buffer_size=AUTOTUNE)\ntest_ds = test_ds.prefetch(buffer_size=AUTOTUNE)\n\n# With Caching\n# train_ds = train_ds.cache().prefetch(buffer_size=AUTOTUNE)\n# val_ds = val_ds.cache().prefetch(buffer_size=AUTOTUNE)\n# test_ds = test_ds.cache().prefetch(buffer_size=AUTOTUNE)","metadata":{"execution":{"iopub.status.busy":"2021-10-05T09:43:18.736077Z","iopub.execute_input":"2021-10-05T09:43:18.736437Z","iopub.status.idle":"2021-10-05T09:43:18.743172Z","shell.execute_reply.started":"2021-10-05T09:43:18.736405Z","shell.execute_reply":"2021-10-05T09:43:18.742213Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Data Augmentation layer","metadata":{}},{"cell_type":"code","source":"data_augmentation = Sequential(\n  [\n    preprocessing.RandomFlip(\"horizontal\"),\n    preprocessing.RandomRotation(0.1),\n    preprocessing.RandomZoom(0.1),\n  ]\n)","metadata":{"execution":{"iopub.status.busy":"2021-10-05T09:43:18.744922Z","iopub.execute_input":"2021-10-05T09:43:18.745175Z","iopub.status.idle":"2021-10-05T09:43:18.778435Z","shell.execute_reply.started":"2021-10-05T09:43:18.745144Z","shell.execute_reply":"2021-10-05T09:43:18.777564Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Let's visualize what the first image of the first batch looks like after various random transformations:","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(10, 10))\nfor images, labels in train_ds.take(1):\n    first_image = images[0]\n    for i in range(9):\n        ax = plt.subplot(3, 3, i + 1)\n        augmented_image = data_augmentation(\n            tf.expand_dims(first_image, 0), training=True\n        )\n        plt.imshow(augmented_image[0].numpy().astype(\"int32\"))\n        plt.title(dog_breeds[labels[0]])\n        plt.axis(\"off\")","metadata":{"execution":{"iopub.status.busy":"2021-10-05T09:43:38.778634Z","iopub.execute_input":"2021-10-05T09:43:38.778895Z","iopub.status.idle":"2021-10-05T09:43:43.632745Z","shell.execute_reply.started":"2021-10-05T09:43:38.778868Z","shell.execute_reply":"2021-10-05T09:43:43.631944Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Build a Model","metadata":{}},{"cell_type":"code","source":"base_model_1 = xception.Xception(weights='imagenet', include_top=False, input_shape=(IMG_HEIGHT, IMG_WIDTH,3))\n\nbase_model_2 = inception_v3.InceptionV3(weights='imagenet', include_top=False, input_shape=(IMG_HEIGHT, IMG_WIDTH,3))\n\nbase_model_3 = inception_resnet_v2.InceptionResNetV2(weights='imagenet', include_top=False, input_shape=(IMG_HEIGHT, IMG_WIDTH,3))\n\n# base_model_4 = resnet_v2.ResNet152V2(weights='imagenet', include_top=False, input_shape=(IMG_HEIGHT, IMG_WIDTH,3))\n\nbase_model_5 = nasnet.NASNetLarge(weights='imagenet', include_top=False, input_shape=(IMG_HEIGHT, IMG_WIDTH,3))\n\n# display(base_model_1.summary())\n# display(base_model_2.summary())\n# display(base_model_3.summary())\n# display(base_model_4.summary())\n# display(base_model_5.summary())\n\n# train only the top layers (which were randomly initialized)\n# i.e. freeze all convolutional Xception layers\nbase_model_1.trainable = False\nbase_model_2.trainable = False\nbase_model_3.trainable = False\n# base_model_4.trainable = False\nbase_model_5.trainable = False\n\ninputs = Input(shape=(IMG_HEIGHT, IMG_WIDTH, 3))\naug_inputs = data_augmentation(inputs)\n\n## <-----  Xception   -----> ##\nx1 = xception.preprocess_input(aug_inputs)\n# The base model contains batchnorm layers. We want to keep them in inference mode\n# when we unfreeze the base model for fine-tuning, so we make sure that the\n# base_model is running in inference mode here by passing `training=False`.\nx1 = base_model_1(x1, training=False)\nx1 = GlobalAveragePooling2D()(x1)\n\n## <-----  InceptionV3   -----> ##\nx2 = inception_v3.preprocess_input(aug_inputs)\nx2 = base_model_2(x2, training=False)\nx2 = GlobalAveragePooling2D()(x2)\n\n## <-----  InceptionResNetV2   -----> ##\nx3 = inception_resnet_v2.preprocess_input(aug_inputs)\nx3 = base_model_3(x3, training=False)\nx3 = GlobalAveragePooling2D()(x3)\n\n## <-----  ResNet152V2   -----> ##\n# x4 = resnet_v2.preprocess_input(aug_inputs)\n# x4 = base_model_4(x4, training=False)\n# x4 = GlobalAveragePooling2D()(x4)\n\n## <-----  NASNetLarge   -----> ##\nx5 = nasnet.preprocess_input(aug_inputs)\nx5 = base_model_5(x5, training=False)\nx5 = GlobalAveragePooling2D()(x5)\n\n## <-----  Concatenation  -----> ##\nx = Concatenate()([x1, x2, x3, x5])\nx = Dropout(.7)(x)\noutputs = Dense(120, activation='softmax')(x)\nmodel = Model(inputs, outputs)\n\ndisplay(model.summary())","metadata":{"scrolled":true,"execution":{"iopub.status.busy":"2021-10-05T09:43:44.352278Z","iopub.execute_input":"2021-10-05T09:43:44.353193Z","iopub.status.idle":"2021-10-05T09:44:10.508970Z","shell.execute_reply.started":"2021-10-05T09:43:44.353155Z","shell.execute_reply":"2021-10-05T09:44:10.508160Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Training the Model","metadata":{}},{"cell_type":"code","source":"optimizer = Adam(learning_rate=0.001)\nmodel.compile(loss=\"sparse_categorical_crossentropy\", metrics=['accuracy'], optimizer=optimizer)","metadata":{"execution":{"iopub.status.busy":"2021-10-05T09:44:10.513339Z","iopub.execute_input":"2021-10-05T09:44:10.513601Z","iopub.status.idle":"2021-10-05T09:44:10.572828Z","shell.execute_reply.started":"2021-10-05T09:44:10.513566Z","shell.execute_reply":"2021-10-05T09:44:10.572106Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"EarlyStop_callback = EarlyStopping(min_delta=0.001, patience=10, restore_best_weights=True)\n\n# # DECREASE LEARNING RATE EACH EPOCH\n# annealer = LearningRateScheduler(lambda epoch: 1e-5 * 0.95 ** epoch, verbose=1)\n\n# cb=[PlotLossesKeras(), annealer]","metadata":{"execution":{"iopub.status.busy":"2021-10-05T09:44:10.573980Z","iopub.execute_input":"2021-10-05T09:44:10.574268Z","iopub.status.idle":"2021-10-05T09:44:10.808105Z","shell.execute_reply.started":"2021-10-05T09:44:10.574236Z","shell.execute_reply":"2021-10-05T09:44:10.807203Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(\n    train_ds,\n    epochs=N_EPOCHS,\n    validation_data=val_ds,\n    callbacks=[EarlyStop_callback]\n)","metadata":{"execution":{"iopub.status.busy":"2021-10-05T09:44:14.632988Z","iopub.execute_input":"2021-10-05T09:44:14.633554Z","iopub.status.idle":"2021-10-05T10:45:34.371378Z","shell.execute_reply.started":"2021-10-05T09:44:14.633517Z","shell.execute_reply":"2021-10-05T10:45:34.370586Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"accuracy = history.history['accuracy']\nval_accuracy = history.history['val_accuracy']\n\nloss = history.history['loss']\nval_loss = history.history['val_loss']\n\nplt.figure(figsize=(10, 10))\n\nplt.subplot(2, 1, 1)\nplt.plot(accuracy, label='Training Accuracy')\nplt.plot(val_accuracy, label='Validation Accuracy')\nplt.legend(loc='lower right')\nplt.ylabel('Accuracy')\nplt.ylim([min(plt.ylim()),1])\nplt.xticks(list(range(20)))\nplt.title('Training and Validation Accuracy')\n\nplt.subplot(2, 1, 2)\nplt.plot(loss, label='Training Loss')\nplt.plot(val_loss, label='Validation Loss')\nplt.legend(loc='upper right')\nplt.ylabel('Cross Entropy')\nplt.ylim([0,1.0])\nplt.title('Training and Validation Loss')\nplt.xticks(list(range(20)))\nplt.xlabel('Epoch')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-10-05T10:45:34.376256Z","iopub.execute_input":"2021-10-05T10:45:34.378625Z","iopub.status.idle":"2021-10-05T10:45:34.797429Z","shell.execute_reply.started":"2021-10-05T10:45:34.378583Z","shell.execute_reply":"2021-10-05T10:45:34.796768Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Analyse Wrong Predictions on Validation Set","metadata":{}},{"cell_type":"code","source":"wrong_pred_images = np.array([])\nactual_labels = np.array([])\npredicted_labels = np.array([])\n\n\nbatch = 1\nfor images, labels in val_ds:\n    batch_predictions_probs = model.predict_on_batch(images)\n    batch_predictions = np.argmax(batch_predictions_probs, axis=1)\n    mask = (batch_predictions != labels.numpy())\n    print(\"No of wrong predictions on batch {}: {}\".format(batch, mask.sum()))\n    \n    wrong_pred_indices = np.arange(len(batch_predictions))[mask]\n    print(wrong_pred_indices)\n    \n    if len(wrong_pred_images) == 0:\n        wrong_pred_images = images.numpy()[wrong_pred_indices]\n        actual_labels = labels.numpy()[wrong_pred_indices]\n        predicted_labels = batch_predictions[wrong_pred_indices]\n    else:\n        wrong_pred_images = np.append(wrong_pred_images, images.numpy()[wrong_pred_indices], axis = 0)\n        actual_labels = np.append(actual_labels, labels.numpy()[wrong_pred_indices], axis = 0)\n        predicted_labels = np.append(predicted_labels, batch_predictions[wrong_pred_indices], axis = 0)\n        \n    batch = batch + 1\n    \nprint(wrong_pred_images.shape)\nprint(actual_labels.shape)\nprint(predicted_labels.shape)","metadata":{"execution":{"iopub.status.busy":"2021-10-05T10:53:15.879570Z","iopub.execute_input":"2021-10-05T10:53:15.879885Z","iopub.status.idle":"2021-10-05T10:53:50.605065Z","shell.execute_reply.started":"2021-10-05T10:53:15.879852Z","shell.execute_reply":"2021-10-05T10:53:50.604295Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(20, 20))\n\nfor i in range(25):\n    ax = plt.subplot(5, 5, i + 1)\n    plt.imshow(wrong_pred_images[i].astype(\"uint8\"))\n    plt.title(\"Actual : {}\\n Predicted : {}\".format(dog_breeds[actual_labels[i]], dog_breeds[predicted_labels[i]]))\n    plt.axis(\"off\")","metadata":{"execution":{"iopub.status.busy":"2021-10-05T10:46:07.890358Z","iopub.execute_input":"2021-10-05T10:46:07.890787Z","iopub.status.idle":"2021-10-05T10:46:10.383534Z","shell.execute_reply.started":"2021-10-05T10:46:07.890752Z","shell.execute_reply":"2021-10-05T10:46:10.378167Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(20, 20))\n\nfor i in range(25):\n    ax = plt.subplot(5, 5, i + 1)\n    plt.imshow(wrong_pred_images[i+25].astype(\"uint8\"))\n    plt.title(\"Actual : {}\\n Predicted : {}\".format(dog_breeds[actual_labels[i+25]], dog_breeds[predicted_labels[i+25]]))\n    plt.axis(\"off\")","metadata":{"execution":{"iopub.status.busy":"2021-10-05T10:57:50.935296Z","iopub.execute_input":"2021-10-05T10:57:50.935861Z","iopub.status.idle":"2021-10-05T10:57:53.443036Z","shell.execute_reply.started":"2021-10-05T10:57:50.935824Z","shell.execute_reply":"2021-10-05T10:57:53.442420Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Fine Tuning","metadata":{}},{"cell_type":"code","source":"# # Unfreeze the base_model. Note that it keeps running in inference mode\n# # since we passed `training=False` when calling it. This means that\n# # the batchnorm layers will not update their batch statistics.\n# # This prevents the batchnorm layers from undoing all the training\n# # we've done so far.\n# base_model.trainable = True\n# model.summary()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# print(\"Number of layers in the base model: \", len(base_model.layers))","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# fine_tune_from = 100\n\n# # Freeze all the layers before the `fine_tune_at` layer\n# for layer in base_model.layers[:fine_tune_from]:\n#     layer.trainable =  False","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# model.compile(\n#     optimizer=keras.optimizers.Adam(1e-5),  # Low learning rate\n#     loss=keras.losses.BinaryCrossentropy(from_logits=True),\n#     metrics=[keras.metrics.BinaryAccuracy()],\n# )","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# FINE_TUNE_EPOCHS = 10\n# fine_tune_history = model.fit(train_ds,\n#                          epochs = FINE_TUNE_EPOCHS + history.epoch[-1],\n#                          initial_epoch = history.epoch[-1] + 1,\n#                          validation_data = val_ds)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# accuracy += history_fine.history['accuracy']\n# val_accuracy += history_fine.history['val_accuracy']\n\n# loss += history_fine.history['loss']\n# val_loss += history_fine.history['val_loss']","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# plt.figure(figsize=(8, 8))\n# plt.subplot(2, 1, 1)\n# plt.plot(acc, label='Training Accuracy')\n# plt.plot(val_acc, label='Validation Accuracy')\n# plt.ylim([0.8, 1])\n# plt.plot([EPOCHS-1,EPOCHS-1],\n#           plt.ylim(), label='Start Fine Tuning')\n# plt.legend(loc='lower right')\n# plt.title('Training and Validation Accuracy')\n\n# plt.subplot(2, 1, 2)\n# plt.plot(loss, label='Training Loss')\n# plt.plot(val_loss, label='Validation Loss')\n# plt.ylim([0, 1.0])\n# plt.plot([EPOCHS-1,EPOCHS-1],\n#          plt.ylim(), label='Start Fine Tuning')\n# plt.legend(loc='upper right')\n# plt.title('Training and Validation Loss')\n# plt.xlabel('epoch')\n# plt.show()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Predict on Test Dataset","metadata":{}},{"cell_type":"code","source":"# del train_ds, val_ds, train_labels, class_names","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# file_paths = test_ds.file_paths\n# print(file_paths)","metadata":{"scrolled":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = model.predict(\n    test_ds,\n    batch_size = BATCH_SIZE,\n    verbose=1         \n)","metadata":{"execution":{"iopub.status.busy":"2021-10-05T10:46:10.384839Z","iopub.execute_input":"2021-10-05T10:46:10.385175Z","iopub.status.idle":"2021-10-05T10:51:21.944670Z","shell.execute_reply.started":"2021-10-05T10:46:10.385139Z","shell.execute_reply":"2021-10-05T10:51:21.943945Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(predictions.shape)\nprint(predictions)","metadata":{"execution":{"iopub.status.busy":"2021-10-05T10:51:21.946310Z","iopub.execute_input":"2021-10-05T10:51:21.946588Z","iopub.status.idle":"2021-10-05T10:51:21.954247Z","shell.execute_reply.started":"2021-10-05T10:51:21.946552Z","shell.execute_reply":"2021-10-05T10:51:21.953508Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Submission","metadata":{}},{"cell_type":"code","source":"submission.loc[:, dog_breeds] = predictions\nsubmission.head()","metadata":{"execution":{"iopub.status.busy":"2021-10-05T10:51:21.955451Z","iopub.execute_input":"2021-10-05T10:51:21.956059Z","iopub.status.idle":"2021-10-05T10:51:22.415739Z","shell.execute_reply.started":"2021-10-05T10:51:21.956005Z","shell.execute_reply":"2021-10-05T10:51:22.414906Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv('submission.csv', index=False)\n# submission.head()","metadata":{"execution":{"iopub.status.busy":"2021-10-05T10:51:22.416932Z","iopub.execute_input":"2021-10-05T10:51:22.417276Z","iopub.status.idle":"2021-10-05T10:51:24.861276Z","shell.execute_reply.started":"2021-10-05T10:51:22.417238Z","shell.execute_reply":"2021-10-05T10:51:24.860512Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}