当前位置:网站首页>计算IoU(D2L)
计算IoU(D2L)
2022-08-01 17:37:00 【GY-赵】

#IoU
#给定两个边界框或锚框的列表
def box_iou(boxes1,boxes2):
""" 计算两个锚框或边界框列表中成对的交并比"""
box_area = lambda boxes : ((boxes[:,2]-boxes[:,0])*(boxes[:,3]-boxes[:,1])) #box面积
# boxes1:[nums_boxes1,4]
# boxes2:[nums_boxes2,4]
# areas1:[nums_boxes1,]
# areas2:[nums_boxes2,]
areas1 = box_area(boxes1)
areas2 = box_area(boxes2)
""" areas1:tensor([100., 225.]) areas2:tensor([144., 676.]) """
#print(f"areas1:{areas1}\n areas2:{areas2}")
""" inter_upperlefts ,inter_lowerrights,inters : shape:(nums_boxes1,nums_boxes2,2) box_axis:[xmin,ymin,xmax,ymax] """
inter_upperlefts = torch.max(boxes1[:,None,:2],boxes2[:,:2]) #[:2]左上角,两两组合所有左上角坐标max值
inter_lowerrights = torch.min(boxes1[:,None,2:],boxes2[:,2:]) #[2:]右下角
inters =(inter_lowerrights - inter_upperlefts).clamp(min=0) #clamp确保不出现负数
""" inter_upperlefts: 4组坐标左上角max值(与右下角一一对应) tensor([[[13., 13.], [22., 22.]], [[15., 15.], [22., 22.]]]) inter_lowerrights:4组坐标右下角min值 tensor([[[20., 20.], [20., 20.]], [[25., 25.], [30., 30.]]]) inters:(右下角-左上角)得到每一对box交集的(宽,高) tensor([[[ 7., 7.], [ 0., 0.]], [[10., 10.], [ 8., 8.]]]) """
print(f"inters:{
inters}")
# inter_areas , union_areas ,shape:[nums_boxes1,nums_boxes2]
inter_areas =inters[:,:,0] * inters[:,:,1] #分子 每一组数据的宽和高相乘获得交集面积
union_areas =areas1[:,None] + areas2 - inter_areas #分母
""" inter_areas: tensor([[[ 7., 7.], [ 0., 0.]], [[10., 10.], [ 8., 8.]]]) union_areas: tensor([[195., 776.], [269., 837.]]) """
return inter_areas / union_areas
#-----Test--------------------
#假设每个box中有两组数据:[2,4]
boxes1 = [ [10.,10.,20.,20.],
[15.,15.,30.,30.]]
boxes2 = [[13.,13.,25.,25.],
[22.,22.,48.,48.]]
boxes1 = torch.tensor(boxes1)
boxes2 = torch.tensor(boxes2)
print(boxes2.shape)
box_iou(boxes1,boxes2)
输出:
tensor([[0.25, 0.00],
[0.37, 0.08]])
如何理解左上角和右下角坐标计算代码?
x =torch.arange(4,8).view(2,2)
y = torch.arange(4).view(2,2)
print(x,y)
""" x:tensor([[4, 5], [6, 7]]) ---------------------- y:tensor([[0, 1], [2, 3]]) ------------------------------------- x[:,None,:]: 根据广播机制,x会在中间None创建的新维度复制数据形成[2,2,2]以匹配y的维度[2,2],只有在计算的时候才会复制,打印不出来 tensor([[[4, 5], [4,5]], [[6, 7], [6,7]]]) """
print(x[:,None,:]-y[:,:]) #shape:[2,1,2]-[2,2]
""" 输出: tensor([[[4, 4], [2, 2]], [[6, 6], [4, 4]]]) """
边栏推荐
猜你喜欢

SRM供应商管理系统如何助力口腔护理企业实现采购战略的转型升级

DBPack SQL Tracing 功能及数据加密功能详解

RecSys'22|CARCA:交叉注意力感知上下文和属性进行推荐
![[Dark Horse Morning Post] Hu Jun's endorsement of Wukong's financial management is suspected of fraud, which is suspected to involve 39 billion yuan; Fuling mustard responded that mustard ate toenails](/img/d7/4671b5a74317a8f87ffd36be2b34e1.jpg)
[Dark Horse Morning Post] Hu Jun's endorsement of Wukong's financial management is suspected of fraud, which is suspected to involve 39 billion yuan; Fuling mustard responded that mustard ate toenails

千万级乘客排队系统重构&压测方案总结篇

2022年SQL大厂高频实战面试题(详细解析)

生物制药产业发展现状和趋势展望

Bugku-Misc-贝斯手

Shell nl命令详解(显示行号、读取文件)
![[供应链·案例篇]石油和天然气行业的数字化转型用例](/img/44/9ef9f86f8afb85f49aac1cce55723d.jpg)
[供应链·案例篇]石油和天然气行业的数字化转型用例
随机推荐
在码云拉取代码后,调整了seata版本1.5.2。出现如下异常。是因为数据库表缺少字段导致的吗?
使用设备树时对应的驱动编程
Are online account opening commissions reliable? Is online account opening safe?
MySQL 慢查询
请问数据库中报错信息如下,mongoshake 有什么配置的方式解决这种大消息问题吗?
SQL窗口函数
完美指南|如何使用 ODBC 进行无代理 Oracle 数据库监控?
QT常用全局宏定义
2022年SQL经典面试题总结(带解析)
创造建材数字转型新视界,中建材如何多边赋能集团业务快速发展
Bugku-Misc-贝斯手
2022 Strong Net Cup CTF---Strong Net Pioneer ASR wp
金仓数据库KingbaseES安全指南--6.4. RADIUS身份验证
How can become a good architect necessary skills: painting for all the people praise the system architecture diagram?What is the secret?Quick to open this article and have a look!.
关于MySql中explain结果filtered的理解
金仓数据库 KDTS 迁移工具使用指南(3. 系统部署)
深圳市商务局2022年度中央资金(跨境电子商务企业市场开拓扶持事项)申报指南
Description of common operations and help projects about DevExpress in C#
夸克网盘资源站
中信证券是国内十大券商吗?怎么开户安全?