深度学习——损失函数(Regression Loss、Classification Loss)

简介

Loss function

损失函数

用于定义单个训练样本与真实值之间的误差

Cost function

代价函数

用于定义单个批次/整个训练集样本与真实值之间的误差

Objective function

目标函数

泛指任意可以被优化的函数

损失函数用于衡量模型所做出的预测离真实值(GT)之间的偏离程度。

损失函数分为两种:回归损失(针对连续型变量)和分类损失(针对离散型变量)

知道每一种损失函数的优点和局限性,才能更好的利用它们去解决实际问题

回归损失(Regression Loss)

L1 Loss

也称为Mean Absolute Error,即平均绝对误差(MAE),它衡量的是预测值与真实值之间的距离的平均误差幅度。

作用范围:0到正无穷。

公式如下:(N个样本,M个类别)

L_{l1 \space loss} = \frac{1}{N} \sum^{N}_{i=1} \sum^{M}_{t=1} |y_t^i - \hat{y}_t^i|

优点:收敛速度快,能够对梯度给予合适的惩罚权重,使梯度更新的方向可以更加精确。

缺点:对异常值敏感,梯度更新的方向很容易受离群点所主导,不具备鲁棒性。

L2 Loss

也称为Mean Squared Error,即均方差(MSE),它衡量的是预测值与真实值之间距离的平方和。

作用范围:0到正无穷。

公式如下:(N个样本,M个类别)

L_{l2 \space loss} = \frac{1}{N} \sum^{N}_{i=1} \sum^{M}_{t=1} (y_t^i - \hat{y}_t^i)^2

优点:对离群点(outliers)或者异常值更具有鲁棒性。

缺点:

  1. 在0点处的导数不连续,使得求解效率低下,导致收敛速度慢

  2. 对于较小的损失值,其梯度也与其他区间损失值的梯度一样大,不利于网络学习

异常值对于实际业务非常重要,可使用MSE作为损失函数

异常值仅仅表示损坏的数据,可使用MAE作为损失函数

Smooth L1 Loss

出自 Fast RCNN

综合L1和L2损失的优点,在0点处附近采用了L2损失中的平方函数,解决了L1损失在0点处梯度不可导的问题,使其更加平滑易于收敛。

公式如下:

smooth_{L1}(x) = \begin{cases} 0.5x^2 &\text{if } |x|<1 \\ |x|-0.5 &\text{otherwise} \end{cases}

IoU Loss

出自 UnitBox

常规的Lx损失中,都是基于目标边界中的4个坐标点信息之间分别进行回归损失计算,因此,这些边框信息之间相互独立。

IoU损失将候选框的四个边界信息作为一个整体进行回归,从而实现准确、高效的定位,具有很好的尺度不变性。

为了解决IoU度量不可导的现象,引入了负Ln范数来间接计算IoU损失。

 

Ground \space truth: \hat{x} = (\hat{x}_t, \hat{x}_b, \hat{x}_l, \hat{x}_r) \\ Prediction: x = (x_t, x_b, x_l, x_r) \\ IoU \space Loss = - \ln{\frac{Intersection(Ground \space truth, Prediction)}{Union(Ground \space truth, Prediction)}}

GIoU Loss

出自 Generalized Intersection over Union

A,B, the \space smallest \space encolsing \space convex \space object \space C \\ IoU = \frac{|A \cap B|}{|A \cup B|} \\ GIoU = IoU - \frac{|C-(A \cup B)|}{|C|} \\ L_{GIoU} = 1- GIoU

分类损失(Classification Loss)

Cross Entropy Loss

其中y_i​为真实值,p_i​​为该类别的预测值(对单个样本)

L(y,p) = -\sum_{i}{y_i \cdot \log{p_i}}

Focal Loss

出自 Focal Loss for Dense Object Detection

出发点:解决one-stage算法不如two-stage算法准确率高的问题。

主要原因:样本的类别不均衡(比如前景和背景)

比如在很多输入图片中,我们利用网格去划分小窗口,大多数的窗口是不包含目标的。

直接运用原始的交叉熵损失,那么负样本所占比例会非常大,主导梯度的优化方向,即网络会偏向于将前景预测为背景。即使使用OHEM(在线困难样本挖掘)算法来处理不均衡的问题,虽然其增加了误分类样本的权重,但也容易忽略掉易分类样本。

解决方案:Focal loss聚焦于训练一个困难样本的稀疏集,通过直接在标准的交叉熵损失函数基础上做改进,引进了两个惩罚因子,来减少易分类样本的权重,使得模型在训练过程中更专注于困难样本。

FL(p, \hat{p}) = -(\alpha (1-\hat{p})^{\gamma} p\log{\hat{p}} + (1-\alpha)\hat{p}^{\gamma}(1-p)\log{(1-\hat{p})})

Reference

一文看尽深度学习中的各种损失函数

损失函数|交叉熵损失函数

将 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 可能会导致梯度爆炸或梯度消失的问题,因此需要进行一些处理,例如使用渐进式策略或者加入正则化项等。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值