目標檢測的標準的NMS

2021-10-09 08:33:08 字數 2186 閱讀 8026

# 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)

def non_max_suppress(predicts_dict, threshold=0.2):

"""implement non-maximum supression on predict bounding boxes.

args:

predicts_dict: .

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]

## 獲取所有與當前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

參考: 目標定位和檢測系列(3):交並比(iou)和非極大值抑制(nms)的python實現

CV基石 對目標檢測原始邊框進行NMS

對每乙個類別的目標分別進行nms for object name,bbox in predicts dict.items bbox array np.array bbox,dtype np.float 獲取當前目標類別下所有bounding box的座標和confidence,並計算所有bbox的面...

目標檢測中的example mining

目標檢測,一般分為2個部分,定位和分類,example mining是選擇出特定樣本來計算損失函式 從實際問題出發hard example應該就是指定位較困難或分類較困難或者兩者都困難的候選框。ssd的caffe中支援negative mining和hard example mining,當shar...

目標檢測中region proposal的作用

首先我們明確乙個定義,當前主流的object detection框架分為1 stage和2 stage,而2 stage多出來的這個stage就是regional proposal過程,明確這一點後,我們繼續講。regional proposal的輸出到底是什麼?我們首先看一下以faster r c...