{"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 glob\n\nimport matplotlib.pyplot as plt\nimport tensorflow as tf\nimport numpy as np \nimport pandas as pd \n\nfrom tensorflow.keras.callbacks import ModelCheckpoint, EarlyStopping\nfrom tensorflow.keras import layers\nfrom tensorflow.keras.layers import AveragePooling2D, GlobalAveragePooling2D, GlobalMaxPooling2D\nfrom tensorflow.keras.optimizers import Adam\nfrom tensorflow.keras.layers import Dropout\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.applications import InceptionV3, DenseNet201, ResNet50, EfficientNetB6\nfrom tensorflow.keras.applications.densenet import DenseNet121\nfrom tensorflow.keras.layers import Input, concatenate\nfrom tensorflow.keras.layers import Flatten, Concatenate\nfrom tensorflow.keras.layers import Dense\nfrom tensorflow.keras.models import Model\nimport os\n","metadata":{"execution":{"iopub.status.busy":"2022-04-02T17:41:23.731086Z","iopub.execute_input":"2022-04-02T17:41:23.731493Z","iopub.status.idle":"2022-04-02T17:41:31.768574Z","shell.execute_reply.started":"2022-04-02T17:41:23.731396Z","shell.execute_reply":"2022-04-02T17:41:31.767091Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"batch_size = 256\nIMAGE_SIZE = [512, 512]\nnum_epochs = 80","metadata":{"execution":{"iopub.status.busy":"2022-04-02T17:41:31.770944Z","iopub.execute_input":"2022-04-02T17:41:31.771353Z","iopub.status.idle":"2022-04-02T17:41:31.779097Z","shell.execute_reply.started":"2022-04-02T17:41:31.771317Z","shell.execute_reply":"2022-04-02T17:41:31.777652Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Set distribution strategy to use TPUs\nresolver = tf.distribute.cluster_resolver.TPUClusterResolver()  # TPU detection\nprint('Found connected TPU: ', resolver.cluster_spec().as_dict()['worker'])\n\ntf.config.experimental_connect_to_cluster(resolver)\ntf.tpu.experimental.initialize_tpu_system(resolver)\nstrategy = tf.distribute.TPUStrategy(resolver)","metadata":{"execution":{"iopub.status.busy":"2022-04-02T17:41:31.780483Z","iopub.execute_input":"2022-04-02T17:41:31.780776Z","iopub.status.idle":"2022-04-02T17:41:37.758299Z","shell.execute_reply.started":"2022-04-02T17:41:31.780743Z","shell.execute_reply":"2022-04-02T17:41:37.756996Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"AUTO = tf.data.experimental.AUTOTUNE","metadata":{"execution":{"iopub.status.busy":"2022-04-02T17:41:37.760366Z","iopub.execute_input":"2022-04-02T17:41:37.761194Z","iopub.status.idle":"2022-04-02T17:41:37.766551Z","shell.execute_reply.started":"2022-04-02T17:41:37.761148Z","shell.execute_reply":"2022-04-02T17:41:37.765638Z"},"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) ","metadata":{"execution":{"iopub.status.busy":"2022-04-02T17:41:37.767776Z","iopub.execute_input":"2022-04-02T17:41:37.768669Z","iopub.status.idle":"2022-04-02T17:41:38.229526Z","shell.execute_reply.started":"2022-04-02T17:41:37.768629Z","shell.execute_reply":"2022-04-02T17:41:38.228326Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_files = tf.io.gfile.glob(f'{GCS_DS_PATH}/tfrecords-jpeg-512x512/train/*')\nval_files = tf.io.gfile.glob(f'{GCS_DS_PATH}/tfrecords-jpeg-512x512/val/*')\ntest_files = tf.io.gfile.glob(f'{GCS_DS_PATH}/tfrecords-jpeg-512x512/test/*')","metadata":{"execution":{"iopub.status.busy":"2022-04-02T17:41:38.231270Z","iopub.execute_input":"2022-04-02T17:41:38.232358Z","iopub.status.idle":"2022-04-02T17:41:38.490284Z","shell.execute_reply.started":"2022-04-02T17:41:38.232303Z","shell.execute_reply":"2022-04-02T17:41:38.489205Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_num_samples(file_list):\n    count = 0 \n    for file_name in file_list:\n        num_sample = int(file_name.split('.tfrec')[0].rsplit('-', 1)[1])\n        count += num_sample\n    return count","metadata":{"execution":{"iopub.status.busy":"2022-04-02T17:41:38.493901Z","iopub.execute_input":"2022-04-02T17:41:38.494385Z","iopub.status.idle":"2022-04-02T17:41:38.501393Z","shell.execute_reply.started":"2022-04-02T17:41:38.494327Z","shell.execute_reply":"2022-04-02T17:41:38.500588Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_size = get_num_samples(train_files)\nval_size = get_num_samples(val_files)\ntest_size = get_num_samples(test_files)\n\nprint(f\"Train dataset size: {train_size}\")\nprint(f\"Validation dataset size: {val_size}\")\nprint(f\"Test dataset size: {test_size}\")","metadata":{"execution":{"iopub.status.busy":"2022-04-02T17:41:38.502483Z","iopub.execute_input":"2022-04-02T17:41:38.503003Z","iopub.status.idle":"2022-04-02T17:41:38.515772Z","shell.execute_reply.started":"2022-04-02T17:41:38.502964Z","shell.execute_reply":"2022-04-02T17:41:38.514215Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Functions and 'classes' variable in this cell were taken from https://www.kaggle.com/code/ryanholbrook/create-your-first-submission/notebook\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\ndef data_augment(image, label):\n    # Thanks to the dataset.prefetch(AUTO)\n    # statement in the next function (below), this happens essentially\n    # for free on TPU. Data pipeline code is executed on the \"CPU\"\n    # part of the TPU while the TPU itself is computing gradients.\n    image = tf.image.random_flip_left_right(image)\n    #image = tf.image.random_saturation(image, 0, 2)\n    return image, label   \n\ndef 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)","metadata":{"execution":{"iopub.status.busy":"2022-04-02T17:41:38.517902Z","iopub.execute_input":"2022-04-02T17:41:38.518587Z","iopub.status.idle":"2022-04-02T17:41:38.539365Z","shell.execute_reply.started":"2022-04-02T17:41:38.518505Z","shell.execute_reply":"2022-04-02T17:41:38.538173Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dataset = tf.data.TFRecordDataset(train_files, num_parallel_reads=AUTO)\ntest_dataset = tf.data.TFRecordDataset(test_files, num_parallel_reads=AUTO)\nval_dataset = tf.data.TFRecordDataset(val_files, num_parallel_reads=AUTO)\n\ntrain_dataset = train_dataset.map(read_labeled_tfrecord)\ntrain_dataset = train_dataset.map(data_augment, num_parallel_calls=AUTO)\ntrain_dataset = train_dataset.shuffle(buffer_size=batch_size)\ntrain_dataset = train_dataset.repeat()\ntrain_dataset = train_dataset.batch(batch_size=batch_size)\ntrain_dataset = train_dataset.prefetch(buffer_size=batch_size)\n\nval_dataset = val_dataset.map(read_labeled_tfrecord)\nval_dataset = val_dataset.batch(batch_size=batch_size)\nval_dataset = val_dataset.prefetch(buffer_size=batch_size)\n\ntest_dataset = test_dataset.map(read_unlabeled_tfrecord)\ntest_dataset = test_dataset.prefetch(buffer_size=batch_size)","metadata":{"execution":{"iopub.status.busy":"2022-04-02T17:41:38.543930Z","iopub.execute_input":"2022-04-02T17:41:38.544314Z","iopub.status.idle":"2022-04-02T17:41:38.934629Z","shell.execute_reply.started":"2022-04-02T17:41:38.544270Z","shell.execute_reply":"2022-04-02T17:41:38.933564Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def create_densenet201_model():\n    base_model = DenseNet201(weights=\"imagenet\", include_top=False, input_tensor=Input(shape=(512, 512, 3)))\n    head_model = base_model.output\n    head_model = GlobalAveragePooling2D()(head_model)\n    head_model = Flatten(name=\"flatten\")(head_model)\n#     head_model = Dense(4096, activation=\"relu\")(head_model)\n#     head_model = Dropout(0.4)(head_model)\n    head_model = Dense(len(CLASSES), activation=\"softmax\")(head_model)\n\n    # Place the head FC model on top of the base model (this will become the actual model we will train)\n    model = Model(inputs=base_model.input, outputs=head_model)\n    for layer in base_model.layers:\n        layer.trainable = True\n#     opt = Adam(learning_rate=0.0001)\n    model.compile(optimizer='adam',\n                  loss='sparse_categorical_crossentropy',\n                  metrics=['sparse_categorical_accuracy'])\n    model.summary()\n    return model","metadata":{"execution":{"iopub.status.busy":"2022-04-02T17:41:38.936513Z","iopub.execute_input":"2022-04-02T17:41:38.936932Z","iopub.status.idle":"2022-04-02T17:41:38.947961Z","shell.execute_reply.started":"2022-04-02T17:41:38.936884Z","shell.execute_reply":"2022-04-02T17:41:38.946328Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# def desenet121_model():\n#     base_model = EfficientNetB6(weights=\"imagenet\", include_top=False, input_tensor=Input(shape=(512, 512, 3)))\n#     head_model = base_model.output\n#     head_model = GlobalAveragePooling2D()(head_model)\n#     head_model = Flatten(name=\"flatten\")(head_model)\n# #     head_model = Dense(2048, activation=\"relu\")(head_model)\n# #     head_model = Dropout(0.5)(head_model)\n#     head_model = Dense(len(CLASSES), activation=\"softmax\")(head_model)\n\n#     # Place the head FC model on top of the base model (this will become the actual model we will train)\n#     model = Model(inputs=base_model.input, outputs=head_model)\n#     for layer in base_model.layers:\n#         layer.trainable = True\n# #     opt = Adam(learning_rate=0.0001)\n#     model.compile(optimizer='adam',\n#                   loss='sparse_categorical_crossentropy',\n#                   metrics=['sparse_categorical_accuracy'])\n#     model.summary()\n#     return model","metadata":{"execution":{"iopub.status.busy":"2022-04-02T17:41:38.949819Z","iopub.execute_input":"2022-04-02T17:41:38.950122Z","iopub.status.idle":"2022-04-02T17:41:38.968945Z","shell.execute_reply.started":"2022-04-02T17:41:38.950091Z","shell.execute_reply":"2022-04-02T17:41:38.967245Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# def nerual_network_model():\n#     base_model = DenseNet201(weights=\"imagenet\", include_top=False, input_tensor=Input(shape=(512, 512, 3)))\n#     head_model = base_model.output\n#     head_model = GlobalAveragePooling2D()(head_model)\n#     head_model = Flatten(name=\"flatten1\")(head_model)\n#     head_model = Dense(4096, activation=\"relu\")(head_model)\n#     head_model = Dropout(0.5)(head_model)\n#     head_model = Dense(len(CLASSES), activation=\"softmax\")(head_model)\n#     model_1 = Model(inputs=base_model.input, outputs=head_model)\n\n\n#     base_model = InceptionV3(weights=\"imagenet\", include_top=False, input_tensor=Input(shape=(512, 512, 3)))\n#     head_model = base_model.output\n#     head_model = GlobalAveragePooling2D()(head_model)\n#     head_model = Flatten(name=\"flatten2\")(head_model)\n#     head_model = Dense(4096, activation=\"relu\")(head_model)\n#     head_model = Dropout(0.5)(head_model)\n#     head_model = Dense(len(CLASSES), activation=\"softmax\")(head_model)\n#     model_2 = Model(inputs=base_model.input, outputs=head_model)\n    \n#     x = Concatenate()([model_1.output, model_2.output])\n#     x = Dense(len(CLASSES), activation='softmax')(x)\n#     model = Model(inputs=[model_1.input,model_2.input],outputs=x)\n# #     model.summary()\n#     opt = Adam(learning_rate=0.0001)\n#     model.compile(optimizer=opt,\n#                   loss='sparse_categorical_crossentropy',\n#                   metrics=['sparse_categorical_accuracy'])\n#     return model","metadata":{"execution":{"iopub.status.busy":"2022-04-02T17:41:38.970293Z","iopub.execute_input":"2022-04-02T17:41:38.970609Z","iopub.status.idle":"2022-04-02T17:41:38.984574Z","shell.execute_reply.started":"2022-04-02T17:41:38.970571Z","shell.execute_reply":"2022-04-02T17:41:38.983410Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# def nerual_network_model():\n#     inception_model =DenseNet121(weights=\"imagenet\", include_top=False, input_shape=(512,512,3))\n#     x = inception_model.output\n#     x = GlobalAveragePooling2D()(x)\n#     x = Flatten()(x)\n# #     x = Dense(4096, activation='relu')(x)\n# #     x = Dropout(0.5)(x)\n# #     predictions=Dense(len(CLASSES), activation='softmax')(x)\n    \n#     #model_1 = Model(inputs=vgg16_model.input, outputs=predictions)\n#     model_1 = Model(inputs=inception_model.input, outputs=x)\n#     for layer in inception_model.layers:\n#         layer.trainable = False\n        \n#     densenet201_model = DenseNet201(weights=\"imagenet\", include_top=False, input_shape=(512,512,3))#Edited here.Using same model causes name conflicts of layers.Every layer names should be unique.\n#     x = densenet201_model.output\n#     #resnet50_model =ResNet50(weights=\"imagenet\", include_top=False, input_shape=(224,224,3))\n#     #x = resnet50_model.output\n#     x = GlobalAveragePooling2D()(x)\n#     x = Flatten()(x)\n# #     x = Dense(4096, activation='relu')(x)\n# #     x = Dropout(0.5)(x)\n# #     predictions=Dense(len(CLASSES), activation='softmax')(x)\n\n#     model_2 = Model(inputs=densenet201_model.input, outputs=x)\n#     for layer in densenet201_model.layers:\n#         layer.trainable = False\n#     #model_2 = Model(inputs=resnet50_model.input, outputs=predictions)\n#     combined_model_input = Input(shape = (512,512,3), name = \"combined_model_input\")\n#     m1_predict = model_1(combined_model_input)\n#     m2_predict = model_2(combined_model_input)\n#     combined = Concatenate()([m1_predict, m2_predict])\n# #     fc = Dense(4096, activation='relu',name = \"fc1\")(combined)\n# #     fc = Dense(4096, activation='relu',name = \"fc2\")(combined)\n#     output_layer = Dense(len(CLASSES), activation='softmax',name = \"fc3\")(combined)\n#     model = Model(combined_model_input, output_layer)\n# #     model = Model(inputs=([model_1.input, model_2.input]), outputs=merged)\n# #     opt = Adam(learning_rate=0.0001)\n#     model.compile(optimizer='adam',\n#                       loss='sparse_categorical_crossentropy',\n#                       metrics=['sparse_categorical_accuracy'])\n#     model.summary()\n#     tf.keras.utils.plot_model(\n#     model)\n#     return model","metadata":{"execution":{"iopub.status.busy":"2022-04-02T17:41:38.986322Z","iopub.execute_input":"2022-04-02T17:41:38.986694Z","iopub.status.idle":"2022-04-02T17:41:39.005301Z","shell.execute_reply.started":"2022-04-02T17:41:38.986649Z","shell.execute_reply":"2022-04-02T17:41:39.004325Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# tf.keras.utils.plot_model(\n#     model)","metadata":{"execution":{"iopub.status.busy":"2022-04-02T17:41:39.006811Z","iopub.execute_input":"2022-04-02T17:41:39.007264Z","iopub.status.idle":"2022-04-02T17:41:39.024206Z","shell.execute_reply.started":"2022-04-02T17:41:39.007193Z","shell.execute_reply":"2022-04-02T17:41:39.022665Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cp_callback = ModelCheckpoint(filepath='flower_model1.hdf5',\n                              monitor='val_sparse_categorical_accuracy',\n                              save_freq='epoch', verbose=1, period=1,\n                              save_best_only=True, save_weights_only=True)\n\nearly_stopping = EarlyStopping(monitor='val_sparse_categorical_accuracy',\n                               verbose=1, patience=5)","metadata":{"execution":{"iopub.status.busy":"2022-04-02T17:41:39.026553Z","iopub.execute_input":"2022-04-02T17:41:39.027383Z","iopub.status.idle":"2022-04-02T17:41:39.040175Z","shell.execute_reply.started":"2022-04-02T17:41:39.027331Z","shell.execute_reply":"2022-04-02T17:41:39.038973Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from datetime import datetime, timedelta\n\nstart_time = datetime.now()\nprint('Time now is', start_time)\nend_training_by_tdelta = timedelta(seconds=8400)\nthis_run_file_prefix = start_time.strftime('%Y%m%d_%H%M_')\n\n\n# EPOCHS = 25\n# STEPS_PER_EPOCH = NUM_TRAINING_IMAGES // BATCH_SIZE\n\n\n\n# Learning Rate Schedule for Fine Tuning #\n# Learning rate schedule for TPU, GPU and CPU.\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\ndef lrfn(epoch):\n    if epoch < LR_RAMPUP_EPOCHS:\n        lr = LR_START + (epoch * (LR_MAX - LR_START) / LR_RAMPUP_EPOCHS)\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)\nrng = [i for i in range(num_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]))\n\n","metadata":{"execution":{"iopub.status.busy":"2022-04-02T17:41:39.043210Z","iopub.execute_input":"2022-04-02T17:41:39.043866Z","iopub.status.idle":"2022-04-02T17:41:39.382828Z","shell.execute_reply.started":"2022-04-02T17:41:39.043680Z","shell.execute_reply":"2022-04-02T17:41:39.382041Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"with strategy.scope():\n    batch_size = batch_size * strategy.num_replicas_in_sync\n    steps_per_epoch = (12753 / 256)*2\n#     model = nerual_network_model()\n#     model = desenet121_model()\n    model = create_densenet201_model()\n#     model = create_resnet50_model()\n    history = model.fit(\n                train_dataset, \n                validation_data=val_dataset,\n                epochs=num_epochs,\n                steps_per_epoch=steps_per_epoch,\n#                 validation_steps= (3712 / 256)*2,\n                callbacks=[cp_callback, early_stopping, lr_callback])","metadata":{"execution":{"iopub.status.busy":"2022-04-02T17:41:39.383977Z","iopub.execute_input":"2022-04-02T17:41:39.384553Z","iopub.status.idle":"2022-04-02T18:20:40.509304Z","shell.execute_reply.started":"2022-04-02T17:41:39.384491Z","shell.execute_reply":"2022-04-02T18:20:40.507955Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(history.history['loss'], label='train_loss') \nplt.plot(history.history['val_loss'], label='val_loss')\nplt.legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-04-02T18:20:40.511048Z","iopub.execute_input":"2022-04-02T18:20:40.511394Z","iopub.status.idle":"2022-04-02T18:20:40.720046Z","shell.execute_reply.started":"2022-04-02T18:20:40.511351Z","shell.execute_reply":"2022-04-02T18:20:40.719015Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(history.history['sparse_categorical_accuracy'], label='train_accuracy')\nplt.plot(history.history['val_sparse_categorical_accuracy'], label='val_sparse_categorical_accuracy')\nplt.legend() \nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-04-02T18:20:40.721632Z","iopub.execute_input":"2022-04-02T18:20:40.722484Z","iopub.status.idle":"2022-04-02T18:20:40.944958Z","shell.execute_reply.started":"2022-04-02T18:20:40.722413Z","shell.execute_reply":"2022-04-02T18:20:40.943932Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = create_densenet201_model()\nmodel.load_weights('flower_model1.hdf5')","metadata":{"execution":{"iopub.status.busy":"2022-04-02T18:20:40.946187Z","iopub.execute_input":"2022-04-02T18:20:40.946800Z","iopub.status.idle":"2022-04-02T18:20:58.712321Z","shell.execute_reply.started":"2022-04-02T18:20:40.946758Z","shell.execute_reply":"2022-04-02T18:20:58.710728Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"results = {'id': [], 'label': []}","metadata":{"execution":{"iopub.status.busy":"2022-04-02T18:20:58.715402Z","iopub.execute_input":"2022-04-02T18:20:58.715834Z","iopub.status.idle":"2022-04-02T18:20:58.722253Z","shell.execute_reply.started":"2022-04-02T18:20:58.715789Z","shell.execute_reply":"2022-04-02T18:20:58.720902Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def predict(element):\n    image = element[0]\n    id_ = tf.keras.backend.get_value(element[1]).decode(\"utf-8\")\n    result = list(model.predict(np.array([image]))[0])\n    max_pred = max(result)\n    result = result.index(max_pred)\n    results['id'].append(id_)\n    results['label'].append(result) ","metadata":{"execution":{"iopub.status.busy":"2022-04-02T18:20:58.724048Z","iopub.execute_input":"2022-04-02T18:20:58.724358Z","iopub.status.idle":"2022-04-02T18:20:58.738143Z","shell.execute_reply.started":"2022-04-02T18:20:58.724326Z","shell.execute_reply":"2022-04-02T18:20:58.736858Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"count = 0\nfor row in test_dataset: \n    image = row[0]\n    id_ = tf.keras.backend.get_value(row[1]).decode(\"utf-8\")\n    result = list(model.predict(np.array([image]))[0])\n    max_pred = max(result) \n    result = result.index(max_pred) \n    results['id'].append(id_)\n    results['label'].append(result)\n    count += 1 \n    if (count % 500) == 0:\n        print(f\"Finished predicting {count} images\") ","metadata":{"execution":{"iopub.status.busy":"2022-04-02T18:20:58.739717Z","iopub.execute_input":"2022-04-02T18:20:58.740022Z","iopub.status.idle":"2022-04-02T18:51:40.103853Z","shell.execute_reply.started":"2022-04-02T18:20:58.739980Z","shell.execute_reply":"2022-04-02T18:51:40.101676Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"results_df = pd.DataFrame(results)\nresults_df.to_csv('submission.csv', index=False)\n","metadata":{"execution":{"iopub.status.busy":"2022-04-02T18:51:40.105741Z","iopub.execute_input":"2022-04-02T18:51:40.108100Z","iopub.status.idle":"2022-04-02T18:51:40.161458Z","shell.execute_reply.started":"2022-04-02T18:51:40.108017Z","shell.execute_reply":"2022-04-02T18:51:40.159652Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"results_df","metadata":{"execution":{"iopub.status.busy":"2022-04-02T18:51:40.163359Z","iopub.execute_input":"2022-04-02T18:51:40.163935Z","iopub.status.idle":"2022-04-02T18:51:40.196260Z","shell.execute_reply.started":"2022-04-02T18:51:40.163892Z","shell.execute_reply":"2022-04-02T18:51:40.195003Z"},"trusted":true},"execution_count":null,"outputs":[]}]}