{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# Install mmdet","metadata":{}},{"cell_type":"code","source":"!pip install ../input/mmsegmentation/mmcv-full/mmcv_full-1.5.3-cp37-cp37m-linux_x86_64.whl\n!pip install ../input/openmmlab-essential-repositories/openmmlab-repos/src/mmcls-0.23.1-py2.py3-none-any.whl\n!pip install ../input/mmdetection/addict-2.4.0-py3-none-any.whl\n!pip install ../input/mmdetection/yapf-0.31.0-py2.py3-none-any.whl\n!pip install ../input/mmdetection/terminaltables-3.1.0-py3-none-any.whl\n!pip install ../input/mmdetection/einops-0.4.1-py3-none-any.whl","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-08-08T13:35:08.311319Z","iopub.execute_input":"2023-08-08T13:35:08.312531Z","iopub.status.idle":"2023-08-08T13:36:23.164211Z","shell.execute_reply.started":"2023-08-08T13:35:08.312418Z","shell.execute_reply":"2023-08-08T13:36:23.162951Z"},"collapsed":true,"jupyter":{"outputs_hidden":true}},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!cp -r /kaggle/input/openmmlab-essential-repositories/openmmlab-repos/mmdetection /kaggle/working\n%cd mmdetection\n!pip install -q -e .","metadata":{"execution":{"iopub.status.busy":"2023-08-08T13:37:07.207928Z","iopub.execute_input":"2023-08-08T13:37:07.208367Z","iopub.status.idle":"2023-08-08T13:38:05.603222Z","shell.execute_reply.started":"2023-08-08T13:37:07.208327Z","shell.execute_reply":"2023-08-08T13:38:05.601865Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Config","metadata":{}},{"cell_type":"markdown","source":"- This config below is for training **stage 1**.\n- **For stage 2** (finetuning with dataset1 only) we need change 2 lines:\n    - #1: data.train.ann_file -> /kaggle/input/coco-hubmap-final-2class/coco_annotations_train_all_fold1_ds1_only_2classes.json\n    - #2: load_from -> best model from stage1\n- **For other models** like MaskRCNN and HTC, we modified the model and pre-trained settings in the configuration file while retaining the other configurations unchanged.\n\n- Increase *samples_per_gpu* and *workers_per_gpu* if you have GPU with more VRam","metadata":{}},{"cell_type":"code","source":"%%writefile /kaggle/working/hubmap_regnet.py\n\nnum_classes = 2\nmodel = dict(\n    type='CascadeRCNN',\n    backbone=dict(\n        type='RegNet',\n        arch='regnetx_4.0gf',\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    neck=dict(\n        type='FPN',\n        in_channels=[80, 240, 560, 1360],\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, 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(\n            type='SmoothL1Loss', beta=0.1111111111111111, 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=2,\n                bbox_coder=dict(\n                    type='DeltaXYWHBBoxCoder',\n                    target_means=[0.0, 0.0, 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=2,\n                bbox_coder=dict(\n                    type='DeltaXYWHBBoxCoder',\n                    target_means=[0.0, 0.0, 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=2,\n                bbox_coder=dict(\n                    type='DeltaXYWHBBoxCoder',\n                    target_means=[0.0, 0.0, 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=2,\n            loss_mask=dict(\n                type='CrossEntropyLoss', use_mask=True, loss_weight=1.0))),\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.6),\n            max_per_img=100,\n            mask_thr_binary=0.5)))\nclasses = ['blood_vessel', 'unsure']\ndataset_type = 'CocoDataset'\ndata_root = '/kaggle/input/hubmap-hacking-the-human-vasculature/'\nalbu_train_transforms = [\n    dict(type='VerticalFlip', p=0.5),\n    dict(type='RandomRotate90', p=0.5)\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(\n        type='Resize', img_scale=[(1536, 1536), (512, 512)], keep_ratio=False),\n    dict(type='RandomFlip', flip_ratio=0.5),\n    dict(\n        type='Normalize',\n        mean=[123.675, 116.28, 103.53],\n        std=[58.395, 57.12, 57.375],\n        to_rgb=True),\n    dict(type='Pad', size_divisor=32),\n    dict(\n        type='Albu',\n        transforms=[\n            dict(type='VerticalFlip', p=0.5),\n            dict(type='RandomRotate90', p=0.5)\n        ],\n        bbox_params=dict(\n            type='BboxParams',\n            format='pascal_voc',\n            label_fields=['gt_labels'],\n            min_visibility=0.0,\n            filter_lost_elements=True),\n        keymap=dict(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), (512, 512)],\n        flip=True,\n        flip_direction=['horizontal', 'vertical'],\n        transforms=[\n            dict(type='Resize', keep_ratio=True),\n            dict(type='RandomFlip'),\n            dict(\n                type='Normalize',\n                mean=[123.675, 116.28, 103.53],\n                std=[58.395, 57.12, 57.375],\n                to_rgb=True),\n            dict(type='Pad', size_divisor=32),\n            dict(type='ImageToTensor', keys=['img']),\n            dict(type='Collect', keys=['img'])\n        ])\n]\ndata = dict(\n    samples_per_gpu=2,\n    workers_per_gpu=2,\n    train=dict(\n        type='CocoDataset',\n        ann_file=\n        '/kaggle/input/coco-hubmap-final-2class/coco_annotations_train_all_fold1_2classes.json', #1\n        img_prefix='/kaggle/input/hubmap-hacking-the-human-vasculature/train/',\n        classes=['blood_vessel', 'unsure'],\n        pipeline=[\n            dict(type='LoadImageFromFile'),\n            dict(type='LoadAnnotations', with_bbox=True, with_mask=True),\n            dict(\n                type='Resize',\n                img_scale=[(1536, 1536), (512, 512)],\n                keep_ratio=False),\n            dict(type='RandomFlip', flip_ratio=0.5),\n            dict(\n                type='Normalize',\n                mean=[123.675, 116.28, 103.53],\n                std=[58.395, 57.12, 57.375],\n                to_rgb=True),\n            dict(type='Pad', size_divisor=32),\n            dict(\n                type='Albu',\n                transforms=[\n                    dict(type='VerticalFlip', p=0.5),\n                    dict(type='RandomRotate90', p=0.5)\n                ],\n                bbox_params=dict(\n                    type='BboxParams',\n                    format='pascal_voc',\n                    label_fields=['gt_labels'],\n                    min_visibility=0.0,\n                    filter_lost_elements=True),\n                keymap=dict(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(\n                type='Collect',\n                keys=['img', 'gt_bboxes', 'gt_masks', 'gt_labels'])\n        ]),\n    val=dict(\n        type='CocoDataset',\n        ann_file=\n        '/kaggle/input/coco-hubmap-final-2class/coco_annotations_valid_all_fold1_2classes.json',\n        img_prefix='/kaggle/input/hubmap-hacking-the-human-vasculature/train/',\n        classes=['blood_vessel', 'unsure'],\n        pipeline=[\n            dict(type='LoadImageFromFile'),\n            dict(\n                type='MultiScaleFlipAug',\n                img_scale=[(1024, 1024), (512, 512)],\n                flip=True,\n                flip_direction=['horizontal', 'vertical'],\n                transforms=[\n                    dict(type='Resize', keep_ratio=True),\n                    dict(type='RandomFlip'),\n                    dict(\n                        type='Normalize',\n                        mean=[123.675, 116.28, 103.53],\n                        std=[58.395, 57.12, 57.375],\n                        to_rgb=True),\n                    dict(type='Pad', size_divisor=32),\n                    dict(type='ImageToTensor', keys=['img']),\n                    dict(type='Collect', keys=['img'])\n                ])\n        ]),\n    test=dict(\n        type='CocoDataset',\n        ann_file=\n        '/kaggle/input/coco-hubmap-final-2class/coco_annotations_valid_all_fold1_2classes.json',\n        img_prefix='/kaggle/input/hubmap-hacking-the-human-vasculature/train/',\n        classes=['blood_vessel', 'unsure'],\n        pipeline=[\n            dict(type='LoadImageFromFile'),\n            dict(\n                type='MultiScaleFlipAug',\n                img_scale=[(1024, 1024), (512, 512)],\n                flip=True,\n                flip_direction=['horizontal', 'vertical'],\n                transforms=[\n                    dict(type='Resize', keep_ratio=True),\n                    dict(type='RandomFlip'),\n                    dict(\n                        type='Normalize',\n                        mean=[123.675, 116.28, 103.53],\n                        std=[58.395, 57.12, 57.375],\n                        to_rgb=True),\n                    dict(type='Pad', size_divisor=32),\n                    dict(type='ImageToTensor', keys=['img']),\n                    dict(type='Collect', keys=['img'])\n                ])\n        ]))\nevaluation = dict(\n    metric=['bbox', 'segm'], interval=1, save_best='segm_mAP', classwise=True)\noptimizer = dict(\n    type='AdamW',\n    lr=0.0001,\n    betas=(0.9, 0.999),\n    weight_decay=0.01,\n    paramwise_cfg=dict(\n        custom_keys=dict(\n            absolute_pos_embed=dict(decay_mult=0.0),\n            relative_position_bias_table=dict(decay_mult=0.0),\n            norm=dict(decay_mult=0.0))))\noptimizer_config = dict(grad_clip=None)\nlr_config = dict(\n    policy='step',\n    warmup='linear',\n    warmup_iters=2000,\n    warmup_ratio=0.3333333333333333,\n    step=[14, 22],\n    gamma=0.5)\nrunner = dict(type='EpochBasedRunner', max_epochs=24)\nfind_unused_parameters = True\ncudnn_benchmark = True\ncheckpoint_config = dict(interval=-1, save_optimizer=False, by_epoch=True)\nlog_config = dict(interval=20, hooks=[dict(type='TextLoggerHook')])\ncustom_hooks = [dict(type='NumClassCheckHook')]\ndist_params = dict(backend='nccl')\nlog_level = 'INFO'\nload_from = 'https://download.openmmlab.com/mmdetection/v2.0/regnet/cascade_mask_rcnn_regnetx-4GF_fpn_mstrain_3x_coco/cascade_mask_rcnn_regnetx-4GF_fpn_mstrain_3x_coco_20210715_212034-cbb1be4c.pth'  #2\nresume_from = None\nfp16 = dict(loss_scale=dict(init_scale=512))\nworkflow = [('train', 1)]\nauto_resume = False\nauto_scale_lr = dict(enable=False, base_batch_size=4)\ngpu_ids = [0]\nwork_dir = './work_dirs/regnet_stage1'\n","metadata":{"execution":{"iopub.status.busy":"2023-08-08T13:59:45.476212Z","iopub.execute_input":"2023-08-08T13:59:45.476665Z","iopub.status.idle":"2023-08-08T13:59:45.493754Z","shell.execute_reply.started":"2023-08-08T13:59:45.476607Z","shell.execute_reply":"2023-08-08T13:59:45.492701Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Training","metadata":{}},{"cell_type":"code","source":"%cd /kaggle/working/mmdetection\n!python tools/train.py /kaggle/working/hubmap_regnet.py","metadata":{"execution":{"iopub.status.busy":"2023-08-08T13:59:47.288502Z","iopub.execute_input":"2023-08-08T13:59:47.289790Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}