# =============================================================================
# =============================================================================
# 关于NMS 源码 py cpp ----
# 底层实现 C ---
# https://www.zhihu.com/question/454936025/answer/1849513369
# https://github.com/pytorch/vision/blob/main/torchvision/csrc/ops/cpu/nms_kernel.cpp
# D:\program\anaconda\envs\torch17\Lib\site-packages\torchvision\ops\boxes.py
# pytorch开源代码架构---- https://github.com/pytorch/vision/tree/main/torchvision
# pytorch本机安装后架构---- D:\program\anaconda\envs\torch17\Lib\site-packages\torchvision
# 对比
# 前者架构 csrc文件夹即为其他文件夹下所有python文件对应C程序
# 前者架构 csrc文件夹 在安装pip至本机时候 生成 后者架构 _C.pyd文件
# 即python对应的C程序在前者的csrc文件夹 可以查看(比如各种model metwork、包括这里的nms实现);在本地的话就是_C.pyd文件 已经编译链接好 没法查看
# 解释
# https://www.cnblogs.com/wangyong123/articles/14703719.html
# =============================================================================
# =============================================================================
#%%
# 假设有6个矩形框,根据分类器的类别分类概率做排序,假设从小到大属于车辆的概率 分别为A、B、C、D、E、
# (1)从最大概率矩形框F开始,分别判断A~E与F的重叠度IOU是否大于某个设定的阈值;
# (2)假设B、D与F的重叠度超过阈值,那么就扔掉B、D;并标记第一个矩形框F,是我们保留下来的。
# (3)从剩下的矩形框A、C、E中,选择概率最大的E,然后判断E与A、C的重叠度,重叠度大于一定的阈值,那么就扔掉;并标记E是我们保留下来的第二个矩形框。
# 就这样一直重复,找到所有被保留下来的矩形框。
# 注意点
# 1 while循环内 i = index[0] --- i的出现跟后面的 index[1:] 是同步的 都代表置信度下标
# 2 xx2 yy2均为 np.minimum()
def py_cpu_nms(dets, thresh):
#dets数据格式 [[xmin,ymin,xmax,ymax,scores]...]
x1 = dets[:, 0] # x1-表每个框的横坐标较小值!!!
y1 = dets[:, 1] # y1-表每个框的纵坐标较大值
x2 = dets[:, 2] # x2-表每个框的横坐标较大值
y2 = dets[:, 3] # y2-表每个框的纵坐标较小值
scores = dets[:, 4] # <class 'numpy.ndarray'>
print('scores',scores,'\n')
#每一个检测框的面积
areas = (x2 - x1 + 1) * (y2 - y1 + 1)
#按照score置信度降序排序 得到下标序列 index
index = scores.argsort()[::-1] # argsort()是从小到大排列
#保留NMS的结果框集合
keep = []
while index.size > 0:
i = index[0]
keep.append(i) # 保留该类剩余box中得分最高的一个
#得到相交区域,左上及右下
xx1 = np.maximum(x1[i], x1[index[1:]])
yy1 = np.maximum(y1[i], y1[index[1:]])
xx2 = np.minimum(x2[i], x2[index[1:]])
yy2 = np.minimum(y2[i], y2[index[1:]])
#计算相交的面积,不重叠时面积为0
w = np.maximum(0.0, xx2 - xx1 + 1)
h = np.maximum(0.0, yy2 - yy1 + 1)
inter = w * h
#计算IoU:重叠面积 /(面积1+面积2-重叠面积)
iou = inter / (areas[i] + areas[index[1:]] - inter)
#保留IoU小于阈值的box 保存的是下标 完成一次iou阀值的抑制 进入下一轮
inds = np.where(iou <= thresh)[0] # 解析见下
index = index[inds + 1] # 因为 iou 数组的长度比 index 数组少一个,所以这里要将所有下标后移一位
print('keep ',keep,'\n')
return keep
import numpy as np
dets=np.array([[100,100,210,210,0.72],
[250,250,420,420,0.82],
[210,220,320,330,0.92],
[100,100,210,210,0.72],
[230,240,325,330,0.81],
[220,230,315,340,0.90]])
threshold = 0.5
res = py_cpu_nms(dets, threshold)
print(dets[res])
#%% # =============================================================================
# 解析 np.where(iou <= thresh)[0]---
import numpy as np
a = np.array([2,4,6,8,10])
b = np.where(a > 5) # 返回索引 (array([2, 3, 4], dtype=int64),)
c = np.where(a > 5)[0] # 返回索引 array([2, 3, 4], dtype=int64)
d = a[np.where(a > 5)] # 返回元素 array([ 6, 8, 10]) 等价于 a[a>5]
e = a[np.where(a > 5)[0]]
#%% # =============================================================================
# 绘图
threshold = 0.5
dets=np.array([[100,100,210,210,0.72],
[250,250,420,420,0.82],
[210,220,320,330,0.92],
[100,100,210,210,0.72],
[230,240,325,330,0.81],
[220,230,315,340,0.90]])
# https://zhuanlan.zhihu.com/p/258106097
import matplotlib.pyplot as plt
def plot_bbox(dets, c='k'):
x1 = dets[:,0]
y1 = dets[:,1]
x2 = dets[:,2]
y2 = dets[:,3]
plt.plot([x1,x2], [y1,y1], c)
plt.plot([x1,x1], [y1,y2], c)
plt.plot([x1,x2], [y2,y2], c)
plt.plot([x2,x2], [y1,y2], c)
plt.title(" nms")
plt.figure(1)
ax1 = plt.subplot(1,2,1)
ax2 = plt.subplot(1,2,2)
# before nms
plt.sca(ax1)
plot_bbox(dets,'b')
# after nms
res = py_cpu_nms(dets, threshold)
plt.sca(ax2)
plot_bbox(dets[res], 'r')