1. 程式人生 > >目標定位和檢測系列:交併比(IOU)和非極大值抑制(NMS)的python與C/C++實現

目標定位和檢測系列:交併比(IOU)和非極大值抑制(NMS)的python與C/C++實現

Python實現

交併比(Intersection over Union)和非極大值抑制是(Non-Maximum Suppression)是目標檢測任務中非常重要的兩個概念。例如在用訓練好的模型進行測試時,網路會預測出一系列的候選框。這時候我們會用NMS來移除一些多餘的候選框。即移除一些IOU值大於某個閾值的框。然後在剩下的候選框中,分別計算與ground truth的IOU值,通常會規定當候選框和ground truth的IOU值大於0.5時,認為檢測正確。下面我們分別用python實現IOU和NMS。

交併比IOU

 
如上圖所示,IOU值定位為兩個矩形框面積的交集和並集的比值。即: 
IOU=A∩BA∪B

import numpy as np
def compute_iou(box1, box2, wh=False):
    """
    compute the iou of two boxes.
    Args:
        box1, box2: [xmin, ymin, xmax, ymax] (wh=False) or [xcenter, ycenter, w, h] (wh=True)
        wh: the format of coordinate.
    Return:
        iou: iou of box1 and box2.
    """
    if wh == False:
        xmin1, ymin1, xmax1, ymax1 = box1
        xmin2, ymin2, xmax2, ymax2 = box2
    else:
        xmin1, ymin1 = int(box1[0]-box1[2]/2.0), int(box1[1]-box1[3]/2.0)
        xmax1, ymax1 = int(box1[0]+box1[2]/2.0), int(box1[1]+box1[3]/2.0)
        xmin2, ymin2 = int(box2[0]-box2[2]/2.0), int(box2[1]-box2[3]/2.0)
        xmax2, ymax2 = int(box2[0]+box2[2]/2.0), int(box2[1]+box2[3]/2.0)

    ## 獲取矩形框交集對應的左上角和右下角的座標(intersection)
    xx1 = np.max([xmin1, xmin2])
    yy1 = np.max([ymin1, ymin2])
    xx2 = np.min([xmax1, xmax2])
    yy2 = np.min([ymax1, ymax2])

    ## 計算兩個矩形框面積
    area1 = (xmax1-xmin1) * (ymax1-ymin1) 
    area2 = (xmax2-xmin2) * (ymax2-ymin2)

    inter_area = (np.max([0, xx2-xx1])) * (np.max([0, yy2-yy1])) #計算交集面積
    iou = inter_area / (area1+area2-inter_area+1e-6) #計算交併比

    return iou

非極大值抑制(NMS)

NMS的演算法步驟如下:

# INPUT:所有預測出的bounding box (bbx)資訊(座標和置信度confidence), IOU閾值(大於該閾值的bbx將被移除)
for object in all objects:
    (1) 獲取當前目標類別下所有bbx的資訊
    (2) 將bbx按照confidence從高到低排序,並記錄當前confidence最大的bbx
    (3) 計算最大confidence對應的bbx與剩下所有的bbx的IOU,移除所有大於IOU閾值的bbx
    (4) 對剩下的bbx,迴圈執行(2)和(3)直到所有的bbx均滿足要求(即不能再移除bbx)
需要注意的是,NMS是對所有的類別分別執行的。舉個栗子,假設最後預測出的矩形框有2類(分別為cup, pen),在NMS之前,每個類別可能都會有不只一個bbx被預測出來,這個時候我們需要對這兩個類別分別執行一次NMS過程。 
我們用python編寫NMS程式碼,假設對於一張圖片,所有的bbx資訊已經儲存在一個字典中,儲存形式如下:
 

predicts_dict: {"cup": [[x1_1, y1_1, x2_1, y2_1, scores1], [x1_2, y1_2, x2_2, y2_2, scores2], ...], "pen": [[x1_1, y1_1, x2_1, y2_1, scores1], [x1_2, y1_2, x2_2, y2_2, scores2], ...]}.

即目標的位置和置信度用列表儲存,每個列表中的一個子列表代表一個bbx資訊。詳細的程式碼如下:

def non_max_suppress(predicts_dict, threshold=0.2):
    """
    implement non-maximum supression on predict bounding boxes.
    Args:
        predicts_dict: {"stick": [[x1, y1, x2, y2, scores1], [...]]}.
        threshhold: iou threshold
    Return:
        predicts_dict processed by non-maximum suppression
    """
    for object_name, bbox in predicts_dict.items():   #對每一個類別的目標分別進行NMS
        bbox_array = np.array(bbox, dtype=np.float)

        ## 獲取當前目標類別下所有矩形框(bounding box,下面簡稱bbx)的座標和confidence,並計算所有bbx的面積
        x1, y1, x2, y2, scores = bbox_array[:,0], bbox_array[:,1], bbox_array[:,2], bbox_array[:,3], bbox_array[:,4]
        areas = (x2-x1+1) * (y2-y1+1)
        #print "areas shape = ", areas.shape

        ## 對當前類別下所有的bbx的confidence進行從高到低排序(order儲存索引資訊)
        order = scores.argsort()[::-1]
        print "order = ", order
        keep = [] #用來存放最終保留的bbx的索引資訊

        ## 依次從按confidence從高到低遍歷bbx,移除所有與該矩形框的IOU值大於threshold的矩形框
        while order.size > 0:
            i = order[0]
            keep.append(i) #保留當前最大confidence對應的bbx索引

            ## 獲取所有與當前bbx的交集對應的左上角和右下角座標,並計算IOU(注意這裡是同時計算一個bbx與其他所有bbx的IOU)
            xx1 = np.maximum(x1[i], x1[order[1:]]) #當order.size=1時,下面的計算結果都為np.array([]),不影響最終結果
            yy1 = np.maximum(y1[i], y1[order[1:]])
            xx2 = np.minimum(x2[i], x2[order[1:]])
            yy2 = np.minimum(y2[i], y2[order[1:]])
            inter = np.maximum(0.0, xx2-xx1+1) * np.maximum(0.0, yy2-yy1+1)
            iou = inter/(areas[i]+areas[order[1:]]-inter)
            print "iou =", iou

            print np.where(iou<=threshold) #輸出沒有被移除的bbx索引(相對於iou向量的索引)
            indexs = np.where(iou<=threshold)[0] + 1 #獲取保留下來的索引(因為沒有計算與自身的IOU,所以索引相差1,需要加上)
            print "indexs = ", type(indexs)
            order = order[indexs] #更新保留下來的索引
            print "order = ", order
        bbox = bbox_array[keep]
        predicts_dict[object_name] = bbox.tolist()
        predicts_dict = predicts_dict
    return predicts_dict

--------------------- 
原文:https://blog.csdn.net/sinat_34474705/article/details/80045294 

========分割線==========
IOU&NMS C++實現

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;

typedef struct Bbox
{
    int x;
    int y;
    int w;
    int h;
    float score;

}Bbox;

bool sort_score(Bbox box1,Bbox box2)
{
    return (box1.score > box2.score);

}

float iou(Bbox box1,Bbox box2)
{
    int x1 = std::max(box1.x,box2.x);
    int y1 = std::max(box1.y,box2.y);
    int x2 = std::min((box1.x + box1.w),(box2.x + box2.w));
    int y2 = std::min((box1.y + box1.h),(box2.y + box2.h));
    float over_area = (x2 - x1) * (y2 - y1);
    float iou = over_area/(box1.w * box1.h + box2.w * box2.h-over_area);
    return iou;
}
//方法1
vector<Bbox> nms(std::vector<Bbox>&vec_boxs,float threshold)
{
    std::sort(vec_boxs.begin(),vec_boxs.end(),sort_score);
    std::vector<Bbox>del(vec_boxs.size(),false);
    for(int i =0; i<vec_boxs.size();i++)
    {
        for (int j =0;j<vec_boxs.size();j++)
        {
            float iou_value =iou(vec_boxs[i],vec_boxs[j]);
            if(iou_value>threshold)
            {
                del[j]=true;
            }
        }
    }
     std::vector<Bbox>results;
    for(const auto i :del)
    {
        if(!del[i]) results.push_back(vec_box[i]);
    }
    return results;
}
//方法2  這種執行效率更高
vector<Bbox> nms(std::vector<Bbox>&vec_boxs,float threshold)
{
    vector<Bbox>results;
    while(vec_boxs.size() > 0)
    {
        std::sort(vec_boxs.begin(),vec_boxs.end(),sort_score);
        results.push_back(vec_boxs[0]);
        for(int i =0;i <vec_boxs.size()-1;i++)
        {
            float iou_value =iou(vec_boxs[0],vec_boxs[i+1]);
            if (iou_value >threshold)
            {
                vec_boxs.erase(vec_boxs[i+1]);
            }
        }
        vec_boxs.erase(vec_boxs[0]);

    }
}
--------------------- 
原文:https://blog.csdn.net/qq_32582681/article/details/81352758 

 

原理:
對於Bounding Box的列表B及其對應的置信度S,選擇具有最大score的檢測框M,將其從B集合中移除並加入到最終的檢測結果D中.通常將B中剩餘檢測框中與M的IoU大於閾值Nt的框從B中移除.重複這個過程,直到B為空

實現過程:

就像上面的圖片一樣,定位一個車輛,最後演算法就找出了一堆的方框,我們需要判別哪些矩形框是沒用的。

非極大值抑制:先假設有6個候選框,根據分類器類別分類概率做排序,從小到大分別屬於車輛的概率分別為A、B、C、D、E、F。

1、從最大概率矩形框F開始,分別判斷A~E與F的重疊度IOU是否大於某個設定的閾值;

2、假設B、D與F的重疊度超過閾值,那麼就扔掉B、D;並標記第一個矩形框F,是我們保留下來的。

3、從剩下的矩形框A、C、E中,選擇概率最大的E,然後判斷E與A、C的重疊度,重疊度大於一定的閾值,那麼就扔掉;並標記E是我們保留下來的第二個矩形框。

4、一直重複這個過程,找到所有曾經被保留下來的矩形框。

//升序排列
bool cmpScore(Bbox lsh, Bbox rsh) {
	if (lsh.score < rsh.score)
		return true;
	else
		return false;
}

 
void nms(vector<Bbox> &boundingBox_, const float overlap_threshold, string modelname = "Union"){
 
    if(boundingBox_.empty()){
        return;
    }
    //對各個候選框根據score的大小進行升序排列
    sort(boundingBox_.begin(), boundingBox_.end(), cmpScore);
    float IOU = 0;
    float maxX = 0;
    float maxY = 0;
    float minX = 0;
    float minY = 0;
    vector<int> vPick;
    int nPick = 0;
    multimap<float, int> vScores;   //存放升序排列後的score和對應的序號
    const int num_boxes = boundingBox_.size();
	vPick.resize(num_boxes);
	for (int i = 0; i < num_boxes; ++i){
		vScores.insert(pair<float, int>(boundingBox_[i].score, i));
	}
    while(vScores.size() > 0){
        int last = vScores.rbegin()->second;  //反向迭代器,獲得vScores序列的最後那個序列號
        vPick[nPick] = last;
        nPick += 1;
        for (multimap<float, int>::iterator it = vScores.begin(); it != vScores.end();){
            int it_idx = it->second;
            maxX = max(boundingBox_.at(it_idx).x1, boundingBox_.at(last).x1);
            maxY = max(boundingBox_.at(it_idx).y1, boundingBox_.at(last).y1);
            minX = min(boundingBox_.at(it_idx).x2, boundingBox_.at(last).x2);
            minY = min(boundingBox_.at(it_idx).y2, boundingBox_.at(last).y2);
            //轉換成了兩個邊界框相交區域的邊長
            maxX = ((minX-maxX+1)>0)? (minX-maxX+1) : 0;
            maxY = ((minY-maxY+1)>0)? (minY-maxY+1) : 0;
            //求交併比IOU
            
            IOU = (maxX * maxY)/(boundingBox_.at(it_idx).area + boundingBox_.at(last).area - IOU);
            if(IOU > overlap_threshold){
                it = vScores.erase(it);    //刪除交併比大於閾值的候選框,erase返回刪除元素的下一個元素
            }else{
                it++;
            }
        }
    }
    
    vPick.resize(nPick);
    vector<Bbox> tmp_;
    tmp_.resize(nPick);
    for(int i = 0; i < nPick; i++){
        tmp_[i] = boundingBox_[vPick[i]];
    }
    boundingBox_ = tmp_;
}

其中程式碼中有一個地方的設計很巧妙值得注意,while迴圈中,先確定最大的score後,讓所有候選框都跟這個候選框計算交併比(包括自己),這樣刪除了交併比大於閾值的所有候選框,包括自己,這樣就可以重複進行迴圈獲得最終的候選框。
對於Bounding Box的列表B及其對應的置信度S,採用下面的計算方式.選擇具有最大score的檢測框M,將其從B集合中移除並加入到最終的檢測結果D中.通常將B中剩餘檢測框中與M的IoU大於閾值Nt的框從B中移除.重複這個過程,直到B為空。
--------------------- 
原文:https://blog.csdn.net/krais_wk/article/details/80938752