前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Faster-RCNN中Anchor锚框生成

Faster-RCNN中Anchor锚框生成

作者头像
YoungTimes
发布2022-04-28 13:05:45
9500
发布2022-04-28 13:05:45
举报

Anchor是Faster RCNN中的一个重要的概念,在对图像中的物体进行分类检测之前,先要生成一系列候选的检测框,以便于神经网络进行分类和识别。

图1-Faster RCNN中的锚框

一、什么是Anchor

论文中的描述如下:

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。

二、Anchor的生成

在论文中,每个锚点有3种面积

和3种长宽比

,它们相互组合,每个Anchor生成9个锚框。

三、Anchor的代码实现

1.辅助函数

根据锚框得到其中心点(x_ctr,y_ctr)、宽度w、高度h。

代码语言:javascript
复制
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)生成锚框。

代码语言:javascript
复制
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
2.生成不同宽高比的锚框
代码语言:javascript
复制
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。 函数输入:

函数输出:

3.生成不同比例的锚框
代码语言:javascript
复制
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.]]

4.锚框生成入口函数
代码语言:javascript
复制
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效果

本文参与?腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2019-07-06,如有侵权请联系?cloudcommunity@tencent.com 删除

本文分享自 半杯茶的小酒杯 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与?腾讯云自媒体分享计划? ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 一、什么是Anchor
  • 二、Anchor的生成
  • 三、Anchor的代码实现
    • 1.辅助函数
      • 2.生成不同宽高比的锚框
        • 3.生成不同比例的锚框
          • 4.锚框生成入口函数
          • 四、锚框的效果
          领券
          问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
          http://www.vxiaotou.com