mask-rcnn解析(二) anchors生成

anchors个数

基于FPN的特征图[P2,P3,P4,P5,P6],介绍下MASKRCNN网络中Anchor锚框的生成,根据源码中介绍的规则,遍历P2到P6这五个特征层,以每个特征图上的每个像素点都生成Anchor锚框,每个像素点生成的锚框根据框的面积相同,设置不同的长宽比率RATIO=[0.5,1,2]完成了三种变换,因此每个像素点可生成3个框

featureshape(h,w,c)anchhors numfeature_strideRPN_ANCHOR_SCALES
p2256* 256* 256256* 256* 3 =196608432
p3128* 128* 256128* 128* 3 =49152864
p464* 64* 25664* 64* 3 =1228816128
p532* 32* 25632* 32* 3 =307232256
p616 * 16 * 25616 * 16 * 3 =76864512
总计261888

anchors生成

这部分在源代码里是有一个专门的函数:generate_anchors()

            anchors = self.get_anchors(config.IMAGE_SHAPE)
            #config.IMAGE_SHAPE = (1024,1024,3)原图的shape
            # Duplicate across the batch dimension because Keras requires it
            # TODO: can this be optimized to avoid duplicating the anchors?
            #广播(坐标)-》(batch_size,坐标)
            anchors = np.broadcast_to(anchors, (config.BATCH_SIZE,) + anchors.shape)
 a = utils.generate_pyramid_anchors(
                self.config.RPN_ANCHOR_SCALES,#(32, 64, 128, 256, 512)
                self.config.RPN_ANCHOR_RATIOS,#[0.5, 1, 2]
                backbone_shapes,
                self.config.BACKBONE_STRIDES,#[4, 8, 16, 32, 64]
                self.config.RPN_ANCHOR_STRIDE) #  1
# 每一层特征图 生成anchors
anchors = []
    for i in range(len(scales)):
        anchors.append(generate_anchors(scales[i], ratios, feature_shapes[i],
                                        feature_strides[i], anchor_stride))

以p2层特征图为例,查看具体计算流程
scales = 32
ratios = [0.5,1,2]
feature_shapes = [256,256]
feature_strides = 4
anchor_stride =1

   print('scales:',scales)
    print('ratios:', ratios)
    print('shape:', shape)
    print('feature_stride:', feature_stride)
    print('anchor_stride:', anchor_stride)
    scales, ratios = np.meshgrid(np.array(scales), np.array(ratios))
    scales = scales.flatten()
    ratios = ratios.flatten()

    print('scales:', scales)
    print('ratios:', ratios)

scales: 32
ratios: [0.5, 1, 2]
shape: [256 256]
feature_stride: 4
anchor_stride: 1
scales: [32 32 32]
ratios: [0.5 1. 2. ]

以scales 为方形边长,进行ratios三种变换时,矩形宽高

    heights = scales / np.sqrt(ratios)
    widths = scales * np.sqrt(ratios)

    print('heights:', heights)
    print('widths:', widths)

heights: [45.254834 32. 22.627417]
widths: [22.627417 32. 45.254834]

   # Enumerate shifts in feature space
    shifts_y = np.arange(0, shape[0], anchor_stride) * feature_stride
    shifts_x = np.arange(0, shape[1], anchor_stride) * feature_stride
    shifts_x, shifts_y = np.meshgrid(shifts_x, shifts_y)
    print(shifts_x)
    print(shifts_y)

[[ 0 4 8 … 1012 1016 1020]
[ 0 4 8 … 1012 1016 1020]
[ 0 4 8 … 1012 1016 1020]

[ 0 4 8 … 1012 1016 1020]
[ 0 4 8 … 1012 1016 1020]
[ 0 4 8 … 1012 1016 1020]]
[[ 0 0 0 … 0 0 0]
[ 4 4 4 … 4 4 4]
[ 8 8 8 … 8 8 8]

[1012 1012 1012 … 1012 1012 1012]
[1016 1016 1016 … 1016 1016 1016]
[1020 1020 1020 … 1020 1020 1020]]

    box_widths, box_centers_x = np.meshgrid(widths, shifts_x)
    box_heights, box_centers_y = np.meshgrid(heights, shifts_y)

    # Reshape to get a list of (y, x) and a list of (h, w)
    box_centers = np.stack(
        [box_centers_y, box_centers_x], axis=2).reshape([-1, 2])
    print(box_centers)
    box_sizes = np.stack([box_heights, box_widths], axis=2).reshape([-1, 2])
    print(box_sizes)

[[ 0 0]
[ 0 0]
[ 0 0]

[1020 1020]
[1020 1020]
[1020 1020]]
[[45.254834 22.627417]
[32. 32. ]
[22.627417 45.254834]

[45.254834 22.627417]
[32. 32. ]
[22.627417 45.254834]]

矩形框左上角和右下角坐标

boxes = np.concatenate([box_centers - 0.5 * box_sizes,
                            box_centers + 0.5 * box_sizes], axis=1)
print(boxes)
print(boxes.shape)

[[ -22.627417 -11.3137085 22.627417 11.3137085]
[ -16. -16. 16. 16. ]
[ -11.3137085 -22.627417 11.3137085 22.627417 ]

[ 997.372583 1008.6862915 1042.627417 1031.3137085]
[1004. 1004. 1036. 1036. ]
[1008.6862915 997.372583 1031.3137085 1042.627417 ]]
(196608, 4)

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值