{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# Data preparation for models training #","metadata":{"id":"0x0vaE6ojYto"}},{"cell_type":"code","source":"from google.colab import drive\ndrive.mount('/content/drive')","metadata":{"id":"MWhPjrtYg6rr","outputId":"4f13cf3f-bdae-4c32-965d-fcfc40fe4309","execution":{"iopub.status.busy":"2022-01-12T16:13:39.141923Z","iopub.execute_input":"2022-01-12T16:13:39.142437Z","iopub.status.idle":"2022-01-12T16:13:39.16133Z","shell.execute_reply.started":"2022-01-12T16:13:39.142405Z","shell.execute_reply":"2022-01-12T16:13:39.160299Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# RUN THIS\nimport os\nimport shutil\nimport numpy as np\nimport zipfile\nimport math\nfrom functools import partial\nimport tensorflow as tf\nimport random","metadata":{"id":"C8Qe2A_1UGG3","execution":{"iopub.status.busy":"2022-01-12T17:24:23.044381Z","iopub.execute_input":"2022-01-12T17:24:23.045023Z","iopub.status.idle":"2022-01-12T17:24:27.079828Z","shell.execute_reply.started":"2022-01-12T17:24:23.044884Z","shell.execute_reply":"2022-01-12T17:24:27.07908Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install --upgrade --force-reinstall --no-deps kaggle","metadata":{"id":"9LgxddaDUWu-"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"! mkdir ~/.kaggle","metadata":{"id":"YohtBc6oXIcX"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"! cp kaggle.json ~/.kaggle/","metadata":{"id":"V1NWKflvU9j0"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"! chmod 600 ~/.kaggle/kaggle.json","metadata":{"id":"aNd4RjK4VkQw"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"! kaggle competitions download tpu-getting-started","metadata":{"id":"84qDUiD5Vw4_"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"with zipfile.ZipFile(\"/content/tpu-getting-started.zip\", 'r') as zip_ref:\n    zip_ref.extractall(\"/content\")","metadata":{"id":"EN5EUK1mVyjn"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# RUN THIS\n\nos.mkdir(\"flower_classification\")\nos.mkdir(\"flower_classification/data_before_split\")\nos.mkdir(\"flower_classification/training\")\nos.mkdir(\"flower_classification/validation\")\nos.mkdir(\"flower_classification/test\")","metadata":{"id":"PvwrUjOfe0mp","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# RUN THIS\n\ntemp_classes = ['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']\n\nclasses = {}\n\nfor i in range( len( temp_classes  ) ):\n  classes[i] = temp_classes[i]\n  ","metadata":{"id":"h38C2yqTAV-_","execution":{"iopub.status.busy":"2022-01-12T18:12:09.150029Z","iopub.execute_input":"2022-01-12T18:12:09.150329Z","iopub.status.idle":"2022-01-12T18:12:09.163291Z","shell.execute_reply.started":"2022-01-12T18:12:09.150294Z","shell.execute_reply":"2022-01-12T18:12:09.162459Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# RUN THIS AFTER RUNNING THE NEXT TWO CELLS\n\n\ndatasets = [ \"../input/tpu-getting-started/tfrecords-jpeg-192x192\" ,\n            \"../input/tpu-getting-started/tfrecords-jpeg-224x224\" ,\n            \"../input/tpu-getting-started/tfrecords-jpeg-331x331\" ,\n            \"../input/tpu-getting-started/tfrecords-jpeg-512x512\"]\nimage_sizes = [[192,192] , [224,224] , [331,331] , [512,512] ]\nid_labeled = 0\nid_unlabeled = 0 \nfor i in range( len(datasets) ) :\n  labeled_data_file_names = []\n  unlabeled_data_file_names = []\n  for j in os.listdir(datasets[i] + \"/train\"):\n    labeled_data_file_names.append( datasets[i] + \"/train\" + \"/\" + j )\n  for j in os.listdir(datasets[i] + \"/val\"):\n    labeled_data_file_names.append( datasets[i] + \"/val\" + \"/\" + j )\n  for j in os.listdir(datasets[i] + \"/test\"):\n    unlabeled_data_file_names.append( datasets[i] + \"/test\" + \"/\" + j )\n  examples = convert_tfrecords_to_jpeg( labeled_data_file_names )\n  for example in examples:\n    save_image( example , image_sizes[i] , \"./flower_classification/data_before_split/\" , id_labeled )\n    id_labeled += 1\n  examples = convert_tfrecords_to_jpeg( unlabeled_data_file_names , labeled = False )\n  for example in examples:\n    save_image( example , image_sizes[i] , \"./flower_classification/test/\" , id_unlabeled , labeled = False )\n    id_unlabeled += 1","metadata":{"id":"XdjW3dOvkDS_","execution":{"iopub.status.busy":"2022-01-12T17:24:45.348025Z","iopub.execute_input":"2022-01-12T17:24:45.348508Z","iopub.status.idle":"2022-01-12T17:37:55.057075Z","shell.execute_reply.started":"2022-01-12T17:24:45.348457Z","shell.execute_reply":"2022-01-12T17:37:55.056332Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# RUN THIS\n\ndef save_image( example , image_size , save_path , id , labeled = True ):\n  image = tf.image.decode_jpeg( example['image'] , channels=3)\n  image = tf.cast(image, tf.float32)\n  image = tf.reshape(image, [ *image_size  , 3])\n  image = image.numpy()\n  if labeled:\n    if not os.path.isdir( save_path + classes[ example['class'].numpy().squeeze() ] ):\n      os.mkdir( save_path + classes[ example['class'].numpy().squeeze() ] )\n    tf.keras.utils.save_img(\n        save_path + classes [ example['class'].numpy().squeeze() ] + \"/\" + str(id) + '.jpeg'  , image , scale=True\n    )\n  else:\n    tf.keras.utils.save_img(\n        save_path  + str(id) + '.jpeg'  , image , scale=True\n    )","metadata":{"id":"s1Rs195XDPHc","execution":{"iopub.status.busy":"2022-01-12T17:24:37.125537Z","iopub.execute_input":"2022-01-12T17:24:37.126067Z","iopub.status.idle":"2022-01-12T17:24:37.133389Z","shell.execute_reply.started":"2022-01-12T17:24:37.126029Z","shell.execute_reply":"2022-01-12T17:24:37.132527Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# RUN THIS\n\ndef convert_tfrecords_to_jpeg( files_paths , labeled = True ):\n  # Define features\n  read_features = (\n        {\n            \"image\": tf.io.FixedLenFeature([], tf.string),\n            \"class\": tf.io.FixedLenFeature([], tf.int64),\n        }\n        if labeled\n        else {\"image\": tf.io.FixedLenFeature([], tf.string),}\n  )\n  examples = []\n  for path in files_paths:\n    for example in tf.compat.v1.io.tf_record_iterator( path ):\n      example = tf.io.parse_single_example( example , read_features )\n      examples.append(example)\n  return examples","metadata":{"id":"M0y8FdavteJk","execution":{"iopub.status.busy":"2022-01-12T17:38:18.274009Z","iopub.execute_input":"2022-01-12T17:38:18.274267Z","iopub.status.idle":"2022-01-12T17:38:18.281124Z","shell.execute_reply.started":"2022-01-12T17:38:18.27424Z","shell.execute_reply":"2022-01-12T17:38:18.280315Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# RUN THIS\n\n\nfor class_name in os.listdir(\"./flower_classification/data_before_split\"):\n  os.mkdir(\"./flower_classification/training/\" + class_name )\n  os.mkdir(\"./flower_classification/validation/\" + class_name )\n  class_images = os.listdir( \"./flower_classification/data_before_split/\" + class_name )\n  random.shuffle(class_images)\n  training_images = class_images[ : int( 0.9 * len(class_images) ) ]\n  val_images = class_images[ int( 0.9 * len(class_images) ) :  ]\n  for image in training_images:\n    shutil.copyfile( \"./flower_classification/data_before_split/\" + class_name + \"/\" + image ,\n                \"./flower_classification/training/\" + class_name + '/' + image )\n  for image in val_images:\n    shutil.copyfile( \"./flower_classification/data_before_split/\" + class_name + \"/\" + image ,\n                \"./flower_classification/validation/\" + class_name + '/' + image )","metadata":{"id":"y-kPklphPAAO","execution":{"iopub.status.busy":"2022-01-12T17:38:25.485751Z","iopub.execute_input":"2022-01-12T17:38:25.486562Z","iopub.status.idle":"2022-01-12T17:38:35.562916Z","shell.execute_reply.started":"2022-01-12T17:38:25.486516Z","shell.execute_reply":"2022-01-12T17:38:35.562122Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from google.colab import drive\ndrive.flush_and_unmount()","metadata":{"id":"h8A7m3aPUuh6","execution":{"iopub.status.busy":"2022-01-12T16:40:26.517491Z","iopub.execute_input":"2022-01-12T16:40:26.518029Z","iopub.status.idle":"2022-01-12T16:40:26.782579Z","shell.execute_reply.started":"2022-01-12T16:40:26.517991Z","shell.execute_reply":"2022-01-12T16:40:26.781242Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Models part #","metadata":{"id":"AbMPw_eSfOos"}},{"cell_type":"markdown","source":"----------------------------------------------- Setting image data generators ------------------------------------","metadata":{"id":"-PYDa9COj-Bd"}},{"cell_type":"code","source":"!pip install tensorflow_addons==0.15.0","metadata":{"id":"sTA8xTz7NVuA","outputId":"84c4e558-58fe-408c-f816-17854f44c929","execution":{"iopub.status.busy":"2022-01-12T17:38:42.399483Z","iopub.execute_input":"2022-01-12T17:38:42.400159Z","iopub.status.idle":"2022-01-12T17:39:01.147203Z","shell.execute_reply.started":"2022-01-12T17:38:42.400123Z","shell.execute_reply":"2022-01-12T17:39:01.146297Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import math\nimport tensorflow as tf\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nimport pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport random \nimport tensorflow.keras\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Dense, Activation, Dropout, Flatten,Conv2D, MaxPooling2D,BatchNormalization,LayerNormalization\nfrom tensorflow.keras.applications.inception_resnet_v2 import InceptionResNetV2\nfrom keras.models import Model\nfrom keras import optimizers\nimport tensorflow_addons as tfa\nfrom tensorflow.keras.metrics import Accuracy","metadata":{"id":"NOc4AHO4Zzk1","execution":{"iopub.status.busy":"2022-01-12T17:39:02.175758Z","iopub.execute_input":"2022-01-12T17:39:02.178175Z","iopub.status.idle":"2022-01-12T17:39:02.79844Z","shell.execute_reply.started":"2022-01-12T17:39:02.178134Z","shell.execute_reply":"2022-01-12T17:39:02.797724Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_batch_size = 512\nval_batch_size =  512                \nimage_size = ( 227 , 227 )               \n\ntrain_datagen = ImageDataGenerator( rescale = 1./255 ,\n                                   rotation_range = 40 ,\n                                   width_shift_range = 0.2 ,\n                                   height_shift_range = 0.2,\n                                   zoom_range = 0.2,\n                                   fill_mode = 'nearest',\n                                   horizontal_flip = True )\n\nval_datagen = ImageDataGenerator(rescale = 1./255)\n\ntrain_path = \"./flower_classification/training\"\n\nval_path = \"./flower_classification/validation\"\n\ntrain_data = train_datagen.flow_from_directory( train_path ,\n                                               target_size = image_size ,\n                                               class_mode = 'categorical',\n                                               batch_size = train_batch_size)\n\nval_data = val_datagen.flow_from_directory( val_path ,\n                                          target_size = image_size ,\n                                          class_mode = 'categorical',\n                                          batch_size = val_batch_size )","metadata":{"id":"bcOHphq2fhBI","outputId":"b9aa14d6-6729-4675-e42e-f783d1d31b9b","execution":{"iopub.status.busy":"2022-01-12T17:40:47.699297Z","iopub.execute_input":"2022-01-12T17:40:47.699855Z","iopub.status.idle":"2022-01-12T17:40:51.243155Z","shell.execute_reply.started":"2022-01-12T17:40:47.699817Z","shell.execute_reply":"2022-01-12T17:40:51.242434Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow.keras\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Dense, Activation, Dropout, Flatten,Conv2D, MaxPooling2D,BatchNormalization,LayerNormalization\n# here we are using 'BatchNormalization' instead of 'LayerNormalization'\n\nmodel = Sequential()\n\n# 1st Convolutional Layer\nmodel.add(Conv2D(filters=256, input_shape=(227,227,3), kernel_size=(5,5),strides=(4,4), padding='valid'))\nmodel.add(Activation('relu'))\n# Pooling \nmodel.add(MaxPooling2D(pool_size=(5,5), strides=(2,2), padding='valid'))\n# Batch Normalisation before passing it to the next layer\nmodel.add(BatchNormalization())\n\n\n# 2nd Convolutional Layer\nmodel.add(Conv2D(filters=128, kernel_size=(5,5), strides=(1,1), padding='same'))\nmodel.add(Activation('relu'))\n# Pooling\nmodel.add(MaxPooling2D(pool_size=(5,5), strides=(2,2), padding='valid'))\n# Batch Normalisation\nmodel.add(BatchNormalization())\n\n\n# Passing it to a dense layer\nmodel.add(Flatten())\n\n# 1st Dense Layer\nmodel.add(Dense(512, input_shape=(224*224*3,)))\nmodel.add(Activation('relu'))\n\n\n# Add Dropout to prevent overfitting\nmodel.add(Dropout(0.5))\n# Batch Normalisation\nmodel.add(BatchNormalization())\n\n\n# 2nd Dense Layer\nmodel.add(Dense(128))\nmodel.add(Activation('relu'))\n\n# Add Dropout\nmodel.add(Dropout(0.5))\n# Batch Normalisation\nmodel.add(BatchNormalization())\n\n \nmodel.add(Dense(104, Activation('softmax')))\n\nmodel.summary()","metadata":{"id":"xCIM3jvfWSMl","execution":{"iopub.status.busy":"2022-01-12T17:39:10.422451Z","iopub.execute_input":"2022-01-12T17:39:10.423231Z","iopub.status.idle":"2022-01-12T17:39:10.626931Z","shell.execute_reply.started":"2022-01-12T17:39:10.423193Z","shell.execute_reply":"2022-01-12T17:39:10.626169Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.mkdir(\"history\")\naccuracy_checkpoint_filepath1 = './history/acc_weights1.h5'\nfscore_checkpoint_filepath1 = './history/fscore_weights1.h5'\n\naccuracy_callback1 = tf.keras.callbacks.ModelCheckpoint(\n    filepath= accuracy_checkpoint_filepath1 ,\n    save_weights_only=True,\n    monitor='val_acc',\n    mode='max',\n    save_best_only=True)\n\nfscore_callback1 = tf.keras.callbacks.ModelCheckpoint(\n    filepath= fscore_checkpoint_filepath1 ,\n    save_weights_only=True,\n    monitor='val_my_fscore',\n    mode='max',\n    save_best_only=True)","metadata":{"execution":{"iopub.status.busy":"2022-01-12T18:14:01.909643Z","iopub.execute_input":"2022-01-12T18:14:01.910143Z","iopub.status.idle":"2022-01-12T18:14:01.915211Z","shell.execute_reply.started":"2022-01-12T18:14:01.910105Z","shell.execute_reply":"2022-01-12T18:14:01.914556Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow_addons as tfa\nfrom keras.callbacks import CSVLogger\ncsv_logger1 = CSVLogger(\"./history/model_history_log1.csv\", append=True)\nfscore_metric = tfa.metrics.F1Score(num_classes=104,average = 'macro' , name = 'my_fscore')\nadam = tf.keras.optimizers.Adam(learning_rate=0.05)\nmodel.compile(optimizer=adam , loss=\"categorical_crossentropy\" , metrics=[ fscore_metric , \"acc\" ])","metadata":{"execution":{"iopub.status.busy":"2022-01-12T18:14:17.893955Z","iopub.execute_input":"2022-01-12T18:14:17.894216Z","iopub.status.idle":"2022-01-12T18:14:17.912633Z","shell.execute_reply.started":"2022-01-12T18:14:17.894188Z","shell.execute_reply":"2022-01-12T18:14:17.911873Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"accuracy_checkpoint_filepath = '../input/acc-weights/acc_weights.h5'\nmodel.load_weights(accuracy_checkpoint_filepath)","metadata":{"execution":{"iopub.status.busy":"2022-01-12T18:14:19.793084Z","iopub.execute_input":"2022-01-12T18:14:19.793342Z","iopub.status.idle":"2022-01-12T18:14:21.044540Z","shell.execute_reply.started":"2022-01-12T18:14:19.793313Z","shell.execute_reply":"2022-01-12T18:14:21.043653Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history1 = model.fit(\n     x=train_data, epochs=10, callbacks = [ fscore_callback1 , accuracy_callback1, csv_logger1 ], validation_data=val_data)","metadata":{"execution":{"iopub.status.busy":"2022-01-12T18:14:21.835060Z","iopub.execute_input":"2022-01-12T18:14:21.835319Z","iopub.status.idle":"2022-01-12T20:18:34.458134Z","shell.execute_reply.started":"2022-01-12T18:14:21.835290Z","shell.execute_reply":"2022-01-12T20:18:34.457401Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\nhist = pd.read_csv('./history/model_history_log1.csv')\nprint(hist)","metadata":{"execution":{"iopub.status.busy":"2022-01-12T20:18:54.878984Z","iopub.execute_input":"2022-01-12T20:18:54.879303Z","iopub.status.idle":"2022-01-12T20:18:54.920386Z","shell.execute_reply.started":"2022-01-12T20:18:54.879267Z","shell.execute_reply":"2022-01-12T20:18:54.919721Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pylab as plt\n\nacc = hist['acc']\nepoch = hist['epoch']\n\nplt.plot(epoch, acc, 'r')\nvalacc = hist['val_acc']\n\nplt.plot(epoch, valacc, 'b')\nplt.xlabel('epoch')\nplt.legend([\"Training Acc\", \"Val acc\"])\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-01-12T20:18:56.183348Z","iopub.execute_input":"2022-01-12T20:18:56.183624Z","iopub.status.idle":"2022-01-12T20:18:56.398181Z","shell.execute_reply.started":"2022-01-12T20:18:56.183593Z","shell.execute_reply":"2022-01-12T20:18:56.397528Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fscore = hist['my_fscore']\n\nplt.plot(epoch, fscore)\nplt.xlabel('epoch')\nplt.ylabel('fscore')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-01-12T20:19:04.549476Z","iopub.execute_input":"2022-01-12T20:19:04.550168Z","iopub.status.idle":"2022-01-12T20:19:04.726127Z","shell.execute_reply.started":"2022-01-12T20:19:04.550130Z","shell.execute_reply":"2022-01-12T20:19:04.725419Z"},"trusted":true},"execution_count":null,"outputs":[]}]}