Faster RCNN 改變anchor數量或尺寸
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(三個檔案都在目錄FRCN_ROOT/lib/rpn/
一,不改變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檔案。以Faster RCNN為例,將目錄FRCN_ROOT/models/pascal_voc/VGG16/faster_rcnn_end2end/
中test.prototxt、 train.prototxt檔案都要改:
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 } }
}