{"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 sys\nsys.path.append(\"../input/monai-v060-deep-learning-in-healthcare-imaging\")\nimport monai\nfrom monai.inferers import sliding_window_inference\n\nfrom skimage import io, filters\nimport albumentations as A\n\nimport gc\nimport os\nimport numpy as np\nimport pandas as pd\n\nimport torch\nimport torch.nn as nn\n\nfrom transformers import SegformerForSemanticSegmentation","metadata":{"execution":{"iopub.status.busy":"2022-07-12T03:58:01.227285Z","iopub.execute_input":"2022-07-12T03:58:01.227694Z","iopub.status.idle":"2022-07-12T03:58:12.776110Z","shell.execute_reply.started":"2022-07-12T03:58:01.227613Z","shell.execute_reply":"2022-07-12T03:58:12.775174Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def rle_encode_less_memory(img):\n    #the image should be transposed\n    pixels = img.T.flatten()\n    \n    # This simplified method requires first and last pixel to be zero\n    pixels[0] = 0\n    pixels[-1] = 0\n    runs = np.where(pixels[1:] != pixels[:-1])[0] + 2\n    runs[1::2] -= runs[::2]\n    \n    return ' '.join(str(x) for x in runs)","metadata":{"execution":{"iopub.status.busy":"2022-07-12T03:58:19.652124Z","iopub.execute_input":"2022-07-12T03:58:19.652777Z","iopub.status.idle":"2022-07-12T03:58:19.660278Z","shell.execute_reply.started":"2022-07-12T03:58:19.652745Z","shell.execute_reply":"2022-07-12T03:58:19.659236Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class HubDataset(torch.utils.data.Dataset):\n    def __init__(self, image_paths ):\n        self.image_paths = image_paths\n        \n    def __len__(self):\n        return len(self.image_paths)\n    \n    def __getitem__(self, item):\n        image = io.imread(self.image_paths[item])\n        \n       \n        data_transforms ={\"valid\": A.Compose([\n                A.Resize(image.shape[0],image.shape[1]),\n                A.Normalize(\n                mean=[0.7720342, 0.74582646, 0.76392896],\n                std=[0.7720342, 0.74582646, 0.76392896],\n                max_pixel_value=255.0,\n                p=1.0,\n            ),    \n            ], p=1.0)\n        }\n        transforms = data_transforms[\"valid\"]\n        \n        augmented = transforms(image=image,)\n        image = augmented[\"image\"]\n\n        image = np.transpose(image, (2,0,1))\n\n        return {\n            \"image\": torch.tensor(image, dtype=float),}","metadata":{"execution":{"iopub.status.busy":"2022-07-12T03:58:27.578926Z","iopub.execute_input":"2022-07-12T03:58:27.579291Z","iopub.status.idle":"2022-07-12T03:58:27.588054Z","shell.execute_reply.started":"2022-07-12T03:58:27.579259Z","shell.execute_reply":"2022-07-12T03:58:27.587218Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class HuBMAP_model(nn.Module):\n    def __init__(self):\n        super(HuBMAP_model, self).__init__()\n#         configuration = SegformerConfig.from_pretrained(\"nvidia/segformer-b0-finetuned-ade-512-512\")\n#         configuration.num_labels = 1 ## set output as 1 \n#         self.model = SegformerForSemanticSegmentation(config=configuration)\n\n        self.model = SegformerForSemanticSegmentation.from_pretrained(\"../input/nvidia-segformer-b2-finetuned-ade-512-512/segformer-b2-finetuned-ade-512-512\",\n                                                         num_labels=1,ignore_mismatched_sizes=True)\n    \n    def forward(self, image):\n        img_segs = self.model(image)\n    \n        upsampled_logits = nn.functional.interpolate(img_segs.logits,\n#                 size=(image.shape[0],1,image.shape[2],image.shape[3]), # (height, width)\n                scale_factor=4,\n                mode='bilinear',\n                align_corners=False)\n#         print(upsampled_logits.shape)\n        upsampled_logits = torch.sigmoid(upsampled_logits)\n        return upsampled_logits","metadata":{"execution":{"iopub.status.busy":"2022-07-12T03:58:29.603707Z","iopub.execute_input":"2022-07-12T03:58:29.604342Z","iopub.status.idle":"2022-07-12T03:58:29.611086Z","shell.execute_reply.started":"2022-07-12T03:58:29.604307Z","shell.execute_reply":"2022-07-12T03:58:29.610200Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"@torch.no_grad()\ndef inference(model, data_loader ):\n    model.eval()\n\n    for data in data_loader:\n        image = data[\"image\"].to(\"cuda\", dtype=torch.float)\n        roi_size = (512,512)\n        sw_batch_size = 4\n        outputs = sliding_window_inference(image, roi_size, sw_batch_size, model)\n        \n        \n    return outputs\n        ","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-07-12T03:58:30.905503Z","iopub.execute_input":"2022-07-12T03:58:30.905848Z","iopub.status.idle":"2022-07-12T03:58:30.911545Z","shell.execute_reply.started":"2022-07-12T03:58:30.905819Z","shell.execute_reply":"2022-07-12T03:58:30.910496Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\nsubmission = pd.read_csv(\"../input/hubmap-organ-segmentation/test.csv\")","metadata":{"execution":{"iopub.status.busy":"2022-07-12T03:58:33.309183Z","iopub.execute_input":"2022-07-12T03:58:33.310672Z","iopub.status.idle":"2022-07-12T03:58:33.324632Z","shell.execute_reply.started":"2022-07-12T03:58:33.310630Z","shell.execute_reply":"2022-07-12T03:58:33.323630Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# image_paths = submission.id.values\n# image_paths = [os.path.join(\"../input/hubmap-organ-segmentation/test_images\",str(i) + \".tiff\") for i in image_paths]\n\n# infer_dataset = HubDataset(image_paths)\n# infer_loader = torch.utils.data.DataLoader(infer_dataset, batch_size=12, shuffle=False)\n\n# model = HuBMAP_model()\n# model.to(\"cuda\")\n# model.load_state_dict(torch.load(\"../input/train-hubmap-segformer/model-3epoch-4.pth\"))\n\n# masks = inference(model, infer_loader)","metadata":{"execution":{"iopub.status.busy":"2022-07-12T01:30:02.127364Z","iopub.execute_input":"2022-07-12T01:30:02.128115Z","iopub.status.idle":"2022-07-12T01:30:20.398868Z","shell.execute_reply.started":"2022-07-12T01:30:02.128069Z","shell.execute_reply":"2022-07-12T01:30:20.397692Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# rle_list = []\n# for mask in masks:\n#     mask = mask.reshape(mask.shape[1],mask.shape[2]).cpu().numpy() ## from BxCxHxW  to HxW where B == 1\n#     threshold_otsu = filters.threshold_otsu(mask)\n#     mask = mask > threshold_otsu\n#     mask = mask.astype(np.int8)\n#     rle_list.append(rle_encode_less_memory(mask))\n    ","metadata":{"execution":{"iopub.status.busy":"2022-07-12T01:33:35.610223Z","iopub.execute_input":"2022-07-12T01:33:35.610631Z","iopub.status.idle":"2022-07-12T01:33:36.322701Z","shell.execute_reply.started":"2022-07-12T01:33:35.610595Z","shell.execute_reply":"2022-07-12T01:33:36.321195Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## new attempt\nrle_list = []\nfor idx,row in submission.iterrows():\n    image_paths = row[\"id\"]\n    image_paths = [os.path.join(\"../input/hubmap-organ-segmentation/test_images\",str(image_paths) + \".tiff\")]\n\n    infer_dataset = HubDataset(image_paths)\n    infer_loader = torch.utils.data.DataLoader(infer_dataset, batch_size=12, shuffle=False)\n\n    model = HuBMAP_model()\n    model.to(\"cuda\")\n    model.load_state_dict(torch.load(\"../input/train-hubmap-segformer/model-3epoch-4.pth\"))\n\n    masks = inference(model, infer_loader)\n    \n    mask = masks[0].reshape(masks[0].shape[1],masks[0].shape[2]).cpu().numpy()\n    \n    threshold_otsu = filters.threshold_otsu(mask)\n    mask = mask > threshold_otsu\n    mask = mask.astype(np.int8)\n    \n    rle_list.append(rle_encode_less_memory(mask))\n    \n    ","metadata":{"execution":{"iopub.status.busy":"2022-07-12T04:00:53.468892Z","iopub.execute_input":"2022-07-12T04:00:53.469262Z","iopub.status.idle":"2022-07-12T04:00:55.317551Z","shell.execute_reply.started":"2022-07-12T04:00:53.469228Z","shell.execute_reply":"2022-07-12T04:00:55.316300Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission[\"rle\"] = rle_list","metadata":{"execution":{"iopub.status.busy":"2022-07-12T04:01:03.486768Z","iopub.execute_input":"2022-07-12T04:01:03.487107Z","iopub.status.idle":"2022-07-12T04:01:03.492934Z","shell.execute_reply.started":"2022-07-12T04:01:03.487078Z","shell.execute_reply":"2022-07-12T04:01:03.491665Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = submission[[\"id\",\"rle\"]]","metadata":{"execution":{"iopub.status.busy":"2022-07-12T04:01:07.478604Z","iopub.execute_input":"2022-07-12T04:01:07.478948Z","iopub.status.idle":"2022-07-12T04:01:07.488252Z","shell.execute_reply.started":"2022-07-12T04:01:07.478918Z","shell.execute_reply":"2022-07-12T04:01:07.487289Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv(\"submission.csv\", index=False)","metadata":{"execution":{"iopub.status.busy":"2022-07-12T04:01:10.672581Z","iopub.execute_input":"2022-07-12T04:01:10.672928Z","iopub.status.idle":"2022-07-12T04:01:10.681586Z","shell.execute_reply.started":"2022-07-12T04:01:10.672897Z","shell.execute_reply":"2022-07-12T04:01:10.680609Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-12T04:01:11.686420Z","iopub.execute_input":"2022-07-12T04:01:11.687436Z","iopub.status.idle":"2022-07-12T04:01:11.699822Z","shell.execute_reply.started":"2022-07-12T04:01:11.687389Z","shell.execute_reply":"2022-07-12T04:01:11.698649Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# test_mask = masks[0].reshape(masks[0].shape[2],masks[0].shape[3]).cpu().numpy() #test#","metadata":{"execution":{"iopub.status.busy":"2022-07-11T18:41:23.730470Z","iopub.execute_input":"2022-07-11T18:41:23.731110Z","iopub.status.idle":"2022-07-11T18:41:23.738161Z","shell.execute_reply.started":"2022-07-11T18:41:23.731063Z","shell.execute_reply":"2022-07-11T18:41:23.737253Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# mask2rle(masks[0].reshape(masks[0].shape[2],masks[0].shape[3]).cpu().numpy()) #test#","metadata":{"execution":{"iopub.status.busy":"2022-07-11T18:41:23.741301Z","iopub.execute_input":"2022-07-11T18:41:23.741649Z","iopub.status.idle":"2022-07-11T18:41:23.749292Z","shell.execute_reply.started":"2022-07-11T18:41:23.741623Z","shell.execute_reply":"2022-07-11T18:41:23.748241Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# io.imshow(test_mask) #test#","metadata":{"execution":{"iopub.status.busy":"2022-07-11T18:41:23.752554Z","iopub.execute_input":"2022-07-11T18:41:23.752806Z","iopub.status.idle":"2022-07-11T18:41:23.760614Z","shell.execute_reply.started":"2022-07-11T18:41:23.752783Z","shell.execute_reply":"2022-07-11T18:41:23.759561Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# thresh_isodata = filters.threshold_otsu(test_mask) #test#\n# thresh_test_mask = test_mask > thresh_isodata #test#","metadata":{"execution":{"iopub.status.busy":"2022-07-11T18:41:23.761727Z","iopub.execute_input":"2022-07-11T18:41:23.762637Z","iopub.status.idle":"2022-07-11T18:41:23.771884Z","shell.execute_reply.started":"2022-07-11T18:41:23.761989Z","shell.execute_reply":"2022-07-11T18:41:23.770813Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# io.imshow(thresh_test_mask.astype(np.int8)) #test#","metadata":{"execution":{"iopub.status.busy":"2022-07-11T18:41:23.773234Z","iopub.execute_input":"2022-07-11T18:41:23.773799Z","iopub.status.idle":"2022-07-11T18:41:23.783759Z","shell.execute_reply.started":"2022-07-11T18:41:23.773751Z","shell.execute_reply":"2022-07-11T18:41:23.782539Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# io.imshow(io.imread(\"../input/hubmap-organ-segmentation/test_images/10078.tiff\")) #test#","metadata":{"execution":{"iopub.status.busy":"2022-07-11T18:41:23.784766Z","iopub.execute_input":"2022-07-11T18:41:23.785008Z","iopub.status.idle":"2022-07-11T18:41:23.794326Z","shell.execute_reply.started":"2022-07-11T18:41:23.784985Z","shell.execute_reply":"2022-07-11T18:41:23.793419Z"},"trusted":true},"execution_count":null,"outputs":[]}]}