这几天用mmdetection和mmrotate来做微小物体检测的时候,加了一个Rcopypaste的数据增强的trick,发现无论怎么调参,Loss和grad_norm训着训着就变成nan了,换什么loss都不行。
三天之后理解了。因为船只的尺寸特别小,只有几个像素,所以如果你copypaste的话会丢失很多背景信息,本来船只的识别就很靠背景信息,比如船尾迹之类的,如果你复制粘贴就没有背景信息了,就更难识别了,模型就不稳定了!!!!
所以解决方式:把Rcopypaste换成RMosaic!就正常了!
下面贴一下配置文件:
dataset_type = 'DOTADataset'
data_root = '/home/jovyan/mmrotate/data/split_ss_dota/S2/'
img_norm_cfg = dict(
mean=[123.675, 116.28, 103.53], std=[58.395, 57.12, 57.375], to_rgb=True)
train_pipeline = [
dict(
type='RRandomFlip',
flip_ratio=[0.25, 0.25, 0.25],
direction=['horizontal', 'vertical', 'diagonal'],
version='le90'),
dict(
type='RMosaic',
img_scale=(512, 512),
center_ratio_range=(0.9, 1.1),
min_bbox_size=0,
version='le90',
pad_val=0),
# dict(
# type='RCopyPaste',
# max_num_pasted=200,
# rbbox_occluded_iou_thr=0.3,
# mask_occluded_thr=0,
# selected=True,
# version='le90',
# ),
dict(
type='Normalize',
mean=[123.675, 116.28, 103.53],
std=[58.395, 57.12, 57.375],
to_rgb=True),
dict(type='Pad', size_divisor=32),
dict(type='DefaultFormatBundle'),
dict(type='Collect', keys=['img', 'gt_bboxes', 'gt_labels'])
]
train_dataset = dict(
type='MultiImageMixDataset',
dataset=dict(
type=dataset_type,
ann_file=data_root + 'train/annfiles/',
img_prefix=data_root + 'train/images/',
pipeline=[
dict(type='LoadImageFromFile'),
dict(type='LoadAnnotations', with_bbox=True)
],
version='le90'
),
pipeline=train_pipeline,
max_refetch=1000
)
test_pipeline = [
dict(type='LoadImageFromFile'),
dict(
type='MultiScaleFlipAug',
img_scale=(512, 512),
flip=False,
transforms=[
dict(
type='Normalize',
mean=[123.675, 116.28, 103.53],
std=[58.395, 57.12, 57.375],
to_rgb=True),
dict(type='Pad', size_divisor=32),
dict(type='DefaultFormatBundle'),
dict(type='Collect', keys=['img'])
])
]
data = dict(
samples_per_gpu=4,
workers_per_gpu=4,
train=train_dataset,
val=dict(
type='DOTADataset',
ann_file=data_root+'val/annfiles/',
img_prefix=data_root+'val/images/',
pipeline=test_pipeline,
version='le90'),
test=dict(
type='DOTADataset',
ann_file=data_root+'test/annfiles/',
img_prefix=data_root+'test/images/',
pipeline=test_pipeline,
version='le90'))
evaluation = dict(interval=1, metric='mAP')
optimizer = dict(type='SGD', lr=0.001, momentum=0.9, weight_decay=0.0001)
optimizer_config = dict(grad_clip=dict(max_norm=35, norm_type=2))
lr_config = dict(
policy='step',
warmup='linear',
warmup_iters=500,
warmup_ratio=0.3333333333333333,
step=[1, 6, 11])
runner = dict(type='EpochBasedRunner', max_epochs=100)
checkpoint_config = dict(interval=10)
log_config = dict(interval=50, hooks=[dict(type='TextLoggerHook')])
dist_params = dict(backend='nccl')
log_level = 'INFO'
load_from = './work_dirs/my_config/airbus_epoch_20.pth'
resume_from = None
workflow = [('train', 3)]
opencv_num_threads = 0
mp_start_method = 'fork'
angle_version = 'le90'
model = dict(
type='ReDet',
backbone=dict(
type='ReResNet',
depth=50,
num_stages=4,
out_indices=(0, 1, 2, 3),
frozen_stages=1,
style='pytorch',
pretrained=None),
neck=dict(
type='ReFPN',
in_channels=[256, 512, 1024, 2048],
out_channels=256,
num_outs=5),
rpn_head=dict(
type='RotatedRPNHead',
in_channels=256,
feat_channels=256,
version='le90',
anchor_generator=dict(
type='AnchorGenerator',
scales=[2],
ratios=[0.5, 1.0, 2.0, 4.0],
strides=[4, 8, 16, 32, 64]),
bbox_coder=dict(
type='DeltaXYWHBBoxCoder',
target_means=[0.0, 0.0, 0.0, 0.0],
target_stds=[1.0, 1.0, 1.0, 1.0]),
loss_cls=dict(
type='CrossEntropyLoss', use_sigmoid=True, loss_weight=1.0),
loss_bbox=dict(type='SmoothL1Loss', beta=1.0 / 9.0, loss_weight=1.0)),
roi_head=dict(
type='RoITransRoIHead',
version='le90',
num_stages=2,
stage_loss_weights=[1, 1],
bbox_roi_extractor=[
dict(
type='SingleRoIExtractor',
roi_layer=dict(
type='RoIAlign', output_size=7, sampling_ratio=0),
out_channels=256,
featmap_strides=[4, 8, 16, 32]),
dict(
type='RotatedSingleRoIExtractor',
roi_layer=dict(
type='RiRoIAlignRotated',
out_size=7,
num_samples=2,
num_orientations=8,
clockwise=True),
out_channels=256,
featmap_strides=[4, 8, 16, 32])
],
bbox_head=[
dict(
type='RotatedShared2FCBBoxHead',
in_channels=256,
fc_out_channels=1024,
roi_feat_size=7,
num_classes=1,
bbox_coder=dict(
type='DeltaXYWHAHBBoxCoder',
angle_range='le90',
norm_factor=2,
edge_swap=True,
target_means=[0.0, 0.0, 0.0, 0.0, 0.0],
target_stds=[0.1, 0.1, 0.2, 0.2, 0.1]),
reg_class_agnostic=True,
loss_cls=dict(
type='CrossEntropyLoss',
use_sigmoid=False,
loss_weight=1.0),
loss_bbox=dict(type='SmoothL1Loss', beta=1.0,loss_weight=1.0)),
dict(
type='RotatedShared2FCBBoxHead',
in_channels=256,
fc_out_channels=1024,
roi_feat_size=7,
num_classes=1,
bbox_coder=dict(
type='DeltaXYWHAOBBoxCoder',
angle_range='le90',
norm_factor=None,
edge_swap=True,
proj_xy=True,
target_means=[0.0, 0.0, 0.0, 0.0, 0.0],
target_stds=[0.05, 0.05, 0.1, 0.1, 0.5]),
reg_class_agnostic=False,
loss_cls=dict(
type='CrossEntropyLoss',
use_sigmoid=False,
loss_weight=1.0),
loss_bbox=dict(type='SmoothL1Loss', beta=1.0, loss_weight=1.0))
]),
train_cfg=dict(
rpn=dict(
assigner=dict(
type='MaxIoUAssigner',
pos_iou_thr=0.7,
neg_iou_thr=0.3,
min_pos_iou=0.3,
match_low_quality=True,
ignore_iof_thr=-1,
gpu_assign_thr=200),
sampler=dict(
type='RandomSampler',
num=256,
pos_fraction=0.5,
neg_pos_ub=-1,
add_gt_as_proposals=False),
allowed_border=0,
pos_weight=-1,
debug=False),
rpn_proposal=dict(
nms_pre=2000,
max_per_img=2000,
nms=dict(type='nms', iou_threshold=0.7),
min_bbox_size=0),
rcnn=[
dict(
assigner=dict(
type='MaxIoUAssigner',
pos_iou_thr=0.5,
neg_iou_thr=0.5,
min_pos_iou=0.5,
match_low_quality=False,
ignore_iof_thr=-1,
iou_calculator=dict(type='BboxOverlaps2D')),
sampler=dict(
type='RandomSampler',
num=512,
pos_fraction=0.25,
neg_pos_ub=-1,
add_gt_as_proposals=True),
pos_weight=-1,
debug=False),
dict(
assigner=dict(
type='MaxIoUAssigner',
pos_iou_thr=0.5,
neg_iou_thr=0.5,
min_pos_iou=0.5,
match_low_quality=False,
ignore_iof_thr=-1,
iou_calculator=dict(type='RBboxOverlaps2D')),
sampler=dict(
type='RRandomSampler',
num=512,
pos_fraction=0.25,
neg_pos_ub=-1,
add_gt_as_proposals=True),
pos_weight=-1,
debug=False)
]),
test_cfg=dict(
rpn=dict(
nms_pre=2000,
max_per_img=2000,
nms=dict(type='nms', iou_threshold=0.5),
min_bbox_size=0),
rcnn=dict(
nms_pre=2000,
min_bbox_size=0,
score_thr=0.05,
nms=dict(iou_thr=0.1),
max_per_img=2000)))
classes = ('ship', )
work_dir = './work_dirs/my_config'
auto_resume = False
gpu_ids = range(0, 1)