其他分享
首页 > 其他分享> > Focal-EIOU Loss:用于精确边界框回归的高效IOU损失

Focal-EIOU Loss:用于精确边界框回归的高效IOU损失

作者:互联网

损失函数之Focal-EIoU Loss:

前言

EIoU Loss及Focal-EIoU Loss表达式

Focal-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

对 Focal-EIoU Loss性能进行验证

!git clone https://github.com/PaddlePaddle/PaddleDetection.git
%cd PaddleDetection/
!pip install -r requirements.txt -i https://mirror.baidu.com/pypi/simple
!pip install paddleslim==2.2.1

!python tools/train.py -c configs/picodet/picodet_s_320_voc.yml --eval --use_vdl=True --vdl_log_dir='./output_vdl'

GIOU、Focal-EIOU训练mAP可视化

GIOU、Focal-EIOU可视化细节

总结

实验结果表明:

请点击此处查看本环境基本用法.

Please click here for more detailed instructions.

标签:Loss,EIoU,EIOU,loss,IOU,paddle,损失,Focal
来源: https://blog.csdn.net/m0_63642362/article/details/122491856