{"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":"Mostly modified from https://www.kaggle.com/code/w3579628328/mmsegmentation-lb0-78-inference-1-5folds. Many thanks to @DiamondH!","metadata":{}},{"cell_type":"markdown","source":"#### Dependents\n","metadata":{"papermill":{"duration":0.00991,"end_time":"2021-03-12T06:33:14.88117","exception":false,"start_time":"2021-03-12T06:33:14.87126","status":"completed"},"tags":[]}},{"cell_type":"code","source":"!pip install ../input/mmdetection/addict-2.4.0-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\n!pip install ../input/mmsegmentation/mmcv-full/mmcv_full-1.5.3-cp37-cp37m-linux_x86_64.whl\n!pip install ../input/openmmlab-essential-repositories/openmmlab-repos/src/mmcls-0.23.1-py2.py3-none-any.whl","metadata":{"_kg_hide-input":false,"_kg_hide-output":true,"execution":{"iopub.status.busy":"2022-11-01T06:40:04.338125Z","iopub.execute_input":"2022-11-01T06:40:04.339069Z","iopub.status.idle":"2022-11-01T06:43:04.063650Z","shell.execute_reply.started":"2022-11-01T06:40:04.338962Z","shell.execute_reply":"2022-11-01T06:43:04.062413Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!rm -rf mmsegmentation\n# ../input/mmsegunknown/mmsegmentation\n!cp -r /kaggle/input/mmsegunknown  /kaggle/working/mmsegmentation\n# !mv /kaggle/working/mmdet225 /kaggle/working/mmdetection\n%cd /kaggle/working/mmsegmentation\n!pip install -e .\n","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2022-11-01T06:43:04.066349Z","iopub.execute_input":"2022-11-01T06:43:04.066741Z","iopub.status.idle":"2022-11-01T06:43:46.692781Z","shell.execute_reply.started":"2022-11-01T06:43:04.066701Z","shell.execute_reply":"2022-11-01T06:43:46.691552Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### Import librarys","metadata":{}},{"cell_type":"code","source":"%cd ../","metadata":{"execution":{"iopub.status.busy":"2022-11-01T06:43:46.695750Z","iopub.execute_input":"2022-11-01T06:43:46.696917Z","iopub.status.idle":"2022-11-01T06:43:46.705141Z","shell.execute_reply.started":"2022-11-01T06:43:46.696875Z","shell.execute_reply":"2022-11-01T06:43:46.703615Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import cv2\nimport numpy as np\nimport torch\nimport pandas as pd\nimport os\nfrom glob import glob\nfrom tqdm.notebook import tqdm\nimport sys\nimport gc\nsys.path.append('./mmsegmentation')\nfrom mmseg.apis import init_segmentor, inference_segmentor, show_result_pyplot\nfrom mmcv.utils import config\nimport mmcv","metadata":{"_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_kg_hide-input":false,"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","papermill":{"duration":3.066435,"end_time":"2021-03-12T06:33:17.956368","exception":false,"start_time":"2021-03-12T06:33:14.889933","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-11-01T06:43:46.708810Z","iopub.execute_input":"2022-11-01T06:43:46.709214Z","iopub.status.idle":"2022-11-01T06:43:52.455141Z","shell.execute_reply.started":"2022-11-01T06:43:46.709180Z","shell.execute_reply":"2022-11-01T06:43:52.454109Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### Load models","metadata":{}},{"cell_type":"code","source":"configs = [\n    '../input/mmseg-train-mit-ori/config.py',\n    '../input/mmseg-convx-960/config.py', \n    '../input/mmseg-train-mit/config.py',\n     \n]\nckpts = [\n    '../input/mmseg-train-mit-ori/seg_model_output/iter_5000.pth',\n    '../input/mmseg-convx-960/seg_model_output/iter_5000.pth', \n    '../input/mmseg-train-mit/seg_model_output/iter_5000.pth',\n     \n]\nweights = [0.5, 0.2, 0.3]\nDATA = '../input/hubmap-organ-segmentation/test_images/'\ndf_sample = pd.read_csv('../input/hubmap-organ-segmentation/test.csv')[['id', 'organ']].set_index('id')\nmodels = []\nfor idx,(cfg, ckpt) in enumerate(zip(configs, ckpts)):\n    cfg = config.Config.fromfile(cfg)\n#     if idx==1 :\n#         cfg.model.backbone.window_size=[16, 16, 16, 16]\n    cfg.model.test_cfg.mode='whole'\n#     cfg.model.decode_head.channels=1\n    cfg.model.test_cfg[\"sigmoid\"]=True\n    model = init_segmentor(cfg, ckpt, device='cuda:0')\n    models.append(model)","metadata":{"execution":{"iopub.status.busy":"2022-11-01T06:43:52.460117Z","iopub.execute_input":"2022-11-01T06:43:52.462676Z","iopub.status.idle":"2022-11-01T06:44:06.390177Z","shell.execute_reply.started":"2022-11-01T06:43:52.462636Z","shell.execute_reply":"2022-11-01T06:44:06.389091Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_sample.head()","metadata":{"execution":{"iopub.status.busy":"2022-11-01T06:44:06.391738Z","iopub.execute_input":"2022-11-01T06:44:06.392101Z","iopub.status.idle":"2022-11-01T06:44:06.408570Z","shell.execute_reply.started":"2022-11-01T06:44:06.392064Z","shell.execute_reply":"2022-11-01T06:44:06.407399Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### mask -> rle","metadata":{"papermill":{"duration":0.008314,"end_time":"2021-03-12T06:33:18.008555","exception":false,"start_time":"2021-03-12T06:33:18.000241","status":"completed"},"tags":[]}},{"cell_type":"code","source":"def 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":{"_kg_hide-input":false,"papermill":{"duration":0.026676,"end_time":"2021-03-12T06:33:18.043775","exception":false,"start_time":"2021-03-12T06:33:18.017099","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-11-01T06:44:06.410252Z","iopub.execute_input":"2022-11-01T06:44:06.410791Z","iopub.status.idle":"2022-11-01T06:44:06.419810Z","shell.execute_reply.started":"2022-11-01T06:44:06.410756Z","shell.execute_reply":"2022-11-01T06:44:06.418631Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"* Inference section from Fastai-base, which is more flexible than inference-api from mmsegmentation, you may get a better score with proper modification.","metadata":{}},{"cell_type":"markdown","source":"#### My inference","metadata":{"_kg_hide-output":true,"papermill":{"duration":638.710533,"end_time":"2021-03-12T06:44:10.832427","exception":false,"start_time":"2021-03-12T06:33:32.121894","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-06-23T00:49:52.322916Z","iopub.execute_input":"2022-06-23T00:49:52.323682Z","iopub.status.idle":"2022-06-23T00:49:53.476194Z","shell.execute_reply.started":"2022-06-23T00:49:52.323641Z","shell.execute_reply":"2022-06-23T00:49:53.475228Z"}}},{"cell_type":"code","source":"'''\nTo ensemble models, you need to do some modifications with \n\"../input/mmsegm/mmsegmentation-master/mmseg/models/segmentors/encoder_decoder.py\"\n'''\nnames,preds = [],[]\nimgs, pd_mks = [],[]\nmask_map = dict(\n    kidney=0,\n    prostate=1,\n    largeintestine=2,\n    spleen=3,\n    lung=4)\nthr=[0.9, 0.9, 0.9, 0.9, 0.6]\ndebug = len(df_sample)<2\nfor idx,row in tqdm(df_sample.iterrows(),total=len(df_sample)):\n    img = mmcv.imread(os.path.join(DATA,str(idx)+'.tiff'))\n#     used = np.zeros((img.shape[:-1]), dtype=float)\n    masks = np.zeros((img.shape[0], img.shape[1]), dtype=float)\n    pred = None\n    i=0\n    for model in models:\n        result = inference_segmentor(model, img)\n        pred = result[0]\n        pred = pred[mask_map[row[\"organ\"]]+1]*weights[i] # if binary classes, u need to modify this\n        i+=1\n        masks+=pred\n        \n    used = masks/len(models)\n    \n#     used = torch.from_numpy(used)\n    used = (used>thr[mask_map[row[\"organ\"]]]).astype(np.uint8)\n#     print(used)\n#     used = used.argmax(axis=0)\n#     print(used)\n#     used = (used>0).astype(np.uint8)\n    rle = rle_encode_less_memory(used)\n    names.append(str(idx))\n    preds.append(rle)\n    if debug:\n        imgs.append(img)\n        pd_mks.append(used)\n    del img, pred, rle, idx, row\n    gc.collect()","metadata":{"execution":{"iopub.status.busy":"2022-11-01T06:44:06.421733Z","iopub.execute_input":"2022-11-01T06:44:06.422143Z","iopub.status.idle":"2022-11-01T06:44:17.001306Z","shell.execute_reply.started":"2022-11-01T06:44:06.422103Z","shell.execute_reply":"2022-11-01T06:44:16.999300Z"},"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-11-01T06:44:17.002902Z","iopub.execute_input":"2022-11-01T06:44:17.003984Z","iopub.status.idle":"2022-11-01T06:44:18.868537Z","shell.execute_reply.started":"2022-11-01T06:44:17.003942Z","shell.execute_reply":"2022-11-01T06:44:18.867651Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!rm -rf /kaggle/working/*","metadata":{"execution":{"iopub.status.busy":"2022-11-01T06:44:18.871629Z","iopub.execute_input":"2022-11-01T06:44:18.872118Z","iopub.status.idle":"2022-11-01T06:44:19.973254Z","shell.execute_reply.started":"2022-11-01T06:44:18.872084Z","shell.execute_reply":"2022-11-01T06:44:19.971964Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.DataFrame({'id':names,'rle':preds})\ndf.to_csv('submission.csv',index=False)","metadata":{"papermill":{"duration":0.419953,"end_time":"2021-03-12T06:44:11.262501","exception":false,"start_time":"2021-03-12T06:44:10.842548","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-11-01T06:44:19.975170Z","iopub.execute_input":"2022-11-01T06:44:19.975647Z","iopub.status.idle":"2022-11-01T06:44:19.988509Z","shell.execute_reply.started":"2022-11-01T06:44:19.975606Z","shell.execute_reply":"2022-11-01T06:44:19.987561Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df","metadata":{"execution":{"iopub.status.busy":"2022-11-01T06:44:19.990275Z","iopub.execute_input":"2022-11-01T06:44:19.990758Z","iopub.status.idle":"2022-11-01T06:44:20.003506Z","shell.execute_reply.started":"2022-11-01T06:44:19.990658Z","shell.execute_reply":"2022-11-01T06:44:20.002619Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":0.010126,"end_time":"2021-03-12T06:44:11.283196","exception":false,"start_time":"2021-03-12T06:44:11.27307","status":"completed"},"tags":[],"trusted":true},"execution_count":null,"outputs":[]}]}