目标检测中的Bounding Box Regression Loss
发布日期:2021-06-30 20:33:03 浏览次数:2 分类:技术文章

本文共 5269 字,大约阅读时间需要 17 分钟。

目标检测中的Bounding Box Regression Loss

PyTorch:

目标检测任务的损失函数由Classificition Loss和Bounding Box Regeression Loss两部分构成。本文介绍目标检测任务中近几年来Bounding Box Regression Loss的演变过程

Smooth L1 Loss

Smooth L1通过综合L1和L2损失的优点,在0点处附近采用了L2损失中的平方函数,解决了L1损失在0点处梯度不可导的问题,使其更加平滑易于收敛。此外,在|x|>1的区间上,它又采用了L1损失中的线性函数,使得梯度能够快速下降。

在这里插入图片描述

  • L1损失函数对x的导数为常数,在训练后期,x很小时,如果learning rate 不变,损失函数会在稳定值附近波动,很难收敛到更高的精度。
  • L2损失函数对x的导数在x值很大时,其导数也非常大,在训练初期不稳定。
  • smooth L1完美的避开了L1和L2损失的缺点。Smooth L1 Loss结合了L2 Loss收敛更快,且在0点有导数,便于收敛的好处。也在边界区域结合了L1 Loss的好处,让网络对异常值更加robust,能够在偏移值较大时还能拉回来。

在这里插入图片描述

def smooth_l1_loss(input, target, beta=1. / 9, size_average=True):    """    very similar to the smooth_l1_loss from pytorch, but with    the extra beta parameter    """    n = torch.abs(input - target)    cond = n < beta    loss = torch.where(cond, 0.5 * n ** 2 / beta, n - 0.5 * beta)    if size_average:        return loss.mean()    return loss.sum()

IoU Loss

L1 和L2 loss是将bbox四个点分别求loss然后相加,并没有考虑靠坐标之间的相关性,而实际评价指标IOU是具备相关性。此外基于L1和L2的距离的loss对于尺度不具有不变性。

因此IoU Loss,其将4个点构成的box看成一个整体进行回归,同时为了解决IoU度量不可导的现象,引入了负Ln范数来间接计算IoU损失。

在这里插入图片描述

class IOULoss(nn.Module):    def __init__(self):        super(IOULoss, self).__init__()    def forward(self, pred, target, weight=None):        pred_left = pred[:, 0]        pred_top = pred[:, 1]        pred_right = pred[:, 2]        pred_bottom = pred[:, 3]        target_left = target[:, 0]        target_top = target[:, 1]        target_right = target[:, 2]        target_bottom = target[:, 3]        target_aera = (target_left + target_right) * \                      (target_top + target_bottom)        pred_aera = (pred_left + pred_right) * \                    (pred_top + pred_bottom)        w_intersect = torch.min(pred_left, target_left) + \                      torch.min(pred_right, target_right)        h_intersect = torch.min(pred_bottom, target_bottom) + \                      torch.min(pred_top, target_top)        g_w_intersect = torch.max(pred_left, target_left) + \                        torch.max(pred_right, target_right)        g_h_intersect = torch.max(pred_bottom, target_bottom) + \                        torch.max(pred_top, target_top)        ac_uion = g_w_intersect * g_h_intersect        area_intersect = w_intersect * h_intersect        area_union = target_aera + pred_aera - area_intersect        ious = (area_intersect + 1.0) / (area_union + 1.0)        losses = -torch.log(ious)        if weight is not None:            return (losses * weight).sum()        else:            return losses.sum()

优点:

  • IoU作为距离度量时, 包含了作为度量的所有属性,比如:非负性,不确定性对称性和三角不等性。
  • IoU有尺度不变性,这意味着任意两个方框A和B的相似性与他们的空间尺度的无关。

缺点:

  • 当预测框和目标框不相交时,IoU(A,B)=0时,不能反映A,B距离的远近,此时损失函数不可导,IoU Loss 无法优化两个框不相交的情况。
  • 假设预测框和目标框的大小都确定,只要两个框的相交值是确定的,其IoU值是相同时,IoU值不能反映两个框是如何相交的。

GIoU Loss

IOU Loss无法对两个不重叠的框进行优化,而且IOU Loss无法反映出两个框到底距离有多远。因此GIoU 的提出就是为了缓解这种问题。

在这里插入图片描述

在这里插入图片描述

其中,C表示两个框的最小外接矩阵面积。

class IOULoss(nn.Module):    def __init__(self):        super(IOULoss, self).__init__()    def forward(self, pred, target, weight=None):        pred_left = pred[:, 0]        pred_top = pred[:, 1]        pred_right = pred[:, 2]        pred_bottom = pred[:, 3]        target_left = target[:, 0]        target_top = target[:, 1]        target_right = target[:, 2]        target_bottom = target[:, 3]        target_aera = (target_left + target_right) * \                      (target_top + target_bottom)        pred_aera = (pred_left + pred_right) * \                    (pred_top + pred_bottom)        w_intersect = torch.min(pred_left, target_left) + \                      torch.min(pred_right, target_right)        h_intersect = torch.min(pred_bottom, target_bottom) + \                      torch.min(pred_top, target_top)        g_w_intersect = torch.max(pred_left, target_left) + \                        torch.max(pred_right, target_right)        g_h_intersect = torch.max(pred_bottom, target_bottom) + \                        torch.max(pred_top, target_top)        ac_uion = g_w_intersect * g_h_intersect        area_intersect = w_intersect * h_intersect        area_union = target_aera + pred_aera - area_intersect        ious = (area_intersect + 1.0) / (area_union + 1.0)        gious = ious - (ac_uion - area_union) / ac_uion        losses = 1 - gious        if weight is not None:            return (losses * weight).sum()        else:            return losses.sum()

优点:

  • GIOU具有尺度不变性
  • GIoU考虑到了 IoU 没有考虑到的非重叠区域,能够反应出 A,B 重叠的方式。
  • 相较于IoU Loss, GIoU Loss在单阶段检测器YOLO v1涨了2个点,两阶段检测器涨点相对较少(RPN的box比较多,两个框未相交的数量相对较少)

缺点:

  • 收敛速度慢
  • 当预测框和目标框垂直或者水平时,C − A U B变得很小,甚至为零(包含时),GIOU退化为IOU

DIoU Loss

DIoU:在IoU损失上加上一个惩罚项,以直接最小化两个边界盒中心点之间的规范化距离,从而比GIoU损失更快地收敛。

  • 边界盒框的重叠区域
  • 中心点距离
    在这里插入图片描述
    其中,​d表示预测框和标定框的中点的欧氏距离,c是覆盖这两个盒子的最小包围盒的对角线长度。
 

优点:

DIoU和GIoU的区别
在这里插入图片描述
明显可以看到
GIoU:会产生更大的预测框来覆盖到目标,这是之前已有的base。
DIoU:可以使预测框的中心更大程度的接近目标,避免产生过大的框体
缺点:
没有考虑高宽比的一致性

CIoU Loss

CIoU:提出的DIoU损失旨在同时考虑边界盒框的重叠区域和中心点距离。然而,边界框高宽比的一致性也是一个重要的几何因素。因此,在DIoU损失的基础上,通过对高宽比的一致性施加影响,提出了CIoU损失的概念。

  • 边界盒框的重叠区域(Overlap area)
  • 中心点距离(Central point distance)
  • 高宽比的一致性(Aspect ratio)
 

优点:

缺点:

KL Loss

目标检测依赖于边界框回归来提高定位精度,而且通常只是直接预测出边界框偏移量,并未考虑边界框的不确定性。

边界框的不确定来通常来源于:

(1)数据集标注的不确定性
(2)遮挡所引入的不确定性
(3)数据自身的不确定性
KL Loss考虑了边界框回归的不确定性,即,首先假设将边界框回归满足高斯分布,然后通过KL loss来学习高斯分布的标准差,并以标准差来衡量边界框回归的不确定性。

 

优点:

  • 数据集中的边框的标注的歧义性可以很好的捕捉到。边框的回归可以得到更小的loss从歧义性的标注边框中
  • 网络学习的方差是有用的,可以应用于后续后处理中。
  • 学习的概率分布具有可解释性。包括,网络预测的高斯分布,ground truth的狄拉克分布。

缺点:

参考资料:

1
2
3
4
5
6
7

转载地址:https://liumin.blog.csdn.net/article/details/116612484 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:深度学习论文: Activate or Not: Learning Customized Activation及其PyTorch实现
下一篇:PyTorch代码优化技巧

发表评论

最新留言

哈哈,博客排版真的漂亮呢~
[***.90.31.176]2024年04月26日 01时45分56秒