{"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":"class INFER_CFG:\n    mmdet_version = 2\n    exp_name = r'0710_htc_db_b_swa12_aug2_d1_e24_d2pretrain'\n    pth_file = r'/kaggle/input/hubmap2023-model-pth/0715_htc_db_b_swa12_512_1536_ddp_3flipsaug_d1/f4_swa_model_5_cv439.pth'\n#     cfg_list = [\n        \n#     ]\n#     pth_list = [\n        \n#     ]\n    \n\ncfg = INFER_CFG\nwrite_dir = r'/kaggle/working/mmdetection-3.0.0/mmdet/models/layers/' if cfg.mmdet_version == 3 else  r'/kaggle/working/mmdetection-2.28.2/mmdet/core/post_processing/'","metadata":{"execution":{"iopub.status.busy":"2023-07-16T14:55:50.51336Z","iopub.execute_input":"2023-07-16T14:55:50.513765Z","iopub.status.idle":"2023-07-16T14:55:50.51974Z","shell.execute_reply.started":"2023-07-16T14:55:50.513732Z","shell.execute_reply":"2023-07-16T14:55:50.518499Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install /kaggle/input/mmdet3-py310-libs/addict-2.4.0-py3-none-any.whl\n!pip install /kaggle/input/mmdet3-py310-libs/yapf-0.33.0-py2.py3-none-any.whl\n!pip install /kaggle/input/mmdet3-py310-libs/terminaltables-3.1.10-py2.py3-none-any.whl\n!pip install /kaggle/input/mmdet3-py310-libs/pycocotools-2.0.6-cp310-cp310-linux_x86_64.whl\n!pip install /kaggle/input/mmdet3-py310-libs/einops-0.4.1-py3-none-any.whl\n!pip install /kaggle/input/mmdet3-py310-libs/mat4py-0.5.0-py2.py3-none-any.whl\n!pip install /kaggle/input/mmdet3-py310-libs/ordered_set-4.1.0-py3-none-any.whl\n!pip install /kaggle/input/mmdet3-py310-libs/model_index-0.1.11-py3-none-any.whl\n!pip install /kaggle/input/mmdet3-py310-libs/modelindex-0.0.2-py3-none-any.whl","metadata":{"execution":{"iopub.status.busy":"2023-07-16T14:55:50.522321Z","iopub.execute_input":"2023-07-16T14:55:50.522748Z","iopub.status.idle":"2023-07-16T15:00:29.791733Z","shell.execute_reply.started":"2023-07-16T14:55:50.522717Z","shell.execute_reply":"2023-07-16T15:00:29.790492Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"if cfg.mmdet_version == 3:\n    !pip install /kaggle/input/mmdet3-py310-libs/mmengine-0.7.3-py3-none-any.whl\n    !pip install /kaggle/input/mmdet3-py310-libs/mmcv-2.0.0-cp310-cp310-linux_x86_64.whl\nelse:\n    !pip install /kaggle/input/mmdet3-py310-libs/mmengine-0.7.3-py3-none-any.whl\n    !pip install /kaggle/input/mmdet2-py10-libs/mmcv_full-1.7.1-cp310-cp310-linux_x86_64.whl\n    #!cp -r /kaggle/input/mmdet2-py10-libs/mmcv-1.7.1/mmcv-1.7.1 . && cd ./mmcv-1.7.1 && pip install . && cd ..","metadata":{"execution":{"iopub.status.busy":"2023-07-16T15:00:29.795736Z","iopub.execute_input":"2023-07-16T15:00:29.796188Z","iopub.status.idle":"2023-07-16T15:01:31.942311Z","shell.execute_reply.started":"2023-07-16T15:00:29.796153Z","shell.execute_reply":"2023-07-16T15:01:31.941153Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"if cfg.mmdet_version == 3:\n    #!cp -r /kaggle/input/mmdet3-py310-libs/mmpretrain-1.0.0rc8/mmpretrain-1.0.0rc8 . && cd ./mmpretrain-1.0.0rc8 && pip install -e . && cd ..\n    !cp -r /kaggle/input/mmdet3-py310-libs/mmdetection-3.0.0/mmdetection-3.0.0 . && cd ./mmdetection-3.0.0 && pip install -e . && cd ..\nelse:\n    !cp -r /kaggle/input/mmdet2-py10-libs/mmdetection-custom0709-2.28.2/mmdetection-custom-2.28.2 ./mmdetection-2.28.2 && cd ./mmdetection-2.28.2 && pip install -e . && cd ..\n    #!pip install /kaggle/input/mmdet2-py10-libs/mmdet-2.19.1-py3-none-any.whl","metadata":{"execution":{"iopub.status.busy":"2023-07-16T15:01:31.94538Z","iopub.execute_input":"2023-07-16T15:01:31.945797Z","iopub.status.idle":"2023-07-16T15:02:13.544298Z","shell.execute_reply.started":"2023-07-16T15:01:31.945759Z","shell.execute_reply":"2023-07-16T15:02:13.543089Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import sys\nimport json\nimport pandas as pd\nimport numpy as np\n#import cupy as cp\nfrom glob import glob\nimport os\nimport cv2\nfrom tqdm.notebook import tqdm\nimport pickle\nfrom itertools import groupby\nfrom pycocotools import mask as mutils\nfrom pycocotools import _mask as coco_mask\nimport matplotlib.pyplot as plt\nimport os\nimport base64\nimport typing as t\nimport zlib\nimport random\nrandom.seed(0)","metadata":{"execution":{"iopub.status.busy":"2023-07-16T15:02:13.546527Z","iopub.execute_input":"2023-07-16T15:02:13.546948Z","iopub.status.idle":"2023-07-16T15:02:13.554584Z","shell.execute_reply.started":"2023-07-16T15:02:13.546901Z","shell.execute_reply":"2023-07-16T15:02:13.553435Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"if cfg.mmdet_version == 3:\n    #sys.path.append(r'/kaggle/working/mmpretrain-1.0.0rc8')\n    sys.path.append(r'/kaggle/working/mmdetection-3.0.0')\nelse:\n    sys.path.append(r'/kaggle/working/mmdetection-2.28.2')","metadata":{"execution":{"iopub.status.busy":"2023-07-16T15:02:13.556454Z","iopub.execute_input":"2023-07-16T15:02:13.557333Z","iopub.status.idle":"2023-07-16T15:02:13.567759Z","shell.execute_reply.started":"2023-07-16T15:02:13.557231Z","shell.execute_reply":"2023-07-16T15:02:13.566869Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#!cp -r /kaggle/input/mmdet2-py10-libs/mmcv-1.7.1/mmcv-1.7.1 . && cd ./mmcv-1.7.1","metadata":{"execution":{"iopub.status.busy":"2023-07-16T15:02:13.569413Z","iopub.execute_input":"2023-07-16T15:02:13.569834Z","iopub.status.idle":"2023-07-16T15:02:13.577249Z","shell.execute_reply.started":"2023-07-16T15:02:13.569803Z","shell.execute_reply":"2023-07-16T15:02:13.576345Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#!MMCV_WITH_OPS=1 python setup.py bdist_wheel","metadata":{"execution":{"iopub.status.busy":"2023-07-16T15:02:13.579029Z","iopub.execute_input":"2023-07-16T15:02:13.579376Z","iopub.status.idle":"2023-07-16T15:02:13.58711Z","shell.execute_reply.started":"2023-07-16T15:02:13.579345Z","shell.execute_reply":"2023-07-16T15:02:13.586077Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 1. gen coco format test json ","metadata":{}},{"cell_type":"code","source":"ROOT = r'/kaggle/input/hubmap-hacking-the-human-vasculature'\ndf  = pd.DataFrame(glob(ROOT+f'/test/*'), columns=['image_path'])\ndf['id'] = df.image_path.map(lambda x: x.split('/')[-1].split('.')[0])\n# display(df.iloc[0,0])\n# display(df.iloc[0,1])\n\nannos = { \n        \"info\": {},\n        \"licenses\": [],\n        \"categories\": [\n            {\n            \"id\": 1,\n            \"name\": \"blood_vessel\",\n            }\n        ],\n        'images':[],\n        }\nfor idx in tqdm(range(len(df))):\n    image_id = df.iloc[idx]['id']\n    \n    ann = {\n        'id': image_id,\n        'file_name': image_id+'.tif',\n        'width': 512,\n        'height': 512,\n    }\n    annos['images'].append(ann)\n\nwith open(f'/kaggle/working/mmdet_test.json','w',encoding='utf8') as fp:\n    json.dump(annos,fp,ensure_ascii=False,indent=2)\n\n# with open(f'/kaggle/working/mmdet_test.json','r',encoding='utf8') as fp:\n#     data = json.load(fp)\n# print(data)","metadata":{"execution":{"iopub.status.busy":"2023-07-16T15:02:13.591454Z","iopub.execute_input":"2023-07-16T15:02:13.592673Z","iopub.status.idle":"2023-07-16T15:02:13.626797Z","shell.execute_reply.started":"2023-07-16T15:02:13.59264Z","shell.execute_reply":"2023-07-16T15:02:13.625934Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 2. prepare model config","metadata":{}},{"cell_type":"code","source":"# %%writefile /kaggle/working/{cfg.exp_name}.py","metadata":{"execution":{"iopub.status.busy":"2023-07-16T15:02:13.628098Z","iopub.execute_input":"2023-07-16T15:02:13.628667Z","iopub.status.idle":"2023-07-16T15:02:13.632578Z","shell.execute_reply.started":"2023-07-16T15:02:13.628636Z","shell.execute_reply":"2023-07-16T15:02:13.631571Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%writefile /kaggle/working/{cfg.exp_name}.py\n\nclasses = ('blood_vessel',)\npalette = [(255, 0, 0), ]\n# classes = ['blood_vessel', 'glomerulus', 'unsure']\n# palette = [(255, 0, 0), (0, 255, 0), (0, 0, 255)]\nnum_classes = len(classes)\n\nmodel = dict(\n    type='HybridTaskCascade',\n    backbone=dict(\n        type='CBSwinTransformer',\n        embed_dim=128,\n        depths=[2, 2, 18, 2],\n        num_heads=[4, 8, 16, 32],\n        window_size=7,\n        ape=False,\n        drop_path_rate=0.3,\n        patch_norm=True,\n        use_checkpoint=False\n    ),\n    neck=dict(\n         type='CBFPN',\n        in_channels=[128, 256, 512, 1024],\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='HybridTaskCascadeRoIHead',\n        interleaved=True,\n        mask_info_flow=True,\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='ConvFCBBoxHead',\n                num_shared_convs=4,\n                num_shared_fcs=1,\n                in_channels=256,\n                conv_out_channels=256,\n                fc_out_channels=1024,\n                roi_feat_size=7,\n                num_classes=num_classes,\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                reg_decoded_bbox=True,\n                norm_cfg=dict(type='BN', requires_grad=True),\n                loss_cls=dict(\n                    type='CrossEntropyLoss', use_sigmoid=False, loss_weight=1.0),\n                loss_bbox=dict(type='GIoULoss', loss_weight=10.0)),\n            dict(\n                type='ConvFCBBoxHead',\n                num_shared_convs=4,\n                num_shared_fcs=1,\n                in_channels=256,\n                conv_out_channels=256,\n                fc_out_channels=1024,\n                roi_feat_size=7,\n                num_classes=num_classes,\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                reg_decoded_bbox=True,\n                norm_cfg=dict(type='BN', requires_grad=True),\n                loss_cls=dict(\n                    type='CrossEntropyLoss', use_sigmoid=False, loss_weight=1.0),\n                loss_bbox=dict(type='GIoULoss', loss_weight=10.0)),\n            dict(\n                type='ConvFCBBoxHead',\n                num_shared_convs=4,\n                num_shared_fcs=1,\n                in_channels=256,\n                conv_out_channels=256,\n                fc_out_channels=1024,\n                roi_feat_size=7,\n                num_classes=num_classes,\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                reg_decoded_bbox=True,\n                norm_cfg=dict(type='BN', requires_grad=True),\n                loss_cls=dict(\n                    type='CrossEntropyLoss', use_sigmoid=False, loss_weight=1.0),\n                loss_bbox=dict(type='GIoULoss', loss_weight=10.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=[\n            dict(\n                type='HTCMaskHead',\n                with_conv_res=False,\n                num_convs=4,\n                in_channels=256,\n                conv_out_channels=256,\n                num_classes=num_classes,\n                loss_mask=dict(\n                    type='CrossEntropyLoss', use_mask=True, loss_weight=1.0)),\n            dict(\n                type='HTCMaskHead',\n                num_convs=4,\n                in_channels=256,\n                conv_out_channels=256,\n                num_classes=num_classes,\n                loss_mask=dict(\n                    type='CrossEntropyLoss', use_mask=True, loss_weight=1.0)),\n            dict(\n                type='HTCMaskHead',\n                num_convs=4,\n                in_channels=256,\n                conv_out_channels=256,\n                num_classes=num_classes,\n                loss_mask=dict(\n                    type='CrossEntropyLoss', use_mask=True, loss_weight=1.0))\n        ]),\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                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                    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                    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                    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.001,\n            nms=dict(type='nms', iou_threshold=0.5),\n            max_per_img=1000,\n            mask_thr_binary=0.5)))\n\n\ndataset_type = 'CocoDataset'\ndata_root = '/root/autodl-tmp/hubmap/data/'\njson_type = 'dataset12_blood_vessel'\nfold = 1\n\nalbu_train_transforms = [\n    dict(type='VerticalFlip', p=0.5),\n    dict(type='RandomRotate90', p=0.5),\n]\n\nimg_norm_cfg = dict(\n    mean=[123.675, 116.28, 103.53], std=[58.395, 57.12, 57.375], to_rgb=True)\ntrain_pipeline = [\n    dict(type='LoadImageFromFile'),\n    dict(type='LoadAnnotations', with_bbox=True, with_mask=True),\n    dict(type='Resize', img_scale=[(256, 256), (960, 960)], keep_ratio=True),\n    dict(type='RandomFlip', flip_ratio=0.5),\n    dict(type='Normalize', **img_norm_cfg),\n    dict(type='Pad', size_divisor=32),\n    dict(type='Albu',\n         transforms=albu_train_transforms,\n         bbox_params=dict(type='BboxParams',\n                          format='pascal_voc',\n                          label_fields=['gt_labels'],\n                          min_visibility=0.0,\n                          filter_lost_elements=True),\n         keymap={'img': 'image', 'gt_bboxes': 'bboxes', 'gt_masks': 'masks'},\n         update_pad_shape=False,\n         skip_img_without_anno=True),\n    dict(type='DefaultFormatBundle'),\n    dict(type='Collect', keys=['img', 'gt_bboxes', 'gt_masks', 'gt_labels']),\n]\ntest_pipeline = [\n    dict(type='LoadImageFromFile'),\n    dict(\n        type='MultiScaleFlipAug',\n        img_scale=[(1024, 1024)],  #(960, 960), (704, 704), (480, 480)\n        flip=False,\n        flip_direction=[\"horizontal\",\"vertical\",\"diagonal\"],\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\ndata = dict(\n    samples_per_gpu=4,\n    workers_per_gpu=2,\n    train=dict(\n            classes=classes,\n            type=dataset_type,\n            ann_file=f'coco_json/{json_type}/train_fold{fold}.json',\n            img_prefix=data_root + 'train/',\n            pipeline=train_pipeline), # if fold != holdout],\n    val=dict(\n            classes=classes,\n            type=dataset_type,\n            ann_file=f'coco_json/{json_type}/valid_fold{fold}.json',\n            img_prefix=data_root + 'train/',\n            pipeline=test_pipeline),\n    test=dict(\n            classes=classes,\n            type=dataset_type,\n            ann_file=f'coco_json/{json_type}/valid_fold{fold}.json',\n            img_prefix=data_root + 'train/',\n            pipeline=test_pipeline)\n)\n\nnx = 1\nwork_dir = f'./work_dirs/cell/drsx50_{nx}x_hvflip_rot90_tiny_d2_all'\nevaluation = dict(\n    classwise=True,\n    interval=1,\n    metric=['bbox', 'segm'],\n    jsonfile_prefix=f\"{work_dir}/valid\")\n\noptimizer = dict(type='AdamW', lr=0.0001, betas=(0.9, 0.999), weight_decay=0.05,\n                 paramwise_cfg=dict(custom_keys={'absolute_pos_embed': dict(decay_mult=0.),\n                                                 'relative_position_bias_table': dict(decay_mult=0.),\n                                                 'norm': dict(decay_mult=0.)}))\noptimizer_config = dict(\n            grad_clip=dict(max_norm=35, norm_type=2))\nlr_config = dict(\n    policy='step',\n    warmup='linear',\n    warmup_iters=20,\n    warmup_ratio=1/3,\n    step=[8 * nx, 11 * nx])\n\ncustom_hooks = [dict(type='NumClassCheckHook')]\ntotal_epochs = 12 * nx\nrunner = dict(type='EpochBasedRunner', max_epochs=total_epochs)\ncheckpoint_config = dict(interval=total_epochs, save_optimizer=False)\nlog_config = dict(interval=10, hooks=[dict(type='TextLoggerHook')])\ndist_params = dict(backend='nccl')\nlog_level = 'INFO'\nload_from = r'/root/autodl-fs/hubmap/htc_cbv2_swin_base22k_patch4_window7_mstrain_400-1400_giou_4conv1f_adamw_20e_coco.pth'\nresume_from = False\nworkflow = [('train', 1)]\n\nonly_swa_training = False\n# whether to perform swa training\nswa_training = True\n# load the best pre_trained model as the starting model for swa training\nswa_load_from = work_dir + f'/epoch_{total_epochs}.pth'\nswa_resume_from = None\n\n# swa optimizer\nswa_optimizer = dict(type='AdamW', lr=0.0001, betas=(0.9, 0.999), weight_decay=0.05,\n                     paramwise_cfg=dict(custom_keys={'absolute_pos_embed': dict(decay_mult=0.),\n                                                 'relative_position_bias_table': dict(decay_mult=0.),\n                                                 'norm': dict(decay_mult=0.)}))\nswa_optimizer_config = dict(\n            grad_clip=dict(max_norm=35, norm_type=2))\n\n# swa learning policy\nswa_lr_config = dict(\n    policy='cyclic',\n    target_ratio=(1, 0.01),\n    cyclic_times=12,\n    step_ratio_up=0.0)\nswa_runner = dict(type='EpochBasedRunner', max_epochs=12)\n# the epoch interval to perform swa\nswa_interval = 1\n\n# swa checkpoint setting\nswa_checkpoint_config = dict(interval=1, filename_tmpl='swa_epoch_{}.pth', save_optimizer=False)\n\n#runner = dict(type='EpochBasedRunnerAmp', max_epochs=36)\n\n# do not use mmdet version fp16\n# fp16 = None\nfp16 = dict(loss_scale=512.0)","metadata":{"execution":{"iopub.status.busy":"2023-07-16T15:02:13.634083Z","iopub.execute_input":"2023-07-16T15:02:13.634701Z","iopub.status.idle":"2023-07-16T15:02:13.654326Z","shell.execute_reply.started":"2023-07-16T15:02:13.63467Z","shell.execute_reply":"2023-07-16T15:02:13.653349Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# %%writefile /kaggle/working/{cfg.exp_name}.py","metadata":{"execution":{"iopub.status.busy":"2023-07-16T15:02:13.655836Z","iopub.execute_input":"2023-07-16T15:02:13.656427Z","iopub.status.idle":"2023-07-16T15:02:13.664741Z","shell.execute_reply.started":"2023-07-16T15:02:13.656397Z","shell.execute_reply":"2023-07-16T15:02:13.663785Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# %%writefile /kaggle/working/{cfg.exp_name}.py\n\n# classes = ('blood_vessel',)\n# palette = [(255, 0, 0), ]\n# # classes = ['blood_vessel', 'glomerulus', 'unsure']\n# # palette = [(255, 0, 0), (0, 255, 0), (0, 0, 255)]\n# num_classes = len(classes)\n\n# model = dict(\n#     type='CascadeRCNN',\n#     pretrained=None,\n#     backbone=dict(\n#         type='OfficialSwin',\n#         embed_dim=128,\n#         depths=[2, 2, 18, 2],\n#         num_heads=[4, 8, 16, 32],\n#         window_size=7,\n#         mlp_ratio=4.,\n#         qkv_bias=True,\n#         qk_scale=None,\n#         drop_rate=0.,\n#         attn_drop_rate=0.,\n#         drop_path_rate=0.3,\n#         ape=False,\n#         patch_norm=True,\n#         out_indices=(0, 1, 2, 3),\n#         use_checkpoint=False),\n#     neck=dict(\n#         type='FPN',\n#         in_channels=[128, 256, 512, 1024],\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='ConvFCBBoxHead',\n#                 num_shared_convs=4,\n#                 num_shared_fcs=1,\n#                 in_channels=256,\n#                 conv_out_channels=256,\n#                 fc_out_channels=1024,\n#                 roi_feat_size=7,\n#                 num_classes=num_classes,\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#                 reg_decoded_bbox=True,\n#                 norm_cfg=dict(type='BN', requires_grad=True),\n#                 loss_cls=dict(\n#                     type='CrossEntropyLoss', use_sigmoid=False, loss_weight=1.0),\n#                 loss_bbox=dict(type='GIoULoss', loss_weight=10.0)),\n#             dict(\n#                 type='ConvFCBBoxHead',\n#                 num_shared_convs=4,\n#                 num_shared_fcs=1,\n#                 in_channels=256,\n#                 conv_out_channels=256,\n#                 fc_out_channels=1024,\n#                 roi_feat_size=7,\n#                 num_classes=num_classes,\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#                 reg_decoded_bbox=True,\n#                 norm_cfg=dict(type='BN', requires_grad=True),\n#                 loss_cls=dict(\n#                     type='CrossEntropyLoss', use_sigmoid=False, loss_weight=1.0),\n#                 loss_bbox=dict(type='GIoULoss', loss_weight=10.0)),\n#             dict(\n#                 type='ConvFCBBoxHead',\n#                 num_shared_convs=4,\n#                 num_shared_fcs=1,\n#                 in_channels=256,\n#                 conv_out_channels=256,\n#                 fc_out_channels=1024,\n#                 roi_feat_size=7,\n#                 num_classes=num_classes,\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#                 reg_decoded_bbox=True,\n#                 norm_cfg=dict(type='BN', requires_grad=True),\n#                 loss_cls=dict(\n#                     type='CrossEntropyLoss', use_sigmoid=False, loss_weight=1.0),\n#                 loss_bbox=dict(type='GIoULoss', loss_weight=10.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=num_classes,\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_across_levels=False,\n#             nms_pre=2000,\n#             nms_post=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_across_levels=False,\n#             nms_pre=1000,\n#             nms_post=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.001,\n#             nms=dict(type='nms', iou_threshold=0.5),\n#             max_per_img=1000,\n#             mask_thr_binary=0.5)))\n\n# img_norm_cfg = dict(\n#     mean=[123.675, 116.28, 103.53], std=[58.395, 57.12, 57.375], to_rgb=True)\n\n# albu_train_transforms = [\n#     dict(type='VerticalFlip', p=0.5),\n#     dict(type='RandomRotate90', p=0.5),\n# ]\n\n# # augmentation strategy originates from DETR / Sparse RCNN\n# train_pipeline = [\n#     dict(type='LoadImageFromFile'),\n#     dict(type='LoadAnnotations', with_bbox=True, with_mask=True),\n#     dict(type='RandomFlip', flip_ratio=0.5),\n#     dict(type='AutoAugment',\n#          policies=[\n#              [\n#                  dict(type='Resize',\n#                       img_scale=[(480, 1333), (512, 1333), (544, 1333), (576, 1333),\n#                                  (608, 1333), (640, 1333), (672, 1333), (704, 1333),\n#                                  (736, 1333), (768, 1333), (800, 1333), (832, 1333), \n#                                  (864, 1333), (896, 1333), (928, 1333), (960, 1333)],\n#                       multiscale_mode='value',\n#                       keep_ratio=True)\n#              ],\n#              [\n#                  dict(type='Resize',\n#                       img_scale=[(500, 1333), (600, 1333),(700,1333), (800,1333), (900,1333), (1000, 1333)],\n#                       multiscale_mode='value',\n#                       keep_ratio=True),\n#                  dict(type='RandomCrop',\n#                       crop_type='absolute_range',\n#                       crop_size=(512, 512),\n#                       allow_negative_crop=True),\n#                  dict(type='Resize',\n#                       img_scale=[(480, 1333), (512, 1333), (544, 1333),\n#                                  (576, 1333), (608, 1333), (640, 1333),\n#                                  (672, 1333), (704, 1333), (736, 1333),\n#                                  (768, 1333), (800, 1333), (832, 1333), \n#                                  (864, 1333), (896, 1333), (928, 1333), (960, 1333)],\n#                       multiscale_mode='value',\n#                       override=True,\n#                       keep_ratio=True)\n#              ]\n#          ]),\n#     dict(type='Albu',\n#          transforms=albu_train_transforms,\n#          bbox_params=dict(type='BboxParams',\n#                           format='pascal_voc',\n#                           label_fields=['gt_labels'],\n#                           min_visibility=0.0,\n#                           filter_lost_elements=True),\n#          keymap={'img': 'image', 'gt_bboxes': 'bboxes', 'gt_masks': 'masks'},\n#          update_pad_shape=False,\n#          skip_img_without_anno=True),\n#     dict(type='Normalize', **img_norm_cfg),\n#     dict(type='Pad', size_divisor=32),\n#     dict(type='DefaultFormatBundle'),\n#     dict(type='Collect', keys=['img', 'gt_bboxes', 'gt_labels', 'gt_masks']),\n# ]\n\n\n# test_pipeline = [\n#     dict(type='LoadImageFromFile'),\n#     dict(\n#         type='MultiScaleFlipAug',\n#         img_scale=[(704, 704)],  #(960, 960), (704, 704), (480, 480)\n#         flip=False,\n#         flip_direction=[\"horizontal\",\"vertical\"],\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# dataset_type = 'CocoDataset'\n# data_root = '/root/autodl-tmp/hubmap/data/'\n# json_type = 'dataset12_blood_vessel'\n# fold = 1\n\n# data = dict(\n#     samples_per_gpu=4,\n#     workers_per_gpu=2,\n#     train=dict(\n#             classes=classes,\n#             type=dataset_type,\n#             ann_file=f'coco_json/{json_type}/train_fold{fold}.json',\n#             img_prefix=data_root + 'train/',\n#             pipeline=train_pipeline), # if fold != holdout],\n#     val=dict(\n#             classes=classes,\n#             type=dataset_type,\n#             ann_file=f'coco_json/{json_type}/valid_fold{fold}.json',\n#             img_prefix=data_root + 'train/',\n#             pipeline=test_pipeline),\n#     test=dict(\n#             classes=classes,\n#             type=dataset_type,\n#             ann_file=f'coco_json/{json_type}/valid_fold{fold}.json',\n#             img_prefix=data_root + 'train/',\n#             pipeline=test_pipeline)\n# )\n\n# nx = 1\n# work_dir = f'./work_dirs/cell/drsx50_{nx}x_hvflip_rot90_tiny_d2_all'\n# evaluation = dict(\n#     classwise=True,\n#     interval=1,\n#     metric=['bbox', 'segm'],\n#     jsonfile_prefix=f\"{work_dir}/valid\")\n\n# optimizer = dict(type='AdamW', lr=0.0001, betas=(0.9, 0.999), weight_decay=0.05,\n#                  paramwise_cfg=dict(custom_keys={'absolute_pos_embed': dict(decay_mult=0.),\n#                                                  'relative_position_bias_table': dict(decay_mult=0.),\n#                                                  'norm': dict(decay_mult=0.)}))\n\n# optimizer_config = dict(\n#             grad_clip=None)\n\n# # learning policy\n# lr_config = dict(\n#     policy='step',\n#     warmup='linear',\n#     warmup_iters=20,\n#     warmup_ratio=1/3,\n#     step=[8 * nx, 11 * nx])\n\n# total_epochs = 12 * nx\n# runner = dict(type='EpochBasedRunner', max_epochs=total_epochs)\n\n# custom_hooks = [dict(type='NumClassCheckHook')]\n# checkpoint_config = dict(interval=total_epochs, save_optimizer=False)\n# log_config = dict(interval=10, hooks=[dict(type='TextLoggerHook')])\n\n\n# # do not use mmdet version fp16\n# fp16 = None\n\n# dist_params = dict(backend='nccl')\n# log_level = 'INFO'\n# load_from = r'/root/autodl-fs/hubmap/cascade_mask_rcnn_swin_base_patch4_window7.pth'\n# resume_from = None\n# workflow = [('train', 1)]\n\n\n# #swa settings\n# only_swa_training = False\n# # whether to perform swa training\n# swa_training = True\n# # load the best pre_trained model as the starting model for swa training\n# swa_load_from = work_dir + f'/epoch_{total_epochs}.pth'\n# swa_resume_from = None\n# swa_interval = 1\n# # swa optimizer\n# swa_optimizer = dict(type='AdamW', lr=0.0001, betas=(0.9, 0.999), weight_decay=0.05,\n#                  paramwise_cfg=dict(custom_keys={'absolute_pos_embed': dict(decay_mult=0.),\n#                                                  'relative_position_bias_table': dict(decay_mult=0.),\n#                                                  'norm': dict(decay_mult=0.)}))\n# swa_optimizer_config = dict(\n#             grad_clip=None)\n\n# # swa learning policy\n# swa_lr_config = dict(\n#     policy='cyclic',\n#     target_ratio=(1, 0.01),\n#     cyclic_times=12,\n#     step_ratio_up=0.0)\n\n# swa_runner = dict(type='EpochBasedRunner', max_epochs=12)\n\n# # swa checkpoint setting\n# swa_checkpoint_config = dict(interval=1, filename_tmpl='swa_epoch_{}.pth', save_optimizer=False)","metadata":{"execution":{"iopub.status.busy":"2023-07-16T15:02:13.666586Z","iopub.execute_input":"2023-07-16T15:02:13.66754Z","iopub.status.idle":"2023-07-16T15:02:13.687774Z","shell.execute_reply.started":"2023-07-16T15:02:13.667384Z","shell.execute_reply":"2023-07-16T15:02:13.686811Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%writefile {write_dir}bbox_nms.py\n\n# Copyright (c) OpenMMLab. All rights reserved.\n#from typing import Optional, Tuple, Union\n#from torch import Tensor\n\nimport torch\nfrom mmcv.ops.nms import batched_nms\n#mmdet3\n#from mmdet.structures.bbox import bbox_overlaps\n#mmdet2\nfrom mmdet.core.bbox.iou_calculators import bbox_overlaps\n\ndef multiclass_nms(\n    multi_bboxes,\n    multi_scores,\n    score_thr,\n    nms_cfg,\n    max_num=-1,\n    score_factors = None,\n    return_inds = False,\n    box_dim = 4\n):\n    \"\"\"NMS for multi-class bboxes.\n\n    Args:\n        multi_bboxes (Tensor): shape (n, #class*4) or (n, 4)\n        multi_scores (Tensor): shape (n, #class), where the last column\n            contains scores of the background class, but this will be ignored.\n        score_thr (float): bbox threshold, bboxes with scores lower than it\n            will not be considered.\n        nms_cfg (Union[:obj:`ConfigDict`, dict]): a dict that contains\n            the arguments of nms operations.\n        max_num (int, optional): if there are more than max_num bboxes after\n            NMS, only top max_num will be kept. Default to -1.\n        score_factors (Tensor, optional): The factors multiplied to scores\n            before applying NMS. Default to None.\n        return_inds (bool, optional): Whether return the indices of kept\n            bboxes. Default to False.\n        box_dim (int): The dimension of boxes. Defaults to 4.\n\n    Returns:\n        Union[Tuple[Tensor, Tensor, Tensor], Tuple[Tensor, Tensor]]:\n            (dets, labels, indices (optional)), tensors of shape (k, 5),\n            (k), and (k). Dets are boxes with scores. Labels are 0-based.\n    \"\"\"\n    num_classes = multi_scores.size(1) - 1\n    # exclude background category\n    if multi_bboxes.shape[1] > box_dim:\n        bboxes = multi_bboxes.view(multi_scores.size(0), -1, box_dim)\n    else:\n        bboxes = multi_bboxes[:, None].expand(\n            multi_scores.size(0), num_classes, box_dim)\n\n    scores = multi_scores[:, :-1]\n\n    labels = torch.arange(num_classes, dtype=torch.long, device=scores.device)\n    labels = labels.view(1, -1).expand_as(scores)\n\n    bboxes = bboxes.reshape(-1, box_dim)\n    scores = scores.reshape(-1)\n    labels = labels.reshape(-1)\n\n    if not torch.onnx.is_in_onnx_export():\n        # NonZero not supported  in TensorRT\n        # remove low scoring boxes\n        valid_mask = scores > score_thr\n    # multiply score_factor after threshold to preserve more bboxes, improve\n    # mAP by 1% for YOLOv3\n    if score_factors is not None:\n        # expand the shape to match original shape of score\n        score_factors = score_factors.view(-1, 1).expand(\n            multi_scores.size(0), num_classes)\n        score_factors = score_factors.reshape(-1)\n        scores = scores * score_factors\n\n    if not torch.onnx.is_in_onnx_export():\n        # NonZero not supported  in TensorRT\n        inds = valid_mask.nonzero(as_tuple=False).squeeze(1)\n        bboxes, scores, labels = bboxes[inds], scores[inds], labels[inds]\n    else:\n        # TensorRT NMS plugin has invalid output filled with -1\n        # add dummy data to make detection output correct.\n        bboxes = torch.cat([bboxes, bboxes.new_zeros(1, box_dim)], dim=0)\n        scores = torch.cat([scores, scores.new_zeros(1)], dim=0)\n        labels = torch.cat([labels, labels.new_zeros(1)], dim=0)\n\n    if bboxes.numel() == 0:\n        if torch.onnx.is_in_onnx_export():\n            raise RuntimeError('[ONNX Error] Can not record NMS '\n                               'as it has not been executed this time')\n        dets = torch.cat([bboxes, scores[:, None]], -1)\n        if return_inds:\n            return dets, labels, inds\n        else:\n            return dets, labels\n\n    if nms_cfg[\"type\"] == \"weighted_cluster_nms\":\n        dets, labels, keep = weighted_cluster_nms(bboxes, scores, labels, nms_cfg)\n    else:\n        dets, keep = batched_nms(bboxes, scores, labels, nms_cfg)\n\n    if max_num > 0:\n        dets = dets[:max_num]\n        keep = keep[:max_num]\n\n    if return_inds:\n        return dets, labels[keep], inds[keep]\n    else:\n        return dets, labels[keep]\n\n\ndef fast_nms(\n    multi_bboxes,\n    multi_scores,\n    multi_coeffs,\n    score_thr,\n    iou_thr,\n    top_k,\n    max_num = -1\n):\n    \"\"\"Fast NMS in `YOLACT <https://arxiv.org/abs/1904.02689>`_.\n\n    Fast NMS allows already-removed detections to suppress other detections so\n    that every instance can be decided to be kept or discarded in parallel,\n    which is not possible in traditional NMS. This relaxation allows us to\n    implement Fast NMS entirely in standard GPU-accelerated matrix operations.\n\n    Args:\n        multi_bboxes (Tensor): shape (n, #class*4) or (n, 4)\n        multi_scores (Tensor): shape (n, #class+1), where the last column\n            contains scores of the background class, but this will be ignored.\n        multi_coeffs (Tensor): shape (n, #class*coeffs_dim).\n        score_thr (float): bbox threshold, bboxes with scores lower than it\n            will not be considered.\n        iou_thr (float): IoU threshold to be considered as conflicted.\n        top_k (int): if there are more than top_k bboxes before NMS,\n            only top top_k will be kept.\n        max_num (int): if there are more than max_num bboxes after NMS,\n            only top max_num will be kept. If -1, keep all the bboxes.\n            Default: -1.\n\n    Returns:\n        Union[Tuple[Tensor, Tensor, Tensor], Tuple[Tensor, Tensor]]:\n            (dets, labels, coefficients), tensors of shape (k, 5), (k, 1),\n            and (k, coeffs_dim). Dets are boxes with scores.\n            Labels are 0-based.\n    \"\"\"\n\n    scores = multi_scores[:, :-1].t()  # [#class, n]\n    scores, idx = scores.sort(1, descending=True)\n\n    idx = idx[:, :top_k].contiguous()\n    scores = scores[:, :top_k]  # [#class, topk]\n    num_classes, num_dets = idx.size()\n    boxes = multi_bboxes[idx.view(-1), :].view(num_classes, num_dets, 4)\n    coeffs = multi_coeffs[idx.view(-1), :].view(num_classes, num_dets, -1)\n\n    iou = bbox_overlaps(boxes, boxes)  # [#class, topk, topk]\n    iou.triu_(diagonal=1)\n    iou_max, _ = iou.max(dim=1)\n\n    # Now just filter out the ones higher than the threshold\n    keep = iou_max <= iou_thr\n\n    # Second thresholding introduces 0.2 mAP gain at negligible time cost\n    keep *= scores > score_thr\n\n    # Assign each kept detection to its corresponding class\n    classes = torch.arange(\n        num_classes, device=boxes.device)[:, None].expand_as(keep)\n    classes = classes[keep]\n\n    boxes = boxes[keep]\n    coeffs = coeffs[keep]\n    scores = scores[keep]\n\n    # Only keep the top max_num highest scores across all classes\n    scores, idx = scores.sort(0, descending=True)\n    if max_num > 0:\n        idx = idx[:max_num]\n        scores = scores[:max_num]\n\n    classes = classes[idx]\n    boxes = boxes[idx]\n    coeffs = coeffs[idx]\n\n    cls_dets = torch.cat([boxes, scores[:, None]], dim=1)\n    return cls_dets, classes, coeffs\n\n\n\ndef box_diou(boxes1, boxes2):\n    def box_area(box):\n        # box = 4xn\n        return (box[2] - box[0]) * (box[3] - box[1])\n\n    area1 = box_area(boxes1.t())\n    area2 = box_area(boxes2.t())\n\n    lt = torch.max(boxes1[:, None, :2], boxes2[:, :2])  # [N,M,2]\n    rb = torch.min(boxes1[:, None, 2:], boxes2[:, 2:])  # [N,M,2]\n    clt = torch.min(boxes1[:, None, :2], boxes2[:, :2])\n    crb = torch.max(boxes1[:, None, 2:], boxes2[:, 2:])\n    x1 = (boxes1[:, None, 0] + boxes1[:, None, 2]) / 2\n    y1 = (boxes1[:, None, 1] + boxes1[:, None, 3]) / 2\n    x2 = (boxes2[:, None, 0] + boxes2[:, None, 2]) / 2\n    y2 = (boxes2[:, None, 1] + boxes2[:, None, 3]) / 2\n    d = (x1 - x2.t()) ** 2 + (y1 - y2.t()) ** 2\n    c = ((crb - clt) ** 2).sum(dim = 2)\n\n    inter = (rb - lt).clamp(min = 0).prod(2)  # [N,M]\n    return inter / (area1[:, None] + area2 - inter) - (d / c) ** 0.6 \n\ndef box_iou(boxes1, boxes2):\n    def box_area(box):\n        # box = 4xn\n        return (box[2] - box[0]) * (box[3] - box[1])\n\n    area1 = box_area(boxes1.t())\n    area2 = box_area(boxes2.t())\n\n    lt = torch.max(boxes1[:, None, :2], boxes2[:, :2])  # [N,M,2]\n    rb = torch.min(boxes1[:, None, 2:], boxes2[:, 2:])  # [N,M,2]\n\n    inter = (rb - lt).clamp(min = 0).prod(2)  # [N,M]\n    return inter / (area1[:, None] + area2 - inter) \n\ndef weighted_cluster_nms(boxes, scores, idxs, nms_cfg):\n    nms_cfg_ = nms_cfg.copy()\n    n = len(scores)\n    scores, idx = scores.sort(descending = True)\n    boxes = boxes[idx]\n    idxs = idxs[idx]\n\n    class_agnostic = nms_cfg_.pop('class_agnostic', False)\n    if class_agnostic:\n        boxes_for_nms = boxes\n    else:\n        max_coordinate = boxes.max()\n        offsets = idxs.to(boxes) * (max_coordinate + torch.tensor(1).to(boxes))\n        boxes_for_nms = boxes + offsets[:, None]\n\n    iou_threshold = nms_cfg_.pop(\"iou_threshold\", 0.5)\n    iou_method = nms_cfg_.pop(\"iou_method\", \"iou\")\n    iou_method = eval(\"box_\" + iou_method)\n    \n    iou = iou_method(boxes_for_nms, boxes_for_nms).triu_(diagonal = 1)  # IoU矩阵，上三角化\n    C = iou\n    for _ in range(200):    \n        A = C\n        maxA = A.max(dim = 0)[0]   # 列最大值向量\n        E = (maxA < iou_threshold).float().unsqueeze(1).expand_as(A)   # 对角矩阵E的替代\n        C = iou.mul(E)     # 按元素相乘\n        if A.equal(C) == True:     # 终止条件\n            break\n            \n    keep_index = (maxA < iou_threshold).cpu()\n    keep = torch.arange(n)[keep_index]  # 列最大值向量，二值化\n    weights = (C * (C > iou_threshold).float() + torch.eye(n).cuda()) * (scores.reshape((1, n)))\n\n    xx1 = boxes[:, 0].expand(n, n)\n    yy1 = boxes[:, 1].expand(n, n)\n    xx2 = boxes[:, 2].expand(n, n)\n    yy2 = boxes[:, 3].expand(n, n)\n\n    weightsum = weights.sum(dim = 1)         # 坐标加权平均\n    xx1 = (xx1 * weights).sum(dim = 1) / weightsum\n    yy1 = (yy1 * weights).sum(dim = 1) / weightsum\n    xx2 = (xx2 * weights).sum(dim = 1) / weightsum\n    yy2 = (yy2 * weights).sum(dim = 1) / weightsum\n    boxes = torch.stack([xx1, yy1, xx2, yy2], 1)\n\n    boxes = boxes[keep]\n    scores = scores[keep]\n\n    max_num = nms_cfg_.pop('max_num', -1)\n    if max_num > 0:\n        keep = keep[:max_num]\n        boxes = boxes[:max_num]\n        scores = scores[:max_num]\n\n    return torch.cat([boxes, scores[:, None]], -1), idxs, keep","metadata":{"execution":{"iopub.status.busy":"2023-07-16T15:02:13.690838Z","iopub.execute_input":"2023-07-16T15:02:13.691178Z","iopub.status.idle":"2023-07-16T15:02:13.707861Z","shell.execute_reply.started":"2023-07-16T15:02:13.691152Z","shell.execute_reply":"2023-07-16T15:02:13.706616Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 3. infer cmd","metadata":{}},{"cell_type":"code","source":"%%writefile /kaggle/working/mmdetection-2.28.2/mmdet/models/backbones/cbnet.py\n\nimport torch\nimport torch.nn as nn\nimport torch.nn.functional as F\n\nfrom mmcv.cnn import constant_init\nfrom mmdet.utils import get_root_logger\nfrom ..builder import BACKBONES\nfrom .resnet import ResNet, build_norm_layer, _BatchNorm\nfrom .res2net import Res2Net\nfrom .swin import SwinTransformer\n\nfrom mmcv.runner import BaseModule\n'''\nFor CNN\n'''\nclass _CBSubnet(BaseModule):\n    def _freeze_stages(self):\n        if self.frozen_stages >= 0:\n            if self.deep_stem and hasattr(self, 'stem'):\n                self.stem.eval()\n                for param in self.stem.parameters():\n                    param.requires_grad = False\n            elif hasattr(self, 'conv1'):\n                self.norm1.eval()\n                for m in [self.conv1, self.norm1]:\n                    for param in m.parameters():\n                        param.requires_grad = False\n\n        for i in range(1, self.frozen_stages + 1):\n            if not hasattr(self, f'layer{i}'):\n                continue\n            m = getattr(self, f'layer{i}')\n            m.eval()\n            for param in m.parameters():\n                param.requires_grad = False\n    \n    def del_layers(self, del_stages):\n        self.del_stages = del_stages\n        if self.del_stages>=0:\n            if self.deep_stem:\n                del self.stem\n            else:\n                del self.conv1\n        \n        for i in range(1, self.del_stages+1):\n            delattr(self, f'layer{i}')\n\n    def forward(self, x, cb_feats=None, pre_outs=None):\n        \"\"\"Forward function.\"\"\"\n        spatial_info = []\n        outs = []\n\n        if self.deep_stem and hasattr(self, 'stem'):\n            x = self.stem(x)\n            x = self.maxpool(x)\n        elif hasattr(self, 'conv1'):\n            x = self.conv1(x)\n            x = self.norm1(x)\n            x = self.relu(x)\n            x = self.maxpool(x)\n        else:\n            x = pre_outs[0]\n        outs.append(x)\n        \n        for i, layer_name in enumerate(self.res_layers):\n            if hasattr(self, layer_name):\n                res_layer = getattr(self, layer_name)\n                spatial_info.append(x.shape[2:])\n                if cb_feats is not None:\n                    x = x + cb_feats[i]\n                x = res_layer(x)\n            else:\n                x = pre_outs[i+1]\n            outs.append(x)\n        return tuple(outs), spatial_info\n\n    def train(self, mode=True):\n        \"\"\"Convert the model into training mode while keep layers freezed.\"\"\"\n        super().train(mode)\n        self._freeze_stages()\n\nclass _ResNet(_CBSubnet, ResNet):\n    def __init__(self, **kwargs):\n        _CBSubnet.__init__(self)\n        ResNet.__init__(self, **kwargs)\n\nclass _Res2Net(_CBSubnet, Res2Net):\n    def __init__(self, **kwargs):\n        _CBSubnet.__init__(self)\n        Res2Net.__init__(self, **kwargs)\n\nclass _CBNet(BaseModule):\n    def _freeze_stages(self):\n        for m in self.cb_modules:\n            m._freeze_stages()\n    \n    def init_cb_weights(self):\n        raise NotImplementedError\n\n    def init_weights(self):\n        self.init_cb_weights()\n        for m in self.cb_modules:\n            m.init_weights()\n\n    def _get_cb_feats(self, feats, spatial_info):\n        raise NotImplementedError\n\n    def forward(self, x):\n        outs_list = []\n        for i, module in enumerate(self.cb_modules):\n            if i == 0:\n                pre_outs, spatial_info = module(x)\n            else:\n                pre_outs, spatial_info = module(x, cb_feats, pre_outs)\n\n            outs = [pre_outs[i+1] for i in self.out_indices]\n            outs_list.append(tuple(outs))\n            \n            if i < len(self.cb_modules)-1:\n                cb_feats = self._get_cb_feats(pre_outs, spatial_info)  \n        return tuple(outs_list)\n\n    def train(self, mode=True):\n        \"\"\"Convert the model into training mode while keep layers freezed.\"\"\"\n        super().train(mode)\n        for m in self.cb_modules:\n            m.train(mode=mode)\n        self._freeze_stages()\n        for m in self.cb_linears.modules():\n            # trick: eval have effect on BatchNorm only\n            if isinstance(m, _BatchNorm):\n                m.eval()\n\nclass _CBResNet(_CBNet):\n    def __init__(self, net, cb_inplanes, cb_zero_init=True, cb_del_stages=0, **kwargs):\n        super(_CBResNet, self).__init__()\n        self.cb_zero_init = cb_zero_init\n        self.cb_del_stages = cb_del_stages\n\n        self.cb_modules = nn.ModuleList()\n        for cb_idx in range(2):\n            cb_module = net(**kwargs)\n            if cb_idx > 0:\n                cb_module.del_layers(self.cb_del_stages)\n            self.cb_modules.append(cb_module)\n        self.out_indices = self.cb_modules[0].out_indices\n\n        self.cb_linears = nn.ModuleList()\n        self.num_layers = len(self.cb_modules[0].stage_blocks)\n        norm_cfg = self.cb_modules[0].norm_cfg\n        for i in range(self.num_layers):\n            linears = nn.ModuleList()\n            if i >= self.cb_del_stages:\n                jrange = 4 - i\n                for j in range(jrange):\n                    linears.append(\n                        nn.Sequential(\n                            nn.Conv2d(cb_inplanes[i + j + 1], cb_inplanes[i], 1, bias=False),\n                            build_norm_layer(norm_cfg, cb_inplanes[i])[1]\n                        )\n                    )\n                \n            self.cb_linears.append(linears)\n    \n    def init_cb_weights(self):\n        if self.cb_zero_init:\n            for ls in self.cb_linears:\n                for m in ls:\n                    if isinstance(m, nn.Sequential):\n                        constant_init(m[-1], 0)\n                    else:\n                        constant_init(m, 0)\n\n    def _get_cb_feats(self, feats, spatial_info):\n        cb_feats = []\n        for i in range(self.num_layers):\n            if i >= self.cb_del_stages:\n                h, w = spatial_info[i]\n                feeds = []\n                jrange = 4 - i\n                for j in range(jrange):\n                    tmp = self.cb_linears[i][j](feats[j + i + 1])\n                    tmp = F.interpolate(tmp, size=(h, w), mode='nearest')\n                    feeds.append(tmp)\n                feed = torch.sum(torch.stack(feeds,dim=-1), dim=-1)\n            else:\n                feed = 0\n            cb_feats.append(feed)\n            \n        return cb_feats\n\n\n@BACKBONES.register_module()\nclass CBResNet(_CBResNet):\n    def __init__(self, **kwargs):\n        super().__init__(net=_ResNet, **kwargs)\n\n@BACKBONES.register_module()\nclass CBRes2Net(_CBResNet):\n    def __init__(self, **kwargs):\n        super().__init__(net=_Res2Net, **kwargs)\n        \n\n'''\nFor Swin Transformer\n'''\nclass _SwinTransformer(SwinTransformer):\n    def _freeze_stages(self):\n        if self.frozen_stages >= 0 and hasattr(self, 'patch_embed'):\n            self.patch_embed.eval()\n            for param in self.patch_embed.parameters():\n                param.requires_grad = False\n\n        if self.frozen_stages >= 1 and self.ape:\n            self.absolute_pos_embed.requires_grad = False\n\n        if self.frozen_stages >= 2:\n            self.pos_drop.eval()\n            for i in range(0, self.frozen_stages - 1):\n                m = self.layers[i]\n                if m is None:\n                    continue\n                m.eval()\n                for param in m.parameters():\n                    param.requires_grad = False\n\n    def del_layers(self, del_stages):\n        self.del_stages = del_stages\n        if self.del_stages>=0:\n            del self.patch_embed\n        \n        if self.del_stages >=1 and self.ape:\n            del self.absolute_pos_embed\n        \n        for i in range(0, self.del_stages - 1):\n            self.layers[i] = None\n\n    def forward(self, x, cb_feats=None, pre_tmps=None):\n        \"\"\"Forward function.\"\"\"\n        outs = []\n        tmps = []\n        if hasattr(self, 'patch_embed'):\n            x = self.patch_embed(x)\n\n            Wh, Ww = x.size(2), x.size(3)\n            if self.ape:\n                # interpolate the position embedding to the corresponding size\n                absolute_pos_embed = F.interpolate(\n                    self.absolute_pos_embed, size=(Wh, Ww), mode='bicubic')\n                x = (x + absolute_pos_embed).flatten(2).transpose(1, 2)  # B Wh*Ww C\n            else:\n                x = x.flatten(2).transpose(1, 2)\n            x = self.pos_drop(x)\n\n            tmps.append((x, Wh, Ww))\n        else:\n            x, Wh, Ww = pre_tmps[0]\n\n        for i in range(self.num_layers):\n            layer = self.layers[i]\n            if layer is None:\n                x_out, H, W, x, Wh, Ww = pre_tmps[i+1]\n            else:\n                if cb_feats is not None:\n                    x = x + cb_feats[i]\n                x_out, H, W, x, Wh, Ww = layer(x, Wh, Ww)\n            tmps.append((x_out, H, W, x, Wh, Ww))\n\n            if i in self.out_indices:\n                norm_layer = getattr(self, f'norm{i}')\n                x_out = norm_layer(x_out)\n\n                out = x_out.view(-1, H, W,\n                                 self.num_features[i]).permute(0, 3, 1, 2).contiguous()\n                outs.append(out)\n\n        return tuple(outs), tmps\n\n    def train(self, mode=True):\n        \"\"\"Convert the model into training mode while keep layers freezed.\"\"\"\n        super(_SwinTransformer, self).train(mode)\n        self._freeze_stages()\n\n\n@BACKBONES.register_module()\nclass CBSwinTransformer(BaseModule):\n    def __init__(self, embed_dim=96, cb_zero_init=True, cb_del_stages=1, **kwargs):\n        super(CBSwinTransformer, self).__init__()\n        self.cb_zero_init = cb_zero_init\n        self.cb_del_stages = cb_del_stages\n        self.cb_modules = nn.ModuleList()\n        for cb_idx in range(2):\n            cb_module = _SwinTransformer(embed_dim=embed_dim, **kwargs)\n            if cb_idx > 0:\n                cb_module.del_layers(cb_del_stages)\n            self.cb_modules.append(cb_module)\n\n        self.num_layers = self.cb_modules[0].num_layers\n\n        cb_inplanes = [embed_dim * 2 ** i for i in range(self.num_layers)]\n\n        self.cb_linears = nn.ModuleList()\n        for i in range(self.num_layers):\n            linears = nn.ModuleList()\n            if i >= self.cb_del_stages-1:\n                jrange = 4 - i\n                for j in range(jrange):\n                    if cb_inplanes[i + j] != cb_inplanes[i]:\n                        layer = nn.Conv2d(cb_inplanes[i + j], cb_inplanes[i], 1)\n                    else:\n                        layer = nn.Identity()\n                    linears.append(layer)\n            self.cb_linears.append(linears)\n\n    def _freeze_stages(self):\n        for m in self.cb_modules:\n            m._freeze_stages()\n\n    def init_weights(self):\n        \"\"\"Initialize the weights in backbone.\n\n        Args:\n            pretrained (str, optional): Path to pre-trained weights.\n                Defaults to None.\n        \"\"\"\n        # constant_init(self.cb_linears, 0)\n        if self.cb_zero_init:\n            for ls in self.cb_linears:\n                for m in ls:\n                    constant_init(m, 0)\n                        \n        for m in self.cb_modules:\n            m.init_weights()\n\n    def spatial_interpolate(self, x, H, W):\n        B, C = x.shape[:2]\n        if H != x.shape[2] or W != x.shape[3]:\n            # B, C, size[0], size[1]\n            x = F.interpolate(x, size=(H, W), mode='nearest')\n        x = x.view(B, C, -1).permute(0, 2, 1).contiguous()  # B, T, C\n        return x\n\n    def _get_cb_feats(self, feats, tmps):\n        cb_feats = []\n        Wh, Ww = tmps[0][-2:]\n        for i in range(self.num_layers):\n            feed = 0\n            if i >= self.cb_del_stages-1:\n                jrange = 4 - i\n                for j in range(jrange):\n                    tmp = self.cb_linears[i][j](feats[j + i])\n                    tmp = self.spatial_interpolate(tmp, Wh, Ww)\n                    feed += tmp\n            cb_feats.append(feed)\n            Wh, Ww = tmps[i+1][-2:]\n\n        return cb_feats\n\n    def forward(self, x):\n        outs = []\n        for i, module in enumerate(self.cb_modules):\n            if i == 0:\n                feats, tmps = module(x)\n            else:\n                feats, tmps = module(x, cb_feats, tmps)\n\n            outs.append(feats)\n            \n            if i < len(self.cb_modules)-1:\n                cb_feats = self._get_cb_feats(outs[-1], tmps)  \n        return tuple(outs)\n\n    def train(self, mode=True):\n        \"\"\"Convert the model into training mode while keep layers freezed.\"\"\"\n        super(CBSwinTransformer, self).train(mode)\n        for m in self.cb_modules:\n            m.train(mode=mode)\n        self._freeze_stages()\n        for m in self.cb_linears.modules():\n            # trick: eval have effect on BatchNorm only\n            if isinstance(m, _BatchNorm):\n                m.eval()\n","metadata":{"execution":{"iopub.status.busy":"2023-07-16T15:02:13.709676Z","iopub.execute_input":"2023-07-16T15:02:13.710097Z","iopub.status.idle":"2023-07-16T15:02:13.728596Z","shell.execute_reply.started":"2023-07-16T15:02:13.710064Z","shell.execute_reply":"2023-07-16T15:02:13.727375Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"if cfg.mmdet_version == 3:\n    config = f'/kaggle/working/{cfg.exp_name}.py'\n    #model_file = f'/kaggle/input/0619-htc-x101-baseline-fold1/fold5_best_coco_bbox_mAP_epoch_13.pth'\n    test_pkl = r'/kaggle/working/mmdet_test.json'\n    result_pkl = f'/kaggle/working/{cfg.exp_name}_infer_res.pkl'\n    \n    cfg_options = '--cfg-options'\n    cfg_options += f' test_dataloader.dataset.ann_file={test_pkl}'\n    cfg_options += f' test_dataloader.dataset.data_root=/kaggle/input/hubmap-hacking-the-human-vasculature/'\n    cfg_options += f' test_evaluator.ann_file={test_pkl}'\n    cfg_options += f' test_dataloader.dataset.data_prefix.img=test/'\n    cfg_options += f' model.test_cfg.rpn.max_per_img=1000' \n    cfg_options += f' model.test_cfg.rcnn.nms.type=weighted_cluster_nms'\n    cfg_options += f' model.test_cfg.rcnn.nms.iou_method=diou'\n    cfg_options += f' model.test_cfg.rcnn.nms.iou_threshold=0.5'\n    \n    cmd = f'python tools/test.py {config} {cfg.pth_file} --out {result_pkl} {cfg_options}'\n    !cd /kaggle/working/mmdetection-3.0.0; {cmd}\n    result = pickle.load(open(result_pkl, 'rb'))\n\nelse:\n    config = f'/kaggle/working/{cfg.exp_name}.py'\n    #model_file = f'/kaggle/input/0619-htc-x101-baseline-fold1/fold5_best_coco_bbox_mAP_epoch_13.pth'\n    test_pkl = r'/kaggle/working/mmdet_test.json'\n    result_pkl = f'/kaggle/working/{cfg.exp_name}_infer_res.pkl'\n    \n    cfg_options = '--cfg-options'\n    cfg_options += f' data.test.ann_file={test_pkl}'\n    cfg_options += f' data.test.img_prefix=/kaggle/input/hubmap-hacking-the-human-vasculature/test/'\n    cfg_options += f' model.test_cfg.rpn.nms_pre=1000' \n    cfg_options += f' model.test_cfg.rcnn.nms_pre=1000' \n    cfg_options += f' model.test_cfg.rcnn.nms.type=weighted_cluster_nms'\n    cfg_options += f' model.test_cfg.rcnn.nms.iou_method=diou'\n    cfg_options += f' model.test_cfg.rcnn.nms.iou_threshold=0.45'\n    #cfg_options += f\" data.test.pipeline.1.img_scale='[(608,608),(640,640),(704,704),(736,736),(800,800),(832,832),(896,896),(960,960),(992,992)]'\"\n    #cfg_options += f\" data.test.pipeline.1.img_scale='[(800,800),(864,864),(928,928),(1024,1024),(1120,1120),(1216,1216),(1312,1312),(1408,1408),(1472,1472),(1536,1536),(1664,1664),(1792,1792),(1920,1920)]'\"\n    cfg_options += f\" data.test.pipeline.1.img_scale='[(640,640),(800,800),(1024,1024),(1408,1408),(1696,1696),(1920,1920)]'\"\n    #cfg_options += r\" data.test.pipeline.1.img_scale='[(608,608),(640,640),(704,704),(736,736),(800,800),(832,832),(896,896),(960,960),(992,992)]'\"\n    cfg_options += f' data.test.pipeline.1.flip=True'\n    cfg_options += f\" data.test.pipeline.1.flip_direction='[horizontal,vertical]'\"\n    cfg_options += f' data.samples_per_gpu=1'\n    cfg_options += f' data.workers_per_gpu=1'\n    \n    cmd = f'python tools/test.py {config} {cfg.pth_file} --out {result_pkl} {cfg_options}'\n    !cd /kaggle/working/mmdetection-2.28.2; {cmd}\n    result = pickle.load(open(result_pkl, 'rb'))","metadata":{"execution":{"iopub.status.busy":"2023-07-16T15:02:13.730402Z","iopub.execute_input":"2023-07-16T15:02:13.730838Z","iopub.status.idle":"2023-07-16T15:03:01.613132Z","shell.execute_reply.started":"2023-07-16T15:02:13.730757Z","shell.execute_reply":"2023-07-16T15:03:01.612007Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 4.infer and ensemble","metadata":{}},{"cell_type":"markdown","source":"### 4.1 wbf of masks","metadata":{}},{"cell_type":"code","source":"# import warnings\n# import numpy as np\n# from numba import jit\n\n# @jit(nopython=True)\n# def bb_intersection_over_union(A, B) -> float:\n#     xA = max(A[0], B[0])\n#     yA = max(A[1], B[1])\n#     xB = min(A[2], B[2])\n#     yB = min(A[3], B[3])\n\n#     # compute the area of intersection rectangle\n#     interArea = max(0, xB - xA) * max(0, yB - yA)\n\n#     if interArea == 0:\n#         return 0.0\n\n#     # compute the area of both the prediction and ground-truth rectangles\n#     boxAArea = (A[2] - A[0]) * (A[3] - A[1])\n#     boxBArea = (B[2] - B[0]) * (B[3] - B[1])\n\n#     iou = interArea / float(boxAArea + boxBArea - interArea)\n#     return iou\n\n# #@jit(nopython=True)\n# def get_weighted_mask(masks, scores, inmodels, conf_type):\n#     mask = np.zeros(masks[0].shape, dtype=np.float32)\n#     conf = 0\n#     conf_list = []\n#     for m, s, im in zip(masks, scores, inmodels):\n#         if conf_type == 'model_weight2':\n#             mask += s * im * m\n#             conf += s * im\n#         else:\n#             mask += s * m\n#             conf += s\n#         conf_list.append(s)\n#     score = np.max(conf_list)\n#     mask = mask / conf\n#     return mask, score, conf_list\n\n# def get_weighted_box(boxes, scores, inmodels, conf_type):\n#     box = np.zeros(4, dtype=np.float32)\n#     conf = 0\n#     conf_list = []\n#     for b, s, im in zip(boxes, scores, inmodels):\n#         if conf_type == 'model_weight2':\n#             box += s * im * b\n#             conf += s * im\n#         else:\n#             box += s * b\n#             conf += s\n#         conf_list.append(s)\n#     score = np.max(conf_list)\n#     box = box / conf\n#     return box, score\n\n\n# def find_matching_box(boxes_list, new_box, match_iou):\n#     best_iou = match_iou\n#     best_index = -1\n#     for i in range(len(boxes_list)):\n#         box = boxes_list[i]\n#         iou = bb_intersection_over_union(box, new_box)\n#         if iou > best_iou:\n#             best_index = i\n#             best_iou = iou\n\n#     return best_index, best_iou\n\n# def weighted_masks_fusion(masks, boxes, scores, models, iou_thr=0.7, skip_mask_thr=0.0, \n#                         conf_type='max_weight', soft_weight=5, thresh_type=None, model_weights=1,\n#                         num_thresh=4, num_models=5):\n#     masks = masks[scores > skip_mask_thr]\n#     boxes = boxes[scores > skip_mask_thr]\n#     models = models[scores > skip_mask_thr]\n#     scores = scores[scores > skip_mask_thr]\n    \n#     new_masks = []\n#     new_boxes = []\n#     new_scores = []\n#     inmodels = []\n#     weighted_boxes = []\n#     weighted_scores = []\n#     # Clusterize boxes\n#     for i in range(len(masks)):\n            \n#         index, best_iou = find_matching_box(weighted_boxes, boxes[i], iou_thr)\n#         if index != -1:\n#             new_masks[index].append(masks[i])\n#             new_boxes[index].append(boxes[i])\n#             new_scores[index].append(scores[i])\n#             inmodels[index].append(models[i])\n#             weighted_boxes[index], weighted_scores[index] = get_weighted_box(new_boxes[index], new_scores[index], inmodels[index], conf_type)\n#         else:\n#             new_masks.append([masks[i]])\n#             new_boxes.append([boxes[i].copy()])\n#             new_scores.append([scores[i].copy()])\n#             inmodels.append([models[i]])\n#             weighted_boxes.append(boxes[i].copy())\n#             weighted_scores.append(scores[i].copy())\n            \n#     ens_masks = []\n#     ens_scores = []\n#     ens_boxes = []\n#     for nmasks, nscores, wbox, inms in zip(new_masks, new_scores, weighted_boxes, inmodels):\n#         mask, score, conf_list = get_weighted_mask(nmasks, nscores, inms, conf_type)\n#         if thresh_type == 'num_thresh':\n#             if len(conf_list) >= num_thresh:\n#                 ens_masks.append(mask)\n#                 ens_boxes.append(wbox)\n#             else:\n#                 continue\n#         else:\n#             ens_masks.append(mask)\n#             ens_boxes.append(wbox)\n\n#         if conf_type =='max_weight':\n#             ens_scores.append(score * min(len(conf_list), num_models) / num_models)\n#         elif conf_type == 'max':\n#             ens_scores.append(score)\n#         elif conf_type == 'soft_weight':\n#             ens_scores.append(score * (min(len(conf_list), num_models) + soft_weight) / (soft_weight + num_models))\n#         elif conf_type == 'model_weight' or conf_type == 'model_weight2':\n#             this_weights = [model_weights[i] for i in inms]\n#             ens_scores.append(score * (min(np.sum(this_weights), np.sum(model_weights)) + soft_weight) / (soft_weight + np.sum(model_weights)))\n\n#     return ens_masks, ens_scores, ens_boxes\n\n","metadata":{"execution":{"iopub.status.busy":"2023-07-16T15:03:01.614785Z","iopub.execute_input":"2023-07-16T15:03:01.61515Z","iopub.status.idle":"2023-07-16T15:03:01.627068Z","shell.execute_reply.started":"2023-07-16T15:03:01.615113Z","shell.execute_reply":"2023-07-16T15:03:01.626203Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 4. Submission ","metadata":{}},{"cell_type":"code","source":"def encode_binary_mask(mask: np.ndarray) -> t.Text:\n  \"\"\"Converts a binary mask into OID challenge encoding ascii text.\"\"\"\n\n  # check input mask --\n  if mask.dtype != np.bool:\n    raise ValueError(\n        \"encode_binary_mask expects a binary mask, received dtype == %s\" %\n        mask.dtype)\n\n  mask = np.squeeze(mask)\n  if len(mask.shape) != 2:\n    raise ValueError(\n        \"encode_binary_mask expects a 2d mask, received shape == %s\" %\n        mask.shape)\n\n  # convert input mask to expected COCO API input --\n  mask_to_encode = mask.reshape(mask.shape[0], mask.shape[1], 1)\n  mask_to_encode = mask_to_encode.astype(np.uint8)\n  mask_to_encode = np.asfortranarray(mask_to_encode)\n\n  # RLE encode mask --\n  encoded_mask = coco_mask.encode(mask_to_encode)[0][\"counts\"]\n\n  # compress and base64 encoding --\n  binary_str = zlib.compress(encoded_mask, zlib.Z_BEST_COMPRESSION)\n  base64_str = base64.b64encode(binary_str)\n  return base64_str","metadata":{"execution":{"iopub.status.busy":"2023-07-16T15:03:01.628865Z","iopub.execute_input":"2023-07-16T15:03:01.629268Z","iopub.status.idle":"2023-07-16T15:03:01.639374Z","shell.execute_reply.started":"2023-07-16T15:03:01.629233Z","shell.execute_reply":"2023-07-16T15:03:01.638421Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!rm -rf /kaggle/working/mmdetection*\n!rm -rf /kaggle/working/mmpretrain*","metadata":{"execution":{"iopub.status.busy":"2023-07-16T15:03:01.640641Z","iopub.execute_input":"2023-07-16T15:03:01.641049Z","iopub.status.idle":"2023-07-16T15:03:03.588395Z","shell.execute_reply.started":"2023-07-16T15:03:01.641013Z","shell.execute_reply":"2023-07-16T15:03:03.58703Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#with open('/kaggle/working/submission.csv', 'w') as sub_file:\n #   sub_file.write('id,height,width,prediction_string\\n')\nif cfg.mmdet_version == 3:\n    data = []\n    for ii in tqdm(range(len(result))):\n        image_id = result[ii]['img_id']\n\n        #sub_file.write(f'{image_id},512,512,')\n\n        bbs = result[ii]['pred_instances']['bboxes']\n        sgs = result[ii]['pred_instances']['masks']\n        confs = result[ii]['pred_instances']['scores']\n        labels = result[ii]['pred_instances']['labels']\n        pred_string=\"\"\n        flag = 0\n        for bb, sg, cnf, label in zip(bbs, sgs, confs, labels):\n            box = bb\n            h = sg['size'][0]\n            w = sg['size'][1]\n\n            msk=np.array(mutils.decode(sg))  #h*w的mask   #cp\n            msk = np.array(msk, dtype=bool)\n            rle  = encode_binary_mask(msk)\n            if flag == 0:\n                flag = 1\n                pred_string += f\"{int(label)} {cnf} {rle.decode('utf-8')}\"\n            else:\n                pred_string += f\" {int(label)} {cnf} {rle.decode('utf-8')}\" \n        data.append((image_id,w,h, pred_string))\n                #sub_file.write(f'{int(label)} {cnf} {rle.decode()} ' )   \n\nelse:\n    data = []\n    for ii in tqdm(range(len(result))):\n        image_id = annos[\"images\"][ii]['id']\n\n        #sub_file.write(f'{image_id},512,512,')\n\n        bbs = result[ii][0][0]\n        sgs = result[ii][1][0]\n        confs = bbs[:,4]\n        label = 0\n        pred_string=\"\"\n        flag = 0\n        for bb, sg, cnf in zip(bbs, sgs, confs):\n            box = bb\n            h = sg['size'][0]\n            w = sg['size'][1]\n\n            msk=np.array(mutils.decode(sg))  #h*w的mask   #cp\n            msk = np.array(msk, dtype=bool)\n            rle  = encode_binary_mask(msk)\n            if flag == 0:\n                flag = 1\n                pred_string += f\"{int(label)} {cnf} {rle.decode('utf-8')}\"\n            else:\n                pred_string += f\" {int(label)} {cnf} {rle.decode('utf-8')}\" \n        data.append((image_id,w,h, pred_string))","metadata":{"execution":{"iopub.status.busy":"2023-07-16T15:03:03.591256Z","iopub.execute_input":"2023-07-16T15:03:03.592043Z","iopub.status.idle":"2023-07-16T15:03:03.707658Z","shell.execute_reply.started":"2023-07-16T15:03:03.592001Z","shell.execute_reply":"2023-07-16T15:03:03.706762Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_sub = pd.DataFrame(data, columns=['id','height','width','prediction_string'])\ndf_sub.to_csv(\"submission.csv\", index=False)\ndf_sub.head()","metadata":{"execution":{"iopub.status.busy":"2023-07-16T15:03:03.709101Z","iopub.execute_input":"2023-07-16T15:03:03.70969Z","iopub.status.idle":"2023-07-16T15:03:03.727579Z","shell.execute_reply.started":"2023-07-16T15:03:03.709657Z","shell.execute_reply":"2023-07-16T15:03:03.726733Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#!cat submission.csv","metadata":{"execution":{"iopub.status.busy":"2023-07-16T15:03:03.730618Z","iopub.execute_input":"2023-07-16T15:03:03.730891Z","iopub.status.idle":"2023-07-16T15:03:03.736893Z","shell.execute_reply.started":"2023-07-16T15:03:03.730868Z","shell.execute_reply":"2023-07-16T15:03:03.735907Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}