1.从官方文档https://github.com/bharatsingh430/soft-nms/blob/master/lib/nms/cpu_nms.pyx,下载cython实现的softnms的代码  记住最后一行的把return keep改为return boxes[keep,:],具体如下

# --------------------------------------------------------
# Deformable Convolutional Networks
# Copyright (c) 2015 Microsoft
# Licensed under The MIT License [see LICENSE for details]
# Modified from py-faster-rcnn (https://github.com/rbgirshick/py-faster-rcnn)
# --------------------------------------------------------import numpy as np
cimport numpy as npcdef inline np.float32_t max(np.float32_t a, np.float32_t b):return a if a >= b else bcdef inline np.float32_t min(np.float32_t a, np.float32_t b):return a if a <= b else bdef cpu_soft_nms(np.ndarray[float, ndim=2] boxes, float sigma=0.5, float Nt=0.3, float threshold=0.001, unsigned int method=0):cdef unsigned int N = boxes.shape[0]cdef float iw, ih, box_areacdef float uacdef int pos = 0cdef float maxscore = 0cdef int maxpos = 0cdef float x1,x2,y1,y2,tx1,tx2,ty1,ty2,ts,area,weight,ovfor i in range(N):maxscore = boxes[i, 4]maxpos = itx1 = boxes[i,0]ty1 = boxes[i,1]tx2 = boxes[i,2]ty2 = boxes[i,3]ts = boxes[i,4]pos = i + 1# get max boxwhile pos < N:if maxscore < boxes[pos, 4]:maxscore = boxes[pos, 4]maxpos = pospos = pos + 1# add max box as a detection boxes[i,0] = boxes[maxpos,0]boxes[i,1] = boxes[maxpos,1]boxes[i,2] = boxes[maxpos,2]boxes[i,3] = boxes[maxpos,3]boxes[i,4] = boxes[maxpos,4]# swap ith box with position of max boxboxes[maxpos,0] = tx1boxes[maxpos,1] = ty1boxes[maxpos,2] = tx2boxes[maxpos,3] = ty2boxes[maxpos,4] = tstx1 = boxes[i,0]ty1 = boxes[i,1]tx2 = boxes[i,2]ty2 = boxes[i,3]ts = boxes[i,4]pos = i + 1# NMS iterations, note that N changes if detection boxes fall below thresholdwhile pos < N:x1 = boxes[pos, 0]y1 = boxes[pos, 1]x2 = boxes[pos, 2]y2 = boxes[pos, 3]s = boxes[pos, 4]area = (x2 - x1 + 1) * (y2 - y1 + 1)iw = (min(tx2, x2) - max(tx1, x1) + 1)if iw > 0:ih = (min(ty2, y2) - max(ty1, y1) + 1)if ih > 0:ua = float((tx2 - tx1 + 1) * (ty2 - ty1 + 1) + area - iw * ih)ov = iw * ih / ua #iou between max box and detection boxif method == 1: # linearif ov > Nt: weight = 1 - ovelse:weight = 1elif method == 2: # gaussianweight = np.exp(-(ov * ov)/sigma)else: # original NMSif ov > Nt: weight = 0else:weight = 1boxes[pos, 4] = weight*boxes[pos, 4]# if box score falls below threshold, discard the box by swapping with last box# update Nif boxes[pos, 4] < threshold:boxes[pos,0] = boxes[N-1, 0]boxes[pos,1] = boxes[N-1, 1]boxes[pos,2] = boxes[N-1, 2]boxes[pos,3] = boxes[N-1, 3]boxes[pos,4] = boxes[N-1, 4]N = N - 1pos = pos - 1pos = pos + 1keep = [i for i in range(N)]return boxes[keep,:]

2.创建setup.py文件

from distutils.core import setup
from Cython.Build import cythonizesetup(name = 'softnms_module',ext_modules = cythonize('cpu_nms.pyx'),)

3.开始生成动态链接库so文件和o文件,以及c源代码:

python setup.py build

我在这里出现了错误,提示numpy/arrayobject.h: No such file or directory,usr/include/python2.7下面的numpy的软连接,出现错误,然后顺势找到/usr/lib/python2.7/dist-packages发现没有numpy文件夹,然后按照网上的提示 sudo apt-get install python-numpy,提示我已经安装了,然后我在终端下运行cython,import numpy也没有问题。后来pip install numpy,然后发下/usr/lib/python2.7/dist-packages还是发现没有numpy文件夹,之后想起了我是多用户的,在/home/ygx/.local/lib/python2.7/site-packages/numpy,把这个文件夹复制到/usr/lib/python2.7/dist-packages,问题解决。

4.将生成的build文件夹下面的so文件,拷贝到主文件夹下面,删除build文件夹,即可。

5.之后下面是softnms.py的文件,该文件作用是单独执行softnms操作,可调参数有sigma和method,method如果是0就和普通的nms没差。注意我这里的的nms_thresh其实就是置信度的阈值,和普通的不一样。你把它设置成和原先检测时置信度的阈值就可以了,因为重叠区域高的置信度不会变,会将相对低的变的更低,比如0.4变成0.1这样。

#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Created on Mon Apr  9 21:09:09 2018
对每一类生成的prebbox.txt进行softnms操作
1.读入文本,读入bbox
@author: ygx
"""
import sys
defaultencoding = 'utf-8'
if sys.getdefaultencoding() != defaultencoding:  reload(sys)  sys.setdefaultencoding(defaultencoding)import os
import numpy as np
from cpu_nms import cpu_soft_nmsnms_thresh = 0.45 #这个和你之前设置的置信度的阈值一样就好了def custombasename(fullname):return os.path.basename(os.path.splitext(fullname)[0])def GetFileFromThisRootDir(dir,ext = None):allfiles = []needExtFilter = (ext != None)for root,dirs,files in os.walk(dir):for filespath in files:filepath = os.path.join(root, filespath)extension = os.path.splitext(filepath)[1][1:]if needExtFilter and extension in ext:allfiles.append(filepath)elif not needExtFilter:allfiles.append(filepath)return allfilesdef cpu_soft_nms_ygx(dets, sigma=0.5, Nt=0.3, threshold=0.001, method=1):keep = cpu_soft_nms(np.ascontiguousarray(dets, dtype=np.float32),np.float32(sigma), np.float32(Nt),np.float32(threshold),np.uint8(method))return keepdef nmsbynamedict(nameboxdict, nms):nameboxnmsdict = {x: [] for x in nameboxdict}for imgname in nameboxdict:#print('imgname:', imgname)#keep = py_cpu_nms(np.array(nameboxdict[imgname]), thresh)#print('type nameboxdict:', type(nameboxnmsdict))#print('type imgname:', type(imgname))#print('type nms:', type(nms))keep = nms(np.array(nameboxdict[imgname]))#======================ygx=====================#keep = nms(np.array(keep))#又多进行了一层的softnms###############################################print('keep:', keep)outdets = []#print('nameboxdict[imgname]: ', nameboxnmsdict[imgname])#======================ygx=====================all_num = len(keep)keep_soft = [] for i in range(all_num):#可以调试的时候看一下if keep[i][4]>nms_thresh:keep_soft.append(i)else:continuefor index in keep_soft:# print('index:', index)outdets.append(keep[index])#千万 注意在softnms之后索引已经发生了变化。。。#outdets.append(nameboxdict[imgname][index])nameboxnmsdict[imgname] = outdetsreturn nameboxnmsdict#我要将他改成softnms,对15类最终的txt进行softnms
def softnmsbase(srcpath, dstpath, nms):filelist = GetFileFromThisRootDir(srcpath)for fullname in filelist:name = custombasename(fullname)#print('name:', name)dstname = os.path.join(dstpath, name + '_softnms_'+str(nms_thresh)+'.txt')with open(fullname, 'r') as f_in:nameboxdict = {}lines = f_in.readlines()splitlines = [x.strip().split(' ') for x in lines]for splitline in splitlines:oriname = splitline[0]confidence = splitline[1]poly = list(map(float, splitline[2:]))det = poly#det.append(classname)det.append(confidence)det = list(map(float, det))if (oriname not in nameboxdict):nameboxdict[oriname] = []nameboxdict[oriname].append(det)nameboxnmsdict = nmsbynamedict(nameboxdict, nms)with open(dstname, 'w') as f_out:for imgname in nameboxnmsdict:for det in nameboxnmsdict[imgname]:#print('det:', det)confidence = det[-1]bbox = det[0:-1]outline = str(imgname) + ' '  + ' '.join(map(str, bbox))#outline = imgname + ' ' + str(confidence) + ' ' + ' '.join(map(str, bbox))#print('outline:', outline)f_out.write(outline + '\n')if __name__ == '__main__':#mergebypoly()srcpath = r'/home/ygx/666/nms之前'dstpath = r'/home/ygx/666/nms之后'softnmsbase(srcpath,dstpath,cpu_soft_nms_ygx)

参考博客:http://www.cnblogs.com/king-lps/p/9031568.html

对检测出来的结果单独进行softnms操作相关推荐

  1. 深度学习目标检测常用工具型代码:对检测出来的结果单独进行nms操作

    p.s. 这里的例子我说的都是航空影像目标,所以大家考虑的时候都要考虑成俯视图的状态. 多目标检测的时候,有容易发生拥挤的类别,比如小汽车,也有不易拥挤的类别,比如篮球场.所以需要不同的nms阈值,而 ...

  2. linux下float的寄存器,检测x86上Linux的非正常浮动操作(Detecting denormal float operations on Linux for x86)...

    检测x86上Linux的非正常浮动操作(Detecting denormal float operations on Linux for x86) 我正在将一个windows程序移植到linux的过程 ...

  3. 一文看懂目标检测之RCNN(NMS和Soft-NMS算法)

    目标检测之RCNN 1. RCNN:Regions with CNN features[2014 Ross] 图片输入 区域建议(region proposal) 特征提取(feature extra ...

  4. 计算机视觉检测 白皓月,基于视线跟踪的操作界面的人机交互方法研究

    摘要: 人机交互主要是研究人与计算机之间的信息交换,是与人机工程学,认知心理学,虚拟现实技术,多媒体技术等密切相关的综合学科.本文研究的基于视线跟踪的操作界面的人机交互方法采用桌面式双目立体视觉实时跟 ...

  5. 如何使用Soft-NMS实现目标检测并提升准确率

    用一行代码提升目标检测准确率 论文摘要 非最大抑制(Non-maximum suppression, NMS)是物体检测流程中重要的组成部分.它首先基于物体检测分数产生检测框,分数最高的检测框M被选中 ...

  6. 【目标检测系列】非极大值抑制(NMS)的各类变体汇总

    关注上方"深度学习技术前沿",选择"星标公众号", 技术干货,第一时间送达! [导读]前面已经先后为大家详细介绍过了目标检测领域的基础知识:[目标检测基础积累] ...

  7. 详解计算机视觉中的特征点检测:Harris / SIFT / SURF / ORB

    作者丨Encoder@知乎 来源丨https://zhuanlan.zhihu.com/p/36382429 编辑丨极市平台 本文仅用于学术分享,若侵权,联系后台作删文处理.极市导读 Harris角点 ...

  8. 利用RGB-D数据进行人体检测 带dataset

    利用RGB-D数据进行人体检测 LucianoSpinello, Kai O. Arras 摘要 人体检测是机器人和智能系统中的重要问题.之前的研究工作使用摄像机和2D或3D测距器.本文中我们提出一种 ...

  9. OpenCV对象检测实例

    https://www.toutiao.com/a6644490462834983438/ 2019-01-09 21:36:31 在本文中,我将演示如何跟踪传送带上的对象.这可以用于在传送带上定位对 ...

最新文章

  1. android tab 悬停效果代码,Android 仿腾讯应用宝 之 Toolbar +Scroolview +tab滑动悬停效果...
  2. 智能的源泉,大脑从何而来?
  3. 以下内容仅对你可见个性签名_这些微信个性签名,有你喜欢的吗?
  4. 辽师大计算机科学与技术专业怎么样,性价比很高的大学,辽师大的优势专业分析!家长请收藏...
  5. golang 反射_golang原理篇- nil:接口类型和值类型的区别
  6. python三角函数拟合_使用python进行数据拟合最小化函数
  7. pip命令安装pygeme后 IDLE import pygame报错
  8. 【图像处理】形态学及其它集合运算(Morphological and Other Set Operations)
  9. ROS保姆级0基础入门教程⭐ |第一章 ROS的概述与环境搭建(4万字教程,建议收藏)
  10. Spring.net(一)----Spring.NET框架简介及模块说明
  11. Linux下source ./bashrc出现的command not found: shopt问题
  12. Jersey学习笔记
  13. 北邮带研究生的计算机导师有哪些,GitHub - sunichi/BUPTNiceMentors: 北邮研究生导师口碑榜...
  14. MSVC创建的Qt工程图标设置
  15. js利用点击事件更换皮肤
  16. D200和D2X区别
  17. Project directory ‘x/x/x‘ is not part of the build defined by settings file ‘x/x/x‘. If this is ...
  18. Josh 的学习笔记之 Verilog(Part 4——RTL 概念与常用 RTL 建模)
  19. 文献阅读-10X单细胞揭示肿瘤浸润性T细胞的泛癌单细胞图谱
  20. 荷兰专用服务器1g无限流量,sharktech:荷兰机房1Gbps带宽不限流量服务器简单测评...

热门文章

  1. 美团点评技术与算法文章汇总,设计算法、前后端、客户端、小程序等
  2. 3DMark Sky Driver
  3. 浏览器和服务器的交互过程
  4. 7月7日易用性SIG技术分享活动精彩回顾
  5. POLARDB:向着更快、更高、更强不断前行!
  6. 海思35XX系列芯片型号规律
  7. 黄色-图片识别引擎的一些心得
  8. CobaltStrike二次开发环境准备以及免杀
  9. 既约分数(python)
  10. 数据存在内存里的格式是什么?