Swin Transformer for Object Detection - Inference with COCO dataset 4. Cascade Mask RCNN pretrained ImageNet22k
on Projects
1. Reference
2. Edit the code
configs/swin/cascade_mask_rcnn_swin-s-p4-w7_fpn_ms-crop-3x_coco.py
_base_ = './cascade_mask_rcnn_swin-t-p4-w7_fpn_fp16_ms-crop-3x_coco.py'
pretrained = 'https://github.com/SwinTransformer/storage/releases/download/v1.0.8/swin_small_patch4_window7_224_22k.pth' # noqa
model = dict(
backbone=dict(
depths=[2, 2, 18, 2],
init_cfg=dict(type='Pretrained', checkpoint=pretrained)))
configs/swin/cascade_mask_rcnn_swin-t-p4-w7_fpn_ms-crop-3x_coco.py
_base_ = [
'../_base_/models/cascade_mask_rcnn_swin_fpn.py',
'../_base_/datasets/coco_instance.py',
'../_base_/schedules/schedule_1x.py', '../_base_/default_runtime.py'
]
pretrained = 'https://github.com/SwinTransformer/storage/releases/download/v1.0.8/swin_tiny_patch4_window7_224_22k.pth' # noqa
model = dict(
type='CascadeRCNN',
backbone=dict(
_delete_=True,
type='SwinTransformer',
embed_dims=96,
depths=[2, 2, 6, 2],
num_heads=[3, 6, 12, 24],
window_size=7,
mlp_ratio=4,
qkv_bias=True,
qk_scale=None,
drop_rate=0.,
attn_drop_rate=0.,
drop_path_rate=0.2,
patch_norm=True,
out_indices=(0, 1, 2, 3),
with_cp=False,
convert_weights=True,
init_cfg=dict(type='Pretrained', checkpoint=pretrained)),
neck=dict(in_channels=[96, 192, 384, 768]))
img_norm_cfg = dict(
mean=[123.675, 116.28, 103.53], std=[58.395, 57.12, 57.375], to_rgb=True)
# augmentation strategy originates from DETR / Sparse RCNN
train_pipeline = [
dict(type='LoadImageFromFile'),
dict(type='LoadAnnotations', with_bbox=True, with_mask=True),
dict(type='RandomFlip', flip_ratio=0.5),
dict(
type='AutoAugment',
policies=[[
dict(
type='Resize',
img_scale=[(480, 1333), (512, 1333), (544, 1333), (576, 1333),
(608, 1333), (640, 1333), (672, 1333), (704, 1333),
(736, 1333), (768, 1333), (800, 1333)],
multiscale_mode='value',
keep_ratio=True)
],
[
dict(
type='Resize',
img_scale=[(400, 1333), (500, 1333), (600, 1333)],
multiscale_mode='value',
keep_ratio=True),
dict(
type='RandomCrop',
crop_type='absolute_range',
crop_size=(384, 600),
allow_negative_crop=True),
dict(
type='Resize',
img_scale=[(480, 1333), (512, 1333), (544, 1333),
(576, 1333), (608, 1333), (640, 1333),
(672, 1333), (704, 1333), (736, 1333),
(768, 1333), (800, 1333)],
multiscale_mode='value',
override=True,
keep_ratio=True)
]]),
dict(type='Normalize', **img_norm_cfg),
dict(type='Pad', size_divisor=32),
dict(type='DefaultFormatBundle'),
dict(type='Collect', keys=['img', 'gt_bboxes', 'gt_labels', 'gt_masks']),
]
data = dict(train=dict(pipeline=train_pipeline))
optimizer = dict(
_delete_=True,
type='AdamW',
lr=0.0001,
betas=(0.9, 0.999),
weight_decay=0.05,
paramwise_cfg=dict(
custom_keys={
'absolute_pos_embed': dict(decay_mult=0.),
'relative_position_bias_table': dict(decay_mult=0.),
'norm': dict(decay_mult=0.)
}))
lr_config = dict(warmup_iters=1000, step=[27, 33])
runner = dict(max_epochs=36)
3. Train
- 4 GPU 3090 RTX
- 3~4 days
cascade_mask_rcnn_swin-s-p4-w7_fpn_fp16_ms-crop-3x_coco.py
bash ./tools/dist_train.sh configs/swin/cascade_mask_rcnn_swin-s-p4-w7_fpn_fp16_ms-crop-3x_coco.py 4
cascade_mask_rcnn_swin-t-p4-w7_fpn_fp16_ms-crop-3x_coco.py
bash ./tools/dist_train.sh configs/swin/cascade_mask_rcnn_swin-t-p4-w7_fpn_fp16_ms-crop-3x_coco.py 4
4. Test
cascade_mask_rcnn_swin-s-p4-w7_fpn_fp16_ms-crop-3x_coco.py
python tools/test.py configs/swin/cascade_mask_rcnn_swin-s-p4-w7_fpn_fp16_ms-crop-3x_coco.py checkpoints/cascade_mask_rcnn_swin_s_imgnet22k_pretrained.pth --eval bbox segm
cascade_mask_rcnn_swin-t-p4-w7_fpn_fp16_ms-crop-3x_coco.py
python tools/test.py configs/swin/cascade_mask_rcnn_swin-t-p4-w7_fpn_fp16_ms-crop-3x_coco.py checkpoints/cascade_mask_rcnn_swin-t-p4-w7_fpn_ms-crop-3x_coco_22k.pth --eval bbox segm
5. Results
cascade_mask_rcnn_swin-s-p4-w7_fpn_fp16_ms-crop-3x_coco.py
Evaluating bbox...
Loading and preparing results...
DONE (t=0.34s)
creating index...
index created!
Running per image evaluation...
Evaluate annotation type *bbox*
DONE (t=19.67s).
Accumulating evaluation results...
DONE (t=3.62s).
Average Precision (AP) @[ IoU=0.50:0.95 | area= all | maxDets=100 ] = 0.512
Average Precision (AP) @[ IoU=0.50 | area= all | maxDets=1000 ] = 0.704
Average Precision (AP) @[ IoU=0.75 | area= all | maxDets=1000 ] = 0.560
Average Precision (AP) @[ IoU=0.50:0.95 | area= small | maxDets=1000 ] = 0.337
Average Precision (AP) @[ IoU=0.50:0.95 | area=medium | maxDets=1000 ] = 0.550
Average Precision (AP) @[ IoU=0.50:0.95 | area= large | maxDets=1000 ] = 0.668
Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets=100 ] = 0.635
Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets=300 ] = 0.635
Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets=1000 ] = 0.635
Average Recall (AR) @[ IoU=0.50:0.95 | area= small | maxDets=1000 ] = 0.460
Average Recall (AR) @[ IoU=0.50:0.95 | area=medium | maxDets=1000 ] = 0.673
Average Recall (AR) @[ IoU=0.50:0.95 | area= large | maxDets=1000 ] = 0.787
Evaluating segm...
/opt/conda/lib/python3.7/site-packages/mmdet/datasets/coco.py:474: UserWarning: The key "bbox" is deleted for more accurate mask AP of small/medium/large instances since v2.12.0. This does not change the overall mAP calculation.
UserWarning)
Loading and preparing results...
DONE (t=0.69s)
creating index...
index created!
Running per image evaluation...
Evaluate annotation type *segm*
DONE (t=21.17s).
Accumulating evaluation results...
/opt/conda/lib/python3.7/site-packages/pycocotools/cocoeval.py:378: DeprecationWarning: `np.float` is a deprecated alias for the builtin `float`. To silence this warning, use `float` by itself. Doing this will not modify any behavior and is safe. If you specifically wanted the numpy scalar type, use `np.float64` here.
Deprecated in NumPy 1.20; for more details and guidance: https://numpy.org/devdocs/release/1.20.0-notes.html#deprecations
tp_sum = np.cumsum(tps, axis=1).astype(dtype=np.float)
DONE (t=3.64s).
Average Precision (AP) @[ IoU=0.50:0.95 | area= all | maxDets=100 ] = 0.446
Average Precision (AP) @[ IoU=0.50 | area= all | maxDets=1000 ] = 0.677
Average Precision (AP) @[ IoU=0.75 | area= all | maxDets=1000 ] = 0.484
Average Precision (AP) @[ IoU=0.50:0.95 | area= small | maxDets=1000 ] = 0.248
Average Precision (AP) @[ IoU=0.50:0.95 | area=medium | maxDets=1000 ] = 0.477
Average Precision (AP) @[ IoU=0.50:0.95 | area= large | maxDets=1000 ] = 0.642
Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets=100 ] = 0.559
Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets=300 ] = 0.559
Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets=1000 ] = 0.559
Average Recall (AR) @[ IoU=0.50:0.95 | area= small | maxDets=1000 ] = 0.384
Average Recall (AR) @[ IoU=0.50:0.95 | area=medium | maxDets=1000 ] = 0.598
Average Recall (AR) @[ IoU=0.50:0.95 | area= large | maxDets=1000 ] = 0.724
OrderedDict([('bbox_mAP', 0.512), ('bbox_mAP_50', 0.704), ('bbox_mAP_75', 0.56), ('bbox_mAP_s', 0.337), ('bbox_mAP_m', 0.55), ('bbox_mAP_l', 0.668), ('bbox_mAP_copypaste', '0.512 0.704 0.560 0.337 0.550 0.668'), ('segm_mAP', 0.446), ('segm_mAP_50', 0.677), ('segm_mAP_75', 0.484), ('segm_mAP_s', 0.248), ('segm_mAP_m', 0.477), ('segm_mAP_l', 0.642), ('segm_mAP_copypaste', '0.446 0.677 0.484 0.248 0.477 0.642')])
cascade_mask_rcnn_swin-t-p4-w7_fpn_fp16_ms-crop-3x_coco.py
Evaluating bbox...
Loading and preparing results...
DONE (t=0.28s)
creating index...
index created!
Running per image evaluation...
Evaluate annotation type *bbox*
DONE (t=16.57s).
Accumulating evaluation results...
DONE (t=3.45s).
Average Precision (AP) @[ IoU=0.50:0.95 | area= all | maxDets=100 ] = 0.491
Average Precision (AP) @[ IoU=0.50 | area= all | maxDets=1000 ] = 0.683
Average Precision (AP) @[ IoU=0.75 | area= all | maxDets=1000 ] = 0.540
Average Precision (AP) @[ IoU=0.50:0.95 | area= small | maxDets=1000 ] = 0.334
Average Precision (AP) @[ IoU=0.50:0.95 | area=medium | maxDets=1000 ] = 0.527
Average Precision (AP) @[ IoU=0.50:0.95 | area= large | maxDets=1000 ] = 0.630
Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets=100 ] = 0.622
Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets=300 ] = 0.622
Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets=1000 ] = 0.622
Average Recall (AR) @[ IoU=0.50:0.95 | area= small | maxDets=1000 ] = 0.459
Average Recall (AR) @[ IoU=0.50:0.95 | area=medium | maxDets=1000 ] = 0.660
Average Recall (AR) @[ IoU=0.50:0.95 | area= large | maxDets=1000 ] = 0.760
Evaluating segm...
/opt/conda/lib/python3.7/site-packages/mmdet/datasets/coco.py:474: UserWarning: The key "bbox" is deleted for more accurate mask AP of small/medium/large instances since v2.12.0. This does not change the overall mAP calculation.
UserWarning)
Loading and preparing results...
DONE (t=0.69s)
creating index...
index created!
Running per image evaluation...
Evaluate annotation type *segm*
DONE (t=19.17s).
Accumulating evaluation results...
/opt/conda/lib/python3.7/site-packages/pycocotools/cocoeval.py:378: DeprecationWarning: `np.float` is a deprecated alias for the builtin `float`. To silence this warning, use `float` by itself. Doing this will not modify any behavior and is safe. If you specifically wanted the numpy scalar type, use `np.float64` here.
Deprecated in NumPy 1.20; for more details and guidance: https://numpy.org/devdocs/release/1.20.0-notes.html#deprecations
tp_sum = np.cumsum(tps, axis=1).astype(dtype=np.float)
DONE (t=3.43s).
Average Precision (AP) @[ IoU=0.50:0.95 | area= all | maxDets=100 ] = 0.430
Average Precision (AP) @[ IoU=0.50 | area= all | maxDets=1000 ] = 0.658
Average Precision (AP) @[ IoU=0.75 | area= all | maxDets=1000 ] = 0.463
Average Precision (AP) @[ IoU=0.50:0.95 | area= small | maxDets=1000 ] = 0.235
Average Precision (AP) @[ IoU=0.50:0.95 | area=medium | maxDets=1000 ] = 0.463
Average Precision (AP) @[ IoU=0.50:0.95 | area= large | maxDets=1000 ] = 0.614
Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets=100 ] = 0.550
Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets=300 ] = 0.550
Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets=1000 ] = 0.550
Average Recall (AR) @[ IoU=0.50:0.95 | area= small | maxDets=1000 ] = 0.375
Average Recall (AR) @[ IoU=0.50:0.95 | area=medium | maxDets=1000 ] = 0.593
Average Recall (AR) @[ IoU=0.50:0.95 | area= large | maxDets=1000 ] = 0.704
OrderedDict([('bbox_mAP', 0.491), ('bbox_mAP_50', 0.683), ('bbox_mAP_75', 0.54), ('bbox_mAP_s', 0.334), ('bbox_mAP_m', 0.527), ('bbox_mAP_l', 0.63), ('bbox_mAP_copypaste', '0.491 0.683 0.540 0.334 0.527 0.630'), ('segm_mAP', 0.43), ('segm_mAP_50', 0.658), ('segm_mAP_75', 0.463), ('segm_mAP_s', 0.235), ('segm_mAP_m', 0.463), ('segm_mAP_l', 0.614), ('segm_mAP_copypaste', '0.430 0.658 0.463 0.235 0.463 0.614')])
backbone | head | pretrained | dataset | box AP | mask AP |
---|---|---|---|---|---|
Swin T | Cascade Mask R-CNN | ImageNet 22k | COCO | 49.1 | 43.0 |
Swin S | Cascade Mask R-CNN | ImageNet 22k | COCO | 51.2 | 44.6 |