类间:也就是不同类之间也进行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类间+类内的更多相关文章

  1. 使用PHP发送邮件的两种方法

    使用PHP发送邮件的两种方法 May242013 作者:Jerry Bendy   发布:2013-05-24 22:25   分类:PHP   阅读:2,107 views   抢沙发     今天 ...

  2. C++类的实例化的两种方法

    C++ 类的实例化有两种方法: 直接定义对象: 先定义一个类:   class A { public: A(); virtual ~A(); ... ... };   类实现略. 用的时候: A a; ...

  3. Js类的静态方法与实例方法区分以及jQuery如何拓展两种方法

    上学时C#老师讲到对象有两类方法,静态方法(Static)和实例方法(非Static),当时不理解静态是为何意,只是强记. 后来从事前端工作,一直在对类(即对象,Js中严格来说没有类的定义,虽众所周知 ...

  4. [转载]C#读写txt文件的两种方法介绍

    C#读写txt文件的两种方法介绍 by 大龙哥 1.添加命名空间 System.IO; System.Text; 2.文件的读取 (1).使用FileStream类进行文件的读取,并将它转换成char ...

  5. .net中创建xml文件的两种方法

    .net中创建xml文件的两种方法 方法1:根据xml结构一步一步构建xml文档,保存文件(动态方式) 方法2:直接加载xml结构,保存文件(固定方式) 方法1:动态创建xml文档 根据传递的值,构建 ...

  6. 实现LRU的两种方法---python实现

    这也是豆瓣2016年的一道笔试题... 参考:http://www.3lian.com/edu/2015/06-25/224322.html LRU(least recently used)就不做过多 ...

  7. C#实现Dll(OCX)控件自动注册的两种方法 网上找的 然后 自己试了试 还是可以用的

    尽管MS为我们提供了丰富的.net framework库,我们的程序C#开发带来了极大的便利,但是有时候,一些特定功能的控件库还是需要由第三方提供或是自己编写.当需要用到Dll引用的时候,我们通常会通 ...

  8. Android 抗锯齿的两种方法

    Android 抗锯齿的两种方法 (其一:paint.setAntiAlias(ture);paint.setBitmapFilter(true))   在Android中,目前,我知道有两种出现锯齿 ...

  9. C# web api返回类型设置为json的两种方法

    web api写api接口时默认返回的是把你的对象序列化后以XML形式返回,那么怎样才能让其返回为json呢,下面就介绍两种方法: 方法一:(改配置法) 找到Global.asax文件,在Applic ...

  10. Intent传递对象的两种方法(Serializable,Parcelable) (转)

    今天讲一下Android中Intent中如何传递对象,就我目前所知道的有两种方法,一种是Bundle.putSerializable(Key,Object);另一种是Bundle.putParcela ...

随机推荐

  1. 1、eureka的注册流程

    客户端注册到服务端是通过http请求的 涉及到多级缓存 register注册表 源码精髓:多级缓存设计思想 在拉取注册表的时候: 首先从ReadOnlyCacheMap里查缓存的注册表. 若没有,就找 ...

  2. IDE中使用Git提交代码报错:Push to origin/release-V2 was rejected

    一.问题由来 当前项目开发好之后,已经正常稳定运行一两个月,在使用过程中基本上没在出现什么BUG.因此公司在讨论准备开发二期项目,自己 就在之前的基础之上,使用git创建了分支,一个分支release ...

  3. use shell scrpit to jlink download bin file

    一 JLINK 下载 JLINK作为一个arm的调试工具,是很多基于arm芯片无法绕过去的调试和下载工具.这里有一个问题,就是该工具链接和使用需要的命令特别多,假如不做成脚本,会浪费很多时间,笔者花了 ...

  4. Rancher 2.5.x 证书过期报错 x509: certificate has expired or is not yet valid 解决方案

    Rancher 的证书过期会出现什么状况?不可以继续通过Rancher UI访问你的集群 查看Rancher Server日志报错:x509: certificate has expired or i ...

  5. 记录--webpack和vite原理

    这里给大家分享我在网上总结出来的一些知识,希望对大家有所帮助 前言 每次用vite创建项目秒建好,前几天用vue-cli创建了一个项目,足足等了我一分钟,那为什么用 vite 比 webpack 要快 ...

  6. 记录--一个好用的轮子 turn.js 实现仿真翻书的效果

    这里给大家分享我在网上总结出来的一些知识,希望对大家有所帮助 国际惯例,官网链接 官网传送门 Github地址 github上有几个demos例子,介绍了基础用法. 我参考官网的例子,写了一个demo ...

  7. Elasticsearch索引不存在时,查询接口报错怎么办?

    1.索引不存在,报错:type=index_not_found_exception, reason=no such index解决办法: DSL: GET /text_index_003/_searc ...

  8. 移动端弹性布局方案lib-flexible实践

    2个月前,写过一篇文章<从网易与淘宝的font-size思考前端设计稿与工作流>总结过一些移动web中有关手机适配的一些思路,当时也是因为工作的关系分析了下网易跟淘宝的移动页面,最后才有那 ...

  9. Java 中文、unicode编码互转 ;汉字、二进制字符串互转

    //中文转unicode编码 public static String gbEncoding(final String gbString) { char[] utfBytes = gbString.t ...

  10. 搭建Spring Cloud父工程

    1.首先创建一个maven项目 删除src目录,当做一级目录用来管理第三方jar版本控制. 2.配置pom文件. SpringCloud.SpringCloudAlibaba.SpringBoot版本 ...