NMS(Non-Maximum Suppression)非极大值抑制
生活随笔
收集整理的這篇文章主要介紹了
NMS(Non-Maximum Suppression)非极大值抑制
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
非極大值抑制
概述
在目標檢測領域,我們經常用到非極大值抑制(NMS),NMS就是在局部范圍內抑制不是極大值的目標,只保留極大值。
?
原理
在檢測任務重,我們會得到一批具有置信度S的bbox列表B,首先根據置信度S對bbox進行排序,選擇置信度最高的框M,從B中移除M并加入到最終結果D中,將剩余的框與B分別作交并比運算,IOU大于閾值Nt(通常設為0.3~0.5)的框從B中移除,一輪結束,再重新對B中的框按照置信度排序,選擇下一個框加入D,并移除一些框,重復這個過程,直到B為空。
?
Python代碼
def nms(dets, thresh):x1 = dets[:, 0]y1 = dets[:, 1]x2 = dets[:, 2]y2 = dets[:, 3]scores = dets[:, 4]areas = (x2 - x1 + 1) * (y2 - y1 + 1)order = scores.argsort()[::-1]keep = []while order.size > 0:i = order[0]keep.append(i)xx1 = np.maximum(x1[i], x1[order[1:]])yy1 = np.maximum(y1[i], y1[order[1:]])xx2 = np.minimum(x2[i], x2[order[1:]])yy2 = np.minimum(y2[i], y2[order[1:]])w = np.maximum(0.0, xx2 - xx1 + 1)h = np.maximum(0.0, yy2 - yy1 + 1)inter = w * hovr = inter / (areas[i] + areas[order[1:]] - inter)inds = np.where(ovr <= thresh)[0]order = order[inds + 1]return keep
Soft-NMS
NMS的方法在遇到兩個ground truth的目標框IOU很高時,會將具有較低置信度的框去掉(置信度改成0),soft-nms的提出就是為了解決這一問題,該方法對于非極大值的框置信度不置0,而是置為IOU的函數,置信度降低但不至于被刪除。
IOU的函數有兩種選擇項:
(1)method=1時,線性函數
(1)method=2時,高斯函數
?
代碼
def cpu_soft_nms(boxes,sigma=0.5, Nt=0.1, threshold=0.001, method=0):N = boxes.shape[0]for i in range(N):maxscore = boxes[i, 4]maxpos = itx1 = boxes[i, 0]ty1 = boxes[i, 1]tx2 = boxes[i, 2]ty2 = boxes[i, 3]ts = boxes[i, 4]pos = i + 1# get max boxwhile pos < N:if maxscore < boxes[pos, 4]:maxscore = boxes[pos, 4]maxpos = pospos = pos + 1# add max box as a detectionboxes[i, 0] = boxes[maxpos, 0]boxes[i, 1] = boxes[maxpos, 1]boxes[i, 2] = boxes[maxpos, 2]boxes[i, 3] = boxes[maxpos, 3]boxes[i, 4] = boxes[maxpos, 4]# swap ith box with position of max boxboxes[maxpos, 0] = tx1boxes[maxpos, 1] = ty1boxes[maxpos, 2] = tx2boxes[maxpos, 3] = ty2boxes[maxpos, 4] = tstx1 = boxes[i, 0]ty1 = boxes[i, 1]tx2 = boxes[i, 2]ty2 = boxes[i, 3]ts = boxes[i, 4]pos = i + 1# NMS iterations, note that N changes if detection boxes fall below thresholdwhile pos < N:x1 = boxes[pos, 0]y1 = boxes[pos, 1]x2 = boxes[pos, 2]y2 = boxes[pos, 3]s = boxes[pos, 4]area = (x2 - x1 + 1) * (y2 - y1 + 1)iw = (min(tx2, x2) - max(tx1, x1) + 1)if iw > 0:ih = (min(ty2, y2) - max(ty1, y1) + 1)if ih > 0:ua = float((tx2 - tx1 + 1) * (ty2 - ty1 + 1) + area - iw * ih)ov = iw * ih / ua # iou between max box and detection boxif method == 1: # linearif ov > Nt:weight = 1 - ovelse:weight = 1elif method == 2: # gaussianweight = np.exp(-(ov * ov) / sigma)else: # original NMSif ov > Nt:weight = 0else:weight = 1boxes[pos, 4] = weight * boxes[pos, 4]# if box score falls below threshold, discard the box by swapping with last box# update Nif boxes[pos, 4] < threshold:boxes[pos, 0] = boxes[N - 1, 0]boxes[pos, 1] = boxes[N - 1, 1]boxes[pos, 2] = boxes[N - 1, 2]boxes[pos, 3] = boxes[N - 1, 3]boxes[pos, 4] = boxes[N - 1, 4]N = N - 1pos = pos - 1pos = pos + 1keep = [i for i in range(N)]return keep
參考
參考博客 https://blog.csdn.net/Quincuntial/article/details/78815187 代碼,可進行實驗觀察
參考 https://www.cnblogs.com/makefile/p/nms.html
總結
以上是生活随笔為你收集整理的NMS(Non-Maximum Suppression)非极大值抑制的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Python学习笔记——time模块和d
- 下一篇: 训练网络指定层pytorch实现方法