[9f60b7]: / 2DNet / src / tuils / loss_function.py

Download this file

345 lines (294 with data), 12.9 kB

  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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
import torch.nn.functional as F
import torch.nn as nn
import numpy as np
import torch
from torch.autograd import Variable
class FocalLoss(nn.Module):
def __init__(self, gamma=2):
super().__init__()
self.gamma = gamma
def forward(self, logit, target):
target = target.float()
max_val = (-logit).clamp(min=0)
loss = logit - logit * target + max_val + \
((-max_val).exp() + (-logit - max_val).exp()).log()
invprobs = F.logsigmoid(-logit * (target * 2.0 - 1.0))
loss = (invprobs * self.gamma).exp() * loss
if len(loss.size())==2:
loss = loss.sum(dim=1)
return loss.mean()
class BinaryEntropyLoss_weight(nn.Module):
def __init__(self, weight=None, size_average=True, is_weight=True):
super(BinaryEntropyLoss_weight, self).__init__()
self.weight = weight
self.size_average = size_average
self.is_weight = is_weight
self.class_num = np.array([[2.0, 1.0, 1.0, 1.0, 1.0, 1.0]])
# self.class_num = np.power((1-self.class_num/50000), 2)
# print(target.shape)
def forward(self, input, target):
self.weight = torch.cuda.FloatTensor(self.class_num.repeat(target.shape[0], axis=0))
loss = F.binary_cross_entropy(input, target, self.weight, self.size_average)
return loss
class FocalLoss_BCE(nn.Module):
def __init__(self, gamma=2.0, alpha=None, size_average=True):
super(FocalLoss_BCE, self).__init__()
self.gamma = gamma
self.alpha = alpha
if isinstance(alpha,(float,int)): self.alpha = torch.Tensor([alpha,1-alpha])
if isinstance(alpha,list): self.alpha = torch.Tensor(alpha)
self.size_average = size_average
def forward(self, input, target):
if input.dim()>2:
input = input.view(input.size(0),input.size(1),-1) # N,C,H,W => N,C,H*W
input = input.transpose(1,2) # N,C,H*W => N,H*W,C
input = input.contiguous().view(-1,input.size(2)) # N,H*W,C => N*H*W,C
target = target.view(-1)
# pt = torch.sigmoid(input)
pt = input
pt = pt.view(-1)
error = torch.abs(pt - target)
log_error = torch.log(error)
loss = -1 * (1-error)**self.gamma * log_error
if self.size_average: return loss.mean()
else: return loss.sum()
class BinaryEntropyLoss_weight_v2(nn.Module):
def __init__(self, weight=None, size_average=True, is_weight=True):
super(BinaryEntropyLoss_weight_v2, self).__init__()
self.weight = weight
self.size_average = size_average
self.is_weight = is_weight
def forward(self, input, target):
if self.is_weight:
total_pixel = target.numel()
weights_list = []
for i in range(2):
if target[target==i].numel() == 0:
weights_list.append(0)
else:
weights_list.append(target[target!=i].numel()/total_pixel)
weights_list = np.clip(weights_list, 0.2, 0.8)
self.weight = target.clone()
self.weight[self.weight==0] = weights_list[0]
self.weight[self.weight==1] = weights_list[1]
# self.weight = torch.FloatTensor(self.weight).cuda()
# loss_f = nn.BCELoss()
loss = F.binary_cross_entropy(F.sigmoid(input), target, self.weight,self.size_average)
# loss = F.binary_cross_entropy_with_logits(input, target, self.weight, reduce=False)
# value, index= loss.topk(int(target.shape[1] * OHEM_percent), dim=1, largest=True, sorted=True)
# return value.mean()
return loss
class BinaryEntropyLoss_weight_v2_topk(nn.Module):
def __init__(self, weight=None, size_average=True, is_weight=True):
super(BinaryEntropyLoss_weight_v2_topk, self).__init__()
self.weight = weight
self.size_average = size_average
self.is_weight = is_weight
self.OHEM_percent = 0.1
def forward(self, input, target):
if self.is_weight:
total_pixel = target.numel()
# print(target.shape, total_pixel)
weights_list = []
for i in range(2):
if target[target==i].numel() == 0:
weights_list.append(0)
else:
weights_list.append(target[target!=i].numel()/total_pixel)
weights_list = np.clip(weights_list, 0.2, 0.8)
self.weight = target.clone()
self.weight[self.weight==0] = weights_list[0]
self.weight[self.weight==1] = weights_list[1]
# self.weight = torch.FloatTensor(self.weight).cuda()
# loss = F.binary_cross_entropy(F.sigmoid(input), target, self.weight,self.size_average)
loss = F.binary_cross_entropy_with_logits(input, target, self.weight, reduce=False)
loss = loss.view(loss.size(0), -1)
value, index= loss.topk(int(target.shape[1] * target.shape[2] * self.OHEM_percent), dim=1, largest=True, sorted=True)
return value.mean()
# return loss
class SoftDiceLoss_binary_v2(nn.Module):
def __init__(self):
super(SoftDiceLoss_binary_v2, self).__init__()
def forward(self, input, target):
smooth = 0.01
batch_size = input.size(0)
input = F.sigmoid(input).view(batch_size, -1)
# print(target.shape)
# print(target.view(-1))
target = target.clone().view(batch_size, -1)
input_b = 1 - input
target_b = 1 - target
inter_f = torch.sum(input * target, 1) + smooth
union_f = torch.sum(input * input, 1) + torch.sum(target * target, 1) + smooth
score_f = torch.sum(2.0 * inter_f / union_f) / float(batch_size)
inter_b = torch.sum(input_b * target_b, 1) + smooth
union_b = torch.sum(input_b * input_b, 1) + torch.sum(target_b * target_b, 1) + smooth
score_b = torch.sum(2.0 * inter_b / union_b) / float(batch_size)
score = 1 - score_f - score_b
# weight_f = score_b/(score_f + score_b)
# weight_b = score_f/(score_f + score_b)
# # score = 1.0 - torch.clamp(score_f, 0.0, 1.0 - 1e-7) - torch.clamp(score_b, 0.0, 1.0 - 1e-7)
# score = 1 - weight_f * score_f - weight_b * score_b
# inter_f = torch.sum(input * target, 1) + smooth
# union_f = torch.sum(input * input, 1) + torch.sum(target * target, 1) + smooth
# score_f = inter_f / union_f
# inter_b = torch.sum(input_b * target_b, 1) + smooth
# union_b = torch.sum(input_b * input_b, 1) + torch.sum(target_b * target_b, 1) + smooth
# score_b = inter_b / union_b
# score = 1 - torch.sum(score_f*score_b) / float(batch_size)
return score
class SoftDiceLoss_binary_v3(nn.Module):
def __init__(self):
super(SoftDiceLoss_binary_v3, self).__init__()
def forward(self, input, target):
smooth = 0.01
batch_size = input.size(0)
input = F.sigmoid(input).view(batch_size, -1)
# print(target.shape)
# print(target.view(-1))
target = target.clone().view(batch_size, -1)
input_b = 1 - input
target_b = 1 - target
inter_f = torch.sum(input * target, 1) + smooth
union_f = torch.sum(input * input, 1) + torch.sum(target * target, 1) + smooth
score_f = torch.sum(2.0 * inter_f / union_f) / float(batch_size)
inter_b = torch.sum(input_b * target_b, 1) + smooth
union_b = torch.sum(input_b * input_b, 1) + torch.sum(target_b * target_b, 1) + smooth
score_b = torch.sum(2.0 * inter_b / union_b) / float(batch_size)
# score = 1 - score_f - score_b
weight_f = score_b/(score_f + score_b)
weight_b = score_f/(score_f + score_b)
# score = 1.0 - torch.clamp(score_f, 0.0, 1.0 - 1e-7) - torch.clamp(score_b, 0.0, 1.0 - 1e-7)
score = 1 - weight_f * score_f - weight_b * score_b
# inter_f = torch.sum(input * target, 1) + smooth
# union_f = torch.sum(input * input, 1) + torch.sum(target * target, 1) + smooth
# score_f = inter_f / union_f
# inter_b = torch.sum(input_b * target_b, 1) + smooth
# union_b = torch.sum(input_b * input_b, 1) + torch.sum(target_b * target_b, 1) + smooth
# score_b = inter_b / union_b
# score = 1 - torch.sum(score_f*score_b) / float(batch_size)
return score
class SoftDiceLoss_binary(nn.Module):
def __init__(self):
super(SoftDiceLoss_binary, self).__init__()
def forward(self, input, target):
smooth = 0.01
batch_size = input.size(0)
input = F.sigmoid(input).view(batch_size, -1)
# print(target.shape)
# print(target.view(-1))
target = target.clone().view(batch_size, -1)
inter = torch.sum(input * target, 1) + smooth
union = torch.sum(input * input, 1) + torch.sum(target * target, 1) + smooth
score = torch.sum(2.0 * inter / union) / float(batch_size)
score = 1.0 - torch.clamp(score, 0.0, 1.0 - 1e-7)
return score
class SoftDiceLoss(nn.Module):
def __init__(self):
super(SoftDiceLoss, self).__init__()
def forward(self, logits, targets):
smooth = 1
num = targets.size(0)
probs = F.sigmoid(logits)
m1 = probs.view(num, -1)
m2 = targets.view(num, -1)
intersection = (m1 * m2)
score = 2. * (intersection.sum(1) + smooth) / (m1.sum(1) + m2.sum(1) + smooth)
score = 1 - score.sum() / num
return score
"""
Lovasz-Softmax and Jaccard hinge loss in PyTorch
Maxim Berman 2018 ESAT-PSI KU Leuven (MIT License)
"""
import torch
from torch.autograd import Variable
import torch.nn.functional as F
import numpy as np
try:
from itertools import ifilterfalse
except ImportError: # py3k
from itertools import filterfalse
def lovasz_grad(gt_sorted):
"""
Computes gradient of the Lovasz extension w.r.t sorted errors
See Alg. 1 in paper
"""
p = len(gt_sorted)
gts = gt_sorted.sum()
intersection = gts - gt_sorted.float().cumsum(0)
union = gts + (1 - gt_sorted).float().cumsum(0)
jaccard = 1. - intersection / union
if p > 1: # cover 1-pixel case
jaccard[1:p] = jaccard[1:p] - jaccard[0:-1]
return jaccard
def lovasz_hinge(logits, labels, per_image=True, ignore=None):
"""
Binary Lovasz hinge loss
logits: [B, H, W] Variable, logits at each pixel (between -\infty and +\infty)
labels: [B, H, W] Tensor, binary ground truth masks (0 or 1)
per_image: compute the loss per image instead of per batch
ignore: void class id
"""
if per_image:
loss = mean(lovasz_hinge_flat(*flatten_binary_scores(log.unsqueeze(0), lab.unsqueeze(0), ignore))
for log, lab in zip(logits, labels))
else:
loss = lovasz_hinge_flat(*flatten_binary_scores(logits, labels, ignore))
return loss
def lovasz_hinge_flat(logits, labels):
"""
Binary Lovasz hinge loss
logits: [P] Variable, logits at each prediction (between -\infty and +\infty)
labels: [P] Tensor, binary ground truth labels (0 or 1)
ignore: label to ignore
"""
if len(labels) == 0:
# only void pixels, the gradients should be 0
return logits.sum() * 0.
signs = 2. * labels.float() - 1.
errors = (1. - logits * Variable(signs))
errors_sorted, perm = torch.sort(errors, dim=0, descending=True)
perm = perm.data
gt_sorted = labels[perm]
grad = lovasz_grad(gt_sorted)
# loss = torch.dot(F.elu(errors_sorted)+1, Variable(grad))
loss = torch.dot(F.relu(errors_sorted), Variable(grad))
return loss
def flatten_binary_scores(scores, labels, ignore=None):
"""
Flattens predictions in the batch (binary case)
Remove labels equal to 'ignore'
"""
scores = scores.view(-1)
labels = labels.view(-1)
if ignore is None:
return scores, labels
valid = (labels != ignore)
vscores = scores[valid]
vlabels = labels[valid]
return vscores, vlabels
def mean(l, ignore_nan=False, empty=0):
"""
nanmean compatible with generators.
"""
l = iter(l)
if ignore_nan:
l = ifilterfalse(np.isnan, l)
try:
n = 1
acc = next(l)
except StopIteration:
if empty == 'raise':
raise ValueError('Empty mean')
return empty
for n, v in enumerate(l, 2):
acc += v
if n == 1:
return acc
return acc / n
class SymmetricLovaszLoss(nn.Module):
def __init__(self, weight=None, size_average=True):
super(SymmetricLovaszLoss, self).__init__()
def forward(self, logits, targets):
return ((lovasz_hinge(logits, targets, per_image=True)) \
+ (lovasz_hinge(-logits, 1-targets, per_image=True))) / 2