{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"gpu","dataSources":[{"sourceId":14897,"databundleVersionId":1020216,"sourceType":"competition"},{"sourceId":853555,"sourceType":"datasetVersion","datasetId":451905},{"sourceId":222472266,"sourceType":"kernelVersion"}],"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"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# https://www.kaggle.com/competitions/bengaliai-cv19/submissions\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\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))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session\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 joblib\nfrom tqdm import tqdm\nimport torch\nimport warnings\nwarnings.filterwarnings('ignore')\nimport torch.nn as nn\nimport albumentations as A\nimport albumentations.pytorch\n\nfrom torch.utils.data import Dataset\nfrom sklearn.metrics import recall_score\nfrom torchvision import models","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2025-02-15T08:40:14.860487Z","iopub.execute_input":"2025-02-15T08:40:14.860838Z","iopub.status.idle":"2025-02-15T08:40:14.873327Z","shell.execute_reply.started":"2025-02-15T08:40:14.860804Z","shell.execute_reply":"2025-02-15T08:40:14.872589Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model = torch.load('/kaggle/input/bengali-trial/model.pth', weights_only=False)","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2025-02-15T08:40:14.874383Z","iopub.execute_input":"2025-02-15T08:40:14.874581Z","iopub.status.idle":"2025-02-15T08:40:14.959936Z","shell.execute_reply.started":"2025-02-15T08:40:14.874564Z","shell.execute_reply":"2025-02-15T08:40:14.959263Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\nclass GraphemeDataset(Dataset):\n    def __init__(self,df, transform, _type='train'):\n        self.df = df\n        self.transform = transform\n    def __len__(self):\n        return len(self.df)\n    def __getitem__(self,idx):\n        image = self.df.iloc[idx][1:].values.reshape(137, 236).astype(np.uint8)\n        image = 255 - image\n        image = image[:, :, np.newaxis]\n        image = np.repeat(image, 3, 2)\n        if self.transform is not None:\n            image = self.transform(image=image)['image']\n        return image, self.df.iloc[idx][0]\n\nvalid_augmentation = A.Compose([\n    A.Normalize(normalization=\"min_max\"),\n    A.pytorch.transforms.ToTensorV2()\n])","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2025-02-15T08:40:14.961373Z","iopub.execute_input":"2025-02-15T08:40:14.961581Z","iopub.status.idle":"2025-02-15T08:40:14.967506Z","shell.execute_reply.started":"2025-02-15T08:40:14.961564Z","shell.execute_reply":"2025-02-15T08:40:14.966553Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"n_grapheme=168\nn_vowel=11\nn_consonant=7\nmodel.eval()\ntest_data = ['test_image_data_0.parquet','test_image_data_1.parquet','test_image_data_2.parquet','test_image_data_3.parquet']\npredictions = []\nrow_ids = []\nbatch_size=256\nfor fname in test_data:\n    data = pd.read_parquet(f'/kaggle/input/bengaliai-cv19/{fname}')\n    test_image = GraphemeDataset(data, valid_augmentation)\n    test_loader = torch.utils.data.DataLoader(test_image,batch_size=256,shuffle=False, pin_memory = True, num_workers = 4)\n    with torch.no_grad():\n        for inputs, names in tqdm(test_loader,total=len(test_loader)):\n            for name in names:\n                row_ids += [f\"{name}_grapheme_root\", f\"{name}_vowel_diacritic\", f\"{name}_consonant_diacritic\"]\n            inputs = inputs.cuda()\n            logits = model(inputs)\n            logits_split = torch.split(logits, [n_grapheme, n_vowel, n_consonant], dim=1)\n\n            grapheme = logits_split[0].cpu().argmax(dim=1).data.numpy()\n            vowel = logits_split[1].cpu().argmax(dim=1).data.numpy()\n            cons = logits_split[2].cpu().argmax(dim=1).data.numpy()\n            # print(grapheme)\n            # print(vowel)\n            # print(cons)\n            # print(\"-----\")\n            # for i in range(len(cons)):\n            #     if cons[i] == 7:\n            #         cons[i] = 2\n\n            predictions.append(np.stack([grapheme, vowel, cons], axis=1))\n            ","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2025-02-15T09:43:10.702397Z","iopub.execute_input":"2025-02-15T09:43:10.702688Z","iopub.status.idle":"2025-02-15T09:43:18.667850Z","shell.execute_reply.started":"2025-02-15T09:43:10.702665Z","shell.execute_reply":"2025-02-15T09:43:18.666981Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"predictions = np.concatenate(predictions, axis=0).flatten()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-15T09:43:20.565142Z","iopub.execute_input":"2025-02-15T09:43:20.565449Z","iopub.status.idle":"2025-02-15T09:43:20.569974Z","shell.execute_reply.started":"2025-02-15T09:43:20.565424Z","shell.execute_reply":"2025-02-15T09:43:20.569067Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"submission = pd.DataFrame({'row_id':row_ids,'target':predictions},columns=['row_id','target'])\nsubmission.to_csv('submission.csv',index=False)\nsubmission","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2025-02-15T09:43:21.299731Z","iopub.execute_input":"2025-02-15T09:43:21.300046Z","iopub.status.idle":"2025-02-15T09:43:21.316922Z","shell.execute_reply.started":"2025-02-15T09:43:21.300022Z","shell.execute_reply":"2025-02-15T09:43:21.316108Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}