1. <strong id="7actg"></strong>
    2. <table id="7actg"></table>

    3. <address id="7actg"></address>
      <address id="7actg"></address>
      1. <object id="7actg"><tt id="7actg"></tt></object>

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

        共 5470字,需瀏覽 11分鐘

         ·

        2020-12-26 17:25

        ↑↑↑關(guān)注后"星標(biāo)"Datawhale
        每日干貨?&?每月組隊(duì)學(xué)習(xí),不錯(cuò)過
        ?Datawhale干貨?
        作者:徐和鼎,浙江大學(xué),Datawhale優(yōu)秀學(xué)習(xí)者
        本文講解了目標(biāo)檢測的基本概念,分析了實(shí)現(xiàn)目標(biāo)檢測的常用思路。下一篇將介紹目標(biāo)檢測經(jīng)典數(shù)據(jù)集—VOC數(shù)據(jù)集的基本信息,和對VOC數(shù)據(jù)集進(jìn)行處理的方法。

        一、目標(biāo)檢測基本概念

        1. 什么是目標(biāo)檢測

        目標(biāo)檢測是計(jì)算機(jī)視覺中的一個(gè)重要任務(wù),近年來傳統(tǒng)目標(biāo)檢測方法已經(jīng)難以滿足人們對目標(biāo)檢測效果的要求,隨著深度學(xué)習(xí)在計(jì)算機(jī)視覺任務(wù)上取得的巨大進(jìn)展,目前基于深度學(xué)習(xí)的目標(biāo)檢測算法已經(jīng)成為主流。

        相比較于基于深度學(xué)習(xí)的圖像分類任務(wù),目標(biāo)檢測任務(wù)更具難度,具體區(qū)別如下圖所示。

        圖像分類:只需要判斷輸入的圖像中是否包含感興趣物體。

        目標(biāo)檢測:需要在識(shí)別出圖片中目標(biāo)類別的基礎(chǔ)上,還要精確定位到目標(biāo)的具體位置,并用外接矩形框標(biāo)出。

        分類和目標(biāo)檢測任務(wù)示意圖

        2. 目標(biāo)檢測常用思路

        自2012年Alex Krizhevsky憑借Alex在ImageNet圖像分類挑戰(zhàn)賽中拿下冠軍之后,深度學(xué)習(xí)在圖像識(shí)別尤其是圖像分類領(lǐng)域開始大放異彩,大眾的視野也重新回到深度神經(jīng)網(wǎng)絡(luò)中。緊接著,不斷有更深更復(fù)雜的網(wǎng)絡(luò)出現(xiàn),一再刷新ImageNet圖像分類比賽的記錄。

        大家發(fā)現(xiàn),通過合理的構(gòu)造,神經(jīng)網(wǎng)絡(luò)可以用來預(yù)測各種各樣的實(shí)際問題。于是人們開始了基于CNN的目標(biāo)檢測研究, 但是隨著進(jìn)一步的探索大家發(fā)現(xiàn),似乎CNN并不善于直接預(yù)測坐標(biāo)信息。并且一幅圖像中可能出現(xiàn)的物體個(gè)數(shù)也是不定的,模型如何構(gòu)建也比較棘手。

        因此,人們就想,如果知道了圖中某個(gè)位置存在物體,再將對應(yīng)的局部區(qū)域送入到分類網(wǎng)絡(luò)中去進(jìn)行判別,那我不就可以知道圖像中每個(gè)物體的位置和類別了嗎?

        但是,怎么樣才能知道每個(gè)物體的位置呢?顯然我們是沒辦法知道的,但是我們可以去猜??!所謂猜,其實(shí)就是通過滑窗的方式,羅列圖中各種可能的區(qū)域,一個(gè)個(gè)去試,分別送入到分類網(wǎng)絡(luò)進(jìn)行分類得到其類別,同時(shí)我們會(huì)對當(dāng)前的邊界框進(jìn)行微調(diào),這樣對于圖像中每個(gè)區(qū)域都能得到(class,x1,y1,x2,y2)五個(gè)屬性,匯總后最終就得到了圖中物體的類別和坐標(biāo)信息。

        總結(jié)一下我們的這種方案思路:先確立眾多候選框,再對候選框進(jìn)行分類和微調(diào)。

        觀察下圖,更形象的理解下這種思想:

        從分類角度去看目標(biāo)檢測

        3. 目標(biāo)框定義方式

        任何圖像任務(wù)的訓(xùn)練數(shù)據(jù)都要包括兩項(xiàng),圖片和真實(shí)標(biāo)簽信息,通常叫做GT。

        圖像分類中,標(biāo)簽信息是類別。目標(biāo)檢測的標(biāo)簽信息除了類別label以外,需要同時(shí)包含目標(biāo)的位置信息,也就是目標(biāo)的外接矩形框bounding box。

        用來表達(dá)bbox的格式通常有兩種,(x1, y1, x2, y2) 和 (c_x, c_y, w, h) ,如圖所示:

        目標(biāo)框定義方式

        之所以使用兩種不同的目標(biāo)框信息表達(dá)格式,是因?yàn)閮煞N格式會(huì)分別在后續(xù)不同場景下更加便于計(jì)算。

        兩種格式互相轉(zhuǎn)換的實(shí)現(xiàn)在utils.py中,代碼也非常簡單:

        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)檢測任務(wù)中,關(guān)于IOU的計(jì)算貫穿整個(gè)模型的訓(xùn)練測試和評(píng)價(jià)過程,是非常非常重要的一個(gè)概念,其目的是用來衡量兩個(gè)目標(biāo)框的重疊程度。

        IoU的全稱是交并比(Intersection over Union),表示兩個(gè)目標(biāo)框的交集占其并集的比例。下圖為IOU計(jì)算示意圖:

        IOU計(jì)算示意圖

        圖中可以看到,分子中黃色區(qū)域?yàn)榧tbbox和綠bbox的交集,分母中黃+紅+綠區(qū)域?yàn)榧tbbox和綠bbox的并集,兩者之比即為iou。

        那么具體怎么去計(jì)算呢?這里給出計(jì)算流程的簡述:

        • 首先獲取兩個(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),如果超過就變成那個(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)檢測的問題背景,隨后分析了一個(gè)實(shí)現(xiàn)目標(biāo)檢測的解決思路,這也是眾多經(jīng)典檢測網(wǎng)絡(luò)所采用的思路(即先確立眾多候選框,再對候選框進(jìn)行分類和微調(diào))。最后介紹了bbox和IoU這兩個(gè)目標(biāo)檢測相關(guān)的基本概念。

        下一篇將會(huì)從數(shù)據(jù)入手,介紹下目標(biāo)檢測領(lǐng)域最常見的一個(gè)數(shù)據(jù)集VOC,以及數(shù)據(jù)讀取相關(guān)的代碼。


        “干貨學(xué)習(xí),點(diǎn)三連
        瀏覽 106
        點(diǎn)贊
        評(píng)論
        收藏
        分享

        手機(jī)掃一掃分享

        分享
        舉報(bào)
        評(píng)論
        圖片
        表情
        推薦
        點(diǎn)贊
        評(píng)論
        收藏
        分享

        手機(jī)掃一掃分享

        分享
        舉報(bào)
        1. <strong id="7actg"></strong>
        2. <table id="7actg"></table>

        3. <address id="7actg"></address>
          <address id="7actg"></address>
          1. <object id="7actg"><tt id="7actg"></tt></object>
            A片特级黄色免费电影网站 | 爆操小骚逼视频 | 激情无码人妻 | 国产九一网站免费观看 | 男生的小鸡鸡插入女生的小鸡鸡 | 仙女棒坐着使用教程 | 电家庭影院午夜29332 | 少妇愉情理伦片法国4级 | 美女扒开裤子让男人桶 | 2022黄片|