faster rcnn RPN之anchor(generate_anchors)源碼解析

英文原文:faste rcnn。其中生成RPN(Regional proposal network)的python代碼解析

本代碼主要用於:生成尺度爲:128,256,512; 寬高比爲:1:2,1:1,2:1的anchor

<span style="font-size:24px;">#功能描述:生成多尺度、多寬高比的anchors。
#          尺度爲:128,256,512; 寬高比爲:1:2,1:1,2:1

import numpy as np  #提供矩陣運算功能的庫

#生成anchors總函數:ratios爲一個列表,表示寬高比爲:1:2,1:1,2:1
#2**x表示:2^x,scales:[2^3 2^4 2^5],即:[8 16 32]
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  #新建一個數組:base_anchor:[0 0 15 15]
    ratio_anchors = _ratio_enum(base_anchor, ratios)  #枚舉各種寬高比
    anchors = np.vstack([_scale_enum(ratio_anchors[i, :], scales)  #枚舉各種尺度,vstack:豎向合併數組
                         for i in xrange(ratio_anchors.shape[0])]) #shape[0]:讀取矩陣第一維長度,其值爲3
    return anchors

#用於返回width,height,(x,y)中心座標(對於一個anchor窗口)
def _whctrs(anchor):
    """
    Return width, height, x center, and y center for an anchor (window).
    """
	#anchor:存儲了窗口左上角,右下角的座標
    w = anchor[2] - anchor[0] + 1
    h = anchor[3] - anchor[1] + 1
    x_ctr = anchor[0] + 0.5 * (w - 1)  #anchor中心點座標
    y_ctr = anchor[1] + 0.5 * (h - 1)
    return w, h, x_ctr, y_ctr

#給定一組寬高向量,輸出各個anchor,即預測窗口,**輸出anchor的面積相等,只是寬高比不同**
def _mkanchors(ws, hs, x_ctr, y_ctr):
    #ws:[23 16 11],hs:[12 16 22],ws和hs一一對應。
    """
    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]  #newaxis:將數組轉置
    hs = hs[:, np.newaxis]
    anchors = np.hstack((x_ctr - 0.5 * (ws - 1),    #hstack、vstack:合併數組
                         y_ctr - 0.5 * (hs - 1),    #anchor:[[-3.5 2 18.5 13]
                         x_ctr + 0.5 * (ws - 1),     #        [0  0  15  15]
                         y_ctr + 0.5 * (hs - 1)))     #       [2.5 -3 12.5 18]]
    return anchors

#枚舉一個anchor的各種寬高比,anchor[0 0 15 15],ratios[0.5,1,2]
def _ratio_enum(anchor, ratios):
    """   列舉關於一個anchor的三種寬高比 1:2,1:1,2:1
    Enumerate a set of anchors for each aspect ratio wrt an anchor.
    """

    w, h, x_ctr, y_ctr = _whctrs(anchor)  #返回寬高和中心座標,w:16,h:16,x_ctr:7.5,y_ctr:7.5
    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

#枚舉一個anchor的各種尺度,以anchor[0 0 15 15]爲例,scales[8 16 32]
def _scale_enum(anchor, scales):
    """   列舉關於一個anchor的三種尺度 128*128,256*256,512*512
    Enumerate a set of anchors for each scale wrt an anchor.
    """
    w, h, x_ctr, y_ctr = _whctrs(anchor) #返回寬高和中心座標,w:16,h:16,x_ctr:7.5,y_ctr:7.5
    ws = w * scales   #[128 256 512]
    hs = h * scales   #[128 256 512]
    anchors = _mkanchors(ws, hs, x_ctr, y_ctr) #[[-56 -56 71 71] [-120 -120 135 135] [-248 -248 263 263]]
    return anchors

if __name__ == '__main__':  #主函數
    import time
    t = time.time()
    a = generate_anchors()  #生成anchor(窗口)
    print time.time() - t   #顯示時間
    print a
    from IPython import embed; embed()
</span>
       其中,_ratio_enum()部分生成三種寬高比 1:2,1:1,2:1的anchor如下圖所示:
       其中,_scale_enum()部分,生成三種尺寸的anchor,以_ratio_enum()部分生成的anchor[0 0 15 15]爲例,擴展了三種尺度 128*128,256*256,512*512,如下圖所示:

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章