损失函数:DIOU loss手写实现

2023-10-28 21:59

本文主要是介绍损失函数:DIOU loss手写实现,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

下面是纯diou代码

            '''计算两个box的中心点距离d'''# d = math.sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2)d = math.sqrt((pred[:, -1] - target[:, -1]) ** 2 + (pred[:, -2] - target[:, -2]) ** 2)# 左边xpred_l = pred[:, -1] - pred[:, -1] / 2target_l = target[:, -1] - target[:, -1] / 2# 上边ypred_t = pred[:, -2] - pred[:, -2] / 2target_t = target[:, -2] - target[:, -2] / 2# 右边xpred_r = pred[:, -1] + pred[:, -1] / 2target_r = target[:, -1] + target[:, -1] / 2# 下边ypred_b = pred[:, -2] + pred[:, -2] / 2target_b = target[:, -2] + target[:, -2] / 2'''计算两个box的bound的对角线距离'''bound_l = torch.min(pred_l, target_l)  # leftbound_r = torch.max(pred_r, target_r)  # rightbound_t = torch.min(pred_t, target_t)  # topbound_b = torch.max(pred_b, target_b)  # bottomc = math.sqrt((bound_r - bound_l) ** 2 + (bound_b - bound_t) ** 2)dloss = iou - (d ** 2) / (c ** 2)loss = 1 - dloss.clamp(min=-1.0, max=1.0)

第一步 计算两个box的中心点距离d

首先要知道pred和target的输出结果是什么
pred[:,:2]第一个:表示多个图片,第二个:2表示前两个数值,代表矩形框中心点(Y,X)
pred[:,2:]第一个:表示多个图片,第二个2:表示两个数值,代表矩形框长宽(H,W)
target[:,:2]同理,
d =
 

根据上面的分析来计算左右上下坐标lrtb

 然后计算内部2个矩形的最小外接矩形的对角线长度c

 d是两个预测矩形中心点的距离

 下面接受各种极端情况
A 两个框中心对齐时候,d/c=0,iou可能0-1

 A 两个框相距很远时,d/c=1,iou=0

 所以d/c属于0-1
dloss=iou-d/c属于-1到1
因此设置loss=1-dloss属于0-2

 

展示iou\giou\diou代码,这是YOLOX自带的损失函数,其中dloss是我自己写的
YOLOX是下载自
GitHub - Megvii-BaseDetection/YOLOX: YOLOX is a high-performance anchor-free YOLO, exceeding yolov3~v5 with MegEngine, ONNX, TensorRT, ncnn, and OpenVINO supported. Documentation: https://yolox.readthedocs.io/YOLOX is a high-performance anchor-free YOLO, exceeding yolov3~v5 with MegEngine, ONNX, TensorRT, ncnn, and OpenVINO supported. Documentation: https://yolox.readthedocs.io/ - GitHub - Megvii-BaseDetection/YOLOX: YOLOX is a high-performance anchor-free YOLO, exceeding yolov3~v5 with MegEngine, ONNX, TensorRT, ncnn, and OpenVINO supported. Documentation: https://yolox.readthedocs.io/https://github.com/Megvii-BaseDetection/YOLOX

class IOUloss(nn.Module):def __init__(self, reduction="none", loss_type="iou"):super(IOUloss, self).__init__()self.reduction = reductionself.loss_type = loss_typedef forward(self, pred, target):assert pred.shape[0] == target.shape[0]pred = pred.view(-1, 4)target = target.view(-1, 4)tl = torch.max((pred[:, :2] - pred[:, 2:] / 2), (target[:, :2] - target[:, 2:] / 2))# pred target都是[H,W,Y,X]# (Y,X)-(H,W) 左上角br = torch.min((pred[:, :2] + pred[:, 2:] / 2), (target[:, :2] + target[:, 2:] / 2))# (X,Y)+(H,W) 右下角area_p = torch.prod(pred[:, 2:], 1)  # HxWarea_g = torch.prod(target[:, 2:], 1)en = (tl < br).type(tl.type()).prod(dim=1)area_i = torch.prod(br - tl, 1) * enarea_u = area_p + area_g - area_iiou = (area_i) / (area_u + 1e-16)if self.loss_type == "iou":loss = 1 - iou ** 2elif self.loss_type == "giou":c_tl = torch.min((pred[:, :2] - pred[:, 2:] / 2), (target[:, :2] - target[:, 2:] / 2))c_br = torch.max((pred[:, :2] + pred[:, 2:] / 2), (target[:, :2] + target[:, 2:] / 2))area_c = torch.prod(c_br - c_tl, 1)giou = iou - (area_c - area_u) / area_c.clamp(1e-16)loss = 1 - giou.clamp(min=-1.0, max=1.0)# pred[:, :2]  pred[:, 2:]# (Y,X)        (H,W)# target[:, :2]  target[:, 2:]# (Y,X)        (H,W)elif self.loss_type == "diou":'''计算两个box的中心点距离d'''# d = math.sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2)d = math.sqrt((pred[:, -1] - target[:, -1]) ** 2 + (pred[:, -2] - target[:, -2]) ** 2)# 左边xpred_l = pred[:, -1] - pred[:, -1] / 2target_l = target[:, -1] - target[:, -1] / 2# 上边ypred_t = pred[:, -2] - pred[:, -2] / 2target_t = target[:, -2] - target[:, -2] / 2# 右边xpred_r = pred[:, -1] + pred[:, -1] / 2target_r = target[:, -1] + target[:, -1] / 2# 下边ypred_b = pred[:, -2] + pred[:, -2] / 2target_b = target[:, -2] + target[:, -2] / 2'''计算两个box的bound的对角线距离'''bound_l = torch.min(pred_l, target_l)  # leftbound_r = torch.max(pred_r, target_r)  # rightbound_t = torch.min(pred_t, target_t)  # topbound_b = torch.max(pred_b, target_b)  # bottomc = math.sqrt((bound_r - bound_l) ** 2 + (bound_b - bound_t) ** 2)dloss = iou - (d ** 2) / (c ** 2)loss = 1 - dloss.clamp(min=-1.0, max=1.0)# Step1# def DIoU(a, b):# d = a.center_distance(b)# c = a.bound_diagonal_distance(b)# return IoU(a, b) - (d ** 2) / (c ** 2)# Step2-1# def center_distance(self, other):#    '''#    计算两个box的中心点距离#    '''#    return euclidean_distance(self.center, other.center)# Step2-2# def euclidean_distance(p1, p2):#    '''#    计算两个点的欧式距离#    '''#     x1, y1 = p1#    x2, y2 = p2#    return math.sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2)# Step3# def bound_diagonal_distance(self, other):#    '''#    计算两个box的bound的对角线距离#    '''#    bound = self.boundof(other)#    return euclidean_distance((bound.x, bound.y), (bound.r, bound.b))# Step3-2# def boundof(self, other):#    '''#    计算box和other的边缘外包框,使得2个box都在框内的最小矩形#    '''#    xmin = min(self.x, other.x)#    ymin = min(self.y, other.y)#    xmax = max(self.r, other.r)#    ymax = max(self.b, other.b)#    return BBox(xmin, ymin, xmax, ymax)# Step3-3# def euclidean_distance(p1, p2):#    '''#    计算两个点的欧式距离#    '''#     x1, y1 = p1#    x2, y2 = p2#    return math.sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2)if self.reduction == "mean":loss = loss.mean()elif self.reduction == "sum":loss = loss.sum()return loss

GitHub - Megvii-BaseDetection/YOLOX: YOLOX is a high-performance anchor-free YOLO, exceeding yolov3~v5 with MegEngine, ONNX, TensorRT, ncnn, and OpenVINO supported. Documentation: https://yolox.readthedocs.io/

这篇关于损失函数:DIOU loss手写实现的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!


原文地址:https://blog.csdn.net/zjc910997316/article/details/125500138
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.chinasem.cn/article/296226

相关文章

Spring StateMachine实现状态机使用示例详解

《SpringStateMachine实现状态机使用示例详解》本文介绍SpringStateMachine实现状态机的步骤,包括依赖导入、枚举定义、状态转移规则配置、上下文管理及服务调用示例,重点解... 目录什么是状态机使用示例什么是状态机状态机是计算机科学中的​​核心建模工具​​,用于描述对象在其生命

Spring Boot 结合 WxJava 实现文章上传微信公众号草稿箱与群发

《SpringBoot结合WxJava实现文章上传微信公众号草稿箱与群发》本文将详细介绍如何使用SpringBoot框架结合WxJava开发工具包,实现文章上传到微信公众号草稿箱以及群发功能,... 目录一、项目环境准备1.1 开发环境1.2 微信公众号准备二、Spring Boot 项目搭建2.1 创建

IntelliJ IDEA2025创建SpringBoot项目的实现步骤

《IntelliJIDEA2025创建SpringBoot项目的实现步骤》本文主要介绍了IntelliJIDEA2025创建SpringBoot项目的实现步骤,文中通过示例代码介绍的非常详细,对大家... 目录一、创建 Spring Boot 项目1. 新建项目2. 基础配置3. 选择依赖4. 生成项目5.

PostgreSQL中rank()窗口函数实用指南与示例

《PostgreSQL中rank()窗口函数实用指南与示例》在数据分析和数据库管理中,经常需要对数据进行排名操作,PostgreSQL提供了强大的窗口函数rank(),可以方便地对结果集中的行进行排名... 目录一、rank()函数简介二、基础示例:部门内员工薪资排名示例数据排名查询三、高级应用示例1. 每

全面掌握 SQL 中的 DATEDIFF函数及用法最佳实践

《全面掌握SQL中的DATEDIFF函数及用法最佳实践》本文解析DATEDIFF在不同数据库中的差异,强调其边界计算原理,探讨应用场景及陷阱,推荐根据需求选择TIMESTAMPDIFF或inte... 目录1. 核心概念:DATEDIFF 究竟在计算什么?2. 主流数据库中的 DATEDIFF 实现2.1

Linux下删除乱码文件和目录的实现方式

《Linux下删除乱码文件和目录的实现方式》:本文主要介绍Linux下删除乱码文件和目录的实现方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录linux下删除乱码文件和目录方法1方法2总结Linux下删除乱码文件和目录方法1使用ls -i命令找到文件或目录

MySQL中的LENGTH()函数用法详解与实例分析

《MySQL中的LENGTH()函数用法详解与实例分析》MySQLLENGTH()函数用于计算字符串的字节长度,区别于CHAR_LENGTH()的字符长度,适用于多字节字符集(如UTF-8)的数据验证... 目录1. LENGTH()函数的基本语法2. LENGTH()函数的返回值2.1 示例1:计算字符串

SpringBoot+EasyExcel实现自定义复杂样式导入导出

《SpringBoot+EasyExcel实现自定义复杂样式导入导出》这篇文章主要为大家详细介绍了SpringBoot如何结果EasyExcel实现自定义复杂样式导入导出功能,文中的示例代码讲解详细,... 目录安装处理自定义导出复杂场景1、列不固定,动态列2、动态下拉3、自定义锁定行/列,添加密码4、合并

mybatis执行insert返回id实现详解

《mybatis执行insert返回id实现详解》MyBatis插入操作默认返回受影响行数,需通过useGeneratedKeys+keyProperty或selectKey获取主键ID,确保主键为自... 目录 两种方式获取自增 ID:1. ​​useGeneratedKeys+keyProperty(推

Spring Boot集成Druid实现数据源管理与监控的详细步骤

《SpringBoot集成Druid实现数据源管理与监控的详细步骤》本文介绍如何在SpringBoot项目中集成Druid数据库连接池,包括环境搭建、Maven依赖配置、SpringBoot配置文件... 目录1. 引言1.1 环境准备1.2 Druid介绍2. 配置Druid连接池3. 查看Druid监控