NMS和soft-NMS、Fast-NMS实现

  • 前言
  • 正文

前言

Soft-NMS – Improving Object Detection With One Line of Code.通过阅读这篇文章,从中了解到相比较于NMS还有改进的Soft-NMS,文章说的是可以提升在很多数据集上的检测精度。

2021-07-05 更新了fast nms的程序

无意中看到Matrix nms算法,本想实现这个算法,但是无奈没找到详细的原理,如果大家有合适的算法解析,可以私聊我分享呢,感谢。

正文

下面的计算IOU函数

def iou(predicted_bound, ground_truth_bound):       #计算IOU"""computing the IoU of two boxes.Args:box: (xmin, ymin, xmax, ymax),通过左下和右上两个顶点坐标来确定矩形位置Return:IoU: IoU of box1 and box2."""pxmin, pymin, pxmax, pymax = predicted_bound# print("预测框P的坐标是:({}, {}, {}, {})".format(pxmin, pymin, pxmax, pymax))gxmin, gymin, gxmax, gymax = ground_truth_bound# print("原标记框G的坐标是:({}, {}, {}, {})".format(gxmin, gymin, gxmax, gymax))parea = (pxmax - pxmin) * (pymax - pymin)  # 计算P的面积garea = (gxmax - gxmin) * (gymax - gymin)  # 计算G的面积# print("预测框P的面积是:{};原标记框G的面积是:{}".format(parea, garea))# 求相交矩形的左下和右上顶点坐标(xmin, ymin, xmax, ymax)xmin = max(pxmin, gxmin)  # 得到左下顶点的横坐标ymin = max(pymin, gymin)  # 得到左下顶点的纵坐标xmax = min(pxmax, gxmax)  # 得到右上顶点的横坐标ymax = min(pymax, gymax)  # 得到右上顶点的纵坐标# 计算相交矩形的面积w = xmax - xminh = ymax - yminif w <=0 or h <= 0:return 0area = w * h  # G∩P的面积# area = max(0, xmax - xmin) * max(0, ymax - ymin)  # 可以用一行代码算出来相交矩形的面积# print("G∩P的面积是:{}".format(area))# 并集的面积 = 两个矩形面积 - 交集面积IoU = area / (parea + garea - area)return IoU

使用NMS筛选boxes

def nms(boxes,threshold=0.5):boxes_copy = boxes.copy()result = []while len(boxes) > 0:index = [i[4] for i in boxes]max_ = np.argmax(index)max_cor = boxes[max_]result.append(boxes[max_])boxes = np.delete(boxes, max_, axis=0)res = []for i in range(len(boxes)):if iou(max_cor[:-1], boxes[i][:-1]) < threshold:res.append(boxes[i])boxes = resplt.figure()img = np.zeros((616, 616, 3))plt.subplot(121)for i in result:cv2.rectangle(img, (int(i[0]), int(i[1])), (int(i[2]), int(i[3])), (0, 255, 0), 2)plt.imshow(img)plt.title('nms')plt.subplot(122)img_ = np.zeros((616, 616, 3))for i in boxes_copy:cv2.rectangle(img_, (int(i[0]), int(i[1])), (int(i[2]), int(i[3])), (0, 255, 0), 2)plt.imshow(img_)plt.title('original')plt.show()return result

使用soft-nms函数筛选目标框

def soft_nms(boxes,threshold,score,λ):boxes_copy = boxes.copy()result = []while len(boxes)>0:boxes = [i for i in boxes if i[-1]>score]if len(boxes)==0:breakindex = [i[-1] for i in boxes]max_ = np.argmax(index)max_cor = boxes[max_]boxes = np.delete(boxes, max_, axis=0)result.append(max_cor)for i in range(len(boxes)):if iou(max_cor[:-1],boxes[i][:-1])>threshold:boxes[i][-1] = boxes[i][-1]*np.exp(-1*iou(max_cor[:-1],boxes[i][:-1])/λ)plt.figure()img = np.zeros((616, 616, 3))plt.subplot(121)for i in result:cv2.rectangle(img, (int(i[0]), int(i[1])), (int(i[2]), int(i[3])), (0, 255, 0), 2)plt.imshow(img)plt.title('soft-nms')plt.subplot(122)img_ = np.zeros((616, 616, 3))for i in boxes_copy:cv2.rectangle(img_, (int(i[0]), int(i[1])), (int(i[2]), int(i[3])), (0, 255, 0), 2)plt.imshow(img_)plt.title('original')plt.show()result = [[int(i[0]),int(i[1]),int(i[2]),int(i[3]),float(i[4])] for i in result]return result

这是fast nms的程序

def fast_nms(boxes,threshold=0.5):score = [i[-1] for i in boxes]matrix = np.zeros((len(boxes),len(boxes)))for i in range(len(boxes)-1):for j in range(i+1,len(boxes)):matrix[i,j] = iou(boxes[i][:-1],boxes[j][:-1])res1 = [max(matrix[:,i]) for i in range(len(matrix))]# print(res1)result = []for i in range(len(res1)):if res1[i]<threshold:result.append(boxes[i])print(result)plt.figure()img = np.zeros((616, 616, 3))plt.subplot(121)for i in result:cv2.rectangle(img, (int(i[0]), int(i[1])), (int(i[2]), int(i[3])), (0, 255, 0), 2)plt.imshow(img)plt.title('fast-nms')plt.subplot(122)img_ = np.zeros((616, 616, 3))for i in boxes:cv2.rectangle(img_, (int(i[0]), int(i[1])), (int(i[2]), int(i[3])), (0, 255, 0), 2)plt.imshow(img_)plt.title('original')plt.show()

这些是调试的程序

boxes = np.array([[100, 110, 210, 210, 0.71],[250, 250, 420, 420, 0.8],[220, 200, 320, 330, 0.92],[120, 100, 210, 210, 0.72],[230, 240, 325, 330, 0.81],[220, 230, 315, 340, 0.91]])nms(boxes,threshold=0.5)
soft_nms(boxes,0.5,0.4,0.1)    #boxes,threshold,score,λ(gauss函数参数)

得到的NMS算法的结果:

得到的soft-NMS算法的结果:

得到 Fast NMS的效果

有问题可以直接提问,欢迎一起交流。

参考文章
[1]:《Improving Object Detection With One Line of Code》

NMS和soft-NMS实现相关推荐

  1. NMS、soft NMS、softer NMS与IOU-Guided NMS

    提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档 NMS.soft NMS.softer NMS与IOU-Guided NMS 一.NMS 二.soft NMS 三.softer NM ...

  2. 目标检测中的NMS,soft NMS,softer NMS,Weighted Boxes Fusion

    NMS 非最大值抑制算法,诞生至少50年了. 在经典的两阶段目标检测算法中,为了提高对于目标的召回率,在anchor阶段会生成密密麻麻的anchor框. 所以在后处理的时候,会存在着很多冗余框对应着同 ...

  3. NMS与Soft NMS

    1.NMS 非最大抑制(Non-maximum suppression, NMS)是物体检测流程中重要的组成部分.NMS算法的大致思想:对于有重叠的候选框:若大于规定阈值(某一提前设定的置信度)则删除 ...

  4. Soft NMS算法笔记

    原博客地址:https://blog.csdn.net/u014380165/article/details/79502197#comments 提出问题 如下图,测算法本来应该输出两个框,但是传统的 ...

  5. 交并比 (IoU), mAP (mean Average Precision), 非极大值抑制 (NMS, Soft NMS, Softer NMS, IoU-Net)

    目录 目标检测的评价指标 交并比 (Intersection of Union, IoU) mAP (mean Average Precision) 其他指标 非极大值抑制 (Non-Maximum ...

  6. nms,soft nms算法理解

    从上面这张图可以看出来nms和soft nms的算法原理: ** 经典nms的原理 **设定目标框的置信度阈值,常用的阈值是0.5左右 根据置信度降序排列候选框列表 选取置信度最高的框A添加到输出列表 ...

  7. nms修改为soft nms

    nms修改为soft nms 一,NMS与soft nms原理 先放一张论文中的图 在对上述图片中的目标进行检测过程中,使用非极大值抑制(NMS)进行筛选时只能获取到得分最高的框(得分0.95),对于 ...

  8. 卷积在计算机中实现+pool作用+数据预处理目的+特征归一化+理解BN+感受野理解与计算+梯度回传+NMS/soft NMS

    一.卷积在计算机中实现 1.卷积 将其存入内存当中再操作(按照"行先序"): 这样就造成混乱. 故需要im2col操作,将特征图转换成庞大的矩阵来进行卷积计算,利用矩阵加速来实现, ...

  9. Soft NMS论文笔记

    论文:Improving Object Detection With One Line of Code. Navaneeth Bodla*, Bharat Singh*, Rama Chellappa ...

  10. Soft NMS+Softer NMS+KL Loss

    论文1: Soft-NMS – Improving Object Detection With One Line of Code (ICCV2017) 速达>> 论文2: Softer-N ...

最新文章

  1. 主机入侵防御系统(HIPS)分析
  2. Linux查看端口号是否使用
  3. 基础连接已经关闭: 无法与远程服务器建立信任关系
  4. 如何获取Google地图API密钥?(翻译版)
  5. leetcode 148 --- 链表排序
  6. 的好处_女性做下蹲运动有什么好处 原来有这些好处
  7. FTP+SSL(加密的ftp)
  8. labview曲线上两点画延长线_自由泳,那些防不胜防的错误动作(上)
  9. 远程控制软件用户群分析
  10. 关于 Visual Studio 2017 ,或2019 ,Installer 没检测到已安装的程序.以及C++ 创建项目失败...
  11. 计算机科学导论填空题,计算机科学导论习题
  12. CTFshow-大牛杯
  13. 美国经济数据公布时间
  14. 信用卡逾期怎么办,如何让信用卡不逾期?
  15. 山东大学软件学院2021软件项目管理考试回忆
  16. AI修图!pix2pix网络介绍与tensorflow实现
  17. 手机设备唤醒计算机,手机遥控电脑开机神器!局域网唤醒App
  18. python国际象棋ai程序_只需五步!手把手教你搭建国际象棋AI机器人
  19. 多DZ和UC同步登陆状态(支持HTTPS,PHP7.4.3)
  20. Gartner:2018人工智能技术成熟度曲线

热门文章

  1. 从固定管线到可编程管线:十段代码入门OpenGL
  2. 97年阿里P7晒出工资单:狠补了这个,真香
  3. 一个简单的在线答题程序
  4. Python BMI体质计算
  5. 【翻译】使用EGSEA进行简单有效的基因组测试
  6. 【测试基础】你写过测试计划和测试报告吗?
  7. 2020全新的微信域名防封技术,域名怎么避免防封,如何防拦截?
  8. 《一分钟经理人》有感
  9. 家庭分享会 | 第一期:如何组织分享会
  10. 黄山踏青春游记-两天两夜