{"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":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-05-23T16:30:50.552742Z","iopub.execute_input":"2021-05-23T16:30:50.553348Z","iopub.status.idle":"2021-05-23T16:30:50.648151Z","shell.execute_reply.started":"2021-05-23T16:30:50.553224Z","shell.execute_reply":"2021-05-23T16:30:50.647091Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pwd","metadata":{"execution":{"iopub.status.busy":"2021-05-23T16:30:51.232677Z","iopub.execute_input":"2021-05-23T16:30:51.233018Z","iopub.status.idle":"2021-05-23T16:30:51.971137Z","shell.execute_reply.started":"2021-05-23T16:30:51.232983Z","shell.execute_reply":"2021-05-23T16:30:51.970033Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install -q -U albumentations","metadata":{"execution":{"iopub.status.busy":"2021-05-23T16:30:52.267299Z","iopub.execute_input":"2021-05-23T16:30:52.267688Z","iopub.status.idle":"2021-05-23T16:31:00.795073Z","shell.execute_reply.started":"2021-05-23T16:30:52.267652Z","shell.execute_reply":"2021-05-23T16:31:00.793592Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install tensorflow-addons","metadata":{"execution":{"iopub.status.busy":"2021-05-23T16:31:00.797828Z","iopub.execute_input":"2021-05-23T16:31:00.798201Z","iopub.status.idle":"2021-05-23T16:31:07.351129Z","shell.execute_reply.started":"2021-05-23T16:31:00.798161Z","shell.execute_reply":"2021-05-23T16:31:07.349410Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install -U --pre efficientnet","metadata":{"execution":{"iopub.status.busy":"2021-05-23T16:31:07.354170Z","iopub.execute_input":"2021-05-23T16:31:07.354750Z","iopub.status.idle":"2021-05-23T16:31:14.706266Z","shell.execute_reply.started":"2021-05-23T16:31:07.354679Z","shell.execute_reply":"2021-05-23T16:31:14.705191Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Import Dependencies\nimport os\nimport re\nimport math\nimport numpy as np\nfrom kaggle_datasets import KaggleDatasets\nimport tensorflow as tf\nimport tensorflow_addons as tfa\nimport efficientnet.tfkeras as efn\nfrom sklearn.metrics import f1_score, precision_score, recall_score, confusion_matrix\nfrom albumentations import (\n    Compose, RandomBrightness, JpegCompression, HueSaturationValue, RandomContrast, HorizontalFlip,\n    Rotate, Blur,\n)\nfrom matplotlib import pyplot as plt\n%matplotlib inline","metadata":{"execution":{"iopub.status.busy":"2021-05-23T16:31:14.707877Z","iopub.execute_input":"2021-05-23T16:31:14.708181Z","iopub.status.idle":"2021-05-23T16:31:23.288068Z","shell.execute_reply.started":"2021-05-23T16:31:14.708149Z","shell.execute_reply":"2021-05-23T16:31:23.286753Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tf.__version__","metadata":{"execution":{"iopub.status.busy":"2021-05-23T16:31:23.289902Z","iopub.execute_input":"2021-05-23T16:31:23.290515Z","iopub.status.idle":"2021-05-23T16:31:23.299247Z","shell.execute_reply.started":"2021-05-23T16:31:23.290459Z","shell.execute_reply":"2021-05-23T16:31:23.297971Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.random.uniform()","metadata":{"execution":{"iopub.status.busy":"2021-05-23T16:31:25.719937Z","iopub.execute_input":"2021-05-23T16:31:25.720441Z","iopub.status.idle":"2021-05-23T16:31:25.725976Z","shell.execute_reply.started":"2021-05-23T16:31:25.720396Z","shell.execute_reply":"2021-05-23T16:31:25.724980Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Set number of items to pre-fetch to Autotune\nAUTO = tf.data.experimental.AUTOTUNE","metadata":{"execution":{"iopub.status.busy":"2021-05-23T16:31:26.173801Z","iopub.execute_input":"2021-05-23T16:31:26.174480Z","iopub.status.idle":"2021-05-23T16:31:26.179442Z","shell.execute_reply.started":"2021-05-23T16:31:26.174412Z","shell.execute_reply":"2021-05-23T16:31:26.178234Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Setup TPU\ntry:\n    tpu = tf.distribute.cluster_resolver.TPUClusterResolver.connect() # TPU detection\n    tf.config.experimental_connect_to_cluster(tpu)\n    tf.tpu.experimental.initialize_tpu_system(tpu)\n    strategy = tf.distribute.TPUStrategy(tpu)\nexcept ValueError:\n    strategy = tf.distribute.MirroredStrategy()\n\nprint(\"Number of accelerators: \", strategy.num_replicas_in_sync)","metadata":{"execution":{"iopub.status.busy":"2021-05-23T16:31:26.493371Z","iopub.execute_input":"2021-05-23T16:31:26.494177Z","iopub.status.idle":"2021-05-23T16:31:39.539321Z","shell.execute_reply.started":"2021-05-23T16:31:26.494125Z","shell.execute_reply":"2021-05-23T16:31:39.538327Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Since TPU's read data from a GCS Bucket only, load all data to GCS Bucket\nGCS_DS_PATH = KaggleDatasets().get_gcs_path()","metadata":{"execution":{"iopub.status.busy":"2021-05-23T16:31:39.540674Z","iopub.execute_input":"2021-05-23T16:31:39.540952Z","iopub.status.idle":"2021-05-23T16:31:39.907585Z","shell.execute_reply.started":"2021-05-23T16:31:39.540925Z","shell.execute_reply":"2021-05-23T16:31:39.906514Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Configure Training Parameters\nIMAGE_SIZE = [512, 512]\nEPOCHS = 20\nBATCH_SIZE = 16 * strategy.num_replicas_in_sync","metadata":{"execution":{"iopub.status.busy":"2021-05-23T16:31:39.911328Z","iopub.execute_input":"2021-05-23T16:31:39.911696Z","iopub.status.idle":"2021-05-23T16:31:39.916889Z","shell.execute_reply.started":"2021-05-23T16:31:39.911662Z","shell.execute_reply":"2021-05-23T16:31:39.915528Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Select the dataset to use for training\nGCS_PATH_SELECT = {\n    192: GCS_DS_PATH + '/tfrecords-jpeg-192x192',\n    224: GCS_DS_PATH + '/tfrecords-jpeg-224x224',\n    331: GCS_DS_PATH + '/tfrecords-jpeg-331x331',\n    512: GCS_DS_PATH + '/tfrecords-jpeg-512x512'\n}\n\n# Selecting 512 x 512 images here\nGCS_PATH = GCS_PATH_SELECT[IMAGE_SIZE[0]]","metadata":{"execution":{"iopub.status.busy":"2021-05-23T16:31:42.171169Z","iopub.execute_input":"2021-05-23T16:31:42.171543Z","iopub.status.idle":"2021-05-23T16:31:42.177731Z","shell.execute_reply.started":"2021-05-23T16:31:42.171507Z","shell.execute_reply":"2021-05-23T16:31:42.176582Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Get filepaths for train, val and test set\nTRAINING_FILENAMES = tf.io.gfile.glob(GCS_PATH + '/train/*.tfrec')\nVALIDATION_FILENAMES = tf.io.gfile.glob(GCS_PATH + '/val/*.tfrec')\nTEST_FILENAMES = tf.io.gfile.glob(GCS_PATH + '/test/*.tfrec')\n\n# Adds more dataset to model training\nSKIP_VALIDATION = True\nif SKIP_VALIDATION:\n    TRAINING_FILENAMES = TRAINING_FILENAMES + VALIDATION_FILENAMES","metadata":{"execution":{"iopub.status.busy":"2021-05-23T16:31:42.451738Z","iopub.execute_input":"2021-05-23T16:31:42.452118Z","iopub.status.idle":"2021-05-23T16:31:42.728166Z","shell.execute_reply.started":"2021-05-23T16:31:42.452083Z","shell.execute_reply":"2021-05-23T16:31:42.727071Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Sample Training TFRecord Path\nTRAINING_FILENAMES[0]","metadata":{"execution":{"iopub.status.busy":"2021-05-23T16:31:42.743483Z","iopub.execute_input":"2021-05-23T16:31:42.743851Z","iopub.status.idle":"2021-05-23T16:31:42.749414Z","shell.execute_reply.started":"2021-05-23T16:31:42.743817Z","shell.execute_reply":"2021-05-23T16:31:42.748568Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Class Names\nCLASSES = ['pink primrose',    'hard-leaved pocket orchid', 'canterbury bells', 'sweet pea',     'wild geranium',     'tiger lily',           'moon orchid',              'bird of paradise', 'monkshood',        'globe thistle',         # 00 - 09\n           'snapdragon',       \"colt's foot\",               'king protea',      'spear thistle', 'yellow iris',       'globe-flower',         'purple coneflower',        'peruvian lily',    'balloon flower',   'giant white arum lily', # 10 - 19\n           'fire lily',        'pincushion flower',         'fritillary',       'red ginger',    'grape hyacinth',    'corn poppy',           'prince of wales feathers', 'stemless gentian', 'artichoke',        'sweet william',         # 20 - 29\n           'carnation',        'garden phlox',              'love in the mist', 'cosmos',        'alpine sea holly',  'ruby-lipped cattleya', 'cape flower',              'great masterwort', 'siam tulip',       'lenten rose',           # 30 - 39\n           'barberton daisy',  'daffodil',                  'sword lily',       'poinsettia',    'bolero deep blue',  'wallflower',           'marigold',                 'buttercup',        'daisy',            'common dandelion',      # 40 - 49\n           'petunia',          'wild pansy',                'primula',          'sunflower',     'lilac hibiscus',    'bishop of llandaff',   'gaura',                    'geranium',         'orange dahlia',    'pink-yellow dahlia',    # 50 - 59\n           'cautleya spicata', 'japanese anemone',          'black-eyed susan', 'silverbush',    'californian poppy', 'osteospermum',         'spring crocus',            'iris',             'windflower',       'tree poppy',            # 60 - 69\n           'gazania',          'azalea',                    'water lily',       'rose',          'thorn apple',       'morning glory',        'passion flower',           'lotus',            'toad lily',        'anthurium',             # 70 - 79\n           'frangipani',       'clematis',                  'hibiscus',         'columbine',     'desert-rose',       'tree mallow',          'magnolia',                 'cyclamen ',        'watercress',       'canna lily',            # 80 - 89\n           'hippeastrum ',     'bee balm',                  'pink quill',       'foxglove',      'bougainvillea',     'camellia',             'mallow',                   'mexican petunia',  'bromelia',         'blanket flower',        # 90 - 99\n           'trumpet creeper',  'blackberry lily',           'common tulip',     'wild rose'] ","metadata":{"execution":{"iopub.status.busy":"2021-05-23T16:31:46.817919Z","iopub.execute_input":"2021-05-23T16:31:46.818316Z","iopub.status.idle":"2021-05-23T16:31:46.827994Z","shell.execute_reply.started":"2021-05-23T16:31:46.818262Z","shell.execute_reply":"2021-05-23T16:31:46.826850Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Visualization Utilities","metadata":{}},{"cell_type":"code","source":"# numpy and matplotlib defaults\nnp.set_printoptions(threshold=15, linewidth=80)\n\ndef batch_to_numpy_images_and_labels(data):\n    images, labels = data\n    numpy_images = images.numpy()\n    numpy_labels = labels.numpy()\n    if numpy_labels.dtype == object: # binary string in this case, these are image ID strings\n        numpy_labels = [None for _ in enumerate(numpy_images)]\n    # If no labels, only image IDs, return None for labels (this is the case for test data)\n    return numpy_images, numpy_labels\n\ndef title_from_label_and_target(label, correct_label):\n    if correct_label is None:\n        return CLASSES[label], True\n    correct = (label == correct_label)\n    return \"{} [{}{}{}]\".format(CLASSES[label], 'OK' if correct else 'NO', u\"\\u2192\" if not correct else '',\n                                CLASSES[correct_label] if not correct else ''), correct\n\ndef display_one_flower(image, title, subplot, red=False, titlesize=16):\n    plt.subplot(*subplot)\n    plt.axis('off')\n    plt.imshow(image)\n    if len(title) > 0:\n        plt.title(title, fontsize=int(titlesize) if not red else int(titlesize/1.2), color='red' if red else 'black', fontdict={'verticalalignment':'center'}, pad=int(titlesize/1.5))\n    return (subplot[0], subplot[1], subplot[2]+1)\n    \ndef display_batch_of_images(databatch, predictions=None):\n    \"\"\"This will work with:\n    display_batch_of_images(images)\n    display_batch_of_images(images, predictions)\n    display_batch_of_images((images, labels))\n    display_batch_of_images((images, labels), predictions)\n    \"\"\"\n    # data\n    images, labels = batch_to_numpy_images_and_labels(databatch)\n    if labels is None:\n        labels = [None for _ in enumerate(images)]\n        \n    # auto-squaring: this will drop data that does not fit into square or square-ish rectangle\n    rows = int(math.sqrt(len(images)))\n    cols = len(images)//rows\n        \n    # size and spacing\n    FIGSIZE = 13.0\n    SPACING = 0.1\n    subplot=(rows,cols,1)\n    if rows < cols:\n        plt.figure(figsize=(FIGSIZE,FIGSIZE/cols*rows))\n    else:\n        plt.figure(figsize=(FIGSIZE/rows*cols,FIGSIZE))\n    \n    # display\n    for i, (image, label) in enumerate(zip(images[:rows*cols], labels[:rows*cols])):\n        title = '' if label is None else CLASSES[label]\n        correct = True\n        if predictions is not None:\n            title, correct = title_from_label_and_target(predictions[i], label)\n        dynamic_titlesize = FIGSIZE*SPACING/max(rows,cols)*40+3 # magic formula tested to work from 1x1 to 10x10 images\n        subplot = display_one_flower(image, title, subplot, not correct, titlesize=dynamic_titlesize)\n    \n    #layout\n    plt.tight_layout()\n    if label is None and predictions is None:\n        plt.subplots_adjust(wspace=0, hspace=0)\n    else:\n        plt.subplots_adjust(wspace=SPACING, hspace=SPACING)\n    plt.show()\n\ndef display_confusion_matrix(cmat, score, precision, recall):\n    plt.figure(figsize=(15,15))\n    ax = plt.gca()\n    ax.matshow(cmat, cmap='Reds')\n    ax.set_xticks(range(len(CLASSES)))\n    ax.set_xticklabels(CLASSES, fontdict={'fontsize': 7})\n    plt.setp(ax.get_xticklabels(), rotation=45, ha=\"left\", rotation_mode=\"anchor\")\n    ax.set_yticks(range(len(CLASSES)))\n    ax.set_yticklabels(CLASSES, fontdict={'fontsize': 7})\n    plt.setp(ax.get_yticklabels(), rotation=45, ha=\"right\", rotation_mode=\"anchor\")\n    titlestring = \"\"\n    if score is not None:\n        titlestring += 'f1 = {:.3f} '.format(score)\n    if precision is not None:\n        titlestring += '\\nprecision = {:.3f} '.format(precision)\n    if recall is not None:\n        titlestring += '\\nrecall = {:.3f} '.format(recall)\n    if len(titlestring) > 0:\n        ax.text(101, 1, titlestring, fontdict={'fontsize': 18, 'horizontalalignment':'right', 'verticalalignment':'top', 'color':'#804040'})\n    plt.show()\n    \ndef display_training_curves(training, validation, title, subplot):\n    if subplot%10==1: # set up the subplots on the first call\n        plt.subplots(figsize=(10,10), facecolor='#F0F0F0')\n        plt.tight_layout()\n    ax = plt.subplot(subplot)\n    ax.set_facecolor('#F8F8F8')\n    ax.plot(training)\n    ax.plot(validation)\n    ax.set_title('model '+ title)\n    ax.set_ylabel(title)\n    #ax.set_ylim(0.28,1.05)\n    ax.set_xlabel('epoch')\n    ax.legend(['train', 'valid.'])","metadata":{"execution":{"iopub.status.busy":"2021-05-23T16:31:48.056060Z","iopub.execute_input":"2021-05-23T16:31:48.056448Z","iopub.status.idle":"2021-05-23T16:31:48.083274Z","shell.execute_reply.started":"2021-05-23T16:31:48.056402Z","shell.execute_reply":"2021-05-23T16:31:48.082217Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Dataset Utilities","metadata":{}},{"cell_type":"code","source":"# Since the dataset is in the TFRecord format and all images inside that are in JPG format, we need a couple of helper functions\n# 1. Load the TFRecord file\n# 2. Read the Image, Label pairs from TFRecord Files\n# 3. For images without labels [Test Data], read only images with their id's\n# 4. Apply Data Augmentation\n\n# HELPER FUNCTIONS\n# Decode Images\ndef decode_image(image_data):\n    # Decode the JPEG Image\n    image = tf.image.decode_jpeg(image_data, channels=3)\n    # Reshape the image to set size\n    image = tf.reshape(image, [*IMAGE_SIZE, 3])\n    \n    # Return the decoded image\n    return image\n\n# Read and decode labeled tfrecord files [train/val]\ndef read_labeled_tfrecords(example):\n    # Configuration for parsing a single, fixed-length input feature i.e. \n    # a single instance of (image, label)\n    LABELED_TFREC_FORMAT = {\n        \"image\": tf.io.FixedLenFeature([], tf.string), # tf.string means bytestring\n        \"class\": tf.io.FixedLenFeature([], tf.int64),  # shape [] means single element\n    }\n    # Parse out the data using the format\n    example = tf.io.parse_single_example(example, LABELED_TFREC_FORMAT)\n    # Since the image is currently in a bytestring form, it needs to be decoded\n    image = decode_image(example['image'])\n    label = tf.cast(example['class'], tf.int32)\n    \n    # Return (image, label) pairs\n    return image, label\n\n# Read and decode unlabeled tfrecord files [test]\ndef read_unlabeled_tfrecords(example):\n    # Configuration for parsing a single, fixed-length input feature i.e. \n    # a single instance of (image, label)\n    # Since, no labels are present, we read in the image and it's id, no class\n    LABELED_TFREC_FORMAT = {\n        \"image\": tf.io.FixedLenFeature([], tf.string), # tf.string means bytestring\n        \"id\": tf.io.FixedLenFeature([], tf.string),  # shape [] means single element\n    }\n    \n    # Parse out the data using the format\n    example = tf.io.parse_single_example(example, LABELED_TFREC_FORMAT)\n    # Since the image is currently in a bytestring form, it needs to be decoded\n    image = decode_image(example['image'])\n    idNum = example['id']\n    \n    # Return (image, id) pairs\n    return image, idNum","metadata":{"execution":{"iopub.status.busy":"2021-05-23T16:31:54.602682Z","iopub.execute_input":"2021-05-23T16:31:54.603031Z","iopub.status.idle":"2021-05-23T16:31:54.612566Z","shell.execute_reply.started":"2021-05-23T16:31:54.603003Z","shell.execute_reply":"2021-05-23T16:31:54.611221Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Function to Load Dataset\ndef load_dataset(fnames, labeled=True, ordered=False):\n    # We read in the data in parallel and hence are not concerned about the order of it as we usually shuffle the data\n    ignore_order = tf.data.Options()\n    if not ordered:\n        ignore_order.experimental_deterministic = False\n    \n    # Read multiple TFRecord Files\n    dataset = tf.data.TFRecordDataset(fnames, num_parallel_reads=AUTO)\n    # Read data out of order, just as soon as it's read in\n    dataset = dataset.with_options(ignore_order)\n    # Read the (image, class) or (image, id) pairs from TFRecord files\n    dataset = dataset.map(read_labeled_tfrecords if labeled else read_unlabeled_tfrecords, num_parallel_calls=AUTO)\n    \n    # Return (image, class) or (image, id) pairs depending on the dataset\n    return dataset","metadata":{"execution":{"iopub.status.busy":"2021-05-23T16:31:56.648239Z","iopub.execute_input":"2021-05-23T16:31:56.648616Z","iopub.status.idle":"2021-05-23T16:31:56.654567Z","shell.execute_reply.started":"2021-05-23T16:31:56.648584Z","shell.execute_reply":"2021-05-23T16:31:56.653789Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Helper Function\n\n# Create Albumentation Augmentation Pipeline\ntransforms = Compose([\n            Rotate(limit=40),\n            RandomBrightness(limit=0.1),\n            JpegCompression(quality_lower=85, quality_upper=100, p=0.5),\n            HueSaturationValue(hue_shift_limit=20, sat_shift_limit=30, val_shift_limit=20, p=0.5),\n            RandomContrast(limit=0.2, p=0.5),\n            HorizontalFlip(),\n            Blur(p=0.5)\n])\n\n# Augmentation using albumentations\ndef aug_fn(image, img_size):\n    data = {\"image\": image}\n    # Apply transforms\n    aug_data = transforms(**data)\n    aug_img = aug_data[\"image\"]\n    aug_img = tf.cast(aug_img/255.0, tf.float32)\n    aug_img = tf.image.resize(aug_img, size=[img_size, img_size])\n    \n    return aug_img\n\n# Function to perform Data Augmentation\ndef data_augmentation(image, label):\n    aug_image = tf.numpy_function(func=aug_fn, inp=[image, IMAGE_SIZE[0]], Tout=tf.float32)\n    \n    # Return Augmented (image, label) pairs\n    return aug_image, label\n\n# Original Augment\ndef data_augment(image, label):\n    image = tf.image.random_flip_left_right(image)\n    # Random Flip Up/Down\n    image = tf.image.random_flip_up_down(image)\n    # Random Brightness\n    image = tf.image.random_brightness(image, 0.1)\n    \n    # Random Augmentations\n    if np.random.uniform() > 0.8:\n        # Random Rotation\n        image = tfa.image.rotate(image, 15.0)\n        # Random Shear in x direction\n        image = tfa.image.shear_x(image, 5.0, 0)\n        # Random Shear in y direction\n        image = tfa.image.shear_y(image, 5.0, 0)\n    \n    # Random Cutout\n    image = tfa.image.random_cutout(tf.expand_dims(image, 0), (362, 102))\n    image = tf.squeeze(image)\n    # Random Contrast\n    #image = tf.image.random_contrast(image, 0.2, 0.5)\n    return image, label","metadata":{"execution":{"iopub.status.busy":"2021-05-23T16:33:01.872940Z","iopub.execute_input":"2021-05-23T16:33:01.873296Z","iopub.status.idle":"2021-05-23T16:33:01.886815Z","shell.execute_reply.started":"2021-05-23T16:33:01.873266Z","shell.execute_reply":"2021-05-23T16:33:01.885724Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Helper Functions to get Train, Val, Test Datasets\n\n# Function to load Training Data\ndef get_train_dataset(ordered=False):\n    # Load Training Dataset\n    dataset = load_dataset(TRAINING_FILENAMES, labeled=True, ordered=ordered)\n    # Apply Augmentations\n    dataset = dataset.map(data_augment, num_parallel_calls=AUTO)\n    # Repeat Dataset for several epochs\n    dataset = dataset.repeat()\n    # Shuffle Dataset\n    dataset = dataset.shuffle(2048)\n    # Create a dataset batch\n    dataset = dataset.batch(BATCH_SIZE)\n    # Prefetch next batch of data while training\n    dataset = dataset.prefetch(AUTO)\n    \n    return dataset\n\n# Function to load Validation Data\ndef get_val_dataset(ordered=False):\n    # Load Validation Dataset\n    dataset = load_dataset(VALIDATION_FILENAMES, labeled=True, ordered=ordered)\n    # Create data batch\n    dataset = dataset.batch(BATCH_SIZE)\n    dataset = dataset.cache()\n    # Prefetch next batch of data while training\n    dataset = dataset.prefetch(AUTO)\n    \n    return dataset\n\n# Function to load Test Data\ndef get_test_dataset(ordered=False):\n    # Load Test Dataset\n    dataset = load_dataset(TEST_FILENAMES, labeled=False, ordered=ordered)\n    # Apply Augmentations\n    dataset = dataset.map(data_augment, num_parallel_calls=AUTO)\n    # Load data batch\n    dataset = dataset.batch(BATCH_SIZE)\n    # Prefetch next batch of data\n    dataset = dataset.prefetch(AUTO)\n    \n    return dataset","metadata":{"execution":{"iopub.status.busy":"2021-05-23T16:33:06.027218Z","iopub.execute_input":"2021-05-23T16:33:06.027626Z","iopub.status.idle":"2021-05-23T16:33:06.036856Z","shell.execute_reply.started":"2021-05-23T16:33:06.027588Z","shell.execute_reply":"2021-05-23T16:33:06.035824Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Function to count number of images in a TFRecord\ndef count_data_items(filenames):\n    # the number of data items is written in the name of the .tfrec files, i.e. flowers00-230.tfrec = 230 data items\n    n = [int(re.compile(r\"-([0-9]*)\\.\").search(filename).group(1)) for filename in filenames]\n    return np.sum(n)","metadata":{"execution":{"iopub.status.busy":"2021-05-23T16:33:06.457368Z","iopub.execute_input":"2021-05-23T16:33:06.457804Z","iopub.status.idle":"2021-05-23T16:33:06.462992Z","shell.execute_reply.started":"2021-05-23T16:33:06.457771Z","shell.execute_reply":"2021-05-23T16:33:06.461976Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Data Metrics\nNUM_TRAINING_IMAGES = count_data_items(TRAINING_FILENAMES)\nNUM_VALIDATION_IMAGES = (1 - SKIP_VALIDATION) * count_data_items(VALIDATION_FILENAMES)\nNUM_TEST_IMAGES = count_data_items(TEST_FILENAMES)\n\nSTEPS_PER_EPOCH = NUM_TRAINING_IMAGES // BATCH_SIZE\nVALIDATION_STEPS = -(-NUM_VALIDATION_IMAGES // BATCH_SIZE) # The \"-(-//)\" trick rounds up instead of down :-)\nTEST_STEPS = -(-NUM_TEST_IMAGES // BATCH_SIZE)             # The \"-(-//)\" trick rounds up instead of down :-)\n\nprint('Dataset: {} training images, {} validation images, {} unlabeled test images'.format(NUM_TRAINING_IMAGES, NUM_VALIDATION_IMAGES, NUM_TEST_IMAGES))","metadata":{"execution":{"iopub.status.busy":"2021-05-23T16:33:08.162856Z","iopub.execute_input":"2021-05-23T16:33:08.163375Z","iopub.status.idle":"2021-05-23T16:33:08.169420Z","shell.execute_reply.started":"2021-05-23T16:33:08.163342Z","shell.execute_reply":"2021-05-23T16:33:08.168721Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Dataset Visualization","metadata":{}},{"cell_type":"code","source":"# Evaluate Training Data\nprint(\"Training data shapes:\")\n# Take 3 batches of data from training set and evaluate\nfor image, label in get_train_dataset().take(3):\n    print(image.numpy().shape, label.numpy().shape)\nprint(\"Training data label examples:\", label.numpy())","metadata":{"execution":{"iopub.status.busy":"2021-05-23T16:33:08.538399Z","iopub.execute_input":"2021-05-23T16:33:08.538935Z","iopub.status.idle":"2021-05-23T16:33:11.184405Z","shell.execute_reply.started":"2021-05-23T16:33:08.538903Z","shell.execute_reply":"2021-05-23T16:33:11.183208Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Evaluate Validation Data\nprint(\"Validation data shapes:\")\n# Take 3 batches of data from validation set and evaluate\nfor image, label in get_val_dataset().take(3):\n    print(image.numpy().shape, label.numpy().shape)\nprint(\"Validation data label examples:\", label.numpy())","metadata":{"execution":{"iopub.status.busy":"2021-05-23T16:33:11.186179Z","iopub.execute_input":"2021-05-23T16:33:11.186776Z","iopub.status.idle":"2021-05-23T16:33:11.911332Z","shell.execute_reply.started":"2021-05-23T16:33:11.186727Z","shell.execute_reply":"2021-05-23T16:33:11.910112Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Evaluate Test Data\nprint(\"Test data shapes:\")\n# Take 3 batches of data from test set and evaluate\nfor image, idnum in get_test_dataset().take(3):\n    print(image.numpy().shape, idnum.numpy().shape)\nprint(\"Test data IDs:\", idnum.numpy().astype('U'))","metadata":{"execution":{"iopub.status.busy":"2021-05-23T16:33:11.913193Z","iopub.execute_input":"2021-05-23T16:33:11.913580Z","iopub.status.idle":"2021-05-23T16:33:13.247566Z","shell.execute_reply.started":"2021-05-23T16:33:11.913539Z","shell.execute_reply":"2021-05-23T16:33:13.246336Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Visualize Dataset","metadata":{}},{"cell_type":"code","source":"# Get a batch of data from training dataset\ntraining_dataset = get_train_dataset()\ntraining_dataset = training_dataset.unbatch().batch(20)\ntrain_batch = iter(training_dataset)\n\n# Display the batch of training images\ndisplay_batch_of_images(next(train_batch))","metadata":{"execution":{"iopub.status.busy":"2021-05-23T16:33:13.249118Z","iopub.execute_input":"2021-05-23T16:33:13.249622Z","iopub.status.idle":"2021-05-23T16:33:16.331665Z","shell.execute_reply.started":"2021-05-23T16:33:13.249576Z","shell.execute_reply":"2021-05-23T16:33:16.330082Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Get a batch of Test Data\ntest_dataset = get_test_dataset()\ntest_dataset = test_dataset.unbatch().batch(20)\ntest_batch = iter(test_dataset)\n\n# Display the batch of test images\ndisplay_batch_of_images(next(test_batch))","metadata":{"execution":{"iopub.status.busy":"2021-05-23T16:33:26.651701Z","iopub.execute_input":"2021-05-23T16:33:26.652078Z","iopub.status.idle":"2021-05-23T16:33:29.870801Z","shell.execute_reply.started":"2021-05-23T16:33:26.652043Z","shell.execute_reply":"2021-05-23T16:33:29.869658Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Learning Rate Scheduler","metadata":{}},{"cell_type":"code","source":"LR_START = 0.00001\nLR_MAX = 0.00005 * strategy.num_replicas_in_sync\nLR_MIN = 0.00001\nLR_RAMPUP_EPOCHS = 5\nLR_SUSTAIN_EPOCHS = 0\nLR_EXP_DECAY = .8\n\ndef lrfn(epoch):\n    if epoch < LR_RAMPUP_EPOCHS:\n        lr = (LR_MAX - LR_START) / LR_RAMPUP_EPOCHS * epoch + LR_START\n    elif epoch < LR_RAMPUP_EPOCHS + LR_SUSTAIN_EPOCHS:\n        lr = LR_MAX\n    else:\n        lr = (LR_MAX - LR_MIN) * LR_EXP_DECAY**(epoch - LR_RAMPUP_EPOCHS - LR_SUSTAIN_EPOCHS) + LR_MIN\n    return lr\n    \nlr_callback = tf.keras.callbacks.LearningRateScheduler(lrfn, verbose=True)\n\n# Visualize Learning Rate Scheduler\nrng = [i for i in range(EPOCHS)]\ny = [lrfn(x) for x in rng]\nplt.plot(rng, y)\nprint(\"Learning rate schedule: {:.3g} to {:.3g} to {:.3g}\".format(y[0], max(y), y[-1]))","metadata":{"execution":{"iopub.status.busy":"2021-05-23T16:34:05.873249Z","iopub.execute_input":"2021-05-23T16:34:05.873649Z","iopub.status.idle":"2021-05-23T16:34:06.043470Z","shell.execute_reply.started":"2021-05-23T16:34:05.873613Z","shell.execute_reply":"2021-05-23T16:34:06.042324Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"checkpoint1 = tf.keras.callbacks.ModelCheckpoint('EfficientNet_flower_clf.h5', verbose=1, monitor='val_loss', mode='min', \n                                        save_best_only=True, save_weights_only=True)\n\ncheckpoint2 = tf.keras.callbacks.ModelCheckpoint('DenseNet_flower_clf.h5', verbose=1, monitor='val_loss', mode='min', \n                                        save_best_only=True, save_weights_only=True)","metadata":{"execution":{"iopub.status.busy":"2021-05-23T16:34:12.806544Z","iopub.execute_input":"2021-05-23T16:34:12.806890Z","iopub.status.idle":"2021-05-23T16:34:12.812554Z","shell.execute_reply.started":"2021-05-23T16:34:12.806860Z","shell.execute_reply":"2021-05-23T16:34:12.811451Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Model Development\n\nHere, we'll try out a couple of models and approaches.\n\n1. Use VGG-16 pre-trained model\n2. Use DenseNet-101/152 pre-trained model\n3. Use EfficientNet-B0/B7 pre-trained model\n4. Try an ensemble of models from above list","metadata":{}},{"cell_type":"code","source":"# Function to get VGG model\ndef get_vgg_model():\n    # Get Image Pre-processing layer for VGG\n    img_adjust_layer = tf.keras.layers.Lambda(lambda data: tf.keras.applications.vgg16.preprocess_input(tf.cast(data, tf.float32)), input_shape=[*IMAGE_SIZE, 3])\n    # Get the pre-trained model without the classification output layer\n    pretrained_model = tf.keras.applications.VGG16(weights='imagenet', include_top=False)\n    # Freeze model layers\n    pretrained_model.trainable = False # False = transfer learning, True = fine-tuning\n    \n    # Setup the model\n    model = tf.keras.Sequential([\n        img_adjust_layer,\n        pretrained_model,\n        tf.keras.layers.GlobalAveragePooling2D(),\n        tf.keras.layers.Dense(len(CLASSES), activation='softmax')\n    ])\n    \n    return model","metadata":{"execution":{"iopub.status.busy":"2021-05-23T16:34:18.879288Z","iopub.execute_input":"2021-05-23T16:34:18.879692Z","iopub.status.idle":"2021-05-23T16:34:18.888038Z","shell.execute_reply.started":"2021-05-23T16:34:18.879658Z","shell.execute_reply":"2021-05-23T16:34:18.887002Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_denseNet_model():\n    img_adjust_layer = tf.keras.layers.Lambda(lambda data: tf.keras.applications.densenet.preprocess_input(tf.cast(data, tf.float32)), input_shape=[*IMAGE_SIZE, 3])\n    # Get the pre-trained model without the classification output layer\n    pretrained_model = tf.keras.applications.DenseNet201(weights='imagenet', include_top=False)\n    # Freeze model layers\n    #pretrained_model.trainable = False # False = transfer learning, True = fine-tuning\n    \n    model = tf.keras.Sequential([\n        img_adjust_layer,\n        pretrained_model,\n        tf.keras.layers.GlobalAveragePooling2D(),\n        tf.keras.layers.Dense(len(CLASSES), activation='softmax')\n    ])\n    \n    return model","metadata":{"execution":{"iopub.status.busy":"2021-05-23T16:34:20.810118Z","iopub.execute_input":"2021-05-23T16:34:20.810751Z","iopub.status.idle":"2021-05-23T16:34:20.817560Z","shell.execute_reply.started":"2021-05-23T16:34:20.810715Z","shell.execute_reply":"2021-05-23T16:34:20.816758Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_efficeintNet_model():\n    enet = efn.EfficientNetB7(\n        input_shape=(512, 512, 3),\n        weights='noisy-student',\n        include_top=False\n    )\n\n    model = tf.keras.Sequential([\n        enet,\n        tf.keras.layers.GlobalAveragePooling2D(),\n        tf.keras.layers.Dense(len(CLASSES), activation='softmax')\n    ])\n    \n    return model","metadata":{"execution":{"iopub.status.busy":"2021-05-23T16:34:21.297749Z","iopub.execute_input":"2021-05-23T16:34:21.298359Z","iopub.status.idle":"2021-05-23T16:34:21.304136Z","shell.execute_reply.started":"2021-05-23T16:34:21.298323Z","shell.execute_reply":"2021-05-23T16:34:21.303042Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Training Model 1: EfficientNet-B7","metadata":{}},{"cell_type":"code","source":"with strategy.scope():\n    # Get the model\n    model1 = get_efficeintNet_model()\n    \n    # Compile the Model\n    model1.compile(\n        optimizer='adam',\n        loss = 'sparse_categorical_crossentropy',\n        metrics=['sparse_categorical_accuracy'],\n        steps_per_execution=16\n    )","metadata":{"execution":{"iopub.status.busy":"2021-05-23T16:36:05.857562Z","iopub.execute_input":"2021-05-23T16:36:05.857956Z","iopub.status.idle":"2021-05-23T16:36:41.350887Z","shell.execute_reply.started":"2021-05-23T16:36:05.857923Z","shell.execute_reply":"2021-05-23T16:36:41.350067Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(model1.summary())","metadata":{"execution":{"iopub.status.busy":"2021-05-23T16:36:41.352205Z","iopub.execute_input":"2021-05-23T16:36:41.352626Z","iopub.status.idle":"2021-05-23T16:36:41.414295Z","shell.execute_reply.started":"2021-05-23T16:36:41.352595Z","shell.execute_reply":"2021-05-23T16:36:41.413450Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history1 = model1.fit(\n    get_train_dataset(),\n    steps_per_epoch=STEPS_PER_EPOCH,\n    epochs=EPOCHS,\n    validation_data=None if SKIP_VALIDATION else get_validation_dataset(),\n    callbacks=[lr_callback, checkpoint1])","metadata":{"execution":{"iopub.status.busy":"2021-05-23T16:36:41.415613Z","iopub.execute_input":"2021-05-23T16:36:41.416022Z","iopub.status.idle":"2021-05-23T17:13:10.823210Z","shell.execute_reply.started":"2021-05-23T16:36:41.415979Z","shell.execute_reply":"2021-05-23T17:13:10.821032Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Training Model 2: DenseNet-121","metadata":{}},{"cell_type":"code","source":"with strategy.scope():\n    # Get the model\n    model2 = get_denseNet_model()\n    \n    # Compile the Model\n    model2.compile(\n        optimizer='adam',\n        loss = 'sparse_categorical_crossentropy',\n        metrics=['sparse_categorical_accuracy'],\n        steps_per_execution=16\n    )","metadata":{"execution":{"iopub.status.busy":"2021-05-23T17:13:10.829874Z","iopub.execute_input":"2021-05-23T17:13:10.830317Z","iopub.status.idle":"2021-05-23T17:13:48.826557Z","shell.execute_reply.started":"2021-05-23T17:13:10.830252Z","shell.execute_reply":"2021-05-23T17:13:48.825406Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(model2.summary())","metadata":{"execution":{"iopub.status.busy":"2021-05-23T17:13:48.828367Z","iopub.execute_input":"2021-05-23T17:13:48.828715Z","iopub.status.idle":"2021-05-23T17:13:48.894948Z","shell.execute_reply.started":"2021-05-23T17:13:48.828682Z","shell.execute_reply":"2021-05-23T17:13:48.893656Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history2 = model2.fit(\n    get_train_dataset(),\n    steps_per_epoch=STEPS_PER_EPOCH,\n    epochs=EPOCHS,\n    validation_data=None if SKIP_VALIDATION else get_validation_dataset(),\n    callbacks=[lr_callback, checkpoint2])","metadata":{"execution":{"iopub.status.busy":"2021-05-23T17:13:48.896613Z","iopub.execute_input":"2021-05-23T17:13:48.897054Z","iopub.status.idle":"2021-05-23T17:33:18.179695Z","shell.execute_reply.started":"2021-05-23T17:13:48.897008Z","shell.execute_reply":"2021-05-23T17:33:18.178711Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Model Training Evaluation","metadata":{}},{"cell_type":"code","source":"if not SKIP_VALIDATION:\n    # Plot model training curves\n    display_training_curves(history1.history['loss'], history1.history['val_loss'], 'loss', 211)\n    display_training_curves(history1.history['sparse_categorical_accuracy'], history1.history['val_sparse_categorical_accuracy'], 'accuracy', 212)","metadata":{"execution":{"iopub.status.busy":"2021-05-23T17:39:43.091544Z","iopub.execute_input":"2021-05-23T17:39:43.092106Z","iopub.status.idle":"2021-05-23T17:39:43.141081Z","shell.execute_reply.started":"2021-05-23T17:39:43.092070Z","shell.execute_reply":"2021-05-23T17:39:43.139632Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Model Performance Evaluation\n\nFor evaluating the model performance, we'll use the following:\n\n1. Create a Confusion Matrix to check for True Positives, False Positives, True Negatives and False Negatives.\n2. Precision\n3. Recall","metadata":{}},{"cell_type":"code","source":"!ls /kaggle/working/","metadata":{"execution":{"iopub.status.busy":"2021-05-23T17:39:47.840225Z","iopub.execute_input":"2021-05-23T17:39:47.840629Z","iopub.status.idle":"2021-05-23T17:39:48.685251Z","shell.execute_reply.started":"2021-05-23T17:39:47.840596Z","shell.execute_reply":"2021-05-23T17:39:48.683967Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Load Best Trained Model\n# model.load_weights('/kaggle/working/EfficientNet_flower_clf.h5')","metadata":{"execution":{"iopub.status.busy":"2021-05-23T15:04:06.037863Z","iopub.execute_input":"2021-05-23T15:04:06.038263Z","iopub.status.idle":"2021-05-23T15:04:16.810987Z","shell.execute_reply.started":"2021-05-23T15:04:06.038225Z","shell.execute_reply":"2021-05-23T15:04:16.809501Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"if not SKIP_VALIDATION:\n    cmdataset = get_val_dataset(ordered=True)\n    images_ds = cmdataset.map(lambda image, label: image)\n    labels_ds = cmdataset.map(lambda image, label: label).unbatch()\n    cm_correct_labels = next(iter(labels_ds.batch(NUM_VALIDATION_IMAGES))).numpy()\n    \n    cm_probabilities1 = model1.predict(images_ds)\n    cm_probabilities2 = model2.predict(images_ds)\n    scores = []\n    \n    for alpha in np.linspace(0,1,100):\n        cm_probabilities = alpha * cm_probabilities1 + (1 - alpha) * cm_probabilities2\n        cm_predictions = np.argmax(cm_probabilities, axis=-1)\n        scores.append(f1_score(cm_correct_labels, cm_predictions, labels=range(len(CLASSES)), average='macro'))\n        \n    print(\"Correct   labels: \", cm_correct_labels.shape, cm_correct_labels)\n    print(\"Predicted labels: \", cm_predictions.shape, cm_predictions)\n    \n    plt.plot(scores)\n    best_alpha = np.argmax(scores)/100\n    cm_probabilities = best_alpha * cm_probabilities1 + (1 - best_alpha) * cm_probabilities2\n    cm_predictions = np.argmax(cm_probabilities, axis=-1)\n\nelse:\n    best_alpha = 0.44","metadata":{"execution":{"iopub.status.busy":"2021-05-23T17:39:52.967862Z","iopub.execute_input":"2021-05-23T17:39:52.968485Z","iopub.status.idle":"2021-05-23T17:39:52.981184Z","shell.execute_reply.started":"2021-05-23T17:39:52.968408Z","shell.execute_reply":"2021-05-23T17:39:52.979978Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"if not SKIP_VALIDATION:\n    cmat = confusion_matrix(cm_correct_labels, cm_predictions, labels=range(len(CLASSES)))\n    score = f1_score(cm_correct_labels, cm_predictions, labels=range(len(CLASSES)), average='macro')\n    precision = precision_score(cm_correct_labels, cm_predictions, labels=range(len(CLASSES)), average='macro')\n    recall = recall_score(cm_correct_labels, cm_predictions, labels=range(len(CLASSES)), average='macro')\n    cmat = (cmat.T / cmat.sum(axis=1)).T # normalized\n    display_confusion_matrix(cmat, score, precision, recall)\n    print('f1 score: {:.3f}, precision: {:.3f}, recall: {:.3f}'.format(score, precision, recall))","metadata":{"execution":{"iopub.status.busy":"2021-05-23T17:39:57.556464Z","iopub.execute_input":"2021-05-23T17:39:57.556820Z","iopub.status.idle":"2021-05-23T17:39:57.565669Z","shell.execute_reply.started":"2021-05-23T17:39:57.556791Z","shell.execute_reply":"2021-05-23T17:39:57.564443Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Make Predictions on Test Dataset using Test Time Augmentation [TTA]","metadata":{}},{"cell_type":"code","source":"# Get Prediction Probabilities from EfficientNet model\n# Number of Test Time Augmentations to perform\nTTA_NUM = 10\nprobabilities1 = []\n\nfor i in range(TTA_NUM):\n    test_ds = get_test_dataset(ordered=True)\n    print('Computing predictions...')\n    test_images_ds = test_ds.map(lambda image, idnum: image)\n    probabilities1.append(model1.predict(test_images_ds, steps=TEST_STEPS))\n\nprob1 = np.mean(probabilities1,axis=0)","metadata":{"execution":{"iopub.status.busy":"2021-05-23T17:44:02.892944Z","iopub.execute_input":"2021-05-23T17:44:02.893520Z","iopub.status.idle":"2021-05-23T17:46:52.945133Z","shell.execute_reply.started":"2021-05-23T17:44:02.893486Z","shell.execute_reply":"2021-05-23T17:46:52.943807Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Get Prediction Probabilities from DenseNet model\n# Number of Test Time Augmentations to perform\nTTA_NUM = 10\nprobabilities2 = []\n\nfor i in range(TTA_NUM):\n    test_ds = get_test_dataset(ordered=True)\n    print('Computing predictions...')\n    test_images_ds = test_ds.map(lambda image, idnum: image)\n    probabilities2.append(model2.predict(test_images_ds, steps=TEST_STEPS))\n\nprob2 = np.mean(probabilities2,axis =0)","metadata":{"execution":{"iopub.status.busy":"2021-05-23T17:46:52.946934Z","iopub.execute_input":"2021-05-23T17:46:52.947230Z","iopub.status.idle":"2021-05-23T17:49:35.299921Z","shell.execute_reply.started":"2021-05-23T17:46:52.947203Z","shell.execute_reply":"2021-05-23T17:49:35.298829Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"probb1: \", prob1)\nprint(\"probb2: \", prob2)","metadata":{"execution":{"iopub.status.busy":"2021-05-23T17:49:35.302007Z","iopub.execute_input":"2021-05-23T17:49:35.302453Z","iopub.status.idle":"2021-05-23T17:49:35.311059Z","shell.execute_reply.started":"2021-05-23T17:49:35.302388Z","shell.execute_reply":"2021-05-23T17:49:35.310015Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Calculate and Print Weighted Predictions of 2 models\nprob = best_alpha * prob1 + (1 - best_alpha) * prob2\npredictions = np.argmax(prob, axis=-1)\nprint(\"predictions: \", predictions)","metadata":{"execution":{"iopub.status.busy":"2021-05-23T17:49:42.863832Z","iopub.execute_input":"2021-05-23T17:49:42.864216Z","iopub.status.idle":"2021-05-23T17:49:42.875041Z","shell.execute_reply.started":"2021-05-23T17:49:42.864174Z","shell.execute_reply":"2021-05-23T17:49:42.874095Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('Generating submission.csv file...')\ntest_ids_ds = test_ds.map(lambda image, idnum: idnum).unbatch()\ntest_ids = next(iter(test_ids_ds.batch(NUM_TEST_IMAGES))).numpy().astype('U') # all in one batch\nnp.savetxt('submission.csv', np.rec.fromarrays([test_ids, predictions]), fmt=['%s', '%d'], delimiter=',', header='id,label', comments='')\n!head submission.csv","metadata":{"execution":{"iopub.status.busy":"2021-05-23T18:44:29.811326Z","iopub.execute_input":"2021-05-23T18:44:29.811772Z","iopub.status.idle":"2021-05-23T18:44:29.893163Z","shell.execute_reply.started":"2021-05-23T18:44:29.811684Z","shell.execute_reply":"2021-05-23T18:44:29.891438Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Visual Validation","metadata":{}},{"cell_type":"code","source":"# Get validation dataset\ndataset = get_test_dataset()\ndataset = dataset.unbatch().batch(20)\nbatch = iter(dataset)","metadata":{"execution":{"iopub.status.busy":"2021-05-23T17:51:29.355498Z","iopub.execute_input":"2021-05-23T17:51:29.355949Z","iopub.status.idle":"2021-05-23T17:51:29.568132Z","shell.execute_reply.started":"2021-05-23T17:51:29.355907Z","shell.execute_reply":"2021-05-23T17:51:29.566931Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Visualize Model Prdeictions\nimages, labels = next(batch)\npredictions = np.argmax(prob, axis=-1)\ndisplay_batch_of_images((images, labels), predictions)","metadata":{"execution":{"iopub.status.busy":"2021-05-23T17:54:03.064032Z","iopub.execute_input":"2021-05-23T17:54:03.064445Z","iopub.status.idle":"2021-05-23T17:54:12.182986Z","shell.execute_reply.started":"2021-05-23T17:54:03.064397Z","shell.execute_reply":"2021-05-23T17:54:12.179003Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}