faster rcnn pytorch 复现系列(二):generate_anchors源码解析

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/e01528/article/details/83576796

目录

1. 总函数 generate_anchors

2. 函数分功能写,首先是ratios的实现,其次是scale的实现

3. anchor2WHXY函数+WsHsXsYs2anchors函数[s表示复数]

4.  _ratio_enum(anchor,ratios) 

5 ._scale_enum(anchor,scales)

6. 最终获得的anchors的坐标:


学习到的变成知识点:

函数按照功能分开写,每一个函数只实现一个功能,如果需要叠加,交给循环或者np.vstack

其中ratios函数的功能:一次只实现3种ratios的变化 _ratio_enum

scale的功能:一次只实现3种scale的变化,_scale_enum

要得到3*3anchors,交给stack处理

首先,添加print,然后直接运行py文件,生成anchor结果

大意理解:

由一个anchor得到9个anchors

[0 0 15 15]


[[ -84.  -40.   99.   55.]
 [-176.  -88.  191.  103.]
 [-360. -184.  375.  199.]
 [ -56.  -56.   71.   71.]
 [-120. -120.  135.  135.]
 [-248. -248.  263.  263.]
 [ -36.  -80.   51.   95.]
 [ -80. -168.   95.  183.]
 [-168. -344.  183.  359.]]


1. 总函数 generate_anchors

  输入包括:特征图对应于原图的大小,ratios长宽比,scales放大倍数

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
    print ("base anchors",base_anchor)

    ratio_anchors = _ratio_enum(base_anchor, ratios)
    print ("anchors after ratio",ratio_anchors)
    """
    对ratios后处理得到的每一行,分别进行3次scale变化,循环三次
    """
    anchors = np.vstack([_scale_enum(ratio_anchors[i, :], scales)
                         for i in xrange(ratio_anchors.shape[0])])
    print ("achors after ration and scale",anchors)
    return anchors

2. 函数分功能写,首先是ratios的实现,其次是scale的实现

  其中ratios函数的功能:一次实现3种ratios的变化

  scale的功能:一次实现3中scale的变化,其余交给循环处理x

3. anchor2WHXY函数+WsHsXsYs2anchors函数[s表示复数]

  无论是ratio还是scale的实现,本身的中心点没有变,只是长宽比发生了变化 ,所以需要相互转换。

  下面两个scales和ratios的函数的实现,都是先将anchor2whxy然后WsHsXsYs2anchors 。

def _whctrs(anchor):
    """
    Return width, height, x center, and y center for an anchor (window).
    本身负责的只是一个行向量的转变,x0 y0 x1 y1 to  xc yc w h
    input:anchor [x0,y0,x1,y1]
    """
    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):
    """
    input:xc yc w_after h_after
    out:anchors 注意这里是anchors复数
    method:首先需要将w改成列向量,然后水平拼接
    
             x0  y0 x1 y1
    state1
    state2
    state3
    ...
    """
    #(x_ctr, y_ctr) 7.5  7.5  
    ws = ws[:, np.newaxis]#[[23],[16],[11]]  col 
    hs = hs[:, np.newaxis]#[[12],[16],[22]]
    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

4.  _ratio_enum(anchor,ratios) 

  输入:一个anchor,return都是三个anchor。

def _ratio_enum(anchor, ratios):
    """
    input: 
          anchor[np.array]  [0,0,15,15] 
          ratios[list]   [0.5,1,2]
    output:
          anchors
    method:
          1.x0 y0 x1 y1 to xc yc w h 
          2.compute w_after and h_after
          3.xc yc w h to x0 y0 x1 y1
    """
    
    w, h, x_ctr, y_ctr = _whctrs(anchor)#
    size = w * h   #size:16*16=256
    size_ratios = size / ratios  #256/ratios[0.5,1,2]=[512,256,128]
    #round()方法返回x的四舍五入的数字,sqrt()方法返回数字x的平方根
    ws = np.round(np.sqrt(size_ratios)) #ws:[23 16 11]
    hs = np.round(ws * ratios)    #hs:[12 16 22],ws和hs一一对应。as:23&12
    #给定一组宽高向量,输出各个预测窗口,也就是将(宽,高,中心点横坐标,中心点纵坐标)的形式,转成
    #四个坐标值的形式
    anchors = _mkanchors(ws, hs, x_ctr, y_ctr)  
    return anchors

高比变换之后anchors的坐标

ratio_anchors = _ratio_enum(base_anchor, ratios)
       x0     y0      x1     y1
'''[[ -3.5,   2. ,  18.5,  13. ],
    [  0. ,   0. ,  15. ,  15. ],
    [  2.5,  -3. ,  12.5,  18. ]]'''

5 ._scale_enum(anchor,scales)

 对上一步得到的ratio_anchors中的三种宽高比的anchor,再分别进行三种scale的变换

 输入:一个anchor,return都是三个anchor。

def _scale_enum(anchor, scales):
    """
    输入一个anchr行向量,一次得到3种变化
    """

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

6. 最终获得的anchors的坐标:

anchors = np.vstack([_scale_enum(ratio_anchors[i, :], scales)
                         for i in xrange(ratio_anchors.shape[0])])
'''
[[ -84.  -40.   99.   55.]
 [-176.  -88.  191.  103.]
 [-360. -184.  375.  199.]
 [ -56.  -56.   71.   71.]
 [-120. -120.  135.  135.]
 [-248. -248.  263.  263.]
 [ -36.  -80.   51.   95.]
 [ -80. -168.   95.  183.]
 [-168. -344.  183.  359.]]

 

猜你喜欢

转载自blog.csdn.net/e01528/article/details/83576796
今日推荐