pytorch,numpy两种方法实现nms类间+类内
类间:也就是不同类之间也进行nms
类内:就是只把同类的bboxes进行nms
numpy实现 nms类间+类内:
import numpy as np
# 类间nms
def nms(bboxes, scores, thresh):
x1, y1, x2, y2 = bboxes[:, 0], bboxes[:, 1], bboxes[:, 2], bboxes[:, 3]
areas = (x2 - x1 + 1) * (y2 - y1 + 1)
# 按照score降序排序(保存的是索引)
indices = scores.argsort()[::-1]
indice_res = []
while indices.size > 0:
i = indices[0]
indice_res.append(i)
inter_x1 = np.maximum(x1[i], x1[indices[1:]])
inter_y1 = np.maximum(y1[i], y1[indices[1:]])
inter_x2 = np.minimum(x2[i], x2[indices[1:]])
inter_y2 = np.minimum(y2[i], y2[indices[1:]])
inter_w = np.maximum(0.0, inter_x2 - inter_x1 + 1)
inter_h = np.maximum(0.0, inter_y2 - inter_y1 + 1)
inter_area = inter_w * inter_h
union_area = areas[i] + areas[indices[1:]] - inter_area + 1e-6
ious = inter_area / union_area
idxs = np.where(ious < thresh)[0] # np.where(ious < thresh)返回的是一个tuple,第一个元素是一个满足条件的array
indices = indices[idxs + 1]
return indice_res
# 类内nms,把不同类别的乘以一个偏移量,把不同类别的bboxes给偏移到不同位置。
def class_nms(bboxes, scores, cat_ids, iou_threshold):
'''
:param bboxes: np.array, shape of (N, 4), N is the number of bboxes, np.float32
:param scores: np.array, shape of (N, 1), np.float32
:param cat_ids: np.array, shape of (N, 1),np.int32
:param iou_threshold: float
'''
max_coordinate = bboxes.max()
# 为每一个类别/每一层生成一个足够大的偏移量,使不同类别的bboxes不会相交
offsets = cat_ids * (max_coordinate + 1)
# bboxes加上对应类别的偏移量后,保证不同类别之间bboxes不会有重合的现象
bboxes_for_nms = bboxes + offsets[:, None]
indice_res = nms(bboxes_for_nms, scores, iou_threshold)
return indice_res
torch实现 nms类间+类内:
import torch
# 类间nms
def nms(bboxes, scores, thresh):
x1, y1, x2, y2 = bboxes[:, 0], bboxes[:, 1], bboxes[:, 2], bboxes[:, 3]
areas = (x2 - x1 + 1) * (y2 - y1 + 1)
# 按照score降序排序(保存的是索引)
# values, indices = torch.sort(scores, descending=True)
indices = scores.sort(descending=True)[1] # torch
indice_res = torch.randn([1, 4]).to(bboxes)
while indices.size()[0] > 0: # indices.size()是一个Size对象,我们要取第一个元素是int,才能比较
save_idx, other_idx = indices[0], indices[1:]
indice_res = torch.cat((indice_res, bboxes[save_idx].unsqueeze(0)),
dim=0) # unsqueeze是添加一个维度,让bboxes.shape从[4]-->[1,4]
inter_x1 = torch.max(x1[save_idx], x1[other_idx])
inter_y1 = torch.max(y1[save_idx], y1[other_idx])
inter_x2 = torch.min(x2[save_idx], x2[other_idx])
inter_y2 = torch.min(y2[save_idx], y2[other_idx])
inter_w = torch.max(inter_x2 - inter_x1 + 1, torch.tensor(0).to(bboxes))
inter_h = torch.max(inter_y2 - inter_y1 + 1, torch.tensor(0).to(bboxes))
inter_area = inter_w * inter_h
union_area = areas[save_idx] + areas[other_idx] - inter_area + 1e-6
iou = inter_area / union_area
indices = other_idx[iou < thresh]
return indice_res[1:]
# 类内nms,把不同类别的乘以一个偏移量,把不同类别的bboxes给偏移到不同位置。
def class_nms(bboxes, scores, cat_ids, iou_threshold):
'''
:param bboxes: torch.tensor([n, 4], dtype=torch.float32)
:param scores: torch.tensor([n], dtype=torch.float32)
:param cat_ids: torch.tensor([n], dtype=torch.int32)
:param iou_threshold: float
'''
max_coordinate = bboxes.max()
# 为每一个类别/每一层生成一个很大的偏移量
offsets = cat_ids * (max_coordinate + 1)
# bboxes加上对应类别的偏移量后,保证不同类别之间bboxes不会有重合的现象
bboxes_for_nms = bboxes + offsets[:, None]
indice_res = nms(bboxes_for_nms, scores, iou_threshold)
return indice_res
pytorch,numpy两种方法实现nms类间+类内的更多相关文章
- 使用PHP发送邮件的两种方法
使用PHP发送邮件的两种方法 May242013 作者:Jerry Bendy 发布:2013-05-24 22:25 分类:PHP 阅读:2,107 views 抢沙发 今天 ...
- C++类的实例化的两种方法
C++ 类的实例化有两种方法: 直接定义对象: 先定义一个类: class A { public: A(); virtual ~A(); ... ... }; 类实现略. 用的时候: A a; ...
- Js类的静态方法与实例方法区分以及jQuery如何拓展两种方法
上学时C#老师讲到对象有两类方法,静态方法(Static)和实例方法(非Static),当时不理解静态是为何意,只是强记. 后来从事前端工作,一直在对类(即对象,Js中严格来说没有类的定义,虽众所周知 ...
- [转载]C#读写txt文件的两种方法介绍
C#读写txt文件的两种方法介绍 by 大龙哥 1.添加命名空间 System.IO; System.Text; 2.文件的读取 (1).使用FileStream类进行文件的读取,并将它转换成char ...
- .net中创建xml文件的两种方法
.net中创建xml文件的两种方法 方法1:根据xml结构一步一步构建xml文档,保存文件(动态方式) 方法2:直接加载xml结构,保存文件(固定方式) 方法1:动态创建xml文档 根据传递的值,构建 ...
- 实现LRU的两种方法---python实现
这也是豆瓣2016年的一道笔试题... 参考:http://www.3lian.com/edu/2015/06-25/224322.html LRU(least recently used)就不做过多 ...
- C#实现Dll(OCX)控件自动注册的两种方法 网上找的 然后 自己试了试 还是可以用的
尽管MS为我们提供了丰富的.net framework库,我们的程序C#开发带来了极大的便利,但是有时候,一些特定功能的控件库还是需要由第三方提供或是自己编写.当需要用到Dll引用的时候,我们通常会通 ...
- Android 抗锯齿的两种方法
Android 抗锯齿的两种方法 (其一:paint.setAntiAlias(ture);paint.setBitmapFilter(true)) 在Android中,目前,我知道有两种出现锯齿 ...
- C# web api返回类型设置为json的两种方法
web api写api接口时默认返回的是把你的对象序列化后以XML形式返回,那么怎样才能让其返回为json呢,下面就介绍两种方法: 方法一:(改配置法) 找到Global.asax文件,在Applic ...
- Intent传递对象的两种方法(Serializable,Parcelable) (转)
今天讲一下Android中Intent中如何传递对象,就我目前所知道的有两种方法,一种是Bundle.putSerializable(Key,Object);另一种是Bundle.putParcela ...
随机推荐
- 23_FFmpeg像素格式转换
简介 前面使用 SDL 显示了一张YUV图片以及YUV视频.接下来使用Qt中的QImage来实现一个简单的 YUV 播放器,查看QImage支持的像素格式,你会发现QImage仅支持显示RGB像素格式 ...
- 11_使用SDL播放WAV
使用命令播放WAV 对于WAV文件来说,可以直接使用ffplay命令播放,而且不用像PCM那样增加额外的参数.因为WAV的文件头中已经包含了相关的音频参数信息. ffplay in.wav 接下来演示 ...
- Linux快速入门(四)Linux用户管理
root用户和普通用户 虽然root用户的的权限很大,但一般情况下,我们都不会直接使用root用户而是创建一个普通用户,这样可以避免因为权限过大带来的一些误操作,当使用一些需要权限的操作时,可以使用s ...
- 记录--使用率比较低的10个Web API
这里给大家分享我在网上总结出来的一些知识,希望对大家有所帮助 avaScript中有些API可能使用率比较低,下面我们逐一介绍它们的用法和使用场景. 至于标题,主要是想让你进来看看,兄弟们别打我! B ...
- SHELL使用教程
疑难解答 执行完shell文件后不退出 在shell文件末尾添加如下命令即可. exec /bin/bash 参考资料 为什么sh脚本运行之后自动退出,有没有让终端不自动关闭的方法. - Ubuntu ...
- linux xfce 在文件管理器里点击运行shell脚本文件
1.打开 Settings Editor 2.点击左边的 thunar 3.点击右边的 添加 ,在属性中输入 /misc-exec-shell-scripts-by-default 在类型中选择布尔类 ...
- 第十三届蓝桥杯大赛软件赛省赛【Java 大学B 组】试题C: 字符统计
1 import java.util.Scanner; 2 3 public class Main { 4 public static void main(String args[]) { 5 Sca ...
- MySQL查询表结构信息SQL语句
一.获取某数据库下的所有数据表名 SELECT TABLE_NAME, TABLE_COMMENT FROM information_schema.TABLES WHERE TABLE_SCHEMA ...
- .net 发邮件的小工具,包含json,环境变量,命令行参数三种配置方式
一.业务需求 在工作中遇到一个场景,软件bug或功能发布之后,会通知测试进行测试,要求写一个小工具能自动发送邮件,功能包含发送和抄送支持多个,因为只是通知没有写进附件功能,这个其他博客都有搜一下就可以 ...
- #Tarjan,SPFA#洛谷 3627 [APIO2009] 抢掠计划
题目 分析 首先重复走,钱只会计算一次,而且与路程长度无关,考虑有向图缩点,然后跑最长路,这里吧边权取反跑最短路,然后在酒吧结束也就是求\(-dis[col_x]\)的最大值,\(col_x\)也就是 ...