损失函数: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手写实现的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/296226

相关文章

openCV中KNN算法的实现

《openCV中KNN算法的实现》KNN算法是一种简单且常用的分类算法,本文主要介绍了openCV中KNN算法的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的... 目录KNN算法流程使用OpenCV实现KNNOpenCV 是一个开源的跨平台计算机视觉库,它提供了各

OpenCV图像形态学的实现

《OpenCV图像形态学的实现》本文主要介绍了OpenCV图像形态学的实现,包括腐蚀、膨胀、开运算、闭运算、梯度运算、顶帽运算和黑帽运算,文中通过示例代码介绍的非常详细,需要的朋友们下面随着小编来一起... 目录一、图像形态学简介二、腐蚀(Erosion)1. 原理2. OpenCV 实现三、膨胀China编程(

通过Spring层面进行事务回滚的实现

《通过Spring层面进行事务回滚的实现》本文主要介绍了通过Spring层面进行事务回滚的实现,包括声明式事务和编程式事务,具有一定的参考价值,感兴趣的可以了解一下... 目录声明式事务回滚:1. 基础注解配置2. 指定回滚异常类型3. ​不回滚特殊场景编程式事务回滚:1. ​使用 TransactionT

MySQL高级查询之JOIN、子查询、窗口函数实际案例

《MySQL高级查询之JOIN、子查询、窗口函数实际案例》:本文主要介绍MySQL高级查询之JOIN、子查询、窗口函数实际案例的相关资料,JOIN用于多表关联查询,子查询用于数据筛选和过滤,窗口函... 目录前言1. JOIN(连接查询)1.1 内连接(INNER JOIN)1.2 左连接(LEFT JOI

Android实现打开本地pdf文件的两种方式

《Android实现打开本地pdf文件的两种方式》在现代应用中,PDF格式因其跨平台、稳定性好、展示内容一致等特点,在Android平台上,如何高效地打开本地PDF文件,不仅关系到用户体验,也直接影响... 目录一、项目概述二、相关知识2.1 PDF文件基本概述2.2 android 文件访问与存储权限2.

使用Python实现全能手机虚拟键盘的示例代码

《使用Python实现全能手机虚拟键盘的示例代码》在数字化办公时代,你是否遇到过这样的场景:会议室投影电脑突然键盘失灵、躺在沙发上想远程控制书房电脑、或者需要给长辈远程协助操作?今天我要分享的Pyth... 目录一、项目概述:不止于键盘的远程控制方案1.1 创新价值1.2 技术栈全景二、需求实现步骤一、需求

Spring Shell 命令行实现交互式Shell应用开发

《SpringShell命令行实现交互式Shell应用开发》本文主要介绍了SpringShell命令行实现交互式Shell应用开发,能够帮助开发者快速构建功能丰富的命令行应用程序,具有一定的参考价... 目录引言一、Spring Shell概述二、创建命令类三、命令参数处理四、命令分组与帮助系统五、自定义S

SpringBatch数据写入实现

《SpringBatch数据写入实现》SpringBatch通过ItemWriter接口及其丰富的实现,提供了强大的数据写入能力,本文主要介绍了SpringBatch数据写入实现,具有一定的参考价值,... 目录python引言一、ItemWriter核心概念二、数据库写入实现三、文件写入实现四、多目标写入

Android Studio 配置国内镜像源的实现步骤

《AndroidStudio配置国内镜像源的实现步骤》本文主要介绍了AndroidStudio配置国内镜像源的实现步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,... 目录一、修改 hosts,解决 SDK 下载失败的问题二、修改 gradle 地址,解决 gradle

SpringSecurity JWT基于令牌的无状态认证实现

《SpringSecurityJWT基于令牌的无状态认证实现》SpringSecurity中实现基于JWT的无状态认证是一种常见的做法,本文就来介绍一下SpringSecurityJWT基于令牌的无... 目录引言一、JWT基本原理与结构二、Spring Security JWT依赖配置三、JWT令牌生成与