Faster rcnn–改变anchor的size
- 代码用的是py-faster-rcnn:https://github.com/rbgirshick/py-faster-rcnn
- 训练时用的模型和方法:models\pascal_voc\VGG_CNN_M_1024\faster_rcnn_end2end
修改generate_anchors.py
#ratios中加了一种比例1.5,所以anchor的数量为4*3=12
def generate_anchors(base_size=16, ratios=[0.5, 1, 1.5, 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
修改train.prototxt
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: 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 }
param { lr_mult: 2.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 } }
}
修改test.prototxt
同train.prototxt
上述步骤只针对改ratios,如果你想改scales,还要进行以下修改
修改generate_anchors.py
#anchor的数量为4*5=20
def generate_anchors(base_size=16, ratios=[0.5, 1, 1.5, 2],
scales=2**np.arange(1, 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
修改anchor_target_layer.py
def setup(self, bottom, top):
layer_params = yaml.load(self.param_str)
anchor_scales = layer_params.get('scales', (2, 4, 8, 16, 32))
self._anchors = generate_anchors(scales=np.array(anchor_scales))
self._num_anchors = self._anchors.shape[0]
self._feat_stride = layer_params['feat_stride']
修改proposal_layer.py
def setup(self, bottom, top):
# parse the layer parameter string, which must be valid YAML
layer_params = yaml.load(self.param_str)
self._feat_stride = layer_params['feat_stride']
anchor_scales = layer_params.get('scales', (2, 4, 8, 16, 32))
self._anchors = generate_anchors(scales=np.array(anchor_scales))
self._num_anchors = self._anchors.shape[0]
如果报错,删除faster_rcnn_version\fasterRcnn_final_v1_3\data\cache下.pkl文件