锚框(anchors)产生:
感觉论文这块内容说的不太细致,只能深挖源码了~
from __future__ import print_function
# --------------------------------------------------------
# Faster R-CNN
# Copyright (c) 2015 Microsoft
# Licensed under The MIT License [see LICENSE for details]
# Written by Ross Girshick and Sean Bell
# --------------------------------------------------------
import numpy as np
import pdb
# 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.]])
try:
xrange # Python 2
except NameError:
xrange = range # Python 3
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_anchor)
ratio_anchors = _ratio_enum(base_anchor, ratios)
anchors = np.vstack([_scale_enum(ratio_anchors[i, :], scales)
for i in xrange(ratio_anchors.shape[0])])
return anchors
def _whctrs(anchor):
"""
Return width, height, x center, and y center for an anchor (window).
"""
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):
"""
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]
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):
"""
Enumerate a set of anchors for each aspect ratio wrt an anchor.
"""
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)
print("haha", hs)
anchors = _mkanchors(ws, hs, x_ctr, y_ctr)
return anchors
def _scale_enum(anchor, scales):
"""
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__':
import time
t = time.time()
a = generate_anchors()
print(time.time() - t)
print(a)
from IPython import embed; embed()
源码中除了第一个函数以外,其余函数都在第一个函数体调用过。我们注意第一个函数上来就有个base_anchor
,这个可以理解为一个“基础框”,其他不同尺度和不同宽高比的锚框都是在“基础框”之上建立起来的,而且feature map
同一个像素点的不同的9个锚框共用同一个几何中心点。base_anchor = [0, 0, 15, 15]
,这指的是base_anchor
的左下角坐标点(0,0)
和右上角坐标点(15,15)
。
generate_anchors
函数的参数中有ratios=[0.5,1,2]
和scales=[8,16,32]
,ratios
代表三种不同宽高比的锚框,scales
代表三种不同尺度的锚框(即三种不同大小的锚框),这两者组合即可得到9种锚框。
_ratio_enum(anchor, ratios)
函数中有调用_whctrs(anchor)
,我们通过源码看一下_whctrs(anchor)
函数是用来做什么的:
def _whctrs(anchor):
"""
Return width, height, x center, and y center for an anchor (window).
"""
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
该函数将锚框(xmin, ymin, xmax, ymax)
坐标形式转为(w,h,x_ctr,y_ctr)
的形式,后者为锚框的宽、高以及中心点坐标表示形式。经转换,base_anchor
的(0,0,15,15)
变为(16,16,7.5,7.5)
。为什么宽和高是16呢?因为这里计算的锚框的宽高是按照像素计算的,并不是根据数学中的长度来计量,并且base_anchor
是从0像素点起始的。base_anchor的面积即为16x16=256
,而且不同宽高比的锚框等于其面积比(即base_anchor/ratio
得到的是宽高比为ratio
的锚框),base_anchor
即相当于宽高比为1的锚框,则其余情况得到的锚框面积即为256的1/2倍和2倍,size_ratios=[512,256,128]
,然后
ws = np.round(np.sqrt(size_ratios))
,hs = np.round(ws * ratios)
(不太明白这里ws和hs的计算原理是啥~),得到ws=[23,16,11]
, hs=[12,16,22]
,之后计算三种不同size_ratio
下的锚框的坐标表示,是将(w,h,x_ctr,y_ctr)
转为(xmin,ymin,xmax,ymax)
:
def _mkanchors(ws, hs, x_ctr, y_ctr):
"""
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]
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 _scale_enum(anchor, scales):
"""
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
手法类似,将上述的坐标(xmin,ymin,xmax,ymax)
再次转为(w,h,x_ctr,y_ctr)
的形式,w和h分别乘以尺度scales
,即将宽高分别放大8倍、16倍、32倍,之后再次转为(xmin,ymin,xmax,ymax)
的表达形式,即可得到不同尺度的锚框。