最近看了Deep Learning中關於目標檢測的一些內容,其中大部分的內容都是Coursera上吳恩達卷積神經網絡的課程,沒看過的可以看一下,講的很好,通俗易懂,只是在編程
作業中關於網絡訓練以及具體的細節沒有體現,可能是網絡太復雜,不太好訓練。。於是想看一下原作者的論文,看看有沒有實現的細節,雖然論文里也米有具體的實現,但是啃下
一篇論文還是挺開心的。寫博客只是階段性的復習一下自己的學習成果,同時也留個紀念,流水賬一樣,勿噴
論文地址:https://www.cv-foundation.org/openaccess/content_cvpr_2016/papers/Redmon_You_Only_Look_CVPR_2016_paper.pdf
在談論YOLO算法之前先說一下Bounding box,假設我們要檢測的圖像只有一個物體,就是一張圖像中只會出現一個物體,我們都很熟悉,因為在之前做classfication(分類)
問題的時候處理的就是單目標,我們能不能在做分類問題的時候同時也輸出檢測的目標的位置信息呢?也就是localization(定位),答案是可以的,例如我們已經訓練好了一個
CNN模型能夠很好的完成分類問題,那么我們也可以改變一下模型的輸出,即給模型增加幾個輸出:(Pc, bx, by, bh, bw, c1, c2, c3), 其中Pc表示圖像中是否存在目標,bx, by, bh, bw
分別表示Bounding box的中心點坐標,寬度和高度,假設我們原本的分類器只訓練了人, 車, 狗三種分類,則c1, c2, c3對應人, 車, 狗三種分類的概率,(原諒我這三個類別選擇
的可能不是很恰當),下面是關於分類,定位,和目標檢測的一些聯系
說完了Bounding box,下面來說說YOLO算法,You Only Live Once...
在論文的開始,作者列出了模型的一些優缺點:
1.模型簡單速度快(見下圖,確實挺簡單,圖像》》卷積網絡》》輸出),每秒45幀以上,能夠達到實時性的要求
2.算法能夠‘’考慮‘’到整張圖片的所有信息,YOLO reasons globally about the image when
making predictions ,詞窮了我,不知道怎么翻譯最好。這一點要比其他基於RPN算法的目標檢測算法要好,對於背景物體的識別,有更高的准確率
3.YOLO能夠學習到目標的generalizable representations,應該理解成泛化能力比較強吧
當然,作者也說了關於YOLO的缺點,就是准確率可能稍稍低了一點。。
==================================================================================================================================================
We unify the separate components of object detection into a single neural network. Our network uses features from the entire image to predict each bounding box. It
also predicts all bounding boxes across all classes for an image simultaneously. This means our network reasons globally about the full image and all the objects in the image.
The YOLO design enables end-to-end training and realtime speeds while maintaining high average precision
Our system divides the input image into an S × S grid. If the center of an object falls into a grid cell, that grid cells responsible for detecting that object
YOLO模型支持端到端的訓練,這樣會減少很多不必要的工作,而整個模型其實就是一個卷積神經網絡
首先,對於輸入的圖片,將其分成s*s個網格,如果圖像中目標的中心點落在網格內,可以表示為這個網格檢測到了目標
Each grid cell predicts B bounding boxes and confidence scores for those boxes. These confidence scores reflect how confident the model is that the box contains an object and
also how accurate it thinks the box is that it predicts. Formally we define confidence as Pr(Object) ∗ IOUtruth pred. If no object exists in that cell, the confidence scores should be
zero. Otherwise we want the confidence score to equal the intersection over union (IOU) between the predicted box and the ground truth
Each bounding box consists of 5 predictions: x, y, w, h, and confidence. The (x; y) coordinates represent the center of the box relative to the bounds of the grid cell. The width
and height are predicted relative to the whole image. Finally the confidence prediction represents the IOU between the predicted box and any ground truth box.
Each grid cell also predicts C conditional class probabilities, Pr(Classij\Object). These probabilities are conditioned on the grid cell containing an object. We only predict
one set of class probabilities per grid cell, regardless of the number of boxes B.
At test time we multiply the conditional class probabilities and the individual box confidence predictions,
每個網格會預測出B個Bounding box,和這些Bounding box的confidence scores,這個得分反應的是box包含物體的置信度和box預測的正確率,把confidence定義為
如果目標存在,則confidence =,如果不存在,則為0。同時每個BOX也會預測出x, y, w, h四個量,x,y表示物體中心點的坐標,w,h表示寬度和高度。每個網格也會輸出每個
類別對應的可能:Pr(Classij\Object), 是一個條件概率。
作者還給出下面這個公式表示每個類別對應的confidence score
For evaluating YOLO on PASCAL VOC, we use S = 7,B = 2. PASCAL VOC has 20 labelled classes so C = 20.Our final prediction is a 7 × 7 × 30 tensor
下面是作者的網絡的設計,順便說一下,在讀到這里的時候自己也仿照這搭建一個網絡,當然只是做分類問題,結果手頭圖片太少,分分鍾過擬合
關於網絡的訓練有幾點細節,
每個grid有30維,這30維中,8維是回歸box的坐標,2維是box的confidence,還有20維是類別
其中坐標的x,y用對應網格的offset歸一化到0-1之間,w,h用圖像的width和height歸一化到0-1之間。
在實現中,最主要的就是怎么設計損失函數,讓這個三個方面得到很好的平衡。作者簡單粗暴的全部采用了sum-squared error loss來做這件事。這種做法存在以下幾個問題:
It weights localization error equally with classification error which may not be ideal. Also, in every image many grid cells do not contain any object. This pushes the “confidence” scores of those cells
towards zero, often overpowering the gradient from cells that do contain objects. This can lead to model instability,causing training to diverge early on.
To remedy this, we increase the loss from bounding box coordinate predictions and decrease the loss from confidence predictions for boxes that don’t contain objects. We use two parameters, λcoord
and λnoobj to accomplish this. We set λcoord = 5 and λnoobj = 0.5
第一,8維的localization error和20維的classification error同等重要顯然是不合理的
第二,如果一個網格中沒有object(一幅圖中這種網格很多),那么就會將這些網格中的box的confidence push到0,相比於較少的有object的網格,這種做法是overpowering的,這會導致網絡不穩定甚至發散。
解決辦法
更重視8維的坐標預測,給這些損失前面賦予更大的loss weight, 記為在pascal VOC訓練中取5
對沒有object的box的confidence loss,賦予小的loss weight,記為在pascal VOC訓練中取0.5
有object的box的confidence loss和類別的loss的loss weight正常取1
Sum-squared error also equally weights errors in large boxes and small boxes. Our error metric should reflect that small deviations in large boxes matter less than in small
boxes. To partially address this we predict the square root of the bounding box width and height instead of the width and height directly
為了緩和這個問題,作者用了一個比較取巧的辦法,就是將box的width和height取平方根代替原本的height和width。
關於論文就說這么多,這里還想說一些關於IOU和非極大值抑制的理解,下圖出自吳恩達老師的PPT
===================================================================================================================================================
因為YOLO會把圖片分成s*s個網格,那么相鄰的網格內跟肯定會有box圈到的同一個目標,就算在之前的步驟中把一些class confidence scores 低的過濾掉之后還是會得到類似下面的結果
那么該怎么辦呢,非極大值抑制就要發揮作用了,首先,在每個類別中選出得分最高的,每個類別中其余的和選出來的這些做IOU運算(同類別之間運算),設定一個IOU_THRESHOLD
把結果中大於IOU_THRESHOLD的pass掉(因為IOU大說明重疊的部分大,表明box之間很有可能是同一個目標),然后在剩下的box中再次作同樣的處理。多做幾次之后就能得到滿意的結果