RT-DETR改进策略【损失函数篇】| NWD损失函数,提高小目标检测精度

一、本文介绍

本文记录的是基于NWD的RT-DETR的损失函数改进方法研究。目前的IoU-Loss在一些情况下不能提供梯度来优化网络,例如当预测边界框P和真实边界框G没有重叠(即 ∣ P ∩ G ∣ = 0 |P \cap G| = 0 PG=0),或者P完全包含G或反之(即 ∣ P ∩ G ∣ = P |P \cap G| = P P

### RT-DETR 模型使用的损失函数解释 #### WIoU 损失函数 RT-DETR 中引入了一种新的损失函数——WIoU (Weighted Intersection over Union),该损失函数旨在解决传统 IoU 及其变体(如 CIoU、SIoU)在某些情况下表现不佳的问题[^2]。 WIoU 的核心思想是在计算两个边界框之间的交并比时加入权重因子,使得对于不同大小的目标能够更加公平地评估预测框的质量。具体来说,WIoU 不仅考虑了重叠区域的比例,还通过加权的方式调整了对大目标和小目标的不同敏感度: \[ \text{WIoU} = \frac{\sum w_i A_i}{\sum w_j B_j}, \] 其中 \(A\) 表示真实标签框面积,\(B\) 是预测框面积,而 \(w\) 则代表基于物体尺寸或其他特征设定的权重系数[^3]。 这种改进有助于提高模型处理多尺度对象的能力,并且可以加速训练过程中的收敛速度以及改善最终检测精度。 #### 实现代码示例 以下是 Python 版本实现 WIoU 计算的一个简单例子: ```python import torch def wiou_loss(pred_boxes, gt_boxes): """ Calculate Weighted IOU loss between predicted boxes and ground truth boxes. Args: pred_boxes (Tensor): Predictions from the model with shape [N, 4]. gt_boxes (Tensor): Ground truths with same shape as `pred_boxes`. Returns: Tensor: Scalar tensor representing mean weighted IOU loss across all pairs of boxes. """ # Compute areas of both sets of boxes area_pred = (pred_boxes[:, 2] - pred_boxes[:, 0]) * \ (pred_boxes[:, 3] - pred_boxes[:, 1]) area_gt = (gt_boxes[:, 2] - gt_boxes[:, 0]) * \ (gt_boxes[:, 3] - gt_boxes[:, 1]) # Find intersection coordinates inter_xmin = torch.max(pred_boxes[:, None, 0], gt_boxes[:, 0]) inter_ymin = torch.max(pred_boxes[:, None, 1], gt_boxes[:, 1]) inter_xmax = torch.min(pred_boxes[:, None, 2], gt_boxes[:, 2]) inter_ymax = torch.min(pred_boxes[:, None, 3], gt_boxes[:, 3]) # Clip values to ensure no negative intersections occur width_intersections = torch.clamp(inter_xmax - inter_xmin, min=0.) height_intersections = torch.clamp(inter_ymax - inter_ymin, min=0.) # Calculate intersection area intersection_area = width_intersections * height_intersections # Define weights based on box sizes or other criteria here... weight_factor = ... union_areas = area_pred.view(-1, 1) + area_gt.view(1, -1) - intersection_area ious = intersection_area / union_areas # Apply weighting factor wiou_losses = 1. - (weight_factor * ious).mean() return wiou_losses.mean() ``` 此段代码展示了如何定义一个 PyTorch 函数来计算给定一组预测框 (`pred_boxes`) 和对应的真实标注框 (`gt_boxes`) 下的平均 WIoU 损失值。注意,在实际应用中可能还需要根据具体情况设置合适的权重参数 `weight_factor` 来优化性能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Limiiiing

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

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

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

打赏作者

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

抵扣说明:

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

余额充值