{"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":"# Simple example of blending models from mmseg for inference.\n\nMostly borowed from https://www.kaggle.com/code/w3579628328/mmsegmentation-lb0-78-inference-1-5folds.\nMany thanks to [DiamondH](https://www.kaggle.com/w3579628328)!","metadata":{}},{"cell_type":"code","source":"!pip install ../input/openmmlab-essential-repositories/openmmlab-repos/src/torch-1.10.0+cu111-cp37-cp37m-linux_x86_64.whl\n!pip install ../input/openmmlab-essential-repositories/openmmlab-repos/src/torchvision-0.11.0+cu111-cp37-cp37m-linux_x86_64.whl\n!pip install ../input/mmdetection/addict-2.4.0-py3-none-any.whl\n!pip install ../input/openmmlab-essential-repositories/openmmlab-repos/src/mmcv_full-1.5.3-cp37-cp37m-manylinux1_x86_64.whl\n!pip install ../input/openmmlab-essential-repositories/openmmlab-repos/src/mmcls-0.23.1-py2.py3-none-any.whl\n!pip install ../input/mmdetection/yapf-0.31.0-py2.py3-none-any.whl\n!pip install ../input/mmdetection/terminaltables-3.1.0-py3-none-any.whl\n!pip install ../input/mmdetection/einops-0.4.1-py3-none-any.whl","metadata":{"execution":{"iopub.status.busy":"2022-09-12T08:40:07.501861Z","iopub.execute_input":"2022-09-12T08:40:07.502816Z","iopub.status.idle":"2022-09-12T08:43:07.339250Z","shell.execute_reply.started":"2022-09-12T08:40:07.502714Z","shell.execute_reply":"2022-09-12T08:43:07.337757Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Installing new version of mmseg, I just edited few line in original repo.","metadata":{}},{"cell_type":"code","source":"!cp -r ../input/mmseg-with-logits/mmsegmentation-master /kaggle/working/ && cd /kaggle/working/mmsegmentation-master && pip install -e . && cd ..","metadata":{"execution":{"iopub.status.busy":"2022-09-12T08:43:07.342180Z","iopub.execute_input":"2022-09-12T08:43:07.342610Z","iopub.status.idle":"2022-09-12T08:43:25.837751Z","shell.execute_reply.started":"2022-09-12T08:43:07.342566Z","shell.execute_reply":"2022-09-12T08:43:25.836616Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import cv2\nimport numpy as np\nimport pandas as pd\nimport os\nfrom glob import glob\nfrom tqdm.notebook import tqdm\nimport sys\nimport gc\nsys.path.append('./mmsegmentation-master')\nfrom mmseg.apis import init_segmentor, inference_segmentor\nfrom mmcv.utils import config\n\n\ndef rle_encode_less_memory(img):\n    pixels = img.T.flatten()\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    return ' '.join(str(x) for x in runs)","metadata":{"execution":{"iopub.status.busy":"2022-09-12T08:43:56.419678Z","iopub.execute_input":"2022-09-12T08:43:56.420337Z","iopub.status.idle":"2022-09-12T08:43:56.428446Z","shell.execute_reply.started":"2022-09-12T08:43:56.420296Z","shell.execute_reply":"2022-09-12T08:43:56.427138Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Adding all your models","metadata":{}},{"cell_type":"code","source":"configs = [\n    '../input/hubmap-models/cfg_model_1.py',\n    '../input/hubmap-models/cfg_model_2.py'\n]\nckpts = [\n    '../input/hubmap-models/model_1.pth',\n    '../input/hubmap-models/model_2.pth'\n]\n\n\nmodels = []\nfor idx,(cfg, ckpt) in enumerate(zip(configs, ckpts)):\n    cfg = config.Config.fromfile(cfg)\n    model = init_segmentor(cfg, ckpt, device='cuda:0')\n    models.append(model)","metadata":{"execution":{"iopub.status.busy":"2022-09-12T08:44:25.178763Z","iopub.execute_input":"2022-09-12T08:44:25.179811Z","iopub.status.idle":"2022-09-12T08:45:22.832827Z","shell.execute_reply.started":"2022-09-12T08:44:25.179775Z","shell.execute_reply":"2022-09-12T08:45:22.831782Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"DATA = '../input/hubmap-organ-segmentation/test_images/'\ndf_sample = pd.read_csv('../input/hubmap-organ-segmentation/sample_submission.csv').set_index('id')\ndf_test = pd.read_csv('../input/hubmap-organ-segmentation/test.csv').set_index('id')","metadata":{"execution":{"iopub.status.busy":"2022-09-12T08:45:22.835248Z","iopub.execute_input":"2022-09-12T08:45:22.835639Z","iopub.status.idle":"2022-09-12T08:45:22.876403Z","shell.execute_reply.started":"2022-09-12T08:45:22.835603Z","shell.execute_reply":"2022-09-12T08:45:22.875535Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import mmcv\n\n\nnames,preds = [],[]\nimgs, pd_mks = [],[]\ndebug = len(df_test)<11\nclasses_to_num = {'background':0, 'kidney':1, 'prostate':2, 'largeintestine':3, 'spleen':4, 'lung':5}\nfor idx,row in tqdm(df_test.iterrows(),total=len(df_test)):\n    try:\n        organ = row.organ\n        class_num = classes_to_num[organ]\n        \n        img  = mmcv.imread(os.path.join(DATA,str(idx)+'.tiff'))\n        \n        pred = np.zeros((len(classes_to_num), img.shape[0], img.shape[1]))\n        for model in models:\n            pred += inference_segmentor(model, img)[0]/len(models)\n        \n        # I just use argmax for result mask, you can use thresholds or something else\n        pred_class = pred.argmax(axis=0) == class_num\n        pred_class = pred_class.astype(np.uint8)\n        \n        rle = rle_encode_less_memory(pred_class)\n        names.append(str(idx))\n        preds.append(rle)\n        if debug:\n            imgs.append(img)\n            pd_mks.append(pred_class)\n        del img, pred, rle, idx, row, pred_class\n        gc.collect()\n    except Exception as e:\n        print(e)\n        names.append(str(idx))\n        preds.append('')","metadata":{"execution":{"iopub.status.busy":"2022-09-12T08:52:57.612817Z","iopub.execute_input":"2022-09-12T08:52:57.614103Z","iopub.status.idle":"2022-09-12T08:52:59.958735Z","shell.execute_reply.started":"2022-09-12T08:52:57.614049Z","shell.execute_reply":"2022-09-12T08:52:59.957757Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"if debug:\n    import matplotlib.pyplot as plt\n    for img, mask in zip(imgs, pd_mks):\n        plt.figure(figsize=(12, 7))\n        plt.subplot(1, 3, 1); plt.imshow(img); plt.axis('OFF'); plt.title('image')\n        plt.subplot(1, 3, 2); plt.imshow(mask*255); plt.axis('OFF'); plt.title('mask')\n        plt.subplot(1, 3, 3); plt.imshow(img); plt.imshow(mask*255, alpha=0.4); plt.axis('OFF'); plt.title('overlay')\n        plt.tight_layout()\n        plt.show()","metadata":{"execution":{"iopub.status.busy":"2022-09-12T08:53:02.877401Z","iopub.execute_input":"2022-09-12T08:53:02.877766Z","iopub.status.idle":"2022-09-12T08:53:05.111816Z","shell.execute_reply.started":"2022-09-12T08:53:02.877733Z","shell.execute_reply":"2022-09-12T08:53:05.110921Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!rm -rf /kaggle/working/*","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.DataFrame({'id':names,'rle':preds})\ndf.to_csv('submission.csv',index=False)\n\ndf","metadata":{"execution":{"iopub.status.busy":"2022-09-12T08:46:09.615287Z","iopub.execute_input":"2022-09-12T08:46:09.615890Z","iopub.status.idle":"2022-09-12T08:46:09.636037Z","shell.execute_reply.started":"2022-09-12T08:46:09.615837Z","shell.execute_reply":"2022-09-12T08:46:09.634927Z"},"trusted":true},"execution_count":null,"outputs":[]}]}