{"cells":[{"metadata":{"_uuid":"b721ebea-db6f-4ded-be2e-a933d523bb3a","_cell_guid":"c72f2eb5-1485-4289-9eba-b562063c4681","trusted":true},"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 in \n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nfrom tqdm.auto import tqdm\nfrom glob import glob\nimport time, gc\nimport cv2\n\nfrom tensorflow import keras\nimport matplotlib.image as mpimg\nfrom keras.preprocessing.image import ImageDataGenerator\nfrom keras.models import Model\nfrom keras.models import clone_model\nfrom keras.layers import Dense,Conv2D,Flatten,MaxPool2D,Dropout,BatchNormalization, Input\nfrom keras.optimizers import Adam\nfrom keras.callbacks import ReduceLROnPlateau\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import confusion_matrix\nimport PIL.Image as Image, PIL.ImageDraw as ImageDraw, PIL.ImageFont as ImageFont\nfrom matplotlib import pyplot as plt\nimport seaborn as sns\n\n# Input data files are available in the \"../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))\n\n# Any results you write to the current directory are saved as output.","execution_count":0,"outputs":[]},{"metadata":{"_uuid":"cf3c071f-e2c9-4070-a253-38a5e5ad1bd1","_cell_guid":"62d7fe7e-3e11-46d9-9b7f-0d67eca2580d","trusted":true},"cell_type":"code","source":"train_df_ = pd.read_csv('/kaggle/input/bengaliai-cv19/train.csv')\ntest_df_ = pd.read_csv('/kaggle/input/bengaliai-cv19/test.csv')\nclass_map_df = pd.read_csv('/kaggle/input/bengaliai-cv19/class_map.csv')\nsample_sub_df = pd.read_csv('/kaggle/input/bengaliai-cv19/sample_submission.csv')","execution_count":0,"outputs":[]},{"metadata":{"_uuid":"4c60b41b-a19b-4724-a7d4-626d7390304e","_cell_guid":"a7c1cb83-a689-4811-b371-a1eb25976b14","trusted":true},"cell_type":"code","source":"train_df_.head()","execution_count":0,"outputs":[]},{"metadata":{"_uuid":"6c5ea99a-30da-4418-83d0-316ab1743afd","_cell_guid":"007cdc6e-7ae4-4b5e-86f3-8b90a39733eb","trusted":true},"cell_type":"code","source":"test_df_.head()","execution_count":0,"outputs":[]},{"metadata":{"_uuid":"0419e223-9504-410a-aee8-ef3d5a9190f9","_cell_guid":"7c240ba1-137a-46cc-b307-19fe57aa4e64","trusted":true},"cell_type":"code","source":"sample_sub_df.head()","execution_count":0,"outputs":[]},{"metadata":{"_uuid":"cd8b5bb8-38a3-40b9-9350-b2b3a8d3932f","_cell_guid":"16844c12-2d2a-4b97-ae5a-0389c19c9c0f","trusted":true},"cell_type":"code","source":"class_map_df.head()","execution_count":0,"outputs":[]},{"metadata":{"_uuid":"b7a620c4-586a-49f1-b132-74168f0586d6","_cell_guid":"2b66fb55-5c4c-49b3-997f-b6b55117df27","trusted":true},"cell_type":"code","source":"print(f'Size of training data: {train_df_.shape}')\nprint(f'Size of test data: {test_df_.shape}')\nprint(f'Size of class map: {class_map_df.shape}')","execution_count":0,"outputs":[]},{"metadata":{"_uuid":"df9034d6-d7fd-4aec-a909-50e07d5591fe","_cell_guid":"9cad83ca-96e6-4945-9903-d73ed3159fbd","trusted":true},"cell_type":"markdown","source":"## Exploratory Data Analysis\n# Exploratory data analysis (EDA) is an approach to analyzing data sets to summarize their main characteristics, often with visual methods."},{"metadata":{"_uuid":"85866e16-dae4-4306-ba1e-ae46b2042075","_cell_guid":"dc7ceb98-151a-44a8-a5bc-f483cda65114","trusted":true},"cell_type":"code","source":"HEIGHT = 236\nWIDTH = 236\n\ndef get_n(df, field, n, top=True):\n    top_graphemes = df.groupby([field]).size().reset_index(name='counts')['counts'].sort_values(ascending=not top)[:n]\n    top_grapheme_roots = top_graphemes.index\n    top_grapheme_counts = top_graphemes.values\n    top_graphemes = class_map_df[class_map_df['component_type'] == field].reset_index().iloc[top_grapheme_roots]\n    top_graphemes.drop(['component_type', 'label'], axis=1, inplace=True)\n    top_graphemes.loc[:, 'count'] = top_grapheme_counts\n    return top_graphemes\n\ndef image_from_char(char):\n    image = Image.new('RGB', (WIDTH, HEIGHT))\n    draw = ImageDraw.Draw(image)\n    myfont = ImageFont.truetype('/kaggle/input/kalpurush-fonts/kalpurush-2.ttf', 120)\n    w, h = draw.textsize(char, font=myfont)\n    draw.text(((WIDTH - w) / 2,(HEIGHT - h) / 3), char, font=myfont)\n\n    return image","execution_count":0,"outputs":[]},{"metadata":{"_uuid":"2c219f97-a01b-4b62-a738-048c806cbde0","_cell_guid":"e7d86b86-4830-4d52-8cf7-97bce8b5c810","trusted":true},"cell_type":"markdown","source":"### Number of unique values"},{"metadata":{"_uuid":"8ed67bd3-6586-49f1-95fe-edf5678e0add","_cell_guid":"d22a2050-67ae-4698-a1f2-da77b8a2f594","trusted":true},"cell_type":"code","source":"print(f'Number of unique grapheme roots: {train_df_[\"grapheme_root\"].nunique()}')\nprint(f'Number of unique vowel diacritic: {train_df_[\"vowel_diacritic\"].nunique()}')\nprint(f'Number of unique consonant diacritic: {train_df_[\"consonant_diacritic\"].nunique()}')","execution_count":0,"outputs":[]},{"metadata":{"_uuid":"94e11544-3957-43cc-bfbb-435868da7451","_cell_guid":"f9a91d8f-260b-445a-b2b1-3a57834133b5","trusted":true},"cell_type":"markdown","source":"### Most used top 10 Grapheme Roots in training set"},{"metadata":{"_uuid":"a2c8201f-37dc-4c05-8c62-bb74b7be8b61","_cell_guid":"606c23e3-0763-41a4-bcd5-f846661199af","trusted":true},"cell_type":"code","source":"top_10_roots = get_n(train_df_, 'grapheme_root', 10)\ntop_10_roots","execution_count":0,"outputs":[]},{"metadata":{"_uuid":"709237a6-d175-4435-b1a5-9e0f332f0cef","_cell_guid":"49b6a205-fdd0-445a-8581-022ffbbf83b0","trusted":true},"cell_type":"code","source":"f, ax = plt.subplots(2, 5, figsize=(16, 8))\nax = ax.flatten()\n\nfor i in range(10):\n    ax[i].imshow(image_from_char(top_10_roots['component'].iloc[i]), cmap='Greys')","execution_count":0,"outputs":[]},{"metadata":{"_uuid":"c7c31342-ed69-4d35-8b5e-86346a5d8760","_cell_guid":"beb4abc5-f290-4005-9504-130c14edd1cc","trusted":true},"cell_type":"markdown","source":"### Least used 10 Grapheme Roots in training set"},{"metadata":{"_uuid":"b46c9621-4b3f-42fd-93f7-4aed7b95f6aa","_cell_guid":"54607b39-a230-4741-ad92-3864a8c70cb9","trusted":true},"cell_type":"code","source":"bottom_10_roots = get_n(train_df_, 'grapheme_root', 10, False)\nbottom_10_roots","execution_count":0,"outputs":[]},{"metadata":{"_uuid":"94ba0410-e002-4e92-b167-cdf9d7f372dc","_cell_guid":"675e8a88-63e7-413b-bcfd-911f983eb497","trusted":true},"cell_type":"code","source":"f, ax = plt.subplots(2, 5, figsize=(16, 8))\nax = ax.flatten()\n\nfor i in range(10):\n    ax[i].imshow(image_from_char(bottom_10_roots['component'].iloc[i]), cmap='Greys')","execution_count":0,"outputs":[]},{"metadata":{"_uuid":"e214b2f8-a5a8-43a3-a50f-552256620ac4","_cell_guid":"03a4d88e-6143-416a-98b4-9591405a12b0","trusted":true},"cell_type":"markdown","source":"### Top 5 Vowel Diacritic in taining data"},{"metadata":{"_uuid":"8a534f3e-fbd3-4725-8bc6-22d6406514d7","_cell_guid":"edecf138-ea10-4139-999b-a41a515f5643","trusted":true},"cell_type":"code","source":"top_5_vowels = get_n(train_df_, 'vowel_diacritic', 5)\ntop_5_vowels","execution_count":0,"outputs":[]},{"metadata":{"_uuid":"ca4c033b-31dc-44b6-b1e4-81ee71a85462","_cell_guid":"36eef90b-300f-4c1e-91c0-11420963cb61","trusted":true},"cell_type":"code","source":"f, ax = plt.subplots(1, 5, figsize=(16, 8))\nax = ax.flatten()\n\nfor i in range(5):\n    ax[i].imshow(image_from_char(top_5_vowels['component'].iloc[i]), cmap='Greys')","execution_count":0,"outputs":[]},{"metadata":{"_uuid":"48b97f91-1421-4d5a-93a5-11866b287780","_cell_guid":"c7b1ce04-e48e-4f9c-8de7-5895364dfb21","trusted":true},"cell_type":"markdown","source":"### Top 5 Consonant Diacritic in training data"},{"metadata":{"_uuid":"b76b28a6-6e94-42cd-b9a1-7acc1f3d0297","_cell_guid":"1f0c0bcc-ad32-402b-89bf-2bb0db8b2608","trusted":true},"cell_type":"code","source":"top_5_consonants = get_n(train_df_, 'consonant_diacritic', 5)\ntop_5_consonants","execution_count":0,"outputs":[]},{"metadata":{"_uuid":"c8c78d9c-536e-4e66-ac65-628eb1ae8824","_cell_guid":"69c86362-3f04-47bd-9d34-a546a982f515","trusted":true},"cell_type":"code","source":"f, ax = plt.subplots(1, 5, figsize=(16, 8))\nax = ax.flatten()\n\nfor i in range(5):\n    ax[i].imshow(image_from_char(top_5_consonants['component'].iloc[i]), cmap='Greys')","execution_count":0,"outputs":[]},{"metadata":{"_uuid":"189a441c-703d-4b92-950b-b9c738235584","_cell_guid":"9e7474cc-1b0a-46ac-ae48-77c3875d1f82","trusted":true},"cell_type":"code","source":"train_df_ = train_df_.drop(['grapheme'], axis=1, inplace=False)","execution_count":0,"outputs":[]},{"metadata":{"_uuid":"4a292516-d426-42e8-9888-58157070cb41","_cell_guid":"4bae63f8-9e18-4122-9a84-b90555d6e47f","trusted":true},"cell_type":"code","source":"train_df_[['grapheme_root', 'vowel_diacritic', 'consonant_diacritic']] = train_df_[['grapheme_root', 'vowel_diacritic', 'consonant_diacritic']].astype('uint8')","execution_count":0,"outputs":[]},{"metadata":{"_uuid":"b9712611-f2e1-4454-8361-e15994a190cd","_cell_guid":"99f16b6f-871c-4087-b591-d3c1faaf8abc","trusted":true},"cell_type":"code","source":"IMG_SIZE=64\nN_CHANNELS=1","execution_count":0,"outputs":[]},{"metadata":{"_uuid":"6b295e34-8368-4a4c-9554-5d4a7452cf5b","_cell_guid":"68827ded-f37d-4445-9acd-a7374c4e5947","trusted":true},"cell_type":"markdown","source":"Let's apply some image processing (credits: [this kernel](https://www.kaggle.com/shawon10/bangla-graphemes-image-processing-deep-cnn)) while resizing the images, which will center crop the region of interest from the original images."},{"metadata":{"_uuid":"3ce1eb8f-0f3b-4dc6-ab4b-220c03b8b5cb","_cell_guid":"f82e41b3-444d-45d7-8cbc-3d19e10ea582","trusted":true},"cell_type":"code","source":"def resize(df, size=64, need_progress_bar=True):\n    resized = {}\n    resize_size=64\n    if need_progress_bar:\n        for i in tqdm(range(df.shape[0])):\n            image=df.loc[df.index[i]].values.reshape(137,236)\n            _, thresh = cv2.threshold(image, 30, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)\n            contours, _ = cv2.findContours(thresh,cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE)[-2:]\n\n            idx = 0 \n            ls_xmin = []\n            ls_ymin = []\n            ls_xmax = []\n            ls_ymax = []\n            for cnt in contours:\n                idx += 1\n                x,y,w,h = cv2.boundingRect(cnt)\n                ls_xmin.append(x)\n                ls_ymin.append(y)\n                ls_xmax.append(x + w)\n                ls_ymax.append(y + h)\n            xmin = min(ls_xmin)\n            ymin = min(ls_ymin)\n            xmax = max(ls_xmax)\n            ymax = max(ls_ymax)\n\n            roi = image[ymin:ymax,xmin:xmax]\n            resized_roi = cv2.resize(roi, (resize_size, resize_size),interpolation=cv2.INTER_AREA)\n            resized[df.index[i]] = resized_roi.reshape(-1)\n    else:\n        for i in range(df.shape[0]):\n            #image = cv2.resize(df.loc[df.index[i]].values.reshape(137,236),(size,size),None,fx=0.5,fy=0.5,interpolation=cv2.INTER_AREA)\n            image=df.loc[df.index[i]].values.reshape(137,236)\n            _, thresh = cv2.threshold(image, 30, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)\n            contours, _ = cv2.findContours(thresh,cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE)[-2:]\n\n            idx = 0 \n            ls_xmin = []\n            ls_ymin = []\n            ls_xmax = []\n            ls_ymax = []\n            for cnt in contours:\n                idx += 1\n                x,y,w,h = cv2.boundingRect(cnt)\n                ls_xmin.append(x)\n                ls_ymin.append(y)\n                ls_xmax.append(x + w)\n                ls_ymax.append(y + h)\n            xmin = min(ls_xmin)\n            ymin = min(ls_ymin)\n            xmax = max(ls_xmax)\n            ymax = max(ls_ymax)\n\n            roi = image[ymin:ymax,xmin:xmax]\n            resized_roi = cv2.resize(roi, (resize_size, resize_size),interpolation=cv2.INTER_AREA)\n            resized[df.index[i]] = resized_roi.reshape(-1)\n    resized = pd.DataFrame(resized).T\n    return resized","execution_count":0,"outputs":[]},{"metadata":{"_uuid":"5017abbd-3d4b-4720-9cad-0a3b70652235","_cell_guid":"81c70f68-dd44-4aa3-a447-5ad171a8cc78","trusted":true},"cell_type":"code","source":"def get_dummies(df):\n    cols = []\n    for col in df:\n        cols.append(pd.get_dummies(df[col].astype(str)))\n    return pd.concat(cols, axis=1)","execution_count":0,"outputs":[]},{"metadata":{"_uuid":"ecf165b2-cba9-49cf-bdb3-2001dbfee3cb","_cell_guid":"34c6e983-2ff8-4e57-b832-a381e87df57b","trusted":true},"cell_type":"markdown","source":"## Basic Model"},{"metadata":{"_uuid":"569f92ed-d5dc-405c-bdf9-b35d822fd5bf","_cell_guid":"b19c759a-5166-4609-8439-d2671b66b613","trusted":true},"cell_type":"code","source":"inputs = Input(shape = (IMG_SIZE, IMG_SIZE, 1))\n\nmodel = Conv2D(filters=32, kernel_size=(3, 3), padding='SAME', activation='relu', input_shape=(IMG_SIZE, IMG_SIZE, 1))(inputs)\nmodel = Conv2D(filters=32, kernel_size=(3, 3), padding='SAME', activation='relu')(model)\nmodel = Conv2D(filters=32, kernel_size=(3, 3), padding='SAME', activation='relu')(model)\nmodel = Conv2D(filters=32, kernel_size=(3, 3), padding='SAME', activation='relu')(model)\nmodel = BatchNormalization(momentum=0.15)(model)\nmodel = MaxPool2D(pool_size=(2, 2))(model)\nmodel = Conv2D(filters=32, kernel_size=(5, 5), padding='SAME', activation='relu')(model)\nmodel = Dropout(rate=0.3)(model)\n\nmodel = Conv2D(filters=64, kernel_size=(3, 3), padding='SAME', activation='relu')(model)\nmodel = Conv2D(filters=64, kernel_size=(3, 3), padding='SAME', activation='relu')(model)\nmodel = Conv2D(filters=64, kernel_size=(3, 3), padding='SAME', activation='relu')(model)\nmodel = Conv2D(filters=64, kernel_size=(3, 3), padding='SAME', activation='relu')(model)\nmodel = BatchNormalization(momentum=0.15)(model)\nmodel = MaxPool2D(pool_size=(2, 2))(model)\nmodel = Conv2D(filters=64, kernel_size=(5, 5), padding='SAME', activation='relu')(model)\nmodel = BatchNormalization(momentum=0.15)(model)\nmodel = Dropout(rate=0.3)(model)\n\nmodel = Conv2D(filters=128, kernel_size=(3, 3), padding='SAME', activation='relu')(model)\nmodel = Conv2D(filters=128, kernel_size=(3, 3), padding='SAME', activation='relu')(model)\nmodel = Conv2D(filters=128, kernel_size=(3, 3), padding='SAME', activation='relu')(model)\nmodel = Conv2D(filters=128, kernel_size=(3, 3), padding='SAME', activation='relu')(model)\nmodel = BatchNormalization(momentum=0.15)(model)\nmodel = MaxPool2D(pool_size=(2, 2))(model)\nmodel = Conv2D(filters=128, kernel_size=(5, 5), padding='SAME', activation='relu')(model)\nmodel = BatchNormalization(momentum=0.15)(model)\nmodel = Dropout(rate=0.3)(model)\n\nmodel = Conv2D(filters=256, kernel_size=(3, 3), padding='SAME', activation='relu')(model)\nmodel = Conv2D(filters=256, kernel_size=(3, 3), padding='SAME', activation='relu')(model)\nmodel = Conv2D(filters=256, kernel_size=(3, 3), padding='SAME', activation='relu')(model)\nmodel = Conv2D(filters=256, kernel_size=(3, 3), padding='SAME', activation='relu')(model)\nmodel = BatchNormalization(momentum=0.15)(model)\nmodel = MaxPool2D(pool_size=(2, 2))(model)\nmodel = Conv2D(filters=256, kernel_size=(5, 5), padding='SAME', activation='relu')(model)\nmodel = BatchNormalization(momentum=0.15)(model)\nmodel = Dropout(rate=0.3)(model)\n\nmodel = Flatten()(model)\nmodel = Dense(1024, activation = \"relu\")(model)\nmodel = Dropout(rate=0.3)(model)\ndense = Dense(512, activation = \"relu\")(model)\n\nhead_root = Dense(168, activation = 'softmax')(dense)\nhead_vowel = Dense(11, activation = 'softmax')(dense)\nhead_consonant = Dense(7, activation = 'softmax')(dense)\n\nmodel = Model(inputs=inputs, outputs=[head_root, head_vowel, head_consonant])","execution_count":0,"outputs":[]},{"metadata":{"_uuid":"3c9f10bb-8df2-4cd7-93d3-072cebc19be0","_cell_guid":"96b15795-8538-4e7c-b9a7-75a418d62b4b","trusted":true},"cell_type":"code","source":"model.summary()","execution_count":0,"outputs":[]},{"metadata":{"_uuid":"be6eb72e-0e5d-45d4-885a-7849cb004b09","_cell_guid":"18563cdd-8f73-438f-83de-18f49bddfb4e","trusted":true},"cell_type":"markdown","source":"Let's visualize the 3-tailed (3 output) CNN by plotting it."},{"metadata":{"_uuid":"4c8cc5bb-0781-4df9-8073-d640d46e9d4e","_cell_guid":"6f549dec-5b86-4dfb-a5a9-b7464d72c804","trusted":true},"cell_type":"code","source":"from keras.utils import plot_model\nplot_model(model, to_file='model.png')","execution_count":0,"outputs":[]},{"metadata":{"_uuid":"e5a3f8af-edf5-4679-ae24-20bd263d71fe","_cell_guid":"71257fda-614e-419d-bb06-e9467754f760","trusted":true},"cell_type":"code","source":"model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])","execution_count":0,"outputs":[]},{"metadata":{"_uuid":"b5c8ea9d-384a-48de-b12e-56e802d25113","_cell_guid":"9e60ba48-9eeb-4931-9ef3-a8f6d14d04b5","trusted":true},"cell_type":"code","source":"# Set a learning rate annealer. Learning rate will be half after 3 epochs if accuracy is not increased\nlearning_rate_reduction_root = ReduceLROnPlateau(monitor='dense_3_accuracy', \n                                            patience=3, \n                                            verbose=1,\n                                            factor=0.5, \n                                            min_lr=0.00001)\nlearning_rate_reduction_vowel = ReduceLROnPlateau(monitor='dense_4_accuracy', \n                                            patience=3, \n                                            verbose=1,\n                                            factor=0.5, \n                                            min_lr=0.00001)\nlearning_rate_reduction_consonant = ReduceLROnPlateau(monitor='dense_5_accuracy', \n                                            patience=3, \n                                            verbose=1,\n                                            factor=0.5, \n                                            min_lr=0.00001)","execution_count":0,"outputs":[]},{"metadata":{"_uuid":"6b77535d-a005-4f12-9285-aaa3f0b9ffca","_cell_guid":"0a9c9b10-4e48-426a-8644-abd8f737ff34","trusted":true},"cell_type":"code","source":"batch_size = 256\nepochs = 30","execution_count":0,"outputs":[]},{"metadata":{"_uuid":"6196c123-ca6e-4ba1-a405-f6379d698d67","_cell_guid":"94782d7d-534e-4f9e-b2ff-35e6228484be","trusted":true},"cell_type":"code","source":"class MultiOutputDataGenerator(keras.preprocessing.image.ImageDataGenerator):\n\n    def flow(self,\n             x,\n             y=None,\n             batch_size=32,\n             shuffle=True,\n             sample_weight=None,\n             seed=None,\n             save_to_dir=None,\n             save_prefix='',\n             save_format='png',\n             subset=None):\n\n        targets = None\n        target_lengths = {}\n        ordered_outputs = []\n        for output, target in y.items():\n            if targets is None:\n                targets = target\n            else:\n                targets = np.concatenate((targets, target), axis=1)\n            target_lengths[output] = target.shape[1]\n            ordered_outputs.append(output)\n\n\n        for flowx, flowy in super().flow(x, targets, batch_size=batch_size,\n                                         shuffle=shuffle):\n            target_dict = {}\n            i = 0\n            for output in ordered_outputs:\n                target_length = target_lengths[output]\n                target_dict[output] = flowy[:, i: i + target_length]\n                i += target_length\n\n            yield flowx, target_dict","execution_count":0,"outputs":[]},{"metadata":{"_uuid":"74820526-6445-4353-9a38-605b24c7ec2a","_cell_guid":"23424865-3cb8-4fb9-905e-b02f301b16d1","trusted":true},"cell_type":"code","source":"HEIGHT = 137\nWIDTH = 236","execution_count":0,"outputs":[]},{"metadata":{"_uuid":"15773fe3-0118-476b-b06c-30e8f83825fb","_cell_guid":"c40605c5-e981-4675-9c59-6c5f353db11a","trusted":true},"cell_type":"markdown","source":"### Training loop"},{"metadata":{"_uuid":"e7c01d17-bb56-4a3e-9ece-3cdd5cdc4440","_cell_guid":"6c76d128-9c8b-4f85-8c2e-276b4f8506d7","trusted":true},"cell_type":"code","source":"histories = []\nfor i in range(4):\n    train_df = pd.merge(pd.read_parquet(f'/kaggle/input/bengaliai-cv19/train_image_data_{i}.parquet'), train_df_, on='image_id').drop(['image_id'], axis=1)\n    \n    # Visualize few samples of current training dataset\n    fig, ax = plt.subplots(nrows=3, ncols=4, figsize=(16, 8))\n    count=0\n    for row in ax:\n        for col in row:\n            col.imshow(resize(train_df.drop(['grapheme_root', 'vowel_diacritic', 'consonant_diacritic'], axis=1).iloc[[count]], need_progress_bar=False).values.reshape(-1).reshape(IMG_SIZE, IMG_SIZE).astype(np.float64))\n            count += 1\n    plt.show()\n    \n    X_train = train_df.drop(['grapheme_root', 'vowel_diacritic', 'consonant_diacritic'], axis=1)\n    X_train = resize(X_train)/255\n    \n    # CNN takes images in shape `(batch_size, h, w, channels)`, so reshape the images\n    X_train = X_train.values.reshape(-1, IMG_SIZE, IMG_SIZE, N_CHANNELS)\n    \n    Y_train_root = pd.get_dummies(train_df['grapheme_root']).values\n    Y_train_vowel = pd.get_dummies(train_df['vowel_diacritic']).values\n    Y_train_consonant = pd.get_dummies(train_df['consonant_diacritic']).values\n\n    print(f'Training images: {X_train.shape}')\n    print(f'Training labels root: {Y_train_root.shape}')\n    print(f'Training labels vowel: {Y_train_vowel.shape}')\n    print(f'Training labels consonants: {Y_train_consonant.shape}')\n\n    # Divide the data into training and validation set\n    x_train, x_test, y_train_root, y_test_root, y_train_vowel, y_test_vowel, y_train_consonant, y_test_consonant = train_test_split(X_train, Y_train_root, Y_train_vowel, Y_train_consonant, test_size=0.08, random_state=666)\n    del train_df\n    del X_train\n    del Y_train_root, Y_train_vowel, Y_train_consonant\n\n    # Data augmentation for creating more training data\n    datagen = MultiOutputDataGenerator(\n        featurewise_center=False,  # set input mean to 0 over the dataset\n        samplewise_center=False,  # set each sample mean to 0\n        featurewise_std_normalization=False,  # divide inputs by std of the dataset\n        samplewise_std_normalization=False,  # divide each input by its std\n        zca_whitening=False,  # apply ZCA whitening\n        rotation_range=8,  # randomly rotate images in the range (degrees, 0 to 180)\n        zoom_range = 0.15, # Randomly zoom image \n        width_shift_range=0.15,  # randomly shift images horizontally (fraction of total width)\n        height_shift_range=0.15,  # randomly shift images vertically (fraction of total height)\n        horizontal_flip=False,  # randomly flip images\n        vertical_flip=False)  # randomly flip images\n\n\n    # This will just calculate parameters required to augment the given data. This won't perform any augmentations\n    datagen.fit(x_train)\n\n    # Fit the model\n    history = model.fit_generator(datagen.flow(x_train, {'dense_3': y_train_root, 'dense_4': y_train_vowel, 'dense_5': y_train_consonant}, batch_size=batch_size),\n                              epochs = epochs, validation_data = (x_test, [y_test_root, y_test_vowel, y_test_consonant]), \n                              steps_per_epoch=x_train.shape[0] // batch_size, \n                              callbacks=[learning_rate_reduction_root, learning_rate_reduction_vowel, learning_rate_reduction_consonant])\n\n    histories.append(history)\n    \n    # Delete to reduce memory usage\n    del x_train\n    del x_test\n    del y_train_root\n    del y_test_root\n    del y_train_vowel\n    del y_test_vowel\n    del y_train_consonant\n    del y_test_consonant\n    gc.collect()","execution_count":0,"outputs":[]},{"metadata":{"_uuid":"a88c919f-995b-4c91-84f2-78af82e5c153","_cell_guid":"46ab65f0-b6ea-487c-917b-44a0309011a8","trusted":true},"cell_type":"code","source":"%matplotlib inline\ndef plot_loss(his, epoch, title):\n    plt.style.use('ggplot')\n    plt.figure()\n    plt.plot(np.arange(0, epoch), his.history['loss'], label='train_loss')\n    plt.plot(np.arange(0, epoch), his.history['dense_3_loss'], label='train_root_loss')\n    plt.plot(np.arange(0, epoch), his.history['dense_4_loss'], label='train_vowel_loss')\n    plt.plot(np.arange(0, epoch), his.history['dense_5_loss'], label='train_consonant_loss')\n    \n    plt.plot(np.arange(0, epoch), his.history['val_dense_3_loss'], label='val_train_root_loss')\n    plt.plot(np.arange(0, epoch), his.history['val_dense_4_loss'], label='val_train_vowel_loss')\n    plt.plot(np.arange(0, epoch), his.history['val_dense_5_loss'], label='val_train_consonant_loss')\n    \n    plt.title(title)\n    plt.xlabel('Epoch #')\n    plt.ylabel('Loss')\n    plt.legend(loc='upper right')\n    plt.show()\n\ndef plot_acc(his, epoch, title):\n    plt.style.use('ggplot')\n    plt.figure()\n    plt.plot(np.arange(0, epoch), his.history['dense_3_accuracy'], label='train_root_acc')\n    plt.plot(np.arange(0, epoch), his.history['dense_4_accuracy'], label='train_vowel_accuracy')\n    plt.plot(np.arange(0, epoch), his.history['dense_5_accuracy'], label='train_consonant_accuracy')\n    \n    plt.plot(np.arange(0, epoch), his.history['val_dense_3_accuracy'], label='val_root_acc')\n    plt.plot(np.arange(0, epoch), his.history['val_dense_4_accuracy'], label='val_vowel_accuracy')\n    plt.plot(np.arange(0, epoch), his.history['val_dense_5_accuracy'], label='val_consonant_accuracy')\n    plt.title(title)\n    plt.xlabel('Epoch #')\n    plt.ylabel('Accuracy')\n    plt.legend(loc='upper right')\n    plt.show()","execution_count":0,"outputs":[]},{"metadata":{"_uuid":"90660262-777a-495b-99b7-beff6356a317","_cell_guid":"6aaffddc-9790-430a-ba16-a967d38d85bf","trusted":true},"cell_type":"code","source":"for dataset in range(4):\n    plot_loss(histories[dataset], epochs, f'Training Dataset: {dataset}')\n    plot_acc(histories[dataset], epochs, f'Training Dataset: {dataset}')","execution_count":0,"outputs":[]},{"metadata":{"_uuid":"8e1c5e5c-5b98-4ad2-8ec6-da2e314f4fa4","_cell_guid":"ab289db9-0481-49e4-9711-dddaecb2f40e","trusted":true},"cell_type":"code","source":"del histories\ngc.collect()","execution_count":0,"outputs":[]},{"metadata":{"_uuid":"4b7fa91b-140a-4e88-a647-12c93747e95b","_cell_guid":"418e62f2-0723-4800-b551-8d4c263bd006","trusted":true},"cell_type":"code","source":"preds_dict = {\n    'grapheme_root': [],\n    'vowel_diacritic': [],\n    'consonant_diacritic': []\n}","execution_count":0,"outputs":[]},{"metadata":{"_uuid":"c124ea43-dc24-482b-be0c-ac38e8babb4b","_cell_guid":"5df84984-b22c-40ac-ac2c-fbbac20a9768","trusted":true},"cell_type":"code","source":"components = ['consonant_diacritic', 'grapheme_root', 'vowel_diacritic']\ntarget=[] # model predictions placeholder\nrow_id=[] # row_id place holder\nfor i in range(4):\n    df_test_img = pd.read_parquet('/kaggle/input/bengaliai-cv19/test_image_data_{}.parquet'.format(i)) \n    df_test_img.set_index('image_id', inplace=True)\n\n    X_test = resize(df_test_img, need_progress_bar=False)/255\n    X_test = X_test.values.reshape(-1, IMG_SIZE, IMG_SIZE, N_CHANNELS)\n    \n    preds = model.predict(X_test)\n\n    for i, p in enumerate(preds_dict):\n        preds_dict[p] = np.argmax(preds[i], axis=1)\n\n    for k,id in enumerate(df_test_img.index.values):  \n        for i,comp in enumerate(components):\n            id_sample=id+'_'+comp\n            row_id.append(id_sample)\n            target.append(preds_dict[comp][k])\n    del df_test_img\n    del X_test\n    gc.collect()\n\ndf_sample = pd.DataFrame(\n    {\n        'row_id': row_id,\n        'target':target\n    },\n    columns = ['row_id','target'] \n)\ndf_sample.to_csv('submission.csv',index=False)\ndf_sample.head()","execution_count":0,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.4","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}