多标签的focal_loss(基于yolov3的confidence_loss修改)

      最近在修改yolov3的损失函数,想提升模型性能,了解了focal_loss,记录一下这脑子转不动的一天。

   1.二分类Loss表达式

      首先,了解一下损失函数。p为预测概率,值在0-1之间,y为真实标签,只有两个值,存在1(1)与否(0)。如果存在,则我们计算的算是就是预测准确的损失,否则就是预测不存在的损失。

    2. Focal_loss表达式

       二分类的focal_loss表达式时基于上式演化而来的,首先我们获得不同真实标签时,概率的表达式。

       之后,我们将此概率代入focal_lossb表达式,如下图,其中pt的取值是根据真实标签获取的。   

在这里插入图片描述

 

      详细拆分后,具体公式如下所示:

           

### Focal EIOU Loss Implementation and Explanation in Object Detection In the context of object detection, loss functions play a crucial role in training models to accurately predict bounding boxes around objects. The focal EIOU (Extended Intersection over Union) loss function is an advanced variant that addresses some limitations found in traditional IoU-based losses. The standard IoU measures how well two bounding boxes overlap but does not account for other factors like scale or aspect ratio differences between predicted and ground truth boxes. To improve upon this, researchers introduced variants such as CIoU (Complete IoU), DIoU (Distance IoU), and finally EIoU which considers three aspects: - **Overlap**: Measures intersection area relative to union. - **Scale Difference**: Accounts for size discrepancy by penalizing predictions with different scales more heavily. - **Aspect Ratio Distortion**: Penalizes distortions along both axes independently rather than just considering overall shape similarity[^1]. Focal loss modifies cross entropy so it focuses on hard examples during training while reducing contribution from easy negatives. Combining these ideas leads us to define focal-EIoU as follows: \[ \text{FL}(p_t)=\alpha(1-p_t)^{\gamma}\log(p_t)\] Where \( p_t \) represents prediction confidence score after applying sigmoid activation. For regression part we use modified version of EIoU where weights are adjusted according to difficulty level indicated through classification branch output. Here's Python code implementing focal EIoU loss: ```python import torch from torch import nn class FocalEIoULoss(nn.Module): def __init__(self, alpha=0.25, gamma=2.0, eps=1e-7): super(FocalEIoULoss, self).__init__() self.alpha = alpha self.gamma = gamma self.eps = eps def forward(self, pred_boxes, target_boxes, labels): """ Args: pred_boxes: Predicted box coordinates [batch_size, num_anchors, 4]. target_boxes: Ground-truth box coordinates [batch_size, num_anchors, 4]. labels: Classification scores indicating positive/negative samples [batch_size, num_anchors]. Returns: Total loss value combining classification and localization components. """ # Compute pairwise distances between centers c_x_p, c_y_p = pred_boxes[:, :, :2].split([1, 1], dim=-1) c_x_g, c_y_g = target_boxes[:, :, :2].split([1, 1], dim=-1) dist_center = ((c_x_p - c_x_g)**2 + (c_y_p - c_y_g)**2).sqrt() w_pred, h_pred = pred_boxes[:, :, 2:].split([1, 1], dim=-1) w_gt, h_gt = target_boxes[:, :, 2:].split([1, 1], dim=-1) iou_loss_term = _compute_eiou(pred_boxes, target_boxes) cls_focal_loss = -(labels * (1-labels)**self.gamma * torch.log(torch.sigmoid(labels)+self.eps)).mean()[^2] total_loss = cls_focal_loss + iou_loss_term.mean() return total_loss def _compute_eiou(box_a, box_b): """Helper function computing Extended IOU.""" # Calculate center points distance squared rho_squared = ... # Width/height difference terms v_w = ... v_h = ... # Regular IoU component ious = ... eious = ious - (rho_squared / ...) - (...*(v_w+v_h)/(...)) return eious ```
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值