-
Notifications
You must be signed in to change notification settings - Fork 0
/
loss.py
219 lines (149 loc) · 5.94 KB
/
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
import torch
import torch.nn.functional as F
from catalyst.contrib import registry
from torch import nn
import numpy as np
def IoUOneExample(pred, target):
pred_area = abs(pred[2] - pred[0])*abs(pred[3] - pred[1])
target_area = (target[2] - target[0])*(target[3] - target[1])
x_p1 = min(pred[2], pred[0])
x_p2 = max(pred[2], pred[0])
y_p1 = min(pred[1], pred[3])
y_p2 = max(pred[1], pred[3])
x1 = max(x_p1, target[0])
x2 = min(x_p2, target[2])
y1 = max(y_p1, target[1])
y2 = min(y_p2, target[3])
overlap_area = (x2 - x1) * (y2 - y1)
if overlap_area < 0:
overlap_area = 0
return 0.0
return (overlap_area / (target_area + pred_area - overlap_area))
def IoULoss(pred, target):
iou = IoUOneExample(pred, target)
return 1 - iou
def GeneralizedIoUExample(pred, target):
pred_area = abs(pred[2] - pred[0])*abs(pred[3] - pred[1])
target_area = (target[2] - target[0])*(target[3] - target[1])
x_p1 = min(pred[2], pred[0])
x_p2 = max(pred[2], pred[0])
y_p1 = min(pred[1], pred[3])
y_p2 = max(pred[1], pred[3])
x1 = max(x_p1, target[0])
x2 = min(x_p2, target[2])
y1 = max(y_p1, target[1])
y2 = min(y_p2, target[3])
overlap_area = (x2 - x1) * (y2 - y1)
if overlap_area < 0:
overlap_area = 0
x1_c = min(x_p1, target[0])
x2_c = max(x_p2, target[2])
y1_c = min(y_p1, target[1])
y2_c = max(y_p2, target[3])
enclose_area = (x2_c - x1_c) * (y2_c - y1_c)
return (overlap_area / (target_area + pred_area - overlap_area)) - ((enclose_area - (target_area + pred_area - overlap_area)) / enclose_area)
def GeneralizedIoULoss(pred, target):
gen_iou = GeneralizedIoUExample(pred, target)
return 1 - gen_iou
def EfficientIoUExample(pred, target):
pred_area = abs(pred[2] - pred[0])*abs(pred[3] - pred[1])
target_area = (target[2] - target[0])*(target[3] - target[1])
x_p1 = min(pred[2], pred[0])
x_p2 = max(pred[2], pred[0])
y_p1 = min(pred[1], pred[3])
y_p2 = max(pred[1], pred[3])
x1 = max(x_p1, target[0])
x2 = min(x_p2, target[2])
y1 = max(y_p1, target[1])
y2 = min(y_p2, target[3])
overlap_area = (x2 - x1) * (y2 - y1)
if overlap_area < 0:
overlap_area = 0
x1_c = min(x_p1, target[0])
x2_c = max(x_p2, target[2])
y1_c = min(y_p1, target[1])
y2_c = max(y_p2, target[3])
c_w = x2_c - x1_c
w = x_p2 - x_p1
w_t = target[2] - target[0]
c_h = y2_c - y1_c
h = y_p2 - y_p1
h_t = target[3] - target[1]
iou = (overlap_area / (target_area + pred_area - overlap_area))
center_pred_x = (x_p2 + x_p1)/2.0
center_pred_y = (y_p2 + y_p1)/2.0
center_target_x = (target[2] + target[0])/2.0
center_target_y = (target[3] + target[1])/2.0
diag_enclosing_box = c_w**2 + c_h**2
center_part = ((center_pred_x - center_target_x)**2 + (center_pred_y - center_target_y)**2) / diag_enclosing_box
weight_part = ((w - w_t)**2) / (c_w**2)
height_part = ((h - h_t)**2) / (h_t**2)
return iou - (center_part + weight_part + height_part)
def EfficientIoULoss(pred, target):
eff_iou = EfficientIoUExample(pred, target)
return 1 - eff_iou
def FocalEfficientIoULoss(pred, target, gamma = 0.5):
iou = IoUOneExample(pred, target)
eiou_loss = EfficientIoULoss(pred, target)
return (iou**gamma)*eiou_loss
@registry.Criterion
class IoU(nn.Module):
def __init__(self, bce_coeff = 0.2):
super(IoU, self).__init__()
self.sigmoid = nn.Sigmoid()
self.bce_coeff = bce_coeff
def forward(self, preds, target):
n = len(target)
loss_sum = 0
for i in range(n):
loss_sum += IoULoss(self.sigmoid(preds[i][1:]), target[i][1:])
bce = F.binary_cross_entropy_with_logits(preds[:, 0], target[:, 0])
return self.bce_coeff * bce + (loss_sum / n)
@registry.Criterion
class GeneralizedIoU(nn.Module):
def __init__(self, bce_coeff = 0.2):
super(GeneralizedIoU, self).__init__()
self.sigmoid = nn.Sigmoid()
self.bce_coeff = bce_coeff
def forward(self, preds, target):
n = len(target)
loss_sum = 0
for i in range(n):
loss_sum += GeneralizedIoULoss(self.sigmoid(preds[i][1:]), target[i][1:])
bce = F.binary_cross_entropy_with_logits(preds[:, 0], target[:, 0])
return self.bce_coeff * bce + (loss_sum / n)
@registry.Criterion
class EfficientIoU(nn.Module):
def __init__(self, bce_coeff = 0.2):
super(EfficientIoU, self).__init__()
self.sigmoid = nn.Sigmoid()
self.bce_coeff = bce_coeff
def forward(self, preds, target):
n = len(target)
loss_sum = 0
for i in range(n):
loss_sum += EfficientIoULoss(self.sigmoid(preds[i][1:]), target[i][1:])
bce = F.binary_cross_entropy_with_logits(preds[:, 0], target[:, 0])
return self.bce_coeff * bce + (loss_sum / n)
@registry.Criterion
class FocalEfficientIoU(nn.Module):
def __init__(self, bce_coeff = 0.2, gamma = 0.5):
super(FocalEfficientIoU, self).__init__()
self.sigmoid = nn.Sigmoid()
self.bce_coeff = bce_coeff
self.gamma = gamma
def forward(self, preds, target):
n = len(target)
loss_sum = 0
for i in range(n):
loss_sum += FocalEfficientIoULoss(self.sigmoid(preds[i][1:]), target[i][1:], self.gamma)
bce = F.binary_cross_entropy_with_logits(preds[:, 0], target[:, 0])
return self.bce_coeff * bce + (loss_sum / n)
def get_loss(name_loss, bce_coeff, gamma = 0.5):
LEVELS = {
'IoU': IoU(bce_coeff),
'GeneralizedIoU': GeneralizedIoU(bce_coeff),
'EfficientIoU': EfficientIoU(bce_coeff),
'FocalEfficientIoU': FocalEfficientIoU(bce_coeff, gamma)
}
return LEVELS[name_loss]