{"cells":[{"metadata":{"trusted":true},"cell_type":"code","source":"import os\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport numpy as np\nfrom sklearn.model_selection import train_test_split\nimport keras\nfrom keras.preprocessing.image import ImageDataGenerator\nfrom keras.layers import Conv2D, MaxPooling2D, Input, Dense, Dropout, GlobalAveragePooling2D\nfrom keras.models import Model, Sequential\nfrom keras.applications.vgg19 import VGG19 ","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"base_dir = '/kaggle/input/bengaliai-cv19'\n# for f in os.listdir(base_dir):\n#     print(f)\ntrain_folders = sorted([os.path.join(base_dir, f) for f in os.listdir(base_dir) if f.startswith('train_image_data')])\ntrain_csv = os.path.join(base_dir, 'train.csv')\nclass_map = os.path.join(base_dir, 'class_map.csv')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_df = pd.read_csv(train_csv)\ndf_class = pd.read_csv(class_map)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# grapheme, g_counts = np.unique(df['grapheme_root'], return_counts=True)\n# vowel, v_counts = np.unique(df['vowel_diacritic'], return_counts=True)\n# consonant, c_counts = np.unique(df['consonant_diacritic'], return_counts=True)\n# fig = plt.figure(figsize=(20, 20))\n# plt.scatter(consonant, c_counts, s=c_counts, c=c_counts)\n# plt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df0 = pd.read_parquet(train_folders[0])\ndf1 = pd.read_parquet(train_folders[1])\ndf2 = pd.read_parquet(train_folders[2])\ndf3 = pd.read_parquet(train_folders[3])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df0 = df0.iloc[:, 1:]\ndf1 = df1.iloc[:, 1:]\ndf2 = df2.iloc[:, 1:]\ndf3 = df3.iloc[:, 1:]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df = pd.concat([df0, df1, df2, df3])\ndel [df0, df1, df2, df3]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"WIDTH = 137\nHEIGHT = 236\nBATCH_SIZE = 32","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"class BengaliGenerator(keras.utils.Sequence):\n    def __init__(self ,data, batch_size, dim):\n        self.data = data\n        self.labels1 = pd.get_dummies(data['grapheme_root'])\n        self.labels2 = pd.get_dummies(data['vowel_diacritic'])\n        self.labels3 = pd.get_dummies(data['consonant_diacritic'])\n        self.batch_size = batch_size\n        self.dim = dim\n        self.list_ids = self.data.index.values\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,idx):\n        imgs = df.iloc[idx*self.batch_size: (idx+1)*self.batch_size].values.reshape(-1, WIDTH, HEIGHT)\n        \n#         imgs = np.expand_dims(imgs, axis=3)\n#         for i in range(len(imgs)):\n#             for j in range(1, 3):\n#                 imgs[i, :, :, j] = imgs[i, :, :, 0]\n\n        imgs = np.repeat(imgs[..., np.newaxis], 3, -1)\n        \n        labels0 = pd.get_dummies(self.data['grapheme_root'])\n        labels1 = pd.get_dummies(self.data['vowel_diacritic'])\n        labels2 = pd.get_dummies(self.data['consonant_diacritic'])\n        y0 = labels0.iloc[idx*self.batch_size: (idx+1)*self.batch_size].values\n        del labels0\n        y1 = labels1.iloc[idx*self.batch_size: (idx+1)*self.batch_size].values\n        del labels1\n        y2 = labels2.iloc[idx*self.batch_size: (idx+1)*self.batch_size].values\n        del labels2\n\n        return imgs, [y0, y1, y2]\n    \n    def on_epoch_end(self):\n        self.indexes = np.arange(len(self.list_ids))\n#         if self.shuffle:\n#             np.random.shuffle(self.indexes)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train, val = train_test_split(train_df, test_size = 0.2, random_state = 2019)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_gen = BengaliGenerator(train, BATCH_SIZE, (WIDTH, HEIGHT ))\nval_gen = BengaliGenerator(val, BATCH_SIZE, (WIDTH, HEIGHT))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"conv_base = VGG19(include_top=False, weights='imagenet', input_shape=(WIDTH, HEIGHT, 3))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"for layer in conv_base.layers[:-3]:\n    layer.trainable = False","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# conv_base.summary()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"inp = Input(shape = (WIDTH, HEIGHT, 3))\noutput = conv_base(inp)\nx = GlobalAveragePooling2D()(output)\nout1 = Dense(168, activation = 'softmax')(x)\nout2 = Dense(11, activation = 'softmax')(x)\nout3 = Dense(7, activation = 'softmax')(x)\n    \nmodel = Model(inputs = inp, outputs = [out1,out2,out3])\nmodel.compile(loss='categorical_crossentropy', metrics=['acc'], optimizer='adam')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# history = model.fit_generator(train_gen, epochs=10, steps_per_epoch=len(train) / BATCH_SIZE)\n# #                               ,validation_data=val_gen, validation_steps=len(val) / BATCH_SIZE)","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":1}