-
Notifications
You must be signed in to change notification settings - Fork 8
/
loss.py
executable file
·195 lines (134 loc) · 5.83 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
import torch
import torch.nn as nn
import torch.nn.functional as F
class LossWrapper(nn.modules.loss._Loss):
def __init__(self, loss, input_index, target_index, size_average=None, reduce=None, reduction='elementwise_mean'):
super(LossWrapper, self).__init__(size_average, reduce, reduction)
self.loss = loss
self.target_index = target_index
self.input_index = input_index
def forward(self, input, target):
return self.loss(input[self.input_index], target[self.target_index])
class MSE_Loss(nn.Module):
def __init__(self, index = 0, priority=1):
super(MSE_Loss, self).__init__()
self.index = index
self.priority=priority
def forward(self, x, y):
assert (x[self.index].shape == y[self.index].shape)
pred = x[self.index].view(-1)
gt = y[self.index].view(-1)
loss = (pred - gt)**2
return torch.mean(loss)*self.priority
class MSE_Loss_masked(nn.Module):
def __init__(self, index_mse = 1, index_mask=0, priority=1):
super(MSE_Loss_masked, self).__init__()
self.index_mse = index_mse
self.index_mask = index_mask
self.priority=priority
def forward(self, x, y):
#print(x[self.index_mse].shape, y[self.index_mse].shape)
assert (x[self.index_mse].shape == y[self.index_mse].shape)
pred = x[self.index_mse]
gt = y[self.index_mse]
mask = torch.unsqueeze((torch.sum(y[self.index_mask],dim=(2,3)) > 0).float(),dim=3)[:,:2]
loss = mask*(pred - gt)**2
return (torch.mean(loss)/(torch.sum(mask)+1e-8))*self.priority
class CE_Loss(nn.Module):
def __init__(self, index = 0):
super(CE_Loss, self).__init__()
self.index = index
def forward(self, x, y):
assert (x[self.index].shape == y[self.index].shape)
loss = y[self.index]*torch.log(x[self.index]+1e-6)
return -torch.mean(loss)
class BCE_Loss(nn.Module):
def __init__(self, index = 0, bg_weight=1):
super(BCE_Loss, self).__init__()
self.label_index = index
self.bg_weight=bg_weight
def forward(self, x, y):
assert (x[self.label_index].shape == y[self.label_index].shape)
pred = x[self.label_index]
gt = y[self.label_index]
loss = gt*torch.log(pred+1e-6) + \
self.bg_weight*(1. - gt)* torch.log((1.+1e-6) - pred)
return -torch.mean(loss)
class Dice1D(nn.Module):
def __init__(self, label_index = 0):
super(Dice1D, self).__init__()
self.label_index = label_index
def forward(self, x, y):
assert (x[self.label_index].shape == y[self.label_index].shape)
shape = x[self.label_index].shape
x = x[self.label_index].view(shape[0], shape[1], -1)
y = y[self.label_index].view(shape[0], shape[1], -1)
intersection = (x * y).sum(dim=(0,2)) + 1
union = (x + y).sum(dim=(0,2)) + 2
return 1.0-2.0*torch.mean(intersection / union)
class Dice_loss_joint(nn.Module):
def __init__(self, index = 0, priority = 1):
super(Dice_loss_joint, self).__init__()
self.index = index
self.priority = priority
def forward(self, x, y):
#print(x[self.index].shape, y[self.index].shape)
assert (x[self.index].shape == y[self.index].shape)
N, C, H, W, D = x[self.index].shape
pred = x[self.index].view(N, C, -1)
gt = y[self.index].view(N, C, -1)
intersection = (pred*gt).sum(dim=(0,2)) + 1e-6
union = (pred**2 + gt).sum(dim=(0,2)) + 2e-6
dice = 2.0*intersection / union
#exp_loss = torch.pow(-torch.log(dice),0.3)
#print(dice.cpu().detach())
return self.priority*(1.0 - torch.mean(dice))
class GDL_joint(nn.Module):
def __init__(self, index=0, priority=1):
super(GDL_joint, self).__init__()
self.index = index
self.priority = priority
def forward(self, x, y):
# print(x[self.index].shape, y[self.index].shape)
assert (x[self.index].shape == y[self.index].shape)
N, C, H, W, D = x[self.index].shape
pred = x[self.index].view(N, C, -1)[:,1:]
gt = y[self.index].view(N, C, -1)[:,1:]
w = 1 / gt.sum(dim=(0,2))
intersection = w*((pred * gt).sum(dim=(0, 2)) + 1)
union = w*((pred**2 + gt).sum(dim=(0, 2)) + 1)
dice = 2.0 * intersection.sum() / union.sum()
# exp_loss = torch.pow(-torch.log(dice),0.3)
# print(dice.cpu().detach())
return self.priority * (1.0 - torch.mean(dice))
class sens_loss_joint(nn.Module):
def __init__(self, index=0, priority=1):
super(sens_loss_joint, self).__init__()
self.index = index
self.priority = priority
def forward(self, x, y):
# print(x[self.index].shape, y[self.index].shape)
assert (x[self.index].shape == y[self.index].shape)
pred = x[self.index]
gt = y[self.index]
intersection = (pred * gt).sum(dim=(0, 2, 3, 4)) + 1
union = gt.sum(dim=(0, 2, 3, 4)) + 1
loss = intersection / union
# exp_loss = torch.pow(-torch.log(dice),0.3)
# print(dice.cpu().detach())
return self.priority * (1.0 - torch.mean(loss))
class Dice_loss_separate(nn.Module):
def __init__(self, index = 0, priority = 1):
super(Dice_loss_separate, self).__init__()
self.index = index
self.priority = priority
def forward(self, x, y):
#print(x[self.index].shape, y[self.index].shape)
assert (x[self.index].shape == y[self.index].shape)
N, C, H, W, D = x[self.index].shape
pred = x[self.index].view(N, C, -1)[:,1:]
gt = y[self.index].view(N, C, -1)[:,1:]
intersection = (pred*gt).sum(dim=2)
union = (pred**2 + gt).sum(dim=2)
dice = (2.0*intersection+1) / (union+1)
return 1.0-torch.mean(dice)