YOLOv10改进策略【损失函数篇】| Shape-IoU:考虑边界框形状和尺度的更精确度量

一、本文介绍

本文记录的是改进YOLOv10的损失函数,将其替换成Shape-IoU。现有边界框回归方法通常考虑真实GT(Ground Truth)框预测框之间的几何关系,通过边界框的相对位置和形状计算损失,但忽略了边界框本身的形状和尺度等固有属性对边界框回归的影响。为了弥补现有研究的不足,Shape-IoU提出了一种关注边界框本身形状和尺度的边界框回归方法。



二、Shape-IoU设计原理

Shape-IoU:考虑边界框形状和尺度的更精确度量

以下是关于Shape-IoU的详细介绍:

2.1 原理

  • 分析边界框回归特性:通过对边界框回归样本的分析,得出以下结论:
    • 当回归样本的偏差和形状偏差相同且不全为0时,假设GT框不是正方形且有长短边,边界框形状和尺度的差异会导致其IoU值的差异。
    • 对于相同尺度的边界框回归样本,当回归样本的偏差和形状偏差相同且不全为0时,边界框的形状会对回归样本的IoU值产生影响。沿着边界框短边方向的偏差和形状偏差对应的IoU值变化更为显著。
    • 对于具有相同形状边界框的回归样本,当回归样本偏差和形状偏差相同且不全为0时,与较大尺度的回归样本相比,较小尺度边界框回归样本的IoU值受GT框形状的影响更为显著。
  • Shape - IoU公式
    • I o U = ∣ B ∩ B g t ∣ ∣ B ∪ B g t ∣ IoU = \frac{|B \cap B^{gt}|}{|B \cup B^{gt}|} IoU=BBgtBBgt
    • w w = 2 × ( w g t ) s c a l e ( w g t ) s c a l e + ( h g t ) s c a l e ww = \frac{2 \times (w^{gt})^{scale}}{(w^{gt})^{scale} + (h^{gt})^{scale}} ww=(wgt)scale+(hgt)scale2×(wgt)scale
    • h h = 2 × ( h g t ) s c a l e ( w g t ) s c a l e + ( h g t ) s c a l e hh = \frac{2 \times (h^{gt})^{scale}}{(w^{gt})^{scale} + (h^{gt})^{scale}} hh=(wgt)scale+(hgt)scale2×(hgt)scale
    • d i s t a n c e s h a p e = h h × ( x c − x c g t c ) 2 + w w × ( y c − y c g t c ) 2 distance^{shape} = hh \times (\frac{x_c - x_c^{gt}}{c})^{2} + ww \times (\frac{y_c - y_c^{gt}}{c})^{2} distanceshape=hh×(cxcxcgt)2+ww×(cycycgt)2
    • Ω s h a p e = ∑ t = w , h ( 1 − e − ω t ) θ , θ = 4 \Omega^{shape} = \sum_{t = w, h}(1 - e^{-\omega_t})^{\theta}, \theta = 4 Ωshape=t=w,h(1eωt)θ,θ=4,其中 { ω w = h h × ∣ w − w g t ∣ m a x ( w , w g t ) ω h = w w × ∣ h − h g t ∣ m a x ( h , h g t ) \left\{\begin{array}{l} \omega_{w} = hh \times \frac{|w - w^{gt}|}{max(w, w^{gt})} \\ \omega_{h} = ww \times \frac{|h - h^{gt}|}{max(h, h^{gt})} \end{array}\right. {ωw=hh×max(w,wgt)wwgtωh=ww×max(h,hgt)hhgt
  • 对应的边界框回归损失 L S h a p e − I o U = 1 − I o U + d i s t a n c e s h a p e + 0.5 × Ω s h a p e L_{Shape - IoU} = 1 - IoU + distance^{shape} + 0.5 \times \Omega^{shape} LShapeIoU=1IoU+distanceshape+0.5×Ωshape

在这里插入图片描述

2.2 优势

  • 提高检测性能:论文中通过一系列对比实验,证明了Shape-IoU方法在不同检测任务中能够有效提高检测性能,优于现有方法,在不同检测任务中达到了最先进的性能。
  • 关注边界框自身属性:考虑了边界框本身的形状和尺度对边界框回归的影响,弥补了现有研究忽略这一因素的不足。
  • 在小目标检测任务中的应用:针对小目标检测任务,提出了Shape-Dot DistanceShape-NWD,将Shape-IoU的思想融入其中,提高了在小目标检测方面的性能。

论文:https://arxiv.org/pdf/2312.17663
源码:https://github.com/malagoutou/Shape-IoU


三、Shape-IoU的实现代码

Shape-IoU的实现代码如下:

def shape_iou(box1, box2, xywh=True, scale=0, eps=1e-7):
    (x1, y1, w1, h1), (x2, y2, w2, h2) = box1.chunk(4, -1), box2.chunk(4, -1)
    w1_, h1_, w2_, h2_ = w1 / 2, h1 / 2, w2 / 2, h2 / 2
    b1_x1, b1_x2, b1_y1, b1_y2 = x1 - w1_, x1 + w1_, y1 - h1_, y1 + h1_
    b2_x1, b2_x2, b2_y1, b2_y2 = x2 - w2_, x2 + w2_, y2 - h2_, y2 + h2_
 
    # Intersection area
    inter = (torch.min(b1_x2, b2_x2) - torch.max(b1_x1, b2_x1)).clamp(0) * \
            (torch.min(b1_y2, b2_y2) - torch.max(b1_y1, b2_y1)).clamp(0)
 
    # Union Area
    union = w1 * h1 + w2 * h2 - inter + eps
 
    # IoU
    iou = inter / union
 
    #Shape-Distance    #Shape-Distance    #Shape-Distance    #Shape-Distance    #Shape-Distance    #Shape-Distance    #Shape-Distance  
    ww = 2 * torch.pow(w2, scale) / (torch.pow(w2, scale) + torch.pow(h2, scale))
    hh = 2 * torch.pow(h2, scale) / (torch.pow(w2, scale) + torch.pow(h2, scale))
    cw = torch.max(b1_x2, b2_x2) - torch.min(b1_x1, b2_x1)  # convex width
    ch = torch.max(b1_y2, b2_y2) - torch.min(b1_y1, b2_y1)  # convex height
    c2 = cw ** 2 + ch ** 2 + eps                            # convex diagonal squared
    center_distance_x = ((b2_x1 + b2_x2 - b1_x1 - b1_x2) ** 2) / 4
    center_distance_y = ((b2_y1 + b2_y2 - b1_y1 - b1_y2) ** 2) / 4
    center_distance = hh * center_distance_x + ww * center_distance_y
    distance = center_distance / c2
  
    omiga_w = hh * torch.abs(w1 - w2) / torch.max(w1, w2)
    omiga_h = ww * torch.abs(h1 - h2) / torch.max(h1, h2)
    shape_cost = torch.pow(1 - torch.exp(-1 * omiga_w), 4) + torch.pow(1 - torch.exp(-1 * omiga_h), 4)
    
    iou = iou - distance - 0.5 * ( shape_cost)
    return iou  # IoU

四、添加步骤

4.1 修改ultralytics/utils/metrics.py

此处需要查看的文件是ultralytics/utils/metrics.py

metrics.py中定义了模型的损失函数和计算方法,我们想要加入新的损失函数就只需要将代码放到这个文件内即可

Shape-IoU添加后如下:

在这里插入图片描述

4.2 修改ultralytics/utils/loss.py

utils\loss.py用于计算各种损失。

ultralytics/utils/loss.py在的引用中添加shape_iou,然后在BboxLoss函数内修改如下代码,使模型调用此Shape-IoU损失函数。

在这里插入图片描述


iou = shape_iou(pred_bboxes[fg_mask], target_bboxes[fg_mask])

在这里插入图片描述

4.3 修改ultralytics/utils/tal.py

tal.py中是一些损失函数的功能应用。

ultralytics/utils/tal.py在的引用中添加shape_iou,然后在iou_calculation函数内修改如下代码,使模型调用此Shape-IoU损失函数。

在这里插入图片描述

在这里插入图片描述

此时再次训练模型便会使用Shape-IoU计算模型的损失函数。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Limiiiing

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值