Fasterrcnn代码个人精细解读(先验框生成部分)
生活随笔
收集整理的這篇文章主要介紹了
Fasterrcnn代码个人精细解读(先验框生成部分)
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
import numpy as np
def generate_anchor_base(base_size=16, ratios=[0.5, 1, 2], scales=[8, 16, 32]):·生成如圖各點處的3種長寬比、3種大小(長寬幾何均值為size*scale)的9個先驗框,如此長寬可由h*w
=(ratio*w)*w=ratio*(w^2)等于(b*s)^2,得w=bs*√(1/r),h=ratio*w=bs*√r,如下循環(huán)即得anchor_base = np.zeros(9, 4)·九行四列的空矩陣以存九個框中主角點(左下+右上)坐標(biāo)for i in range(len(ratios)):for j in range(len(scales)):
index = i * len(scales) + j·先以i,j值即[0,1,2]*3+[0,1,2]為九個框偏上序號h = base_size * scales[j] * np.sqrt(ratios[i])w = base_size * scales[j] * np.sqrt(1. / ratios[i])anchor_base[index, 0] = - h / 2.anchor_base[index, 1] = - w / 2.anchor_base[index, 2] = h / 2.anchor_base[index, 3] = w / 2.
return anchor_base ·即生成相對當(dāng)前點的先驗框角點坐標(biāo),但最終需要關(guān)于圖片(此處是受采后特征圖)原點的各點處的各框坐標(biāo),故再定義函數(shù)def _enumerate_shifted_anchor(anchor_base, feat_stride, height, width):
·stride即經(jīng)4次pooling所得的縮小的倍數(shù)即16shift_x = np.arange(0, width * feat_stride, feat_stride)·特征圖上各點在原圖的坐標(biāo)shift_y = np.arange(0, height * feat_stride, feat_stride)shift_x, shift_y = np.meshgrid(shift_x, shift_y)·meshgrid用于在指定橫縱坐標(biāo)向量下生成兩者搭配組成的網(wǎng)格點,如圖中x=[1,2],y=[3,4],則x豎向擴展為[[1,2],[1,2]],y則先轉(zhuǎn)列矢[[3],[4]]再橫向擴展為[[3,3],[4,4]],這樣X,Y矩陣為網(wǎng)格點的橫縱坐標(biāo)矩,組合即得完整坐標(biāo)shift = np.stack((shift_x.ravel(), shift_y.ravel(), shift_x.ravel(), shift_y.ravel(),), axis=1)·但是這樣還不能真正轉(zhuǎn)為坐標(biāo)集的形式,可配合拉平函數(shù)ravel將X,Y矩陣按閱讀的順序拉成列向量并按列(axis=1)堆疊,實戰(zhàn)中為[[ 0 0 0 0],[ 16 0 16 0],..[592 592 592 592]]即先定Y再變X,即生成受采的原圖網(wǎng)格坐標(biāo)集如圖,這樣將各點坐標(biāo)與點原框坐標(biāo)疊加即得圖中產(chǎn)生的各框坐標(biāo)如下A = anchor_base.shape[0]·每個點的先驗框數(shù)量為9
K = shift.shape[0]·總的特征圖上的點數(shù)亦采樣的點數(shù)為38*38=1444
·現(xiàn)在問題在于如何疊加(A,4)和(K,4),可以用循環(huán)遍歷的方法,但在numpy中高低維數(shù)組相加低維自動升維如[[1,1,1],[2,2,2]]+1=[[2 2 2], [3 3 3]],同維異度相加低度(多為1)自動以復(fù)制的形式補位成高度如[1,2,3]+[[1],[2],[3],[4],[5]]=[[2 3 4] [3 4 5] [4 5 6] [5 6 7] [6 7 8]],這樣可以為(A,4)和(K,4)錯位添維成(1, A, 4)和(K, 1, 4),相加補位得(K, A, 4)正是疊加的結(jié)果:anchor = anchor_base.reshape((1, A, 4)) + shift.reshape((K, 1, 4))anchor = anchor.reshape((K * A, 4)).astype(np.float32)·再reshape回正常的矩陣即可return anchor·得到基于原圖的各點處九個先驗框的坐標(biāo),一共1444*9個if __name__ == "__main__":·此句表示單獨運行當(dāng)前文件時此段作主程序運行且不做可調(diào)函import matplotlib.pyplot as plt·此段就是畫出單點處先驗框的示意圖anchors_all= _enumerate_shifted_anchor(generate_anchor_base(), 16, 38, 38)fig = plt.figure()·用Python的matplotlib庫創(chuàng)建一個新的圖形對象ax = fig.add_subplot(111)·若(234)表示生成的圖在2*3矩陣中的第4位置plt.ylim(-300,900); plt.xlim(-300,900)·指定顯示范圍shift_x = np.arange(0, width * feat_stride, feat_stride)shift_y = np.arange(0, height * feat_stride, feat_stride)shift_x, shift_y = np.meshgrid(shift_x, shift_y)plt.scatter(shift_x,shift_y)·scatter即將兩矩同位素作橫縱坐標(biāo)以畫其散點圖box_widths = anchors_all[:,2]-anchors_all[:,0]box_heights = anchors_all[:,3]-anchors_all[:,1]·算矩寬高for i in [108, 109, 110, 111, 112, 113, 114, 115, 116]:rect=plt.Rectangle([anchors_all[i,0],anchors_all[i, 1]],box_widths[i],box_heights[i],
color="r",fill=False)·首參為矩形左上角點坐標(biāo)(整個粗體所示),后面依次為寬高ax.add_patch(rect)·向子圖subplot(111)添加矩形框plt.show()
總結(jié)
以上是生活随笔為你收集整理的Fasterrcnn代码个人精细解读(先验框生成部分)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 中标麒麟系统下(Neokylin7)达梦
- 下一篇: 【diannaoxitong】高手分享: