目标检测--边框回归损失函数SIoU原理详解及代码实现

边框回归损失函数

1. SIoU

1.1 原理

有关IoU损失函数,如(GIoU, DIoU, CIoU)没有考虑到真实框与预测框框之间的方向,导致收敛速度较慢,对此SIoU引入真实框和预测框之间的向量角度,重新定义相关损失函数,具体包含四个部分:
(1)角度损失(Angle cost),定义如下
在这里插入图片描述
Λ = 1 − 2 ∗ sin ⁡ 2 ( arcsin ⁡ ( c h σ ) − π 4 ) = cos ⁡ ( 2 ∗ ( arcsin ⁡ ( c h σ ) − π 4 ) ) \Lambda = 1-2*\sin^2(\arcsin(\frac{c_h}{\sigma}) - \frac{\pi}{4})=\cos(2*(\arcsin(\frac{c_h}{\sigma}) - \frac{\pi}{4})) Λ=12sin2(arcsin(σch)4π)=cos(2(arcsin(σch)4π))
其中 c h c_h ch为真实框和预测框中心点的高度差, σ \sigma σ为真实框和预测框中心点的距离,事实上 arcsin ⁡ ( c h σ ) \arcsin (\frac{c_h}{\sigma}) arcsin(σch)等于角度 α \alpha α
c h σ = sin ⁡ ( α ) \frac{c_h}{\sigma}=\sin(\alpha) σch=sin(α)
σ = ( b c x g t − b c x ) 2 + ( b c y g t − b c y ) 2 \sigma = \sqrt{(b_{c_x}^{gt}-b_{c_x})^2+(b_{c_y}^{gt}-b_{c_y})^2} σ=(bcxgtbcx)2+(bcygtbcy)2
c h = max ⁡ ( b c y g t , b c y ) − min ⁡ ( b c y g t , b c y ) c_h = \max(b_{c_y}^{gt}, b_{c_y}) - \min(b_{c_y}^{gt}, b_{c_y}) ch=max(bcygt,bcy)min(bcygt,bcy)

( b c x g t , b c y g t ) (b_{c_x}^{gt}, b_{c_y}^{gt}) (bcxgt,bcygt)为真实框中心坐标 ( b c x , b c y ) (b_{c_x}, b_{c_y}) (bcx,bcy)为预测框中心坐标,可以注意到当 α \alpha α π 2 \frac{\pi}{2} 2π或0时,角度损失为0,在训练过程中若 α < π 4 \alpha < \frac{\pi}{4} α<4π,则最小化 α \alpha α,否则最小化 β \beta β

(2)距离损失(Distance cost),定义如下:
在这里插入图片描述
Δ = ∑ t = x , y ( 1 − e − γ ρ t ) = 2 − e − γ ρ x − e − γ ρ y \Delta = \sum_{t=x,y}(1-e^{-\gamma\rho_t})=2-e^{-\gamma\rho_x}-e^{-\gamma\rho_y} Δ=t=x,y(1eγρt)=2eγρxeγρy
其中:
ρ x = ( b c x g t − b c x c w ) 2 , ρ y = ( b c y g t − b c y c h ) 2 γ = 2 − Λ \rho_x = (\frac{b_{c_x}^{gt} - b_{c_x}}{c_w})^2, \quad \rho_y= (\frac{b_{c_y}^{gt} - b_{c_y}}{c_h})^2 \quad \gamma = 2 - \Lambda ρx=(cwbcxgtbcx)2,ρy=(chbcygtbcy)2γ=2Λ
注意:这里的 ( c w , c h ) (c_w, c_h) (cw,ch)为真实框和预测框最小外接矩形的宽和高

(3)形状损失(Shape cost),定义如下:
Ω = ∑ t = w , h ( 1 − e − w t ) θ = ( 1 − e − w w ) θ + ( 1 − e − w h ) θ \Omega = \sum_{t=w, h}(1-e^{-w_t})^\theta=(1-e^{-w_w})^\theta+(1-e^{-w_h})^\theta Ω=t=w,h(1ewt)θ=(1eww)θ+(1ewh)θ
其中:
w w = ∣ w − w g t ∣ max ⁡ ( w , w g t ) , w h = ∣ h − h g t ∣ max ⁡ ( h , h g t ) w_w=\frac{|w-w^{gt}|}{\max(w, w^{gt})}, \quad w_h=\frac{|h-h^{gt}|}{\max(h, h^{gt})} ww=max(w,wgt)wwgt,wh=max(h,hgt)hhgt
( w , h ) (w, h) (w,h) ( w g t , h g t ) (w^{gt}, h^{gt}) (wgt,hgt)分别为预测框和真实框的宽和高, θ \theta θ控制对形状损失的关注程度,为了避免过于关注形状损失而降低对预测框的移动,作者使用遗传算法计算出 θ \theta θ接近4,因此作者定于 θ \theta θ参数范围为[2, 6]

(4)IoU损失(IoU cost)
在这里插入图片描述
I o U = 交 集 A 并 集 B IoU=\frac{交集A}{并集B} IoU=BA

综上所诉,最终SIoU损失函数定义如下:
L o s s S I o U = 1 − I o U + Δ + Ω 2 Loss_{SIoU}=1-IoU+\frac{\Delta + \Omega}{2} LossSIoU=1IoU+2Δ+Ω

1.2 代码实现

有关SIoU得代码实现如下(来源美团yolov6):

elif self.iou_type == 'siou':
	# SIoU Loss https://arxiv.org/pdf/2205.12740.pdf
	'''
	预测框和真实框坐标形式为xyxy,即左下右上角坐标或左上右下角坐标
	'''
	s_cw = (b2_x1 + b2_x2 - b1_x1 - b1_x2) * 0.5 #真实框和预测框中心点的宽度差
	s_ch = (b2_y1 + b2_y2 - b1_y1 - b1_y2) * 0.5 #真实框和预测框中心点的高度差
	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) #α大于45°则考虑优化β,否则优化α
	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) #形状损失
	iou = iou - 0.5 * (distance_cost + shape_cost) #siou

loss = 1.0 - iou
### SIoU 损失函数概述 SIoU (Scaled IoU) 是一种改进版的目标检测边界框回归损失函数,在传统IoU基础上进行了增强。这种损失函数旨在解决不同尺度物体之间的边界框回归问题,提供更强大的学习能力[^3]。 #### SIoU 的特点 相比传统的IoU和其他变体(如GIoU, CIoU),SIoU通过引入缩放因子来更好地处理大尺寸变化下的对象定位精度问题。具体来说: - **比例不变性**:能够适应各种大小的对象而不受其绝对尺寸的影响。 - **更好的收敛特性**:有助于加速训练过程并获得更高的最终性能指标。 - **鲁棒性强**:即使在极端情况下也能保持良好的表现。 #### 实现细节 以下是Python代码示例展示了如何定义和使用SIoU损失函数来进行边界框回归任务: ```python import torch from torch import nn class SIoULoss(nn.Module): def __init__(self, eps=1e-7): super(SIoULoss, self).__init__() self.eps = eps def forward(self, pred_bboxes, target_bboxes): """ 计算两个边界框间的SIoU距离 参数: pred_bboxes: 预测的边界框坐标 Tensor 形状为[N, 4], xywh格式. target_bboxes: 真实标签边界框坐标 Tensor 形状为[N, 4], xywh格式. 返回: siou_loss: 边界框间SIoU距离平均值 """ # 将中心点形式转换成左上角右下角的形式 px1y1, px2y2 = pred_bboxes[:, :2] - pred_bboxes[:, 2:] / 2, \ pred_bboxes[:, :2] + pred_bboxes[:, 2:] / 2 tx1y1, tx2y2 = target_bboxes[:, :2] - target_bboxes[:, 2:] / 2, \ target_bboxes[:, :2] + target_bboxes[:, 2:] / 2 # 获取交集区域面积以及并集总面积 inter_area = (torch.min(px2y2, tx2y2) - torch.max(px1y1, tx1y1)).clamp(0).prod(dim=-1) union_area = ((px2y2 - px1y1).prod(-1) + (tx2y2 - tx1y1).prod(-1) - inter_area) iou = inter_area / (union_area + self.eps) # 计算中心点差距向量及其长度平方 center_dist_squared = ( (pred_bboxes[..., :2] - target_bboxes[..., :2]) ** 2).sum(axis=-1) # 对角线最长边长计算 enclose_wh = (torch.max(px2y2, tx2y2) - torch.min(px1y1, tx1y1)).clamp(min=self.eps) diagonal_len_squared = (enclose_wh ** 2).sum(dim=-1) # 定义额外惩罚项C C = center_dist_squared / diagonal_len_squared # 缩放因子S S = 4 * torch.sigmoid(pred_bboxes[..., 2:]) * \ torch.sigmoid(target_bboxes[..., 2:]) v = (4 / math.pi ** 2) * \ torch.pow(torch.atan(w_true/h_true)-torch.atan(w_pred/h_pred), 2) alpha = v/(v-(iou-S*C)+eps) loss = 1-iou+(alpha*v)+(S*C) return loss.mean() ``` 此实现基于PyTorch框架,并考虑到了数值稳定性因素以防止除零错误等问题的发生。需要注意的是实际应用时可能还需要根据具体情况调整超参数设置或优化策略。
评论 12
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值