什么是NMS?
NMS(非极大值抑制,Non-Maximum Suppression),是一种提取目标检测中分数最高的窗口的算法。例如在目标检测中,我们的神经网络对一个目标生成了多个检测窗口,但是事实上这些窗口中大部分内容都是重复的,我们真正需要的只有一个。

所以我们要做就是找到目标检测最优的窗口,NMS就是用来选取多个检测窗口中分数最高的那个窗口,剔除掉其他的同类型的窗口,因此 NMS 在计算机视觉领域有着非常重要的应用。
目标检测网络输出
为什么目标检测在同一物体会有多个框?

在目标检测任务中,预测结果会得到多个bounding box(bbox),这是为了模型目标检测的准确性,例如上图模型得到了两个bbox,可以看到网络的最终输出是 7×7×30。7x7 指的是图像分为 7x7 个区域进行预测。
最终输出 tensor 的前五个数值,分别是 bbox 的
x
x
x,
y
y
y,
w
w
w,
h
h
h,
c
c
c或者
x
1
x_1
x1,
y
1
y_1
y1,
x
2
x_2
x2,
y
2
y_2
y2,
c
c
c, 前四个值为bbox的位置信息,
c
c
c为bbox 的置信度。
置信度就是算法的自信心得分,越高表示越坚信这个检测的目标没错。
通过多个bbox增加预测的准确性,进一步通过NMS去除冗余bbox,最后得到更加准备的预测结果。
基础知识IoU
IoU 的全称为交并比(Intersection over Union),通过这个名称我们得出 IoU 的计算方法。IoU 计算的是 “预测的边框” 和 “真实的边框” 的交集和并集的比值。在目标检测任务中,IoU成为一个衡量预测的检测框的准确度的一个重要计算指标。包括目标检测中的AP和mAP的指标,都是依据IoU的计算来实现的。

对于任意的检测框,设定两个框的IoU阈值作为两者是否合并的评判依据,并执行NMS操作来删除冗余的检测框。
iou实现思路
- 首先计算两个box左上角点坐标的最大值和右下角坐标的最小值(这是因为图像数据标注时一般以图像的左上角为坐标原点);
- 然后计算交集面积;
- 最后把交集面积除以对应的并集面积。
NMS算法流程
- 给出一张图片和上面许多物体检测的候选框(即每个框可能都代表某种物体),但是这些框很可能有互相重叠的部分,我们要做的就是只保留最优的框。假设有N个框,每个框被分类器计算得到的分数为 S i S_i Si, 其中1 ≤ i ≤ N \leq i \leq N ≤i≤N;
- 建造一个存放待处理候选框的集合 H \bm{H} H,初始化为包含全部 N N N个框;建造一个存放最优框的集合 M \bm{M} M,初始化为空集;
- 将所有集合 H \bm{H} H中的框进行排序,选出分数最高的框 m m m,从集合 H \bm{H} H 移到集合 M \bm{M} M;
- 遍历集合 H \bm{H} H中的框,分别与框 m m m计算交并比(Interection-over-union,IoU),如果高于某个阈值(一般为0~0.5),则认为此框与 m m m重叠,将此框从集合 H \bm{H} H中去除;
- 回到第2步进行迭代,直到集合 H \bm{H} H为空。集合 M \bm{M} M中的框为我们所需。
NMS核心代码
def py_cpu_nms(dets, thresh):
"""Pure Python NMS baseline."""
# x1、y1、x2、y2、以及score赋值
x1 = dets[:, 0]
y1 = dets[:, 1]
x2 = dets[:, 2]
y2 = dets[:, 3]
scores = dets[:, 4]
# 每一个检测框的面积
areas = (x2 - x1 + 1) * (y2 - y1 + 1)
# 按照score置信度降序排序
order = scores.argsort()[::-1]
pick = [] # 保留的结果框集合
while order.size > 0:
i = order[0]
pick.append(i) # 保留该类剩余box中得分最高的一个
# 得到相交区域,左上及右下
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:]])
# 计算相交的面积,不重叠时面积为0
w = np.maximum(0.0, xx2 - xx1 + 1)
h = np.maximum(0.0, yy2 - yy1 + 1)
inter = w * h
# 计算IoU:重叠面积 /(面积1+面积2-重叠面积)
ovr = inter / (areas[i] + areas[order[1:]] - inter)
# 保留IoU小于阈值的box
inds = np.where(ovr <= thresh)[0]
order = order[inds + 1] # 因为ovr数组的长度比order数组少一个,所以这里要将所有下标后移一位
return pick
NMS完整代码
import numpy as np
# import cv2
import matplotlib.pyplot as plt
det_result = np.array([[20, 5, 200, 100, 0.353],
[50, 50, 200, 200, 0.624],
[20, 120, 150, 150, 0.667],
[250, 250, 400, 350, 0.5],
[90, 10, 300, 300, 0.3],
[40, 220, 280, 380, 0.46]])
colors = ['red', 'blue', 'green', 'yellow', 'pink', 'gray']
# plt.figure()
def py_cpu_nms(dets, thresh):
"""Pure Python NMS baseline."""
# x1、y1、x2、y2、以及score赋值
x1 = dets[:, 0]
y1 = dets[:, 1]
x2 = dets[:, 2]
y2 = dets[:, 3]
scores = dets[:, 4]
# 每一个检测框的面积
areas = (x2 - x1 + 1) * (y2 - y1 + 1)
# 按照score置信度降序排序
order = scores.argsort()[::-1]
pick = [] # 保留的结果框集合
while order.size > 0:
i = order[0]
pick.append(i) # 保留该类剩余box中得分最高的一个
# 得到相交区域,左上及右下
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:]])
# 计算相交的面积,不重叠时面积为0
w = np.maximum(0.0, xx2 - xx1 + 1)
h = np.maximum(0.0, yy2 - yy1 + 1)
inter = w * h
# 计算IoU:重叠面积 /(面积1+面积2-重叠面积)
ovr = inter / (areas[i] + areas[order[1:]] - inter)
# 保留IoU小于阈值的box
inds = np.where(ovr <= thresh)[0]
order = order[inds + 1] # 因为ovr数组的长度比order数组少一个,所以这里要将所有下标后移一位
return pick
def non_max_suppression(dets, threshold):
"""执行non-maximum suppression并返回保留的boxes的索引.
dets:(x1、y1、x2、y2,scores)
threshold: Float型. 用于过滤IoU的阈值.
"""
x1 = dets[:, 0]
y1 = dets[:, 1]
x2 = dets[:, 2]
y2 = dets[:, 3]
scores = dets[:, 4]
# 每一个检测框的面积
areas = (x2 - x1 + 1) * (y2 - y1 + 1)
# 获取根据分数排序的boxes的索引(最高的排在对前面)
ixs = scores.argsort()[::-1]
pick = []
while len(ixs) > 0:
# 选择排在最前的box,并将其索引加到列表中
i = ixs[0]
pick.append(i)
# 计算选择的box与剩下的box的IoU
xx1 = np.maximum(x1[i], x1[ixs[1:]])
yy1 = np.maximum(y1[i], y1[ixs[1:]])
xx2 = np.minimum(x2[i], x2[ixs[1:]])
yy2 = np.minimum(y2[i], y2[ixs[1:]])
# 计算相交的面积,不重叠时面积为0
w = np.maximum(0.0, xx2 - xx1 + 1)
h = np.maximum(0.0, yy2 - yy1 + 1)
inter = w * h
# 计算IoU:重叠面积 /(面积1+面积2-重叠面积)
ovr = inter / (areas[i] + areas[ixs[1:]] - inter)
remove_ixs = np.where(ovr > threshold)[0] + 1
# 将选择的box和重叠的boxes的索引删除.
ixs = np.delete(ixs, remove_ixs)
ixs = np.delete(ixs, 0)
return np.array(pick)
if __name__ == '__main__':
for i in range(len(det_result)):
plt.plot([det_result[i][1], det_result[i][3], det_result[i][3], det_result[i][1],
det_result[i][1]], # col
[det_result[i][0], det_result[i][0], det_result[i][2], det_result[i][2],
det_result[i][0]], # row
color=colors[i], marker='.', ms=0)
plt.show()
result = py_cpu_nms(det_result, 0.1)
# result=non_max_suppression(det_result,0.1)
print(result)
for j in range(len(result)):
i = result[j]
plt.plot([det_result[i][1], det_result[i][3], det_result[i][3], det_result[i][1],
det_result[i][1]], # col
[det_result[i][0], det_result[i][0], det_result[i][2], det_result[i][2],
det_result[i][0]], # row
color=colors[i], marker='.', ms=0)
plt.show()
1万+



被折叠的 条评论
为什么被折叠?



