generate_anchor.py运算解读(转)

为自己普及一下生成anchor的函数实现原理,方便看懂原理,追随源码(generate_anchors.py): 
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 xrange(ratio_anchors.shape[0])])
return anchors
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

这个函数就是生成九个anchors的函数,首先有一个base_anchor坐标为[0,0,15,15],因为电脑是从0开始计数的,其实是[1,1,16,16],先调用_ratio_enum 
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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

在这个函数里先调用了_whctrs,作用是得到anchor 的四个参数,宽度w=16,高度h=16,中心点坐标x=7.5,y=7.5,之后做了一系列数学计算,最终结果为ws=[23,16,11], hs=[12,16,22],调用_mkanchors 
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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

在这个函数的里面有个np.newaxis,是增加数据的维数的意思,原来ws,hs均为一组数据(只有一个维度),之后变成一个3行,1列(虽然是1,但是也是个维度)的二维数据,即变成了ws={[23],[16],[11]}, hs={[12],[16],[22]},最后一个函数不想细说,总之就是变成了[■(-3.5&2&18.5@0&0&15@2.5&-3&12.5) ■(13@15@18)],即ratio_anchors,我们在回到最基本的那个函数,接下来又调用了一个函数 
anchors = np.vstack([_scale_enum(ratio_anchors[i, :], scales) 
for i in xrange(ratio_anchors.shape[0])]) 
在这里ratio_anchors.shape[0]指的是3行4列的3,也就是一行一行的输送给_scale_enum函数 
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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

由最开始的那个函数的参数可知,scales为2的3,4,5次方即[8,16,32]。首先,先得到四个参数(w, h, x_ctr, y_ctr)=[■(23&12&7.5@16&16&7.5@11&22&7.5) ■(7.5@7.5@7.5)]所以ws=[184,368,736], [128,256,512],[88,176,352], hs=[96,192,384], [128,256,512], [176,352,704], 这是9组对应的宽和高,其实每次只能得到三组,我是直接把循环三次的结果写了出来,将这9组的数据送到_mkanchors之后得到9个anchors,分别为:

-84.0 -40.0 99 55 
-176.0 -88.0 191 103 
-360.0 -184.0 375 199 
-56.0 -56.0 71 71 
-120.0 -120.0 135 135 
-248.0 -248.0 263 263 
-36.0 -80.0 51 95 
-80.0 -168.0 95 183 
-168.0 -344.0 183 359

转换成我们需要的四个参数分别为: 
ratio = 0.5 
(184.0, 96.0, 7.5, 7.5) 
(368.0, 192.0, 7.5, 7.5) 
(736.0, 384.0, 7.5, 7.5) 
ratio = 1.0 
(128.0, 128.0, 7.5, 7.5) 
(256.0, 256.0, 7.5, 7.5) 
(512.0, 512.0, 7.5, 7.5) 
ratio = 2.0 
(88.0, 176.0, 7.5, 7.5) 
(176.0, 352.0, 7.5, 7.5) 
(352.0, 704.0, 7.5, 7.5)

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值