{"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":"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-13T21:05:37.314214Z","iopub.execute_input":"2021-12-13T21:05:37.314622Z","iopub.status.idle":"2021-12-13T21:07:46.509576Z","shell.execute_reply.started":"2021-12-13T21:05:37.314517Z","shell.execute_reply":"2021-12-13T21:07:46.508676Z"},"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-13T21:07:46.511898Z","iopub.execute_input":"2021-12-13T21:07:46.51218Z","iopub.status.idle":"2021-12-13T21:11:16.784413Z","shell.execute_reply.started":"2021-12-13T21:07:46.512144Z","shell.execute_reply":"2021-12-13T21:11:16.783407Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import sys\nsys.path.append('/kaggle/input/sartorious-code/main_folder/')\nsys.path.append('/kaggle/input/timm-pytorch-image-models/pytorch-image-models-master')","metadata":{"execution":{"iopub.status.busy":"2021-12-13T21:11:16.786007Z","iopub.execute_input":"2021-12-13T21:11:16.786293Z","iopub.status.idle":"2021-12-13T21:11:16.791768Z","shell.execute_reply.started":"2021-12-13T21:11:16.786247Z","shell.execute_reply":"2021-12-13T21:11:16.790524Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Imports","metadata":{}},{"cell_type":"code","source":"import os\nfrom os.path import join as pjoin\nfrom tqdm import tqdm\nfrom copy import deepcopy\nfrom scipy.special import expit\nfrom glob import glob\n\nimport numpy as np\nimport pandas as pd\nimport torch\nimport gc\nimport shutil\n\nfrom mmcv import Config\nfrom code_base.datasets import MMDETCellDataset\nfrom code_base.models import DetectionWrapper\nfrom code_base.forwards import MMDetectionForward\nfrom code_base.utils import CELL_TYPE_MAPPING\nfrom code_base.inference_functions.inference_utils import (\n    get_nn_models,\n)\nfrom code_base.inference_functions.mmdet_inference_utils import mmdet_test_prediction_with_encoding_loop\nfrom code_base.utils import combine_masks, filter_pred_sample, iou_map, HEIGHT, WIDTH, combine_masks\n\nimport seaborn as sns\nfrom matplotlib import pyplot as plt\n\nfrom code_base.utils import collect_all_gc\nfrom code_base.utils import weighted_boxes_fusion\nfrom code_base.utils import rle_encoding\n\n%matplotlib inline","metadata":{"execution":{"iopub.status.busy":"2021-12-13T21:11:25.92531Z","iopub.execute_input":"2021-12-13T21:11:25.925906Z","iopub.status.idle":"2021-12-13T21:11:52.39278Z","shell.execute_reply.started":"2021-12-13T21:11:25.925869Z","shell.execute_reply":"2021-12-13T21:11:52.391953Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%cd ..","metadata":{"execution":{"iopub.status.busy":"2021-12-13T21:11:52.394829Z","iopub.execute_input":"2021-12-13T21:11:52.395259Z","iopub.status.idle":"2021-12-13T21:11:52.40149Z","shell.execute_reply.started":"2021-12-13T21:11:52.39522Z","shell.execute_reply":"2021-12-13T21:11:52.400485Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Model Config","metadata":{}},{"cell_type":"code","source":"# Model config\ncfg = Config.fromfile('/kaggle/working/mmdetection/configs/cascade_rcnn/cascade_mask_rcnn_x101_64x4d_fpn_mstrain_3x_coco.py')\ncfg.model = dict(\n        type='CascadeRCNN',\n        backbone=dict(\n            type='ResNeXt',\n            depth=101,\n            groups=64,\n            base_width=4,\n            num_stages=4,\n            out_indices=(0, 1, 2, 3),\n            frozen_stages=1,\n            norm_cfg=dict(type='BN', requires_grad=True),\n            style='pytorch',\n            init_cfg=dict(\n                type='Pretrained', checkpoint='open-mmlab://resnext101_64x4d')),\n        neck=dict(\n            type='FPN',\n            in_channels=[256, 512, 1024, 2048],\n            out_channels=256,\n            num_outs=5),\n        rpn_head=dict(\n            type='RPNHead',\n            in_channels=256,\n            feat_channels=256,\n            anchor_generator=dict(\n                type='AnchorGenerator',\n                scales=[8],\n                ratios=[0.5, 1.0, 2.0],\n                strides=[4, 8, 16, 32, 64]),\n            bbox_coder=dict(\n                type='DeltaXYWHBBoxCoder',\n                target_means=[.0, .0, .0, .0],\n                target_stds=[1.0, 1.0, 1.0, 1.0]),\n            loss_cls=dict(\n                type='CrossEntropyLoss', use_sigmoid=True, loss_weight=1.0),\n            loss_bbox=dict(type='SmoothL1Loss', beta=1.0 / 9.0, loss_weight=1.0)),\n        roi_head=dict(\n            type='CascadeRoIHead',\n            num_stages=3,\n            stage_loss_weights=[1, 0.5, 0.25],\n            bbox_roi_extractor=dict(\n                type='SingleRoIExtractor',\n                roi_layer=dict(type='RoIAlign', output_size=7, sampling_ratio=0),\n                out_channels=256,\n                featmap_strides=[4, 8, 16, 32]),\n            bbox_head=[\n                dict(\n                    type='Shared2FCBBoxHead',\n                    in_channels=256,\n                    fc_out_channels=1024,\n                    roi_feat_size=7,\n                    num_classes=3,\n                    bbox_coder=dict(\n                        type='DeltaXYWHBBoxCoder',\n                        target_means=[0., 0., 0., 0.],\n                        target_stds=[0.1, 0.1, 0.2, 0.2]),\n                    reg_class_agnostic=True,\n                    loss_cls=dict(\n                        type='CrossEntropyLoss',\n                        use_sigmoid=False,\n                        loss_weight=1.0),\n                    loss_bbox=dict(type='SmoothL1Loss', beta=1.0,\n                                loss_weight=1.0)),\n                dict(\n                    type='Shared2FCBBoxHead',\n                    in_channels=256,\n                    fc_out_channels=1024,\n                    roi_feat_size=7,\n                    num_classes=3,\n                    bbox_coder=dict(\n                        type='DeltaXYWHBBoxCoder',\n                        target_means=[0., 0., 0., 0.],\n                        target_stds=[0.05, 0.05, 0.1, 0.1]),\n                    reg_class_agnostic=True,\n                    loss_cls=dict(\n                        type='CrossEntropyLoss',\n                        use_sigmoid=False,\n                        loss_weight=1.0),\n                    loss_bbox=dict(type='SmoothL1Loss', beta=1.0,\n                                loss_weight=1.0)),\n                dict(\n                    type='Shared2FCBBoxHead',\n                    in_channels=256,\n                    fc_out_channels=1024,\n                    roi_feat_size=7,\n                    num_classes=3,\n                    bbox_coder=dict(\n                        type='DeltaXYWHBBoxCoder',\n                        target_means=[0., 0., 0., 0.],\n                        target_stds=[0.033, 0.033, 0.067, 0.067]),\n                    reg_class_agnostic=True,\n                    loss_cls=dict(\n                        type='CrossEntropyLoss',\n                        use_sigmoid=False,\n                        loss_weight=1.0),\n                    loss_bbox=dict(type='SmoothL1Loss', beta=1.0, loss_weight=1.0))\n            ],\n            mask_roi_extractor=dict(\n                type='SingleRoIExtractor',\n                roi_layer=dict(type='RoIAlign', output_size=14, sampling_ratio=0),\n                out_channels=256,\n                featmap_strides=[4, 8, 16, 32]),\n            mask_head=dict(\n                type='FCNMaskHead',\n                num_convs=4,\n                in_channels=256,\n                conv_out_channels=256,\n                num_classes=3,\n                loss_mask=dict(\n                    type='CrossEntropyLoss', use_mask=True, loss_weight=1.0))),\n        # model training and testing settings\n        train_cfg=dict(\n            rpn=dict(\n                assigner=dict(\n                    type='MaxIoUAssigner',\n                    pos_iou_thr=0.7,\n                    neg_iou_thr=0.3,\n                    min_pos_iou=0.3,\n                    match_low_quality=True,\n                    ignore_iof_thr=-1),\n                sampler=dict(\n                    type='RandomSampler',\n                    num=256,\n                    pos_fraction=0.5,\n                    neg_pos_ub=-1,\n                    add_gt_as_proposals=False),\n                allowed_border=0,\n                pos_weight=-1,\n                debug=False),\n            rpn_proposal=dict(\n                nms_pre=2000,\n                max_per_img=2000,\n                nms=dict(type='nms', iou_threshold=0.7),\n                min_bbox_size=0),\n            rcnn=[\n                dict(\n                    assigner=dict(\n                        type='MaxIoUAssigner',\n                        pos_iou_thr=0.5,\n                        neg_iou_thr=0.5,\n                        min_pos_iou=0.5,\n                        match_low_quality=False,\n                        ignore_iof_thr=-1),\n                    sampler=dict(\n                        type='RandomSampler',\n                        num=512,\n                        pos_fraction=0.25,\n                        neg_pos_ub=-1,\n                        add_gt_as_proposals=True),\n                    mask_size=28,\n                    pos_weight=-1,\n                    debug=False),\n                dict(\n                    assigner=dict(\n                        type='MaxIoUAssigner',\n                        pos_iou_thr=0.6,\n                        neg_iou_thr=0.6,\n                        min_pos_iou=0.6,\n                        match_low_quality=False,\n                        ignore_iof_thr=-1),\n                    sampler=dict(\n                        type='RandomSampler',\n                        num=512,\n                        pos_fraction=0.25,\n                        neg_pos_ub=-1,\n                        add_gt_as_proposals=True),\n                    mask_size=28,\n                    pos_weight=-1,\n                    debug=False),\n                dict(\n                    assigner=dict(\n                        type='MaxIoUAssigner',\n                        pos_iou_thr=0.7,\n                        neg_iou_thr=0.7,\n                        min_pos_iou=0.7,\n                        match_low_quality=False,\n                        ignore_iof_thr=-1),\n                    sampler=dict(\n                        type='RandomSampler',\n                        num=512,\n                        pos_fraction=0.25,\n                        neg_pos_ub=-1,\n                        add_gt_as_proposals=True),\n                    mask_size=28,\n                    pos_weight=-1,\n                    debug=False)\n            ]),\n        test_cfg=dict(\n            rpn=dict(\n                nms_pre=1000,\n                max_per_img=1000,\n                nms=dict(type='nms', iou_threshold=0.7),\n                min_bbox_size=0),\n            rcnn=dict(\n                score_thr=0.05,\n                nms=dict(type='nms', iou_threshold=0.5),\n                max_per_img=600,\n                mask_thr_binary=-1))\n)\n\n# cfg = Config.fromfile('mmdetection/configs/queryinst/queryinst_r101_fpn_300_proposals_crop_mstrain_480-800_3x_coco.py')\n\n# cfg.num_proposals = 1000\n\n# cfg.model = dict(\n#         type='QueryInst',\n#         backbone=dict(\n#             type='ResNet',\n#             depth=101,\n#             num_stages=4,\n#             out_indices=(0, 1, 2, 3),\n#             frozen_stages=1,\n#             norm_cfg=dict(type='BN', requires_grad=True),\n#             norm_eval=True,\n#             style='pytorch',\n#             init_cfg=dict(type='Pretrained',\n#                         checkpoint='torchvision://resnet101')),\n#         neck=dict(\n#             type='FPN',\n#             in_channels=[256, 512, 1024, 2048],\n#             out_channels=256,\n#             start_level=0,\n#             add_extra_convs='on_input',\n#             num_outs=4),\n#         rpn_head=dict(\n#             type='EmbeddingRPNHead',\n#             num_proposals=cfg.num_proposals,\n#             proposal_feature_channel=256),\n#         roi_head=dict(\n#             type='SparseRoIHead',\n#             num_stages=6,\n#             stage_loss_weights=[1, 1, 1, 1, 1, 1],\n#             proposal_feature_channel=256,\n#             bbox_roi_extractor=dict(\n#                 type='SingleRoIExtractor',\n#                 roi_layer=dict(type='RoIAlign', output_size=7, sampling_ratio=2),\n#                 out_channels=256,\n#                 featmap_strides=[4, 8, 16, 32]),\n#             mask_roi_extractor=dict(\n#                 type='SingleRoIExtractor',\n#                 roi_layer=dict(type='RoIAlign', output_size=14, sampling_ratio=2),\n#                 out_channels=256,\n#                 featmap_strides=[4, 8, 16, 32]),\n#             bbox_head=[\n#                 dict(\n#                     type='DIIHead',\n#                     num_classes=3,\n#                     num_ffn_fcs=2,\n#                     num_heads=8,\n#                     num_cls_fcs=1,\n#                     num_reg_fcs=3,\n#                     feedforward_channels=2048,\n#                     in_channels=256,\n#                     dropout=0.0,\n#                     ffn_act_cfg=dict(type='ReLU', inplace=True),\n#                     dynamic_conv_cfg=dict(\n#                         type='DynamicConv',\n#                         in_channels=256,\n#                         feat_channels=64,\n#                         out_channels=256,\n#                         input_feat_shape=7,\n#                         act_cfg=dict(type='ReLU', inplace=True),\n#                         norm_cfg=dict(type='LN')),\n#                     loss_bbox=dict(type='L1Loss', loss_weight=5.0),\n#                     loss_iou=dict(type='GIoULoss', loss_weight=2.0),\n#                     loss_cls=dict(\n#                         type='FocalLoss',\n#                         use_sigmoid=True,\n#                         gamma=2.0,\n#                         alpha=0.25,\n#                         loss_weight=2.0),\n#                     bbox_coder=dict(\n#                         type='DeltaXYWHBBoxCoder',\n#                         clip_border=False,\n#                         target_means=[0.0, 0.0, 0.0, 0.0],\n#                         target_stds=[0.5, 0.5, 1.0, 1.0])),\n#                 dict(\n#                     type='DIIHead',\n#                     num_classes=3,\n#                     num_ffn_fcs=2,\n#                     num_heads=8,\n#                     num_cls_fcs=1,\n#                     num_reg_fcs=3,\n#                     feedforward_channels=2048,\n#                     in_channels=256,\n#                     dropout=0.0,\n#                     ffn_act_cfg=dict(type='ReLU', inplace=True),\n#                     dynamic_conv_cfg=dict(\n#                         type='DynamicConv',\n#                         in_channels=256,\n#                         feat_channels=64,\n#                         out_channels=256,\n#                         input_feat_shape=7,\n#                         act_cfg=dict(type='ReLU', inplace=True),\n#                         norm_cfg=dict(type='LN')),\n#                     loss_bbox=dict(type='L1Loss', loss_weight=5.0),\n#                     loss_iou=dict(type='GIoULoss', loss_weight=2.0),\n#                     loss_cls=dict(\n#                         type='FocalLoss',\n#                         use_sigmoid=True,\n#                         gamma=2.0,\n#                         alpha=0.25,\n#                         loss_weight=2.0),\n#                     bbox_coder=dict(\n#                         type='DeltaXYWHBBoxCoder',\n#                         clip_border=False,\n#                         target_means=[0.0, 0.0, 0.0, 0.0],\n#                         target_stds=[0.5, 0.5, 1.0, 1.0])),\n#                 dict(\n#                     type='DIIHead',\n#                     num_classes=3,\n#                     num_ffn_fcs=2,\n#                     num_heads=8,\n#                     num_cls_fcs=1,\n#                     num_reg_fcs=3,\n#                     feedforward_channels=2048,\n#                     in_channels=256,\n#                     dropout=0.0,\n#                     ffn_act_cfg=dict(type='ReLU', inplace=True),\n#                     dynamic_conv_cfg=dict(\n#                         type='DynamicConv',\n#                         in_channels=256,\n#                         feat_channels=64,\n#                         out_channels=256,\n#                         input_feat_shape=7,\n#                         act_cfg=dict(type='ReLU', inplace=True),\n#                         norm_cfg=dict(type='LN')),\n#                     loss_bbox=dict(type='L1Loss', loss_weight=5.0),\n#                     loss_iou=dict(type='GIoULoss', loss_weight=2.0),\n#                     loss_cls=dict(\n#                         type='FocalLoss',\n#                         use_sigmoid=True,\n#                         gamma=2.0,\n#                         alpha=0.25,\n#                         loss_weight=2.0),\n#                     bbox_coder=dict(\n#                         type='DeltaXYWHBBoxCoder',\n#                         clip_border=False,\n#                         target_means=[0.0, 0.0, 0.0, 0.0],\n#                         target_stds=[0.5, 0.5, 1.0, 1.0])),\n#                 dict(\n#                     type='DIIHead',\n#                     num_classes=3,\n#                     num_ffn_fcs=2,\n#                     num_heads=8,\n#                     num_cls_fcs=1,\n#                     num_reg_fcs=3,\n#                     feedforward_channels=2048,\n#                     in_channels=256,\n#                     dropout=0.0,\n#                     ffn_act_cfg=dict(type='ReLU', inplace=True),\n#                     dynamic_conv_cfg=dict(\n#                         type='DynamicConv',\n#                         in_channels=256,\n#                         feat_channels=64,\n#                         out_channels=256,\n#                         input_feat_shape=7,\n#                         act_cfg=dict(type='ReLU', inplace=True),\n#                         norm_cfg=dict(type='LN')),\n#                     loss_bbox=dict(type='L1Loss', loss_weight=5.0),\n#                     loss_iou=dict(type='GIoULoss', loss_weight=2.0),\n#                     loss_cls=dict(\n#                         type='FocalLoss',\n#                         use_sigmoid=True,\n#                         gamma=2.0,\n#                         alpha=0.25,\n#                         loss_weight=2.0),\n#                     bbox_coder=dict(\n#                         type='DeltaXYWHBBoxCoder',\n#                         clip_border=False,\n#                         target_means=[0.0, 0.0, 0.0, 0.0],\n#                         target_stds=[0.5, 0.5, 1.0, 1.0])),\n#                 dict(\n#                     type='DIIHead',\n#                     num_classes=3,\n#                     num_ffn_fcs=2,\n#                     num_heads=8,\n#                     num_cls_fcs=1,\n#                     num_reg_fcs=3,\n#                     feedforward_channels=2048,\n#                     in_channels=256,\n#                     dropout=0.0,\n#                     ffn_act_cfg=dict(type='ReLU', inplace=True),\n#                     dynamic_conv_cfg=dict(\n#                         type='DynamicConv',\n#                         in_channels=256,\n#                         feat_channels=64,\n#                         out_channels=256,\n#                         input_feat_shape=7,\n#                         act_cfg=dict(type='ReLU', inplace=True),\n#                         norm_cfg=dict(type='LN')),\n#                     loss_bbox=dict(type='L1Loss', loss_weight=5.0),\n#                     loss_iou=dict(type='GIoULoss', loss_weight=2.0),\n#                     loss_cls=dict(\n#                         type='FocalLoss',\n#                         use_sigmoid=True,\n#                         gamma=2.0,\n#                         alpha=0.25,\n#                         loss_weight=2.0),\n#                     bbox_coder=dict(\n#                         type='DeltaXYWHBBoxCoder',\n#                         clip_border=False,\n#                         target_means=[0.0, 0.0, 0.0, 0.0],\n#                         target_stds=[0.5, 0.5, 1.0, 1.0])),\n#                 dict(\n#                     type='DIIHead',\n#                     num_classes=3,\n#                     num_ffn_fcs=2,\n#                     num_heads=8,\n#                     num_cls_fcs=1,\n#                     num_reg_fcs=3,\n#                     feedforward_channels=2048,\n#                     in_channels=256,\n#                     dropout=0.0,\n#                     ffn_act_cfg=dict(type='ReLU', inplace=True),\n#                     dynamic_conv_cfg=dict(\n#                         type='DynamicConv',\n#                         in_channels=256,\n#                         feat_channels=64,\n#                         out_channels=256,\n#                         input_feat_shape=7,\n#                         act_cfg=dict(type='ReLU', inplace=True),\n#                         norm_cfg=dict(type='LN')),\n#                     loss_bbox=dict(type='L1Loss', loss_weight=5.0),\n#                     loss_iou=dict(type='GIoULoss', loss_weight=2.0),\n#                     loss_cls=dict(\n#                         type='FocalLoss',\n#                         use_sigmoid=True,\n#                         gamma=2.0,\n#                         alpha=0.25,\n#                         loss_weight=2.0),\n#                     bbox_coder=dict(\n#                         type='DeltaXYWHBBoxCoder',\n#                         clip_border=False,\n#                         target_means=[0.0, 0.0, 0.0, 0.0],\n#                         target_stds=[0.5, 0.5, 1.0, 1.0]))\n#             ],\n#             mask_head=[\n#                 dict(\n#                     type='DynamicMaskHead',\n#                     dynamic_conv_cfg=dict(\n#                         type='DynamicConv',\n#                         in_channels=256,\n#                         feat_channels=64,\n#                         out_channels=256,\n#                         input_feat_shape=14,\n#                         with_proj=False,\n#                         act_cfg=dict(type='ReLU', inplace=True),\n#                         norm_cfg=dict(type='LN')),\n#                     num_convs=4,\n#                     num_classes=3,\n#                     roi_feat_size=14,\n#                     in_channels=256,\n#                     conv_kernel_size=3,\n#                     conv_out_channels=256,\n#                     class_agnostic=False,\n#                     norm_cfg=dict(type='BN'),\n#                     upsample_cfg=dict(type='deconv', scale_factor=2),\n#                     loss_mask=dict(\n#                         type='DiceLoss',\n#                         loss_weight=8.0,\n#                         use_sigmoid=True,\n#                         activate=False,\n#                         eps=1e-05)),\n#                 dict(\n#                     type='DynamicMaskHead',\n#                     dynamic_conv_cfg=dict(\n#                         type='DynamicConv',\n#                         in_channels=256,\n#                         feat_channels=64,\n#                         out_channels=256,\n#                         input_feat_shape=14,\n#                         with_proj=False,\n#                         act_cfg=dict(type='ReLU', inplace=True),\n#                         norm_cfg=dict(type='LN')),\n#                     num_convs=4,\n#                     num_classes=3,\n#                     roi_feat_size=14,\n#                     in_channels=256,\n#                     conv_kernel_size=3,\n#                     conv_out_channels=256,\n#                     class_agnostic=False,\n#                     norm_cfg=dict(type='BN'),\n#                     upsample_cfg=dict(type='deconv', scale_factor=2),\n#                     loss_mask=dict(\n#                         type='DiceLoss',\n#                         loss_weight=8.0,\n#                         use_sigmoid=True,\n#                         activate=False,\n#                         eps=1e-05)),\n#                 dict(\n#                     type='DynamicMaskHead',\n#                     dynamic_conv_cfg=dict(\n#                         type='DynamicConv',\n#                         in_channels=256,\n#                         feat_channels=64,\n#                         out_channels=256,\n#                         input_feat_shape=14,\n#                         with_proj=False,\n#                         act_cfg=dict(type='ReLU', inplace=True),\n#                         norm_cfg=dict(type='LN')),\n#                     num_convs=4,\n#                     num_classes=3,\n#                     roi_feat_size=14,\n#                     in_channels=256,\n#                     conv_kernel_size=3,\n#                     conv_out_channels=256,\n#                     class_agnostic=False,\n#                     norm_cfg=dict(type='BN'),\n#                     upsample_cfg=dict(type='deconv', scale_factor=2),\n#                     loss_mask=dict(\n#                         type='DiceLoss',\n#                         loss_weight=8.0,\n#                         use_sigmoid=True,\n#                         activate=False,\n#                         eps=1e-05)),\n#                 dict(\n#                     type='DynamicMaskHead',\n#                     dynamic_conv_cfg=dict(\n#                         type='DynamicConv',\n#                         in_channels=256,\n#                         feat_channels=64,\n#                         out_channels=256,\n#                         input_feat_shape=14,\n#                         with_proj=False,\n#                         act_cfg=dict(type='ReLU', inplace=True),\n#                         norm_cfg=dict(type='LN')),\n#                     num_convs=4,\n#                     num_classes=3,\n#                     roi_feat_size=14,\n#                     in_channels=256,\n#                     conv_kernel_size=3,\n#                     conv_out_channels=256,\n#                     class_agnostic=False,\n#                     norm_cfg=dict(type='BN'),\n#                     upsample_cfg=dict(type='deconv', scale_factor=2),\n#                     loss_mask=dict(\n#                         type='DiceLoss',\n#                         loss_weight=8.0,\n#                         use_sigmoid=True,\n#                         activate=False,\n#                         eps=1e-05)),\n#                 dict(\n#                     type='DynamicMaskHead',\n#                     dynamic_conv_cfg=dict(\n#                         type='DynamicConv',\n#                         in_channels=256,\n#                         feat_channels=64,\n#                         out_channels=256,\n#                         input_feat_shape=14,\n#                         with_proj=False,\n#                         act_cfg=dict(type='ReLU', inplace=True),\n#                         norm_cfg=dict(type='LN')),\n#                     num_convs=4,\n#                     num_classes=3,\n#                     roi_feat_size=14,\n#                     in_channels=256,\n#                     conv_kernel_size=3,\n#                     conv_out_channels=256,\n#                     class_agnostic=False,\n#                     norm_cfg=dict(type='BN'),\n#                     upsample_cfg=dict(type='deconv', scale_factor=2),\n#                     loss_mask=dict(\n#                         type='DiceLoss',\n#                         loss_weight=8.0,\n#                         use_sigmoid=True,\n#                         activate=False,\n#                         eps=1e-05)),\n#                 dict(\n#                     type='DynamicMaskHead',\n#                     dynamic_conv_cfg=dict(\n#                         type='DynamicConv',\n#                         in_channels=256,\n#                         feat_channels=64,\n#                         out_channels=256,\n#                         input_feat_shape=14,\n#                         with_proj=False,\n#                         act_cfg=dict(type='ReLU', inplace=True),\n#                         norm_cfg=dict(type='LN')),\n#                     num_convs=4,\n#                     num_classes=3,\n#                     roi_feat_size=14,\n#                     in_channels=256,\n#                     conv_kernel_size=3,\n#                     conv_out_channels=256,\n#                     class_agnostic=False,\n#                     norm_cfg=dict(type='BN'),\n#                     upsample_cfg=dict(type='deconv', scale_factor=2),\n#                     loss_mask=dict(\n#                         type='DiceLoss',\n#                         loss_weight=8.0,\n#                         use_sigmoid=True,\n#                         activate=False,\n#                         eps=1e-05))\n#             ]),\n#         train_cfg=dict(\n#             rpn=None,\n#             rcnn=[\n#                 dict(\n#                     assigner=dict(\n#                         type='HungarianAssigner',\n#                         cls_cost=dict(type='FocalLossCost', weight=2.0),\n#                         reg_cost=dict(type='BBoxL1Cost', weight=5.0),\n#                         iou_cost=dict(type='IoUCost', iou_mode='giou',\n#                                     weight=2.0)),\n#                     sampler=dict(type='PseudoSampler'),\n#                     pos_weight=1,\n#                     mask_size=28),\n#                 dict(\n#                     assigner=dict(\n#                         type='HungarianAssigner',\n#                         cls_cost=dict(type='FocalLossCost', weight=2.0),\n#                         reg_cost=dict(type='BBoxL1Cost', weight=5.0),\n#                         iou_cost=dict(type='IoUCost', iou_mode='giou',\n#                                     weight=2.0)),\n#                     sampler=dict(type='PseudoSampler'),\n#                     pos_weight=1,\n#                     mask_size=28),\n#                 dict(\n#                     assigner=dict(\n#                         type='HungarianAssigner',\n#                         cls_cost=dict(type='FocalLossCost', weight=2.0),\n#                         reg_cost=dict(type='BBoxL1Cost', weight=5.0),\n#                         iou_cost=dict(type='IoUCost', iou_mode='giou',\n#                                     weight=2.0)),\n#                     sampler=dict(type='PseudoSampler'),\n#                     pos_weight=1,\n#                     mask_size=28),\n#                 dict(\n#                     assigner=dict(\n#                         type='HungarianAssigner',\n#                         cls_cost=dict(type='FocalLossCost', weight=2.0),\n#                         reg_cost=dict(type='BBoxL1Cost', weight=5.0),\n#                         iou_cost=dict(type='IoUCost', iou_mode='giou',\n#                                     weight=2.0)),\n#                     sampler=dict(type='PseudoSampler'),\n#                     pos_weight=1,\n#                     mask_size=28),\n#                 dict(\n#                     assigner=dict(\n#                         type='HungarianAssigner',\n#                         cls_cost=dict(type='FocalLossCost', weight=2.0),\n#                         reg_cost=dict(type='BBoxL1Cost', weight=5.0),\n#                         iou_cost=dict(type='IoUCost', iou_mode='giou',\n#                                     weight=2.0)),\n#                     sampler=dict(type='PseudoSampler'),\n#                     pos_weight=1,\n#                     mask_size=28),\n#                 dict(\n#                     assigner=dict(\n#                         type='HungarianAssigner',\n#                         cls_cost=dict(type='FocalLossCost', weight=2.0),\n#                         reg_cost=dict(type='BBoxL1Cost', weight=5.0),\n#                         iou_cost=dict(type='IoUCost', iou_mode='giou',\n#                                     weight=2.0)),\n#                     sampler=dict(type='PseudoSampler'),\n#                     pos_weight=1,\n#                     mask_size=28)\n#             ]),\n#         test_cfg=dict(rpn=None, rcnn=dict(max_per_img=600, mask_thr_binary=-1))\n# )","metadata":{"execution":{"iopub.status.busy":"2021-12-13T21:11:52.403772Z","iopub.execute_input":"2021-12-13T21:11:52.404073Z","iopub.status.idle":"2021-12-13T21:11:52.479097Z","shell.execute_reply.started":"2021-12-13T21:11:52.404033Z","shell.execute_reply":"2021-12-13T21:11:52.478075Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Config","metadata":{}},{"cell_type":"code","source":"nn_model_class = DetectionWrapper\nnn_model_config = {\n    \"num_classes\": len(CELL_TYPE_MAPPING),\n    \"model_type\": \"mmdetection\",\n    \"mm_det_cfg\": cfg\n}\n\ndataset_class = MMDETCellDataset\nimg_norm_cfg = dict(mean=[123.675, 116.28, 103.53], std=[58.395, 57.12, 57.375], to_rgb=True)\ndataset_config_test = {\n    \"root\": \"../input/sartorius-cell-instance-segmentation/test/\",\n    \"filter_duplicates_in_masks\": False,\n    \"train_preload_pipe\":[\n        dict(type=\"LoadImageFromFile\"),\n        dict(\n            type=\"MultiScaleFlipAug\",\n            img_scale=[(1184, 1184), (1280, 1280), (1376, 1376)],\n            # img_scale=[(768 + 32 * i, 768 + 32 * i) for i in range(20)],\n            flip=True,\n            flip_direction=[\"horizontal\", \"vertical\"],\n#             img_scale=(1376, 1376),\n#             flip=False,\n            transforms=[\n                dict(type=\"Resize\", keep_ratio=True),\n                dict(type=\"RandomFlip\"),\n                dict(type=\"Normalize\", **img_norm_cfg),\n                dict(type=\"Pad\", size_divisor=32),\n                dict(type=\"ImageToTensor\", keys=[\"img\"]),\n                dict(type=\"Collect\", keys=[\"img\"]),\n            ],\n        ),\n    ],\n    \"train_runtime_pipe\": None,\n    \"train_runtime_pipe_in_collate\": False,\n    \"is_val\": True\n}\nbatch_size = 1","metadata":{"execution":{"iopub.status.busy":"2021-12-13T21:11:52.481072Z","iopub.execute_input":"2021-12-13T21:11:52.481372Z","iopub.status.idle":"2021-12-13T21:11:52.490701Z","shell.execute_reply.started":"2021-12-13T21:11:52.481312Z","shell.execute_reply":"2021-12-13T21:11:52.489623Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Utils","metadata":{}},{"cell_type":"code","source":"MIN_SCORE_DICT = {1: 0.5, 2: 0.5, 3: 0.5}\nMASK_TRESHOLD_DICT = {1: 0.5, 2: 0.5, 3:  0.5}\nMIN_AREA = 50\ndef mask_rcnn_pred_postprocess_first(\n    mask, label, score, box,\n    filter_multiple_labels=True, filter_low_scoring_masks=True, filter_small_masks=True, \n    min_score_dict=MIN_SCORE_DICT, mask_treshold_dict=MASK_TRESHOLD_DICT,\n    min_area=MIN_AREA\n):\n    # Filter multiple labels\n    label_v, label_c = np.unique(label, return_counts=True)\n    if len(label_v) == 0:\n        if combine_mask:\n            return np.zeros((HEIGHT, WIDTH))\n        else:\n            return np.zeros((1, HEIGHT, WIDTH))\n    if len(label_v) > 1:\n        selected_value = label_v[np.argmax(label_c)]\n        if filter_multiple_labels:\n            mask = mask[label==selected_value]\n            score = score[label==selected_value]\n            box = box[label==selected_value]\n            label = label[label==selected_value]\n    else:\n        selected_value = label_v[0]\n    # Filter Low scoring masks\n    if filter_low_scoring_masks:\n        mask = mask[score > min_score_dict[selected_value]]\n        label = label[score > min_score_dict[selected_value]]\n        box = box[score > min_score_dict[selected_value]]\n        score = score[score > min_score_dict[selected_value]]\n    # Filter small area masks\n    if filter_small_masks:\n        mask_areas = (mask > mask_treshold_dict[selected_value]).sum((1,2))\n        label = label[mask_areas > min_area]\n        score = score[mask_areas > min_area]\n        mask = mask[mask_areas > min_area]\n        box = box[mask_areas > min_area]\n    return mask, label, score, box\n    \ndef mask_rcnn_pred_postprocess_second(\n    mask, label, score, box,\n    combine_mask=True,\n    mask_treshold_dict=MASK_TRESHOLD_DICT,\n):\n    label_v, label_c = np.unique(label, return_counts=True)\n    if len(label_v) == 0:\n        if combine_mask:\n            return np.zeros((HEIGHT, WIDTH))\n        else:\n            return np.zeros((1, HEIGHT, WIDTH))\n    if len(label_v) > 1:\n        selected_value = label_v[np.argmax(label_c)]\n    else:\n        selected_value = label_v[0]\n    # Mask postprocess\n    filtered_mask = filter_pred_sample(mask, mask_treshold_dict[selected_value])\n    if combine_mask:\n        combined_mask = combine_masks(filtered_mask, mask_treshold_dict[selected_value])\n        return combined_mask\n    else:\n        return (filtered_mask > mask_treshold_dict[selected_value]).astype(np.int64)","metadata":{"execution":{"iopub.status.busy":"2021-12-13T21:11:52.492148Z","iopub.execute_input":"2021-12-13T21:11:52.492694Z","iopub.status.idle":"2021-12-13T21:11:52.511129Z","shell.execute_reply.started":"2021-12-13T21:11:52.492639Z","shell.execute_reply":"2021-12-13T21:11:52.510185Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Main Pred","metadata":{}},{"cell_type":"code","source":"test_df_or = pd.read_csv(\"../input/sartorius-cell-instance-segmentation/sample_submission.csv\")\ntest_df_or","metadata":{"execution":{"iopub.status.busy":"2021-12-13T21:11:52.512497Z","iopub.execute_input":"2021-12-13T21:11:52.513211Z","iopub.status.idle":"2021-12-13T21:11:52.560119Z","shell.execute_reply.started":"2021-12-13T21:11:52.513168Z","shell.execute_reply":"2021-12-13T21:11:52.559232Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# test_df_or = pd.read_csv(\"../input/sartorius-cell-instance-segmentation/train.csv\")\n# test_df_or = pd.DataFrame({\"id\": test_df_or['id'].unique()})\n# test_df_or[\"predicted\"] = None\n# test_df_or","metadata":{"execution":{"iopub.status.busy":"2021-12-13T21:11:52.561686Z","iopub.execute_input":"2021-12-13T21:11:52.561977Z","iopub.status.idle":"2021-12-13T21:11:52.565873Z","shell.execute_reply.started":"2021-12-13T21:11:52.561938Z","shell.execute_reply":"2021-12-13T21:11:52.5649Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"chckps = [glob(f'/kaggle/input/sartorious-models-mmdet/cascade_maskrcnn_panshin_baseline_fromLiveCellpretrain/cascade_maskrcnn_panshin_baseline_fromLiveCellpretrain/fold_{f_i}/best_segm_mAP*.pth')[0] for f_i in [0,1,2,3,4]]\nprint(\"\\n\".join(chckps))\n# chckps = [torch.load(el, map_location=\"cpu\")['state_dict'] for el in chckps]\n# chckps = [{\"model.\" + k:v for k,v in el.items()} for el in chckps]\n# gc.collect()\n# models, device = get_nn_models(\n#     nn_model_class=nn_model_class,\n#     nn_model_config=nn_model_config,\n#     chckps=chckps\n# )\n# del chckps\n# gc.collect()\ndevice = \"cuda\"\nmodels = []\nfor chkp_name in chckps:\n    models.append(nn_model_class(**nn_model_config, device=device))\n    models[-1].model.load_state_dict(torch.load(chkp_name, map_location=device)['state_dict'])\n    models[-1].eval()\n    collect_all_gc()\n# chckps = [torch.load(el, map_location=\"cpu\")['state_dict'] for el in chckps]\n# chckps = [{\"model.\" + k:v for k,v in el.items()} for el in chckps]\n# gc.collect()\n# models, device = get_nn_models(\n#     nn_model_class=nn_model_class,\n#     nn_model_config=nn_model_config,\n#     chckps=chckps\n# )\ndel chckps\ngc.collect()","metadata":{"execution":{"iopub.status.busy":"2021-12-13T21:12:36.979422Z","iopub.execute_input":"2021-12-13T21:12:36.979675Z","iopub.status.idle":"2021-12-13T21:13:35.766988Z","shell.execute_reply.started":"2021-12-13T21:12:36.979646Z","shell.execute_reply":"2021-12-13T21:13:35.76622Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Prediction","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":"test_df = mmdet_test_prediction_with_encoding_loop(\n    models=models,\n    dataset_class=dataset_class,\n    dataset_config_test=dataset_config_test,\n    test_df=test_df_or,\n    forward_class=MMDetectionForward(\n        scale_mask=True, \n        cascade=True, \n        memory_optimized=True,\n        min_score_dict={1: 0.4, 2: 0.6, 3: 0.4}\n    ),\n    device=device,\n    postproc_pred_function=lambda mask, label, score, box: mask_rcnn_pred_postprocess_first(\n        mask, label, score, box,\n        filter_multiple_labels=True, \n        filter_low_scoring_masks=True, \n        filter_small_masks=False,\n        min_score_dict={1: 0.4, 2: 0.6, 3: 0.4},\n    ),\n    preprare_final_sub_function=lambda mask, label, score, box: mask_rcnn_pred_postprocess_second(\n        mask, label, score, box,\n        combine_mask=False\n    ),\n    use_wbf=True,\n    wbf_mask_thresh=0.5,\n    wbf_use_scores=True,\n    wbf_thresh=0.15,\n    pp_in_forward=True\n)","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-13T21:18:17.172518Z","iopub.execute_input":"2021-12-13T21:18:17.173115Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df","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":[],"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df['id'] = test_df['id'].apply(lambda x: os.path.basename(x)[:-4])\ntest_df = test_df.drop_duplicates(subset=[\"id\", \"predicted\"]).reset_index(drop=True)\ntest_df = test_df[test_df['predicted'] != \"\"].reset_index(drop=True)\n\n# Add unlabeled images\nunlabeled_images = set(test_df_or[\"id\"]) - set(test_df[\"id\"])\nif len(unlabeled_images) > 0:\n    unlabeled_images = list(unlabeled_images)\n    unlabeled_df = pd.DataFrame({\n        \"id\": unlabeled_images,\n        \"predicted\": [\"\"]*len(unlabeled_images)\n    })\n    test_df = pd.concat([test_df, unlabeled_df], axis=0).reset_index(drop=True)\ntest_df","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df.to_csv(\"submission.csv\", index=False)\ntest_df.head()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"shutil.rmtree('/kaggle/working/mmdetection')","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}