-
Notifications
You must be signed in to change notification settings - Fork 0
/
losses.py
232 lines (190 loc) · 8.4 KB
/
losses.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
# import torch
import paddle
# import torch.nn.functional as F
import paddle.nn.functional as F
import numpy as np
# import torch.nn as nn
import paddle.nn as nn
def cross_entropy(input, target, weight=None, reduction='mean',ignore_index=255):
"""
logSoftmax_with_loss
:param input: torch.Tensor, N*C*H*W
:param target: torch.Tensor, N*1*H*W,/ N*H*W
:param weight: torch.Tensor, C
:return: torch.Tensor [0]
"""
target = target.astype('int64')
if len(target.shape) == 4:
target = paddle.squeeze(target, axis=1)
if input.shape[-1] != target.shape[-1]:
input = F.interpolate(input, size=target.shape[1:], mode='bilinear',align_corners=True)
return F.cross_entropy(input.transpose((0,2,3,1)), target, weight=weight,
ignore_index=ignore_index, reduction=reduction)
#Focal Loss
def get_alpha(supervised_loader):
# get number of classes
num_labels = 0
for batch in supervised_loader:
label_batch = batch['L']
label_batch.data[label_batch.data==255] = 0 # pixels of ignore class added to background
l_unique = paddle.unique(label_batch.data)
list_unique = [element.item() for element in l_unique.flatten()]
num_labels = max(max(list_unique),num_labels)
num_classes = num_labels + 1
# count class occurrences
alpha = [0 for i in range(num_classes)]
for batch in supervised_loader:
label_batch = batch['L']
label_batch.data[label_batch.data==255] = 0 # pixels of ignore class added to background
l_unique = paddle.unique(label_batch.data)
list_unique = [element.item() for element in l_unique.flatten()]
l_unique_count = paddle.stack([(label_batch.data==x_u).sum() for x_u in l_unique]) # tensor([65920, 36480])
list_count = [count.item() for count in l_unique_count.flatten()]
for index in list_unique:
alpha[index] += list_count[list_unique.index(index)]
return alpha
# for FocalLoss
def softmax_helper(x):
# copy from: https://github.com/MIC-DKFZ/nnUNet/blob/master/nnunet/utilities/nd_softmax.py
rpt = [1 for _ in range(len(x.size()))]
rpt[1] = x.size(1)
x_max = x.max(1, keepdim=True)[0].repeat(*rpt)
e_x = paddle.exp(x - x_max)
return e_x / e_x.sum(1, keepdim=True).repeat(*rpt)
class FocalLoss(nn.Layer):
"""
copy from: https://github.com/Hsuxu/Loss_ToolBox-PyTorch/blob/master/FocalLoss/FocalLoss.py
This is a implementation of Focal Loss with smooth label cross entropy supported which is proposed in
'Focal Loss for Dense Object Detection. (https://arxiv.org/abs/1708.02002)'
Focal_Loss= -1*alpha*(1-pt)*log(pt)
:param num_class:
:param alpha: (tensor) 3D or 4D the scalar factor for this criterion
:param gamma: (float,double) gamma > 0 reduces the relative loss for well-classified examples (p>0.5) putting more
focus on hard misclassified example
:param smooth: (float,double) smooth value when cross entropy
:param balance_index: (int) balance class index, should be specific when alpha is float
:param size_average: (bool, optional) By default, the losses are averaged over each loss element in the batch.
"""
def __init__(self, apply_nonlin=None, alpha=None, gamma=1, balance_index=0, smooth=1e-5, size_average=True):
super(FocalLoss, self).__init__()
self.apply_nonlin = apply_nonlin
self.alpha = alpha
self.gamma = gamma
self.balance_index = balance_index
self.smooth = smooth
self.size_average = size_average
if self.smooth is not None:
if self.smooth < 0 or self.smooth > 1.0:
raise ValueError('smooth value should be in [0,1]')
def forward(self, logit, target):
if self.apply_nonlin is not None:
logit = self.apply_nonlin(logit)
num_class = logit.shape[1]
if logit.dim() > 2:
# N,C,d1,d2 -> N,C,m (m=d1*d2*...)
logit = logit.view(logit.size(0), logit.size(1), -1)
logit = logit.permute(0, 2, 1).contiguous()
logit = logit.view(-1, logit.size(-1))
target = paddle.squeeze(target, 1)
target = target.view(-1, 1)
alpha = self.alpha
if alpha is None:
alpha = paddle.ones(num_class, 1)
elif isinstance(alpha, (list, np.ndarray)):
assert len(alpha) == num_class
alpha = paddle.to_tensor(alpha,dtype=float).view(num_class, 1)
alpha = alpha / alpha.sum()
alpha = 1/alpha # inverse of class frequency
elif isinstance(alpha, float):
alpha = paddle.ones(num_class, 1)
alpha = alpha * (1 - self.alpha)
alpha[self.balance_index] = self.alpha
else:
raise TypeError('Not support alpha type')
if alpha.device != logit.device:
alpha = alpha.to(logit.device)
idx = target.cpu().long()
one_hot_key = paddle.to_tensor(target.size(0), num_class,dtype=float).zero_()
# to resolve error in idx in scatter_
idx[idx==225]=0
one_hot_key = one_hot_key.scatter_(1, idx, 1)
if one_hot_key.device != logit.device:
one_hot_key = one_hot_key.to(logit.device)
if self.smooth:
one_hot_key = paddle.clip(
one_hot_key, self.smooth/(num_class-1), 1.0 - self.smooth)
pt = (one_hot_key * logit).sum(1) + self.smooth
logpt = pt.log()
gamma = self.gamma
alpha = alpha[idx]
alpha = paddle.squeeze(alpha)
loss = -1 * alpha * paddle.pow((1 - pt), gamma) * logpt
if self.size_average:
loss = loss.mean()
else:
loss = loss.sum()
return loss
#miou loss
# from torch.autograd import Variable
def to_one_hot_var(tensor, nClasses, requires_grad=False):
n, h, w = paddle.squeeze(tensor, dim=1).size()
one_hot = tensor.new(n, nClasses, h, w).fill_(0)
one_hot = one_hot.scatter_(1, tensor.type(paddle.int64).view(n, 1, h, w), 1)
return one_hot
class mIoULoss(nn.Layer):
def __init__(self, weight=None, size_average=True, n_classes=2):
super(mIoULoss, self).__init__()
self.classes = n_classes
self.weights = weight
def forward(self, inputs, target, is_target_variable=False):
# inputs => N x Classes x H x W
# target => N x H x W
# target_oneHot => N x Classes x H x W
N = inputs.size()[0]
if is_target_variable:
target_oneHot = to_one_hot_var(target.data, self.classes).float()
else:
target_oneHot = to_one_hot_var(target, self.classes).float()
# predicted probabilities for each pixel along channel
inputs = F.softmax(inputs, dim=1)
# Numerator Product
inter = inputs * target_oneHot
## Sum over all pixels N x C x H x W => N x C
inter = inter.view(N, self.classes, -1).sum(2)
# Denominator
union = inputs + target_oneHot - (inputs * target_oneHot)
## Sum over all pixels N x C x H x W => N x C
union = union.view(N, self.classes, -1).sum(2)
loss = (self.weights * inter) / (union + 1e-8)
## Return average loss over classes and batch
return -paddle.mean(loss)
#Minimax iou
class mmIoULoss(nn.Layer):
def __init__(self, n_classes=2):
super(mmIoULoss, self).__init__()
self.classes = n_classes
def forward(self, inputs, target, is_target_variable=False):
# inputs => N x Classes x H x W
# target => N x H x W
# target_oneHot => N x Classes x H x W
N = inputs.size()[0]
if is_target_variable:
target_oneHot = to_one_hot_var(target.data, self.classes).float()
else:
target_oneHot = to_one_hot_var(target, self.classes).float()
# predicted probabilities for each pixel along channel
inputs = F.softmax(inputs, dim=1)
# Numerator Product
inter = inputs * target_oneHot
## Sum over all pixels N x C x H x W => N x C
inter = inter.view(N, self.classes, -1).sum(2)
# Denominator
union = inputs + target_oneHot - (inputs * target_oneHot)
## Sum over all pixels N x C x H x W => N x C
union = union.view(N, self.classes, -1).sum(2)
iou = inter/ (union + 1e-8)
#minimum iou of two classes
min_iou = paddle.min(iou)
#loss
loss = -min_iou-paddle.mean(iou)
return loss