YOLOv5改进(三)-- 引进Focaler-IoU损失函数

1、前言

​ 目标检测是计算机视觉的基本任务之一,旨在识别图像中的目标并定位其位置。目标检测算法可分为基于锚点和无锚点的方法。基于锚点的方法包括Faster R-CNN、YOLO系列、SSD和RetinaNet等。无锚点方法包括CornerNet、CenterNet和FCOS等。在这些检测器中,边界框回归损失函数作为定位分支的重要组成部分,起着不可替代的作用。

​ Zhang Hao 提出了一种新的边界框回归损失函数Focaler-IoU,该函数能够关注不同难度的回归样本,并动态调整样本权重以优化回归性能。Focaler-IoU结合了IoU(Intersection over Union)和Focal Loss的思想,通过引入一个可学习的关注因子来调整不同样本的权重。在训练过程中,关注因子会根据回归结果动态调整,使得回归器更加关注那些对定位精度影响较大的样本。

  1. 论文《Focaler-IoU:更聚焦的IoU损失》
  2. 代码
  3. 作者导读:Focaler-IoU: 更加聚焦的IoU损失

2、摘要

​ 边框回归在目标检测领域扮演着关键角色,目标检测的定位精度很大程度上取决于边框回归损失函数。现有的研究通过利用边框之间的几何关系来提升回归效果,而忽略难易样本分布对于边框回归的影响。在本文我们分析了难易样本分布对回归结果的影响,接着我们提出Focaler-IoU方法,其能够在不同的检测任务中通过聚焦不同的回归样本来提升检测器的效果。最后针对不同的检测任务使用现有先进检测器与回归方法进行对比实验,使用本文方法后检测效果得到进一步提升。

3、Focaler-IoU:

​ 为了在能够在不同的检测任务中聚焦不同的回归样本,我们使用线性区间映射的方法来重构IoU损失,使得边框回归效果得到提升。即通过定义 d 和 u 两个数,通过比较原始IoU值去重构新的IoU,具体公式如下:

在这里插入图片描述
​ 其中[d,u]∈[0,1]区间,IoU为原始的IoU值,IoUfacaler 为重构后新的IoU,我们当前记为Facaler-IoU,通过比较原始IoU值,动态调整IoUfacaler 值,得到不同的回归样本。

Focaler损失函数定义:

在这里插入图片描述

将回归样本IoUfacaler 应用至现有基于IoU的边框回归损失函数中,得到不同的回归损失函数:LFacaler-GIoU 、 LFacaler-DIoU 、 LFacaler-CIoU 、 LFacaler-EIoU 、 LFacaler-SIoU ,其定义如下:

在这里插入图片描述

<!-- 其代码样例-->
<p style="text-align:center; color:orange;size:3px;font-weight:bolder">L<sub>Facaler-GIoU</sub> = L<sub>GIoU</sub> + IoU - IoU<sup>facaler</sup></p>

作者实验:

(1)在YOLOv8比较 SIoUFocaler-SIoU

在这里插入图片描述

(1)在YOLOv5比较 SIoUFocaler-SIoU
在这里插入图片描述

4、代码实现

作者论文就给出代码,只有一行

 iou = ((iou - d) / (u - d)).clamp(0, 1)  #default d=0.00,u=0.95

在YOLOv5目录下,utils/loss.pydef __call__()函数里面就有iou的计算,找到iou = bbox_iou()在其后面添加该作者代码即可。因为作者代码写着是三段函数,但是通过查看作者给出的代码,发现并没有做判断,于是这里就根据作者的代码进行了重构。

# 原始IoU
iou = bbox_iou(pbox.T, tbox[i], x1y1x2y2=False, CIoU=True)  # iou(prediction, target)

# 引入 Focaler-IoU 回归样本
# default d=0.00,u=0.95
d = 0.00
u = 0.95
# iou = ((iou - d) / (u - d)).clamp(0, 1)   # 原作者代码
iou = 1 if iou > u else (0 if iou < d else ((iou - d) / (u - d)).clamp(0, 1)) # 根据公式重构 Focaler-IoU

lbox += (1.0 - iou).mean()  # iou loss 损失函数

修改完后的效果
在这里插入图片描述

5、目标检测系列文章

  1. YOLOv5s网络模型讲解(一看就会)
  2. 生活垃圾数据集(YOLO版)
  3. YOLOv5如何训练自己的数据集
  4. 双向控制舵机(树莓派版)
  5. 树莓派部署YOLOv5目标检测(详细篇)
  6. YOLO_Tracking 实践 (环境搭建 & 案例测试)
  7. 目标检测:数据集划分 & XML数据集转YOLO标签
  8. DeepSort行人车辆识别系统(实现目标检测+跟踪+统计)
  9. YOLOv5参数大全(parse_opt篇)
  10. YOLOv5改进(一)-- 轻量化YOLOv5s模型
  11. YOLOv5改进(二)-- 目标检测优化点(添加小目标头检测)
### Focal Loss and IoU in Computer Vision Focal loss is an important concept designed to address the issue of class imbalance encountered during object detection tasks within computer vision[^1]. Traditional cross-entropy loss tends to struggle when there are significantly more background classes than foreground ones, leading models trained with such losses to sometimes ignore rare but critical positive samples. The Intersection over Union (IoU), on the other hand, serves as a metric that quantifies how well two bounding boxes overlap. Specifically, it measures the ratio between the area where these boxes intersect versus their combined areas minus this intersection part. High values indicate better alignment among predicted and actual target locations inside images being analyzed by algorithms employing convolutional neural networks for instance[^2]. Combining focal loss principles alongside considerations regarding IoUs can enhance model performance especially under challenging conditions characterized by imbalanced datasets common across various applications ranging from autonomous driving systems through medical imaging diagnostics all way up till general-purpose robotics solutions seeking precise spatial awareness capabilities[^3]. ```python def compute_iou(boxA, boxB): # determine the (x, y)-coordinates of the intersection rectangle xA = max(boxA[0], boxB[0]) yA = max(boxA[1], boxB[1]) xB = min(boxA[2], boxB[2]) yB = min(boxA[3], boxB[3]) # compute the area of intersection rectangle interArea = max(0, xB - xA + 1) * max(0, yB - yA + 1) # compute the area of both the prediction and ground-truth rectangles boxAArea = (boxA[2] - boxA[0] + 1) * (boxA[3] - boxA[1] + 1) boxBArea = (boxB[2] - boxB[0] + 1) * (boxB[3] - boxB[1] + 1) # compute the intersection over union by taking the intersection # area and dividing it by the sum of prediction + ground-truth # areas - the interesection area iou = interArea / float(boxAArea + boxBArea - interArea) # return the intersection over union value return iou ```
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值