修改配置文件并训练
天天学Java,好久没跑过实验了,记录下,防止日后踩坑
-
将
mmdet.models.utils.se_layer.SELayer
注册为plugin
注意修改下参数channels为in_channels,否则解析参数会对不上,出现unexpected args channels的参数解析错误
# Copyright (c) OpenMMLab. All rights reserved.
import mmcv
import torch.nn as nn
from mmcv.cnn import ConvModule
from mmcv.runner import BaseModule
from mmcv.cnn import PLUGIN_LAYERS
@PLUGIN_LAYERS.register_module()
class SELayer(BaseModule):
def __init__(self,
in_channels, # 对应resnet.py中的参数设置修改下
ratio=16,
conv_cfg=None,
act_cfg=(dict(type='ReLU'), dict(type='Sigmoid')),
init_cfg=None):
super(SELayer, self).__init__(init_cfg)
-
修改SSD配置文件,并训练
此处我是参照
ssdlite_mobilenetv2_scratch_600e_coco.py
进行修改的
_base_ = [
'../_base_/datasets/coco_detection.py', '../_base_/default_runtime.py'
]
model = dict(
type='SingleStageDetector',
backbone=dict(
type='ResNet',
depth=50,
num_stages=4,
# 哪几个stage的结果作为输出,从0开始
out_indices=(1, 2, 3),
frozen_stages=1,
norm_cfg=dict(type='BN', requires_grad=True),
norm_eval=True,
style='pytorch',
# 按照自己需求配置plugin
plugins=[dict(cfg=dict(type='SELayer'),stages=(True, True, True, True),position='after_conv3'),],
# 使用预训练权重
init_cfg=dict(type='Pretrained', checkpoint='torchvision://resnet50')),
# norm_cfg=dict(type='BN', eps=0.001, momentum=0.03),
# init_cfg=dict(type='TruncNormal', layer='Conv2d', std=0.03)),
neck=dict(
type='SSDNeck',
# 对应backbone的输出,默认resnet最后三个stage out_channels=[512, 1024, 2048]
in_channels=(512, 1024, 2048),
# 对应SSD的6层输出,前三层为什么这么设置,见补充说明SSD-NECK结构
out_channels=(512, 1024, 2048, 256, 256, 256),
# 小坑:这里设置注意下,如果没设置对可能出现得到的先验框数不匹配(tensor.shape不匹配)
level_strides=(2, 2, 2),
level_paddings=(1, 1, 0),
l2_norm_scale=None,
use_depthwise=True,
norm_cfg=dict(type='BN', eps=0.001, momentum=0.03),
act_cfg=dict(type='ReLU6'),
init_cfg=dict(type='TruncNormal', layer='Conv2d', std=0.03)),
bbox_head=dict(
type='SSDHead',
in_channels=(512, 1024, 2048, 256, 256, 256),
# 按照自己的数据集配置
num_classes=1,
use_depthwise=True,
norm_cfg=dict(type='BN', eps=0.001, momentum=0.03),
act_cfg=dict(type='ReLU6'),
init_cfg=dict(type='Normal', layer='Conv2d', std=0.001),
# 默认的SSD300 检测框设置
anchor_generator=dict(
type='SSDAnchorGenerator',
scale_major=False,
strides=[8, 16, 32, 64, 100, 300],
ratios=[[2], [2, 3], [2, 3], [2, 3], [2], [2]]),
bbox_coder=dict(
type='DeltaXYWHBBoxCoder',
target_means=[.0, .0, .0, .0],
target_stds=[0.1, 0.1, 0.2, 0.2])),
# model training and testing settings
train_cfg=dict(
assigner=dict(
type='MaxIoUAssigner',
pos_iou_thr=0.5,
neg_iou_thr=0.5,
min_pos_iou=0.,
ignore_iof_thr=-1,
gt_max_assign_all=False),
smoothl1_beta=1.,
allowed_border=-1,
pos_weight=-1,
neg_pos_ratio=3,
debug=False),
test_cfg=dict(
nms_pre=1000,
nms=dict(type='nms', iou_threshold=0.45),
min_bbox_size=0,
score_thr=0.02,
max_per_img=200))
cudnn_benchmark = True
# 以下和ssdlite_mobilenetv2_scratch_600e_coco.py设置差不多
dataset_type = 'CocoDataset'
data_root = '/home/COCO/'
img_norm_cfg = dict(mean=[123.675, 116.28, 103.53], std=[1, 1, 1], to_rgb=True)
train_pipeline = [
dict(type='LoadImageFromFile', to_float32=True),
dict(type='LoadAnnotations', with_bbox=True),
dict(
type='PhotoMetricDistortion',
brightness_delta=32,
contrast_range=(0.5, 1.5),
saturation_range=(0.5, 1.5),
hue_delta=18),
dict(
type='Expand',
mean=img_norm_cfg['mean'],
to_rgb=img_norm_cfg['to_rgb'],
ratio_range=(1, 4)),
dict(
type='MinIoURandomCrop',
min_ious=(0.1, 0.3, 0.5, 0.7, 0.9),
min_crop_size=0.3),
dict(type='Resize', img_scale=(300, 300), keep_ratio=False),
dict(type='Normalize', **img_norm_cfg),
dict(type='RandomFlip', flip_ratio=0.5),
dict(type='DefaultFormatBundle'),
dict(type='Collect', keys=['img', 'gt_bboxes', 'gt_labels']),
]
test_pipeline = [
dict(type='LoadImageFromFile'),
dict(
type='MultiScaleFlipAug',
img_scale=(300, 300),
flip=False,
transforms=[
dict(type='Resize', keep_ratio=False),
dict(type='Normalize', **img_norm_cfg),
dict(type='ImageToTensor', keys=['img']),
dict(type='Collect', keys=['img']),
])
]
data = dict(
samples_per_gpu=24,
workers_per_gpu=4,
train=dict(
_delete_=True,
type='RepeatDataset',
times=5,
dataset=dict(
type=dataset_type,
ann_file=data_root + 'annotations/train2017.json',
img_prefix=data_root + 'images/',
pipeline=train_pipeline)),
val=dict(pipeline=test_pipeline),
test=dict(pipeline=test_pipeline))
# optimizer这里根据自己的实际情况设置,可以参照mmd官方文档学习率设置规则
optimizer = dict(type='SGD', lr=0.002, momentum=0.9, weight_decay=5.0e-4)
optimizer_config = dict(grad_clip=None)
# learning policy
lr_config = dict(
policy='CosineAnnealing',
warmup='linear',
warmup_iters=500,
warmup_ratio=0.001,
min_lr=0)
runner = dict(type='EpochBasedRunner', max_epochs=60)
# Avoid evaluation and saving weights too frequently
evaluation = dict(interval=5, metric='bbox')
checkpoint_config = dict(interval=5)
custom_hooks = [
dict(type='NumClassCheckHook'),
dict(type='CheckInvalidLossHook', interval=50, priority='VERY_LOW')
]
-
实验收获与注意事项
a. 实际训练中从0开始训练,加入SE好像反而map轻微掉点了,根据官方issue,似乎backbone需要通过预训练权重初始化才能有相对好的效果,不明白为什么
b. 在采用cos学习率学习之后还要再用小学习率finetune下
补充说明
-
mmd resnet中plugin可以设置的参数
参照ResNet源码的注释
def make_stage_plugins(self, plugins, stage_idx):
"""
Examples:
>>> plugins=[
... dict(cfg=dict(type='xxx', arg1='xxx'),
... stages=(False, True, True, True),
... position='after_conv2'),
... dict(cfg=dict(type='yyy'),
... stages=(True, True, True, True),
... position='after_conv3'),
... dict(cfg=dict(type='zzz', postfix='1'),
... stages=(True, True, True, True),
... position='after_conv3'),
... dict(cfg=dict(type='zzz', postfix='2'),
... stages=(True, True, True, True),
... position='after_conv3')
... ]
>>> self = ResNet(depth=18)
>>> stage_plugins = self.make_stage_plugins(plugins, 0)
>>> assert len(stage_plugins) == 3
"""
plugins的配置的字典分别需要配置:
1)cfg, 例如以下我自己编写的SELayer按以下配置会解析参数in_channels, map_size,在第一个stage的conv3后添加
dict(cfg=dict(type='SELayer', map_size='150'),stages=(True, False, False, False),position='after_conv3')
2)stages(分别作用ResNet的哪几个stage),
3)position(作用于conv1/2/3之后)
-
ssd_neck
参照MMD官方的知乎文章,SSD的项目代码重构了,通过SSD-NECK取代了之前一直在用的ssd_vgg,详细结构见:
MMD官方文章传送门
版权声明:本文为GiantYum原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。