<filename>mmdet/models/losses/norm_loss.py<gh_stars>0
# Copyright (c) OpenMMLab. All rights reserved.
import torch
import torch.nn as nn

from ..builder import LOSSES
from .utils import weight_reduce_loss


@LOSSES.register_module()
class SimpleNormLoss(nn.Module):

    def __init__(self,
                 reduction='mean',
                 beta=0.1,
                 target=1.0,
                 loss_weight=0.1):
        """CrossEntropyLoss.

        Args:
            reduction (str, optional): . Defaults to 'mean'.
                Options are "none", "mean" and "sum".
            loss_weight (float, optional): Weight of the loss. Defaults to 1.0.
        """
        super(SimpleNormLoss, self).__init__()
        assert beta > 0 and target > 0
        self.beta = beta
        self.reduction = reduction
        self.loss_weight = loss_weight
        self.target = target

    def forward(self,
                feats,
                avg_factor,
                reduction_override=None,
                **kwargs):
        """Forward function.

        Args:
            cls_feats (torch.Tensor): The prediction.
            label_embeddings (torch.Tensor): The learning label of the prediction.
            weight (torch.Tensor, optional): Sample-wise loss weight.
            avg_factor (int, optional): Average factor that is used to average
                the loss. Defaults to None.
            reduction_override (str, optional): The method used to reduce the
                loss. Options are "none", "mean" and "sum".
        Returns:
            torch.Tensor: The calculated loss.
        """
        assert reduction_override in (None, 'none', 'mean', 'sum')
        reduction = (
            reduction_override if reduction_override else self.reduction)

        feats_norm = feats.norm(dim=-1)

        diff = torch.abs(feats_norm - self.target)
        loss = torch.where(diff < self.beta, diff * self.beta,
                           diff)

        loss = weight_reduce_loss(
            loss, weight=torch.ones_like(loss),
            reduction=reduction, avg_factor=avg_factor)

        return loss * self.loss_weight

<filename>mmdet/models/losses/norm_loss.py<gh_stars>0
# Copyright (c) OpenMMLab. All rights reserved.
import torch
import torch.nn as nn

from ..builder import LOSSES
from .utils import weight_reduce_loss


@LOSSES.register_module()
class SimpleNormLoss(nn.Module):

    def __init__(self,
                 reduction='mean',
                 beta=0.1,
                 target=1.0,
                 loss_weight=0.1):
        """CrossEntropyLoss.

        Args:
            reduction (str, optional): . Defaults to 'mean'.
                Options are "none", "mean" and "sum".
            loss_weight (float, optional): Weight of the loss. Defaults to 1.0.
        """
        super(SimpleNormLoss, self).__init__()
        assert beta > 0 and target > 0
        self.beta = beta
        self.reduction = reduction
        self.loss_weight = loss_weight
        self.target = target

    def forward(self,
                feats,
                avg_factor,
                reduction_override=None,
                **kwargs):
        """Forward function.

        Args:
            cls_feats (torch.Tensor): The prediction.
            label_embeddings (torch.Tensor): The learning label of the prediction.
            weight (torch.Tensor, optional): Sample-wise loss weight.
            avg_factor (int, optional): Average factor that is used to average
                the loss. Defaults to None.
            reduction_override (str, optional): The method used to reduce the
                loss. Options are "none", "mean" and "sum".
        Returns:
            torch.Tensor: The calculated loss.
        """
        assert reduction_override in (None, 'none', 'mean', 'sum')
        reduction = (
            reduction_override if reduction_override else self.reduction)

        feats_norm = feats.norm(dim=-1)

        diff = torch.abs(feats_norm - self.target)
        loss = torch.where(diff < self.beta, diff * self.beta,
                           diff)

        loss = weight_reduce_loss(
            loss, weight=torch.ones_like(loss),
            reduction=reduction, avg_factor=avg_factor)

        return loss * self.loss_weight

# Copyright (c) OpenMMLab. All rights reserved. CrossEntropyLoss.          Args:             reduction (str, optional): . Defaults to 'mean'.                 Options are "none", "mean" and "sum".             loss_weight (float, optional): Weight of the loss. Defaults to 1.0. Forward function.          Args:             cls_feats (torch.Tensor): The prediction.             label_embeddings (torch.Tensor): The learning label of the prediction.             weight (torch.Tensor, optional): Sample-wise loss weight.             avg_factor (int, optional): Average factor that is used to average                 the loss. Defaults to None.             reduction_override (str, optional): The method used to reduce the                 loss. Options are "none", "mean" and "sum".         Returns:             torch.Tensor: The calculated loss.