{"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-13T04:00:59.105859Z","iopub.execute_input":"2022-07-13T04:00:59.106670Z","iopub.status.idle":"2022-07-13T04:01:13.064938Z","shell.execute_reply.started":"2022-07-13T04:00:59.106529Z","shell.execute_reply":"2022-07-13T04:01:13.064005Z"},"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-13T04:01:13.066888Z","iopub.execute_input":"2022-07-13T04:01:13.067507Z","iopub.status.idle":"2022-07-13T04:01:13.073355Z","shell.execute_reply.started":"2022-07-13T04:01:13.067480Z","shell.execute_reply":"2022-07-13T04:01:13.072481Z"},"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-13T04:01:13.074584Z","iopub.execute_input":"2022-07-13T04:01:13.075131Z","iopub.status.idle":"2022-07-13T04:01:13.100797Z","shell.execute_reply.started":"2022-07-13T04:01:13.075095Z","shell.execute_reply":"2022-07-13T04:01:13.099815Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## MODEL","metadata":{}},{"cell_type":"code","source":"##### dependencies\nimport sys\n! pip install ../input/einops-030/einops-0.3.0-py2.py3-none-any.whl\nsys.path.append(\"../input/ml-collections/ml_collections\")\nsys.path.append( '../input/transunet/TransUNet')\nimport ml_collections\n\nimport networks\nfrom networks.vit_seg_modeling import VisionTransformer as ViT_seg\nfrom networks.vit_seg_modeling import CONFIGS as CONFIGS_ViT_seg ","metadata":{"execution":{"iopub.status.busy":"2022-07-13T04:01:13.103606Z","iopub.execute_input":"2022-07-13T04:01:13.104060Z","iopub.status.idle":"2022-07-13T04:01:43.880924Z","shell.execute_reply.started":"2022-07-13T04:01:13.103998Z","shell.execute_reply":"2022-07-13T04:01:43.879813Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"config_vit = CONFIGS_ViT_seg[\"R50-ViT-B_16\"]\nconfig_vit.n_classes = 1\nconfig_vit.n_skip = 3\nconfig_vit.pretrained_path = '../input/experiment-model-dumps-hubmap/R50ViT-B_16.npz'\nconfig_vit.transformer.dropout_rate = 0.2\nconfig_vit.transformer.mlp_dim = 3072\nconfig_vit.transformer.num_heads = 4\nconfig_vit.transformer.num_layers = 8\n\nconfig_vit","metadata":{"execution":{"iopub.status.busy":"2022-07-13T04:01:43.883024Z","iopub.execute_input":"2022-07-13T04:01:43.883487Z","iopub.status.idle":"2022-07-13T04:01:43.898627Z","shell.execute_reply.started":"2022-07-13T04:01:43.883448Z","shell.execute_reply":"2022-07-13T04:01:43.897723Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch.nn as nn\nclass ViTHuBMAP(nn.Module):\n    def __init__(self, configs=config_vit):\n        super(ViTHuBMAP, self).__init__()\n        \n        self.model = ViT_seg(configs, img_size=256, num_classes=1)\n        self.model.load_from(weights=np.load(configs.pretrained_path))\n\n        \n    def forward(self, x):\n        img_segs = self.model(x)\n#         img_segs = torch.sigmoid(img_segs) ## added sigmoid\n        return img_segs","metadata":{"execution":{"iopub.status.busy":"2022-07-13T04:06:23.899221Z","iopub.execute_input":"2022-07-13T04:06:23.900116Z","iopub.status.idle":"2022-07-13T04:06:23.908595Z","shell.execute_reply.started":"2022-07-13T04:06:23.900079Z","shell.execute_reply":"2022-07-13T04:06:23.907265Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"###  MASKS thresholding techniques\n\"\"\"\n> threshold_isodata\n> threshold_li\n> threshold_local\n> threshold_mean\n> threshold_minimum\n> threshold_multiotsu\n> threshold_niblack\n> threshold_otsu\n> threshold_sauvola\n> threshold_triangle\n> threshold_yen\n\n> unsharp_mask\n\n\"\"\"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## INFER","metadata":{}},{"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 = (256,256)\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-13T04:01:43.914552Z","iopub.execute_input":"2022-07-13T04:01:43.915224Z","iopub.status.idle":"2022-07-13T04:01:43.922601Z","shell.execute_reply.started":"2022-07-13T04:01:43.915185Z","shell.execute_reply":"2022-07-13T04:01:43.921733Z"},"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-13T04:01:43.924707Z","iopub.execute_input":"2022-07-13T04:01:43.925355Z","iopub.status.idle":"2022-07-13T04:01:43.942158Z","shell.execute_reply.started":"2022-07-13T04:01:43.925330Z","shell.execute_reply":"2022-07-13T04:01:43.941218Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## new attempt\nMODEL_PATHS = [\"../input/experiment-model-dumps-hubmap/model-0epoch-16.pth\",\n          \"../input/experiment-model-dumps-hubmap/model-1epoch-16.pth\",\n          \"../input/experiment-model-dumps-hubmap/model-2epoch-14.pth\"]\n\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 = ViTHuBMAP()\n    model.to(\"cuda\")\n    \n    model.load_state_dict(torch.load(MODEL_PATHS[0]))\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    \n#     io.imshow(mask)\n    \n    threshold_otsu = filters.threshold_otsu(mask)\n    mask = mask > threshold_otsu\n    mask = mask.astype(np.int8)\n    \n    io.imshow(mask)\n    \n    rle_list.append(rle_encode_less_memory(mask))","metadata":{"execution":{"iopub.status.busy":"2022-07-13T04:08:00.185093Z","iopub.execute_input":"2022-07-13T04:08:00.185762Z","iopub.status.idle":"2022-07-13T04:08:04.398939Z","shell.execute_reply.started":"2022-07-13T04:08:00.185722Z","shell.execute_reply":"2022-07-13T04:08:04.398040Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission[\"rle\"] = rle_list","metadata":{"execution":{"iopub.status.busy":"2022-07-13T04:03:06.846667Z","iopub.execute_input":"2022-07-13T04:03:06.847315Z","iopub.status.idle":"2022-07-13T04:03:06.853428Z","shell.execute_reply.started":"2022-07-13T04:03:06.847271Z","shell.execute_reply":"2022-07-13T04:03:06.852547Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = submission[[\"id\",\"rle\"]]","metadata":{"execution":{"iopub.status.busy":"2022-07-13T04:03:06.854865Z","iopub.execute_input":"2022-07-13T04:03:06.855444Z","iopub.status.idle":"2022-07-13T04:03:06.871295Z","shell.execute_reply.started":"2022-07-13T04:03:06.855406Z","shell.execute_reply":"2022-07-13T04:03:06.870271Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv(\"submission.csv\", index=False)","metadata":{"execution":{"iopub.status.busy":"2022-07-13T04:03:06.873860Z","iopub.execute_input":"2022-07-13T04:03:06.874228Z","iopub.status.idle":"2022-07-13T04:03:06.884816Z","shell.execute_reply.started":"2022-07-13T04:03:06.874192Z","shell.execute_reply":"2022-07-13T04:03:06.883871Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-13T04:03:06.887442Z","iopub.execute_input":"2022-07-13T04:03:06.887723Z","iopub.status.idle":"2022-07-13T04:03:06.901478Z","shell.execute_reply.started":"2022-07-13T04:03:06.887699Z","shell.execute_reply":"2022-07-13T04:03:06.900497Z"},"trusted":true},"execution_count":null,"outputs":[]}]}