mmdetection 詳細解讀:https://blog.csdn.net/syysyf99/article/details/96574325
簡介
faster_rcnn_r50_fpn_1x.py配置文件
cascade_rcnn_r50_fpn_1x.py配置文件
一、簡介
在使用mmdetection對模型進行調優的過程中總會遇到很多參數的問題,不知道參數在代碼中是什么作用,會對訓練產生怎樣的影響,這里我以faster_rcnn_r50_fpn_1x.py和cascade_rcnn_r50_fpn_1x.py為例,簡單介紹一下mmdetection中的各項參數含義
二、faster_rcnn_r50_fpn_1x.py配置文件
首先介紹一下這個配置文件所描述的框架,它是基於resnet50的backbone,有着5個fpn特征層的faster-RCNN目標檢測網絡,訓練迭代次數為標准的12次epoch,下面逐條解釋其含義
# model settings model = dict( type='FasterRCNN', # model類型 pretrained='modelzoo://resnet50', # 預訓練模型:imagenet-resnet50 backbone=dict( type='ResNet', # backbone類型 depth=50, # 網絡層數 num_stages=4, # resnet的stage數量 out_indices=(0, 1, 2, 3), # 輸出的stage的序號 frozen_stages=1, # 凍結的stage數量,即該stage不更新參數,-1表示所有的stage都更新參數 style='pytorch'), # 網絡風格:如果設置pytorch,則stride為2的層是conv3x3的卷積層;如果設置caffe,則stride為2的層是第一個conv1x1的卷積層 neck=dict( type='FPN', # neck類型 in_channels=[256, 512, 1024, 2048], # 輸入的各個stage的通道數 out_channels=256, # 輸出的特征層的通道數 num_outs=5), # 輸出的特征層的數量 rpn_head=dict( type='RPNHead', # RPN網絡類型 in_channels=256, # RPN網絡的輸入通道數 feat_channels=256, # 特征層的通道數 anchor_scales=[8], # 生成的anchor的baselen,baselen = sqrt(w*h),w和h為anchor的寬和高 anchor_ratios=[0.5, 1.0, 2.0], # anchor的寬高比 anchor_strides=[4, 8, 16, 32, 64], # 在每個特征層上的anchor的步長(對應於原圖) target_means=[.0, .0, .0, .0], # 均值 target_stds=[1.0, 1.0, 1.0, 1.0], # 方差 use_sigmoid_cls=True), # 是否使用sigmoid來進行分類,如果False則使用softmax來分類 bbox_roi_extractor=dict( type='SingleRoIExtractor', # RoIExtractor類型 roi_layer=dict(type='RoIAlign', out_size=7, sample_num=2), # ROI具體參數:ROI類型為ROIalign,輸出尺寸為7,sample數為2 out_channels=256, # 輸出通道數 featmap_strides=[4, 8, 16, 32]), # 特征圖的步長 bbox_head=dict( type='SharedFCBBoxHead', # 全連接層類型 num_fcs=2, # 全連接層數量 in_channels=256, # 輸入通道數 fc_out_channels=1024, # 輸出通道數 roi_feat_size=7, # ROI特征層尺寸 num_classes=81, # 分類器的類別數量+1,+1是因為多了一個背景的類別 target_means=[0., 0., 0., 0.], # 均值 target_stds=[0.1, 0.1, 0.2, 0.2], # 方差 reg_class_agnostic=False)) # 是否采用class_agnostic的方式來預測,class_agnostic表示輸出bbox時只考慮其是否為前景,后續分類的時候再根據該bbox在網絡中的類別得分來分類,也就是說一個框可以對應多個類別 # model training and testing settings train_cfg = dict( rpn=dict( assigner=dict( type='MaxIoUAssigner', # RPN網絡的正負樣本划分 pos_iou_thr=0.7, # 正樣本的iou閾值 neg_iou_thr=0.3, # 負樣本的iou閾值 min_pos_iou=0.3, # 正樣本的iou最小值。如果assign給ground truth的anchors中最大的IOU低於0.3,則忽略所有的anchors,否則保留最大IOU的anchor ignore_iof_thr=-1), # 忽略bbox的閾值,當ground truth中包含需要忽略的bbox時使用,-1表示不忽略 sampler=dict( type='RandomSampler', # 正負樣本提取器類型 num=256, # 需提取的正負樣本數量 pos_fraction=0.5, # 正樣本比例 neg_pos_ub=-1, # 最大負樣本比例,大於該比例的負樣本忽略,-1表示不忽略 add_gt_as_proposals=False), # 把ground truth加入proposal作為正樣本 allowed_border=0, # 允許在bbox周圍外擴一定的像素 pos_weight=-1, # 正樣本權重,-1表示不改變原始的權重 smoothl1_beta=1 / 9.0, # 平滑L1系數 debug=False), # debug模式 rcnn=dict( assigner=dict( type='MaxIoUAssigner', # RCNN網絡正負樣本划分 pos_iou_thr=0.5, # 正樣本的iou閾值 neg_iou_thr=0.5, # 負樣本的iou閾值 min_pos_iou=0.5, # 正樣本的iou最小值。如果assign給ground truth的anchors中最大的IOU低於0.3,則忽略所有的anchors,否則保留最大IOU的anchor ignore_iof_thr=-1), # 忽略bbox的閾值,當ground truth中包含需要忽略的bbox時使用,-1表示不忽略 sampler=dict( type='RandomSampler', # 正負樣本提取器類型 num=512, # 需提取的正負樣本數量 pos_fraction=0.25, # 正樣本比例 neg_pos_ub=-1, # 最大負樣本比例,大於該比例的負樣本忽略,-1表示不忽略 add_gt_as_proposals=True), # 把ground truth加入proposal作為正樣本 pos_weight=-1, # 正樣本權重,-1表示不改變原始的權重 debug=False)) # debug模式 test_cfg = dict( rpn=dict( # 推斷時的RPN參數 nms_across_levels=False, # 在所有的fpn層內做nms nms_pre=2000, # 在nms之前保留的的得分最高的proposal數量 nms_post=2000, # 在nms之后保留的的得分最高的proposal數量 max_num=2000, # 在后處理完成之后保留的proposal數量 nms_thr=0.7, # nms閾值 min_bbox_size=0), # 最小bbox尺寸 rcnn=dict( score_thr=0.05, nms=dict(type='nms', iou_thr=0.5), max_per_img=100) # max_per_img表示最終輸出的det bbox數量 # soft-nms is also supported for rcnn testing # e.g., nms=dict(type='soft_nms', iou_thr=0.5, min_score=0.05) # soft_nms參數 ) # dataset settings dataset_type = 'CocoDataset' # 數據集類型 data_root = 'data/coco/' # 數據集根目錄 img_norm_cfg = dict( mean=[123.675, 116.28, 103.53], std=[58.395, 57.12, 57.375], to_rgb=True) # 輸入圖像初始化,減去均值mean並處以方差std,to_rgb表示將bgr轉為rgb data = dict( imgs_per_gpu=2, # 每個gpu計算的圖像數量 workers_per_gpu=2, # 每個gpu分配的線程數 train=dict( type=dataset_type, # 數據集類型 ann_file=data_root + 'annotations/instances_train2017.json', # 數據集annotation路徑 img_prefix=data_root + 'train2017/', # 數據集的圖片路徑 img_scale=(1333, 800), # 輸入圖像尺寸,最大邊1333,最小邊800 img_norm_cfg=img_norm_cfg, # 圖像初始化參數 size_divisor=32, # 對圖像進行resize時的最小單位,32表示所有的圖像都會被resize成32的倍數 flip_ratio=0.5, # 圖像的隨機左右翻轉的概率 with_mask=False, # 訓練時附帶mask with_crowd=True, # 訓練時附帶difficult的樣本 with_label=True), # 訓練時附帶label val=dict( type=dataset_type, # 同上 ann_file=data_root + 'annotations/instances_val2017.json', # 同上 img_prefix=data_root + 'val2017/', # 同上 img_scale=(1333, 800), # 同上 img_norm_cfg=img_norm_cfg, # 同上 size_divisor=32, # 同上 flip_ratio=0, # 同上 with_mask=False, # 同上 with_crowd=True, # 同上 with_label=True), # 同上 test=dict( type=dataset_type, # 同上 ann_file=data_root + 'annotations/instances_val2017.json', # 同上 img_prefix=data_root + 'val2017/', # 同上 img_scale=(1333, 800), # 同上 img_norm_cfg=img_norm_cfg, # 同上 size_divisor=32, # 同上 flip_ratio=0, # 同上 with_mask=False, # 同上 with_label=False, # 同上 test_mode=True)) # 同上 # optimizer optimizer = dict(type='SGD', lr=0.02, momentum=0.9, weight_decay=0.0001) # 優化參數,lr為學習率,momentum為動量因子,weight_decay為權重衰減因子 optimizer_config = dict(grad_clip=dict(max_norm=35, norm_type=2)) # 梯度均衡參數 # learning policy lr_config = dict( policy='step', # 優化策略 warmup='linear', # 初始的學習率增加的策略,linear為線性增加 warmup_iters=500, # 在初始的500次迭代中學習率逐漸增加 warmup_ratio=1.0 / 3, # 起始的學習率 step=[8, 11]) # 在第8和11個epoch時降低學習率 checkpoint_config = dict(interval=1) # 每1個epoch存儲一次模型 # yapf:disable log_config = dict( interval=50, # 每50個batch輸出一次信息 hooks=[ dict(type='TextLoggerHook'), # 控制台輸出信息的風格 # dict(type='TensorboardLoggerHook') ]) # yapf:enable # runtime settings total_epochs = 12 # 最大epoch數 dist_params = dict(backend='nccl') # 分布式參數 log_level = 'INFO' # 輸出信息的完整度級別 work_dir = './work_dirs/faster_rcnn_r50_fpn_1x' # log文件和模型文件存儲路徑 load_from = None # 加載模型的路徑,None表示從預訓練模型加載 resume_from = None # 恢復訓練模型的路徑 workflow = [('train', 1)] # 當前工作區名稱
三、cascade_rcnn_r50_fpn_1x.py配置文件
cascade-RCNN是cvpr2018的文章,相比於faster-RCNN的改進主要在於其RCNN有三個stage,這三個stage逐級refine檢測的結果,使得結果達到更高的精度。下面逐條解釋其config的含義,與faster-RCNN相同的部分就不再贅述。
# model settings model = dict( type='CascadeRCNN', num_stages=3, # RCNN網絡的stage數量,在faster-RCNN中為1 pretrained='modelzoo://resnet50', backbone=dict( type='ResNet', depth=50, num_stages=4, out_indices=(0, 1, 2, 3), frozen_stages=1, style='pytorch'), neck=dict( type='FPN', in_channels=[256, 512, 1024, 2048], out_channels=256, num_outs=5), rpn_head=dict( type='RPNHead', in_channels=256, feat_channels=256, anchor_scales=[8], anchor_ratios=[0.5, 1.0, 2.0], anchor_strides=[4, 8, 16, 32, 64], target_means=[.0, .0, .0, .0], target_stds=[1.0, 1.0, 1.0, 1.0], use_sigmoid_cls=True), bbox_roi_extractor=dict( type='SingleRoIExtractor', roi_layer=dict(type='RoIAlign', out_size=7, sample_num=2), out_channels=256, featmap_strides=[4, 8, 16, 32]), bbox_head=[ dict( type='SharedFCBBoxHead', num_fcs=2, in_channels=256, fc_out_channels=1024, roi_feat_size=7, num_classes=81, target_means=[0., 0., 0., 0.], target_stds=[0.1, 0.1, 0.2, 0.2], reg_class_agnostic=True), dict( type='SharedFCBBoxHead', num_fcs=2, in_channels=256, fc_out_channels=1024, roi_feat_size=7, num_classes=81, target_means=[0., 0., 0., 0.], target_stds=[0.05, 0.05, 0.1, 0.1], reg_class_agnostic=True), dict( type='SharedFCBBoxHead', num_fcs=2, in_channels=256, fc_out_channels=1024, roi_feat_size=7, num_classes=81, target_means=[0., 0., 0., 0.], target_stds=[0.033, 0.033, 0.067, 0.067], reg_class_agnostic=True) ]) # model training and testing settings train_cfg = dict( rpn=dict( assigner=dict( type='MaxIoUAssigner', pos_iou_thr=0.7, neg_iou_thr=0.3, min_pos_iou=0.3, ignore_iof_thr=-1), 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, smoothl1_beta=1 / 9.0, debug=False), rcnn=[ # 注意,這里有3個RCNN的模塊,對應開頭的那個RCNN的stage數量 dict( assigner=dict( type='MaxIoUAssigner', pos_iou_thr=0.5, neg_iou_thr=0.5, min_pos_iou=0.5, ignore_iof_thr=-1), 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.6, neg_iou_thr=0.6, min_pos_iou=0.6, ignore_iof_thr=-1), 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.7, neg_iou_thr=0.7, min_pos_iou=0.7, ignore_iof_thr=-1), sampler=dict( type='RandomSampler', num=512, pos_fraction=0.25, neg_pos_ub=-1, add_gt_as_proposals=True), pos_weight=-1, debug=False) ], stage_loss_weights=[1, 0.5, 0.25]) # 3個RCNN的stage的loss權重 test_cfg = dict( rpn=dict( nms_across_levels=False, nms_pre=2000, nms_post=2000, max_num=2000, nms_thr=0.7, min_bbox_size=0), rcnn=dict( score_thr=0.05, nms=dict(type='nms', iou_thr=0.5), max_per_img=100), keep_all_stages=False) # 是否保留所有stage的結果 # dataset settings dataset_type = 'CocoDataset' data_root = 'data/coco/' img_norm_cfg = dict( mean=[123.675, 116.28, 103.53], std=[58.395, 57.12, 57.375], to_rgb=True) data = dict( imgs_per_gpu=2, workers_per_gpu=2, train=dict( type=dataset_type, ann_file=data_root + 'annotations/instances_train2017.json', img_prefix=data_root + 'train2017/', img_scale=(1333, 800), img_norm_cfg=img_norm_cfg, size_divisor=32, flip_ratio=0.5, with_mask=False, with_crowd=True, with_label=True), val=dict( type=dataset_type, ann_file=data_root + 'annotations/instances_val2017.json', img_prefix=data_root + 'val2017/', img_scale=(1333, 800), img_norm_cfg=img_norm_cfg, size_divisor=32, flip_ratio=0, with_mask=False, with_crowd=True, with_label=True), test=dict( type=dataset_type, ann_file=data_root + 'annotations/instances_val2017.json', img_prefix=data_root + 'val2017/', img_scale=(1333, 800), img_norm_cfg=img_norm_cfg, size_divisor=32, flip_ratio=0, with_mask=False, with_label=False, test_mode=True)) # optimizer optimizer = dict(type='SGD', lr=0.02, momentum=0.9, weight_decay=0.0001) optimizer_config = dict(grad_clip=dict(max_norm=35, norm_type=2)) # learning policy lr_config = dict( policy='step', warmup='linear', warmup_iters=500, warmup_ratio=1.0 / 3, step=[8, 11]) checkpoint_config = dict(interval=1) # yapf:disable log_config = dict( interval=50, hooks=[ dict(type='TextLoggerHook'), # dict(type='TensorboardLoggerHook') ]) # yapf:enable # runtime settings total_epochs = 12 dist_params = dict(backend='nccl') log_level = 'INFO' work_dir = './work_dirs/cascade_rcnn_r50_fpn_1x' load_from = None resume_from = None workflow = [('train', 1)]
原文鏈接:https://blog.csdn.net/hajlyx/article/details/85991400