CIou论文
时间: 2025-04-01 17:13:45 浏览: 24
关于CIoU Loss的研究
CIoU (Complete Intersection over Union) 是一种改进的目标检测中的边界框回归损失函数,旨在解决IoU及其变体(GIoU 和 DIoU)中存在的不足之处。CIoU不仅考虑了两个边界框之间的重叠区域,还引入了几何中心距离以及宽高比例因子的影响,从而进一步提升了模型的收敛速度和定位精度。
在论文 "Distance-IoU Loss: Faster and Better Convergence for Bounding Box Estimation" 中首次提出了DIoU的概念,并在此基础上扩展出了CIoU[^4]。该论文指出,传统的IoU仅关注重叠面积的比例关系,在某些情况下无法有效反映实际位置偏差;而GIoU虽然解决了部分问题,但在优化过程中仍然可能存在梯度不稳定的情况。因此,作者设计了一种综合性的指标——CIoU,其定义如下:
[ \text{CIoU} = \text{IoU} - \frac{\rho^2(\text{box}, \text{gt})}{c^2} - \alpha v ]
其中,
- $\rho$ 表示预测框与真实框之间欧几里得距离;
- $c$ 代表覆盖两框最小闭包对角线长度;
- 参数$v$衡量长宽比差异程度:
[ v = \frac{(w_{pred}-w_{gt})^2 + (h_{pred}-h_{gt})^2}{(w_{gt}+h_{gt})^2} ]
最后通过$\alpha$调整权重平衡上述两项影响因素。
以下是实现CIoU计算的一个Python代码片段作为参考:
import torch
def cious(pred_boxes, target_boxes):
"""
Calculate the CIOU between prediction boxes and ground truth.
Args:
pred_boxes (Tensor): Predicted bounding box coordinates with shape [N,4].
target_boxes(Tensor): Ground-truth bounding box coordinates with same size as preds.
Returns:
Tensor: Computed CIoUs values of each pair.
"""
eps = 1e-7
# Extract coordinates from both tensors
px1, py1, px2, py2 = pred_boxes[:,0], pred_boxes[:,1], pred_boxes[:,2], pred_boxes[:,3]
tx1, ty1, tx2, ty2 = target_boxes[:,0], target_boxes[:,1], target_boxes[:,2], target_boxes[:,3]
iw = torch.min(px2,tx2)-torch.max(px1,tx1)+eps
ih = torch.min(py2,ty2)-torch.max(py1,ty1)+eps
inter_area=iw*ih*(iw>0)*(ih>0)
pw=pred_boxes[:,2]-pred_boxes[:,0]+eps
ph=pred_boxes[:,3]-pred_boxes[:,1]+eps
tw=target_boxes[:,2]-target_boxes[:,0]+eps
th=target_boxes[:,3]-target_boxes[:,1]+eps
union=(pw*ph)+(tw*th)-(inter_area)
ious=inter_area/union.clamp(min=eps)
center_x_pred=(px1+px2)/2
center_y_pred=(py1+py2)/2
center_x_gt =(tx1+tx2)/2
center_y_gt =(ty1+ty2)/2
rho_sq=((center_x_pred-center_x_gt)**2+(center_y_pred-center_y_gt)**2).clamp(min=eps)
w_closed=torch.max(tx2,px2)-torch.min(tx1,px1)+eps
h_closed=torch.max(ty2,py2)-torch.min(ty1,py1)+eps
diag_sq=w_closed**2+h_closed**2+eps
alpha_term=rho_sq/diag_sq
arctan_target=torch.atan((tw/th))
arctan_pred =torch.atan((pw/ph))
v_factor=(((arctan_target-arctan_pred)/(math.pi/4))**2)*((pw-ph)**2/(pw+ph)**2+eps)
alpha_v=v_factor/((1-ious+v_factor).clamp(min=eps))
ciou_loss=1-(ious-alpha_term*v_factor-alpha_v)
return ciou_loss.mean()
相关推荐


















