【CV】目標(biāo)檢測(cè)入門和實(shí)現(xiàn)思路!

一、目標(biāo)檢測(cè)基本概念
1. 什么是目標(biāo)檢測(cè)
目標(biāo)檢測(cè)是計(jì)算機(jī)視覺(jué)中的一個(gè)重要任務(wù),近年來(lái)傳統(tǒng)目標(biāo)檢測(cè)方法已經(jīng)難以滿足人們對(duì)目標(biāo)檢測(cè)效果的要求,隨著深度學(xué)習(xí)在計(jì)算機(jī)視覺(jué)任務(wù)上取得的巨大進(jìn)展,目前基于深度學(xué)習(xí)的目標(biāo)檢測(cè)算法已經(jīng)成為主流。
相比較于基于深度學(xué)習(xí)的圖像分類任務(wù),目標(biāo)檢測(cè)任務(wù)更具難度,具體區(qū)別如下圖所示。
圖像分類:只需要判斷輸入的圖像中是否包含感興趣物體。
目標(biāo)檢測(cè):需要在識(shí)別出圖片中目標(biāo)類別的基礎(chǔ)上,還要精確定位到目標(biāo)的具體位置,并用外接矩形框標(biāo)出。
分類和目標(biāo)檢測(cè)任務(wù)示意圖
2. 目標(biāo)檢測(cè)常用思路
自2012年Alex Krizhevsky憑借Alex在ImageNet圖像分類挑戰(zhàn)賽中拿下冠軍之后,深度學(xué)習(xí)在圖像識(shí)別尤其是圖像分類領(lǐng)域開(kāi)始大放異彩,大眾的視野也重新回到深度神經(jīng)網(wǎng)絡(luò)中。緊接著,不斷有更深更復(fù)雜的網(wǎng)絡(luò)出現(xiàn),一再刷新ImageNet圖像分類比賽的記錄。
大家發(fā)現(xiàn),通過(guò)合理的構(gòu)造,神經(jīng)網(wǎng)絡(luò)可以用來(lái)預(yù)測(cè)各種各樣的實(shí)際問(wèn)題。于是人們開(kāi)始了基于CNN的目標(biāo)檢測(cè)研究, 但是隨著進(jìn)一步的探索大家發(fā)現(xiàn),似乎CNN并不善于直接預(yù)測(cè)坐標(biāo)信息。并且一幅圖像中可能出現(xiàn)的物體個(gè)數(shù)也是不定的,模型如何構(gòu)建也比較棘手。
因此,人們就想,如果知道了圖中某個(gè)位置存在物體,再將對(duì)應(yīng)的局部區(qū)域送入到分類網(wǎng)絡(luò)中去進(jìn)行判別,那我不就可以知道圖像中每個(gè)物體的位置和類別了嗎?
但是,怎么樣才能知道每個(gè)物體的位置呢?顯然我們是沒(méi)辦法知道的,但是我們可以去猜啊!所謂猜,其實(shí)就是通過(guò)滑窗的方式,羅列圖中各種可能的區(qū)域,一個(gè)個(gè)去試,分別送入到分類網(wǎng)絡(luò)進(jìn)行分類得到其類別,同時(shí)我們會(huì)對(duì)當(dāng)前的邊界框進(jìn)行微調(diào),這樣對(duì)于圖像中每個(gè)區(qū)域都能得到(class,x1,y1,x2,y2)五個(gè)屬性,匯總后最終就得到了圖中物體的類別和坐標(biāo)信息。
總結(jié)一下我們的這種方案思路:先確立眾多候選框,再對(duì)候選框進(jìn)行分類和微調(diào)。
觀察下圖,更形象的理解下這種思想:

3. 目標(biāo)框定義方式
任何圖像任務(wù)的訓(xùn)練數(shù)據(jù)都要包括兩項(xiàng),圖片和真實(shí)標(biāo)簽信息,通常叫做GT。
圖像分類中,標(biāo)簽信息是類別。目標(biāo)檢測(cè)的標(biāo)簽信息除了類別label以外,需要同時(shí)包含目標(biāo)的位置信息,也就是目標(biāo)的外接矩形框bounding box。
用來(lái)表達(dá)bbox的格式通常有兩種,(x1, y1, x2, y2) 和 (c_x, c_y, w, h) ,如圖所示:

之所以使用兩種不同的目標(biāo)框信息表達(dá)格式,是因?yàn)閮煞N格式會(huì)分別在后續(xù)不同場(chǎng)景下更加便于計(jì)算。
兩種格式互相轉(zhuǎn)換的實(shí)現(xiàn)在utils.py中,代碼也非常簡(jiǎn)單:
def xy_to_cxcy(xy):
"""
Convert bounding boxes from boundary coordinates (x_min, y_min, x_max, y_max) to center-size coordinates (c_x, c_y, w, h).
:param xy: bounding boxes in boundary coordinates, a tensor of size (n_boxes, 4)
:return: bounding boxes in center-size coordinates, a tensor of size (n_boxes, 4)
"""
return torch.cat([(xy[:, 2:] + xy[:, :2]) / 2, # c_x, c_y
xy[:, 2:] - xy[:, :2]], 1) # w, h
def cxcy_to_xy(cxcy):
"""
Convert bounding boxes from center-size coordinates (c_x, c_y, w, h) to boundary coordinates (x_min, y_min, x_max, y_max).
:param cxcy: bounding boxes in center-size coordinates, a tensor of size (n_boxes, 4)
:return: bounding boxes in boundary coordinates, a tensor of size (n_boxes, 4)
"""
return torch.cat([cxcy[:, :2] - (cxcy[:, 2:] / 2), # x_min, y_min
cxcy[:, :2] + (cxcy[:, 2:] / 2)], 1) # x_max, y_max
用torch.cat()將兩個(gè)形狀為(n,2)的tensor在第一維度拼接成(n,4)。
4. 交并比(IoU)
在目標(biāo)檢測(cè)任務(wù)中,關(guān)于IOU的計(jì)算貫穿整個(gè)模型的訓(xùn)練測(cè)試和評(píng)價(jià)過(guò)程,是非常非常重要的一個(gè)概念,其目的是用來(lái)衡量?jī)蓚€(gè)目標(biāo)框的重疊程度。
IoU的全稱是交并比(Intersection over Union),表示兩個(gè)目標(biāo)框的交集占其并集的比例。下圖為IOU計(jì)算示意圖:

圖中可以看到,分子中黃色區(qū)域?yàn)榧tbbox和綠bbox的交集,分母中黃+紅+綠區(qū)域?yàn)榧tbbox和綠bbox的并集,兩者之比即為iou。
那么具體怎么去計(jì)算呢?這里給出計(jì)算流程的簡(jiǎn)述:
首先獲取兩個(gè)框的坐標(biāo),紅框坐標(biāo): 左上(red_x1, red_y1), 右下(red_x2, red_y2),綠框坐標(biāo): 左上(green_x1, green_y1),右下(green_x2, green_y2) 計(jì)算兩個(gè)框左上點(diǎn)的坐標(biāo)最大值:(max(red_x1, green_x1), max(red_y1, green_y1)), 和右下點(diǎn)坐標(biāo)最小值:(min(red_x2, green_x2), min(red_y2, green_y2)) 利用2算出的信息計(jì)算黃框面積:yellow_area 計(jì)算紅綠框的面積:red_area 和 green_area iou = yellow_area / (red_area + green_area - yellow_area)
如果文字表述的不夠清晰,就再看下代碼:
def find_intersection(set_1, set_2):
"""
Find the intersection of every box combination between two sets of boxes that are in boundary coordinates.
:param set_1: set 1, a tensor of dimensions (n1, 4)
:param set_2: set 2, a tensor of dimensions (n2, 4)
:return: intersection of each of the boxes in set 1 with respect to each of the boxes in set 2, a tensor of dimensions (n1, n2)
"""
# PyTorch auto-broadcasts singleton dimensions
lower_bounds = torch.max(set_1[:, :2].unsqueeze(1), set_2[:, :2].unsqueeze(0)) # (n1, n2, 2)
upper_bounds = torch.min(set_1[:, 2:].unsqueeze(1), set_2[:, 2:].unsqueeze(0)) # (n1, n2, 2)
intersection_dims = torch.clamp(upper_bounds - lower_bounds, min=0) # (n1, n2, 2)
return intersection_dims[:, :, 0] * intersection_dims[:, :, 1] # (n1, n2)
def find_jaccard_overlap(set_1, set_2):
"""
Find the Jaccard Overlap (IoU) of every box combination between two sets of boxes that are in boundary coordinates.
:param set_1: set 1, a tensor of dimensions (n1, 4)
:param set_2: set 2, a tensor of dimensions (n2, 4)
:return: Jaccard Overlap of each of the boxes in set 1 with respect to each of the boxes in set 2, a tensor of dimensions (n1, n2)
"""
# Find intersections
intersection = find_intersection(set_1, set_2) # (n1, n2)
# Find areas of each box in both sets
areas_set_1 = (set_1[:, 2] - set_1[:, 0]) * (set_1[:, 3] - set_1[:, 1]) # (n1)
areas_set_2 = (set_2[:, 2] - set_2[:, 0]) * (set_2[:, 3] - set_2[:, 1]) # (n2)
# Find the union
# PyTorch auto-broadcasts singleton dimensions
union = areas_set_1.unsqueeze(1) + areas_set_2.unsqueeze(0) - intersection # (n1, n2)
return intersection / union # (n1, n2)
以上代碼位于utils.py腳本的find_intersection和find_jaccard_overlap。
函數(shù)
find_intersectionfind_intersection(set_1, set_2)是求形狀為 (n1,4) 和 (n2,4) 的boxes的交集的面積。set_1[:, :2]的形狀為(n1,2),后面加上.unsqueeze(1),形狀變?yōu)?n1,1,2)。同理set_2[:, :2].unsqueeze(0),形狀為(1,n2,2)。(n1,1,2)和(1,n2,2),作了torch.max,有廣播存在,(n1,1,2)變成(n1,n2,2) ,(1,n2,2)也變成(n1,n2,2)。因此得到了形狀為(n1,n2,2)的框的左上角坐標(biāo) 那個(gè)2 就是儲(chǔ)存了x1,y1。
torch.clamp()是將函數(shù)限制在最大值和最小值范圍內(nèi),如果超過(guò)就變成那個(gè)最大值或者最小值。這里min=0,意思是如果面積小于0,那么面積取0(排除異常)。函數(shù)
find_jaccard_overlap計(jì)算iou,交集/并集,最后union計(jì)算, ?升維 (n1)->(n1,1) ? ?、 ?(n2)->(1,n2) ? 、 接下去相加,廣播成(n1,n2),減去一個(gè)(n1,n2)的交集面積,得到并集面積。
5. 小結(jié)
本小節(jié)首先介紹了目標(biāo)檢測(cè)的問(wèn)題背景,隨后分析了一個(gè)實(shí)現(xiàn)目標(biāo)檢測(cè)的解決思路,這也是眾多經(jīng)典檢測(cè)網(wǎng)絡(luò)所采用的思路(即先確立眾多候選框,再對(duì)候選框進(jìn)行分類和微調(diào))。最后介紹了bbox和IoU這兩個(gè)目標(biāo)檢測(cè)相關(guān)的基本概念。
下一篇將會(huì)從數(shù)據(jù)入手,介紹下目標(biāo)檢測(cè)領(lǐng)域最常見(jiàn)的一個(gè)數(shù)據(jù)集VOC,以及數(shù)據(jù)讀取相關(guān)的代碼。
往期精彩回顧
獲取本站知識(shí)星球優(yōu)惠券,復(fù)制鏈接直接打開(kāi):
https://t.zsxq.com/qFiUFMV
本站qq群704220115。
加入微信群請(qǐng)掃碼:
