{"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, gc\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 GlobalAveragePooling2D, Dense, MaxPooling2D, Activation, GlobalMaxPooling2D, concatenate\nfrom tensorflow.keras.layers import Conv2D, Dropout, BatchNormalization, Input, SeparableConv2D, 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 = 64\n    VALIDATION_BATCH_SIZE = 64\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]\nIMAGE_TARGET = [64, 64]\nif K.image_data_format() == 'channels_first':\n    SHAPE = (3,*IMAGE_SIZE)\n    INPUT_SHAPE = (3, *IMAGE_TARGET)\nelse:\n    SHAPE = (*IMAGE_SIZE, 3)\n    INPUT_SHAPE = (*IMAGE_TARGET, 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)*2\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 get_mat(rotation, shear, height_zoom, width_zoom, height_shift, width_shift):\n    # returns 3x3 transformmatrix which transforms indicies\n        \n    # CONVERT DEGREES TO RADIANS\n    rotation = math.pi * rotation / 180.\n    shear = math.pi * shear / 180.\n    \n    # ROTATION MATRIX\n    c1 = tf.math.cos(rotation)\n    s1 = tf.math.sin(rotation)\n    one = tf.constant([1],dtype='float32')\n    zero = tf.constant([0],dtype='float32')\n    rotation_matrix = tf.reshape( tf.concat([c1,s1,zero, -s1,c1,zero, zero,zero,one],axis=0),[3,3] )\n        \n    # SHEAR MATRIX\n    c2 = tf.math.cos(shear)\n    s2 = tf.math.sin(shear)\n    shear_matrix = tf.reshape( tf.concat([one,s2,zero, zero,c2,zero, zero,zero,one],axis=0),[3,3] )    \n    \n    # ZOOM MATRIX\n    zoom_matrix = tf.reshape( tf.concat([one/height_zoom,zero,zero, zero,one/width_zoom,zero, zero,zero,one],axis=0),[3,3] )\n    \n    # SHIFT MATRIX\n    shift_matrix = tf.reshape( tf.concat([one,zero,height_shift, zero,one,width_shift, zero,zero,one],axis=0),[3,3] )\n    \n    return K.dot(K.dot(rotation_matrix, shear_matrix), K.dot(zoom_matrix, shift_matrix))\n\n\ndef transform(image,label):\n    # input image - is one image of size [dim,dim,3] not a batch of [b,dim,dim,3]\n    # output - image randomly rotated, sheared, zoomed, and shifted\n    DIM = IMAGE_SIZE[0]\n    XDIM = DIM%2 #fix for size 331\n    \n    rot = 15. * tf.random.normal([1],dtype='float32')\n    shr = 5. * tf.random.normal([1],dtype='float32') \n    h_zoom = 0.8 + tf.random.normal([1],dtype='float32')/10.\n    w_zoom = 0.8 + tf.random.normal([1],dtype='float32')/10.\n    h_shift = 16. * tf.random.normal([1],dtype='float32') \n    w_shift = 16. * tf.random.normal([1],dtype='float32') \n  \n    # GET TRANSFORMATION MATRIX\n    m = get_mat(rot,shr,h_zoom,w_zoom,h_shift,w_shift) \n\n    # LIST DESTINATION PIXEL INDICES\n    x = tf.repeat( tf.range(DIM//2,-DIM//2,-1), DIM )\n    y = tf.tile( tf.range(-DIM//2,DIM//2),[DIM] )\n    z = tf.ones([DIM*DIM],dtype='int32')\n    idx = tf.stack( [x,y,z] )\n    \n    # ROTATE DESTINATION PIXELS ONTO ORIGIN PIXELS\n    idx2 = K.dot(m,tf.cast(idx,dtype='float32'))\n    idx2 = K.cast(idx2,dtype='int32')\n    idx2 = K.clip(idx2,-DIM//2+XDIM+1,DIM//2)\n    \n    # FIND ORIGIN PIXEL VALUES           \n    idx3 = tf.stack( [DIM//2-idx2[0,], DIM//2-1+idx2[1,]] )\n    d = tf.gather_nd(image,tf.transpose(idx3))\n        \n    return tf.reshape(d,[DIM,DIM,3]),label","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\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    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\n\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(transform, 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(2000)\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   \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\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(1, 2, figsize=(16, 7))\n        self.fig.subplots_adjust(wspace=0.3, hspace=0.25)\n        plt.ioff()\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        self.axes[0].cla()\n        self.axes[1].cla()\n      \n        self.axes[0].set_ylim(0, 1.2/self.zoom)\n        self.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 k,v in self.batch_history.items():\n            if isinstance(v, list) and len(v) > 0:\n                label='{}: {:.3f}'.format(k, v[len(v)-1])\n                self.axes[0 if k.endswith('loss') else 1].plot(np.array(self.batch_step) / self.steps_per_epoch, v, label=label)\n            else:\n                continue\n        for k,v in self.epoch_history.items():\n            if isinstance(v, list) and len(v) > 0:\n                label='{}: {:.3f}'.format(k, v[len(v)-1])\n                self.axes[0 if k.endswith('loss') else 1].plot(np.array(self.epoch_step) / self.steps_per_epoch, v, label=label, linewidth=3)\n            else:\n                continue\n        self.axes[0].legend()\n        self.axes[1].legend()\n        self.axes[0].set_xlabel('epochs')\n        self.axes[1].set_xlabel('epochs')\n        self.axes[0].minorticks_on()\n        self.axes[0].grid(True, which='major', axis='both', linestyle='-', linewidth=1)\n        self.axes[0].grid(True, which='minor', axis='both', linestyle=':', linewidth=0.5)\n        self.axes[1].minorticks_on()\n        self.axes[1].grid(True, which='major', axis='both', linestyle='-', linewidth=1)\n        self.axes[1].grid(True, which='minor', axis='both', linestyle=':', linewidth=0.5)\n        display.display(self.fig)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"with strategy.scope():\n    bnmomemtum=0.8\n    def fire(x, squeeze, expand):\n        y  = Conv2D(filters=squeeze, kernel_size=1, activation='relu', padding='same')(x)\n        y  = BatchNormalization(momentum=bnmomemtum)(y)\n        y1 = Conv2D(filters=expand//2, kernel_size=1, activation='relu', padding='same')(y)\n        y1 = BatchNormalization(momentum=bnmomemtum)(y1)\n        y3 = Conv2D(filters=expand//2, kernel_size=3, activation='relu', padding='same')(y)\n        y3 = BatchNormalization(momentum=bnmomemtum)(y3)\n        return concatenate([y1, y3])\n\n    def fire_module(squeeze, expand):\n        return lambda x: fire(x, squeeze, expand)\n\n    img_input = Input(shape=INPUT_SHAPE) \n\n    y = Conv2D(kernel_size=3, filters=64, padding='same', use_bias=True, activation='relu')(img_input)\n    y = BatchNormalization(momentum=bnmomemtum)(y)\n    y = fire_module(48, 96)(y)\n    y = MaxPooling2D(pool_size=2)(y)\n    y = fire_module(96, 192)(y)\n    y = MaxPooling2D(pool_size=2)(y)\n    y = fire_module(192, 384)(y)\n    y = MaxPooling2D(pool_size=2)(y)\n    y = fire_module(384, 768)(y)\n\n    y = GlobalAveragePooling2D()(y)\n    \n    y = Dense(1536)(y)\n    y = Activation('relu')(y)\n    y = Dropout(0.33)(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":"EPOCHS = 15\ndef lr_decay(epoch):\n    return 0.001 * math.pow(0.666, epoch)\n\nlr_decay_callback = tf.keras.callbacks.LearningRateScheduler(lr_decay, verbose=True)\n\nplot_learning_rate(lr_decay, EPOCHS)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"weight_path=\"{}_weights.squeeze.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","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"training_dataset = get_training_dataset()\nvalidation_dataset = get_validation_dataset()","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_Squeeze.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\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\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}