-
Notifications
You must be signed in to change notification settings - Fork 12
/
loss_function.py
216 lines (181 loc) · 7.42 KB
/
loss_function.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
from torch import nn
import torch
import torch.nn.functional as F
from torch.autograd import Variable
class BCEFocalLoss(torch.nn.Module):
def __init__(self, gamma=2, alpha=0.8, reduction='elementwise_mean'):
super().__init__()
self.gamma = gamma
self.alpha = alpha
self.reduction = reduction
def forward(self, _input, target, epsilon = 1e-6,):
pt = torch.sigmoid(_input)
loss = - self.alpha * (1 - pt) ** self.gamma * target * torch.log(pt + epsilon) - \
pt ** self.gamma * (1 - target) * torch.log(1 - pt +epsilon) * (1 - self.alpha)
#if self.alpha:
# loss = loss * self.alpha
if self.reduction == 'elementwise_mean':
loss = torch.mean(loss)
elif self.reduction == 'sum':
loss = torch.sum(loss)
return loss
class weightbceloss(torch.nn.Module):
def __init__(self, gamma=2, alpha=1, reduction='elementwise_mean'):
super().__init__()
self.gamma = gamma
self.alpha = alpha
self.reduction = reduction
def forward(self, _input, target, epsilon = 1e-6,):
pt = torch.sigmoid(_input)
loss = - self.alpha * target * torch.log(pt + epsilon) - \
(1 - target) * torch.log(1 - pt +epsilon) * (1 - self.alpha)
#if self.alpha:
# loss = loss * self.alpha
if self.reduction == 'elementwise_mean':
loss = torch.mean(loss)
elif self.reduction == 'sum':
loss = torch.sum(loss)
return loss
class weightbceloss1(torch.nn.Module):
def __init__(self, gamma=2, alpha=1, reduction='elementwise_mean'):
super().__init__()
self.gamma = gamma
self.alpha = alpha
self.reduction = reduction
def forward(self, outputs_unlabel_unaug, outputs_unlabel, epsilon = 1e-6,):
y1 = torch.sigmoid(outputs_unlabel_unaug)
y2 = torch.sigmoid(outputs_unlabel)
# loss = - self.alpha * (pt - target) * torch.log(1 - target + epsilon)
loss = - (1 - (y1 - y2)) * torch.log(1- (y1 - y2)+epsilon)
#if self.alpha:
# loss = loss * self.alpha
if self.reduction == 'elementwise_mean':
loss = torch.mean(loss)
elif self.reduction == 'sum':
loss = torch.sum(loss)
return loss
class MSE_Loss(nn.Module):
def __init__(self):
super(MSE_Loss, self).__init__()
self.criterion = nn.MSELoss()
def forward(self, model_output, targets):
#targets[targets == 0] = -1
# torch.empty(3, dtype=torch.long)
# model_output = model_output.long()
# targets = targets.long()
# print(model_output)
# print(F.sigmoid(model_output))
# print(targets)
# print('kkk')
# model_output =torch.LongTensor(model_output.cpu())
# targets =torch.LongTensor(targets.cpu())
# model_output = model_output.type(torch.LongTensor)
# targets = targets.type(torch.LongTensor)
loss = self.criterion(model_output, targets)
return loss
def cross_entropy_3D(input, target, weight=None, size_average=True):
n, c, h, w, s = input.size()
log_p = F.log_softmax(input, dim=1)
log_p = log_p.transpose(1, 2).transpose(2, 3).transpose(3, 4).contiguous().view(-1, c)
target = target.view(target.numel())
loss = F.nll_loss(log_p, target, weight=weight, size_average=False)
if size_average:
loss /= float(target.numel())
return loss
class Binary_Loss(nn.Module):
def __init__(self):
super(Binary_Loss, self).__init__()
self.criterion = nn.BCEWithLogitsLoss()
def forward(self, model_output, targets):
#targets[targets == 0] = -1
# torch.empty(3, dtype=torch.long)
# model_output = model_output.long()
# targets = targets.long()
# print(model_output)
# print(F.sigmoid(model_output))
# print(targets)
# print('kkk')
# model_output =torch.LongTensor(model_output.cpu())
# targets =torch.LongTensor(targets.cpu())
# model_output = model_output.type(torch.LongTensor)
# targets = targets.type(torch.LongTensor)
loss = self.criterion(model_output, targets)
return loss
def make_one_hot(input, num_classes):
"""Convert class index tensor to one hot encoding tensor.
Args:
input: A tensor of shape [N, 1, *]
num_classes: An int of number of class
Returns:
A tensor of shape [N, num_classes, *]
"""
shape = np.array(input.shape)
shape[1] = num_classes
shape = tuple(shape)
result = torch.zeros(shape)
result = result.scatter_(1, input.cpu(), 1)
return result
class BinaryDiceLoss(nn.Module):
"""Dice loss of binary class
Args:
smooth: A float number to smooth loss, and avoid NaN error, default: 1
p: Denominator value: \sum{x^p} + \sum{y^p}, default: 2
predict: A tensor of shape [N, *]
target: A tensor of shape same with predict
reduction: Reduction method to apply, return mean over batch if 'mean',
return sum if 'sum', return a tensor of shape [N,] if 'none'
Returns:
Loss tensor according to arg reduction
Raise:
Exception if unexpected reduction
"""
def __init__(self, smooth=1, p=2, reduction='mean'):
super(BinaryDiceLoss, self).__init__()
self.smooth = smooth
self.p = p
self.reduction = reduction
def forward(self, predict, target):
assert predict.shape[0] == target.shape[0], "predict & target batch size don't match"
predict = predict.contiguous().view(predict.shape[0], -1)
target = target.contiguous().view(target.shape[0], -1)
num = torch.sum(torch.mul(predict, target), dim=1) + self.smooth
den = torch.sum(predict.pow(self.p) + target.pow(self.p), dim=1) + self.smooth
loss = 1 - num / den
if self.reduction == 'mean':
return loss.mean()
elif self.reduction == 'sum':
return loss.sum()
elif self.reduction == 'none':
return loss
else:
raise Exception('Unexpected reduction {}'.format(self.reduction))
class DiceLoss(nn.Module):
"""Dice loss, need one hot encode input
Args:
weight: An array of shape [num_classes,]
ignore_index: class index to ignore
predict: A tensor of shape [N, C, *]
target: A tensor of same shape with predict
other args pass to BinaryDiceLoss
Return:
same as BinaryDiceLoss
"""
def __init__(self, weight=None, ignore_index=None, **kwargs):
super(DiceLoss, self).__init__()
self.kwargs = kwargs
self.weight = weight
self.ignore_index = ignore_index
def forward(self, predict, target):
assert predict.shape == target.shape, 'predict & target shape do not match'
dice = BinaryDiceLoss(**self.kwargs)
total_loss = 0
predict = F.softmax(predict, dim=1)
for i in range(target.shape[1]):
if i != self.ignore_index:
dice_loss = dice(predict[:, i], target[:, i])
if self.weight is not None:
assert self.weight.shape[0] == target.shape[1], \
'Expect weight shape [{}], get[{}]'.format(target.shape[1], self.weight.shape[0])
dice_loss *= self.weights[i]
total_loss += dice_loss
return total_loss/target.shape[1]