{"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":"import pandas as pd\nimport numpy as np\nimport math, re, os\nimport cv2           \nimport numpy as np\nimport random\nimport os\nimport tensorflow as tf\nimport tensorflow_addons as tfa\nfrom tensorflow import keras\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras.preprocessing import image\nimport seaborn as sns\nimport collections\nfrom sklearn.model_selection import KFold\nfrom tensorflow.keras.applications import ResNet50V2,ResNet101V2,ResNet152V2,DenseNet201\nimport matplotlib.pyplot as plt\nfrom sklearn.metrics import f1_score, precision_score, recall_score, confusion_matrix\n\n\n# These are for class weights\nimport datetime\nimport tqdm\nimport json\nfrom collections import Counter\nimport gc\n","metadata":{"id":"RgTNuF2VCqAf","execution":{"iopub.status.busy":"2022-01-12T09:35:52.821567Z","iopub.execute_input":"2022-01-12T09:35:52.821796Z","iopub.status.idle":"2022-01-12T09:35:52.829437Z","shell.execute_reply.started":"2022-01-12T09:35:52.821769Z","shell.execute_reply":"2022-01-12T09:35:52.828563Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Initialize TPU\ntry:\n    tpu = tf.distribute.cluster_resolver.TPUClusterResolver()  # TPU detection\n    print('Running on TPU ', tpu.master())# Running on TPU grpc://\nexcept ValueError:\n    tpu = None\nif tpu:\n    tf.config.experimental_connect_to_cluster(tpu) #Connects to give cluster\n    tf.tpu.experimental.initialize_tpu_system(tpu) #Initialize TPU devices\n    strategy = tf.distribute.experimental.TPUStrategy(tpu)\nelse:\n    strategy = tf.distribute.get_strategy() # default distribution strategy in Tensorflow. Works on CPU and single GPU.\n\nprint(\"REPLICAS: \", strategy.num_replicas_in_sync) #Split across 8 TPU cores","metadata":{"id":"-xvZTRkLCwmm","outputId":"2c7f7849-0447-47fe-f378-d9a61a200d97","execution":{"iopub.status.busy":"2022-01-12T09:35:55.776795Z","iopub.execute_input":"2022-01-12T09:35:55.777082Z","iopub.status.idle":"2022-01-12T09:36:03.555585Z","shell.execute_reply.started":"2022-01-12T09:35:55.777053Z","shell.execute_reply":"2022-01-12T09:36:03.554752Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from kaggle_datasets import KaggleDatasets\n\nGCS_DS_PATH = KaggleDatasets().get_gcs_path('tpu-getting-started')\nprint(GCS_DS_PATH) # what do gcs paths look like?\n#GCS_DS_PATH = \"gs://kds-6d8f09dbd2512db4b94e7063db16ed25cc9d5ee315207d6971dd7644\"\nPATH2 = KaggleDatasets().get_gcs_path('tf-flower-photo-tfrec')\nprint(PATH2)","metadata":{"id":"4s7JpJ87DQ68","execution":{"iopub.status.busy":"2022-01-12T09:36:06.536339Z","iopub.execute_input":"2022-01-12T09:36:06.536607Z","iopub.status.idle":"2022-01-12T09:36:07.333837Z","shell.execute_reply.started":"2022-01-12T09:36:06.536581Z","shell.execute_reply":"2022-01-12T09:36:07.332847Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nimg_size=512\nIMAGE_SIZE = [512, 512]\nBATCH_SIZE = 16 * strategy.num_replicas_in_sync\nEPOCHS = 25\n\nGCS_PATH_SELECT = { # available image sizes\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}\nGCS_PATH = GCS_PATH_SELECT[IMAGE_SIZE[0]]\n\nAUTO = tf.data.experimental.AUTOTUNE\n\nTRAINING_FILENAMES = tf.io.gfile.glob(GCS_PATH + '/train/*.tfrec')\nTRAINING_FILENAMES += tf.io.gfile.glob(PATH2 + '/imagenet/tfrecords-jpeg-512x512/*.tfrec')\nTRAINING_FILENAMES += tf.io.gfile.glob(PATH2 + '/inaturalist_1/tfrecords-jpeg-512x512/*.tfrec')\nTRAINING_FILENAMES += tf.io.gfile.glob(PATH2 + '/openimage/tfrecords-jpeg-512x512/*.tfrec')\nTRAINING_FILENAMES += tf.io.gfile.glob(PATH2 + '/oxford_102/tfrecords-jpeg-512x512/*.tfrec')\nTRAINING_FILENAMES += tf.io.gfile.glob(PATH2 + '/tf_flowers/tfrecords-jpeg-512x512/*.tfrec')\n\n\nVALIDATION_FILENAMES = tf.io.gfile.glob(GCS_PATH + '/val/*.tfrec')\nTRAINING_FILENAMES += VALIDATION_FILENAMES\n\nTEST_FILENAMES = tf.io.gfile.glob(GCS_PATH + '/test/*.tfrec')\n\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']                                                                                                                                               # 100 - 102\n\n","metadata":{"execution":{"iopub.status.busy":"2022-01-12T09:36:09.202862Z","iopub.execute_input":"2022-01-12T09:36:09.203712Z","iopub.status.idle":"2022-01-12T09:36:09.921878Z","shell.execute_reply.started":"2022-01-12T09:36:09.203660Z","shell.execute_reply":"2022-01-12T09:36:09.920934Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# numpy and matplotlib defaults\nnp.set_printoptions(threshold=15, linewidth=80)\n#explore data\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":"2022-01-12T09:36:11.587298Z","iopub.execute_input":"2022-01-12T09:36:11.587976Z","iopub.status.idle":"2022-01-12T09:36:11.792678Z","shell.execute_reply.started":"2022-01-12T09:36:11.587924Z","shell.execute_reply":"2022-01-12T09:36:11.791669Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def random_blockout(img, sl=0.1, sh=0.2, rl=0.4):\n\n    h, w, c = img_size, img_size, 3\n    origin_area = tf.cast(h*w, tf.float32)\n\n    e_size_l = tf.cast(tf.round(tf.sqrt(origin_area * sl * rl)), tf.int32)\n    e_size_h = tf.cast(tf.round(tf.sqrt(origin_area * sh / rl)), tf.int32)\n\n    e_height_h = tf.minimum(e_size_h, h)\n    e_width_h = tf.minimum(e_size_h, w)\n\n    erase_height = tf.random.uniform(shape=[], minval=e_size_l, maxval=e_height_h, dtype=tf.int32)\n    erase_width = tf.random.uniform(shape=[], minval=e_size_l, maxval=e_width_h, dtype=tf.int32)\n\n    erase_area = tf.zeros(shape=[erase_height, erase_width, c])\n    erase_area = tf.cast(erase_area, tf.uint8)\n\n    pad_h = h - erase_height\n    pad_top = tf.random.uniform(shape=[], minval=0, maxval=pad_h, dtype=tf.int32)\n    pad_bottom = pad_h - pad_top\n\n    pad_w = w - erase_width\n    pad_left = tf.random.uniform(shape=[], minval=0, maxval=pad_w, dtype=tf.int32)\n    pad_right = pad_w - pad_left\n\n    erase_mask = tf.pad([erase_area], [[0,0],[pad_top, pad_bottom], [pad_left, pad_right], [0,0]], constant_values=1)\n    erase_mask = tf.squeeze(erase_mask, axis=0)\n    erased_img = tf.multiply(tf.cast(img,tf.float32), tf.cast(erase_mask, tf.float32))\n\n    return tf.cast(erased_img, img.dtype)","metadata":{"execution":{"iopub.status.busy":"2022-01-12T09:36:13.345700Z","iopub.execute_input":"2022-01-12T09:36:13.345979Z","iopub.status.idle":"2022-01-12T09:36:13.360591Z","shell.execute_reply.started":"2022-01-12T09:36:13.345946Z","shell.execute_reply":"2022-01-12T09:36:13.358974Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def decode_image(image_data):\n    image = tf.image.decode_jpeg(image_data, channels=3)\n    image = tf.cast(image, tf.float32) / 255.0  # convert image to floats in [0, 1] range\n    image = tf.reshape(image, [*IMAGE_SIZE, 3]) # explicit size needed for TPU\n    return image\n\ndef read_labeled_tfrecord(example):\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    example = tf.io.parse_single_example(example, LABELED_TFREC_FORMAT)\n    image = decode_image(example['image'])\n    label = tf.cast(example['class'], tf.int32)\n    return image, label # returns a dataset of (image, label) pairs\n\ndef read_unlabeled_tfrecord(example):\n    UNLABELED_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        # class is missing, this competitions's challenge is to predict flower classes for the test dataset\n    }\n    example = tf.io.parse_single_example(example, UNLABELED_TFREC_FORMAT)\n    image = decode_image(example['image'])\n    idnum = example['id']\n    return image, idnum # returns a dataset of image(s)\n\ndef load_dataset(filenames, labeled=True, ordered=False):\n    # Read from TFRecords. For optimal performance, reading from multiple files at once and\n    # disregarding data order. Order does not matter since we will be shuffling the data anyway.\n\n    ignore_order = tf.data.Options()\n    if not ordered:\n        ignore_order.experimental_deterministic = False # disable order, increase speed\n\n    dataset = tf.data.TFRecordDataset(filenames, num_parallel_reads=AUTO) # automatically interleaves reads from multiple files\n    dataset = dataset.with_options(ignore_order) # uses data as soon as it streams in, rather than in its original order\n    dataset = dataset.map(read_labeled_tfrecord if labeled else read_unlabeled_tfrecord, num_parallel_calls=AUTO)\n    # returns a dataset of (image, label) pairs if labeled=True or (image, id) pairs if labeled=False\n    return dataset\n\ndef data_augment(image, label):\n    seed = 100\n    flag = random.randint(1,3)\n    coef_1 = random.randint(70, 90) * 0.01\n    coef_2 = random.randint(70, 90) * 0.01\n    image = tf.image.rot90(image, tf.random.uniform(shape=[], minval=0, maxval=4, dtype=tf.dtypes.int32))\n    minval = IMAGE_SIZE[0] // 2\n    maxval = IMAGE_SIZE[0]\n    random_shape = tf.random.uniform(shape=[2], minval=minval, maxval=maxval, dtype=tf.dtypes.int32)\n    image = tf.image.random_crop(image, size=[random_shape[0], random_shape[1], 3])\n    image = tf.image.resize(image, size=[IMAGE_SIZE[0], IMAGE_SIZE[1]])\n     # Randomly flip images\n    if flag == 1:\n        image = tf.image.random_flip_left_right(image, seed = seed)\n    elif flag == 2:\n        image = tf.image.random_flip_up_down(image, seed = seed)\n    else:\n        image = tf.image.random_crop(image, [int(IMAGE_SIZE[0]*coef_1), int(IMAGE_SIZE[0]*coef_2), 3],seed=seed)\n\n    image = tf.image.random_saturation(image, 0.7, 1.3)\n    image = tf.image.random_brightness(image, 0.05)\n\n# Randomly reset contrast of images\n    image = tf.image.random_contrast(image, 0.3, 0.5, seed = seed)\n   \n\n    image = tf.clip_by_value(image, 0, 1)\n    image=random_blockout(image)\n    return image, label \n    # data augmentation. Thanks to the dataset.prefetch(AUTO) statement in the next function (below),\n    # this happens essentially for free on TPU. Data pipeline code is executed on the \"CPU\" part\n    # of the TPU while the TPU itself is computing gradients.\n#     image = tf.image.random_flip_left_right(image)\n#     image= random_blockout(image)\n#     image = tf.image.random_saturation(image, 0, 2)\n#     return image, label\n      \n############# ImageDataGenerator - random transformation #############\n\n# create an ImageDataGenerator \n# update this based on image augmenation exploration results\nimg_gen = tf.keras.preprocessing.image.ImageDataGenerator(\n    rotation_range=45, width_shift_range=0.15, height_shift_range=0.15,\n    brightness_range=None, zoom_range=[1.0, 1.25], fill_mode='constant', \n    horizontal_flip=True, preprocessing_function=None)\n\n# define data augmentation function with random_transform method \n# for dataset.map( ... )\ndef img_gen_random_transform(image, label):\n    # apply random_transform method to single image\n    image = img_gen.random_transform(image)\n    return image, label\n\n# define data augmentation function with random_transform method \n# for dataset.map( ... )\ndef img_gen_random_transform(image, label):\n    # apply random_transform method to single image\n    image = img_gen.random_transform(image)\n    return image, label\n\n# def get_training_wo_aug_dataset():\n#     dataset = load_dataset(TRAINING_FILENAMES, labeled=True)\n#     dataset = dataset.repeat() # the training dataset must repeat for several epochs\n#     dataset = dataset.shuffle(2048)\n#     dataset = dataset.batch(BATCH_SIZE)\n#     dataset = dataset.prefetch(AUTO) # prefetch next batch while training (autotune prefetch buffer size)\n#     return dataset   \n    \ndef get_training_dataset():\n    dataset = load_dataset(TRAINING_FILENAMES, labeled=True)\n      \n    #dataset2= load_dataset(TRAINING_FILENAMES, labeled=True)\n    dataset = dataset.map(data_augment, num_parallel_calls=AUTO)\n    #dataset = dataset.map(img_gen_random_transform, num_parallel_calls=AUTO)\n    dataset = dataset.repeat() # the training dataset must repeat for several epochs\n    dataset = dataset.shuffle(2048)\n    dataset = dataset.batch(BATCH_SIZE)\n    dataset = dataset.prefetch(AUTO) # prefetch next batch while training (autotune prefetch buffer size)\n    return dataset\n\ndef get_validation_dataset(ordered=False):\n    dataset = load_dataset(VALIDATION_FILENAMES, labeled=True, ordered=ordered)\n    dataset = dataset.batch(BATCH_SIZE)\n    dataset = dataset.cache()\n    dataset = dataset.prefetch(AUTO) # prefetch next batch while training (autotune prefetch buffer size)\n    return dataset\n\ndef get_test_dataset(ordered=False):\n    dataset = load_dataset(TEST_FILENAMES, labeled=False, ordered=ordered)\n    dataset = dataset.batch(BATCH_SIZE)\n    dataset = dataset.prefetch(AUTO) # prefetch next batch while training (autotune prefetch buffer size)\n    return dataset\n\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)\n#TRAINING_FILENAMES += train_batch\n\nNUM_TRAINING_IMAGES = count_data_items(TRAINING_FILENAMES)\nNUM_VALIDATION_IMAGES = count_data_items(VALIDATION_FILENAMES)\nNUM_TEST_IMAGES = count_data_items(TEST_FILENAMES)\nSTEPS_PER_EPOCH = NUM_TRAINING_IMAGES // BATCH_SIZE\nprint('Dataset: {} training images, {} validation images, {} unlabeled test images'.format(NUM_TRAINING_IMAGES, NUM_VALIDATION_IMAGES, NUM_TEST_IMAGES))","metadata":{"execution":{"iopub.status.busy":"2022-01-12T09:42:47.163414Z","iopub.execute_input":"2022-01-12T09:42:47.163740Z","iopub.status.idle":"2022-01-12T09:42:47.201486Z","shell.execute_reply.started":"2022-01-12T09:42:47.163706Z","shell.execute_reply":"2022-01-12T09:42:47.200650Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#TRAIN_BATCH_SIZE = int(1594.125)  * strategy.num_replicas_in_sync\nds_train = get_training_dataset()\nds_valid = get_validation_dataset()\nds_test = get_test_dataset()\n\nprint(\"Training:\", ds_train)\nprint (\"Validation:\", ds_valid)\nprint(\"Test:\", ds_test)\n\n# data dump\nprint(\"Training data shapes:\")\nfor image, label in get_training_dataset().take(3):\n    print(image.numpy().shape, label.numpy().shape)\nprint(\"Training data label examples:\", label.numpy())\nprint(\"Validation data shapes:\")\nfor image, label in get_validation_dataset().take(3):\n    print(image.numpy().shape, label.numpy().shape)\nprint(\"Validation data label examples:\", label.numpy())\nprint(\"Test data shapes:\")\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')) # U=unicode string\n\n","metadata":{"id":"rHgrWZy6dj-m","outputId":"c9e58a48-416a-4fa8-995b-f14727182b46","execution":{"iopub.status.busy":"2022-01-12T09:44:07.698254Z","iopub.execute_input":"2022-01-12T09:44:07.698550Z","iopub.status.idle":"2022-01-12T09:44:21.539459Z","shell.execute_reply.started":"2022-01-12T09:44:07.698522Z","shell.execute_reply":"2022-01-12T09:44:21.538402Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"'''\nno_aug_train_set = get_training_dataset(TRAINING_FILENAMES)\n# Re-run these codes to get the next batch of no aug training images\nno_aug_train_batch = (next(iter(no_aug_train_set.unbatch().batch(16)))) # get a batch for \nimages, _ = batch_to_numpy_images_and_labels(no_aug_train_batch)\n# create an ImageDataGenerator for random transformation\nexplore_img_gen = tf.keras.preprocessing.image.ImageDataGenerator(\n    rotation_range=45, width_shift_range=0.15, height_shift_range=0.15,\n    brightness_range=None, zoom_range=[0.75, 1.0], fill_mode='constant', \n    horizontal_flip=True, preprocessing_function=None\n)\n\nprint('Training Dataset')\nprint('Image Augmentation with random_transform method from ImageDataGenerator')\ni = 0\nROW=8  # rows of subplots\nCOL=4  # cols of subplots\nplt.figure(figsize=(COL*3.5,ROW*3))\nfor im in images:\n    plt.subplot(ROW,COL,i*2+1)\n    plt.title('no augmentation')\n    plt.axis('off')\n    plt.imshow(im)\n    plt.subplot(ROW,COL,i*2+2)\n    plt.title('rdm transorm from img_gen')\n    plt.axis('off')\n    plt.imshow(explore_img_gen.random_transform(im))\n    i+=1\nplt.show()\n'''","metadata":{"execution":{"iopub.status.busy":"2022-01-12T09:41:30.793431Z","iopub.execute_input":"2022-01-12T09:41:30.793772Z","iopub.status.idle":"2022-01-12T09:41:30.801824Z","shell.execute_reply.started":"2022-01-12T09:41:30.793738Z","shell.execute_reply":"2022-01-12T09:41:30.800922Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Peek at training data\ntraining_dataset = get_training_dataset()\ntraining_dataset = training_dataset.unbatch().batch(20)\ntraining_dataset = training_dataset.shuffle(20)\ntrain_batch = iter(training_dataset)","metadata":{"id":"RERG6fbVI9bk","execution":{"iopub.status.busy":"2022-01-12T09:44:29.646125Z","iopub.execute_input":"2022-01-12T09:44:29.646886Z","iopub.status.idle":"2022-01-12T09:44:29.828199Z","shell.execute_reply.started":"2022-01-12T09:44:29.646842Z","shell.execute_reply":"2022-01-12T09:44:29.827190Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# run this cell again for next set of images\ndisplay_batch_of_images(next(train_batch))","metadata":{"id":"fRl0sWOCJRZy","outputId":"1b92f878-c376-4d9c-8d75-752094ee10d4","execution":{"iopub.status.busy":"2022-01-12T09:44:31.352956Z","iopub.execute_input":"2022-01-12T09:44:31.353423Z","iopub.status.idle":"2022-01-12T09:44:40.981518Z","shell.execute_reply.started":"2022-01-12T09:44:31.353373Z","shell.execute_reply":"2022-01-12T09:44:40.980782Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# peek at test data\ntest_dataset = get_test_dataset()\ntest_dataset = test_dataset.unbatch().batch(20)\ntest_batch = iter(test_dataset)","metadata":{"execution":{"iopub.status.busy":"2022-01-12T09:44:44.773572Z","iopub.execute_input":"2022-01-12T09:44:44.774062Z","iopub.status.idle":"2022-01-12T09:44:44.811839Z","shell.execute_reply.started":"2022-01-12T09:44:44.773985Z","shell.execute_reply":"2022-01-12T09:44:44.811164Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# run this cell again for next set of images\ndisplay_batch_of_images(next(test_batch))","metadata":{"execution":{"iopub.status.busy":"2022-01-12T09:45:04.258974Z","iopub.execute_input":"2022-01-12T09:45:04.259312Z","iopub.status.idle":"2022-01-12T09:45:07.780421Z","shell.execute_reply.started":"2022-01-12T09:45:04.259276Z","shell.execute_reply":"2022-01-12T09:45:07.779403Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#datagen=ImageDataGenerator(rotation_range=40,width_shift_range=0.2,height_shift_range=0.2,shear_range=0.2,zoom_range=0.2,horizontal_flip=True,fill_mode='nearest')","metadata":{"execution":{"iopub.status.busy":"2022-01-11T12:14:30.923734Z","iopub.execute_input":"2022-01-11T12:14:30.924198Z","iopub.status.idle":"2022-01-11T12:14:30.929877Z","shell.execute_reply.started":"2022-01-11T12:14:30.92416Z","shell.execute_reply":"2022-01-11T12:14:30.928665Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Learning Rate Schedule for Fine Tuning #\n# Using an LR ramp up because fine-tuning a pre-trained model.\n# Starting with a high LR would break the pre-trained weights.\n\nLR_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\nLR_MULTIPLIER = 1.0\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 * LR_MULTIPLIER\n\nlr_callback = tf.keras.callbacks.LearningRateScheduler(lrfn, verbose=True)\n\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":"2022-01-12T10:00:37.493197Z","iopub.execute_input":"2022-01-12T10:00:37.494036Z","iopub.status.idle":"2022-01-12T10:00:37.684511Z","shell.execute_reply.started":"2022-01-12T10:00:37.493969Z","shell.execute_reply":"2022-01-12T10:00:37.683631Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install -U efficientnet\nimport efficientnet.tfkeras as efn","metadata":{"execution":{"iopub.status.busy":"2022-01-12T10:00:42.441454Z","iopub.execute_input":"2022-01-12T10:00:42.442315Z","iopub.status.idle":"2022-01-12T10:00:50.755218Z","shell.execute_reply.started":"2022-01-12T10:00:42.442231Z","shell.execute_reply":"2022-01-12T10:00:50.754347Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# with strategy.scope():\n#     pretrained_model2 = tf.keras.applications.DenseNet201(weights='imagenet', include_top=False ,input_shape=[*IMAGE_SIZE, 3])\n#     pretrained_model2.trainable=True\n    \n#     model2 = tf.keras.Sequential([\n#         pretrained_model2,\n#         tf.keras.layers.GlobalAveragePooling2D(),\n#         tf.keras.layers.Dense(len(CLASSES), activation='softmax')\n#     ])\n        \n# model2.compile(\n#     optimizer=tf.keras.optimizers.Adam(),\n#     loss = 'sparse_categorical_crossentropy',\n#     metrics=['sparse_categorical_accuracy']\n# )\n# model2.summary()","metadata":{"id":"mXAv5zyhbQUd","execution":{"iopub.status.busy":"2022-01-11T12:14:46.577161Z","iopub.execute_input":"2022-01-11T12:14:46.577431Z","iopub.status.idle":"2022-01-11T12:15:25.54367Z","shell.execute_reply.started":"2022-01-11T12:14:46.577399Z","shell.execute_reply":"2022-01-11T12:15:25.542527Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Training","metadata":{"id":"ZhcO0Hdturgh"}},{"cell_type":"code","source":"new_model = tf.keras.models.load_model(\"../input/fc-tpu-petals-to-the-metal/recent_model_densenet.h5\")","metadata":{"execution":{"iopub.status.busy":"2022-01-12T10:41:34.504236Z","iopub.execute_input":"2022-01-12T10:41:34.504542Z","iopub.status.idle":"2022-01-12T10:41:54.199995Z","shell.execute_reply.started":"2022-01-12T10:41:34.504511Z","shell.execute_reply":"2022-01-12T10:41:54.199029Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# new_history = new_model.fit(\n#     get_training_dataset(),\n#     validation_data= ds_valid,\n#     steps_per_epoch=STEPS_PER_EPOCH,\n#     epochs=5, \n#     callbacks=[lr_callback],\n#         #class_weight= weight_per_class\n# )","metadata":{"execution":{"iopub.status.busy":"2022-01-12T10:43:44.507716Z","iopub.execute_input":"2022-01-12T10:43:44.508637Z","iopub.status.idle":"2022-01-12T10:43:44.513787Z","shell.execute_reply.started":"2022-01-12T10:43:44.508577Z","shell.execute_reply":"2022-01-12T10:43:44.513050Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Save the entire model to a HDF5 file.\n# The '.h5' extension indicates that the model should be saved to HDF5.\n#new_model.save('12_01_2022_model_densenet.h5')","metadata":{"execution":{"iopub.status.busy":"2022-01-11T15:00:00.594456Z","iopub.execute_input":"2022-01-11T15:00:00.594823Z","iopub.status.idle":"2022-01-11T15:00:10.324173Z","shell.execute_reply.started":"2022-01-11T15:00:00.594788Z","shell.execute_reply":"2022-01-11T15:00:10.323238Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Evaluate Prediction","metadata":{"id":"yPiOoF43wvOz"}},{"cell_type":"markdown","source":"Confusion Matrix","metadata":{"id":"ZtebDhQkxBSK"}},{"cell_type":"code","source":"cmdataset = get_validation_dataset(ordered=True)\nimages_ds = cmdataset.map(lambda image, label: image)\nlabels_ds = cmdataset.map(lambda image, label: label).unbatch()\n\ncm_correct_labels = next(iter(labels_ds.batch(NUM_VALIDATION_IMAGES))).numpy()\ncm_probabilities = new_model.predict(images_ds)\ncm_predictions = np.argmax(cm_probabilities, axis=-1)\n\nlabels = range(len(CLASSES))\ncmat = confusion_matrix(\n    cm_correct_labels,\n    cm_predictions,\n    labels=labels,\n)\ncmat = (cmat.T / cmat.sum(axis=1)).T # normalize\nprint(\"Correct Labels :\",cm_correct_labels)\nprint(\"Prediction :\",cm_predictions)\n","metadata":{"id":"5vL-x2JzxDKM","outputId":"3c007140-31a6-4f37-f5c1-273c9909fc69","execution":{"iopub.status.busy":"2022-01-12T10:43:59.336661Z","iopub.execute_input":"2022-01-12T10:43:59.336965Z","iopub.status.idle":"2022-01-12T10:48:09.453378Z","shell.execute_reply.started":"2022-01-12T10:43:59.336936Z","shell.execute_reply":"2022-01-12T10:48:09.452443Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"score = f1_score(\n    cm_correct_labels,\n    cm_predictions,\n    labels=labels,\n    average='macro',\n)\nprecision = precision_score(\n    cm_correct_labels,\n    cm_predictions,\n    labels=labels,\n    average='macro',\n)\nrecall = recall_score(\n    cm_correct_labels,\n    cm_predictions,\n    labels=labels,\n    average='macro',\n)\ndisplay_confusion_matrix(cmat, score, precision, recall)","metadata":{"id":"FPYZ6LotxKSO","outputId":"4c07add3-2b31-41e7-fe07-b1aa8a5ec8a9","execution":{"iopub.status.busy":"2022-01-12T10:48:46.332344Z","iopub.execute_input":"2022-01-12T10:48:46.332636Z","iopub.status.idle":"2022-01-12T10:48:51.546889Z","shell.execute_reply.started":"2022-01-12T10:48:46.332601Z","shell.execute_reply":"2022-01-12T10:48:51.545998Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# !pip install pycm\n# from pycm import *\n# cm = ConfusionMatrix(actual_vector=cm_correct_labels, predict_vector=cm_predictions)\n# cm.save_csv(\"DenseNet Confusion matrix\")\n","metadata":{"execution":{"iopub.status.busy":"2022-01-04T18:46:14.813595Z","iopub.execute_input":"2022-01-04T18:46:14.813851Z","iopub.status.idle":"2022-01-04T18:46:14.817409Z","shell.execute_reply.started":"2022-01-04T18:46:14.813821Z","shell.execute_reply":"2022-01-04T18:46:14.816853Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset = get_validation_dataset()\ndataset = dataset.unbatch().batch(20)\nbatch = iter(dataset)","metadata":{"id":"KjW3s48C5zZD","execution":{"iopub.status.busy":"2022-01-12T10:49:04.572185Z","iopub.execute_input":"2022-01-12T10:49:04.572493Z","iopub.status.idle":"2022-01-12T10:49:04.614230Z","shell.execute_reply.started":"2022-01-12T10:49:04.572465Z","shell.execute_reply":"2022-01-12T10:49:04.613271Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"images, labels = next(batch)\nprobabilities = new_model.predict(images)\npredictions = np.argmax(probabilities, axis=-1)\ndisplay_batch_of_images((images, labels), predictions)","metadata":{"id":"ASkmKebF51qq","outputId":"95061963-55cc-4a2c-8c2c-6088557d6655","execution":{"iopub.status.busy":"2022-01-12T10:49:07.589356Z","iopub.execute_input":"2022-01-12T10:49:07.589671Z","iopub.status.idle":"2022-01-12T10:49:13.398553Z","shell.execute_reply.started":"2022-01-12T10:49:07.589629Z","shell.execute_reply":"2022-01-12T10:49:13.397466Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_ds = get_test_dataset(ordered=True)\n\nprint('Computing predictions...')\ntest_images_ds = test_ds.map(lambda image, idnum: image)\nprobabilities = new_model.predict(test_images_ds)\npredictions = np.argmax(probabilities, axis=-1)\nprint(predictions)","metadata":{"id":"NWHHqb0G6GYQ","execution":{"iopub.status.busy":"2022-01-12T10:49:17.893003Z","iopub.execute_input":"2022-01-12T10:49:17.893331Z","iopub.status.idle":"2022-01-12T10:57:26.091296Z","shell.execute_reply.started":"2022-01-12T10:49:17.893301Z","shell.execute_reply":"2022-01-12T10:57:26.090185Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('Generating submission.csv file...')\n\n# Get image ids from test set and convert to unicode\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')\n\n# Write the submission file\nnp.savetxt(\n    'submission.csv',\n    np.rec.fromarrays([test_ids, predictions]),\n    fmt=['%s', '%d'],\n    delimiter=',',\n    header='id,label',\n    comments='',\n)\n# Look at the first few predictions\n!head submission.csv","metadata":{"execution":{"iopub.status.busy":"2022-01-12T11:05:44.521934Z","iopub.execute_input":"2022-01-12T11:05:44.523093Z","iopub.status.idle":"2022-01-12T11:05:48.443671Z","shell.execute_reply.started":"2022-01-12T11:05:44.523040Z","shell.execute_reply":"2022-01-12T11:05:48.442647Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}