{"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-21T11:09:46.764402Z","iopub.execute_input":"2022-10-21T11:09:46.765204Z","iopub.status.idle":"2022-10-21T11:09:51.770267Z","shell.execute_reply.started":"2022-10-21T11:09:46.765091Z","shell.execute_reply":"2022-10-21T11:09:51.769241Z"},"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-21T11:09:51.775140Z","iopub.execute_input":"2022-10-21T11:09:51.776301Z","iopub.status.idle":"2022-10-21T11:09:51.787142Z","shell.execute_reply.started":"2022-10-21T11:09:51.776253Z","shell.execute_reply":"2022-10-21T11:09:51.785680Z"},"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-21T11:09:51.789988Z","iopub.execute_input":"2022-10-21T11:09:51.791199Z","iopub.status.idle":"2022-10-21T11:10:02.620541Z","shell.execute_reply.started":"2022-10-21T11:09:51.791154Z","shell.execute_reply":"2022-10-21T11:10:02.619360Z"},"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-21T11:10:02.626907Z","iopub.execute_input":"2022-10-21T11:10:02.627331Z","iopub.status.idle":"2022-10-21T11:10:02.645847Z","shell.execute_reply.started":"2022-10-21T11:10:02.627291Z","shell.execute_reply":"2022-10-21T11:10:02.643941Z"},"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-21T11:10:02.651788Z","iopub.execute_input":"2022-10-21T11:10:02.652674Z","iopub.status.idle":"2022-10-21T11:10:02.660543Z","shell.execute_reply.started":"2022-10-21T11:10:02.652579Z","shell.execute_reply":"2022-10-21T11:10:02.659428Z"},"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-21T11:10:02.662506Z","iopub.execute_input":"2022-10-21T11:10:02.663954Z","iopub.status.idle":"2022-10-21T11:10:02.707789Z","shell.execute_reply.started":"2022-10-21T11:10:02.663913Z","shell.execute_reply":"2022-10-21T11:10:02.706569Z"},"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-21T11:10:02.712931Z","iopub.execute_input":"2022-10-21T11:10:02.713719Z","iopub.status.idle":"2022-10-21T11:10:02.732173Z","shell.execute_reply.started":"2022-10-21T11:10:02.713673Z","shell.execute_reply":"2022-10-21T11:10:02.729955Z"},"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-21T11:10:02.734166Z","iopub.execute_input":"2022-10-21T11:10:02.735120Z","iopub.status.idle":"2022-10-21T11:10:02.751047Z","shell.execute_reply.started":"2022-10-21T11:10:02.735083Z","shell.execute_reply":"2022-10-21T11:10:02.750049Z"},"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-21T11:10:02.753038Z","iopub.execute_input":"2022-10-21T11:10:02.753447Z","iopub.status.idle":"2022-10-21T11:10:14.623529Z","shell.execute_reply.started":"2022-10-21T11:10:02.753411Z","shell.execute_reply":"2022-10-21T11:10:14.622392Z"},"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-21T11:10:14.627793Z","iopub.execute_input":"2022-10-21T11:10:14.628154Z","iopub.status.idle":"2022-10-21T11:10:14.644997Z","shell.execute_reply.started":"2022-10-21T11:10:14.628123Z","shell.execute_reply":"2022-10-21T11:10:14.643358Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}