ciou_loss.py 2.69 KB
Newer Older
Abhijit Deo's avatar
Abhijit Deo committed
1
2
3
import torch

from ..utils import _log_api_usage_once
Aditya Oke's avatar
Aditya Oke committed
4
5
from ._utils import _upcast_non_float
from .diou_loss import _diou_iou_loss
Abhijit Deo's avatar
Abhijit Deo committed
6
7
8
9
10
11
12
13
14
15
16


def complete_box_iou_loss(
    boxes1: torch.Tensor,
    boxes2: torch.Tensor,
    reduction: str = "none",
    eps: float = 1e-7,
) -> torch.Tensor:

    """
    Gradient-friendly IoU loss with an additional penalty that is non-zero when the
17
18
    boxes do not overlap. This loss function considers important geometrical
    factors such as overlap area, normalized central point distance and aspect ratio.
Abhijit Deo's avatar
Abhijit Deo committed
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
    This loss is symmetric, so the boxes1 and boxes2 arguments are interchangeable.

    Both sets of boxes are expected to be in ``(x1, y1, x2, y2)`` format with
    ``0 <= x1 < x2`` and ``0 <= y1 < y2``, and The two boxes should have the
    same dimensions.

    Args:
        boxes1 : (Tensor[N, 4] or Tensor[4]) first set of boxes
        boxes2 : (Tensor[N, 4] or Tensor[4]) second set of boxes
        reduction : (string, optional) Specifies the reduction to apply to the output:
            ``'none'`` | ``'mean'`` | ``'sum'``. ``'none'``: No reduction will be
            applied to the output. ``'mean'``: The output will be averaged.
            ``'sum'``: The output will be summed. Default: ``'none'``
        eps : (float): small number to prevent division by zero. Default: 1e-7

Aditya Oke's avatar
Aditya Oke committed
34
35
    Returns:
        Tensor: Loss tensor with the reduction option applied.
Abhijit Deo's avatar
Abhijit Deo committed
36

Aditya Oke's avatar
Aditya Oke committed
37
    Reference:
38
        Zhaohui Zheng et al.: Complete Intersection over Union Loss:
Aditya Oke's avatar
Aditya Oke committed
39
        https://arxiv.org/abs/1911.08287
Abhijit Deo's avatar
Abhijit Deo committed
40
41
42

    """

Aditya Oke's avatar
Aditya Oke committed
43
    # Original Implementation from https://github.com/facebookresearch/detectron2/blob/main/detectron2/layers/losses.py
Abhijit Deo's avatar
Abhijit Deo committed
44
45
46
47

    if not torch.jit.is_scripting() and not torch.jit.is_tracing():
        _log_api_usage_once(complete_box_iou_loss)

Aditya Oke's avatar
Aditya Oke committed
48
49
50
51
    boxes1 = _upcast_non_float(boxes1)
    boxes2 = _upcast_non_float(boxes2)

    diou_loss, iou = _diou_iou_loss(boxes1, boxes2)
Abhijit Deo's avatar
Abhijit Deo committed
52
53
54
55
56
57
58
59
60

    x1, y1, x2, y2 = boxes1.unbind(dim=-1)
    x1g, y1g, x2g, y2g = boxes2.unbind(dim=-1)

    # width and height of boxes
    w_pred = x2 - x1
    h_pred = y2 - y1
    w_gt = x2g - x1g
    h_gt = y2g - y1g
61
    v = (4 / (torch.pi**2)) * torch.pow((torch.atan(w_gt / h_gt) - torch.atan(w_pred / h_pred)), 2)
Abhijit Deo's avatar
Abhijit Deo committed
62
63
64
    with torch.no_grad():
        alpha = v / (1 - iou + v + eps)

Aditya Oke's avatar
Aditya Oke committed
65
    loss = diou_loss + alpha * v
66
67
68
69
70

    # Check reduction option and return loss accordingly
    if reduction == "none":
        pass
    elif reduction == "mean":
Abhijit Deo's avatar
Abhijit Deo committed
71
72
73
        loss = loss.mean() if loss.numel() > 0 else 0.0 * loss.sum()
    elif reduction == "sum":
        loss = loss.sum()
74
75
76
77
    else:
        raise ValueError(
            f"Invalid Value for arg 'reduction': '{reduction} \n Supported reduction modes: 'none', 'mean', 'sum'"
        )
Abhijit Deo's avatar
Abhijit Deo committed
78
    return loss