anchors 值的含義為
在 feature maps 上進行滑窗操作(sliding window). 滑窗尺寸為 n×n, 如 3×3.
對於每個滑窗, 會生成 9 個 anchors, anchors 具有相同的中心 center=xa,center=ya, 但 anchors 具有 3 種不同的長寬比(aspect ratios) 和 3 種不同的尺度(scales)。 最終,9個anchor的四個數值分別代表矩形框的左下角x,y,右上角x,y。計算是相對於原始圖片尺寸的.
anchor的值是由如下步驟計算得來的:
主要包括兩步:
保持 anchor 面積固定不變, 改變長寬比 aspect ratio = [0.5, 1, 2] (函數_ratio_enum(anchor, ratios))
保持 anchor 長寬比固定不變,縮放尺度 scale = [8,16,32] (函數_scale_enum(anchor, scales))
1. 先設置基礎anchor,base=16,base_anchor為[0,0,15,15],anchor中心(7.5,7.5),以及面積(size=256)。
2. 計算基礎anchor的面積,分別除以ratio[0.5,1,2],得到 [512,256,128]
3. anchor的寬度w由三個面積的平方根值確定,得到 w[23,16,11]
4. anchor的高度h由[23,16,11]*[0.5,1,2]確定,得到 h[12,16,22]。
5. 由anchor的中心以及不同的寬和高可以得到此時的anchors。此處為[-3.5, 2. , 18.5, 13. ], [ 0. , 0. , 15. , 15. ], [ 2.5, -3. , 12.5, 18. ]
例 xa=7.5, ya=7.5,w=23,h=12,
x_left = xa-(w-1)/2=-3.5, y_left = ya-(h-1)/2 = 2,
x_right = xa+(w-1)/2 = 18.5, y_right = ya+(h-1)/2=13
6. 利用三種不同的scales[8,16,32]分別去擴大anchors,擴大的方法是先計算出來上一步的anchor的中心以及寬高,使寬高分別乘以scale,然后再利用中心和新的寬高計算出最終所要的anchors
例如: xa=7.5, ya=7.5, w=23*8, h=12*8, x_min=xa-(w*scale-1)/2 = 7.5-(23*8-1)/2=84 ==> -83 -39 100 56
"""
generate_anchors.py
"""
import numpy as np # Verify that we compute the same anchors as Shaoqing's matlab implementation: # # >> load output/rpn_cachedir/faster_rcnn_VOC2007_ZF_stage1_rpn/anchors.mat # >> anchors # # 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 # array([[ -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(stride=16, sizes=(32, 64, 128, 256, 512), aspect_ratios=(0.5, 1, 2)): """ 生成 anchor boxes 矩陣,其格式為 (x1, y1, x2, y2). Anchors 是以 stride / 2 的中心,逼近指定大小的平方根面積(sqrt areas),長寬比 Anchors are centered on stride / 2, have (approximate) sqrt areas of the specified sizes, and aspect ratios as given. """ return _generate_anchors(stride, np.array(sizes, dtype=np.float) / stride, np.array(aspect_ratios, dtype=np.float) ) def _generate_anchors(base_size, scales, aspect_ratios): """ 通過枚舉關於參考窗口window (0, 0, base_size - 1, base_size - 1) 的長寬比(aspect ratios) X scales, 來生成 anchore 窗口(參考窗口 reference windows). """ anchor = np.array([1, 1, base_size, base_size], dtype=np.float) - 1 anchors = _ratio_enum(anchor, aspect_ratios) anchors = np.vstack([_scale_enum(anchors[i, :], scales) for i in range(anchors.shape[0])]) return anchors def _whctrs(anchor): """ 返回 anchor 窗口的 width, height, x center, y center. """ 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): """ 給定 center(x_ctr, y_ctr) 及 widths (ws),heights (hs) 向量,輸出 anchors窗口window 集合. """ ws = ws[:, np.newaxis] hs = hs[:, np.newaxis] 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 def _ratio_enum(anchor, ratios): """ 對於每個關於一個 anchor 的長寬比aspect ratio,枚舉 anchors 集合. """ w, h, x_ctr, y_ctr = _whctrs(anchor) size = w * h size_ratios = size / ratios ws = np.round(np.sqrt(size_ratios)) hs = np.round(ws * ratios) anchors = _mkanchors(ws, hs, x_ctr, y_ctr) return anchors def _scale_enum(anchor, scales): """ 對於每個關於一個 anchor 的尺度scale,枚舉 anchors 集合. Enumerate a set of anchors for each scale wrt an anchor.""" w, h, x_ctr, y_ctr = _whctrs(anchor) ws = w * scales hs = h * scales anchors = _mkanchors(ws, hs, x_ctr, y_ctr) return anchors if __name__ == '__main__': print 'Anchor Generating ...' anchors = generate_anchors() print anchors print 'Done.'
參考:
https://blog.csdn.net/qq_26974871/article/details/81251851