{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.8.17","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"<!-- !apt-get update && apt-get install -y python3-opencv\n!pip install opencv-python\n!pip install scikit-learn\n!pip install -U tensorflow-addons\n!pip install pyarrow -->","metadata":{"execution":{"iopub.status.busy":"2023-04-13T05:38:17.725753Z","iopub.execute_input":"2023-04-13T05:38:17.726657Z","iopub.status.idle":"2023-04-13T05:39:44.652056Z","shell.execute_reply.started":"2023-04-13T05:38:17.726623Z","shell.execute_reply":"2023-04-13T05:39:44.650898Z"}}},{"cell_type":"markdown","source":"<!-- import numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport matplotlib.pyplot as plt\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename)) -->","metadata":{}},{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport matplotlib.pyplot as plt\nimport os\nimport warnings\nimport random\n\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\n# import os\n# for dirname, _, filenames in os.walk('/kaggle/input'):\n#     for filename in filenames:\n#         print(os.path.join(dirname, filename))\n\n\nimport cv2\nimport tensorflow as tf\n\nwarnings.simplefilter('ignore')\ntf.compat.v1.logging.set_verbosity(tf.compat.v1.logging.ERROR)\n\nfrom tensorflow.keras.preprocessing.image import load_img, img_to_array\nfrom sklearn.metrics import f1_score\n# import pyarrow.parquet as pq\n\nfrom tensorflow import keras\nfrom tensorflow.keras import layers\nimport tensorflow_addons as tfa\n\nfrom sklearn.model_selection import train_test_split\nfrom tensorflow.keras.utils import to_categorical\n\n#from tensorflow.keras.applications.vit import ViT\nfrom tensorflow.keras.layers import Input, Dense\nfrom tensorflow.keras.models import Model","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.idle":"2023-08-29T17:17:17.202403Z","shell.execute_reply.started":"2023-08-29T17:16:46.515583Z","shell.execute_reply":"2023-08-29T17:17:17.201272Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"resolver = tf.distribute.cluster_resolver.TPUClusterResolver()\ntf.config.experimental_connect_to_cluster(resolver)\ntf.tpu.experimental.initialize_tpu_system(resolver)\nstrategy = tf.distribute.TPUStrategy(resolver)","metadata":{"execution":{"iopub.status.busy":"2023-08-29T17:17:17.204347Z","iopub.execute_input":"2023-08-29T17:17:17.204830Z","iopub.status.idle":"2023-08-29T17:17:24.526941Z","shell.execute_reply.started":"2023-08-29T17:17:17.204802Z","shell.execute_reply":"2023-08-29T17:17:24.525718Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# declare some parameter\nSEED       = 2020\nbatch_size = 128\ndim        = (137, 236)\nSIZE       = 128\nstats      = (0.0692, 0.2051)\nHEIGHT     = 137 \nWIDTH      = 236\n\nlearning_rate = 0.001\nweight_decay = 0.0001\nnum_epochs = 10\ninput_shape = (dim[0], dim[1], 1)\n# image_size = 72  # We'll resize input images to this size\n# patch_size = 6  # Size of the patches to be extract from the input images\n# num_patches = (image_size // patch_size) ** 2\n# projection_dim = 64\n# num_heads = 4\n# transformer_units = [\n#     projection_dim * 2,\n#     projection_dim,\n# ]  # Size of the transformer layers\n# transformer_layers = 32\n# mlp_head_units = [2048, 1024]  # Size of the dense layers of the final classifier\n\ndef seed_all(SEED):\n    random.seed(SEED)\n    np.random.seed(SEED)\n    os.environ['PYTHONHASHSEED'] = str(SEED)\n    \n# seed all\nseed_all(SEED)","metadata":{"execution":{"iopub.status.busy":"2023-08-29T17:17:24.530918Z","iopub.execute_input":"2023-08-29T17:17:24.531196Z","iopub.status.idle":"2023-08-29T17:17:24.539053Z","shell.execute_reply.started":"2023-08-29T17:17:24.531171Z","shell.execute_reply":"2023-08-29T17:17:24.538224Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# load files\nim_path = '../input/grapheme-images/grapheme_images/train/'\ntrain   = pd.read_csv('../input/bengaliai-cv19/train.csv')\ntest    = pd.read_csv('../input/bengaliai-cv19/test.csv')\n\ntrain = train.sample(frac=1).reset_index(drop=True) # shuffling \ntrain['filename'] = train.image_id.apply(lambda filename: im_path + filename + '.png')\n\n# top 5 samples\ntrain.head()","metadata":{"execution":{"iopub.status.busy":"2023-08-29T17:17:24.541478Z","iopub.execute_input":"2023-08-29T17:17:24.541776Z","iopub.status.idle":"2023-08-29T17:17:24.945025Z","shell.execute_reply.started":"2023-08-29T17:17:24.541746Z","shell.execute_reply":"2023-08-29T17:17:24.944038Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"num_classes_1 = len(train['grapheme_root'].unique())\nnum_classes_2 = len(train['vowel_diacritic'].unique())\nnum_classes_3 = len(train['consonant_diacritic'].unique())","metadata":{"execution":{"iopub.status.busy":"2023-08-29T17:17:24.946115Z","iopub.execute_input":"2023-08-29T17:17:24.946407Z","iopub.status.idle":"2023-08-29T17:17:24.954483Z","shell.execute_reply.started":"2023-08-29T17:17:24.946381Z","shell.execute_reply":"2023-08-29T17:17:24.953525Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class GraphemeGenerator(keras.utils.Sequence):\n    def __init__(self, data, batch_size, dim, shuffle=True, transform=None):\n        self._data = data\n        self._label_1 = pd.get_dummies(self._data['grapheme_root'], \n                                       columns = ['grapheme_root'])\n        self._label_2 = pd.get_dummies(self._data['vowel_diacritic'], \n                                       columns = ['vowel_diacritic'])\n        self._label_3 = pd.get_dummies(self._data['consonant_diacritic'], \n                                       columns = ['consonant_diacritic'])\n        self._list_idx = data.index.values\n        self._batch_size = batch_size\n        self._dim = dim\n        self._shuffle = shuffle\n        self.transform = transform\n        self.on_epoch_end()\n        \n    def __len__(self):\n        return int(np.floor(len(self._data)/self._batch_size))\n    \n    def __getitem__(self, index):\n        batch_idx = self._indices[index*self._batch_size:(index+1)*self._batch_size]\n        _idx = [self._list_idx[k] for k in batch_idx]\n\n        Data     = np.empty((self._batch_size, *self._dim, 1))\n        Target_1 = np.empty((self._batch_size, 168), dtype = int)\n        Target_2 = np.empty((self._batch_size, 11 ), dtype = int)\n        Target_3 = np.empty((self._batch_size,  7 ), dtype = int)\n        \n        for i, k in enumerate(_idx):\n            # load the image file using cv2\n            image = cv2.imread(im_path + self._data['image_id'][k] + '.png')\n            #print(image.shape)\n            #image = cv2.resize(image,  self._dim) \n            \n            if self.transform is not None:\n                if np.random.rand() > 0.7:\n                    # albumentation : grid mask\n                    res = self.transform(image=image)\n                    image = res['image']\n                else:\n                    # augmix augmentation\n                    image = augment_and_mix(image)\n            \n            # scaling \n            image = (image.astype(np.float32)/255.0 - stats[0])/stats[1]\n            \n            # gray scaling \n            gray = lambda rgb : np.dot(rgb[... , :3] , [0.299 , 0.587, 0.114]) \n            image = gray(image)  \n            \n            # expand the axises \n            image = image[:, :, np.newaxis]\n            Data[i,:, :, :] =  image\n        \n            Target_1[i,:] = self._label_1.loc[k, :].values\n            Target_2[i,:] = self._label_2.loc[k, :].values\n            Target_3[i,:] = self._label_3.loc[k, :].values\n            \n        return Data, [Target_1, Target_2, Target_3]\n    \n    \n    def on_epoch_end(self):\n        self._indices = np.arange(len(self._list_idx))\n        if self._shuffle:\n            np.random.shuffle(self._indices)","metadata":{"execution":{"iopub.status.busy":"2023-08-29T17:17:24.955649Z","iopub.execute_input":"2023-08-29T17:17:24.955916Z","iopub.status.idle":"2023-08-29T17:17:24.975600Z","shell.execute_reply.started":"2023-08-29T17:17:24.955892Z","shell.execute_reply":"2023-08-29T17:17:24.974788Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_labels, val_labels = train_test_split(train, test_size = 0.20, random_state = SEED,\n                                            stratify = train[['grapheme_root', \n                                                              'vowel_diacritic', \n                                                              'consonant_diacritic']])","metadata":{"execution":{"iopub.status.busy":"2023-08-29T17:17:24.976622Z","iopub.execute_input":"2023-08-29T17:17:24.976899Z","iopub.status.idle":"2023-08-29T17:17:26.689731Z","shell.execute_reply.started":"2023-08-29T17:17:24.976874Z","shell.execute_reply":"2023-08-29T17:17:26.688424Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# training generator\ntrain_generator = GraphemeGenerator(train_labels, batch_size, dim, \n                                shuffle = True, transform=None)\n\n# validation generator: no shuffle , not augmentation\nval_generator = GraphemeGenerator(val_labels, batch_size, dim, \n                              shuffle = False)","metadata":{"execution":{"iopub.status.busy":"2023-08-29T17:17:26.691141Z","iopub.execute_input":"2023-08-29T17:17:26.691478Z","iopub.status.idle":"2023-08-29T17:17:26.923108Z","shell.execute_reply.started":"2023-08-29T17:17:26.691447Z","shell.execute_reply":"2023-08-29T17:17:26.921817Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"temp = list()\nfor i, item in enumerate(train_generator):\n    x, y = item\n    print(x.shape)\n    if i == 0: break\ntemp = x\ntemp = np.asarray(temp)\nprint(temp.shape)","metadata":{"execution":{"iopub.status.busy":"2023-08-29T17:17:26.924345Z","iopub.execute_input":"2023-08-29T17:17:26.924679Z","iopub.status.idle":"2023-08-29T17:17:28.130796Z","shell.execute_reply.started":"2023-08-29T17:17:26.924649Z","shell.execute_reply":"2023-08-29T17:17:28.129796Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from pylab import rcParams\n\n# helper function to plot sample \ndef plot_imgs(dataset_show):\n    rcParams['figure.figsize'] = 20,10\n    for i in range(2):\n        f, ax = plt.subplots(1,5)\n        for p in range(5):\n            idx = np.random.randint(0, len(dataset_show))\n            img, label = dataset_show[idx]\n            ax[p].grid(False)\n            ax[p].imshow(img[0][:,:,0], cmap=plt.get_cmap('gray'))\n            ax[p].set_title(idx)\n\n# calling the above function           \nplot_imgs(val_generator) ","metadata":{"execution":{"iopub.status.busy":"2023-08-29T17:17:28.131873Z","iopub.execute_input":"2023-08-29T17:17:28.132216Z","iopub.status.idle":"2023-08-29T17:17:41.679005Z","shell.execute_reply.started":"2023-08-29T17:17:28.132186Z","shell.execute_reply":"2023-08-29T17:17:41.677943Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_imgs(train_generator)","metadata":{"execution":{"iopub.status.busy":"2023-08-29T17:17:41.680129Z","iopub.execute_input":"2023-08-29T17:17:41.680420Z","iopub.status.idle":"2023-08-29T17:17:54.445121Z","shell.execute_reply.started":"2023-08-29T17:17:41.680394Z","shell.execute_reply":"2023-08-29T17:17:54.443921Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Sometimes we may want to resize the images for future analysis. We can use cv2 package.","metadata":{}},{"cell_type":"code","source":"# data_augmentation = keras.Sequential(\n#     [\n#         layers.Normalization(),\n#         layers.Resizing(image_size, image_size),\n#         layers.RandomFlip(\"horizontal\"),\n#         layers.RandomRotation(factor=0.02),\n#         layers.RandomZoom(\n#             height_factor=0.2, width_factor=0.2\n#         ),\n#     ],\n#     name=\"data_augmentation\",\n# )","metadata":{"execution":{"iopub.status.busy":"2023-08-29T17:17:54.446403Z","iopub.execute_input":"2023-08-29T17:17:54.446754Z","iopub.status.idle":"2023-08-29T17:17:54.451070Z","shell.execute_reply.started":"2023-08-29T17:17:54.446727Z","shell.execute_reply":"2023-08-29T17:17:54.450159Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# def mlp(x, hidden_units, dropout_rate):\n#     for units in hidden_units:\n#         x = layers.Dense(units, activation=tf.nn.gelu)(x)\n#         x = layers.Dropout(dropout_rate)(x)\n#     return x","metadata":{"execution":{"iopub.status.busy":"2023-08-29T17:17:54.454729Z","iopub.execute_input":"2023-08-29T17:17:54.455037Z","iopub.status.idle":"2023-08-29T17:17:54.466212Z","shell.execute_reply.started":"2023-08-29T17:17:54.455010Z","shell.execute_reply":"2023-08-29T17:17:54.465271Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# class Patches(layers.Layer):\n#     def __init__(self, patch_size):\n#         super().__init__()\n#         self.patch_size = patch_size\n\n#     def call(self, images):\n#         batch_size = tf.shape(images)[0]\n#         patches = tf.image.extract_patches(\n#             images=images,\n#             sizes=[1, self.patch_size, self.patch_size, 1],\n#             strides=[1, self.patch_size, self.patch_size, 1],\n#             rates=[1, 1, 1, 1],\n#             padding=\"VALID\",\n#         )\n#         patch_dims = patches.shape[-1]\n#         patches = tf.reshape(patches, [batch_size, -1, patch_dims])\n#         return patches","metadata":{"execution":{"iopub.status.busy":"2023-08-29T17:17:54.467254Z","iopub.execute_input":"2023-08-29T17:17:54.467520Z","iopub.status.idle":"2023-08-29T17:17:54.481376Z","shell.execute_reply.started":"2023-08-29T17:17:54.467496Z","shell.execute_reply":"2023-08-29T17:17:54.480596Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# plt.figure(figsize=(4, 4))\n# image = temp[np.random.choice(range(temp.shape[0]))]\n# plt.imshow(image.astype(\"uint8\"))\n# plt.axis(\"off\")\n\n# resized_image = tf.image.resize(\n#     tf.convert_to_tensor([image]), size=(image_size, image_size)\n# )\n\n# print(resized_image.shape)\n# patches = Patches(patch_size)(resized_image)\n# print(f\"Image size: {image_size} X {image_size}\")\n# print(f\"Patch size: {patch_size} X {patch_size}\")\n# print(f\"Patches per image: {patches.shape[1]}\")\n# print(f\"Elements per patch: {patches.shape[-1]}\")\n\n# n = int(np.sqrt(patches.shape[1]))\n# plt.figure(figsize=(4, 4))\n# for i, patch in enumerate(patches[0]):\n#     ax = plt.subplot(n, n, i + 1)\n#     patch_img = tf.reshape(patch, (patch_size, patch_size, 1))\n#     plt.imshow(patch_img.numpy().astype(\"uint8\"))\n#     plt.axis(\"off\")","metadata":{"execution":{"iopub.status.busy":"2023-08-29T17:17:54.482443Z","iopub.execute_input":"2023-08-29T17:17:54.482994Z","iopub.status.idle":"2023-08-29T17:17:54.492220Z","shell.execute_reply.started":"2023-08-29T17:17:54.482966Z","shell.execute_reply":"2023-08-29T17:17:54.491341Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# class PatchEncoder(layers.Layer):\n#     def __init__(self, num_patches, projection_dim):\n#         super().__init__()\n#         self.num_patches = num_patches\n#         self.projection = layers.Dense(units=projection_dim)\n#         self.position_embedding = layers.Embedding(\n#             input_dim=num_patches, output_dim=projection_dim\n#         )\n\n#     def call(self, patch):\n#         positions = tf.range(start=0, limit=self.num_patches, delta=1)\n#         encoded = self.projection(patch) + self.position_embedding(positions)\n#         return encoded","metadata":{"execution":{"iopub.status.busy":"2023-08-29T17:17:54.493306Z","iopub.execute_input":"2023-08-29T17:17:54.493596Z","iopub.status.idle":"2023-08-29T17:17:54.505596Z","shell.execute_reply.started":"2023-08-29T17:17:54.493570Z","shell.execute_reply":"2023-08-29T17:17:54.504678Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# def create_vit_classifier():\n#     inputs = layers.Input(shape=input_shape)\n#     # Augment data.\n#     augmented = data_augmentation(inputs)\n#     # Create patches.\n#     patches = Patches(patch_size)(augmented)\n#     # Encode patches.\n#     encoded_patches = PatchEncoder(num_patches, projection_dim)(patches)\n\n#     # Create multiple layers of the Transformer block.\n#     for _ in range(transformer_layers):\n#         # Layer normalization 1.\n#         x1 = layers.LayerNormalization(epsilon=1e-6)(encoded_patches)\n#         # Create a multi-head attention layer.\n#         attention_output = layers.MultiHeadAttention(\n#             num_heads=num_heads, key_dim=projection_dim, dropout=0.1\n#         )(x1, x1)\n#         # Skip connection 1.\n#         x2 = layers.Add()([attention_output, encoded_patches])\n#         # Layer normalization 2.\n#         x3 = layers.LayerNormalization(epsilon=1e-6)(x2)\n#         # MLP.\n#         x3 = mlp(x3, hidden_units=transformer_units, dropout_rate=0.1)\n#         # Skip connection 2.\n#         encoded_patches = layers.Add()([x3, x2])\n\n#     # Create a [batch_size, projection_dim] tensor.\n#     representation = layers.LayerNormalization(epsilon=1e-6)(encoded_patches)\n#     representation = layers.Flatten()(representation)\n#     representation = layers.Dropout(0.5)(representation)\n#     # Add MLP.\n#     features = mlp(representation, hidden_units=mlp_head_units, dropout_rate=0.5)\n#     # Classify outputs.\n#     logits_1 = layers.Dense(num_classes_1, name='gra')(features)\n#     logits_2 = layers.Dense(num_classes_2, name='vow')(features)\n#     logits_3 = layers.Dense(num_classes_3, name='cons')(features)\n#     # Create the Keras model.\n#     model = keras.Model(inputs=inputs, outputs=[logits_1, logits_2, logits_3])\n#     return model","metadata":{"execution":{"iopub.status.busy":"2023-08-29T17:17:54.506711Z","iopub.execute_input":"2023-08-29T17:17:54.506973Z","iopub.status.idle":"2023-08-29T17:17:54.517438Z","shell.execute_reply.started":"2023-08-29T17:17:54.506950Z","shell.execute_reply":"2023-08-29T17:17:54.516605Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.applications import ResNet50\nfrom tensorflow.keras.models import Model\nfrom tensorflow.keras.layers import Dense, GlobalAveragePooling2D","metadata":{"execution":{"iopub.status.busy":"2023-08-29T17:17:54.518448Z","iopub.execute_input":"2023-08-29T17:17:54.518843Z","iopub.status.idle":"2023-08-29T17:17:54.531346Z","shell.execute_reply.started":"2023-08-29T17:17:54.518816Z","shell.execute_reply":"2023-08-29T17:17:54.530428Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def create_resnet50_classifier(input_shape, num_classes_1, num_classes_2, num_classes_3):\n    # Load the ResNet50 model pre-trained on ImageNet\n    base_model = ResNet50(weights=None, include_top=False, input_shape=input_shape)\n\n    # Add a global spatial average pooling layer\n    x = base_model.output\n    x = GlobalAveragePooling2D()(x)\n\n    # Add a fully-connected layer\n    x = Dense(1024, activation='relu')(x)\n\n    # Add a logistic layer for each set of classes\n    logits_1 = Dense(num_classes_1, activation='softmax', name='gra')(x)\n    logits_2 = Dense(num_classes_2, activation='softmax', name='vow')(x)\n    logits_3 = Dense(num_classes_3, activation='softmax', name='cons')(x)\n\n    # Create the Keras model\n    model = Model(inputs=base_model.input, outputs=[logits_1, logits_2, logits_3])\n    \n    return model","metadata":{"execution":{"iopub.status.busy":"2023-08-29T17:17:54.532456Z","iopub.execute_input":"2023-08-29T17:17:54.532776Z","iopub.status.idle":"2023-08-29T17:17:54.541081Z","shell.execute_reply.started":"2023-08-29T17:17:54.532748Z","shell.execute_reply":"2023-08-29T17:17:54.540207Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"optimizer = tfa.optimizers.AdamW(\n        learning_rate=learning_rate, weight_decay=weight_decay\n    )\n\nwith strategy.scope():\n    res_classifier = create_resnet50_classifier(input_shape, num_classes_1, num_classes_2, num_classes_3)\n    res_classifier.compile(\n\n            optimizer = optimizer, \n\n            loss = {'gra' : 'categorical_crossentropy', \n                    'vow' : 'categorical_crossentropy', \n                    'cons': 'categorical_crossentropy'},\n\n            loss_weights = {'gra' : 1.0,\n                            'vow' : 1.0,\n                            'cons': 1.0},\n\n            metrics={'gra' : 'accuracy', \n                     'vow' : 'accuracy', \n                     'cons': 'accuracy'}\n            )","metadata":{"execution":{"iopub.status.busy":"2023-08-29T17:17:54.542275Z","iopub.execute_input":"2023-08-29T17:17:54.542598Z","iopub.status.idle":"2023-08-29T17:18:04.837258Z","shell.execute_reply.started":"2023-08-29T17:17:54.542569Z","shell.execute_reply":"2023-08-29T17:18:04.836070Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.callbacks import (ModelCheckpoint, LearningRateScheduler,\n                             EarlyStopping, ReduceLROnPlateau, TensorBoard, CSVLogger)\n\n# # some call back function; feel free to add more for experiment\n# def Call_Back():\n#     # model check point\n#     checkpoint = ModelCheckpoint('../input/weights/bengali_grapheme_ViT.h5', \n#                                  monitor = 'val_loss', \n#                                  verbose = 0, save_best_only=True, \n#                                  mode = 'min',\n#                                  save_weights_only = True)\n    \n#     csv_logger = CSVLogger('./weights/bg_ViT.csv')\n#     early = EarlyStopping(monitor='val_loss', \n#                           mode='min', patience=5)\n    \n#     return [checkpoint, csv_logger, early]\n\n\n# # calling all callbacks \n# callbacks = Call_Back()\n\n#vit_classifier.load_weights('../input/weights/bengali_grapheme_ViT.h5')","metadata":{"execution":{"iopub.status.busy":"2023-08-29T17:18:04.838465Z","iopub.execute_input":"2023-08-29T17:18:04.838756Z","iopub.status.idle":"2023-08-29T17:18:04.844163Z","shell.execute_reply.started":"2023-08-29T17:18:04.838731Z","shell.execute_reply":"2023-08-29T17:18:04.843269Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_history = res_classifier.fit(\n    train_generator,\n    steps_per_epoch=int(len(train_labels)/batch_size),  # recheck later\n    validation_data=val_generator,\n    validation_steps = int(len(val_labels)/batch_size), # recheck later\n    epochs=num_epochs\n)","metadata":{"execution":{"iopub.status.busy":"2023-08-29T17:18:04.845218Z","iopub.execute_input":"2023-08-29T17:18:04.845535Z","iopub.status.idle":"2023-08-29T18:34:18.390881Z","shell.execute_reply.started":"2023-08-29T17:18:04.845507Z","shell.execute_reply":"2023-08-29T18:34:18.388823Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from pathlib import Path\nOUTPUT_PATH = Path('/kaggle/working/weights')\nif not OUTPUT_PATH.exists():\n    OUTPUT_PATH.mkdir(parents=True, exist_ok=True)\nres_classifier.save_weights('/kaggle/working/weights/res_classic.h5')","metadata":{"execution":{"iopub.status.busy":"2023-08-29T18:36:49.828339Z","iopub.execute_input":"2023-08-29T18:36:49.829191Z","iopub.status.idle":"2023-08-29T18:36:51.272904Z","shell.execute_reply.started":"2023-08-29T18:36:49.829148Z","shell.execute_reply":"2023-08-29T18:36:51.271400Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"res_classifier.load_weights('/kaggle/working/weights/res_classic.h5')","metadata":{"execution":{"iopub.status.busy":"2023-08-29T18:37:53.294382Z","iopub.execute_input":"2023-08-29T18:37:53.296968Z","iopub.status.idle":"2023-08-29T18:38:05.274191Z","shell.execute_reply.started":"2023-08-29T18:37:53.295547Z","shell.execute_reply":"2023-08-29T18:38:05.272729Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}],"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"}}