-
Notifications
You must be signed in to change notification settings - Fork 0
/
losses.py
256 lines (218 loc) · 9.24 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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
# -*- coding: utf-8 -*-
import torch
import torch.nn as nn
import torch.functional as f
import torch.nn.functional as F
import numpy as np
from torch.autograd import Variable
import torch
from torch.autograd import Function
from itertools import repeat
import numpy as np
class FocalBinaryTverskyLoss(Function):
def __init__(ctx, alpha=0.5, beta=0.5, gamma=1.0, reduction='mean'):
"""
:param alpha: controls the penalty for false positives.
:param beta: penalty for false negative.
:param gamma : focal coefficient range[1,3]
:param reduction: return mode
Notes:
alpha = beta = 0.5 => dice coeff
alpha = beta = 1 => tanimoto coeff
alpha + beta = 1 => F beta coeff
add focal index -> loss=(1-T_index)**(1/gamma)
"""
ctx.alpha = alpha
ctx.beta = beta
ctx.epsilon = 1e-6
ctx.reduction = reduction
ctx.gamma = gamma
sum = ctx.beta + ctx.alpha
if sum != 1:
ctx.beta = ctx.beta / sum
ctx.alpha = ctx.alpha / sum
# @staticmethod
def forward(ctx, input, target):
batch_size = input.size(0)
_, input_label = input.max(1)
input_label = input_label.float()
target_label = target.float()
ctx.save_for_backward(input, target_label)
input_label = input_label.view(batch_size, -1)
target_label = target_label.view(batch_size, -1)
ctx.P_G = torch.sum(input_label * target_label, 1) # TP
ctx.P_NG = torch.sum(input_label * (1 - target_label), 1) # FP
ctx.NP_G = torch.sum((1 - input_label) * target_label, 1) # FN
index = ctx.P_G / (ctx.P_G + ctx.alpha * ctx.P_NG + ctx.beta * ctx.NP_G + ctx.epsilon)
loss = torch.pow((1 - index), 1 / ctx.gamma)
# target_area = torch.sum(target_label, 1)
# loss[target_area == 0] = 0
if ctx.reduction == 'none':
loss = loss
elif ctx.reduction == 'sum':
loss = torch.sum(loss)
else:
loss = torch.mean(loss)
return loss
# @staticmethod
def backward(ctx, grad_out):
"""
:param ctx:
:param grad_out:
:return:
d_loss/dT_loss=(1/gamma)*(T_loss)**(1/gamma-1)
(dT_loss/d_P1) = 2*P_G*[G*(P_G+alpha*P_NG+beta*NP_G)-(G+alpha*NG)]/[(P_G+alpha*P_NG+beta*NP_G)**2]
= 2*P_G
(dT_loss/d_p0)=
"""
inputs, target = ctx.saved_tensors
inputs = inputs.float()
target = target.float()
batch_size = inputs.size(0)
sum = ctx.P_G + ctx.alpha * ctx.P_NG + ctx.beta * ctx.NP_G + ctx.epsilon
P_G = ctx.P_G.view(batch_size, 1, 1, 1, 1)
if inputs.dim() == 5:
sum = sum.view(batch_size, 1, 1, 1, 1)
elif inputs.dim() == 4:
sum = sum.view(batch_size, 1, 1, 1)
P_G = ctx.P_G.view(batch_size, 1, 1, 1)
sub = (ctx.alpha * (1 - target) + target) * P_G
dL_dT = (1 / ctx.gamma) * torch.pow((P_G / sum), (1 / ctx.gamma - 1))
dT_dp0 = -2 * (target / sum - sub / sum / sum)
dL_dp0 = dL_dT * dT_dp0
dT_dp1 = ctx.beta * (1 - target) * P_G / sum / sum
dL_dp1 = dL_dT * dT_dp1
grad_input = torch.cat((dL_dp1, dL_dp0), dim=1)
# grad_input = torch.cat((grad_out.item() * dL_dp0, dL_dp0 * grad_out.item()), dim=1)
return grad_input, None
class FocalLoss(nn.Module):
"""
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, num_class, alpha=None, gamma=2, balance_index=-1, smooth=None, size_average=True):
super(FocalLoss, self).__init__()
self.num_class = num_class
self.alpha = alpha
self.gamma = gamma
self.smooth = smooth
self.size_average = size_average
if self.alpha is None:
self.alpha = torch.ones(self.num_class, 1)
elif isinstance(self.alpha, (list, np.ndarray)):
assert len(self.alpha) == self.num_class
self.alpha = torch.FloatTensor(alpha).view(self.num_class, 1)
self.alpha = self.alpha / self.alpha.sum()
elif isinstance(self.alpha, float):
alpha = torch.ones(self.num_class, 1)
alpha = alpha * (1 - self.alpha)
alpha[balance_index] = self.alpha
self.alpha = alpha
else:
raise TypeError('Not support alpha type')
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):
# logit = F.softmax(input, dim=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 = target.view(-1, 1)
# N = input.size(0)
# alpha = torch.ones(N, self.num_class)
# alpha = alpha * (1 - self.alpha)
# alpha = alpha.scatter_(1, target.long(), self.alpha)
epsilon = 1e-10
alpha = self.alpha
# if alpha.device != input.device:
# alpha = alpha.to(input.device)
idx = target.cpu().long()
one_hot_key = torch.FloatTensor(target.size(0), self.num_class).zero_()
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 = torch.clamp(
one_hot_key, self.smooth/(self.num_class-1), 1.0 - self.smooth)
pt = (one_hot_key * logit).sum(1) + epsilon
logpt = pt.log()
gamma = self.gamma
alpha = alpha[idx]
# import pdb
# pdb.set_trace()
# pt = pt.cpu()
# logpt = logpt.cpu()
loss = -1 * alpha.cuda() * torch.pow((1 - pt), gamma) * logpt
if self.size_average:
loss = loss.mean()
else:
loss = loss.sum()
return loss
def dice_loss(input, target):
smooth = 1.
iflat = input.view(-1)
tflat = target.view(-1).float()
intersection = (iflat * tflat).sum()
return 1 - ((2. * intersection + smooth) /
(iflat.sum() + tflat.sum() + smooth))
class GeneralizedDiceLoss(nn.Module):
"""Computes Generalized Dice Loss (GDL) as described in https://arxiv.org/pdf/1707.03237.pdf
"""
def __init__(self, epsilon=1e-5, weight=None, ignore_index=None, sigmoid_normalization=False):
super(GeneralizedDiceLoss, self).__init__()
self.epsilon = epsilon
self.register_buffer('weight', weight)
self.ignore_index = ignore_index
if sigmoid_normalization:
self.normalization = nn.Sigmoid()
else:
self.normalization = nn.Softmax(dim=1)
def forward(self, input, target):
# get probabilities from logits
input = input.float()
input = self.normalization(input)
assert input.size() == target.size(), "'input' and 'target' must have the same shape"
# mask ignore_index if present
if self.ignore_index is not None:
mask = target.clone().ne_(self.ignore_index)
mask.requires_grad = False
input = input * mask
target = target * mask
input = flatten_mod(input)
target = flatten_mod(target)
target = target.float()
target_sum = target.sum(-1)
class_weights = Variable(1. / (target_sum * target_sum).clamp(min=self.epsilon), requires_grad=False)
intersect = (input * target).sum(-1) * class_weights
if self.weight is not None:
weight = Variable(self.weight, requires_grad=False)
intersect = weight * intersect
intersect = intersect.sum()
denominator = ((input + target).sum(-1) * class_weights).sum()
return 1. - 2. * intersect / denominator.clamp(min=self.epsilon)
def flatten_mod(tensor):
out = tensor.permute(0, 2, 3, 4, 1).contiguous().view(tensor.size()[1],-1)#class is hardcoded. please avoid
return out
def flatten(tensor):
"""Flattens a given tensor such that the channel axis is first.
The shapes are transformed as follows:
(N, C, D, H, W) -> (C, N * D * H * W)
"""
C = tensor.size(1)
# new axis order
axis_order = (1, 0) + tuple(range(2, tensor.dim()))
# Transpose: (N, C, D, H, W) -> (C, N, D, H, W)
transposed = tensor.permute(axis_order)
# Flatten: (C, N, D, H, W) -> (C, N * D * H * W)
return transposed.view(C, -1)