<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          目標(biāo)檢測(cè)漲點(diǎn)方法:目標(biāo)框加權(quán)融合-Weighted Boxes Fusion(源碼分享)

          共 13396字,需瀏覽 27分鐘

           ·

          2021-05-01 02:20

          今天我們介紹優(yōu)化目標(biāo)檢測(cè)的SOTA目標(biāo)框加權(quán)融合-Weighted Boxes Fusion以及使用WBF快速實(shí)現(xiàn)目標(biāo)檢測(cè)漲點(diǎn)的方法。使用""
          論文:Weighted boxes fusion: Ensembling boxes from different object detection models
          論文鏈接:https://arxiv.org/pdf/1910.13302.pdf
          代碼鏈接:https://github.com/ZFTurbo/Weighted-Boxes-Fusion



          01

          NMS&WBF


          1.1 NMS

          NMS消除冗余的邊界框的流程如下:

          • 根據(jù)置信度得分進(jìn)行排序

          • 選擇置信度最高的比邊界框添加到最終輸出列表中,將其從邊界框列表中刪除

          • 計(jì)算所有邊界框的面積

          • 計(jì)算置信度最高的邊界框與其它候選框的IoU。

          • 刪除IoU大于閾值的邊界框

          • 重復(fù)上述過(guò)程,直至邊界框列表為空。


          NMS方法雖然簡(jiǎn)單有效,但在更高的目標(biāo)檢測(cè)需求下,也存在如下缺點(diǎn):

          • 密集重疊場(chǎng)景造成誤過(guò)濾:將得分較低的邊框強(qiáng)制性地去掉,如果物體出現(xiàn)較為密集時(shí),本身屬于兩個(gè)物體的邊框,其中得分較低的也有可能被抑制掉,降低了模型的召回率。

          • 速度:NMS的實(shí)現(xiàn)存在較多的循環(huán)步驟,GPU的并行化實(shí)現(xiàn)不是特別容易,尤其是預(yù)測(cè)框較多時(shí),耗時(shí)較多。

          • 框的置信度和分類(lèi)的置信度并不是完全對(duì)齊的,NMS簡(jiǎn)單地將得分作為一個(gè)邊框的置信度,但在一些情況下,得分高的邊框不一定位置更準(zhǔn)



          1.2 WBF

          WBF采用的融合框的置信度為所有形成他的邊界框的平均置信度,而其坐標(biāo)也是所有形成他的邊界框的坐標(biāo)的加權(quán)和,權(quán)重為相應(yīng)的邊界框的置信度。也就是置信度越高的邊界框?qū)τ谌诤线吔缈虻淖鴺?biāo)做出的貢獻(xiàn)越大。

          下面是WBF的算法步驟:

          1. 每個(gè)模型的每個(gè)預(yù)測(cè)框都添加到List B,并將此列表按置信度得分C降序排列

          2. 建立空List L 和 F(用于融合的)

          3. 循環(huán)遍歷B,并在F中找到于之匹配的box(同一類(lèi)別MIOU > 0.55)

          4. 如果 step3 中沒(méi)有找到匹配的box 就將這個(gè)框加到L和F的尾部

          5. 如果 step3 中找到了匹配的box 就將這個(gè)框加到L,加入的位置是box在F中匹配框的Index.

          6. L中每個(gè)位置可能有多個(gè)框,需要根據(jù)這多個(gè)框更新對(duì)應(yīng)F[index]的值。使用所有的在L[index]的T個(gè)邊界框重新計(jì)算F [index] 中邊界框的坐標(biāo)和置信度得分:

                     (1)

          7.當(dāng)B中的所有邊界框都被處理了,對(duì)List F的置信度得分重新估計(jì):乘上列表中的邊界框個(gè)數(shù),在除以模型數(shù)目。也就是當(dāng)簇中邊界框數(shù)目少的時(shí)候,則認(rèn)為有較少的邊界框預(yù)測(cè)該融合框,也就是該融合框的置信度應(yīng)該降低:   
          WBF效果演示圖如下:

                  



          02

          WBF快速實(shí)現(xiàn)目標(biāo)檢測(cè)漲點(diǎn)的方法


          WBF可以實(shí)現(xiàn)目標(biāo)框加權(quán)融合消除冗余的邊界框,基于它的特點(diǎn)我們可以使用WBF快速實(shí)現(xiàn)目標(biāo)檢測(cè)漲點(diǎn),主要方法有以下幾種:

              1、用WBF替代檢測(cè)網(wǎng)絡(luò)中的NMS方法

              2、利用WBF實(shí)現(xiàn)測(cè)試增強(qiáng)TTA結(jié)果的融合 

              3、利用WBF實(shí)現(xiàn)目標(biāo)檢測(cè)的多模型集成

          接下來(lái)我們將以YOLOV5代碼進(jìn)行舉例,完整使用代碼可以關(guān)注公眾號(hào)后臺(tái)回復(fù)"WBF"獲取

          2.1 用WBF替代檢測(cè)網(wǎng)絡(luò)中的NMS方法

          1、首先我們需要安裝WBF的庫(kù),可以使用兩種方式
          WBF Github :https://github.com/ZFTurbo/Weighted-Boxes-Fusion
          pip 在線安裝:
          pip install ensemble-boxes
          如果直接pip install ensemble-boxes報(bào)錯(cuò)的話,可以嘗試:
          pip install --upgrade pippip install --upgrade setuptools
          pip 離線安裝:在下載github上下載好
          pip install --no-deps '../input/weightedboxesfusion/'

          2、調(diào)用WBF的庫(kù):
          from ensemble_boxes import *
          3、調(diào)用weighted_boxes_fusion函數(shù)替代nms, 實(shí)現(xiàn)對(duì)冗余的框的消除
          boxes, scores, labels = weighted_boxes_fusion(boxes_list, scores_list, labels_list, weights=weights, iou_thr=iou_thr, skip_box_thr=skip_box_thr)
          以YOLOV5為例,原本檢測(cè)時(shí)使用NMS的代碼如下:
           pred = model(img, augment=opt.augment)[0] pred = non_max_suppression(pred, opt.conf_thres, opt.iou_thres, classes=opt.classes, agnostic=opt.agnostic_nms)
          可以看到y(tǒng)olo輸出結(jié)果(score,lable, box)全部包含在pred當(dāng)中,由于WBF需要boxes_list, scores_list, labels_list等列表形式
          因此我們需要對(duì)pred轉(zhuǎn)變?yōu)楹骲oxes_list, scores_list, labels_list再輸入weighted_boxes_fusion中:
          由于代碼過(guò)長(zhǎng)無(wú)法完整展示,下面只展示思路的代碼示例(無(wú)法直接執(zhí)行),需要完整使用代碼可以關(guān)注公眾號(hào)后臺(tái)回復(fù)"WBF"獲取
          pred = model(img, augment=False)[0]boxes = []scores = []clses = []pred = non_max_suppression(pred, opt.conf_thres, opt.iou_thres, classes=opt.classes, agnostic=opt.agnostic_nms)##將pred轉(zhuǎn)變成 boxes_list, scores_list, labels_listfor i, det in enumerate(pred):  # detections per image    if det is not None and len(det):        # Rescale boxes from img_size to im0 size        det[:, :4] = scale_coords(img.shape[2:], det[:, :4], im0.shape).round()        for *xyxy, conf, cls in det:            boxes.append([int(xyxy[0]), int(xyxy[1]), int(xyxy[2]), int(xyxy[3])])            scores.append(conf)            clses.append(cls)  boxes = np.array(boxes)  scores = np.array(scores)  clses = np.array(clses )    #使用weighted_boxes_fusion加權(quán)融合框   def run_wbf(boxes, scores, clses, image_size=1024, iou_thr=0.5, skip_box_thr=0.7, weights=None):    boxes = [box/(image_size) for box in boxes]    boxes, scores, labels = weighted_boxes_fusion(boxes, scores, clses, weights=None, iou_thr=iou_thr, skip_box_thr=skip_box_thr)    boxes = boxes*(image_size)    return boxes, scores, labels      pred_boxes, scores, labels = run_wbf(boxes , scores , clses , image_size, iou_thr=iou_thr, skip_box_thr=skip_box_thr)  


          2.2 利用WBF實(shí)現(xiàn)測(cè)試增強(qiáng)TTA結(jié)果的融合 
          測(cè)試時(shí)增強(qiáng)(test time augmentation, TTA)可將準(zhǔn)確率提高若干個(gè)百分點(diǎn),。這里會(huì)為原始圖像造出多個(gè)不同版本,包括不同區(qū)域裁剪和更改縮放程度等,并將它們輸入到模型中;然后對(duì)多個(gè)版本進(jìn)行計(jì)算得到平均輸出,作為圖像的最終輸出分?jǐn)?shù)。如下圖所示:

          以YOLOV5為例,代碼如下:
          由于代碼過(guò)長(zhǎng)無(wú)法完整展示,下面只展示思路的代碼示例(無(wú)法直接執(zhí)行會(huì)),需要完整使用代碼可以關(guān)注公眾號(hào)后臺(tái)回復(fù)"WBF"獲取
          (1)測(cè)試圖片旋轉(zhuǎn)增強(qiáng)的函數(shù),以及旋轉(zhuǎn)后圖片預(yù)測(cè)結(jié)果box復(fù)原的函數(shù)
          def TTAImage(image, index):    image1 = image.copy()    if index==0:         rotated_image = cv2.rotate(image1, cv2.ROTATE_90_CLOCKWISE)        return rotated_image    elif index==1:        rotated_image2 = cv2.rotate(image1, cv2.ROTATE_90_CLOCKWISE)        rotated_image2 = cv2.rotate(rotated_image2, cv2.ROTATE_90_CLOCKWISE)        return rotated_image2    elif index==2:        rotated_image3 = cv2.rotate(image1, cv2.ROTATE_90_CLOCKWISE)        rotated_image3 = cv2.rotate(rotated_image3, cv2.ROTATE_90_CLOCKWISE)        rotated_image3 = cv2.rotate(rotated_image3, cv2.ROTATE_90_CLOCKWISE)        return rotated_image3    elif index == 3:        return image1    def rotBoxes90(boxes, im_w, im_h):    ret_boxes =[]    for box in boxes:        x1, y1, x2, y2 = box        x1, y1, x2, y2 = x1-im_w//2, im_h//2 - y1, x2-im_w//2, im_h//2 - y2        x1, y1, x2, y2 = y1, -x1, y2, -x2        x1, y1, x2, y2 = int(x1+im_w//2), int(im_h//2 - y1), int(x2+im_w//2), int(im_h//2 - y2)        x1a, y1a, x2a, y2a = min(x1, x2), min(y1, y2), max(x1, x2), max(y1, y2)        ret_boxes.append([x1a, y1a, x2a, y2a])    return np.array(ret_boxes)
          (2)測(cè)試圖片旋轉(zhuǎn),以及旋轉(zhuǎn)后圖片預(yù)測(cè)結(jié)果復(fù)原
          im_w, im_h = im01.shape[:2]enboxes = []enscores = []for i in range(4):    im0 = TTAImage(im01, i)    boxes, scores = detect1Image(im0, imgsz, model, device, conf_thres, iou_thres)    for _ in range(3-i):        boxes = rotBoxes90(boxes, im_w, im_h)            if 1: #i<3:        enboxes.append(boxes)        enscores.append(scores)     boxes, scores = detect1Image(im01, imgsz, model, device, conf_thres, iou_thres)    enboxes.append(boxes)    enscores.append(scores)
          def run_wbf(boxes, scores, image_size=1023, iou_thr=0.5, skip_box_thr=0.7, weights=None): labels = [np.zeros(score.shape[0]) for score in scores] boxes = [box/(image_size) for box in boxes]    boxes, scores, labels = weighted_boxes_fusion(boxes, scores, labels, weights=None, iou_thr=iou_thr, skip_box_thr=skip_box_thr) boxes = boxes*(image_size) return boxes, scores, labels

          boxes, scores, labels = run_wbf(enboxes, enscores, image_size = im_w, iou_thr=0.6, skip_box_thr=0.5)



          2.3 利用WBF實(shí)現(xiàn)目標(biāo)檢測(cè)的多模型集成
          多模型集成是指可以使用多種模型進(jìn)行預(yù)測(cè),最后結(jié)合多個(gè)模型的預(yù)測(cè)結(jié)果,其中我們可以選擇多種不同網(wǎng)絡(luò)的模型進(jìn)行集成,也可以使用同種網(wǎng)絡(luò)的不同訓(xùn)練方式的模型進(jìn)行集成。
          代碼演示如下:由于代碼過(guò)長(zhǎng)無(wú)法完整展示,下面只展示思路的代碼示例(無(wú)法直接執(zhí)行會(huì)),需要完整使用代碼可以關(guān)注公眾號(hào)后臺(tái)回復(fù)"WBF"獲取
          weights = 'weights/best.pt'weights1 = "../input/otherweight/best_yolov5x_fold0.pt"
          # load modelmodel = torch.load(weights, map_location=device)['model'].float() # load to FP32model.to(device).eval()
          # Load model 1model1 = torch.load(weights1, map_location=device)['model'].float() # load to FP32model1.to(device).eval()
          boxes, scores = detect1Image(im0, imgsz, model, device, conf_thres, iou_thres)boxes, scores = detect1Image(im0, imgsz, model1, device, conf_thres, iou_thres)boxes, scores, labels = weighted_boxes_fusion(boxes, scores, labels, weights=None, iou_thr=iou_thr, skip_box_thr=skip_box_thr)



          03

          總結(jié)


          本文介紹了NMS和WBF消除冗余的邊界框的方法,并且比較了它們的有缺點(diǎn),發(fā)現(xiàn)WBF緩解NMS在密集重疊場(chǎng)景容易造成誤過(guò)濾的問(wèn)題。然后還介紹了使用WBF在目標(biāo)檢測(cè)中快速漲點(diǎn)的方法。


          ?------------------------------------------------


          雙一流高校研究生團(tuán)隊(duì)創(chuàng)建 ↓

          專(zhuān)注于計(jì)算機(jī)視覺(jué)原創(chuàng)并分享相關(guān)知識(shí) ?


          瀏覽 89
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

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

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  国产精品h | a√天堂中文字幕在线 | 日本激情视频 | 亚洲精品成人无码AV在线 | avtt在线看 |