axis_aligned_iou_loss.py 2.94 KB
Newer Older
dingchang's avatar
dingchang committed
1
# Copyright (c) OpenMMLab. All rights reserved.
2
3
from typing import Optional

4
import torch
5
from mmdet.models.losses.utils import weighted_loss
6
from torch import Tensor
7
8
from torch import nn as nn

9
from mmdet3d.registry import MODELS
zhangshilong's avatar
zhangshilong committed
10
from mmdet3d.structures import AxisAlignedBboxOverlaps3D
11
12
13


@weighted_loss
14
def axis_aligned_iou_loss(pred: Tensor, target: Tensor) -> Tensor:
15
16
17
18
    """Calculate the IoU loss (1-IoU) of two set of axis aligned bounding
    boxes. Note that predictions and targets are one-to-one corresponded.

    Args:
19
20
        pred (Tensor): Bbox predictions with shape [..., 3].
        target (Tensor): Bbox targets (gt) with shape [..., 3].
21
22

    Returns:
23
        Tensor: IoU loss between predictions and targets.
24
25
26
27
28
29
30
31
    """

    axis_aligned_iou = AxisAlignedBboxOverlaps3D()(
        pred, target, is_aligned=True)
    iou_loss = 1 - axis_aligned_iou
    return iou_loss


32
@MODELS.register_module()
33
34
35
36
37
class AxisAlignedIoULoss(nn.Module):
    """Calculate the IoU loss (1-IoU) of axis aligned bounding boxes.

    Args:
        reduction (str): Method to reduce losses.
38
39
40
            The valid reduction method are 'none', 'sum' or 'mean'.
            Defaults to 'mean'.
        loss_weight (float): Weight of loss. Defaults to 1.0.
41
42
    """

43
44
45
    def __init__(self,
                 reduction: str = 'mean',
                 loss_weight: float = 1.0) -> None:
46
47
48
49
50
51
        super(AxisAlignedIoULoss, self).__init__()
        assert reduction in ['none', 'sum', 'mean']
        self.reduction = reduction
        self.loss_weight = loss_weight

    def forward(self,
52
53
54
55
56
57
                pred: Tensor,
                target: Tensor,
                weight: Optional[Tensor] = None,
                avg_factor: Optional[float] = None,
                reduction_override: Optional[str] = None,
                **kwargs) -> Tensor:
58
59
60
        """Forward function of loss calculation.

        Args:
61
62
63
            pred (Tensor): Bbox predictions with shape [..., 3].
            target (Tensor): Bbox targets (gt) with shape [..., 3].
            weight (Tensor, optional): Weight of loss.
64
                Defaults to None.
65
66
            avg_factor (float, optional): Average factor that is used to
                average the loss. Defaults to None.
67
68
69
70
71
            reduction_override (str, optional): Method to reduce losses.
                The valid reduction method are 'none', 'sum' or 'mean'.
                Defaults to None.

        Returns:
72
            Tensor: IoU loss between predictions and targets.
73
74
75
76
77
78
79
80
81
82
83
84
85
        """
        assert reduction_override in (None, 'none', 'mean', 'sum')
        reduction = (
            reduction_override if reduction_override else self.reduction)
        if (weight is not None) and (not torch.any(weight > 0)) and (
                reduction != 'none'):
            return (pred * weight).sum()
        return axis_aligned_iou_loss(
            pred,
            target,
            weight=weight,
            avg_factor=avg_factor,
            reduction=reduction) * self.loss_weight