因为去修改deploy.prototxt中nums_threshold参数,想到研究一下NMS非极大抑制,记录一下简单的理解。
非极大抑制用在目标检测后筛选最终结果,更精确的定位。
一般分三步:
1、置信度排序并选出框置信度最高的框
2、计算其他框与最高置信度框的iou,若IOU大于阈值(nums_threshold),则认为与最大一致,去除,保留最大。
3、从未处理的框中重新选置信度最高的框,继续1、2
python代码:
import numpy as np
dets = np.array([
[204, 102, 358, 250, 0.5],
[257, 118, 380, 250, 0.7],
[280, 135, 400, 250, 0.6],
[255, 118, 360, 235, 0.7]])
thresh = 0.3
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) # 每个boundingbox的面积
order = scores.argsort()[::-1] # boundingbox的置信度排序
keep = [] # 用来保存最后留下来的boundingbox
while order.size > 0:
i = order[0] # 置信度最高的boundingbox的index
keep.append(i) # 添加本次置信度最高的boundingbox的index
# 当前bbox和剩下bbox之间的交叉区域
# 选择大于x1,y1和小于x2,y2的区域
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:]])
# 当前bbox和其他剩下bbox之间交叉区域的面积
w = np.maximum(0.0, xx2 - xx1 + 1)
h = np.maximum(0.0, yy2 - yy1 + 1)
inter = w * h
# 交叉区域面积 / (bbox + 某区域面积 - 交叉区域面积)
ovr = inter / (areas[i] + areas[order[1:]] - inter)
#保留交集小于一定阈值的boundingbox
inds = np.where(ovr <= thresh)[0]
order = order[inds + 1]
return keep
print nms(dets, thresh)