【AI面试】回归损失横评:L1 loss、L2 loss和Smooth L1 Loss,L1正则化和L2正则化、IOU loss系列

本文深入探讨了深度学习中的回归损失函数,包括L1 loss、L2 loss和Smooth L1 Loss,以及L1和L2正则化的作用和区别。文章阐述了正则化如何通过限制模型参数复杂度来防止过拟合,并分析了L1正则化产生稀疏解和L2正则化得到平滑解的原因。同时,强调了Smooth L1 Loss在解决L1和L2 Loss缺陷上的优势,适合目标检测任务中的框回归。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


损失函数是深度学习模型优化的一个灵魂基础,所以无论是很新的 transformer模型,还是比较早期的 AlexNet,都不可避免的要涉及到损失函数的设计和应用。

所以,各种形形色色的损失函数(Loss)也就成了面试场上不经意间就会被牵扯到的问题。

其中,目标检测任务的损失函数由Classificition LossBounding Box Regeression Loss两部分构成。

本次我们着重关注BoundingBox Loss 目标检测回归损失,分类等损失函数参考这里:【AI面试】分类损失横评:CrossEntropy Loss 、Balanced Cross Entropy、 Dice Loss 和 Focal Loss

一、 L1 loss、L2 lo

将 Faster R-CNN 中的 smooth L1 loss 修改为 IoU loss 可以尝试如下代码实现: ```python import torch def iou_loss(pred_bbox, gt_bbox, eps=1e-6): """ Compute IoU loss between predicted bboxes and ground truth bboxes. Args: pred_bbox: predicted bboxes, shape [N, 4] gt_bbox: ground truth bboxes, shape [N, 4] eps: epsilon to avoid divide by zero Returns: iou_loss: IoU loss between predicted bboxes and ground truth bboxes, shape [N] """ # compute IoU x1 = torch.max(pred_bbox[:, 0], gt_bbox[:, 0]) y1 = torch.max(pred_bbox[:, 1], gt_bbox[:, 1]) x2 = torch.min(pred_bbox[:, 2], gt_bbox[:, 2]) y2 = torch.min(pred_bbox[:, 3], gt_bbox[:, 3]) w = torch.clamp(x2 - x1, min=0) h = torch.clamp(y2 - y1, min=0) inter = w * h a1 = (pred_bbox[:, 2] - pred_bbox[:, 0]) * (pred_bbox[:, 3] - pred_bbox[:, 1]) a2 = (gt_bbox[:, 2] - gt_bbox[:, 0]) * (gt_bbox[:, 3] - gt_bbox[:, 1]) union = a1 + a2 - inter iou = inter / (union + eps) # compute IoU loss threshold = 0.5 iou_loss = torch.pow(iou - threshold, 2) return iou_loss # example usage pred_bbox = torch.tensor([[2.0, 3.0, 5.0, 6.0], [1.0, 2.0, 4.0, 5.0]]) gt_bbox = torch.tensor([[1.0, 2.0, 4.0, 5.0], [2.0, 3.0, 5.0, 6.0]]) loss = iou_loss(pred_bbox, gt_bbox) print(loss) ``` 然后将 Faster R-CNN 中的 smooth L1 loss 替换为 iou loss,如下所示: ```python import torch import torch.nn as nn def iou_loss(pred_bbox, gt_bbox, eps=1e-6): """ Compute IoU loss between predicted bboxes and ground truth bboxes. Args: pred_bbox: predicted bboxes, shape [N, 4] gt_bbox: ground truth bboxes, shape [N, 4] eps: epsilon to avoid divide by zero Returns: iou_loss: IoU loss between predicted bboxes and ground truth bboxes, shape [N] """ # compute IoU x1 = torch.max(pred_bbox[:, 0], gt_bbox[:, 0]) y1 = torch.max(pred_bbox[:, 1], gt_bbox[:, 1]) x2 = torch.min(pred_bbox[:, 2], gt_bbox[:, 2]) y2 = torch.min(pred_bbox[:, 3], gt_bbox[:, 3]) w = torch.clamp(x2 - x1, min=0) h = torch.clamp(y2 - y1, min=0) inter = w * h a1 = (pred_bbox[:, 2] - pred_bbox[:, 0]) * (pred_bbox[:, 3] - pred_bbox[:, 1]) a2 = (gt_bbox[:, 2] - gt_bbox[:, 0]) * (gt_bbox[:, 3] - gt_bbox[:, 1]) union = a1 + a2 - inter iou = inter / (union + eps) # compute IoU loss threshold = 0.5 iou_loss = torch.pow(iou - threshold, 2) return iou_loss.mean() class FasterRCNN(nn.Module): def __init__(self, num_classes): super().__init__() self.num_classes = num_classes self.backbone = ... self.rpn = ... self.roi_head = ... self.bbox_head = nn.Linear(4096, 4 * self.num_classes) self.cls_head = nn.Linear(4096, self.num_classes) def forward(self, x, gt_bbox=None): # backbone x = self.backbone(x) # RPN rpn_cls, rpn_bbox = self.rpn(x) # RoI pooling rois = self.roi_head(x, rpn_bbox) # bbox regression bbox_pred = self.bbox_head(rois) bbox_pred = bbox_pred.reshape(-1, 4) # classification cls_score = self.cls_head(rois) cls_score = cls_score.reshape(-1, self.num_classes) cls_prob = nn.functional.softmax(cls_score, dim=1) # test or train if self.training: # compute loss rpn_loss, roi_loss = ... bbox_loss = iou_loss(bbox_pred, gt_bbox) cls_loss = ... total_loss = rpn_loss + roi_loss + bbox_loss + cls_loss return total_loss else: # inference result = ... return result ``` 需要注意的是,IoU loss 可能会导致梯度爆炸或梯度消失的问题,因此需要进行一些处理,例如使用渐进式策略或者加入正则化项等。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

钱多多先森

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

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

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

打赏作者

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

抵扣说明:

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

余额充值