{"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":"markdown","source":"<div style=\"height:200px;width:100%;margin: 0;\">\n    <img src=\"https://storage.googleapis.com/kaggle-competitions/kaggle/34547/logos/header.png?t=2022-02-15-22-37-27\" style=\"width:100%;\" />\n</div>","metadata":{}},{"cell_type":"markdown","source":"<h3 class=\"list-group-item list-group-item-action active\" data-toggle=\"list\" style=\"color:#c3448b; background:#efe9e9; border:1px dashed #efe50b;\" role=\"tab\" aria-controls=\"goal\"><center>Goal</center></h3>\n\nGoal of this notebook is to teach you how to make a CoAT submission.<br>\nMost of the code is based/modified on this [hengck23 notebook](https://www.kaggle.com/code/hengck23/lb-0-78-coat-with-no-decoder)<br>\nPlease don't forget to <b>upvote</b>, it's the only reward I get from this :)","metadata":{}},{"cell_type":"markdown","source":"<h3 class=\"list-group-item list-group-item-action active\" data-toggle=\"list\" style=\"color:#c3448b; background:#efe9e9; border:1px dashed #efe50b;\" role=\"tab\" aria-controls=\"libraries\"><center>Libraries</center></h3>\n","metadata":{}},{"cell_type":"code","source":"import sys\nsys.path.append('/kaggle/input/timm-pytorch-image-models/pytorch-image-models-master/')\n!pip install -qq /kaggle/input/mmdetection/einops-0.4.1-py3-none-any.whl","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2022-09-02T13:40:46.031530Z","iopub.execute_input":"2022-09-02T13:40:46.032216Z","iopub.status.idle":"2022-09-02T13:40:56.542067Z","shell.execute_reply.started":"2022-09-02T13:40:46.032179Z","shell.execute_reply":"2022-09-02T13:40:56.540801Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import cv2\nimport os \nsys.path.append('../input/hubmap-coat') \n\nimport tifffile as tiff\nimport pandas as pd\nimport importlib\nfrom timeit import default_timer as timer\n\nimport torch\nimport torch.cuda.amp as amp\nimport torch.nn.functional as F","metadata":{"execution":{"iopub.status.busy":"2022-09-02T13:41:54.322715Z","iopub.execute_input":"2022-09-02T13:41:54.323158Z","iopub.status.idle":"2022-09-02T13:41:54.329949Z","shell.execute_reply.started":"2022-09-02T13:41:54.323120Z","shell.execute_reply":"2022-09-02T13:41:54.328774Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sys.path.append('/kaggle/input/hubmap-coat/')\n\nfrom coat import *\nfrom daformer import *\nfrom helper import *","metadata":{"execution":{"iopub.status.busy":"2022-09-02T13:46:25.714599Z","iopub.execute_input":"2022-09-02T13:46:25.715095Z","iopub.status.idle":"2022-09-02T13:46:25.725096Z","shell.execute_reply.started":"2022-09-02T13:46:25.715052Z","shell.execute_reply":"2022-09-02T13:46:25.723694Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<h3 class=\"list-group-item list-group-item-action active\" data-toggle=\"list\" style=\"color:#c3448b; background:#efe9e9; border:1px dashed #efe50b;\" role=\"tab\" aria-controls=\"config\"><center>Configurations</center></h3>","metadata":{}},{"cell_type":"code","source":"image_size = 384\nfold_nr = 1\n\norgan_threshold = {\n    'Hubmap': {\n        'kidney'        : 0.40,\n        'prostate'      : 0.40,\n        'largeintestine': 0.40,\n        'spleen'        : 0.40,\n        'lung'          : 0.10,\n    },\n    'HPA': {\n        'kidney'        : 0.50,\n        'prostate'      : 0.50,\n        'largeintestine': 0.50,\n        'spleen'        : 0.50,\n        'lung'          : 0.10,\n    },\n}\n\ndata_source =['Hubmap', 'HPA']\norgan = ['kidney', 'prostate', 'largeintestine', 'spleen', 'lung']","metadata":{"execution":{"iopub.status.busy":"2022-09-02T13:49:00.440739Z","iopub.execute_input":"2022-09-02T13:49:00.441124Z","iopub.status.idle":"2022-09-02T13:49:00.447635Z","shell.execute_reply.started":"2022-09-02T13:49:00.441093Z","shell.execute_reply":"2022-09-02T13:49:00.446442Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"valid_file = '../input/hubmap-organ-segmentation/test.csv'\ntiff_dir   = '../input/hubmap-organ-segmentation/test_images'\n\nvalid_df = pd.read_csv(valid_file)\nvalid_df.loc[:,'img_area']=valid_df['img_height']*valid_df['img_width']\nvalid_df = valid_df.sort_values('img_area').reset_index(drop=True)","metadata":{"execution":{"iopub.status.busy":"2022-09-02T13:44:04.454075Z","iopub.execute_input":"2022-09-02T13:44:04.454468Z","iopub.status.idle":"2022-09-02T13:44:04.478566Z","shell.execute_reply.started":"2022-09-02T13:44:04.454436Z","shell.execute_reply":"2022-09-02T13:44:04.477553Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<h3 class=\"list-group-item list-group-item-action active\" data-toggle=\"list\" style=\"color:#c3448b; background:#efe9e9; border:1px dashed #efe50b;\" role=\"tab\" aria-controls=\"model\"><center>Model</center></h3>","metadata":{}},{"cell_type":"code","source":"class Net(nn.Module):\n\n\n    def __init__(self,\n                 encoder=coat_lite_medium,\n                 decoder=daformer_conv3x3,\n                 encoder_cfg={},\n                 decoder_cfg={},\n                 ):\n        super(Net, self).__init__()\n        decoder_dim = decoder_cfg.get('decoder_dim', 320)\n\n        self.encoder = encoder\n        \n        # ----\n        self.rgb = RGB()\n\n        encoder_dim = self.encoder.embed_dims\n        # [64, 128, 320, 512]\n\n        self.decoder = decoder(\n            encoder_dim=encoder_dim,\n            decoder_dim=decoder_dim,\n        )\n        self.logit = nn.Sequential(\n            nn.Conv2d(decoder_dim, 1, kernel_size=1),\n            nn.Upsample(scale_factor = 4, mode='bilinear', align_corners=False),\n        )\n\n    def forward(self, batch):\n\n        x = batch['image']\n        x = self.rgb(x)\n\n        B, C, H, W = x.shape\n        encoder = self.encoder(x)\n\n        last, decoder = self.decoder(encoder)\n        logit = self.logit(last)\n\n        output = {}\n        probability_from_logit = torch.sigmoid(logit)\n        output['probability'] = probability_from_logit\n\n        return output","metadata":{"execution":{"iopub.status.busy":"2022-09-02T13:44:22.391311Z","iopub.execute_input":"2022-09-02T13:44:22.391677Z","iopub.status.idle":"2022-09-02T13:44:22.402461Z","shell.execute_reply.started":"2022-09-02T13:44:22.391646Z","shell.execute_reply":"2022-09-02T13:44:22.401172Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = [\n    dotdict(\n        is_use = 1,\n        module = 'model_daformer_coat',\n        param={'encoder': coat_lite_medium, 'decoder':daformer_conv3x3},\n        checkpoint = [\n            '../input/hubmap-coat-medium/coat_lite_medium_384x384_f9129688.pth'\n        ],\n    ),\n]","metadata":{"execution":{"iopub.status.busy":"2022-09-02T13:51:07.436610Z","iopub.execute_input":"2022-09-02T13:51:07.436977Z","iopub.status.idle":"2022-09-02T13:51:07.442326Z","shell.execute_reply.started":"2022-09-02T13:51:07.436945Z","shell.execute_reply":"2022-09-02T13:51:07.441031Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def load_net(model):\n    net = []\n    for f in range(fold_nr):\n        encoder = coat_lite_medium()\n        checkpoint = '../input/hubmap-coat-medium/coat_lite_medium_384x384_f9129688.pth'\n        checkpoint = torch.load(checkpoint, map_location=lambda storage, loc: storage)\n        state_dict = checkpoint['model']\n        encoder.load_state_dict(state_dict,strict=False)\n\n        model = Net(encoder=encoder).cuda()\n\n        # insert in the checkpoint the folder destination with your folds\n        checkpoint = '../input/hubmap-folds/FOLD'+str(f)+'_best_score.pth'\n        \n        model.load_state_dict(torch.load(checkpoint))\n        net.append(model)\n    return net","metadata":{"execution":{"iopub.status.busy":"2022-09-02T13:58:13.628284Z","iopub.execute_input":"2022-09-02T13:58:13.628677Z","iopub.status.idle":"2022-09-02T13:58:13.635507Z","shell.execute_reply.started":"2022-09-02T13:58:13.628644Z","shell.execute_reply":"2022-09-02T13:58:13.634284Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<h3 class=\"list-group-item list-group-item-action active\" data-toggle=\"list\" style=\"color:#c3448b; background:#efe9e9; border:1px dashed #efe50b;\" role=\"tab\" aria-controls=\"additionals\"><center>Additionals</center></h3>","metadata":{}},{"cell_type":"code","source":"def do_tta_batch(image, organ):\n    batch = {\n        'image': torch.stack([\n            image,\n            torch.flip(image,dims=[1]),\n            torch.flip(image,dims=[2]),\n        ]),\n        'organ': torch.Tensor(\n            [[organ_to_label[organ]]]*3\n        ).long()\n    }\n    return batch","metadata":{"execution":{"iopub.status.busy":"2022-09-02T13:58:14.069968Z","iopub.execute_input":"2022-09-02T13:58:14.070334Z","iopub.status.idle":"2022-09-02T13:58:14.076738Z","shell.execute_reply.started":"2022-09-02T13:58:14.070303Z","shell.execute_reply":"2022-09-02T13:58:14.075149Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def undo_tta_batch(probability):\n    probability[0] = probability[0]\n    probability[1] = torch.flip(probability[1],dims=[1])\n    probability[2] = torch.flip(probability[2],dims=[2])\n    probability = probability.mean(0, keepdims=True)\n    probability = probability[0,0].float()\n    return probability","metadata":{"execution":{"iopub.status.busy":"2022-09-02T13:58:14.296106Z","iopub.execute_input":"2022-09-02T13:58:14.296495Z","iopub.status.idle":"2022-09-02T13:58:14.302411Z","shell.execute_reply.started":"2022-09-02T13:58:14.296461Z","shell.execute_reply":"2022-09-02T13:58:14.301437Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<h3 class=\"list-group-item list-group-item-action active\" data-toggle=\"list\" style=\"color:#c3448b; background:#efe9e9; border:1px dashed #efe50b;\" role=\"tab\" aria-controls=\"submission\"><center>Submission</center></h3>","metadata":{}},{"cell_type":"code","source":"all_net = [ load_net(m) for m in model if m.is_use==1 ]","metadata":{"execution":{"iopub.status.busy":"2022-09-02T14:05:23.342311Z","iopub.execute_input":"2022-09-02T14:05:23.342680Z","iopub.status.idle":"2022-09-02T14:05:25.186891Z","shell.execute_reply.started":"2022-09-02T14:05:23.342648Z","shell.execute_reply":"2022-09-02T14:05:25.185828Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"result = []\n\nfor i,d in valid_df.iterrows():\n    id = d['id']\n    \n    if (d['data_source'] in data_source) and (d['organ'] in organ):\n\n        # read the image from the tiff\n        tiff_file = tiff_dir +'/%d.tiff'%id\n        tiff = read_tiff(tiff_file, 'rgb') \n        tiff = tiff.astype(np.float32)/255\n        H,W,_ = tiff.shape\n\n        # resize it to the image size you been training with\n        image = cv2.resize(tiff,dsize=(image_size,image_size),interpolation=cv2.INTER_LINEAR)\n\n        # create tensor batches\n        image = image_to_tensor(image, 'rgb')\n        batch = { k:v.cuda() for k,v in do_tta_batch(image, d.organ).items() }\n\n        # get the masks probabilities from the pretrained model\n        use = 0\n        probability = 0\n        with torch.no_grad():\n            with amp.autocast(enabled = True):\n\n                for net in all_net:\n                    for n in net:\n                        use += 1\n                        output = n(batch)\n                        \n                        # interpolate - resize to the competition image size\n                        probability += \\\n                            F.interpolate(output['probability'], size=(d.img_height,d.img_width),\n                                          mode='bilinear',align_corners=False, antialias=True )\n\n                probability = undo_tta_batch(probability/use)\n\n        probability = probability.data.cpu().numpy()\n        \n        # use the organ threshold to play with the predictions\n        p = probability>organ_threshold[d.data_source][d.organ]\n        \n        # lossless data compression encoding\n        rle = rle_encode(p)\n        \n    else:\n        rle = ''\n\n    result.append({ 'id':id, 'rle':rle, })","metadata":{"execution":{"iopub.status.busy":"2022-09-02T14:05:51.846629Z","iopub.execute_input":"2022-09-02T14:05:51.847783Z","iopub.status.idle":"2022-09-02T14:05:52.023653Z","shell.execute_reply.started":"2022-09-02T14:05:51.847736Z","shell.execute_reply":"2022-09-02T14:05:52.022696Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub = pd.DataFrame(result)\nsub.to_csv('submission.csv',index=False)\nsub.head()","metadata":{"execution":{"iopub.status.busy":"2022-09-02T14:10:41.577193Z","iopub.execute_input":"2022-09-02T14:10:41.577650Z","iopub.status.idle":"2022-09-02T14:10:41.597813Z","shell.execute_reply.started":"2022-09-02T14:10:41.577609Z","shell.execute_reply":"2022-09-02T14:10:41.596832Z"},"trusted":true},"execution_count":null,"outputs":[]}]}