Faster RCNN generate_anchors.py

Reference:
[1]https://github.com/rbgirshick/py-faster-rcnn/blob/master/lib/rpn/generate_anchors.py

事实上,在generate anchors的时候有两个步骤
步骤一:保持anchor的面积不变,但是改变长宽的比例。
步骤二:保持长宽的比例不变,但是进行scale的缩放,从而改变比例。
不论哪一步都不会改变anchors的中心点的坐标。在这个例子中都是ctr = (7.5,7.5)。
在本程序产生的9个anchors中,高宽比ratio分别是0.5,1,2,width和height都会改变,但是ctrs的坐标不变。

ratio = 0.521739130435
(184.0, 96.0, 7.5, 7.5)
ratio = 0.521739130435
(368.0, 192.0, 7.5, 7.5)
ratio = 0.521739130435
(736.0, 384.0, 7.5, 7.5)
ratio = 1.0
(128.0, 128.0, 7.5, 7.5)
ratio = 1.0
(256.0, 256.0, 7.5, 7.5)
ratio = 1.0
(512.0, 512.0, 7.5, 7.5)
ratio = 2.0
(88.0, 176.0, 7.5, 7.5)
ratio = 2.0
(176.0, 352.0, 7.5, 7.5)
ratio = 2.0
(352.0, 704.0, 7.5, 7.5)

源程序相对来说比较简单,generate_anchors控制程序产生9个不同scale和ratio的anchors,初始的框是(0,0,15,15),之后根据_ratio_enum生成三个中心坐标不变,但是ratio改变的anchors,三个不同ratio的anchors再分别_scale_enum生成不同尺寸的anchors,最后产生三种尺度三种比例的9个anchors。

_whctrs(anchors)的作用是找出一个anchor的宽高和中心坐标
_mkanchors是根据宽高和中心坐标,生成一个新的anchor

# --------------------------------------------------------
# Faster R-CNN
# Copyright (c) 2015 Microsoft
# Licensed under The MIT License [see LICENSE for details]
# Written by Ross Girshick and Sean Bell
# --------------------------------------------------------

import numpy as np

# Verify that we compute the same anchors as Shaoqing's matlab implementation:
#
#    >> load output/rpn_cachedir/faster_rcnn_VOC2007_ZF_stage1_rpn/anchors.mat
#    >> anchors
#
#    anchors =
#
#       -83   -39   100    56
#      -175   -87   192   104
#      -359  -183   376   200
#       -55   -55    72    72
#      -119  -119   136   136
#      -247  -247   264   264
#       -35   -79    52    96
#       -79  -167    96   184
#      -167  -343   184   360

#array([[ -83.,  -39.,  100.,   56.],
#       [-175.,  -87.,  192.,  104.],
#       [-359., -183.,  376.,  200.],
#       [ -55.,  -55.,   72.,   72.],
#       [-119., -119.,  136.,  136.],
#       [-247., -247.,  264.,  264.],
#       [ -35.,  -79.,   52.,   96.],
#       [ -79., -167.,   96.,  184.],
#       [-167., -343.,  184.,  360.]])

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

def _whctrs(anchor):
    """
    Return width, height, x center, and y center for an anchor (window).
    """

    w = anchor[2] - anchor[0] + 1
    h = anchor[3] - anchor[1] + 1
    x_ctr = anchor[0] + 0.5 * (w - 1)
    y_ctr = anchor[1] + 0.5 * (h - 1)
    return w, h, x_ctr, y_ctr

def _mkanchors(ws, hs, x_ctr, y_ctr):
    """
    Given a vector of widths (ws) and heights (hs) around a center
    (x_ctr, y_ctr), output a set of anchors (windows).
    """

    ws = ws[:, np.newaxis]
    hs = hs[:, np.newaxis]
    anchors = np.hstack((x_ctr - 0.5 * (ws - 1),
                         y_ctr - 0.5 * (hs - 1),
                         x_ctr + 0.5 * (ws - 1),
                         y_ctr + 0.5 * (hs - 1)))
    return anchors

def _ratio_enum(anchor, ratios):
    """
    Enumerate a set of anchors for each aspect ratio wrt an anchor.
    """

    w, h, x_ctr, y_ctr = _whctrs(anchor)
    size = w * h
    size_ratios = size / ratios
    ws = np.round(np.sqrt(size_ratios))
    hs = np.round(ws * ratios)
    anchors = _mkanchors(ws, hs, x_ctr, y_ctr)
    return anchors

def _scale_enum(anchor, scales):
    """
    Enumerate a set of anchors for each scale wrt an anchor.
    """

    w, h, x_ctr, y_ctr = _whctrs(anchor)
    ws = w * scales
    hs = h * scales
    anchors = _mkanchors(ws, hs, x_ctr, y_ctr)
    return anchors

if __name__ == '__main__':
    import time
    t = time.time()
    a = generate_anchors()
    print time.time() - t
    print a
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值