faster rcnn anchors代碼

http://blog.csdn.net/xzzppp/article/details/52317863#

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

import numpy as np  #提供矩陣運(yùn)算功能的庫(kù)  
  
#生成anchors總函數(shù):ratios為一個(gè)列表,表示寬高比為: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  #新建一個(gè)數(shù)組:base_anchor:[0 0 15 15]  
    ratio_anchors = _ratio_enum(base_anchor, ratios)  #枚舉各種寬高比  
    anchors = np.vstack([_scale_enum(ratio_anchors[i, :], scales)  #枚舉各種尺度,vstack:豎向合并數(shù)組  
                         for i in xrange(ratio_anchors.shape[0])]) #shape[0]:讀取矩陣第一維長(zhǎng)度,其值為3  
    return anchors  
  
#用于返回width,height,(x,y)中心坐標(biāo)(對(duì)于一個(gè)anchor窗口)  
def _whctrs(anchor):  
    """ 
    Return width, height, x center, and y center for an anchor (window). 
    """  
    #anchor:存儲(chǔ)了窗口左上角,右下角的坐標(biāo)  
    w = anchor[2] - anchor[0] + 1  
    h = anchor[3] - anchor[1] + 1  
    x_ctr = anchor[0] + 0.5 * (w - 1)  #anchor中心點(diǎn)坐標(biāo)  
    y_ctr = anchor[1] + 0.5 * (h - 1)  
    return w, h, x_ctr, y_ctr  
  
#給定一組寬高向量,輸出各個(gè)anchor,即預(yù)測(cè)窗口,**輸出anchor的面積相等,只是寬高比不同**  
def _mkanchors(ws, hs, x_ctr, y_ctr):  
    #ws:[23 16 11],hs:[12 16 22],ws和hs一一對(duì)應(yīng)。  
    """ 
    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:將數(shù)組轉(zhuǎn)置  
    hs = hs[:, np.newaxis]  
    anchors = np.hstack((x_ctr - 0.5 * (ws - 1),    #hstack、vstack:合并數(shù)組  
                         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  
  
#枚舉一個(gè)anchor的各種寬高比,anchor[0 0 15 15],ratios[0.5,1,2]  
def _ratio_enum(anchor, ratios):  
    """   列舉關(guān)于一個(gè)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)  #返回寬高和中心坐標(biāo),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的四舍五入的數(shù)字,sqrt()方法返回?cái)?shù)字x的平方根  
    ws = np.round(np.sqrt(size_ratios)) #ws:[23 16 11]  
    hs = np.round(ws * ratios)    #hs:[12 16 22],ws和hs一一對(duì)應(yīng)。as:23&12  
    anchors = _mkanchors(ws, hs, x_ctr, y_ctr)  #給定一組寬高向量,輸出各個(gè)預(yù)測(cè)窗口  
    return anchors  
  
#枚舉一個(gè)anchor的各種尺度,以anchor[0 0 15 15]為例,scales[8 16 32]  
def _scale_enum(anchor, scales):  
    """   列舉關(guān)于一個(gè)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) #返回寬高和中心坐標(biāo),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__':  #主函數(shù)  
    import time  
    t = time.time()  
    a = generate_anchors()  #生成anchor(窗口)  
    print time.time() - t   #顯示時(shí)間  
    print a  
    from IPython import embed; embed()  
</span>
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容