1. 程式人生 > >Faster-RCNN的RPNnet解析

Faster-RCNN的RPNnet解析

首先,RPNnet注意包括兩部分,一個是用於預測預測框的前景背景,因此輸出為2k,其中k對應的是所有anchor boxes的個數;另一個是用於預測對應的座標對映關係,輸出為4k;

然後RPN net的輸出結合上一層的feature map同時送入ROI pooling層,然後做進一步的判別。

scales=[8,16,32] ratios=[0.5,1,2] w=[23,16,11] h=[12,16,22]

1:1,1:2,2:1三種比例,因此可能的[w,h]共有如下9種, 23,12—>[184,96],[368,192],[736384] 16,16—>[128,128],[256,256],[512,512] 11,22—>[88,176],[176,352],[352

704]

在這裡插入圖片描述 程式碼如下:

import numpy as np
# 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.]])


#after test,we found that anchors returned by <generate_anchors> func is a np.array with shape is (9,4),
#which represent the feature map (0,0) position that output 9 anchors with 4 coordinates.
#below is the result we got!
'''
(9, 4)
[[ -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.]]
'''

#notice actually noticing this code is sufficient for you to understand the process of generating anchors!
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)
  print(ratio_anchors)
  print(ratio_anchors.shape)
  anchors = np.vstack([_scale_enum(ratio_anchors[i, :], scales)
                       for i in range(ratio_anchors.shape[0])])
  print(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)
  print(anchor)
  print(x_ctr,y_ctr,w,h)
  size = w * h
  size_ratios = size / ratios
  print(size_ratios)
  print("**********************")
  ws = np.round(np.sqrt(size_ratios))
  hs = np.round(ws * ratios)
  print(ws,hs)
  print("======================")
  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)
  print(w,h,x_ctr,y_ctr)
  ws = w * scales
  hs = h * scales
  print(ws,hs,x_ctr,y_ctr)
  anchors = _mkanchors(ws, hs, x_ctr, y_ctr)
  return anchors


if __name__ == '__main__':
  generate_anchors()