一、本文介绍
本文给大家带来的是YOLOv11最新改进,为大家带来最近新提出的InnerIoU的内容同时用Inner的思想结合 SIoU 、WIoU、GIoU、DIoU、EIOU、CIoU等 损失函数 , 形成 InnerIoU、InnerSIoU、InnerWIoU、等新版本损失函数,同时还结合了Focus和AIpha思想, 形成的新的损失函数,其中Inner的主要思想是:引入了不同尺度的辅助边界框来计算损失, (该方法在处理非常小目标的检测任务时表现出良好的性能(但是在其它的尺度检测时也要比普通的损失要好)。 文章会详细探讨这些损失函数如何提高YOLOv11在各种检测任务中的性能,包括提升精度、加快收敛速度和增强模型对复杂场景的适应性。
二、各种损失函数的基本原理
论文地址: 官方Inner-IoU论文地址点击即可跳转
官方代码地址: 官方代码地址-官方只放出了两种结合方式CIoU、SIoU
本文改进地址: 文末提供完整代码块-包括InnerEIoU、InnerCIoU、InnerDIoU等七种结合方式和其Focus变种
2.1 交集面积和并集面积
在理解各种损失函数之前我们需要先来理解一下交集面积和并集面积,在数学中我们都学习过集合的概念, 这里的交集和并集的概念和数学集合中的含义是一样的。
2.2 InnerIoU的思想
Inner-IoU(内部交并比)的主要思想是: 改进目标检测中边界框回归(BBR)的准确性,特别是在处理高度重叠的目标时。传统的IoU(交并比)计算方法考虑了预测边界框和真实边界框的整体重叠区域,而Inner-IoU则专注于边界框内部的重叠部分。它通过引入辅助边界框,这些辅助框是原始边界框的缩小版本,来计算损失函数。
这种方法的优点包括:
- 针对性优化 :Inner-IoU通过关注边界框的核心部分而非整体,提供了对重叠区域更加精确的评估。
- 调整尺度 :通过控制辅助边界框的大小,Inner-IoU允许对不同的数据集和检测任务进行微调。
- 提高泛化能力 :实验证明,Inner-IoU在不同的数据集上显示出比传统IoU更好的泛化性能。
- 处理高低IoU样本 :对于高IoU样本,使用较小的辅助框可以加速模型学习;而对于低IoU样本,使用较大的辅助框可以改善回归性能。
总结: Inner-IoU是一种更细致、更专注于目标中心的 性能 评估指标,它通过辅助框的尺度调整提高了目标检测任务的精确度和效率。
2.2.1结合InnerIoU各种损失函数的效果图
上面的图片展示了CIoU 和 Inner-CIoU 方法。图中从左至右分别表示 CIoU 方法,以及不同比例(0.7、0.75 和 0.8)的 Inner-CIoU 方法的检测结果
这个图片可以看出这个Innner的思想在小目标检测的时候效果能够达到极致 (最适用于小范围但是其它的情况也能够有效但是小目标是效果最好的情景)
PS: 下面介绍的是融合的各种思想就是将其中的IoU替换为我们上面求出来的InnerIoU即可和其中的参数也替换为InnerIoU的思想,其中各种损失函数的本身思想并没有改变,只是改变了其中的 参数。
2.3 InnerSIoU
论文地址: SIoU: More Powerful Learning for Bounding Box Regression
适用场景: 适用于需要高精度边界框对齐的场景,如精细的物体检测和小目标检测。
概念: SIoU损失通过融入角度考虑和规模敏感性,引入了一种更为复杂的边界框回归方法,解决了以往损失函数的局限性,SIoU损失函数包含四个组成部分:角度损失、距离损失、形状损失和第四个未指定的组成部分。通过整合这些方面,从而实现更好的训练速度和预测准确性。
2.4 InnerWioU
论文地址 : WIoU: Bounding Box Regression Loss with Dynamic Focusing Mechanism
适用场景: 适用于需要动态调整损失焦点的情况,如不均匀分布的目标或不同尺度的目标检测。
概念: 引入动态聚焦机制的IoU变体,旨在改善边界框回归损失。
2.5 InnerGIoU
论文地址: GIoU: A Metric and A Loss for Bounding Box Regression
适用场景: 适合处理有重叠和非重叠区域的复杂场景,如拥挤场景的目标检测。
概念: 在IoU的基础上考虑非重叠区域,以更全面评估边界框
2.6 InnerDIoU
论文地址: DIoU: Faster and Better Learning for Bounding Box Regression
适用场景: 适用于需要快速收敛和精确定位的任务,特别是在边界框定位精度至关重要的场景。
概念: 结合边界框中心点之间的距离和重叠区域。
2.7 InnerEIoU
论文地址: EIoU:Loss for Accurate Bounding Box Regression
适用场景: 可用于需要进一步优化边界框对齐和形状相似性的高级场景。
概念: EIoU损失函数的核心思想在于提高边界框回归的准确性和效率。它通过以下几个方面来优化目标检测:
1. 增加中心点距离损失:通过最小化预测框和真实框中心点之间的距离,提高边界框的定位准确性。
2. 考虑尺寸差异:通过惩罚宽度和高度的差异,EIoU确保预测框在形状上更接近真实框。
3. 结合最小封闭框尺寸:将损失函数与包含预测框和真实框的最小封闭框的尺寸相结合,从而使得损失更加敏感于对象的尺寸和位置。
EIoU损失函数在传统IoU基础上增加了这些考量,以期在各种尺度上都能获得更精确的目标定位,尤其是在物体大小和形状变化较大的场景中。
2.8 InnerCIoU
论文地址: CIoU:Enhancing Geometric Factors in Model Learning
适用场景: 适合需要综合考虑重叠区域、形状和中心点位置的场景,如复杂背景或多目标跟踪。
概念: 综合考虑重叠区域、中心点距离和长宽比。
2.9 FocusLoss
论文地址: Focal Loss for Dense Object Detection
适用场景: 适用于需要高精度边界框对齐的场景,如精细的物体检测和小目标检测。
Focal Loss 由Kaiming He等人在论文《Focal Loss for Dense Object Detection》中提出,旨在解决在训练过程中正负样本数量极度不平衡的问题,尤其是在一些目标检测任务中,背景类别的样本可能远远多于前景类别的样本。
Focal Loss通过修改交叉熵损失, 增加一个调整因子 , 这个因子降低了那些已经被正确分类的样本的损失值 ,使得 模型 的训练焦点更多地放在难以分类的样本上。这种方式特别有利于提升小目标或者在复杂背景中容易被忽视的目标的检测性能。简而言之,Focal Loss让模型“关注”(或“专注”)于学习那些对提高整体性能更为关键的样本。
三、InnerIoU等损失函数代码块
3.1 代码一
此代码块块的基础版本来源于Github的开源版本,我在其基础上将Inner的思想加入其中形成了各种Inner的思想同时融合各种改良版本的损失函数形成对应版本的InnerIoU、InnerCIoU等损失函数。
- import numpy as np
- import torch
- import math
- from ultralytics.utils import ops
- class WIoU_Scale:
- ''' monotonous: {
- None: origin v1
- True: monotonic FM v2
- False: non-monotonic FM v3
- }
- momentum: The momentum of running mean'''
- iou_mean = 1.
- monotonous = False
- _momentum = 1 - 0.5 ** (1 / 7000)
- _is_train = True
- def __init__(self, iou):
- self.iou = iou
- self._update(self)
- @classmethod
- def _update(cls, self):
- if cls._is_train: cls.iou_mean = (1 - cls._momentum) * cls.iou_mean + \
- cls._momentum * self.iou.detach().mean().item()
- @classmethod
- def _scaled_loss(cls, self, gamma=1.9, delta=3):
- if isinstance(self.monotonous, bool):
- if self.monotonous:
- return (self.iou.detach() / self.iou_mean).sqrt()
- else:
- beta = self.iou.detach() / self.iou_mean
- alpha = delta * torch.pow(gamma, beta - delta)
- return beta / alpha
- return 1
- def bbox_iou(box1, box2, xywh=True, GIoU=False, DIoU=False, CIoU=False, EIoU=False, SIoU=False, WIoU=False, ShapeIoU=False,
- hw=1, mpdiou=False, Inner=False, alpha=1, ratio=0.7, eps=1e-7, scale=0.0):
- """
- Calculate Intersection over Union (IoU) of box1(1, 4) to box2(n, 4).
- Args:
- box1 (torch.Tensor): A tensor representing a single bounding box with shape (1, 4).
- box2 (torch.Tensor): A tensor representing n bounding boxes with shape (n, 4).
- xywh (bool, optional): If True, input boxes are in (x, y, w, h) format. If False, input boxes are in
- (x1, y1, x2, y2) format. Defaults to True.
- GIoU (bool, optional): If True, calculate Generalized IoU. Defaults to False.
- DIoU (bool, optional): If True, calculate Distance IoU. Defaults to False.
- CIoU (bool, optional): If True, calculate Complete IoU. Defaults to False.
- EIoU (bool, optional): If True, calculate Efficient IoU. Defaults to False.
- SIoU (bool, optional): If True, calculate Scylla IoU. Defaults to False.
- eps (float, optional): A small value to avoid division by zero. Defaults to 1e-7.
- Returns:
- (torch.Tensor): IoU, GIoU, DIoU, or CIoU values depending on the specified flags.
- """
- if Inner:
- if not xywh:
- box1, box2 = ops.xyxy2xywh(box1), ops.xyxy2xywh(box2)
- (x1, y1, w1, h1), (x2, y2, w2, h2) = box1.chunk(4, -1), box2.chunk(4, -1)
- b1_x1, b1_x2, b1_y1, b1_y2 = x1 - (w1 * ratio) / 2, x1 + (w1 * ratio) / 2, y1 - (h1 * ratio) / 2, y1 + (
- h1 * ratio) / 2
- b2_x1, b2_x2, b2_y1, b2_y2 = x2 - (w2 * ratio) / 2, x2 + (w2 * ratio) / 2, y2 - (h2 * ratio) / 2, y2 + (
- h2 * ratio) / 2
- # Intersection area
- inter = (b1_x2.minimum(b2_x2) - b1_x1.maximum(b2_x1)).clamp_(0) * \
- (b1_y2.minimum(b2_y2) - b1_y1.maximum(b2_y1)).clamp_(0)
- # Union Area
- union = w1 * h1 * ratio * ratio + w2 * h2 * ratio * ratio - inter + eps
- iou = inter / union
- # Get the coordinates of bounding boxes
- else:
- if xywh: # transform from xywh to xyxy
- (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_
- else: # x1, y1, x2, y2 = box1
- b1_x1, b1_y1, b1_x2, b1_y2 = box1.chunk(4, -1)
- b2_x1, b2_y1, b2_x2, b2_y2 = box2.chunk(4, -1)
- w1, h1 = b1_x2 - b1_x1, b1_y2 - b1_y1 + eps
- w2, h2 = b2_x2 - b2_x1, b2_y2 - b2_y1 + eps
- # Intersection area
- inter = (b1_x2.minimum(b2_x2) - b1_x1.maximum(b2_x1)).clamp_(0) * \
- (b1_y2.minimum(b2_y2) - b1_y1.maximum(b2_y1)).clamp_(0)
- # Union Area
- union = w1 * h1 + w2 * h2 - inter + eps
- # IoU
- iou = inter / union
- if CIoU or DIoU or GIoU or EIoU or SIoU or ShapeIoU or mpdiou or WIoU:
- cw = b1_x2.maximum(b2_x2) - b1_x1.minimum(b2_x1) # convex (smallest enclosing box) width
- ch = b1_y2.maximum(b2_y2) - b1_y1.minimum(b2_y1) # convex height
- if CIoU or DIoU or EIoU or SIoU or mpdiou or WIoU or ShapeIoU: # Distance or Complete IoU https://arxiv.org/abs/1911.08287v1
- c2 = cw ** 2 + ch ** 2 + eps # convex diagonal squared
- rho2 = ((b2_x1 + b2_x2 - b1_x1 - b1_x2) ** 2 + (b2_y1 + b2_y2 - b1_y1 - b1_y2) ** 2) / 4 # center dist ** 2
- if CIoU: # https://github.com/Zzh-tju/DIoU-SSD-pytorch/blob/master/utils/box/box_utils.py#L47
- v = (4 / math.pi ** 2) * (torch.atan(w2 / h2) - torch.atan(w1 / h1)).pow(2)
- with torch.no_grad():
- alpha = v / (v - iou + (1 + eps))
- return iou - (rho2 / c2 + v * alpha) # CIoU
- elif EIoU:
- rho_w2 = ((b2_x2 - b2_x1) - (b1_x2 - b1_x1)) ** 2
- rho_h2 = ((b2_y2 - b2_y1) - (b1_y2 - b1_y1)) ** 2
- cw2 = cw ** 2 + eps
- ch2 = ch ** 2 + eps
- return iou - (rho2 / c2 + rho_w2 / cw2 + rho_h2 / ch2) # EIoU
- elif SIoU:
- # SIoU Loss https://arxiv.org/pdf/2205.12740.pdf
- s_cw = (b2_x1 + b2_x2 - b1_x1 - b1_x2) * 0.5 + eps
- s_ch = (b2_y1 + b2_y2 - b1_y1 - b1_y2) * 0.5 + eps
- sigma = torch.pow(s_cw ** 2 + s_ch ** 2, 0.5)
- sin_alpha_1 = torch.abs(s_cw) / sigma
- sin_alpha_2 = torch.abs(s_ch) / sigma
- threshold = pow(2, 0.5) / 2
- sin_alpha = torch.where(sin_alpha_1 > threshold, sin_alpha_2, sin_alpha_1)
- angle_cost = torch.cos(torch.arcsin(sin_alpha) * 2 - math.pi / 2)
- rho_x = (s_cw / cw) ** 2
- rho_y = (s_ch / ch) ** 2
- gamma = angle_cost - 2
- distance_cost = 2 - torch.exp(gamma * rho_x) - torch.exp(gamma * rho_y)
- omiga_w = torch.abs(w1 - w2) / torch.max(w1, w2)
- omiga_h = 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)
- return iou - 0.5 * (distance_cost + shape_cost) + eps # SIoU
- elif ShapeIoU:
- #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
- #Shape-Shape #Shape-Shape #Shape-Shape #Shape-Shape #Shape-Shape #Shape-Shape #Shape-Shape #Shape-Shape
- 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)
- return iou - distance - 0.5 * shape_cost
- elif mpdiou:
- d1 = (b2_x1 - b1_x1) ** 2 + (b2_y1 - b1_y1) ** 2
- d2 = (b2_x2 - b1_x2) ** 2 + (b2_y2 - b1_y2) ** 2
- return iou - d1 / hw.unsqueeze(1) - d2 / hw.unsqueeze(1) # MPDIoU
- elif WIoU:
- self = WIoU_Scale(1 - iou)
- dist = getattr(WIoU_Scale, '_scaled_loss')(self)
- return iou * dist # WIoU https://arxiv.org/abs/2301.10051
- return iou - rho2 / c2 # DIoU
- c_area = cw * ch + eps # convex area
- return iou - (c_area - union) / c_area # GIoU https://arxiv.org/pdf/1902.09630.pdf
- return iou # IoU
四、添加InnerIoU等损失函数到模型中
4.1 步骤一
上面的代码我们首先找到' ultralytics /utils/metrics.py'文件,然后其中有一个完全同名字的方法,原始样子如下,我们将我们的代码完整替换掉这个代码,记得是全部替换这个方法内的代码。
4.2 步骤二
替换成功后,我们找到另一个文件'ultralytics/utils/loss.py'然后找到如下一行代码原始样子下面的图片然后用我给的代码替换掉其中的红框内的一行即可。
- iou = bbox_iou(pred_bboxes[fg_mask], target_bboxes[fg_mask],
- xywh=False, GIoU=False, DIoU=False, CIoU=False, EIoU=False, SIoU=False,
- WIoU=False, ShapeIoU=False Inner=False,
- ratio=0.75, eps=1e-7, scale=0.0)
上面的代码我来解释一下,我把所有的能选用的参数都写了出来,其中IoU很好理解了,对应的参数设置为True就是使用的对应的IoU包括本文的IoU,需要注意的是Inner这个参数,比如我Inner设置为True然后Shape_IoU也设置为True那么此时使用的就是Inner_Shape_IoU,其它的都是,其中ratio和eps是inner的参数大家可以自己尝试我这里定义了两个基本值。
替换完后的样子如下->
4.3 步骤三
我们还需要修改一处,找到如下的文件''ultralytics/utils/tal.py''然后找到其中下面图片的代码,用我给的代码替换红框内的代码。
- def iou_calculation(self, gt_bboxes, pd_bboxes):
- """IoU calculation for horizontal bounding boxes."""
- return bbox_iou(gt_bboxes, pd_bboxes, xywh=False, GIoU=False, DIoU=False, CIoU=True,
- EIoU=False, SIoU=False, WIoU=False, ShapeIoU=False, Inner=False,
- ratio=0.7, eps=1e-7, scale=0.0).squeeze(-1).clamp_(0)
此处和loss.py里面的最好是使用同一个参数。
替换完之后的样子->
4.4 什么时候使用损失函数改进
在这里多说一下,就是损失函数的使用时间,当我们修改模型的时候,损失函数是作为一种保底的存在,就是说当其它模型结构都修改完成了,已经无法在提升精度了,此时就可以修改损失函数了,不要上来先修改损失函数,当然这是我个人的建议,具体还是由大家自己来选择。
YOLOv11调用 YOLOv8 损失函数计算修改了YOLOv8的损失函数即代表修改了YOLOv11!
五、本文总结
到此本文的正式分享内容就结束了,在这里给大家推荐我的YOLOv11改进有效涨点专栏,本专栏目前为新开的平均质量分98分,后期我会根据各种最新的前沿顶会进行论文复现,也会对一些老的改进机制进行补充,如果大家觉得本文帮助到你了,订阅本专栏,关注后续更多的更新~