{"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\nSo let's get started.","metadata":{}},{"cell_type":"code","source":"import tensorflow as tf\nprint(tf.__version__)","metadata":{"execution":{"iopub.status.busy":"2021-10-03T11:04:46.793508Z","iopub.execute_input":"2021-10-03T11:04:46.794219Z","iopub.status.idle":"2021-10-03T11:04:50.965922Z","shell.execute_reply.started":"2021-10-03T11:04:46.794131Z","shell.execute_reply":"2021-10-03T11:04:50.964341Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install livelossplot","metadata":{"scrolled":true,"execution":{"iopub.status.busy":"2021-10-03T11:04:52.250772Z","iopub.execute_input":"2021-10-03T11:04:52.251044Z","iopub.status.idle":"2021-10-03T11:05:00.345429Z","shell.execute_reply.started":"2021-10-03T11:04:52.251016Z","shell.execute_reply":"2021-10-03T11:05:00.344585Z"},"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.vgg16 import VGG16,preprocess_input\nfrom tensorflow.keras.applications.resnet50 import ResNet50\nfrom tensorflow.keras.applications.xception import Xception, preprocess_input\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator,load_img, img_to_array\nfrom tensorflow.keras import Input\nfrom tensorflow.keras.callbacks import EarlyStopping, LearningRateScheduler \nfrom tensorflow.keras.layers import Conv2D, MaxPooling2D, Dense, Dropout, Flatten,BatchNormalization,Activation\nfrom tensorflow.keras.layers import GlobalAveragePooling2D\nfrom tensorflow.keras.optimizers import Adam, SGD, RMSprop\nfrom tensorflow.keras.callbacks import ModelCheckpoint, Callback, EarlyStopping\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\nfrom livelossplot import PlotLossesKeras","metadata":{"execution":{"iopub.status.busy":"2021-10-03T11:05:00.349047Z","iopub.execute_input":"2021-10-03T11:05:00.349266Z","iopub.status.idle":"2021-10-03T11:05:01.208237Z","shell.execute_reply.started":"2021-10-03T11:05:00.349240Z","shell.execute_reply":"2021-10-03T11:05:01.207531Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Load Data","metadata":{}},{"cell_type":"code","source":"train_src = '../input/dog-breed-identification/train'\ntest_src = '../input/dog-breed-identification/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-03T11:05:01.209396Z","iopub.execute_input":"2021-10-03T11:05:01.211375Z","iopub.status.idle":"2021-10-03T11:05:01.815897Z","shell.execute_reply.started":"2021-10-03T11:05:01.211346Z","shell.execute_reply":"2021-10-03T11:05:01.815042Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_size = len(os.listdir(train_src))\ntest_size = len(os.listdir(test_src))\n\nprint(train_size,test_size)\nprint(train_labels.shape)\nprint(submission.shape)","metadata":{"execution":{"iopub.status.busy":"2021-10-03T11:05:01.818146Z","iopub.execute_input":"2021-10-03T11:05:01.818396Z","iopub.status.idle":"2021-10-03T11:05:02.313073Z","shell.execute_reply.started":"2021-10-03T11:05:01.818365Z","shell.execute_reply":"2021-10-03T11:05:02.312252Z"},"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-03T11:05:02.314169Z","iopub.execute_input":"2021-10-03T11:05:02.314592Z","iopub.status.idle":"2021-10-03T11:05:02.326250Z","shell.execute_reply.started":"2021-10-03T11:05:02.314556Z","shell.execute_reply":"2021-10-03T11:05:02.325247Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"display(train_labels.head())\ndisplay(submission.head())","metadata":{"execution":{"iopub.status.busy":"2021-10-03T11:05:02.327723Z","iopub.execute_input":"2021-10-03T11:05:02.327971Z","iopub.status.idle":"2021-10-03T11:05:02.366292Z","shell.execute_reply.started":"2021-10-03T11:05:02.327938Z","shell.execute_reply":"2021-10-03T11:05:02.365680Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_labels['breed'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2021-10-03T11:05:02.368708Z","iopub.execute_input":"2021-10-03T11:05:02.368890Z","iopub.status.idle":"2021-10-03T11:05:02.381303Z","shell.execute_reply.started":"2021-10-03T11:05:02.368869Z","shell.execute_reply":"2021-10-03T11:05:02.380485Z"},"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-03T11:05:02.382313Z","iopub.execute_input":"2021-10-03T11:05:02.382750Z","iopub.status.idle":"2021-10-03T11:05:04.711105Z","shell.execute_reply.started":"2021-10-03T11:05:02.382718Z","shell.execute_reply":"2021-10-03T11:05:04.710335Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Prepare the Train and Test Data Directories","metadata":{}},{"cell_type":"code","source":"data_dir = pathlib.Path('/kaggle/working/data')\ndata_dir.mkdir()","metadata":{"execution":{"iopub.status.busy":"2021-10-03T11:05:04.712238Z","iopub.execute_input":"2021-10-03T11:05:04.712568Z","iopub.status.idle":"2021-10-03T11:05:04.718338Z","shell.execute_reply.started":"2021-10-03T11:05:04.712524Z","shell.execute_reply":"2021-10-03T11:05:04.717317Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dir = data_dir / 'train'\ntest_dir = data_dir / 'test'\n\ntrain_dir.mkdir()\ntest_dir.mkdir()","metadata":{"execution":{"iopub.status.busy":"2021-10-03T11:05:04.722401Z","iopub.execute_input":"2021-10-03T11:05:04.722717Z","iopub.status.idle":"2021-10-03T11:05:04.728253Z","shell.execute_reply.started":"2021-10-03T11:05:04.722660Z","shell.execute_reply":"2021-10-03T11:05:04.727282Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## Create a directory for each dog breed classes\nfor breed in dog_breeds:\n    breed_dir = train_dir / breed\n    breed_dir.mkdir()","metadata":{"execution":{"iopub.status.busy":"2021-10-03T11:05:04.729936Z","iopub.execute_input":"2021-10-03T11:05:04.730355Z","iopub.status.idle":"2021-10-03T11:05:04.742790Z","shell.execute_reply.started":"2021-10-03T11:05:04.730292Z","shell.execute_reply":"2021-10-03T11:05:04.741954Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for root, dirs, files in os.walk(train_src):\n    for file in files:\n        imgName = file.split('.')[0]\n        src_file = os.path.join(train_src, file)\n        destination = os.path.join(train_dir, train_labels.loc[imgName, 'breed'])\n        shutil.copy2(src_file, destination)","metadata":{"scrolled":true,"execution":{"iopub.status.busy":"2021-10-03T11:05:04.744122Z","iopub.execute_input":"2021-10-03T11:05:04.744387Z","iopub.status.idle":"2021-10-03T11:05:59.505630Z","shell.execute_reply.started":"2021-10-03T11:05:04.744356Z","shell.execute_reply":"2021-10-03T11:05:59.504761Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"shutil.copytree(test_src, test_dir / 'images')","metadata":{"execution":{"iopub.status.busy":"2021-10-03T11:05:59.507122Z","iopub.execute_input":"2021-10-03T11:05:59.507385Z","iopub.status.idle":"2021-10-03T11:06:53.448649Z","shell.execute_reply.started":"2021-10-03T11:05:59.507351Z","shell.execute_reply":"2021-10-03T11:06:53.447872Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Global Variables","metadata":{}},{"cell_type":"code","source":"N_EPOCHS = 50\nBATCH_SIZE = 32\nIMG_HEIGHT = 299\nIMG_WIDTH = 299","metadata":{"execution":{"iopub.status.busy":"2021-10-03T11:06:53.449774Z","iopub.execute_input":"2021-10-03T11:06:53.450018Z","iopub.status.idle":"2021-10-03T11:06:53.455680Z","shell.execute_reply.started":"2021-10-03T11:06:53.449986Z","shell.execute_reply":"2021-10-03T11:06:53.454077Z"},"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.2,\n  subset=\"training\",\n)","metadata":{"execution":{"iopub.status.busy":"2021-10-03T11:06:53.457213Z","iopub.execute_input":"2021-10-03T11:06:53.457547Z","iopub.status.idle":"2021-10-03T11:06:55.670940Z","shell.execute_reply.started":"2021-10-03T11:06:53.457434Z","shell.execute_reply":"2021-10-03T11:06:55.670199Z"},"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-03T11:06:55.672254Z","iopub.execute_input":"2021-10-03T11:06:55.672514Z","iopub.status.idle":"2021-10-03T11:06:55.677358Z","shell.execute_reply.started":"2021-10-03T11:06:55.672482Z","shell.execute_reply":"2021-10-03T11:06:55.676612Z"},"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.2,\n  subset=\"validation\",\n)","metadata":{"execution":{"iopub.status.busy":"2021-10-03T11:06:55.678757Z","iopub.execute_input":"2021-10-03T11:06:55.679290Z","iopub.status.idle":"2021-10-03T11:06:56.033675Z","shell.execute_reply.started":"2021-10-03T11:06:55.679252Z","shell.execute_reply":"2021-10-03T11:06:56.033014Z"},"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-03T11:06:56.034822Z","iopub.execute_input":"2021-10-03T11:06:56.035086Z","iopub.status.idle":"2021-10-03T11:06:56.469723Z","shell.execute_reply.started":"2021-10-03T11:06:56.035053Z","shell.execute_reply":"2021-10-03T11:06:56.462056Z"},"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-03T11:08:24.451855Z","iopub.execute_input":"2021-10-03T11:08:24.452437Z","iopub.status.idle":"2021-10-03T11:08:26.913867Z","shell.execute_reply.started":"2021-10-03T11:08:24.452399Z","shell.execute_reply":"2021-10-03T11:08:26.913214Z"},"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-03T11:08:26.915428Z","iopub.execute_input":"2021-10-03T11:08:26.915860Z","iopub.status.idle":"2021-10-03T11:08:29.455485Z","shell.execute_reply.started":"2021-10-03T11:08:26.915826Z","shell.execute_reply":"2021-10-03T11:08:29.454815Z"},"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-03T11:08:29.456674Z","iopub.execute_input":"2021-10-03T11:08:29.457182Z","iopub.status.idle":"2021-10-03T11:08:31.251943Z","shell.execute_reply.started":"2021-10-03T11:08:29.457143Z","shell.execute_reply":"2021-10-03T11:08:31.251294Z"},"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\ntrain_ds = train_ds.cache().prefetch(buffer_size=AUTOTUNE)\nval_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-03T11:08:31.253537Z","iopub.execute_input":"2021-10-03T11:08:31.254239Z","iopub.status.idle":"2021-10-03T11:08:31.263862Z","shell.execute_reply.started":"2021-10-03T11:08:31.254203Z","shell.execute_reply":"2021-10-03T11:08:31.263183Z"},"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-03T11:08:31.265089Z","iopub.execute_input":"2021-10-03T11:08:31.265477Z","iopub.status.idle":"2021-10-03T11:08:31.294369Z","shell.execute_reply.started":"2021-10-03T11:08:31.265426Z","shell.execute_reply":"2021-10-03T11:08:31.293819Z"},"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-03T11:08:31.295413Z","iopub.execute_input":"2021-10-03T11:08:31.295815Z","iopub.status.idle":"2021-10-03T11:08:33.105009Z","shell.execute_reply.started":"2021-10-03T11:08:31.295768Z","shell.execute_reply":"2021-10-03T11:08:33.104265Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Build a Model","metadata":{}},{"cell_type":"code","source":"base_model = Xception(weights='imagenet', include_top=False, input_shape=(299,299,3)) \n# display(base_model.summary())\n\n# train only the top layers (which were randomly initialized)\n# i.e. freeze all convolutional Xception layers\nbase_model.trainable = False\n\ninputs = Input(shape=(299, 299, 3))\nx = data_augmentation(inputs)\nx = preprocess_input(x) ##  tf.keras.applications.xception.preprocess_input\n\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`.\nx = base_model(x, training=False)\n\nx = GlobalAveragePooling2D()(x)\n\n#     adding extra dense layer\n#     x = Dense(1024, activation='relu')(x)\n#     x = Dropout(.7)(x)\n#     x = Dense(512, activation='relu')(x)\n\nx = Dropout(.5)(x)\noutputs = Dense(120, activation='softmax')(x)\nmodel = Model(inputs, outputs)\n\ndisplay(model.summary())","metadata":{"scrolled":true,"execution":{"iopub.status.busy":"2021-10-03T11:08:33.106433Z","iopub.execute_input":"2021-10-03T11:08:33.106839Z","iopub.status.idle":"2021-10-03T11:08:35.490225Z","shell.execute_reply.started":"2021-10-03T11:08:33.106802Z","shell.execute_reply":"2021-10-03T11:08:35.489512Z"},"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-03T11:08:35.493427Z","iopub.execute_input":"2021-10-03T11:08:35.493650Z","iopub.status.idle":"2021-10-03T11:08:35.731843Z","shell.execute_reply.started":"2021-10-03T11:08:35.493626Z","shell.execute_reply":"2021-10-03T11:08:35.731082Z"},"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-03T11:08:40.294478Z","iopub.execute_input":"2021-10-03T11:08:40.294941Z","iopub.status.idle":"2021-10-03T11:08:40.302093Z","shell.execute_reply.started":"2021-10-03T11:08:40.294906Z","shell.execute_reply":"2021-10-03T11:08:40.301362Z"},"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-03T11:08:44.901775Z","iopub.execute_input":"2021-10-03T11:08:44.902032Z","iopub.status.idle":"2021-10-03T11:21:13.586457Z","shell.execute_reply.started":"2021-10-03T11:08:44.902003Z","shell.execute_reply":"2021-10-03T11:21:13.583339Z"},"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-03T11:21:48.158088Z","iopub.execute_input":"2021-10-03T11:21:48.158355Z","iopub.status.idle":"2021-10-03T11:21:48.641081Z","shell.execute_reply.started":"2021-10-03T11:21:48.158327Z","shell.execute_reply":"2021-10-03T11:21:48.640263Z"},"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","metadata":{"execution":{"iopub.status.busy":"2021-10-03T11:22:01.525493Z","iopub.execute_input":"2021-10-03T11:22:01.526959Z","iopub.status.idle":"2021-10-03T11:22:01.919919Z","shell.execute_reply.started":"2021-10-03T11:22:01.526915Z","shell.execute_reply":"2021-10-03T11:22:01.919179Z"},"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-03T11:24:34.608193Z","iopub.execute_input":"2021-10-03T11:24:34.608494Z","iopub.status.idle":"2021-10-03T11:25:16.768595Z","shell.execute_reply.started":"2021-10-03T11:24:34.608443Z","shell.execute_reply":"2021-10-03T11:25:16.767871Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(predictions.shape)\nprint(predictions)","metadata":{"execution":{"iopub.status.busy":"2021-10-03T11:25:55.357248Z","iopub.execute_input":"2021-10-03T11:25:55.357554Z","iopub.status.idle":"2021-10-03T11:25:55.366108Z","shell.execute_reply.started":"2021-10-03T11:25:55.357522Z","shell.execute_reply":"2021-10-03T11:25:55.365212Z"},"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-03T11:25:59.534092Z","iopub.execute_input":"2021-10-03T11:25:59.534363Z","iopub.status.idle":"2021-10-03T11:26:00.050724Z","shell.execute_reply.started":"2021-10-03T11:25:59.534335Z","shell.execute_reply":"2021-10-03T11:26:00.050011Z"},"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-03T11:26:08.628417Z","iopub.execute_input":"2021-10-03T11:26:08.629233Z","iopub.status.idle":"2021-10-03T11:26:11.148219Z","shell.execute_reply.started":"2021-10-03T11:26:08.629188Z","shell.execute_reply":"2021-10-03T11:26:11.147508Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}