Skip to content

D2L-CH13-Computer Vision

13. Computer Vision

13.1 Image Augmentation

略過

13.2 Fine-Tuning

如何解決資料不足的問題?

  1. 增加更多資料
  2. 採用Fine-Tuning(Transfer Learning的一種技術)

Fine-Tuning的4個步驟:

  1. pre-train model。
  2. 複製網路結構和參數,但是不包括output layer。假設原始資料所習得的知識可以套用在新資料上。
  3. 根據新資料的類別數量更換output layer,並且隨機初始化參數。
  4. 重新訓練output layer參數,並對其他參數進行fine-tuning。

Summary

  • 迁移学习将从源数据集学到的知识迁移到目标数据集上。微调是迁移学习的一种常用技术。
  • 目标模型复制了源模型上除了输出层外的所有模型设计及其参数,并基于目标数据集微调这些参数。而目标模型的输出层需要从头训练。
  • 一般来说,微调参数会使用较小的学习率,而从头训练输出层可以使用较大的学习率。

13.3 Object Detection and Bounding Boxes

object detection 同時要識別圖片裡的目標,以及目標的位置

object detection = object recognition

13.3.1 Bounding Box

bounding box用來描述目標的位置。bounding box是一個矩形框,由矩形左上角的xy軸坐標與右下角的xy軸坐標確定。

In object detection, we usually use a bounding box to describe the target location. The bounding box is a rectangular box that can be determined by the \(x\) and \(y\) axis coordinates in the upper-left corner and the \(x\) and \(y\) axis coordinates in the lower-right corner of the rectangle.

13.4 Anchor Boxes

目標檢測算法通常會在輸入圖像中採樣大量的區域,然後判斷這些區域中是否包含我們感興趣的目標,並調整區域邊緣從而更準確地預測目標的真實邊界框(ground-truth bounding box)。不同的模型使用的區域採樣方法可能不同。

Object detection algorithms usually sample a large number of regions in the input image, determine whether these regions contain objects of interest, and adjust the edges of the regions so as to predict the ground-truth bounding box of the target more accurately. Different models may use different region sampling methods.

作者介紹其中一種取樣方法:以每個像素為中心生成多個大小和寬高比(aspect ratio)不同的bounding boxes。這些bounding boxes被稱為anchor boxes。

Here, we introduce one such method: it generates multiple bounding boxes with different sizes and aspect ratios while centering on each pixel. These bounding boxes are called anchor boxes.

13.4.1 Generating Multiple Anchor Boxes

若預設為每個pixel生成多個anchor boxes, 則一張圖片會生成 \(w\times h\times n\times m\) 個 anchor boxes.

\(w\)代表圖片寬度

\(h\)代表圖片高度

\(n\)代表anchor box的size變化數量

\(m\)代表anchor box的aspect ratio的變化數量


MXNet有內建函數可以生成anchor boxes,但Tensorflow似乎沒有。


下圖說明,針對pixel座標(250,250)生成的anchor boxes。


13.4.2 Intersection over Union (IoU)

透過計算集合相似度的概念,來量化anchor boxes和ground-truth boxing box的相似度。

If the ground-truth bounding box of the target is known, how can “well” here be quantified? An intuitive method is to measure the similarity between anchor boxes and the ground-truth bounding box.

\[J(\mathcal{A},\mathcal{B}) = \frac{\left|\mathcal{A} \cap \mathcal{B}\right|}{\left| \mathcal{A} \cup \mathcal{B}\right|}.\]

在CV不提Jaccard index,而是以IoU表示。但概念其實一樣。

13.4.3 Labeling Training Set Anchor Boxes

在訓練集裡,每個anchor box被視為一個訓練樣本,而每個樣本會被貼上2種標籤:categoryoffset

In the training set, we consider each anchor box as a training example. In order to train the object detection model, we need to mark two types of labels for each anchor box: first, the category of the target contained in the anchor box (category) and, second, the offset of the ground-truth bounding box relative to the anchor box (offset).


首先,透過計算IoU分配ground-truth bounding box給anchor box。

We know that, in the object detection training set, each image is labelled with the location of the ground-truth bounding box and the category of the target contained. After the anchor boxes are generated, we primarily label anchor boxes based on the location and category information of the ground-truth bounding boxes similar to the anchor boxes. So how do we assign ground-truth bounding boxes to anchor boxes similar to them?

用下圖說明對anchor boxes貼標過程。下圖是\(n_{a} \times n_{b}\)的矩陣(anchor box數量 \(\times\) ground-truth bounding box數量),矩陣內的值代表IoU; \(n_{a}\) 必須大於或等於 \(n_{b}\)

1.找出矩陣內最大值\(x_{23}\)\(B_{3}\)分配給\(A_{2}\),並排除掉row2和col3。 2.下個最大值是\(x_{71}\)\(B_{1}\)分配給\(A_{7}\),並排除row7和col1。 3.下個最大值是\(x_{54}\)\(B_{4}\)分配給\(A_{5}\),並排除row5和col4。 4.下個最大值是\(x_{92}\)\(B_{2}\)分配給\(A_{9}\),並排除row9和col2。

可以發現到還剩下5個anchor boxes(\(n_{a}-b_{b}\)),\(i\)=1,3,4,6,8還沒被分配。於是對 \(A_{i}\) row找最大值\(B_{ij}\),並根據門檻值決定是否分配\(B_{j}\)\(A_{i}\)


下一步,對anchor box貼上category以及offset

Now we can label the categories and offsets of the anchor boxes. If an anchor box \(A\) is assigned ground-truth bounding box \(B\), the category of the anchor box \(A\) is set to the category of \(B\).

根據B和A的中心坐標的相對位置以及兩個boxes的相對大小來設置anchor box的offset。

And the offset of the anchor box \(A\) is set according to the relative position of the central coordinates of \(B\) and \(A\) and the relative sizes of the two boxes. Because the positions and sizes of various boxes in the dataset may vary, these relative positions and relative sizes usually require some special transformations to make the offset distribution more uniform and easier to fit.

以下公式是一種常見的offset設定公式:

\((x_a, y_a), (x_b, y_b)\) 是2個boxes中心座標 \(w_a, w_b\) 代表寬度 \(h_a, h_b\) 代表高度

\[\left( \frac{ \frac{x_b - x_a}{w_a} - \mu_x }{\sigma_x}, \frac{ \frac{y_b - y_a}{h_a} - \mu_y }{\sigma_y}, \frac{ \log \frac{w_b}{w_a} - \mu_w }{\sigma_w}, \frac{ \log \frac{h_b}{h_a} - \mu_h }{\sigma_h}\right),\]

預設 \(\mu_x = \mu_y = \mu_w = \mu_h = 0, \sigma_x=\sigma_y=0.1, \sigma_w=\sigma_h=0.2\)

如果anchor box沒有被分配到ground-truth bounding box,它就會被設定為background。

代表背景的anchor box會被視為negative anchor box; 反之被視為positive anchor box。


參考作者的python code, 每個ground truth bounding box的向量設計:第1個值為類別,第2,3個值為左上角axis,第4,5個值為右下角axis。 每個anchor box的向量設計:第1,2個值為左上角axis,第3,4個值為右下角axis。

ground_truth = np.array([[0, 0.1, 0.08, 0.52, 0.92],
                        [1, 0.55, 0.2, 0.9, 0.88]])
anchors = np.array([[0, 0.1, 0.2, 0.3],
                    [0.15, 0.2, 0.4, 0.4],
                    [0.63, 0.05, 0.88, 0.98],
                    [0.66, 0.45, 0.8, 0.8],
                    [0.57, 0.3, 0.92, 0.9]])

後續是MXNet的函數介紹,懶得看了...

13.4.4 Bounding Boxes for Prediction

當anchor box數量較多時,同一個目標上可能會輸出較多相似的prediction bounding boxes。為了使結果更加簡潔,我們可以移除相似的prediction bounding boxes。常用的方法叫作non-maximum suppression(NMS)。

Summary

  • 以每個像素為中心,生成多個大小和寬高比不同的anchor boxes。
  • IoU是兩個邊界框相交面積與相併面積之比。
  • 在訓練集中,為每個anchor box標註兩類標籤:一是錨框所含目標的類別;二是真實邊界框相對錨框的offset。
  • 預測時,可以使用NMS抑制來移除相似的prediction bounding boxes,從而令結果簡潔。

13.5 Multiscale Object Detection

為每個pixel生成多個anchor boxes,後續進行貼標會需要龐大的計算量。

最簡單的方法就是對部份pixels取樣,只對特定pixels生成anchor boxes。而且,我們可以生成不同數量和大小的anchor boxes,並具有多個scales。

It is not difficult to reduce the number of anchor boxes. An easy way is to apply uniform sampling on a small portion of pixels from the input image and generate anchor boxes centered on the sampled pixels. In addition, we can generate anchor boxes of varied numbers and sizes on multiple scales.

when using smaller anchor boxes to detect smaller objects, we can sample more regions; when using larger anchor boxes to detect larger objects, we can sample fewer regions.

小目標比大目標在圖片上出現位置的可能性更多。

舉例:形狀為\(1 \times 11 \times 1\)\(1 \times 21 \times 2\)\(2 \times 22 \times 2\)的目標在形狀為\(2 \times 22 \times 2\)的圖像上可能出現的位置分別有4、2和1種。

因此,當使用比較小的anchor box來檢測小目標時,我們可以採樣比較多的區域;而當使用比較大的anchor box來檢測大目標時,我們可以採樣較少的區域。

透過CNN的feature map,我們可以透過改變feature map的形狀,來決定anchor box的中點。

後續作者引入receptive field的敘述就看不懂了...

When the feature maps of different layers have receptive fields of different sizes on the input image, they are used to detect objects of different sizes.

Summary

  • 可以在多個尺度下生成不同數量和不同大小的anchor boxes,從而在多個尺度下檢測不同大小的目標。
  • feature map的形狀能確定任一圖像上均勻採樣的anchor box中心。
  • 用輸入圖像在某個receptive field區域內的信息來預測輸入圖像上與該區域相近的anchor box的類別和偏移量。

13.6 The Object Detection Dataset

MXNet示範,略過。

13.7 Single Shot Multibox Detection (SSD)