{"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 numpy as np\nimport pandas as pd\nimport os\nimport gc\nimport tifffile\nimport cv2\nfrom tqdm import tqdm\n# import albumentations as A\n# from albumentations.pytorch import ToTensorV2\nimport torch\nfrom torch import nn\nfrom torch.utils.data import DataLoader, Dataset","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-10-05T18:49:15.182592Z","iopub.execute_input":"2022-10-05T18:49:15.183Z","iopub.status.idle":"2022-10-05T18:49:17.519422Z","shell.execute_reply.started":"2022-10-05T18:49:15.182907Z","shell.execute_reply":"2022-10-05T18:49:17.518357Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df = pd.read_csv('../input/mayo-clinic-strip-ai/test.csv')\n\nmodel = torch.jit.load('../input/mayo-image-class-training-2/model.pt')\n\nIMG_SIZE = 384","metadata":{"execution":{"iopub.status.busy":"2022-10-05T18:49:17.522209Z","iopub.execute_input":"2022-10-05T18:49:17.523486Z","iopub.status.idle":"2022-10-05T18:49:21.483557Z","shell.execute_reply.started":"2022-10-05T18:49:17.523447Z","shell.execute_reply":"2022-10-05T18:49:21.482562Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.mkdir(\"./test/\")\nfor i in tqdm(range(test_df.shape[0])):\n    img_id = test_df.iloc[i].image_id\n    try:\n        sz = os.path.getsize(\"../input/mayo-clinic-strip-ai/test/\" + img_id + \".tif\")\n    except:\n        sz = 1000000000\n    if sz > 8e8:\n        img = np.zeros((IMG_SIZE, IMG_SIZE, 3), np.uint8)\n    else:\n        try:\n            img = cv2.resize(tifffile.imread(\"../input/mayo-clinic-strip-ai/test/\" + img_id + \".tif\"), (IMG_SIZE, IMG_SIZE))\n        except:\n            img = np.zeros((IMG_SIZE, IMG_SIZE, 3), np.uint8)\n    cv2.imwrite(f\"./test/{img_id}.jpg\", img)\n    del img\n    gc.collect()","metadata":{"execution":{"iopub.status.busy":"2022-10-05T18:49:21.484984Z","iopub.execute_input":"2022-10-05T18:49:21.485339Z","iopub.status.idle":"2022-10-05T18:49:58.049332Z","shell.execute_reply.started":"2022-10-05T18:49:21.485304Z","shell.execute_reply":"2022-10-05T18:49:58.048365Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class ImageDataset(Dataset):\n    \n    def __init__(self, df, transform=None):\n        self.images = ['./test/'+filepath for filepath in os.listdir('./test/')]\n        self.df = df\n        self.transform = transform\n    \n    def __len__(self):\n        return len(self.images)\n    \n    def __getitem__(self, idx):\n        try:\n            img = cv2.imread(self.images[idx])\n        except:\n            img = np.zeros((IMG_SIZE, IMG_SIZE, 3), np.uint8)\n        try:\n            if len(img.shape) == 5:\n                img = img.squeeze().transpose(1, 2, 0)\n            img = cv2.resize(img, (IMG_SIZE, IMG_SIZE)).transpose(2, 0, 1)\n        except:\n            img = np.zeros((3, IMG_SIZE, IMG_SIZE))\n        if self.transform is not None:\n            img = self.transform(image=img)['image']\n        patient_id = self.df.iloc[idx].patient_id\n        return img, patient_id","metadata":{"execution":{"iopub.status.busy":"2022-10-05T18:49:58.051731Z","iopub.execute_input":"2022-10-05T18:49:58.052676Z","iopub.status.idle":"2022-10-05T18:49:58.062408Z","shell.execute_reply.started":"2022-10-05T18:49:58.052639Z","shell.execute_reply":"2022-10-05T18:49:58.061238Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')\nprint(device)\n\nmodel = model.to(device)","metadata":{"execution":{"iopub.status.busy":"2022-10-05T18:49:58.063684Z","iopub.execute_input":"2022-10-05T18:49:58.064213Z","iopub.status.idle":"2022-10-05T18:50:01.010102Z","shell.execute_reply.started":"2022-10-05T18:49:58.064174Z","shell.execute_reply":"2022-10-05T18:50:01.009102Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_dataset = ImageDataset(test_df)\ntest_loader = DataLoader(test_dataset, shuffle=False, batch_size=1, num_workers=1)","metadata":{"execution":{"iopub.status.busy":"2022-10-05T18:50:01.011504Z","iopub.execute_input":"2022-10-05T18:50:01.012008Z","iopub.status.idle":"2022-10-05T18:50:01.018498Z","shell.execute_reply.started":"2022-10-05T18:50:01.011972Z","shell.execute_reply":"2022-10-05T18:50:01.017438Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ce = []\nlaa = []\nids = []\n\nfor image, p_id in test_loader:\n    try:\n        pred = model(image.to(device).float())\n        sig = nn.Softmax(dim=1)(pred)\n        ce.append(sig.cpu().detach().numpy()[0][0])\n        laa.append(sig.cpu().detach().numpy()[0][1])\n        ids.append(p_id)\n    except:\n        print('Failed!')\n        ce.append(.5)\n        laa.append(.5)\n        ids.append(p_id)","metadata":{"execution":{"iopub.status.busy":"2022-10-05T18:50:01.019895Z","iopub.execute_input":"2022-10-05T18:50:01.020882Z","iopub.status.idle":"2022-10-05T18:50:07.385361Z","shell.execute_reply.started":"2022-10-05T18:50:01.020846Z","shell.execute_reply":"2022-10-05T18:50:07.384114Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"prob = pd.DataFrame(\n    {\"CE\" : ce,\n     \"LAA\" : laa,\n     \"id\" : ids\n    }\n).groupby(\"id\").mean()","metadata":{"execution":{"iopub.status.busy":"2022-10-05T18:50:07.387641Z","iopub.execute_input":"2022-10-05T18:50:07.388065Z","iopub.status.idle":"2022-10-05T18:50:07.406111Z","shell.execute_reply.started":"2022-10-05T18:50:07.388026Z","shell.execute_reply":"2022-10-05T18:50:07.40331Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_s = pd.read_csv('../input/mayo-clinic-strip-ai/sample_submission.csv')\n\nsample_s.CE = prob.CE.to_list()\nsample_s.LAA = prob.LAA.to_list()","metadata":{"execution":{"iopub.status.busy":"2022-10-05T18:50:07.408088Z","iopub.execute_input":"2022-10-05T18:50:07.408747Z","iopub.status.idle":"2022-10-05T18:50:07.423261Z","shell.execute_reply.started":"2022-10-05T18:50:07.40871Z","shell.execute_reply":"2022-10-05T18:50:07.422232Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_s.to_csv('submission.csv', index=False)\n\nsample_s.head()","metadata":{"execution":{"iopub.status.busy":"2022-10-05T18:50:07.426257Z","iopub.execute_input":"2022-10-05T18:50:07.426668Z","iopub.status.idle":"2022-10-05T18:50:07.4482Z","shell.execute_reply.started":"2022-10-05T18:50:07.42664Z","shell.execute_reply":"2022-10-05T18:50:07.447399Z"},"trusted":true},"execution_count":null,"outputs":[]}]}