iou_loss.py 3.96 KB
Newer Older
Kai Chen's avatar
Kai Chen committed
1
import torch
2
3
import torch.nn as nn

Kai Chen's avatar
Kai Chen committed
4
5
from mmdet.core import bbox_overlaps
from .utils import weighted_loss
6
7
8
from ..registry import LOSSES


Kai Chen's avatar
Kai Chen committed
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
@weighted_loss
def iou_loss(pred, target, eps=1e-6):
    """IoU loss.

    Computing the IoU loss between a set of predicted bboxes and target bboxes.
    The loss is calculated as negative log of IoU.

    Args:
        pred (Tensor): Predicted bboxes of format (x1, y1, x2, y2),
            shape (n, 4).
        target (Tensor): Corresponding gt bboxes, shape (n, 4).
        eps (float): Eps to avoid log(0).

    Return:
        Tensor: Loss tensor.
    """
    ious = bbox_overlaps(pred, target, is_aligned=True).clamp(min=eps)
    loss = -ious.log()
    return loss


@weighted_loss
def bounded_iou_loss(pred, target, beta=0.2, eps=1e-3):
    """Improving Object Localization with Fitness NMS and Bounded IoU Loss,
    https://arxiv.org/abs/1711.00164.

    Args:
        pred (tensor): Predicted bboxes.
        target (tensor): Target bboxes.
        beta (float): beta parameter in smoothl1.
        eps (float): eps to avoid NaN.
    """
    pred_ctrx = (pred[:, 0] + pred[:, 2]) * 0.5
    pred_ctry = (pred[:, 1] + pred[:, 3]) * 0.5
    pred_w = pred[:, 2] - pred[:, 0] + 1
    pred_h = pred[:, 3] - pred[:, 1] + 1
    with torch.no_grad():
        target_ctrx = (target[:, 0] + target[:, 2]) * 0.5
        target_ctry = (target[:, 1] + target[:, 3]) * 0.5
        target_w = target[:, 2] - target[:, 0] + 1
        target_h = target[:, 3] - target[:, 1] + 1

    dx = target_ctrx - pred_ctrx
    dy = target_ctry - pred_ctry

    loss_dx = 1 - torch.max(
        (target_w - 2 * dx.abs()) /
        (target_w + 2 * dx.abs() + eps), torch.zeros_like(dx))
    loss_dy = 1 - torch.max(
        (target_h - 2 * dy.abs()) /
        (target_h + 2 * dy.abs() + eps), torch.zeros_like(dy))
    loss_dw = 1 - torch.min(target_w / (pred_w + eps), pred_w /
                            (target_w + eps))
    loss_dh = 1 - torch.min(target_h / (pred_h + eps), pred_h /
                            (target_h + eps))
    loss_comb = torch.stack([loss_dx, loss_dy, loss_dw, loss_dh],
                            dim=-1).view(loss_dx.size(0), -1)

    loss = torch.where(loss_comb < beta, 0.5 * loss_comb * loss_comb / beta,
                       loss_comb - 0.5 * beta)
    return loss


72
73
74
@LOSSES.register_module
class IoULoss(nn.Module):

Kai Chen's avatar
Kai Chen committed
75
    def __init__(self, eps=1e-6, reduction='mean', loss_weight=1.0):
76
        super(IoULoss, self).__init__()
Kai Chen's avatar
Kai Chen committed
77
78
79
80
        self.eps = eps
        self.reduction = reduction
        self.loss_weight = loss_weight

81
82
83
84
85
86
87
    def forward(self,
                pred,
                target,
                weight=None,
                avg_factor=None,
                reduction_override=None,
                **kwargs):
Kai Chen's avatar
Kai Chen committed
88
89
        if weight is not None and not torch.any(weight > 0):
            return (pred * weight).sum()  # 0
90
91
92
        assert reduction_override in (None, 'none', 'mean', 'sum')
        reduction = (
            reduction_override if reduction_override else self.reduction)
Kai Chen's avatar
Kai Chen committed
93
94
95
96
97
        loss = self.loss_weight * iou_loss(
            pred,
            target,
            weight,
            eps=self.eps,
98
            reduction=reduction,
Kai Chen's avatar
Kai Chen committed
99
100
101
102
103
104
105
106
107
108
            avg_factor=avg_factor,
            **kwargs)
        return loss


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

    def __init__(self, beta=0.2, eps=1e-3, reduction='mean', loss_weight=1.0):
        super(BoundedIoULoss, self).__init__()
109
110
        self.beta = beta
        self.eps = eps
Kai Chen's avatar
Kai Chen committed
111
        self.reduction = reduction
112
113
        self.loss_weight = loss_weight

Kai Chen's avatar
Kai Chen committed
114
115
116
117
    def forward(self, pred, target, weight=None, avg_factor=None, **kwargs):
        if weight is not None and not torch.any(weight > 0):
            return (pred * weight).sum()  # 0
        loss = self.loss_weight * bounded_iou_loss(
118
119
120
121
122
            pred,
            target,
            weight,
            beta=self.beta,
            eps=self.eps,
Kai Chen's avatar
Kai Chen committed
123
124
            reduction=self.reduction,
            avg_factor=avg_factor,
125
126
            **kwargs)
        return loss