學習Faster R-CNN代碼nms(七)


非極大值抑制(Non-Maximum Suppression NMS)

NMS就是去除冗余的檢測框,保留最好的一個。

 

產生proposal后使用分類網絡給出每個框的每類置信度,使用回歸網絡修正位置,最終應用NMS.

對於Bounding Box的列表B及其對應的置信度S,采用下面的計算方式.選擇具有最大score的檢測框M,將其從B集合中移除並加入到最終的檢測結果D中.通常將B中剩余檢測框中與M的IoU大於閾值Nt的框從B中移除.重復這個過程,直到B為空.
參考: http://www.cnblogs.com/makefile/p/nms.html © 康行天下

 1 def nms_cpu(dets, thresh):
 2     dets = dets.numpy()
 3     #x1、y1、x2、y2、以及score賦值
 4     x1 = dets[:, 0]
 5     y1 = dets[:, 1]
 6     x2 = dets[:, 2]
 7     y2 = dets[:, 3]
 8     scores = dets[:, 4]
 9 
10     ##每一個檢測框的面積
11     areas = (x2 - x1 + 1) * (y2 - y1 + 1)
12     #按照score置信度降序排序
13     order = scores.argsort()[::-1]#argsort函數返回的是數組值從小到大的索引值,然后又降序
14 
15     keep = []#保留的結果框集合
16     while order.size > 0:
17         i = order.item(0)
18         keep.append(i)#保留該類剩余box中得分最高的一個
19         #得到相交區域,左上及右下##########
20         xx1 = np.maximum(x1[i], x1[order[1:]])#X 與 Y 逐位比較取其大者
21         yy1 = np.maximum(y1[i], y1[order[1:]])
22         xx2 = np.minimum(x2[i], x2[order[1:]])
23         yy2 = np.minimum(y2[i], y2[order[1:]])
24 
25         ##計算相交的面積,不重疊時面積為0
26         w = np.maximum(0.0, xx2 - xx1 + 1)
27         h = np.maximum(0.0, yy2 - yy1 + 1)
28         inter = w * h 
29         #計算IoU:重疊面積 /(面積1+面積2-重疊面積)
30         ovr = inter / (areas[i] + areas[order[1:]] - inter)
31 
32         ##保留IoU小於閾值的box
33         ##只有條件 (condition),沒有x和y,則輸出滿足條件 (即非0) 元素的坐標 (等價於numpy.nonzero)。
34         inds = np.where(ovr <= thresh)[0]
35         order = order[inds + 1]#因為ovr數組的長度比order數組少一個,所以這里要將所有下標后移一位
36 
37     return torch.IntTensor(keep)

ref:https://blog.csdn.net/weixin_43872578/article/details/87909640


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM