分类损失函数

OpenPCDet的多标签分类损失函数使用的是focal loss,进行了自定义实现,代码如下:

import numpy as np
import torch
import torch.nn as nn

class SigmoidFocalClassificationLoss(nn.Module):
    """
    Sigmoid focal cross entropy loss.
    """

    def __init__(self, gamma: float = 2.0, alpha: float = 0.25):
        """
        Args:
            gamma: Weighting parameter to balance loss for hard and easy examples.
            alpha: Weighting parameter to balance loss for positive and negative examples.
        """
        super(SigmoidFocalClassificationLoss, self).__init__()
        self.alpha = alpha
        self.gamma = gamma

    @staticmethod
    def sigmoid_cross_entropy_with_logits(input: torch.Tensor, target: torch.Tensor):
        """ PyTorch Implementation for tf.nn.sigmoid_cross_entropy_with_logits:
            max(x, 0) - x * z + log(1 + exp(-abs(x))) in
            https://www.tensorflow.org/api_docs/python/tf/nn/sigmoid_cross_entropy_with_logits

        Args:
            input: (B, #anchors, #classes) float tensor.
                Predicted logits for each class
            target: (B, #anchors, #classes) float tensor.
                One-hot encoded classification targets

        Returns:
            loss: (B, #anchors, #classes) float tensor.
                Sigmoid cross entropy loss without reduction
        """
        loss = torch.clamp(input, min=0) - input * target + \
               torch.log1p(torch.exp(-torch.abs(input)))
        return loss

    def forward(self, input: torch.Tensor, target: torch.Tensor, weights: torch.Tensor):
        """
        Args:
            input: (B, #anchors, #classes) float tensor.
                Predicted logits for each class
            target: (B, #anchors, #classes) float tensor.
                One-hot encoded classification targets
            weights: (B, #anchors) float tensor.
                Anchor-wise weights.

        Returns:
            weighted_loss: (B, #anchors, #classes) float tensor after weighting.
        """

        import ipdb
        ipdb.set_trace()

        pred_sigmoid = torch.sigmoid(input)
        alpha_weight = target * self.alpha + (1 - target) * (1 - self.alpha)
        pt = target * (1.0 - pred_sigmoid) + (1.0 - target) * pred_sigmoid
        focal_weight = alpha_weight * torch.pow(pt, self.gamma)

        bce_loss = self.sigmoid_cross_entropy_with_logits(input, target)

        loss = focal_weight * bce_loss

        if weights.shape.__len__() == 2 or \
                (weights.shape.__len__() == 1 and target.shape.__len__() == 2):
            weights = weights.unsqueeze(-1)

        assert weights.shape.__len__() == loss.shape.__len__()

        return loss * weights

代码理解:
focal loss源于交叉熵损失,通过两个超参数分别解决了简单和困难样本以及正负样本不均衡的问题,其公式如下:


代码自定义了sigmoid_cross_entropy_with_logits()交叉熵损失,这里以sigmoid函数做为logistic函数,所有输入sigmoid之前的函数都可以叫logit,这里是多输入,所以叫logits。具体解释参见这里,有一些解释非常详细。这里输入的预测值input是网络的回归输出,而target为相同shape的one-hot张量。实现是交叉熵损失的一种变形,具体推导如下:

传入的weights参数对每个正anchor和负anchor进行了一个平均,使得每个样本的损失与样本中目标的数量无关。就是对一个样本中每个正负anchor的损失除以这个样本中目标的数量,至于为什么只除以目标的数量,我觉得可能是因为focal loss已经对正负样本做了均衡。

创建于2020.12.04

你可能感兴趣的:(分类损失函数)