-
Notifications
You must be signed in to change notification settings - Fork 3
/
iou_loss.py
235 lines (193 loc) · 9.12 KB
/
iou_loss.py
1
2
3
4
5
6
7
8
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
import math
import torch
from torch import nn
"""
https://zhuanlan.zhihu.com/p/270663039
IOU Loss: 考虑了重叠面积,归一化坐标尺度;
GIOU Loss: 考虑了重叠面积,基于IOU解决边界框不相交时loss等于0的问题;
DIOU Loss: 考虑了重叠面积和中心点距离,基于IOU解决GIOU收敛慢的问题;
CIOU Loss: 考虑了重叠面积、中心点距离、纵横比,基于DIOU提升回归精确度;
EIOU Loss: 考虑了重叠面积,中心点距离、长宽边长真实差,基于CIOU解决了纵横比的模糊定义,并添加Focal Loss解决BBox回归中的样本不平衡问题。
"""
class IoULoss(nn.Module):
"""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 (torch.Tensor): Predicted bboxes of format (x1, y1, x2, y2),
shape (n, 4).
target (torch.Tensor): Corresponding gt bboxes, shape (n, 4).
eps (float): Eps to avoid log(0).
Return:
torch.Tensor: Loss tensor.
"""
def __init__(self, eps=1e-9) -> None:
super().__init__()
self.eps = eps
def forward(self, pred, target):
pred_w,pred_h = pred[..., 2] - pred[..., 0], pred[..., 3] - pred[..., 1]
target_w,target_h = target[..., 2] - target[..., 0], target[..., 3] - target[..., 1]
pred_area = pred_w * pred_h
target_area = target_w * target_h
inter_lt = torch.max(pred[..., :2], target[..., :2])
inter_rb = torch.min(pred[..., 2:], target[..., 2:])
inter_wh = (inter_rb - inter_lt).clamp(min=0)
inter_area = inter_wh[..., 0] * inter_wh[..., 1]
ious = inter_area / (pred_area + target_area - inter_area + self.eps)
loss = 1-ious
return loss
class GIoULoss(nn.Module):
"""GIoU loss.
Computing the GIoU loss between a set of predicted bboxes and target bboxes.
Args:
pred (torch.Tensor): Predicted bboxes of format (x1, y1, x2, y2),
shape (n, 4).
target (torch.Tensor): Corresponding gt bboxes, shape (n, 4).
eps (float): Eps to avoid log(0).
Return:
torch.Tensor: Loss tensor.
"""
def __init__(self, eps=1e-9) -> None:
super().__init__()
self.eps = eps
def forward(self, pred, target):
pred_w,pred_h = pred[..., 2] - pred[..., 0], pred[..., 3] - pred[..., 1]
target_w,target_h = target[..., 2] - target[..., 0], target[..., 3] - target[..., 1]
pred_area = pred_w * pred_h
target_area = target_w * target_h
union_area = pred_area + target_area
inter_lt = torch.max(pred[..., :2], target[..., :2])
inter_rb = torch.min(pred[..., 2:], target[..., 2:])
inter_wh = (inter_rb - inter_lt).clamp(min=0)
inter_area = inter_wh[..., 0] * inter_wh[..., 1]
ious = inter_area / (union_area - inter_area + self.eps)
loss = (1-ious).mean()
enclosed_lt = torch.min(pred[..., :2], target[..., :2])
enclosed_rb = torch.max(pred[..., 2:], target[..., 2:])
enclose_wh = (enclosed_rb - enclosed_lt).clamp(min=0)
enclose_area = enclose_wh[..., 0] * enclose_wh[..., 1] + self.eps
# GIoU
gious = ious - (enclose_area - union_area) / enclose_area
loss = 1 - gious
return loss
class DIoULoss(nn.Module):
"""CIoU loss.
Computing the DIoU loss between a set of predicted bboxes and target bboxes.
Args:
pred (torch.Tensor): Predicted bboxes of format (x1, y1, x2, y2),
shape (n, 4).
target (torch.Tensor): Corresponding gt bboxes, shape (n, 4).
eps (float): Eps to avoid log(0).
Return:
torch.Tensor: Loss tensor.
"""
def __init__(self, eps=1e-9) -> None:
super().__init__()
self.eps = eps
def forward(self, pred, target):
pred_w,pred_h = pred[..., 2] - pred[..., 0], pred[..., 3] - pred[..., 1]
target_w,target_h = target[..., 2] - target[..., 0], target[..., 3] - target[..., 1]
pred_area = pred_w * pred_h
target_area = target_w * target_h
inter_lt = torch.max(pred[..., :2], target[..., :2])
inter_rb = torch.min(pred[..., 2:], target[..., 2:])
inter_wh = (inter_rb - inter_lt).clamp(min=0)
inter_area = inter_wh[..., 0] * inter_wh[..., 1]
ious = inter_area / (pred_area + target_area - inter_area + self.eps)
center_pred = (pred[..., :2] + pred[..., 2:]) / 2
center_target = (target[..., :2] + target[..., 2:]) / 2
dist1 = torch.sum((center_pred - center_target) ** 2, dim=-1)
enclosed_lt = torch.min(pred[..., :2], target[..., :2])
enclosed_rb = torch.max(pred[..., 2:], target[..., 2:])
dist2 = torch.sum((enclosed_rb - enclosed_lt) ** 2, dim=-1) + self.eps
# DIoU
dious = ious - dist1 / dist2
loss = (1-dious).mean()
return loss
class CIoULoss(nn.Module):
"""CIoU loss.
Computing the CIoU loss between a set of predicted bboxes and target bboxes.
Args:
pred (torch.Tensor): Predicted bboxes of format (x1, y1, x2, y2),
shape (n, 4).
target (torch.Tensor): Corresponding gt bboxes, shape (n, 4).
eps (float): Eps to avoid log(0).
Return:
torch.Tensor: Loss tensor.
"""
def __init__(self, eps=1e-9) -> None:
super().__init__()
self.eps = eps
def forward(self, pred, target):
pred_w,pred_h = pred[..., 2] - pred[..., 0], pred[..., 3] - pred[..., 1]
target_w,target_h = target[..., 2] - target[..., 0], target[..., 3] - target[..., 1]
pred_area = pred_w * pred_h
target_area = target_w * target_h
inter_lt = torch.max(pred[..., :2], target[..., :2])
inter_rb = torch.min(pred[..., 2:], target[..., 2:])
inter_wh = (inter_rb - inter_lt).clamp(min=0)
inter_area = inter_wh[..., 0] * inter_wh[..., 1]
ious = inter_area / (pred_area + target_area - inter_area + self.eps)
center_pred = (pred[..., :2] + pred[..., 2:]) / 2
center_target = (target[..., :2] + target[..., 2:]) / 2
dist1 = torch.sum((center_pred - center_target) ** 2, dim=-1)
enclosed_lt = torch.min(pred[..., :2], target[..., :2])
enclosed_rb = torch.max(pred[..., 2:], target[..., 2:])
dist2 = torch.sum((enclosed_rb - enclosed_lt) ** 2, dim=-1) + self.eps
factor = 4 / math.pi**2
v = factor * torch.pow(torch.atan(target_w / (target_h+self.eps)) - torch.atan(pred_w / (pred_h+self.eps)), 2)
with torch.no_grad():
alpha = (ious > 0.5).float() * v / (1 - ious + v)
# CIoU
cious = ious - (dist1 / dist2 + alpha * v)
loss = 1-cious
return loss
class EIoULoss(nn.Module):
"""EIoU loss.
Computing the EIoU loss between a set of predicted bboxes and target bboxes.
Args:
pred (torch.Tensor): Predicted bboxes of format (x1, y1, x2, y2),
shape (n, 4).
target (torch.Tensor): Corresponding gt bboxes, shape (n, 4).
eps (float): Eps to avoid log(0).
Return:
torch.Tensor: Loss tensor.
"""
def __init__(self, eps=1e-9) -> None:
super().__init__()
self.eps = eps
def forward(self, pred, target):
pred_w,pred_h = pred[..., 2] - pred[..., 0], pred[..., 3] - pred[..., 1]
target_w,target_h = target[..., 2] - target[..., 0], target[..., 3] - target[..., 1]
pred_area = pred_w * pred_h
target_area = target_w * target_h
inter_lt = torch.max(pred[..., :2], target[..., :2])
inter_rb = torch.min(pred[..., 2:], target[..., 2:])
inter_wh = (inter_rb - inter_lt).clamp(min=0)
inter_area = inter_wh[..., 0] * inter_wh[..., 1]
ious = inter_area / (pred_area + target_area - inter_area + self.eps)
center_pred = (pred[..., :2] + pred[..., 2:]) / 2
center_target = (target[..., :2] + target[..., 2:]) / 2
rho_bbox = torch.sum((center_pred - center_target) ** 2, dim=-1)
enclosed_lt = torch.min(pred[..., :2], target[..., :2])
enclosed_rb = torch.max(pred[..., 2:], target[..., 2:])
c_bbox = torch.sum((enclosed_rb - enclosed_lt) ** 2, dim=-1) + self.eps
rho_w = (pred_w - target_w) ** 2
c_w = (enclosed_rb[..., 0] - enclosed_lt[..., 0]) ** 2 + self.eps
rho_h = (pred_h - target_h) ** 2
c_h = (enclosed_rb[..., 1] - enclosed_lt[..., 1]) ** 2 + self.eps
# EIoU
eious = ious - rho_bbox / c_bbox - rho_w/c_w - rho_h/c_h
loss = 1-eious
return loss
if __name__ == "__main__":
pred_bboxes = torch.tensor([[20, 30, 80, 90, 0.7],
[50, 50, 140, 210, 0.6],
[20, 30, 70, 100, 0.8],
[200, 200, 400, 400, 0.6]])
gt_bboxes = torch.tensor([[40, 30, 100, 90, 0.7],
[50, 50, 140, 210, 0.6],
[80, 120, 170, 200, 0.8],
[250, 250, 350, 350, 0.6]])
for loss in [IoULoss(), GIoULoss(), CIoULoss(), DIoULoss(), EIoULoss()]:
print(loss(pred_bboxes[:, :4], gt_bboxes[:, :4]))