anchors的生成过程(Faster Rcnn)

anchors的生成主要在lib\layer_utils\generate_anchors.py
直接运行该程序可以得到生成的结果

#      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

关于传进来的参数

def generate_anchors(base_size=16, ratios=[0.5, 1, 2],
                     scales=2 ** np.arange(3, 6)):

base_size就是最基础的一个尺寸,所以的尺寸变换都是根据base_size来改变的

base_anchor = np.array([1, 1, base_size, base_size]) - 1  # [0,0,15,15]

通过上述代码,得到基础的anchor[0,0,15,15]
ratios就是我们3个宽高比1:2,1:1,2:1
scales就是宽高需要扩大的倍数,对应着**[8,16,32]**

接下来是anchors生成

首先根据base_anchor得到三种比例的anchors

ratio_anchors = _ratio_enum(base_anchor, ratios)  # 枚举各种宽高比

传进的参数base_anchor是xmin,ymin,xmax,ymax的形式,先通过_whctrs()函数把xyxy改为xywh形式

w, h, x_ctr, y_ctr = _whctrs(anchor)  # _whctrs返回一个anchor的中心点(x,y)和w,h
size = w * h 
size_ratios = size / ratios

计算出原来的面积,然后依据ratios对面积进行缩放或者扩大,然后获得新的w和h

ws = np.round(np.sqrt(size_ratios))  # [23,16,11]
hs = np.round(ws * ratios)  # [12,16,22]

在通过_mkanchors()函数把xywh改为xyxy形式,这样对于比例的改变就完成了。

再根据scales对anchors进行三种变换

对_ratio_enum()得到变换后的anchors全部和scales进行尺寸转换。

anchors = np.vstack([_scale_enum(ratio_anchors[i, :], scales)  # 对已经进行宽高比改变的,在进行3种尺度改变
                         for i in range(ratio_anchors.shape[0])])

第一步还是通过_whctrs()先把xyxy改变为xywh形式

w, h, x_ctr, y_ctr = _whctrs(anchor)  # _whctrs返回一个anchor的中心点(x,y)和w,h

接下来的变换是对w和h进行改变,w和h都乘上扩大的尺寸scales[8,16,32]

ws = w * scales
hs = h * scales

最后通过_mkanchors转为xyxy坐标。

anchors = _mkanchors(ws, hs, x_ctr, y_ctr)

这样对于三种比例的三种尺寸变换就结束了。可以得到9种不同的anchor

来举个例子:

我们假设传进来的参数ratios=[0.5],scales=[8,16],一共就两种变幻
首先得到base_anchors=[0,0,15,15] # xyxy形式
通过_ratio_enum枚举宽高比,这里只有0.5也就是1:2

def _ratio_enum(anchor, ratios):
    """
    Enumerate a set of anchors for each aspect ratio wrt an anchor.
    """
    # 列举关于一个anchor的三种宽高比 1:2,1:1,2:1
    w, h, x_ctr, y_ctr = _whctrs(anchor)  # _whctrs返回一个anchor的中心点(x,y)和w,h
    size = w * h  # 16*16
    size_ratios = size / ratios  # 16*16=256/[0.5,1,2]=[512,256,128]
    # round()方法返回x的四舍五入的数字,sqrt()方法返回数字x的平方根
    ws = np.round(np.sqrt(size_ratios))  # [23,16,11]
    hs = np.round(ws * ratios)  # [12,16,22]
    # 就是将中心(x,y),w,h转换为左上和右下坐标
    anchors = _mkanchors(ws, hs, x_ctr, y_ctr)  # 输出各个预测窗口
    return anchors

_ratio_enum传进来的参数anchor=[0,0,15,15],ratios=[0.5]
通过_whctrs得到anchor的坐标为:[7.5,7.5,16,16]
size=16 * 16
size_ratios =16 * 16 / 0.5=512
得到一个新的ws和hs
ws=23
hs=12
再通过_mkanchors转化为xyxy形式。
这趟bsae_anchor通过ratios=[0.5]转变为[-3.5 2. 18.5 13. ]
这样宽高比的改变就结束了。

接下来是通过_scale_enum对尺寸的改变scales=[8,16]

def _scale_enum(anchor, scales):
    """
    Enumerate a set of anchors for each scale wrt an anchor.
    """
    # 列举关于一个anchor的三种尺度 128*128,256*256,512*512
    # 3种尺度,3种宽高比,所有9种
    w, h, x_ctr, y_ctr = _whctrs(anchor)  # _whctrs返回一个anchor的中心点(x,y)和w,h
    print("asdasdasd")
    print(w, h, x_ctr, y_ctr)
    print("asdasdasd")
    ws = w * scales
    hs = h * scales
    anchors = _mkanchors(ws, hs, x_ctr, y_ctr)
    print('--------------------------')
    print(scales)
    print(anchors)
    print('--------------------------')
    return anchors

尺寸的改变比较简单,获取w和h之后,将w、h乘上对应的scales得到新的w和h,然后保持中心坐标不变,再改为xyxy形式即可。
传进来的参数anchor=[-3.5 2. 18.5 13. ],scales=[8.,16]

 w, h, x_ctr, y_ctr = _whctrs(anchor) 

w,h,x_ctr,y_ctr=23.0 , 12.0 , 7.5 , 7.5
ws=23 * 8,23 * 16
wh=12 * 8,12 * 16
再根据_mkanchors就得到了最终的anchors

实在没找到什么好的画坐标图的软件,本想着用图演示的,将就着看看

TIP:
更新一哈
faster R-CNN中anchors 的生成过程(generate_anchors源码解析)
搜到一个带图易懂的博主博客

猜你喜欢

转载自blog.csdn.net/qq_33193309/article/details/99578240
今日推荐