{"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_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import glob\nimport torch\nimport albumentations\nimport pandas as pd\nimport numpy as np\n\nfrom tqdm import tqdm\nfrom PIL import Image\nimport joblib\nimport torch.nn as nn\nfrom torch.nn import functional as F","metadata":{"execution":{"iopub.status.busy":"2022-10-08T10:55:14.677834Z","iopub.execute_input":"2022-10-08T10:55:14.678248Z","iopub.status.idle":"2022-10-08T10:55:14.684518Z","shell.execute_reply.started":"2022-10-08T10:55:14.678215Z","shell.execute_reply":"2022-10-08T10:55:14.683111Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"MODEL_MEAN = (0.485, 0.456, 0.406)\nMODEL_STD = (0.229, 0.224, 0.225)\nIMG_HEIGHT = 137\nIMG_WIDTH = 236\nDEVICE=\"cuda\"","metadata":{"execution":{"iopub.status.busy":"2022-10-08T10:55:14.689949Z","iopub.execute_input":"2022-10-08T10:55:14.690258Z","iopub.status.idle":"2022-10-08T10:55:14.698597Z","shell.execute_reply.started":"2022-10-08T10:55:14.690221Z","shell.execute_reply":"2022-10-08T10:55:14.697647Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!mkdir ./image_pickles\ntest_img_ids = []\nfiles = glob.glob(\"../input/bengaliai-cv19/test_*.parquet\")\n\nfor f in files:\n    df = pd.read_parquet(f)\n    image_ids = df.image_id.values\n    test_img_ids.extend(image_ids)\n    df = df.drop(\"image_id\", axis=1)\n    image_array = df.values\n    for j, image_id in tqdm(enumerate(image_ids), total=len(image_ids)):\n        joblib.dump(image_array[j, :], f\"./image_pickles/{image_id}.pkl\")\n       \nfinal_test_df = pd.DataFrame(test_img_ids, columns =['image_id'])\ndel df, image_ids, image_array, files, test_img_ids","metadata":{"execution":{"iopub.status.busy":"2022-10-08T10:55:14.700078Z","iopub.execute_input":"2022-10-08T10:55:14.700871Z","iopub.status.idle":"2022-10-08T10:55:23.868912Z","shell.execute_reply.started":"2022-10-08T10:55:14.700835Z","shell.execute_reply":"2022-10-08T10:55:23.867772Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class BengaliDatasetTest:\n    def __init__(self, df, img_height, img_width, mean, std):\n        \n        self.image_ids = df.image_id.values\n        self.img_arr = df.iloc[:, 1:].values\n\n        self.aug = albumentations.Compose([\n            albumentations.Resize(img_height, img_width, always_apply=True),\n            albumentations.Normalize(mean, std, always_apply=True)\n        ])\n\n\n    def __len__(self):\n        return len(self.image_ids)\n    \n    def __getitem__(self, item):\n\n        img_id = self.image_ids[item]\n        image = joblib.load(f\"./image_pickles/{self.image_ids[item]}.pkl\")\n        image = image.reshape(137, 236).astype(float)\n        image = Image.fromarray(image).convert(\"RGB\")\n        image = self.aug(image=np.array(image))[\"image\"]\n        image = np.transpose(image, (2, 0, 1)).astype(np.float32)\n        \n\n        return {\n            \"image\": torch.tensor(image, dtype=torch.float),\n            \"image_id\": img_id\n        }","metadata":{"execution":{"iopub.status.busy":"2022-10-08T10:55:23.871328Z","iopub.execute_input":"2022-10-08T10:55:23.871957Z","iopub.status.idle":"2022-10-08T10:55:23.880816Z","shell.execute_reply.started":"2022-10-08T10:55:23.871901Z","shell.execute_reply":"2022-10-08T10:55:23.879883Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# import matplotlib.pyplot as plt\n# %matplotlib inline\n\n# dataset = BengaliDatasetTest ( df = final_test_df,\n#                                 img_height=IMG_HEIGHT,\n#                                 img_width=IMG_WIDTH,\n#                                 mean=MODEL_MEAN,\n#                                 std=MODEL_STD,\n#                                 )\n\n# for idx in range(1,5): \n#     img = dataset[idx][\"image\"]\n# #     print(dataset[idx][\"grapheme_root\"])\n# #     print(dataset[idx][\"vowel_diacritic\"])\n# #     print(dataset[idx][\"consonant_diacritic\"])\n#     npimg = img.numpy()\n#     print(np.mean(npimg))\n#     plt.figure()\n#     plt.imshow(np.transpose(npimg,(1,2,0)))","metadata":{"execution":{"iopub.status.busy":"2022-10-08T10:55:23.882007Z","iopub.execute_input":"2022-10-08T10:55:23.882760Z","iopub.status.idle":"2022-10-08T10:55:23.895807Z","shell.execute_reply.started":"2022-10-08T10:55:23.882734Z","shell.execute_reply":"2022-10-08T10:55:23.894878Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import sys\nsys.path.append(\"../input/effnet\")\nfrom efficientnet_pytorch import EfficientNet","metadata":{"execution":{"iopub.status.busy":"2022-10-08T10:55:23.898475Z","iopub.execute_input":"2022-10-08T10:55:23.898909Z","iopub.status.idle":"2022-10-08T10:55:23.908205Z","shell.execute_reply.started":"2022-10-08T10:55:23.898876Z","shell.execute_reply":"2022-10-08T10:55:23.907110Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class EfficientNet_b0(nn.Module):\n\n  def __init__(self,pretrained):\n    super(EfficientNet_b0,self).__init__()\n    if pretrained is True:\n      self.model = EfficientNet.from_pretrained('efficientnet-b0')\n    else:\n      self.model = EfficientNet.from_name('efficientnet-b0')\n    self.l0 = nn.Linear(1280,168)\n    self.l1 = nn.Linear(1280,11)\n    self.l2 = nn.Linear(1280,8)\n  \n  def forward(self,inputs):\n    bs,_,_,_=inputs.shape\n    x=self.model.extract_features(inputs)\n    x = F.adaptive_avg_pool2d(x,1).reshape(bs,-1)\n    l0 = self.l0(x)\n    l1 = self.l1(x)\n    l2 = self.l2(x)\n    return l0,l1,l2","metadata":{"execution":{"iopub.status.busy":"2022-10-08T10:55:23.909740Z","iopub.execute_input":"2022-10-08T10:55:23.910466Z","iopub.status.idle":"2022-10-08T10:55:23.920342Z","shell.execute_reply.started":"2022-10-08T10:55:23.910397Z","shell.execute_reply":"2022-10-08T10:55:23.919506Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def model_predict():\n    g_pred, v_pred, c_pred = [], [], []\n    img_ids_list = []\n\n    dataset = BengaliDatasetTest(df=final_test_df,\n                                img_height=IMG_HEIGHT,\n                                img_width=IMG_WIDTH,\n                                mean=MODEL_MEAN,\n                                std=MODEL_STD)\n\n    data_loader = torch.utils.data.DataLoader(\n        dataset=dataset,\n        batch_size= TEST_BATCH_SIZE,\n        shuffle=False,\n    )\n\n    for bi, d in enumerate(data_loader):\n        image = d[\"image\"]\n        img_id = d[\"image_id\"]\n        image = image.to(DEVICE, dtype=torch.float)\n\n        g, v, c = model(image)\n\n        for ii, imid in enumerate(img_id):\n            g_pred.append(g[ii].cpu().detach().numpy())\n            v_pred.append(v[ii].cpu().detach().numpy())\n            c_pred.append(c[ii].cpu().detach().numpy())\n            img_ids_list.append(imid)\n        \n    return g_pred, v_pred, c_pred, img_ids_list","metadata":{"execution":{"iopub.status.busy":"2022-10-08T10:55:23.921877Z","iopub.execute_input":"2022-10-08T10:55:23.922280Z","iopub.status.idle":"2022-10-08T10:55:23.935263Z","shell.execute_reply.started":"2022-10-08T10:55:23.922247Z","shell.execute_reply":"2022-10-08T10:55:23.934380Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = EfficientNet_b0(pretrained=False)\nTEST_BATCH_SIZE = 8\n\nfinal_g_pred = []\nfinal_v_pred = []\nfinal_c_pred = []\nfinal_img_ids = []\n\nfor i in range(2):\n    checkpoint = torch.load(f\"../input/effnetb0/checkpoint_efficientnet-b0_fold_{i}.pth\")\n    model.load_state_dict(checkpoint['model'])\n    model.to(DEVICE)\n    model.eval()\n    g_pred, v_pred, c_pred, img_ids_list = model_predict()\n    \n    final_g_pred.append(g_pred)\n    final_v_pred.append(v_pred)\n    final_c_pred.append(c_pred)\n    if i == 0:\n        final_img_ids.extend(img_ids_list)\n        \ndel model, checkpoint, g_pred, v_pred, c_pred","metadata":{"execution":{"iopub.status.busy":"2022-10-08T10:55:23.936404Z","iopub.execute_input":"2022-10-08T10:55:23.937472Z","iopub.status.idle":"2022-10-08T10:55:24.520521Z","shell.execute_reply.started":"2022-10-08T10:55:23.937438Z","shell.execute_reply":"2022-10-08T10:55:24.519535Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"final_g = np.argmax(np.mean(np.array(final_g_pred), axis=0), axis=1)\nfinal_v = np.argmax(np.mean(np.array(final_v_pred), axis=0), axis=1)\nfinal_c = np.argmax(np.mean(np.array(final_c_pred), axis=0), axis=1)\ndel final_g_pred, final_v_pred, final_c_pred\n\npredictions = []\nfor ii, imid in enumerate(final_img_ids):\n    predictions.append((f\"{imid}_grapheme_root\", final_g[ii]))\n    predictions.append((f\"{imid}_vowel_diacritic\", final_v[ii]))\n    predictions.append((f\"{imid}_consonant_diacritic\", final_c[ii]))\n    \nsub = pd.DataFrame(predictions, columns=[\"row_id\", \"target\"])\ndel predictions\nsub.to_csv(\"submission.csv\", index=False)\ndel sub","metadata":{"execution":{"iopub.status.busy":"2022-10-08T10:55:24.522021Z","iopub.execute_input":"2022-10-08T10:55:24.522390Z","iopub.status.idle":"2022-10-08T10:55:24.534337Z","shell.execute_reply.started":"2022-10-08T10:55:24.522351Z","shell.execute_reply":"2022-10-08T10:55:24.533193Z"},"trusted":true},"execution_count":null,"outputs":[]}]}