{"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":"https://www.kaggle.com/hideyukizushi/inf-mmdetection-detectors-303-v2-scale12345?scriptVersionId=82347401","metadata":{}},{"cell_type":"markdown","source":"<h1 style=\"color: #6cb4e4;  text-align: center;  padding: 0.25em;  border-top: solid 2.5px #6cb4e4;  border-bottom: solid 2.5px #6cb4e4;  background: -webkit-repeating-linear-gradient(-45deg, #f0f8ff, #f0f8ff 3px,#e9f4ff 3px, #e9f4ff 7px);  background: repeating-linear-gradient(-45deg, #f0f8ff, #f0f8ff 3px,#e9f4ff 3px, #e9f4ff 7px);height:45px;\">\n<b>Info</b></h1> ","metadata":{}},{"cell_type":"markdown","source":"\n### 801-pseudocheckpointval256-detectors_detectors_htc_r50_1x_coco-\n\n\n\n* Stage2 log\n\n```\n2021-12-23 01:24:15,891 - mmdet - INFO - \nAverage Precision  (AP) @[ IoU=0.50:0.95 | area=   all | maxDets=100 ] = 0.271\nAverage Precision  (AP) @[ IoU=0.50      | area=   all | maxDets=1000 ] = 0.668\nAverage Precision  (AP) @[ IoU=0.75      | area=   all | maxDets=1000 ] = 0.242\nAverage Precision  (AP) @[ IoU=0.50:0.95 | area= small | maxDets=1000 ] = 0.300\nAverage Precision  (AP) @[ IoU=0.50:0.95 | area=medium | maxDets=1000 ] = 0.092\nAverage Precision  (AP) @[ IoU=0.50:0.95 | area= large | maxDets=1000 ] = 0.500\nAverage Recall     (AR) @[ IoU=0.50:0.95 | area=   all | maxDets=100 ] = 0.347\nAverage Recall     (AR) @[ IoU=0.50:0.95 | area=   all | maxDets=300 ] = 0.405\nAverage Recall     (AR) @[ IoU=0.50:0.95 | area=   all | maxDets=1000 ] = 0.405\nAverage Recall     (AR) @[ IoU=0.50:0.95 | area= small | maxDets=1000 ] = 0.397\nAverage Recall     (AR) @[ IoU=0.50:0.95 | area=medium | maxDets=1000 ] = 0.427\nAverage Recall     (AR) @[ IoU=0.50:0.95 | area= large | maxDets=1000 ] = 0.500\n\n2021-12-23 01:24:15,907 - mmdet - INFO - Epoch(val) [5][31]\tsegm_mAP: 0.2710, segm_mAP_50: 0.6680, segm_mAP_75: 0.2420, segm_mAP_s: 0.3000, segm_mAP_m: 0.0920, segm_mAP_l: 0.5000, segm_mAP_copypaste: 0.271 0.668 0.242 0.300 0.092 0.500\n```\n","metadata":{}},{"cell_type":"markdown","source":"<h1 style=\"color: #6cb4e4;  text-align: center;  padding: 0.25em;  border-top: solid 2.5px #6cb4e4;  border-bottom: solid 2.5px #6cb4e4;  background: -webkit-repeating-linear-gradient(-45deg, #f0f8ff, #f0f8ff 3px,#e9f4ff 3px, #e9f4ff 7px);  background: repeating-linear-gradient(-45deg, #f0f8ff, #f0f8ff 3px,#e9f4ff 3px, #e9f4ff 7px);height:45px;\">\n<b>Main</b></h1> ","metadata":{}},{"cell_type":"markdown","source":"<a id=#cbb></a>\n<h2 style=\"color: #6cb4e4; background: #dfefff;  box-shadow: 0px 0px 0px 5px #dfefff;  border: dashed 4px white;  padding: 0.2em 0.5em;\">\n<b>Env\n</b></h2> ","metadata":{}},{"cell_type":"code","source":"ENV_PRE_MODEL='../input/sartorius-detectors-20211211-1600/detectors_htc_r50_1x_coco-329b1453.pth'\nENV_PRE_MODEL_PY='/kaggle/working/mmdetection/configs/detectors/detectors_htc_r50_1x_coco.py'\n# ----------------------------------- #\nENV_MY_MODEL='../input/sartorius-801-20211223133157/801-pseudocheckpointval256-detectors_detectors_htc_r50_1x_coco-/epoch_5.pth'\n# ENV_IMG_SCALE=[(1333, 800), (1690, 960), (1870, 1120), (2133, 1280), (2399, 1440)]\nENV_IMG_SCALE=[(2133, 1280), (2399, 1440)]\n","metadata":{"execution":{"iopub.status.busy":"2021-12-23T13:39:23.976915Z","iopub.execute_input":"2021-12-23T13:39:23.977818Z","iopub.status.idle":"2021-12-23T13:39:24.013776Z","shell.execute_reply.started":"2021-12-23T13:39:23.977685Z","shell.execute_reply":"2021-12-23T13:39:24.012841Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id=#cbb></a>\n<h2 style=\"color: #6cb4e4; background: #dfefff;  box-shadow: 0px 0px 0px 5px #dfefff;  border: dashed 4px white;  padding: 0.2em 0.5em;\">\n<b>Install MMDetection\n</b></h2> ","metadata":{"papermill":{"duration":0.034808,"end_time":"2021-10-28T17:34:14.981619","exception":false,"start_time":"2021-10-28T17:34:14.946811","status":"completed"},"tags":[]}},{"cell_type":"code","source":"!pip install '/kaggle/input/pytorch-170-cuda-toolkit-110221/torch-1.7.0+cu110-cp37-cp37m-linux_x86_64.whl' --no-deps\n!pip install '/kaggle/input/pytorch-170-cuda-toolkit-110221/torchvision-0.8.1+cu110-cp37-cp37m-linux_x86_64.whl' --no-deps\n!pip install '/kaggle/input/pytorch-170-cuda-toolkit-110221/torchaudio-0.7.0-cp37-cp37m-linux_x86_64.whl' --no-deps","metadata":{"_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","papermill":{"duration":127.55412,"end_time":"2021-10-28T17:36:22.571091","exception":false,"start_time":"2021-10-28T17:34:15.016971","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-12-23T13:39:24.015593Z","iopub.execute_input":"2021-12-23T13:39:24.015885Z","iopub.status.idle":"2021-12-23T13:41:21.543409Z","shell.execute_reply.started":"2021-12-23T13:39:24.015848Z","shell.execute_reply":"2021-12-23T13:41:21.542539Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install '/kaggle/input/mmdetectionv2140/addict-2.4.0-py3-none-any.whl' --no-deps\n!pip install '/kaggle/input/mmdetectionv2140/yapf-0.31.0-py2.py3-none-any.whl' --no-deps\n!pip install '/kaggle/input/mmdetectionv2140/terminal-0.4.0-py3-none-any.whl' --no-deps\n!pip install '/kaggle/input/mmdetectionv2140/terminaltables-3.1.0-py3-none-any.whl' --no-deps\n!pip install '/kaggle/input/mmdetectionv2140/mmcv_full-1_3_8-cu110-torch1_7_0/mmcv_full-1.3.8-cp37-cp37m-manylinux1_x86_64.whl' --no-deps\n!pip install '/kaggle/input/mmdetectionv2140/pycocotools-2.0.2/pycocotools-2.0.2' --no-deps\n!pip install '/kaggle/input/mmdetectionv2140/mmpycocotools-12.0.3/mmpycocotools-12.0.3' --no-deps\n\n!rm -rf mmdetection\n\n!cp -r ../input/edited-mmdetection /kaggle/working/\n!mv /kaggle/working/edited-mmdetection /kaggle/working/mmdetection\n%cd /kaggle/working/mmdetection\n!pip install -e .","metadata":{"papermill":{"duration":222.21755,"end_time":"2021-10-28T17:40:04.826612","exception":false,"start_time":"2021-10-28T17:36:22.609062","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-12-23T13:41:21.544999Z","iopub.execute_input":"2021-12-23T13:41:21.54529Z","iopub.status.idle":"2021-12-23T13:44:49.943553Z","shell.execute_reply.started":"2021-12-23T13:41:21.545252Z","shell.execute_reply":"2021-12-23T13:44:49.942638Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install ../../input/ensemble-boxes-104/ensemble_boxes-1.0.4/ -f ./ --no-index\n","metadata":{"execution":{"iopub.status.busy":"2021-12-23T13:44:49.946244Z","iopub.execute_input":"2021-12-23T13:44:49.946537Z","iopub.status.idle":"2021-12-23T13:44:58.804253Z","shell.execute_reply.started":"2021-12-23T13:44:49.946496Z","shell.execute_reply":"2021-12-23T13:44:58.803457Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id=#cbb></a>\n<h2 style=\"color: #6cb4e4; background: #dfefff;  box-shadow: 0px 0px 0px 5px #dfefff;  border: dashed 4px white;  padding: 0.2em 0.5em;\">\n<b>Import Libraries\n</b></h2> ","metadata":{"papermill":{"duration":0.098388,"end_time":"2021-10-28T17:40:04.980758","exception":false,"start_time":"2021-10-28T17:40:04.88237","status":"completed"},"tags":[],"_kg_hide-input":true,"_kg_hide-output":true}},{"cell_type":"code","source":"import torch\nimport torch.nn as nn\nimport torchvision.transforms as transforms\nimport torch.nn.functional as F\nimport sklearn\nimport torchvision\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.preprocessing import LabelEncoder\nimport numpy as np\nimport cupy as cp\nimport gc\nimport pandas as pd\nimport os\nimport matplotlib.pyplot as plt\nimport PIL\nimport json\nfrom PIL import Image, ImageEnhance\nimport albumentations as A\nimport mmdet\nimport mmcv\nfrom albumentations.pytorch import ToTensorV2\nimport seaborn as sns\nimport glob\nfrom pathlib import Path\nimport pycocotools\nfrom pycocotools import mask as maskutils\nimport numpy.random\nimport random\nimport cv2\nimport re\nimport shutil\nfrom mmdet.datasets import build_dataset\nfrom mmdet.models import build_detector\nfrom mmdet.apis import train_detector\nfrom mmdet.apis import inference_detector, init_detector, show_result_pyplot, set_random_seed\nfrom ensemble_boxes import *\nfrom tqdm import tqdm","metadata":{"papermill":{"duration":28.752894,"end_time":"2021-10-28T17:40:33.786328","exception":false,"start_time":"2021-10-28T17:40:05.033434","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-12-23T13:44:58.807112Z","iopub.execute_input":"2021-12-23T13:44:58.807379Z","iopub.status.idle":"2021-12-23T13:45:23.700061Z","shell.execute_reply.started":"2021-12-23T13:44:58.807349Z","shell.execute_reply":"2021-12-23T13:45:23.699266Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%cd ..","metadata":{"papermill":{"duration":0.077806,"end_time":"2021-10-28T17:40:33.91703","exception":false,"start_time":"2021-10-28T17:40:33.839224","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-12-23T13:45:23.701685Z","iopub.execute_input":"2021-12-23T13:45:23.701968Z","iopub.status.idle":"2021-12-23T13:45:23.707342Z","shell.execute_reply.started":"2021-12-23T13:45:23.701926Z","shell.execute_reply":"2021-12-23T13:45:23.706647Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id=#cbb></a>\n<h2 style=\"color: #6cb4e4; background: #dfefff;  box-shadow: 0px 0px 0px 5px #dfefff;  border: dashed 4px white;  padding: 0.2em 0.5em;\">\n<b>Helper Functions\n</b></h2> ","metadata":{"papermill":{"duration":0.052766,"end_time":"2021-10-28T17:40:34.030284","exception":false,"start_time":"2021-10-28T17:40:33.977518","status":"completed"},"tags":[]}},{"cell_type":"code","source":"IMG_WIDTH = 704\nIMG_HEIGHT = 520\nSUBM_PATH='../input/sartorius-cell-instance-segmentation/test'","metadata":{"papermill":{"duration":0.060207,"end_time":"2021-10-28T17:40:34.143033","exception":false,"start_time":"2021-10-28T17:40:34.082826","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-12-23T13:45:23.708782Z","iopub.execute_input":"2021-12-23T13:45:23.709283Z","iopub.status.idle":"2021-12-23T13:45:23.716961Z","shell.execute_reply.started":"2021-12-23T13:45:23.709208Z","shell.execute_reply":"2021-12-23T13:45:23.716302Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def rle_decode(mask_rle, shape):\n    '''\n    mask_rle: run-length as string formated (start length)\n    shape: (height,width) of array to return \n    Returns numpy array, 1 - mask, 0 - background\n\n    '''\n    s = mask_rle.split()\n    starts, lengths = [np.asarray(x, dtype=int) for x in (s[0:][::2], s[1:][::2])]\n    starts -= 1\n    ends = starts + lengths\n    img = np.zeros(shape[0]*shape[1], dtype=np.uint8)\n    for lo, hi in zip(starts, ends):\n        img[lo:hi] = 1\n    return img.reshape(shape)\n\ndef rle_encode(img):\n    '''\n    img: numpy array, 1 - mask, 0 - background\n    Returns run length as string formated\n    '''\n    pixels = img.flatten()\n    pixels = np.concatenate([[0], pixels, [0]])\n    runs = np.where(pixels[1:] != pixels[:-1])[0] + 1\n    runs[1::2] -= runs[::2]\n    return ' '.join(str(x) for x in runs)","metadata":{"papermill":{"duration":0.066347,"end_time":"2021-10-28T17:40:34.262446","exception":false,"start_time":"2021-10-28T17:40:34.196099","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-12-23T13:45:23.71834Z","iopub.execute_input":"2021-12-23T13:45:23.718638Z","iopub.status.idle":"2021-12-23T13:45:23.729429Z","shell.execute_reply.started":"2021-12-23T13:45:23.718603Z","shell.execute_reply":"2021-12-23T13:45:23.728633Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id=#cbb></a>\n<h2 style=\"color: #6cb4e4; background: #dfefff;  box-shadow: 0px 0px 0px 5px #dfefff;  border: dashed 4px white;  padding: 0.2em 0.5em;\">\n<b>Model\n</b></h2> ","metadata":{"papermill":{"duration":0.053488,"end_time":"2021-10-28T17:40:34.718633","exception":false,"start_time":"2021-10-28T17:40:34.665145","status":"completed"},"tags":[]}},{"cell_type":"code","source":"from mmcv import Config\ncfg = Config.fromfile(ENV_PRE_MODEL_PY)","metadata":{"papermill":{"duration":0.083816,"end_time":"2021-10-28T17:40:34.857025","exception":false,"start_time":"2021-10-28T17:40:34.773209","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-12-23T13:45:23.730558Z","iopub.execute_input":"2021-12-23T13:45:23.730891Z","iopub.status.idle":"2021-12-23T13:45:23.769813Z","shell.execute_reply.started":"2021-12-23T13:45:23.730855Z","shell.execute_reply":"2021-12-23T13:45:23.769169Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cfg.model.roi_head.pop('semantic_roi_extractor')\ncfg.model.roi_head.pop('semantic_head')\n\nfor head in cfg.model.roi_head.bbox_head:\n    head.num_classes = 3\n\nfor head in cfg.model.roi_head.mask_head:\n    head.num_classes=3","metadata":{"execution":{"iopub.status.busy":"2021-12-23T13:45:23.772819Z","iopub.execute_input":"2021-12-23T13:45:23.773059Z","iopub.status.idle":"2021-12-23T13:45:23.778136Z","shell.execute_reply.started":"2021-12-23T13:45:23.77303Z","shell.execute_reply":"2021-12-23T13:45:23.776524Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cfg.test_pipeline = [\n    dict(type='LoadImageFromFile'),\n    dict(\n        type='MultiScaleFlipAug',\n        img_scale=ENV_IMG_SCALE,\n        flip=True,\n        transforms=[\n            dict(type='Resize', keep_ratio=True),\n            dict(type='RandomFlip',\n                 flip_ratio=[0.4,0.3,0.3],\n                 #flip_ratio=0.5,\n                 direction= ['horizontal', 'vertical','diagonal' ]),\n                 #direction= 'horizontal' ),\n            dict(\n                type='Normalize',\n#                 mean=[127.96497969, 127.96497969, 127.96497969],\n#                 std=[13.68662336, 13.68662336, 13.68662336],\n                mean=[128, 128, 128],\n                std=[11.58, 11.58, 11.58],\n                to_rgb=True),\n            dict(type='Pad', size_divisor=32),\n            dict(type='ImageToTensor', keys=['img']),\n            dict(type='Collect', keys=['img'])\n        ])\n]\n \ncfg.anchor_generator=dict( \n            type='AnchorGenerator',\n            scales=[8], \n            ratios=[0.3, 0.5, 0.65,1,1.25 ,1.3],  # The ratio between height and width.\n            strides=[4, 8, 16, 32, 64])\n \ncfg.data.test.pipeline = cfg.test_pipeline\ncfg.model.test_cfg.rcnn.max_per_img = 1000\ncfg.model.test_cfg.rpn.min_bbox_size=10\ncfg.load_from = ENV_PRE_MODEL\ncfg.work_dir = '/kaggle/working/model_output'\n\ncfg.data.samples_per_gpu = 2\ncfg.data.workers_per_gpu = 2\n\ncfg.img_norm_cfg = dict(  \n#   mean=[127.96497969, 127.96497969, 127.96497969],\n#   std=[13.68662336, 13.68662336, 13.68662336],\n    mean=[128, 128, 128],  \n    std=[11.58, 11.58, 11.58],  \n    to_rgb=True)\n\ncfg.seed = 0\nset_random_seed(0, deterministic=False)\ncfg.gpu_ids = range(1)\ncfg.fp16 = dict(loss_scale=512.0)\nmeta = dict()\nmeta['config'] = cfg.pretty_text\n\n# print(f'Config:\\n{cfg.pretty_text}')","metadata":{"papermill":{"duration":0.784288,"end_time":"2021-10-28T17:40:35.695074","exception":false,"start_time":"2021-10-28T17:40:34.910786","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-12-23T13:45:23.779505Z","iopub.execute_input":"2021-12-23T13:45:23.779872Z","iopub.status.idle":"2021-12-23T13:45:24.604525Z","shell.execute_reply.started":"2021-12-23T13:45:23.779837Z","shell.execute_reply":"2021-12-23T13:45:24.603762Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(f'Config:\\n{cfg.pretty_text}')","metadata":{"execution":{"iopub.status.busy":"2021-12-23T13:45:24.605838Z","iopub.execute_input":"2021-12-23T13:45:24.606106Z","iopub.status.idle":"2021-12-23T13:45:25.304224Z","shell.execute_reply.started":"2021-12-23T13:45:24.606071Z","shell.execute_reply":"2021-12-23T13:45:25.302459Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id=#cbb></a>\n<h2 style=\"color: #6cb4e4; background: #dfefff;  box-shadow: 0px 0px 0px 5px #dfefff;  border: dashed 4px white;  padding: 0.2em 0.5em;\">\n<b>Inference\n</b></h2> ","metadata":{"papermill":{"duration":0.0544,"end_time":"2021-10-28T17:40:35.805148","exception":false,"start_time":"2021-10-28T17:40:35.750748","status":"completed"},"tags":[]}},{"cell_type":"code","source":"confidence_thresholds = {0: 0.25, 1: 0.55, 2: 0.35}\n# {0: 0.15, 1: 0.4, 2: 0.5} ## shs5ys astro cort\n# {0: 0.15, 1: 0.55, 2: 0.35} ## shs5ys astro cort\n# {0: 0.25, 1: 0.55, 2: 0.35} ## shs5ys astro cort","metadata":{"execution":{"iopub.status.busy":"2021-12-23T13:45:25.305541Z","iopub.execute_input":"2021-12-23T13:45:25.305922Z","iopub.status.idle":"2021-12-23T13:45:25.310468Z","shell.execute_reply.started":"2021-12-23T13:45:25.305882Z","shell.execute_reply":"2021-12-23T13:45:25.309529Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"segms = []\nfiles = []","metadata":{"papermill":{"duration":0.063514,"end_time":"2021-10-28T17:40:36.292327","exception":false,"start_time":"2021-10-28T17:40:36.228813","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-12-23T13:45:25.312355Z","iopub.execute_input":"2021-12-23T13:45:25.312941Z","iopub.status.idle":"2021-12-23T13:45:25.320966Z","shell.execute_reply.started":"2021-12-23T13:45:25.312904Z","shell.execute_reply":"2021-12-23T13:45:25.320255Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"MIN_PIXELS = [75, 150, 75]\nMODELS = []\n\nmodel = init_detector(cfg, ENV_MY_MODEL)\nMODELS.append(model)","metadata":{"execution":{"iopub.status.busy":"2021-12-23T13:45:25.322092Z","iopub.execute_input":"2021-12-23T13:45:25.322414Z","iopub.status.idle":"2021-12-23T13:45:41.117354Z","shell.execute_reply.started":"2021-12-23T13:45:25.322379Z","shell.execute_reply":"2021-12-23T13:45:41.116444Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from scipy.ndimage.morphology import binary_fill_holes\nfrom skimage.morphology import dilation, erosion\n\ndef  postprocess_masks(masks):\n    \n    for mm in range(masks.shape[-1]):\n        # Fill holes inside the mask\n        #mask = binary_fill_holes(masks[..., mm]).astype(np.uint8)\n        # Smoothen edges using dilation and erosion\n        mask = erosion(dilation(mask))\n        # Delete overlaps\n        overlap += mask\n        mask[overlap > 1] = 0\n        out_label = label(mask)\n        # Remove all the pieces if there are more than one pieces\n        if out_label.max() > 1:\n            mask[()] = 0\n\n        result['masks'][..., mm] = mask\n    \n    ","metadata":{"execution":{"iopub.status.busy":"2021-12-23T13:45:41.118822Z","iopub.execute_input":"2021-12-23T13:45:41.119071Z","iopub.status.idle":"2021-12-23T13:45:41.125381Z","shell.execute_reply.started":"2021-12-23T13:45:41.119036Z","shell.execute_reply":"2021-12-23T13:45:41.124519Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def nms_predictions(classes, scores, bboxes, masks, \n                    iou_th=.35, shape=(520, 704)):\n    he, wd = shape[0], shape[1]\n    boxes_list = [[x[0] / wd, x[1] / he, x[2] / wd, x[3] / he]\n                  for x in bboxes]\n    scores_list = [x for x in scores]\n    labels_list = [x for x in classes]\n    nms_bboxes, nms_scores, nms_classes = nms(\n        boxes=[boxes_list], \n        scores=[scores_list], \n        labels=[labels_list], \n        weights=None,\n        iou_thr=iou_th\n    )\n    nms_masks = []\n    for s in nms_scores:\n        nms_masks.append(masks[scores.index(s)])\n    nms_scores, nms_classes, nms_masks = zip(*sorted(zip(nms_scores, nms_classes, nms_masks), reverse=True))\n    return nms_classes, nms_scores, nms_masks\n\n\ndef ensemble_pred_masks(masks, classes, min_pixels=MIN_PIXELS, shape=(520, 704)):\n    result = []\n    pred_class = max(set(classes), key=classes.count)\n    used = np.zeros(shape, dtype=int) \n\n    prev_masks = []\n    for i, mask in enumerate(masks):\n        cont, hier = cv2.findContours(np.array(mask,dtype=np.uint8),cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)\n        if len(cont)>0:\n            for cnt in cont:\n                convex_mask = cv2.fillConvexPoly(np.zeros_like(np.array(mask,dtype=np.uint8)),points=cnt, color=1)\n                fillornot = len(pd.Series((convex_mask==mask).flatten()).value_counts())\n                if fillornot>1: #fill\n                    mask = convex_mask\n                #before= mask.sum()\n                mask = erosion(dilation(mask))  #post processing \n                #if before!=mask.sum():\n                #    print('after',mask.sum(),'before',before)\n                mask = mask * (1-used)\n                if mask.sum() >= min_pixels[pred_class]: # skip predictions with small area\n                    used += mask \n                    result.append(rle_encode(mask))\n        \n    return result","metadata":{"execution":{"iopub.status.busy":"2021-12-23T13:45:41.126726Z","iopub.execute_input":"2021-12-23T13:45:41.127117Z","iopub.status.idle":"2021-12-23T13:45:41.142089Z","shell.execute_reply.started":"2021-12-23T13:45:41.127081Z","shell.execute_reply":"2021-12-23T13:45:41.141319Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#np.argwhere(np.array(c)==np.max(c))[0][0]\n\n#classe.shape","metadata":{"execution":{"iopub.status.busy":"2021-12-23T13:45:41.143201Z","iopub.execute_input":"2021-12-23T13:45:41.143768Z","iopub.status.idle":"2021-12-23T13:45:41.155387Z","shell.execute_reply.started":"2021-12-23T13:45:41.143733Z","shell.execute_reply":"2021-12-23T13:45:41.154746Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"subm_ids, subm_masks = [], []\ntest_names = os.listdir(SUBM_PATH)\n\nfor test_name in tqdm(test_names):\n    for i, model in enumerate(MODELS):\n        img = mmcv.imread('../input/sartorius-cell-instance-segmentation/test/' + test_name) #file)\n        result = inference_detector(model, img)\n        \n        previous_masks = []\n        classes_nms = []\n        scores_nms = []\n        bboxes_nms = []\n        masks_nms = []\n        \n        #find the max class \n        c = []\n        for i, classe in enumerate(result[0]):\n            #print(classe.shape)\n            c.append(classe.shape[0])\n        \n        maxclass = np.argwhere(np.array(c)==np.max(c))[0][0]\n        #print(c,test_name,classe.shape[0],np.max(c))\n        for i, classe in enumerate(result[0]):\n            if i==maxclass: #classe.shape != (0, 5):\n                bbs = classe\n                sgs = result[1][i]\n\n                for bb, sg in zip(bbs,sgs):\n                    box = bb[:4]\n                    cnf = bb[4]\n\n                    if cnf >= confidence_thresholds[i]:\n                        mask = np.array(sg,dtype=np.uint8)  \n                        previous_masks.append(mask)\n                        scores_nms.extend([cnf])\n                        bboxes_nms.extend([box.tolist()])\n\n        masks_nms = previous_masks\n        classes_nms = [i] * len(previous_masks)\n        classes_nms, scores_nms, masks_nms = nms_predictions(classes_nms, scores_nms, bboxes_nms, masks_nms) \n        encoded_masks = ensemble_pred_masks(masks_nms, classes_nms) \n\n        for enc_mask in encoded_masks:\n            subm_ids.append(test_name[:test_name.find('.')])\n            subm_masks.append(enc_mask)\n","metadata":{"execution":{"iopub.status.busy":"2021-12-23T13:45:41.156895Z","iopub.execute_input":"2021-12-23T13:45:41.157229Z","iopub.status.idle":"2021-12-23T13:45:56.968374Z","shell.execute_reply.started":"2021-12-23T13:45:41.15719Z","shell.execute_reply":"2021-12-23T13:45:56.96764Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pd.DataFrame({\n    'id': subm_ids, \n    'predicted': subm_masks\n}).to_csv('submission.csv', index=False)\npd.read_csv('submission.csv').head()","metadata":{"execution":{"iopub.status.busy":"2021-12-23T13:45:56.969577Z","iopub.execute_input":"2021-12-23T13:45:56.97028Z","iopub.status.idle":"2021-12-23T13:45:57.020611Z","shell.execute_reply.started":"2021-12-23T13:45:56.97024Z","shell.execute_reply":"2021-12-23T13:45:57.019867Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pd.read_csv('submission.csv')","metadata":{"execution":{"iopub.status.busy":"2021-12-23T13:45:57.025957Z","iopub.execute_input":"2021-12-23T13:45:57.026575Z","iopub.status.idle":"2021-12-23T13:45:57.050108Z","shell.execute_reply.started":"2021-12-23T13:45:57.02651Z","shell.execute_reply":"2021-12-23T13:45:57.049405Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# def visualize_image(image_id,predrle): #,enc_targs, enc_preds,category, iou):\n\n#     image = cv2.imread(f'../input/sartorius-cell-instance-segmentation/test/{image_id}.png')\n#     image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)\n    \n#     print(f'{image_id}\\n{\"-\" * len(image_id)}')\n#     print(f'Image Mean: {np.mean(image):.4f}  -  Median: {np.median(image):.4f}  -  Std: {np.std(image):.4f} - Min: {np.min(image):.4f} -  Max: {np.max(image):.4f}')\n\n#     fig, axes = plt.subplots(figsize=(20, 20),ncols=2)\n#     fig.tight_layout(pad=5.0)\n    \n#     axes[0].imshow(image, cmap='gray')\n#     masks = []\n#     for mask in predrle:\n#         masks.append(rle_decode(mask, shape=(IMG_HEIGHT,IMG_WIDTH)))\n\n#     mask = np.stack(masks)\n#     mask = np.any(mask == 1, axis=0)\n#     axes[1].imshow(image, cmap='gray')\n#     axes[1].imshow(mask, alpha=0.4)\n    \n    \n#     plt.show()\n#     plt.close(fig)\n\n# tmp =pd.read_csv('submission.csv')\n# rles = tmp[tmp.id=='d48ec7815252']['predicted'].tolist()\n\n# visualize_image(image_id='d48ec7815252',predrle = rles)","metadata":{"execution":{"iopub.status.busy":"2021-12-23T13:45:57.05121Z","iopub.execute_input":"2021-12-23T13:45:57.052276Z","iopub.status.idle":"2021-12-23T13:45:57.069074Z","shell.execute_reply.started":"2021-12-23T13:45:57.052234Z","shell.execute_reply":"2021-12-23T13:45:57.067905Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# tmp1 =pd.read_csv('../input/inf-mmdetection-detectors-303-v2-scale45/submission.csv')\n# rles = tmp1[tmp1.id=='d48ec7815252']['predicted'].tolist()\n\n# visualize_image(image_id='d48ec7815252',predrle = rles)","metadata":{"execution":{"iopub.status.busy":"2021-12-23T13:45:57.070973Z","iopub.execute_input":"2021-12-23T13:45:57.071237Z","iopub.status.idle":"2021-12-23T13:45:57.086449Z","shell.execute_reply.started":"2021-12-23T13:45:57.071204Z","shell.execute_reply":"2021-12-23T13:45:57.081659Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"shutil.rmtree('/kaggle/working/mmdetection')","metadata":{"papermill":{"duration":0.169468,"end_time":"2021-10-28T17:41:35.314035","exception":false,"start_time":"2021-10-28T17:41:35.144567","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-12-23T13:45:57.088014Z","iopub.execute_input":"2021-12-23T13:45:57.088869Z","iopub.status.idle":"2021-12-23T13:45:57.182636Z","shell.execute_reply.started":"2021-12-23T13:45:57.088795Z","shell.execute_reply":"2021-12-23T13:45:57.181678Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}