---
# System prepended metadata

title: Faster R-CNN
tags: [Paper Notes]

---

###### tags: `Paper Notes`
# Faster R-CNN
* 原文：[Faster R-CNN: Towards Real-Time Object Detection with Region Proposal Networks](https://arxiv.org/abs/1506.01497)
* 作者：Shaoqing Ren, Kaiming He (何凱明), Ross Girshick, and Jian Sun
* 時間：2015 年

### Introduction
* 現今的 object detector 都是先用 region proposal method 提出 region 後，在用 region-based CNN 去預測 bounding box 與 object class。然而，region proposal method 常常成為 object detector 的效能瓶頸。例如 R-CNN 中的 Selective Search 處理一張圖片就要花上 2 秒，與 real-time 的目標相差甚遠。
* 因此，作者提出 Region Proposal Networks (RPN) 來當作 region proposal method。RPN 的輸入為 region-based CNN 的 feature maps，輸出為 objectness scores 與 object proposals。也就是說 region-based CNN 與 region proposal method 共享同一份 feature maps。
* RPN 是一個 fully convolutional networks (FCN) 並且可以 end-to-end 的訓練。
* 有了 RPN，region proposal 的時間可以從  Selective Search 的 2 秒降到 10 毫秒。

### Faster R-CNN
* 如 Figure 2 所示，Faster R-CNN 由 RPN 與 classifier (Fast R-CNN) 兩部分組成。RPN 掃過 image 後，會產生多個 region proposals，然後在根據 region proposals 切出對應的 feature maps 的部份。最後將切出的 feature maps 丟進 Fast R-CNN 做最後的處理，提出 bounding boxes。
<center><img src="https://i.imgur.com/yedbCIi.png" width=350></center>

* RPN 架構如 Figure A 所示。在這篇論文中，作者選用了 ZF-Net (與 VGG-16) 作為 RPN 中的 CNN 網路。
  * image 經過 ZF-Net 後，會得到 $13 \times 13 \times 256$ 的 feature maps。
  * 再將這個 feature maps 經過一層 $3 \times 3$ 的 conv. layer，一樣得到 $13 \times 13 \times 256$ 的 feature maps。
  * 最後將 feature maps 分別送入兩個 $1 \times 1$ 的 conv. layer，cls. layer 與 reg. layer。
    * cls. layer 用於分類該 grid 上有無 object。reg. layer 用於回歸 region proposal 的中心座標、寬高 $(x, y, w, h)$。
    * cls. layer 的 filter 數為 $2K$。reg. layer 的 filter 數為 $4K$。
    * $K$ 表示每個 grid 上的 anchor 數。
<center><img src="https://i.imgur.com/VEwHGap.png" width=600></center>
<center>Figure A: RPN 架構圖。</center>

* 總共用了 9 種 anchor box ($K = 9$)：
  * 3 scales：128、256、512 pixels
  * 3 rations：(1:1)、(2:1)、(1:2)
* 此外，作者還實驗了不同組合的 anchor box 的效能，如 Table 8 所示。
<center><img src="https://i.imgur.com/eHX10wq.png" width=350></center>

* Loss Function of RPN：
  $$
  L(\{p_i\}, \{t_i\}) = \frac{1}{N_{cls}} \sum_{i} L_{cls}(p_i, p_{i}^{*}) + \lambda \frac{1}{N_{reg}} \sum_{i} p_{i}^{*} L_{reg}(t_i, t_{i}^{*})
  $$
  * $i$：index of an anchor in a mini-batch。
  * $p_i$：第 $i$ 個 anchor box 偵測該 grid 上有物件的機率。
  * $p_{i}^{*}$：第 $i$ 個 anchor box 所在的 grid 上是否有物件。有則 $p_{i}^{*}=1$，否則 $p_{i}^{*}=0$。
  * $t_i$：第 $i$ 個 anchor box 的 $(x, y, w, h)$ 的預測偏誤。
  * $t_i^*$：第 $i$ 個 anchor box 的 $(x, y, w, h)$ 的真實偏誤。
  * $N_{cls}$：mini-batch size
  * $N_{reg}$：number of anchor locations
  * $\lambda$：balancing parameter，這裡設 10。
* bounding box regression 中 4 個參數的定義如下：
  <center><img src="https://i.imgur.com/AErEjam.png" width=280></center>
  * $x, x_a, x^*$：predicted box、anchor box、ground truth box。
  * $y, w, h$ 的定義同上
* $L_{reg}$ 的定義如下 (smooth L1)：
  $$
  L_{reg}(t_i, t_i^*) = R(t_i - t_i^*) = \\
  smooth_{L1}(x) = 
  \left\{ \begin{array}{rcl}
  0.5 x^2,\ if\ |x| < 1 \\
  |x| - 0.5,\ otherwise
  \end{array}\right.
  $$

* Faster R-CNN 的訓練方式：
  1. 單獨訓練 RPN (CNN 有先預訓練在 ImageNet 上)。
  2. 利用 RPN 提出的 proposal 單獨訓練 Fast R-CNN (CNN 有先預訓練在 ImageNet 上)。
  3. 固定 Fast R-CNN 的 weights 訓練 RPN。
  4. 固定 RPN 的 weights 訓練 Fast R-CNN。

### Experiments & Results
* Faster R-CNN 在 Pascal VOC 2007 的訓練結果如 Table 2 所示。
<center><img src="https://i.imgur.com/4rfLwt2.png" width=700></center>

### My Point of View
> 雖然 Faster R-CNN 是在 2015 年提出，距今 (2020) 也有 5 年的時間了，但還是有很多人在用。例如 MCAN (2019 VQA Challange Winner)。可見 Faster R-CNN 非常重要。