generate_anchors.py

这部分主要是生成anchors,基本思路是首先确定一个base_anchor,然后根据ratios=[0.5,1,2],生成3个anchors,每一个生成的anchor再根据scale=2**np.arange(3,6)生成3个anchor,所以总共生成了9个anchor

1.获得一个anchor的w,h,x_ctr,y_ctr

# Return width, height, x center, and y center for an anchor (window).
def _whctrs(anchor):
    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

2.根据输入的ws,hs,x_ctr,y_ctr四个值计算相应的左上角和右下角的坐标,从而生成对应的anchor

# Given a vector of widths (ws) and heights (hs) around a center
# (x_ctr, y_ctr), output a set of anchors (windows).
def _mkanchors(ws, hs, x_ctr, y_ctr):
    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

3. ratios中存储的是纵横比,首先计算输入的anchor的size,然后用size/ratios,再开平方得到3个变换后的ws的值,再让ws乘以ratios得到hs的值,之后用新的三对ws,hs去生成anchors

# Enumerate a set of anchors for each aspect ratio wrt an anchor.
def _ratio_enum(anchor, ratios):
    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

4.首先对与上一步生成的3个anchor,通过之前的_whctrs函数得到3对的w,h,x_ctr,y_ctr,然后对于每一对的w,h分别scales的一个尺度变换,然后再通过_mkanchors函数生成最终的的anchor,这样输入的每1个anchor都变成了3个,所以最终会生成9个anchor

# Enumerate a set of anchors for each scale wrt an anchor.
def _scale_enum(anchor, scales):
    w, h, x_ctr, y_ctr = _whctrs(anchor)
    ws = w*scales
    hs = h*scales
    anchors = _mkanchors(ws, hs, x_ctr, y_ctr)
    return anchors

5.总的生成函数,初始化一个base_anchor=np.array([1, 1, base_size, base_size]) - 1,然后再分别调用以上的函数,最终生成9个anchors用于后续的过程。

# Generate anchor (reference) windows by enumerating aspect ratios X
# scales wrt a reference (0, 0, 15, 15) window.
def generate_anchors(base_size = 16, ratios = [0.5, 1, 2], scales = 2**np.arange(3,6)):
    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 range(ratio_anchors.shape[0])])
    return anchors

最终gengrate_anchors生成的anchor的尺寸如下:

anchor:
   [[-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]]
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值