Play with MMDetection-MMDetection to make your own configuration file (3)

Interpretation of model framework files and parameters in MMDetection-MMDetection (1)

Play with the data set file, training plan file, operation information file and specific parameter interpretation in MMDetection-MMDetection (2) 

1. Import model architecture, dataset files, training plan files, and running information files

Create a me.py file, import your required model files, dataset files, training plan files, and run information files in _base_

This article uses mask_rcnn_swin_tiny_patch4_window7_mstrain_480-800_adamw_1x_coco.py as an example to explain.

_base_ = [
    '../_base_/models/faster_rcnn_r50_fpn.py',#模型文件
    '../_base_/datasets/coco_detection.py',#数据集文件
    '../_base_/schedules/schedule_1x.py',#训练计划文件
    '../_base_/default_runtime.py' #运行信息文件
]

2. Modify the parameters that need to be modified in the model framework file in a fixed way in your own configuration file

Then, in me.py, according to the ../_base_/models/faster_rcnn_r50_fpn.py file, write some parameters that you need to modify, such as what backbone to choose, the number of backbone channels, etc.

model = dict(
	backbone=dict('各种你需要修改的参数,不需要修改的参数无需填写'),
	neck=dict('各种你需要修改的参数,不需要修改的参数无需填写'),
	rpn_head=dict('各种你需要修改的参数,不需要修改的参数无需填写'),
	roi_head=dict('各种你需要修改的参数,不需要修改的参数无需填写'),
	train_cfg=dict('各种你需要修改的参数,不需要修改的参数无需填写'),
	test_cfg=dict('各种你需要修改的参数,不需要修改的参数无需填写'),
)

It is important to emphasize that in your me.py file, if all parameters are consistent with the ../_base_/models/faster_rcnn_r50_fpn.py file you imported, there is no need to write and change.

 3. Modify the parameters that need to be modified in the data set file in a fixed way in your own configuration file

In the me.py file after the second step, follow the ../_base_/datasets/coco_detection.py file to match and write some parameters you need to modify, such as changing the image preprocessing method, image cropping and zooming, etc. .

img_norm_cfg = dict(各种你需要修改的参数,不需要修改的参数无需填写),
train_pipeline =[
	dict(type='LoadImageFromFile'),
	dict(type='LoadAnnotations', with_bbox=True 修改的你需要的参数),
	dict(type='AutoAugment'
			policies=[
				[    dict(type='Rotate',
							level=5,
							img_fill_val=(124,116,104),
							prob=0.5,
							scale=1)
				],
				[   dict(type='Rotate',level=7,img_fill_val=(124,116,104)),
				        dict(type='Translate',
				        level=5,
				        prob=0.5,
				        img_fill_val=(124,116,104))
				],
				[    dict(type='Resize',
				               img_scale=[(),(),()],
				               multiscale_mode='value',
				               keep_ratio=True),
				],
				[    dict(type='Resize',
							img_scale=[(),(),()],
							multiscale_mode='value',
				            keep_ratio=True),
				     dict(type='RandomCrop',
				            crop_type='absolute_range',
				            crop_size=(),
				            allow_nagative_crop=True),
				    dict(type='Resize',
				           img_scale=[(),(),()], 
				           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']),
]
test_pipeline = [
    dict(type='LoadImageFromFile'),
    dict(
        type='MultiScaleFlipAug',
        img_scale=(1333, 800),
        flip=False,
        transforms=[
            dict(type='Resize', keep_ratio=True),
            dict(type='RandomFlip'),
            dict(type='Normalize', **img_norm_cfg),
            dict(type='Pad', size_divisor=32),
            dict(type='DefaultFormatBundle'),
            dict(type='Collect', keys=['img']),
        ])
]
data= dict(train=dict(pipeline=train_pipeline))

The figure below shows the configuration of the corresponding dataset file in the corresponding mask_rcnn_swin_tiny_patch4_window7_mstrain_480-800_adamw_1x_coco.py file

 4. Modify the parameters that need to be modified in the training plan file in a fixed way in your own configuration file

# optimizer
optimizer = dict(
    type='SGD',  # 可设为 'SGD', 'Adadelta', 'Adagrad', 'Adam', 'RMSprop' 等
    lr=0.02,
    momentum=0.9,
    weight_decay=0.0001)
optimizer_config = dict(grad_clip=None)
# learning policy
lr_config = dict(
    policy='step',  # 可设为 'step', 'cyclic', 'poly', 'ConsineAnnealing' 等
    warmup='linear',  # 可设为 'constant', 'linear', 'exp', 'None'
    warmup_iters=500,
    warmup_ratio=0.001,
    step=[80, 110, 160, 220, 240, 330])
runner = dict(type='EpochBasedRunner', max_epochs=360)#最大训练代数

The following figure shows the file configuration of the corresponding training plan in the corresponding mask_rcnn_swin_tiny_patch4_window7_mstrain_480-800_adamw_1x_coco.py file

 5. Modify the parameters that need to be modified in the running information file in a fixed way in your own configuration file

checkpoint_config = dict(interval=1)  # epoch
# yapf:disable
log_config = dict(
    interval=50,  # iteration
    hooks=[
        dict(type='TextLoggerHook'),
        # dict(type='TensorboardLoggerHook')
    ])
# yapf:enable
custom_hooks = [dict(type='NumClassCheckHook')]

dist_params = dict(backend='nccl')
log_level = 'INFO'
load_from = None #'/media/wkp/Data/zuoxinhao/Swin-Transformer-Object-Detection-master/mask_rcnn_swin_tiny_patch4_window7_1x.pth'
resume_from = None
workflow = [('train', 1)]  # 也可设为 [('train', 1), ('val', 1)]

The figure below shows the file configuration of the corresponding running information in the corresponding mask_rcnn_swin_tiny_patch4_window7_mstrain_480-800_adamw_1x_coco.py file

6. Summary 

First, create your own configuration file me.py ; second, import the model files , data set files , training plan files , and running information files
you need ; then, start from the model file to see if you need to change some parameters, If you need to change, write the me.py file according to the corresponding matching of the imported model file , and write your own parameters; after completing the parameter configuration of your own model file , check the data set file to see if some parameters need to be changed. If you need to change, then Write the me.py file according to the corresponding matching of the imported data set file, and write your own parameters; after completing the configuration of your own data set parameters , check the training plan file to see if some parameters need to be changed. If you need to change, follow the imported The training plan file matches and writes into the me.py file, and writes its own parameters; after completing the configuration of its own training plan parameters , check the running information file to see if some parameters need to be changed, and if necessary, follow the imported running information file Write the corresponding matching into the me.py file, and write your own parameters; so far, the configuration file me.py of your own MMDetection is completed, and then you can call your own me.py file for model training and testing.

The following is the configuration file of the entire mask_rcnn_swin_tiny_patch4_window7_mstrain_480-800_adamw_1x_coco.py

_base_ = [
    '../_base_/models/mask_rcnn_swin_fpn.py',
    '../_base_/datasets/coco_detection.py',
    '../_base_/schedules/schedule_1x.py', '../_base_/default_runtime.py'
]

model = dict(
    backbone=dict(
        embed_dim=96,
        depths=[2, 2, 6, 2],
        num_heads=[3, 6, 12, 24],
        window_size=7,
        ape=False,
        drop_path_rate=0.1,
        patch_norm=True,
        use_checkpoint=False
    ),
    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=False),
    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']),
]
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(step=[8, 11])
runner = dict(type='EpochBasedRunnerAmp', max_epochs=12)

# do not use mmdet version fp16
fp16 = None
optimizer_config = dict(
    type="DistOptimizerHook",
    update_interval=1,
    grad_clip=None,
    coalesce=True,
    bucket_size_mb=-1,
    use_fp16=True,
)

Guess you like

Origin blog.csdn.net/weixin_42715977/article/details/129997937