{"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":"#### 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-08-21T15:14:56.167548Z","iopub.execute_input":"2022-08-21T15:14:56.167880Z","iopub.status.idle":"2022-08-21T15:17:56.403262Z","shell.execute_reply.started":"2022-08-21T15:14:56.167783Z","shell.execute_reply":"2022-08-21T15:17:56.402061Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!cp -r ../input/mmsegm/mmsegmentation-master /kaggle/working/ && cd /kaggle/working/mmsegmentation-master && pip install -e . && cd ..","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2022-08-21T15:23:08.623373Z","iopub.execute_input":"2022-08-21T15:23:08.624134Z","iopub.status.idle":"2022-08-21T15:23:45.843006Z","shell.execute_reply.started":"2022-08-21T15:23:08.624088Z","shell.execute_reply":"2022-08-21T15:23:45.841807Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### Import librarys","metadata":{}},{"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","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-08-21T15:23:45.845848Z","iopub.execute_input":"2022-08-21T15:23:45.846520Z","iopub.status.idle":"2022-08-21T15:23:50.124251Z","shell.execute_reply.started":"2022-08-21T15:23:45.846479Z","shell.execute_reply":"2022-08-21T15:23:50.123170Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### Load models","metadata":{}},{"cell_type":"code","source":"configs = [\n    '../input/segformer3/segformer3.py',\n]\nckpts = [\n    '../input/segformer3/segformer3.pth',\n]\n\nDATA = '../input/hubmap-organ-segmentation/test_images/'\ndf_sample = pd.read_csv('../input/hubmap-organ-segmentation/sample_submission.csv').set_index('id')\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-08-21T15:24:46.666905Z","iopub.execute_input":"2022-08-21T15:24:46.667871Z","iopub.status.idle":"2022-08-21T15:24:50.028221Z","shell.execute_reply.started":"2022-08-21T15:24:46.667830Z","shell.execute_reply":"2022-08-21T15:24:50.027182Z"},"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-08-21T15:24:50.030304Z","iopub.execute_input":"2022-08-21T15:24:50.030713Z","iopub.status.idle":"2022-08-21T15:24:50.038148Z","shell.execute_reply.started":"2022-08-21T15:24:50.030675Z","shell.execute_reply":"2022-08-21T15:24:50.035933Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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 = [],[]\ndebug = len(df_sample)<2\nfor idx,row in tqdm(df_sample.iterrows(),total=len(df_sample)):\n    img  = cv2.imread(os.path.join(DATA,str(idx)+'.tiff'))\n    pred = inference_segmentor(models[0], img)[0]\n    pred = (pred>0).astype(np.uint8)\n    rle = rle_encode_less_memory(pred)\n    names.append(str(idx))\n    preds.append(rle)\n    if debug:\n        imgs.append(img)\n        pd_mks.append(pred)\n    del img, pred, rle, idx, row\n    gc.collect()","metadata":{"execution":{"iopub.status.busy":"2022-08-21T15:24:50.040120Z","iopub.execute_input":"2022-08-21T15:24:50.041209Z","iopub.status.idle":"2022-08-21T15:24:56.849446Z","shell.execute_reply.started":"2022-08-21T15:24:50.041173Z","shell.execute_reply":"2022-08-21T15:24:56.848391Z"},"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-08-21T15:25:18.144403Z","iopub.execute_input":"2022-08-21T15:25:18.144911Z","iopub.status.idle":"2022-08-21T15:25:19.980356Z","shell.execute_reply.started":"2022-08-21T15:25:18.144864Z","shell.execute_reply":"2022-08-21T15:25:19.979453Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!rm -rf /kaggle/working/*","metadata":{"execution":{"iopub.status.busy":"2022-08-21T15:25:23.702016Z","iopub.execute_input":"2022-08-21T15:25:23.703048Z","iopub.status.idle":"2022-08-21T15:25:24.766778Z","shell.execute_reply.started":"2022-08-21T15:25:23.702981Z","shell.execute_reply":"2022-08-21T15:25:24.765377Z"},"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-08-21T15:25:24.769223Z","iopub.execute_input":"2022-08-21T15:25:24.769850Z","iopub.status.idle":"2022-08-21T15:25:24.782812Z","shell.execute_reply.started":"2022-08-21T15:25:24.769812Z","shell.execute_reply":"2022-08-21T15:25:24.781813Z"},"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":[]}]}