改变anchor数量或尺寸(Faster R-CNN或RFCN)

anchor通常设置为3ratio*3scales=9anchors,但实际使用时可能需要进行调整。改变anchor数量或尺寸。

先看Faster R-CNN源码产生anchors的部分,位置$Faster RCNN/lib/rpn/generate_anchors.py:

def generate_anchors(base_size=16, ratios=[0.5, 1, 2],
                     scales=2**np.arange(3, 6)):
    """
    Generate anchor (reference) windows by enumerating aspect ratios X
    scales wrt a reference (0, 0, 15, 15) window.
    """

    base_anchor = np.array([1, 1, base_size, base_size]) - 1
    ratio_anchors = _ratio_enum(base_anchor, ratios)
    anchors = np.vstack([_scale_enum(ratio_anchors[i, :], scales)
                         for i in xrange(ratio_anchors.shape[0])])
    return anchors

这个函数三个参数base_size, ratios 和scales,且都有默认值。base_size要和scales集合起来看,anchor的基础尺度=base_size*scales,例如这里默认base_size=16, scales=(8,16,32),那么三个基本尺度分别是128,256和512。然后在这三个基本尺度上才有3个ratio。

再来看看哪些地方用到了这个generate_anchors函数,分别是:proposal_layer.py, anchor_target_layer.py和generate.py。其中generate.py用到generate_anchors的是RFCN ,Faster RCNN是没有的。

一,不改变anchor数量

如果不需要改变anchor数量,而只是简单改变scales和ratios,直接改generate_anchors函数的默认值就好,比如把base_size改为8或者把scales该为2**np.arrage(2,5),都可以把基础尺度减少为64, 128, 256。ratios也可以根据需要这样改。

此外,proposal_layer.py, anchor_target_layer.py和generate.py这三个调用了generate_anchor的地方也要相应改一下:

anchor_scales =layer_params.get('scales', (8, 16, 32))
self._anchors = generate_anchors(scales=np.array(anchor_scales))

直接改成:

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

generate_anchors函数用默认参数就好。

二,改变anchor数量

改变anchor数量的方式可以是直接增减anchor的基础尺度数量或者ratios数量,或者直接用YOLOv2那样聚类anchor,只要改变了anchor数量,就要去修改网络pt文件。以RFCN为例,将test, train的pt文件都要改:

layer {
  name: "rpn_cls_score"
  type: "Convolution"
  bottom: "rpn/output"
  top: "rpn_cls_score"
  param { lr_mult: 1.0 decay_mult: 1.0 }
  param { lr_mult: 2.0 decay_mult: 0 }
  convolution_param {
    num_output: 24   # 2(bg/fg) * 12(anchors)
    kernel_size: 1 pad: 0 stride: 1
    weight_filler { type: "gaussian" std: 0.01 }
    bias_filler { type: "constant" value: 0 }
  }
}
layer {
  name: "rpn_bbox_pred"
  type: "Convolution"
  bottom: "rpn/output"
  top: "rpn_bbox_pred"
  param { lr_mult: 1.0 decay_mult: 1.0 }
  param { lr_mult: 2.0 decay_mult: 0 }
  convolution_param {
    num_output: 48   # 4 * 12(anchors)
    kernel_size: 1 pad: 0 stride: 1
    weight_filler { type: "gaussian" std: 0.01 }
    bias_filler { type: "constant" value: 0 }
  }
}
layer {
  name: 'rpn_cls_prob_reshape'
  type: 'Reshape'
  bottom: 'rpn_cls_prob'
  top: 'rpn_cls_prob_reshape'
  reshape_param { shape { dim: 0 dim: 24 dim: -1 dim: 0 } }
}

猜你喜欢

转载自blog.csdn.net/hongxingabc/article/details/80001186