目标检测算法——YOLOv5将IOU Loss替换为EIOU Loss

Posted 加勒比海带66

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了目标检测算法——YOLOv5将IOU Loss替换为EIOU Loss相关的知识,希望对你有一定的参考价值。

论文题目:Focal and Efficient IOU Loss for Accurate Bounding Box Regression
论文地址:  https://arxiv.org/abs/2101.08158

实验中,将YOLOv5中的锚框损失函数替换为EIOU Loss,性能远优于原IOU、DIOU以及CIOU等,测试自身数据集发现涨点明显!想要代码咨询的小伙伴请私聊喔~

EIOU Loss代码如下:

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import numpy as np

import paddle

from ppdet.core.workspace import register, serializable
from ..bbox_utils import bbox_iou

__all__ = ['IouLoss', 'GIoULoss', 'EIouLoss']

@register
@serializable
class Focal_EIoU_Loss(GIoULoss):
    """
    Distance-IoU Loss, see https://arxiv.org/abs/1911.08287
    Args:
        loss_weight (float): giou loss weight, default as 1
        eps (float): epsilon to avoid divide by zero, default as 1e-10
        use_complete_iou_loss (bool): whether to use complete iou loss
    """

    def __init__(self, loss_weight=1., eps=1e-10, use_complete_iou_loss=True):
        super(DIouLoss, self).__init__(loss_weight=loss_weight, eps=eps)
        self.use_complete_iou_loss = use_complete_iou_loss

    def __call__(self, pbox, gbox, iou_weight=1.):
        x1, y1, x2, y2 = paddle.split(pbox, num_or_sections=4, axis=-1)
        x1g, y1g, x2g, y2g = paddle.split(gbox, num_or_sections=4, axis=-1)
        cx = (x1 + x2) / 2
        cy = (y1 + y2) / 2
        w = x2 - x1
        h = y2 - y1

        cxg = (x1g + x2g) / 2
        cyg = (y1g + y2g) / 2
        wg = x2g - x1g
        hg = y2g - y1g

        x2 = paddle.maximum(x1, x2)
        y2 = paddle.maximum(y1, y2)

        # A and B
        xkis1 = paddle.maximum(x1, x1g)
        ykis1 = paddle.maximum(y1, y1g)
        xkis2 = paddle.minimum(x2, x2g)
        ykis2 = paddle.minimum(y2, y2g)

        # A or B
        xc1 = paddle.minimum(x1, x1g)
        yc1 = paddle.minimum(y1, y1g)
        xc2 = paddle.maximum(x2, x2g)
        yc2 = paddle.maximum(y2, y2g)

        intsctk = (xkis2 - xkis1) * (ykis2 - ykis1)
        intsctk = intsctk * paddle.greater_than(
            xkis2, xkis1) * paddle.greater_than(ykis2, ykis1)
        unionk = (x2 - x1) * (y2 - y1) + (x2g - x1g) * (y2g - y1g
                                                        ) - intsctk + self.eps
        iouk = intsctk / unionk

        # DIOU term
        dist_intersection = (cx - cxg) * (cx - cxg) + (cy - cyg) * (cy - cyg)
        dist_union = (xc2 - xc1) * (xc2 - xc1) + (yc2 - yc1) * (yc2 - yc1)
        diou_term = (dist_intersection + self.eps) / (dist_union + self.eps)

        
        # EIOU term
        c2_w = (xc2 - xc1) * (xc2 - xc1) + self.eps
        c2_h = (yc2 - yc1) * (yc2 - yc1) + self.eps
        rho2_w = (w - wg) * (w - wg)
        rho2_h = (h - hg) * (h - hg)
        eiou_term = (rho2_w / c2_w) + (rho2_h / c2_h)        

        #Focal-EIOU
        eiou = paddle.mean((1 - iouk + diou_term + eiou_term) * iou_weight)
        focal_eiou = iouk**0.5 * eiou
        return focal_eiou * self.loss_weight

相比于其他目标框回归损失函数,EIOU Loss考虑了重叠面积,中心点距离、长宽边长真实差,基于CIOU解决了纵横比的模糊定义,并添加Focal Loss解决BBox回归中的样本不平衡问题。

《新程序员》:云原生和全面数字化实践 50位技术专家共同创作,文字、视频、音频交互阅读

以上是关于目标检测算法——YOLOv5将IOU Loss替换为EIOU Loss的主要内容,如果未能解决你的问题,请参考以下文章

目标检测回归损失函数简介:SmoothL1/IoU/GIoU/DIoU/CIoU Loss

目标检测中回归损失函数(L1Loss,L2Loss,Smooth L1Loss,IOU,GIOU,DIOU,CIOU,EIOU,αIOU ,SIOU)

深度学习目标检测回归损失函数合集:SmoothL1/IoU/GIoU/DIoU/CIoU Loss

YOLOv5全面解析教程③:更快更好的边界框回归损失

目标检测算法 YOLOv8 原理解析|包揽目标检测实例分割 SOTA

IoU Loss综述(IOU,GIOU,CIOU,EIOU,SIOU,WIOU)