Anchor是Faster RCNN中的一个重要的概念,在对图像中的物体进行分类检测之前,先要生成一系列候选的检测框,以便于神经网络进行分类和识别。
图1-Faster RCNN中的锚框
论文中的描述如下:
An anchor is centered at the sliding window in question, and is associated with a scale and aspect ratio.
如图1所示,Anchor是以待检测位置为中心,以指定的大小和高宽比构成一组锚框。 假设Feature Map的宽度为W,宽度为H,在每个待检测的位置生成的锚框数目为K,根据滑动窗口的方法,生成总的锚框的数量是W * H * K。
在论文中,每个锚点有3种面积
和3种长宽比
,它们相互组合,每个Anchor生成9个锚框。
根据锚框得到其中心点(x_ctr,y_ctr)、宽度w、高度h。
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
根据宽度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)
anchors = _mkanchors(ws, hs, x_ctr, y_ctr)
return anchors
对于同一个Anchor,不同的宽高比(Ratio)的面积是基本相同的; 记Anchor的面积为:area=16*16,宽高比:ratio=w/h,根据面积不变:
这也是上述代码的实现逻辑,代码中在根据ratio计算完w和h之后,进行了取整操作。 在实际生成Anchors时,先定义一个大小为16 * 16的Base Anchor。 函数输入:
函数输出:
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
函数输入: anchor=[-3.5, 2.0, 18.5, 13.0],scales=[8.0, 16.0, 24.0] 函数输出: [[ -84.0, -40.0, 99.0, 55.0], [-176.0, -88.0, 191.0, 103.0], [-360.0, -184.0, 375.0 199.]]
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
ratio_anchors = _ratio_enum(base_anchor, ratios)
anchors = np.vstack([_scale_enum(ratio_anchors[i, :], scales)
for i in range(ratio_anchors.shape[0])])
return anchors
以上代码生成9个锚框: [[ -84. -40. 99. 55.] [-176. -88. 191. 103.] [-360. -184. 375. 199.] [ -56. -56. 71. 71.] [-120. -120. 135. 135.] [-248. -248. 263. 263.] [ -36. -80. 51. 95.] [ -80. -168. 95. 183.] [-168. -344. 183. 359.]]
anchor效果