美文网首页
Mask RCNN之mmdetection配置文件解读

Mask RCNN之mmdetection配置文件解读

作者: 霜冷长河wzq | 来源:发表于2021-01-04 20:12 被阅读0次

    根据mmDetection的mask rcnn的配置文件来进行理解。

    网络结构

    model = dict(
        type='MaskRCNN',
        pretrained='torchvision://resnet50',
        backbone=dict(
            type='ResNet',
            depth=50,
            num_stages=4,
            out_indices=(0, 1, 2, 3),  #使用resnet50作为主干网络,/4,/8,/16,/32作为FPN的输入
            frozen_stages=1, #stage 1及其之前层不进行参数更新,-1表示参数都更新
            norm_cfg=dict(type='BN', requires_grad=True),
            style='pytorch'),
        neck=dict(
            type='FPN',
            in_channels=[256, 512, 1024, 2048], 
            out_channels=256, #利用1*1的卷积改变通道数,低分辨率的进行最近邻上采样,然后进行相邻尺度的相加融合,然后通过3*3的卷积进行输出。
            num_outs=5), #此处额外加了一个尺度输出,即在/32尺度利用2*2的最大池化输出\64尺度。一起作为RPN网络的输入。
        rpn_head=dict(
            type='RPNHead',
            in_channels=256,
            feat_channels=256,
            anchor_scales=[8], 
            anchor_ratios=[0.5, 1.0, 2.0], #3个base_anchors,面积一样。其中1.0表示8*8的大小,数值表示长宽比。
            anchor_strides=[4, 8, 16, 32, 64], #对于输入的5个尺度,生成对应的anchor。设有A个anchors (1, A, 4), 有K个shifts (K, 1, 4),
                                                #其中K为特征图的大小W*H,得到(K*A, 4)的坐标值。
                                                #shift_x = torch.arange(0, feat_w, device=device) * stride
                                               #shift_y = torch.arange(0, feat_h, device=device) * stride
                                               #shift_xx, shift_yy = self._meshgrid(shift_x, shift_y)
                                               #shifts = torch.stack([shift_xx, shift_yy, shift_xx, shift_yy], dim=-1)
            target_means=[.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)), #RPN网络有2条支路——分类支路(anchor数目*2,
                                                                      #如果使用sigmoid,不需要乘2 ),和回归支路(anchor数目*4)
        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]), #ROI Align层,对于一个ROI,根据其大小,进行映射,取相应的尺度上的特征图。
                                            #- scale < finest_scale * 2: level 0
                                           #- finest_scale * 2 <= scale < finest_scale * 4: level 1
                                           #- finest_scale * 4 <= scale < finest_scale * 8: level 2
                                           #- scale >= finest_scale * 8: level 3
        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], #预测的dx和dy需要*0.1,dw和dh需要*0.2
            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)), #输入7*7的ROI 特征,进行类别预测和框的回归
        mask_roi_extractor=dict(
            type='SingleRoIExtractor',
            roi_layer=dict(type='RoIAlign', out_size=14, sample_num=2),
            out_channels=256,
            featmap_strides=[4, 8, 16, 32]), #ROI Align层,同上,只是输出大小为14*14
        mask_head=dict(
            type='FCNMaskHead',
            num_convs=4,
            in_channels=256,
            conv_out_channels=256,
            num_classes=81,
            loss_mask=dict(
                type='CrossEntropyLoss', use_mask=True, loss_weight=1.0))) #输入14*14的ROI特征,FCN进行Mask预测
    

    训练

    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),#对于每个box,分配真值框或者背景。遍历每个bbox,
             #与一gt的iou超过pos_iou_thr为正样本,与所有gt的iou低于neg_iou_thr的为负样本。
             #遍历每个gt,如果与其最重合的box的iou大于min_pos_iou,此gt分配给此box。
            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_across_levels=False,
            nms_pre=2000,
            nms_post=2000,
            max_num=2000,
            nms_thr=0.7,
            min_bbox_size=0),#按照置信度选取前nms_pre的前景,进行nms
                            #然后选前nms_post个框,送入bbox分支
        rcnn=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),#同上
            mask_size=28,
            pos_weight=-1,
            debug=False))
    

    推理

    #推理时,rpn网络输出roi,roi特征图输入bbox分支,得到预测框。
    #预测的框所对应的特征图输入到mask分支。
    test_cfg = dict(
        rpn=dict(
            nms_across_levels=False,
            nms_pre=1000,
            nms_post=1000,
            max_num=1000,
            nms_thr=0.7,
            min_bbox_size=0), #1000个roi框送到bbox分支中
        rcnn=dict(
            score_thr=0.05,
            nms=dict(type='nms', iou_thr=0.5),
            max_per_img=100,
            mask_thr_binary=0.5))  #100个bbox分支预测框送入到mask分支中
    

    相关文章

      网友评论

          本文标题:Mask RCNN之mmdetection配置文件解读

          本文链接:https://www.haomeiwen.com/subject/ijnfoktx.html