{"cells":[{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"# !pip install gcsfs\n\nfrom glob import glob\nimport math, os, time, re, json, shutil, pprint, random\nimport tensorflow as tf\nimport numpy as np\nfrom matplotlib import pyplot as plt\nfrom kaggle_datasets import KaggleDatasets\n\nprint(\"Tensorflow version \" + tf.__version__)\nAUTO = tf.data.experimental.AUTOTUNE\n\nfrom tensorflow.keras.models import Model\nfrom tensorflow.keras.layers import Conv2D, Dropout, BatchNormalization, Input, SeparableConv2D, GlobalAveragePooling2D, Dense, MaxPooling2D, Activation, Flatten\nfrom tensorflow.keras.layers import add as add_concat\nfrom tensorflow.keras.callbacks import ModelCheckpoint, LearningRateScheduler\nfrom tensorflow.keras.optimizers import Adam\nfrom tensorflow.keras import backend as K\n\nfrom IPython.display import clear_output\nimport IPython.display as display\n\nimport warnings\nwarnings.filterwarnings(\"ignore\")\nnp.random.seed(0)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"try:\n    tpu = tf.distribute.cluster_resolver.TPUClusterResolver() \n    print('Running on TPU ', tpu.master())\nexcept ValueError:\n    tpu = None\n\nif tpu:\n    tf.config.experimental_connect_to_cluster(tpu)\n    tf.tpu.experimental.initialize_tpu_system(tpu)\n    strategy = tf.distribute.experimental.TPUStrategy(tpu)\nelse:\n    strategy = tf.distribute.get_strategy()\n\nprint(\"REPLICAS: \", strategy.num_replicas_in_sync)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# TEST_DS_PATH = KaggleDatasets().get_gcs_path('testtfrecords')\n# GCS_DS_PATH = KaggleDatasets().get_gcs_path('tfrecords-bengali-grapheme')\n# !gsutil ls $GCS_DS_PATH\n\nTEST_DS_PATH =  '/kaggle/input/testtfrecords'\nGCS_DS_PATH = '/kaggle/input/tfrecords-bengali-grapheme'","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"if strategy.num_replicas_in_sync == 1: # single GPU or CPU\n    BATCH_SIZE = 256\n    VALIDATION_BATCH_SIZE = 256\nelse: # TPU pod\n    BATCH_SIZE = 8 * strategy.num_replicas_in_sync\n    VALIDATION_BATCH_SIZE = 8 * strategy.num_replicas_in_sync\n    \nFILENAMES = tf.io.gfile.glob(GCS_DS_PATH+'/*.tfrec')\nTEST_FILENAMES  = tf.io.gfile.glob(TEST_DS_PATH+'/*.tfrec')\n\nIMAGE_SIZE = [64, 64]\nif K.image_data_format() == 'channels_first':\n    SHAPE = (3,*IMAGE_SIZE)\nelse:\n    SHAPE = (*IMAGE_SIZE, 3)\nSIZE_TFRECORD = 128\nsplit = int(len(FILENAMES)*0.81)\nTRAINING_FILENAMES = FILENAMES[:split]\nVALIDATION_FILENAMES = FILENAMES[split:]\nSTEP_PER_EPOCH = (len(TRAINING_FILENAMES)*SIZE_TFRECORD)//BATCH_SIZE\nVALIDATION_STEP_PER_EPOCH = (len(VALIDATION_FILENAMES)*SIZE_TFRECORD)//VALIDATION_BATCH_SIZE\nprint(len(TRAINING_FILENAMES))\nprint(len(VALIDATION_FILENAMES))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def display_9_images_from_dataset(dataset):\n  plt.figure(figsize=(13,13))\n  subplot=331\n  for i, (image, label) in enumerate(dataset):\n    plt.subplot(subplot)\n    plt.axis('off')\n    plt.imshow(image.numpy().astype(np.uint8))\n    subplot += 1\n    if i==2:\n      break\n  plt.tight_layout()\n  plt.subplots_adjust(wspace=0.1, hspace=0.1)\n  plt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def read_tfrecord(example):\n    features = {\n      \"image\": tf.io.FixedLenFeature([], tf.string), \n      \"grapheme_root\": tf.io.FixedLenFeature([], tf.int64),       \n      \"vowel_diacritic\": tf.io.FixedLenFeature([], tf.int64),       \n      \"consonant_diacritic\": tf.io.FixedLenFeature([], tf.int64),  \n\n      \"label\":         tf.io.FixedLenFeature([], tf.string),         \n      \"size\":          tf.io.FixedLenFeature([2], tf.int64),     \n      \"head_root_hot\": tf.io.VarLenFeature(tf.float32),\n      \"head_vowel_hot\": tf.io.VarLenFeature(tf.float32),\n      \"head_consonant_hot\": tf.io.VarLenFeature(tf.float32),\n    }\n\n    example = tf.io.parse_single_example(example, features)\n\n    image = tf.image.decode_image(example['image'], channels=3)\n#     image = tf.reshape(image, [*IMAGE_SIZE, 3])\n#     image = tf.cast(image, tf.float32)/255.0 \n    \n    grapheme_root = example['grapheme_root']\n    vowel_diacritic = example['vowel_diacritic']\n    consonant_diacritic = example['consonant_diacritic']\n     \n    head_root_hot = tf.sparse.to_dense(example['head_root_hot'])\n    head_vowel_hot = tf.sparse.to_dense(example['head_vowel_hot'])\n    head_consonant_hot = tf.sparse.to_dense(example['head_consonant_hot'])\n    \n    head_root_hot = tf.reshape(head_root_hot, [168])\n    head_vowel_hot = tf.reshape(head_vowel_hot, [11])\n    head_consonant_hot = tf.reshape(head_consonant_hot, [7])\n    \n    label  = example['label']\n    height = example['size'][0]\n    width  = example['size'][1]\n    return image,  {\"head_root\": head_root_hot, \"head_vowel\": head_vowel_hot, \"head_consonant\": head_consonant_hot}\noption_no_order = tf.data.Options()\noption_no_order.experimental_deterministic = False\ndef resize_and_crop_image(image, label):\n    w = tf.shape(image)[0]\n    h = tf.shape(image)[1]\n    tw = IMAGE_TARGET[1]\n    th = IMAGE_TARGET[0]\n    resize_crit = (w * th) / (h * tw)\n    image = tf.cond(resize_crit < 1,\n                    lambda: tf.image.resize(image, [w*tw/w, h*tw/w]), # if true\n                    lambda: tf.image.resize(image, [w*th/h, h*th/h])  # if false\n                   )\n    nw = tf.shape(image)[0]\n    nh = tf.shape(image)[1]\n    image = tf.image.crop_to_bounding_box(image, (nw - tw) // 2, (nh - th) // 2, tw, th)\n    return image, label\n\ndef normalize(image, label):\n    image = tf.reshape(image, [*IMAGE_SIZE, 3])\n    image = tf.cast(image, tf.float32)/255.0 \n    # image = tf.image.per_image_standardization(image)\n    return image, label\n\n\ndef augmentation(image, label):\n    image = tf.image.random_flip_left_right(image)\n    image = tf.image.random_flip_up_down(image)\n    # image = tf.image.rot90(image, k=random.randrange(4))\n    image = tf.image.random_saturation(image, 0, 2)\n    image = tf.image.random_hue(image, 0.08)\n    image = tf.image.random_contrast(image, 0.7, 1.3)\n    image = tf.image.random_brightness(image, 0.15)\n    return image, label\n\n\ndef force_image_sizes(dataset):\n    reshape_images = lambda image, label: (tf.reshape(image, SHAPE), label)   \n    dataset = dataset.map(reshape_images, num_parallel_calls=AUTO)\n    return dataset\n\ndef load_dataset(filenames):\n    ignore_order = tf.data.Options()\n    ignore_order.experimental_deterministic = False\n    dataset = tf.data.TFRecordDataset(filenames, num_parallel_reads=AUTO) \n    dataset = dataset.with_options(ignore_order)\n    dataset = dataset.map(read_tfrecord, num_parallel_calls=AUTO)\n#     dataset = dataset.map(resize_and_crop_image, num_parallel_calls=AUTO)\n    return dataset\n\n\ndef get_training_dataset():\n    dataset = load_dataset(TRAINING_FILENAMES)\n    dataset = dataset.map(augmentation, num_parallel_calls=AUTO)\n    dataset = dataset.map(normalize, num_parallel_calls=AUTO)\n    dataset = force_image_sizes(dataset)\n    dataset = dataset.repeat()\n    dataset = dataset.shuffle(8036)\n    dataset = dataset.batch(BATCH_SIZE)\n    dataset = dataset.prefetch(AUTO)\n    return dataset\n\ndef get_validation_dataset():\n    dataset = load_dataset(VALIDATION_FILENAMES)\n    dataset = dataset.map(normalize, num_parallel_calls=AUTO)\n    dataset = force_image_sizes(dataset)\n    dataset = dataset.batch(VALIDATION_BATCH_SIZE)\n    dataset = dataset.prefetch(AUTO) \n    return dataset","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Matplotlib config\nplt.ioff()\nplt.rc('image', cmap='gray_r')\nplt.rc('grid', linewidth=1)\nplt.rc('xtick', top=False, bottom=False, labelsize='large')\nplt.rc('ytick', left=False, right=False, labelsize='large')\nplt.rc('axes', facecolor='F8F8F8', titlesize=\"large\", edgecolor='white')\nplt.rc('text', color='a8151a')\nplt.rc('figure', facecolor='F0F0F0', figsize=(16,9))\n# Matplotlib fonts\nMATPLOTLIB_FONT_DIR = os.path.join(os.path.dirname(plt.__file__), \"mpl-data/fonts/ttf\")\n\ndef plot_learning_rate(lr_func, epochs):\n    xx = np.arange(epochs+1, dtype=np.float)\n    y = [lr_decay(x) for x in xx]\n    fig, ax = plt.subplots(figsize=(9, 6))\n    ax.set_xlabel('epochs')\n    ax.set_title('Learning rate\\ndecays from {:0.3g} to {:0.3g}'.format(y[0], y[-2]))\n    ax.minorticks_on()\n    ax.grid(True, which='major', axis='both', linestyle='-', linewidth=1)\n    ax.grid(True, which='minor', axis='both', linestyle=':', linewidth=0.5)\n    ax.step(xx,y, linewidth=3, where='post')\n    display.display(fig)\n\nclass PlotTraining(tf.keras.callbacks.Callback):\n    def __init__(self, sample_rate=1, zoom=1):\n        self.sample_rate = sample_rate\n        self.step = 0\n        self.zoom = zoom\n        self.steps_per_epoch = STEP_PER_EPOCH/2\n\n    def on_train_begin(self, logs={}):\n        self.batch_history = {}\n        self.batch_step = []\n        self.epoch_history = {}\n        self.epoch_step = []\n        self.fig, self.axes = plt.subplots(3, 2, figsize=(19, 19))\n        self.fig.subplots_adjust(wspace=0.3, hspace=0.25)\n        plt.ioff()\n      \n        \n    def on_batch_end(self, batch, logs={}):\n        if (batch % self.sample_rate) == 0:\n            self.batch_step.append(self.step)\n            for k,v in logs.items():\n              # do not log \"batch\" and \"size\" metrics that do not change\n              # do not log training accuracy \"acc\"\n                if k=='batch' or k=='size' or k == 'loss':# or k=='acc':\n                    continue\n                self.batch_history.setdefault(k, []).append(v)\n        self.step += 1\n\n    def on_epoch_end(self, epoch, logs={}):\n        plt.close(self.fig)\n        for axes in self.axes:\n            axes[0].cla()\n            axes[1].cla()\n\n            axes[0].set_ylim(0, 1.2/self.zoom)\n            axes[1].set_ylim(1-1/self.zoom/2, 1+0.1/self.zoom/2)\n    \n        self.epoch_step.append(self.step)\n        for k,v in logs.items():\n          # only log validation metrics\n            if not k.startswith('val_') or k == 'val_loss':\n                continue\n            self.epoch_history.setdefault(k, []).append(v)\n\n        display.clear_output(wait=True)\n        \n        for count, (k,v) in enumerate(self.batch_history.items()):\n            if count <= 2:\n                self.axes[count][0].plot(np.array(self.batch_step) / self.steps_per_epoch, v, label='{}: {:.3f}'.format(k, v[len(v)-1]))\n            else:\n                self.axes[count-3][1].plot(np.array(self.batch_step) / self.steps_per_epoch, v, label='{}: {:.3f}'.format(k, v[len(v)-1]))\n      \n        for count, (k,v) in enumerate(self.epoch_history.items()):\n            if count <= 2:\n                self.axes[count][0].plot(np.array(self.epoch_step) / self.steps_per_epoch, v, label='{}: {:.3f}'.format(k, v[epoch]), linewidth=3)\n            else:\n                self.axes[count-3][1].plot(np.array(self.epoch_step) / self.steps_per_epoch, v, label='{}: {:.3f}'.format(k, v[epoch]), linewidth=3)\n        for axes in self.axes:\n            axes[0].legend()\n            axes[1].legend()\n            axes[0].set_xlabel('epochs')\n            axes[1].set_xlabel('epochs')\n            axes[0].minorticks_on()\n            axes[0].grid(True, which='major', axis='both', linestyle='-', linewidth=1)\n            axes[0].grid(True, which='minor', axis='both', linestyle=':', linewidth=0.5)\n            axes[1].minorticks_on()\n            axes[1].grid(True, which='major', axis='both', linestyle='-', linewidth=1)\n            axes[1].grid(True, which='minor', axis='both', linestyle=':', linewidth=0.5)\n        display.display(self.fig)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# len(list(dataset.as_numpy_iterator()))\ntraining_dataset = get_training_dataset()\nvalidation_dataset = get_validation_dataset()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def weighted_cross_entropy(beta):\n  def convert_to_logits(y_pred):\n      # see https://github.com/tensorflow/tensorflow/blob/r1.10/tensorflow/python/keras/backend.py#L3525\n      y_pred = tf.clip_by_value(y_pred, tf.keras.backend.epsilon(), 1 - tf.keras.backend.epsilon())\n\n      return tf.log(y_pred / (1 - y_pred))\n\n  def loss(y_true, y_pred):\n    y_pred = convert_to_logits(y_pred)\n    loss = tf.nn.weighted_cross_entropy_with_logits(logits=y_pred, targets=y_true, pos_weight=beta)\n\n    # or reduce_sum and/or axis=-1\n    return tf.reduce_mean(loss)\n\n  return loss\n\ndef balanced_cross_entropy(beta):\n  def convert_to_logits(y_pred):\n      # see https://github.com/tensorflow/tensorflow/blob/r1.10/tensorflow/python/keras/backend.py#L3525\n      y_pred = tf.clip_by_value(y_pred, tf.keras.backend.epsilon(), 1 - tf.keras.backend.epsilon())\n\n      return tf.log(y_pred / (1 - y_pred))\n\n  def loss(y_true, y_pred):\n    y_pred = convert_to_logits(y_pred)\n    pos_weight = beta / (1 - beta)\n    loss = tf.nn.weighted_cross_entropy_with_logits(logits=y_pred, targets=y_true, pos_weight=pos_weight)\n\n    # or reduce_sum and/or axis=-1\n    return tf.reduce_mean(loss * (1 - beta))\n\n  return loss\n\ndef focal_loss(alpha=0.25, gamma=2):\n  def focal_loss_with_logits(logits, targets, alpha, gamma, y_pred):\n    weight_a = alpha * (1 - y_pred) ** gamma * targets\n    weight_b = (1 - alpha) * y_pred ** gamma * (1 - targets)\n    \n    return (tf.log1p(tf.exp(-tf.abs(logits))) + tf.nn.relu(-logits)) * (weight_a + weight_b) + logits * weight_b \n\n  def loss(y_true, y_pred):\n    y_pred = tf.clip_by_value(y_pred, tf.keras.backend.epsilon(), 1 - tf.keras.backend.epsilon())\n    logits = tf.log(y_pred / (1 - y_pred))\n\n    loss = focal_loss_with_logits(logits=logits, targets=y_true, alpha=alpha, gamma=gamma, y_pred=y_pred)\n\n    # or reduce_sum and/or axis=-1\n    return tf.reduce_mean(loss)\n\n  return loss","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"with strategy.scope():\n    bnmomemtum=0.88\n    def fire(x, filters, kernel_size):\n        if not isinstance(filters, list): \n            filters = [filters, filters]  \n        x = SeparableConv2D(filters[0], kernel_size, padding='same', use_bias=False)(x)\n        x = BatchNormalization(axis=channel_axis, center=True, scale=False, momentum=bnmomemtum)(x)\n        x = Activation('relu')(x)\n        x = SeparableConv2D(filters[1], kernel_size, padding='same', use_bias=False)(x)\n        return BatchNormalization(axis=channel_axis, center=True, scale=False, momentum=bnmomemtum)(x)\n    \n    def fire_module_separable_conv(filters, kernel_size=(3, 3)):\n        return lambda x: fire(x, filters, kernel_size)\n    \n    channel_axis = 1 if K.image_data_format() == 'channels_first' else -1\n    img_input = Input(shape=SHAPE)\n\n    x = Conv2D(32, (3, 3), strides=(2, 2), use_bias=False)(img_input)\n    x = BatchNormalization(axis=channel_axis, center=True, scale=False, momentum=bnmomemtum)(x)\n    x = Activation('relu')(x)\n\n    x = Conv2D(64, (3, 3), use_bias=False, name='block1_conv2')(x)\n    x = BatchNormalization(axis=channel_axis, center=True, scale=False, momentum=bnmomemtum)(x)\n    x = Activation('relu')(x)\n\n    residual = Conv2D(128, (1, 1), strides=(2, 2), padding='same', use_bias=False)(x)\n    residual = BatchNormalization(axis=channel_axis, center=True, scale=False, momentum=bnmomemtum)(residual)\n\n    x = fire_module_separable_conv(128)(x)\n\n    x = MaxPooling2D((3, 3), strides=(2, 2), padding='same')(x)\n    x = add_concat([x, residual])\n\n    residual = Conv2D(256, (1, 1), strides=(2, 2), padding='same', use_bias=False)(x)\n    residual = BatchNormalization(axis=channel_axis, center=True, scale=False, momentum=bnmomemtum)(residual)\n\n    x = Activation('relu')(x)\n    x = fire_module_separable_conv(256)(x)\n\n    x = MaxPooling2D((3, 3), strides=(2, 2), padding='same')(x)\n    x = add_concat([x, residual])\n\n    for i in range(4):\n        residual = x\n\n        x = Activation('relu')(x)\n        x = SeparableConv2D(256, (3, 3), padding='same', use_bias=False)(x)\n        x = BatchNormalization(axis=channel_axis, center=True, scale=False, momentum=bnmomemtum)(x)\n        x = Activation('relu')(x)\n        x = fire_module_separable_conv(256)(x)\n        \n        x = add_concat([x, residual])\n\n\n    x = fire_module_separable_conv([728, 1024])(x)\n    x = Activation('relu')(x)\n    y = GlobalAveragePooling2D()(x)\n    \n    y = Dense(728)(y)\n    y = Activation('relu')(y)\n    y = Dropout(0.4)(y)\n    \n    head_root = Dense(168, activation = 'softmax', name='head_root')(y)\n    head_vowel = Dense(11, activation = 'softmax', name='head_vowel')(y)\n    head_consonant = Dense(7, activation = 'softmax', name='head_consonant')(y)\n   \n    model = Model(inputs=img_input, outputs=[head_root, head_vowel, head_consonant]) \n    \nmodel.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])\nmodel.summary()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"if strategy.num_replicas_in_sync == 1: # single GPU\n    start_lr =  0.0001\n    min_lr = 0.000158\n    max_lr = 0.01 * strategy.num_replicas_in_sync\n    rampup_epochs = 14\n    sustain_epochs = 0\n    exp_decay = .7\nelse: # TPU pod\n    start_lr = 0.00001\n    min_lr = 0.00001\n    max_lr = 0.00002 * strategy.num_replicas_in_sync\n    rampup_epochs = 7\n    sustain_epochs = 0\n    exp_decay = .8\n\nEPOCHS=25\n\ndef lr_decay(epoch):\n    def lr(epoch, start_lr, min_lr, max_lr, rampup_epochs, sustain_epochs, exp_decay):\n        if epoch < rampup_epochs:\n            lr = (max_lr - start_lr)/rampup_epochs * epoch + start_lr\n        elif epoch < rampup_epochs + sustain_epochs:\n            lr = max_lr\n        else:\n            lr = (max_lr - min_lr) * exp_decay**(epoch-rampup_epochs-sustain_epochs) + min_lr\n        return lr\n    return lr(epoch, start_lr, min_lr, max_lr, rampup_epochs, sustain_epochs, exp_decay)\n    \nlr_decay_callback = tf.keras.callbacks.LearningRateScheduler(lambda epoch: lr_decay(epoch), verbose=True)\n\nrng = [i for i in range(EPOCHS)]\ny = [lr_decay(x) for x in rng]\nplt.plot(rng, [lr_decay(x) for x in rng])\nprint(y[0], y[-1])\nplot_learning_rate(lr_decay_callback, EPOCHS)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"weight_path=\"{}_weights.best.hdf5\".format('model')\n\ncheckpoint = ModelCheckpoint(weight_path, monitor='val_head_root_loss', verbose=1, save_best_only=True, mode='min', save_weights_only=True) \n\nplot_training = PlotTraining(sample_rate=10, zoom=1)\ncallbacks_list = [checkpoint, plot_training, lr_decay_callback]  #, \nSTEP_PER_EPOCH //=2 ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"history = model.fit_generator(\n    training_dataset, \n    steps_per_epoch=STEP_PER_EPOCH, \n    epochs=EPOCHS,\n    validation_data=validation_dataset,\n    validation_steps=VALIDATION_STEP_PER_EPOCH,\n    callbacks=callbacks_list)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model.load_weights(weight_path)\nmodel.save('model_tpu_gpu_cpu.h5')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def read_test_tfrecord(example):\n    TEST_TFREC_FORMAT = {\n        \"image\": tf.io.FixedLenFeature([], tf.string), \n        \"label\": tf.io.FixedLenFeature([], tf.string),  \n    }\n    example = tf.io.parse_single_example(example, TEST_TFREC_FORMAT)\n    image = tf.image.decode_image(example['image'], channels=3)\n    image_model = tf.cast(image, tf.float32)/255.0 \n    image_model = tf.reshape(image_model, SHAPE)\n    head_root_hot_classes =  [x for x in range(168)]\n    head_vowel_hot_classes =  [x  for x in range(11)]\n    head_consonant_hot_classes = [x  for x in range(7)]\n    label = example['label']\n    return image_model, label\n\ndef load_test_dataset(filenames):\n    ignore_order = tf.data.Options()\n    ignore_order.experimental_deterministic = False\n    dataset = tf.data.TFRecordDataset(filenames, num_parallel_reads=AUTO)\n    dataset = dataset.with_options(ignore_order) \n    dataset = dataset.map(read_test_tfrecord, num_parallel_calls=AUTO)\n    return dataset\n\ndef get_test_dataset(filenames):\n    dataset = load_test_dataset(filenames)\n    return dataset\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test = get_test_dataset(TEST_FILENAMES)\ntest = test.batch(1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"predicts = []\nfor i , (image, label) in enumerate(test):\n    predict =  model.predict(image)\n    preds = []\n    for pred in predict:\n        preds += [np.argmax(pred, axis=1).tolist()[0]]\n    predicts += [[preds, label[0].numpy().decode(\"utf-8\")]]\npredicts","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import pandas as pd\nrow_ids = []\ntarget = []\nfor pred in predicts:\n    row_id = pred[1].split('.')[0]\n    consonant = row_id+'_consonant_diacritic'\n    root = row_id+'_grapheme_root'\n    vowel = row_id+'_vowel_diacritic'\n    row_ids.append(consonant)\n    target.append(pred[0][2])\n    row_ids.append(root)\n    target.append(pred[0][0])\n    row_ids.append(vowel)\n    target.append(pred[0][1])        \n\ndf_sample = pd.DataFrame({\n    'row_id': row_ids,\n    'target':target\n},columns=['row_id','target'])\n\ndf_sample.to_csv('submission.csv',index=False)\ndf_sample","execution_count":null,"outputs":[]}],"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":4,"nbformat_minor":4}