SOFT-NMS (二) (non maximum suppression,非极大值抑制)
import numpy as np boxes = np.array([[200, 200, 400, 400], [220, 220, 420, 420], [200, 240, 400, 440], [240, 200, 440, 400], [1, 1, 2, 2]], dtype=np.float32)
boxscores = np.array([0.9, 0.8, 0.7, 0.6, 0.5], dtype=np.float32) # 不放在里面是因为scores会发生变化 def softnms(boxes_x1y1x2y2, score_boxes, threshold_iou = 0.3, sigma=0.5, threshold_end=0.001, method=2):
"""
softnms
:param boxes_x1y1x2y2: boexs 坐标矩阵 format [y1, x1, y2, x2]
:param score_boxes: 每个 boxes 对应的分数
:param threshold_iou: 其余boxes与一个最好score的box分别求解iou,大于score_boxes值表示与该box重叠很高的重复框去掉,小于该值的可能是其它object的框, 交叠门限
:param sigma: 使用 gaussian 函数的方差
:param threshold_end: 最后的分数门限,将一次一次求解最好的box对应的iou值保存在scores中,而后保留大于此阈值的scores对应boxes
:param method: 使用的方法
:return: 留下的 boxes 的 index
"""
# indexes concatenate boxes with the last column
N = boxes_x1y1x2y2.shape[0] # 表示boxes的个数
# the order of boxes coordinate is [y1,x1,y2,x2]
y1 = boxes_x1y1x2y2[:, 0]
x1 = boxes_x1y1x2y2[:, 1]
y2 = boxes_x1y1x2y2[:, 2]
x2 = boxes_x1y1x2y2[:, 3]
scores = score_boxes
areas = (x2 - x1 + 1) * (y2 - y1 + 1)
for i in range(N):
# intermediate parameters for later parameters exchange
tBD = boxes_x1y1x2y2[i, :].copy() # 对应的第i个坐标
tscore = scores[i].copy() # 对应的第i个scores
tarea = areas[i].copy() # 对应的第i个面积
pos = i + 1 # pos为跳过当前第i个的box的索引
if i != N - 1: # 判断i是否取最大了
maxscore = np.max(scores[pos:], axis=0) # 跳过当前第i个后的scores分数取最大的
maxpos = np.argmax(scores[pos:], axis=0) # 跳过当前第i个后的scores分数取最大的分数对应的索引
else:
maxscore = scores[-1] # 如果没有(即只有一个),就取这个
maxpos = 0 # 因为只有一个,即索引为0
if tscore < maxscore: # 第一个与
# 每次把最高score的 往上拿
boxes_x1y1x2y2[i, :] = boxes_x1y1x2y2[maxpos + i + 1, :] # 行置换,score,area也一样
boxes_x1y1x2y2[maxpos + i + 1, :] = tBD
tBD = boxes_x1y1x2y2[i, :]
scores[i] = scores[maxpos + i + 1]
scores[maxpos + i + 1] = tscore
tscore = scores[i]
areas[i] = areas[maxpos + i + 1]
areas[maxpos + i + 1] = tarea
tarea = areas[i]
# IoU calculate
xx1 = np.maximum(boxes_x1y1x2y2[i, 1], boxes_x1y1x2y2[pos:, 1])
yy1 = np.maximum(boxes_x1y1x2y2[i, 0], boxes_x1y1x2y2[pos:, 0])
xx2 = np.minimum(boxes_x1y1x2y2[i, 3], boxes_x1y1x2y2[pos:, 3])
yy2 = np.minimum(boxes_x1y1x2y2[i, 2], boxes_x1y1x2y2[pos:, 2])
w = np.maximum(0.0, xx2 - xx1 + 1)
h = np.maximum(0.0, yy2 - yy1 + 1)
inter = w * h
ovr = inter / (areas[i] + areas[pos:] - inter)
# Three methods: 1.linear 2.gaussian 3.original NMS
if method == 1: # linear
weight = np.ones(ovr.shape)
weight[ovr > threshold_iou] = weight[ovr > threshold_iou] - ovr[ovr > threshold_iou]
elif method == 2: # gaussian
weight = np.exp(-(ovr * ovr) / sigma)
else: # original NMS
weight = np.ones(ovr.shape)
weight[ovr > threshold_iou] = 0 # 表示大于Nt的iou值就和比较的box重合很多,因此大于这个阈值的iou为0,则相当于该score为0
scores[pos:] = weight * scores[pos:] # 给定scores的值,若不好就删除
# select the boxes and keep the corresponding indexes
boxes_new = boxes_x1y1x2y2[scores > threshold_end] # 判断求解的scores是否满足阈值,若满足就保留
return boxes_new
# boxes and scores boees_slect = softnms(boxes, boxscores, method=3)
print(boees_slect) 原始NMS结果如下:

线性soft-nms:

高斯nms:

SOFT-NMS (二) (non maximum suppression,非极大值抑制)的更多相关文章
- NMS(Non-Maximum Suppression) 非极大值抑制
NMS 非极大值抑制:找到局部最大值,并删除邻域内其他的值. 简单说一下流程: 首先剔除背景(背景无需NMS),假设有6个边界框,根据分类置信度对这6个边界框做降序排列,假设顺序为A.B.C.D.E ...
- Non-maximum suppression(非极大值抑制算法)
在RCNN系列目标检测中,有一个重要的算法,用于消除一些冗余的bounding box,这就是non-maximum suppression算法. 这里有一篇博客写的挺好的: http://www.c ...
- 非极大值抑制(Non-Maximum Suppression,NMS)
概述 非极大值抑制(Non-Maximum Suppression,NMS),顾名思义就是抑制不是极大值的元素,可以理解为局部最大搜索.这个局部代表的是一个邻域,邻域有两个参数可变,一是邻域的维数,二 ...
- 非极大值抑制Non-Maximum Suppression(NMS)
非极大值抑制(Non-Maximum Suppression,NMS) 概述 非极大值抑制(Non-Maximum Suppression,NMS),顾名思义就是抑制不是极大值的元素,可以理解为局 ...
- Non-Maximum Suppression,NMS非极大值抑制
Non-Maximum Suppression,NMS非极大值抑制概述非极大值抑制(Non-Maximum Suppression,NMS),顾名思义就是抑制不是极大值的元素,可以理解为局部最大搜索. ...
- 目标检测 非极大值抑制(Non-Maximum Suppression,NMS)
非极大值抑制(Non-Maximum Suppression,NMS),顾名思义就是抑制不是极大值的元素,可以理解为局部最大搜索.也可以理解为只取置信度最高的一个识别结果. 举例:  如图所示,现在 ...
- 非极大值抑制(NMS)
转自:https://www.cnblogs.com/makefile/p/nms.html 概述 非极大值抑制(Non-Maximum Suppression,NMS),顾名思义就是抑制不是极大值的 ...
- 非极大值抑制(NMS)的几种实现
因为之前对比了RoI pooling的几种实现,发现python.pytorch的自带工具函数速度确实很慢,所以这里再对Faster-RCNN中另一个速度瓶颈NMS做一个简单对比试验. 这里做了四组对 ...
- 非极大值抑制(NMS)
非极大值抑制顾名思义就是抑制不是极大值的元素,搜索局部的极大值.这个局部代表的是一个邻域,邻域有两个参数可变,一个是邻域的维数,二是邻域的大小.这里不讨论通用的NMS算法,而是用于在目标检测中提取分数 ...
- IoU与非极大值抑制(NMS)的理解与实现
1. IoU(区域交并比) 计算IoU的公式如下图,可以看到IoU是一个比值,即交并比. 在分子中,我们计算预测框和ground-truth之间的重叠区域: 分母是并集区域,或者更简单地说,是预测框和 ...
随机推荐
- sharding jdbc(sphere) 3.1.0 spring boot配置
sharding jdbc 2.x系列详解参见https://www.cnblogs.com/zhjh256/p/9221634.html. 最近将sharding jdbc的配置从xml切换到了sp ...
- vue form表单上传文件
<script src="https://cdn.staticfile.org/vue-resource/1.5.1/vue-resource.min.js">< ...
- .netcore里使用StackExchange.Redis TimeOut 情况解决方法
在用StackExchange.Redis这个组件时候,时不时会出现异常TimeOut解决方法如下, 解决方法: 在Program的Main入口方法里添加一句话: System.Threading.T ...
- C++ vector使用实例
C++ vector #include <iostream> #include <vector> #include <string> #include <al ...
- light4j一个轻量级的低延时、高吞吐量、内存占用量小的API平台
1.背景(abstract) 笔者算是一个极客类型的程序员了.喜欢探索一些程序内在的原理.稳定性.自动化运维.健壮性,很多时间也会 去对程序的内存使用率.cpu使用率锱铢必较.尽量克扣掉不必要的cpu ...
- 多用户远程连接设置(WindowsServer2008/Win7)
一.Windows server2008 1.点击计算机--->右键属性打开系统对话框.进行如图设置. 2.在开始菜单--->运行中输入gpedit.msc打开本地组策略编辑器对话框. 3 ...
- ASP.NET-------GridView中的字段居中不了
在使用Grid View 控件的时候,回合一些css 放在一块使用之后你会发现 字段没有居中 你会发现该什么都不行 比如: HeaderStyle-HorizontalAlign="Cen ...
- 安装cnpm出现问题
安装cnpm: 命令行中输入 npm install -g cnpm --registry=http://registry.npm.taobao.org 报:cnpm不是内部命令 解决方法:设置环 ...
- 01-Windows Server 2012的配置与部署
一. 背景 这里以阿里云Windows Server 2012系统的服务器为主,介绍服务器的配置以及.Net程序的发布顺序,在后续的项目管理文章中,会介绍<运维手册>的写法. 二. 步骤 ...
- OpenLayers加载百度离线瓦片地图(完美无偏移)
本文使用OpenLayers最新版本V5.3.0演示:如何使用OpenLayer完美无偏移加载百度离线瓦片地图.OpenLayers 5.3.0下载地址为:https://github.com/ope ...