非极大抑制,是在对象检测中用的较为频繁的方法,当在一个对象区域,框出了很多框,那么如下图:



上图来自这里

目的就是为了在这些框中找到最适合的那个框.有以下几种方式:

  • 1 nms
  • 2 soft-nms
  • 3 softer-nms

1. nms

主要就是通过迭代的形式,不断的以最大得分的框去与其他框做iou操作,并过滤那些iou较大(即交集较大)的框

IOU也是一种Tanimoto测量方法[见模式识别,希腊,书609页]

按照github上R-CNN的matlab代码,改成py的,具体如下:


  1. def iou(xminNp,yminNp,xmaxNp,ymaxNp,areas,lastInd,beforeInd,threshold):
  2. # 将lastInd指向的box,与之前的所有存活的box做比较,得到交集区域的坐标。
  3. # np.maximum([3,1,4,2],3) 等于 array([3,3,4,3])
  4. xminNpTmp = np.maximum(xminNp[lastInd], xminNp[beforeInd])
  5. yminNpTmp = np.maximum(yminNp[lastInd], yminNp[beforeInd])
  6. xmaxNpTmp = np.maximum(xmaxNp[lastInd], xmaxNp[beforeInd])
  7. ymaxNpTmp = np.maximum(ymaxNp[lastInd], ymaxNp[beforeInd])
  8. #计算lastInd指向的box,与存活box交集的,所有width,height
  9. w = np.maximum(0.0,xmaxNpTmp-xminNpTmp)
  10. h = np.maximum(0.0,ymaxNpTmp-yminNpTmp)
  11. #计算存活box与last指向box的交集面积
  12. # array([1,2,3,4]) * array([1,2,3,4]) 等于 array([1,4,9,16])
  13. inter = w*h
  14. iouValue = inter/(areas[beforeInd]+areas[lastInd]-inter)
  15. indexOutput = [item[0] for item in zip(beforeInd,iouValue) if item[1] <= threshold ]
  16. return indexOutput
  17. def nms(boxes,threshold):
  18. '''
  19. boxes:n by 5的矩阵,n表示box个数,每一行分别为[xmin,ymin,xmax,ymax,score]
  20. '''
  21. assert isinstance(boxes,numpy.ndarray),'boxes must numpy object'
  22. assert boxes.shape[1] == 5,'the column Dimension should be 5'
  23. xminNp = boxes[:,0]
  24. yminNp = boxes[:,1]
  25. xmaxNp = boxes[:,2]
  26. ymaxNp = boxes[:,3]
  27. scores = boxes[:,4]
  28. #计算每个box的面积
  29. areas = (xmaxNp-xminNp)*(ymaxNp-yminNp)
  30. #对每个box的得分按升序排序
  31. scoresSorted = sorted(list(enumerate(scores)),key = lambda item:item[1])
  32. #提取排序后数据的原索引
  33. index = [ item[0] for item in scoresSorted ]
  34. pick = []
  35. while index:
  36. #将当前index中最后一个加入pick
  37. lastInd = index[-1]
  38. pick.append(lastInd)
  39. #计算最后一个box与之前所有box的iou
  40. index = iou(xminNp,yminNp,xmaxNp,ymaxNp,areas,lastInd,index[:-1],threshold)
  41. return pick
  42. if __name__ == '__main__':
  43. nms(boxes,threshold)

2. soft-nms

  1. import copy
  2. def iou(xminNp,yminNp,xmaxNp,ymaxNp,scores,areas,remainInds,maxGlobalInd,Nt,sigma,threshold, method):
  3. remainInds = np.array(remainInds)
  4. # 将maxGlobalInd指向的box,与所有剩下的box做比较,得到交集区域的坐标。
  5. # np.maximum([3,1,4,2],3) 等于 array([3,3,4,3])
  6. xminNpTmp = np.maximum(xminNp[maxGlobalInd], xminNp[remainInds])
  7. yminNpTmp = np.maximum(yminNp[maxGlobalInd], yminNp[remainInds])
  8. xmaxNpTmp = np.maximum(xmaxNp[maxGlobalInd], xmaxNp[remainInds])
  9. ymaxNpTmp = np.maximum(ymaxNp[maxGlobalInd], ymaxNp[remainInds])
  10. # 计算box交集所有width,height
  11. w = np.maximum(0.0,xmaxNpTmp-xminNpTmp)
  12. h = np.maximum(0.0,ymaxNpTmp-yminNpTmp)
  13. #计算IOU
  14. # array([1,2,3,4]) * array([1,2,3,4]) 等于 array([1,4,9,16])
  15. inter = w*h
  16. iouValue = inter/(areas[remainInds]+areas[maxGlobalInd]-inter)
  17. # 依据不同的方法进行权值更新
  18. weight = np.ones_like(iouValue)
  19. if method == 'linear': # linear
  20. # 实现1 - iou
  21. weight = weight - iouValue
  22. weight[iouValue <= Nt] = 1
  23. elif method == 'gaussian':
  24. weight = np.exp(-(iouValue*iouValue)/sigma)
  25. else: # original NMS
  26. weight[iouValue > Nt] = 0
  27. # 更新scores
  28. scores[remainInds] = weight*scores[remainInds]
  29. # 删除低于阈值的框
  30. remainInds = remainInds[scores[remainInds] > threshold]
  31. return remainInds.tolist(),scores
  32. def soft_nms(boxes, threshold, sigma, Nt, method):
  33. '''
  34. boxes:n by 5的矩阵,n表示box个数,每一行分别为[xmin,ymin,xmax,ymax,score]
  35. # 1 - 先找到最大得分的box,放到结果集中;
  36. # 2 - 然后将最大得分的box与剩下的做对比,去更新剩下的得分权值
  37. # 3 - 删除低于最小值的框;
  38. # 4 - 再找到剩下中最大的,循环
  39. # 5 - 返回结果集
  40. '''
  41. assert isinstance(boxes,numpy.ndarray),'boxes must numpy object'
  42. assert boxes.shape[1] == 5,'the column Dimension should be 5'
  43. pick = []
  44. copyBoxes = copy.deepcopy(boxes)
  45. xminNp = boxes[:,0]
  46. yminNp = boxes[:,1]
  47. xmaxNp = boxes[:,2]
  48. ymaxNp = boxes[:,3]
  49. scores = copy.deepcopy(boxes[:,4]) # 会不断的更新其中的得分数值
  50. remainInds = list(range(len(scores))) # 会不断的被分割成结果集,丢弃
  51. #计算每个box的面积
  52. areas = (xmaxNp-xminNp)*(ymaxNp-yminNp)
  53. while remainInds:
  54. # 1 - 先找到最大得分的box,放到结果集中;
  55. maxLocalInd = np.argmax(scores[remainInds])
  56. maxGlobalInd = remainInds[maxLocalInd]
  57. pick.append(maxGlobalInd)
  58. # 2 - 丢弃最大值在索引中的位置
  59. remainInds.pop(maxLocalInd)
  60. if not remainInds: break
  61. # 3 - 更新scores,remainInds
  62. remainInds,scores = iou(xminNp,yminNp,xmaxNp,ymaxNp,scores,areas,remainInds,maxGlobalInd,Nt,sigma,threshold, method)
  63. return pick
  64. if __name__ == '__main__':
  65. soft_nms(boxes, 0.001, 0.5, 0.3, 'linear')

3. softer-nms

参考资料:

  1. 非极大抑制
  2. [首次提出nms] Rosenfeld A, Thurston M. Edge and curve detection for visual scene analysis[J]. IEEE Transactions on computers, 1971 (5): 562-569.
  3. Theodoridis.S.,.Koutroumbas.K..Pattern.Recognition,.4ed,.AP,.2009
  4. [soft-nms] Bodla N, Singh B, Chellappa R, et al. Soft-nms—improving object detection with one line of code[C]//Computer Vision (ICCV), 2017 IEEE International Conference on. IEEE, 2017: 5562-5570. 【code
  5. [fitness nms] Tychsen-Smith L, Petersson L. Improving Object Localization with Fitness NMS and Bounded IoU Loss[J]. arXiv preprint arXiv:1711.00164, 2017.
  6. [learning NMS] J. H. Hosang, R. Benenson, and B. Schiele. Learning nonmaximum suppression. In CVPR, pages 6469–6477, 2017
  7. [softer-nms] He Y, Zhang X, Savvides M, et al. Softer-NMS: Rethinking Bounding Box Regression for Accurate Object Detection[J]. arXiv preprint arXiv:1809.08545, 2018.)

object detection[NMS]的更多相关文章

  1. Object Detection · RCNN论文解读

    转载请注明作者:梦里茶 Object Detection,顾名思义就是从图像中检测出目标对象,具体而言是找到对象的位置,常见的数据集是PASCAL VOC系列.2010年-2012年,Object D ...

  2. [Arxiv1706] Few-Example Object Detection with Model Communication 论文笔记

    p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 13.0px "Helvetica Neue"; color: #042eee } p. ...

  3. 论文阅读笔记五十五:DenseBox: Unifying Landmark Localization with End to End Object Detection(CVPR2015)

    论文原址:https://arxiv.org/abs/1509.04874 github:https://github.com/CaptainEven/DenseBox 摘要 本文先提出了一个问题:如 ...

  4. 论文阅读笔记五十二:CornerNet-Lite: Efficient Keypoint Based Object Detection(CVPR2019)

    论文原址:https://arxiv.org/pdf/1904.08900.pdf github:https://github.com/princeton-vl/CornerNet-Lite 摘要 基 ...

  5. 论文阅读笔记四十八:Bounding Box Regression with Uncertainty for Accurate Object Detection(CVPR2019)

    论文原址:https://arxiv.org/pdf/1809.08545.pdf github:https://github.com/yihui-he/KL-Loss 摘要 大规模的目标检测数据集在 ...

  6. 论文阅读笔记四十六:Feature Selective Anchor-Free Module for Single-Shot Object Detection(CVPR2019)

    论文原址:https://arxiv.org/abs/1903.00621 摘要 本文提出了基于无anchor机制的特征选择模块,是一个简单高效的单阶段组件,其可以结合特征金字塔嵌入到单阶段检测器中. ...

  7. 论文阅读笔记四十四:RetinaNet:Focal Loss for Dense Object Detection(ICCV2017)

    论文原址:https://arxiv.org/abs/1708.02002 github代码:https://github.com/fizyr/keras-retinanet 摘要 目前,具有较高准确 ...

  8. Adversarial Examples for Semantic Segmentation and Object Detection 阅读笔记

    Adversarial Examples for Semantic Segmentation and Object Detection (语义分割和目标检测中的对抗样本) 作者:Cihang Xie, ...

  9. 论文阅读笔记三十五:R-FCN:Object Detection via Region-based Fully Convolutional Networks(CVPR2016)

    论文源址:https://arxiv.org/abs/1605.06409 开源代码:https://github.com/PureDiors/pytorch_RFCN 摘要 提出了基于区域的全卷积网 ...

随机推荐

  1. iframe 标签属性解读

    iframe 元素会创建包含另外一个文档的内联框架(即行内框架)

  2. Mysql LIMIT 分页

    格式: LIMIT index, size     // index:从哪一行(第几条)开始查,size:多少条 分页: LIMIT (currentPage-1)*pageSize, pageSiz ...

  3. 自己动手写Android插件化框架,让老板对你刮目相看

    欢迎大家前往腾讯云+社区,获取更多腾讯海量技术实践干货哦~ 本文由达文西发表于云+社区专栏 最近在工作中接触到了Android插件内的开发,发现自己这种技术还缺乏最基本的了解,以至于在一些基本问题上浪 ...

  4. Visual Stuido Online:如何禁止多人同时签出同一文件

    这里只说操作步骤,不讨论为什么要禁止同时多个签出同一文件. 版权声明:转载请保留原文链接. 友情链接:http://www.zhoumy.cn

  5. php 接口与前端数据交互实现

    最近在做前后端数据交互的尝试,也跳了很多坑,使用的是php+bootstrap-table+js,把一些收获记录在这里,方便查询. 这个小项目,仅有3个文件,分别为: crud.html data.p ...

  6. EasyUI动画效果

    1.jQuery动画效果 a)基本效果 >show(speed),显示 >hide(speed),隐藏 >toggle(speed),切换 b)滑动的效果 >slideUp(s ...

  7. Scala之Calendar,SimpleDateFormat简单用法

    package com.dingxin.entrance import java.text.SimpleDateFormat import java.util.{Calendar, Date} /** ...

  8. java抓取网页或者文件的邮箱号码

    抓文件的 package reg; import java.io.BufferedReader; import java.io.FileNotFoundException; import java.i ...

  9. 自动化测试基础篇--Selenium单选框(Radio)复选框(CheckBox)

    摘自:https://www.cnblogs.com/sanzangTst/p/7686602.html 一.什么是单选框.复选框? 二.单选框:radio 三.复选框:checkbox 四.判断是否 ...

  10. EOS智能合约开发(一):EOS环境搭建和启动节点

    EOS和以太坊很像,EOS很明确的说明它就是一个区块链的操作系统,BM在博客中也是说过的. 可以这样比喻,EOS就相当于内置激励系统的Windows/Linux/MacOS,这是它的一个定位. 包括以 ...