0%

YOLO的WIoU和CIoU对比

在目标检测任务中,损失函数对检测精度和训练稳定性有重要影响。针对YOLO系列中WIoU(Wise IoU)和CIoU(Complete IoU)两种边界框损失函数的对比分析如下:

维度 CIoU WIoU (v1/v2)
优化目标 位置+形状 位置+动态样本权重
小目标检测 一般 ✅ 更优(抑制低质量样本干扰)
训练稳定性 需谨慎设置学习率 ✅ 更鲁棒(自适应梯度调整)
计算复杂度 略高(需计算动态权重)
超参数敏感性 较高(需调 α 系数) 较低
工业部署兼容性 ✅ 广泛支持 需自定义实现

WIoU v3相比v1和v2有以下优势:
1.非单调性设计,可以更好地处理不同尺度的目标;
2.通过iou * torch.exp((rho2 / c2))的计算方式,在保持IoU基本特性的同时,增加了对中心点距离的惩罚
更适合处理复杂场景下的目标检测任务。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#loss.py
class BboxLoss(nn.Module):
"""Criterion class for computing training losses during training."""

def __init__(self, reg_max=16):
"""Initialize the BboxLoss module with regularization maximum and DFL settings."""
super().__init__()
self.dfl_loss = DFLoss(reg_max) if reg_max > 1 else None

def forward(self, pred_dist, pred_bboxes, anchor_points, target_bboxes, target_scores, target_scores_sum, fg_mask):
"""IoU loss."""
weight = target_scores.sum(-1)[fg_mask].unsqueeze(-1)
iou = bbox_iou(pred_bboxes[fg_mask], target_bboxes[fg_mask], xywh=False, WIoU=True, scale=False) # WIoU v3
loss_iou = ((1.0 - iou) * weight).sum() / target_scores_sum

# DFL loss
if self.dfl_loss:
target_ltrb = bbox2dist(anchor_points, target_bboxes, self.dfl_loss.reg_max - 1)
loss_dfl = self.dfl_loss(pred_dist[fg_mask].view(-1, self.dfl_loss.reg_max), target_ltrb[fg_mask]) * weight
loss_dfl = loss_dfl.sum() / target_scores_sum
else:
loss_dfl = torch.tensor(0.0).to(pred_dist.device)

return loss_iou, loss_dfl
1
2
3
4
5
6
7
8
9
10
11
# metrics.py
#……
elif WIoU:
if Focal:
raise RuntimeError("WIoU do not support Focal.")
elif scale:
return getattr(WIoU_Scale, '_scaled_loss')(self), (1 - iou) * torch.exp(
(rho2 / c2)), iou # WIoU https://arxiv.org/abs/2301.10051
else:
return iou * torch.exp((rho2 / c2)) # WIoU v3
#return iou, torch.exp((rho2 / c2)) # WIoU v1

结论

常规场景
若数据质量较高且目标尺寸分布均匀,CIoU仍能取得较好效果(尤其对形状敏感的任务)。

复杂场景推荐WIoU
在以下情况优先选择WIoU:
数据存在大量低质量标注(遮挡、模糊)
小目标占比高或目标尺寸差异大
追求更高mAP(WIoUv3相比CIoU约有0.5-1.2%提升)