基于caffe的Faster RCNN怎样修改anchor尺寸与大小

对于特定大小的目标检测,修改anchor大小与比例也许会有精确度的提高。
0.源目录/lib/rpn/generate_anchors.py文件
37行

def generate_anchors(base_size=16, ratios=[0.5, 1, 2],
                     scales=2**np.arange(3, 6)):

改为(我需要对应原图上的识别框不需要缩放比例,为了能更好的进行线性回归,设置大小为16*16、30*30、35*35、40*40、45*45、50*50、55*55、60*60、65*65、70*70十个尺寸):

def generate_anchors(base_size=1, ratios=[1], scales=1*np.array([16, 30, 35, 40, 45, 50, 55, 60,  65, 70])):

1.源目录/lib/rpn/anchor_target_layer.py文件
大概在29行

anchor_scales = layer_params.get('scales', (8, 16, 32))

改为

anchor_scales = layer_params.get('scales', (16, 30, 35, 40, 45, 50, 55, 60,  65, 70))

2.源目录/lib/rpn/anchor_target_layer.py文件
大概在29行,

anchor_scales = layer_params.get('scales', (8, 16, 32))

改为

anchor_scales = layer_params.get('scales', (16, 30, 35, 40, 45, 50, 55, 60,  65, 70))

3.源目录/models/pascal_voc/VGG16/faster_rcnn_alt_opt/faster_rcnn_test.pt

layer {
  name: "rpn_cls_score"
  type: "Convolution"
  bottom: "rpn/output"
  top: "rpn_cls_score"
  param { lr_mult: 1.0 }
  param { lr_mult: 2.0 }
  convolution_param {
    num_output:18   # 2(bg/fg) * 9(anchors)
    kernel_size: 1 pad: 0 stride: 1
    weight_filler { type: "gaussian" std: 0.01 }
    bias_filler { type: "constant" value: 0 }
  }
}

这里的num_output是2*anchors的个数,原来是9(3个anchor scale*3个anchor ratio),现在改成对应的数字,我这里anchor的个数是10,所以num_output改成20。

num_output:18   # 2(bg/fg) * 9(anchors)

改成:

num_output:20   # 2(bg/fg) * 9(anchors)

rpn_bbox_pred层:

layer {
  name: "rpn_bbox_pred"
  type: "Convolution"
  bottom: "rpn/output"
  top: "rpn_bbox_pred"
  param { lr_mult: 1.0 }
  param { lr_mult: 2.0 }
  convolution_param {
    num_output: 36   # 4 * 9(anchors)
    kernel_size: 1 pad: 0 stride: 1
    weight_filler { type: "gaussian" std: 0.01 }
    bias_filler { type: "constant" value: 0 }
  }
}

这里的num_output是每个anchor四个角点坐标个数,同样按之前的方法修改:

num_output: 36   # 4 * 9(anchors)

改为:

num_output: 40   # 4 * 9(anchors)

rpn_cls_prob_reshape层:

layer {
  name: 'rpn_cls_prob_reshape'
  type: 'Reshape'
  bottom: 'rpn_cls_prob'
  top: 'rpn_cls_prob_reshape'
  reshape_param { shape { dim: 0 dim: 18 dim: -1 dim: 0 } }
}

这一行:

reshape_param { shape { dim: 0 dim: 18 dim: -1 dim: 0 } }

改为:

reshape_param { shape { dim: 0 dim: 20 dim: -1 dim: 0 } }

训练时,在/models/pascal_voc/VGG16/faster_rcnn_alt_opt/faster_rcnn_test.pt 的其他文件中出现以上三个层的任意一层,都需要进行一样的修改。

猜你喜欢

转载自blog.csdn.net/a_z666666/article/details/80457434