-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlosses.py
184 lines (136 loc) · 4.9 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
import torch
import torch.nn.functional as F
import numpy as np
import math
def l1_sim(x, y):
return torch.mean((x - y))
def gradient_loss(s, penalty='l2'):
dy = torch.abs(s[:, :, 1:, :, :] - s[:, :, :-1, :, :])
dx = torch.abs(s[:, :, :, 1:, :] - s[:, :, :, :-1, :])
dz = torch.abs(s[:, :, :, :, 1:] - s[:, :, :, :, :-1])
if (penalty == 'l2'):
dy = dy * dy
dx = dx * dx
dz = dz * dz
d = torch.mean(dx) + torch.mean(dy) + torch.mean(dz)
return d / 3.0
class NCC(torch.nn.Module):
"""
local (over window) normalized cross correlation
"""
def __init__(self, win=5, eps=1e-5):
super(NCC, self).__init__()
self.win_raw = win
self.eps = eps
self.win = win
def forward(self, I, J):
ndims = 3
win_size = self.win_raw
self.win = [self.win_raw] * ndims
weight_win_size = self.win_raw
weight = torch.ones((1, 1, weight_win_size, weight_win_size, weight_win_size), device=I.device,
requires_grad=False)
conv_fn = F.conv3d
I2 = I * I
J2 = J * J
IJ = I * J
I_sum = conv_fn(I, weight, padding=int(win_size / 2))
J_sum = conv_fn(J, weight, padding=int(win_size / 2))
I2_sum = conv_fn(I2, weight, padding=int(win_size / 2))
J2_sum = conv_fn(J2, weight, padding=int(win_size / 2))
IJ_sum = conv_fn(IJ, weight, padding=int(win_size / 2))
win_size = np.prod(self.win)
u_I = I_sum / win_size
u_J = J_sum / win_size
cross = IJ_sum - u_J * I_sum - u_I * J_sum + u_I * u_J * win_size
I_var = I2_sum - 2 * u_I * I_sum + u_I * u_I * win_size
J_var = J2_sum - 2 * u_J * J_sum + u_J * u_J * win_size
cc = cross * cross / (I_var * J_var + self.eps)
return -1.0 * torch.mean(cc)
class NCC_2d(torch.nn.Module):
"""
local (over window) normalized cross correlation
"""
def __init__(self, win=5, eps=1e-5):
super(NCC_2d, self).__init__()
self.win_raw = win
self.eps = eps
self.win = win
def forward(self, I, J):
ndims = 2
win_size = self.win_raw
self.win = [self.win_raw] * ndims
weight_win_size = self.win_raw
weight = torch.ones((1, 1, weight_win_size, weight_win_size), device=I.device, requires_grad=False)
conv_fn = F.conv2d
I2 = I * I
J2 = J * J
IJ = I * J
I_sum = conv_fn(I, weight, padding=int(win_size / 2))
J_sum = conv_fn(J, weight, padding=int(win_size / 2))
I2_sum = conv_fn(I2, weight, padding=int(win_size / 2))
J2_sum = conv_fn(J2, weight, padding=int(win_size / 2))
IJ_sum = conv_fn(IJ, weight, padding=int(win_size / 2))
win_size = np.prod(self.win)
u_I = I_sum / win_size
u_J = J_sum / win_size
cross = IJ_sum - u_J * I_sum - u_I * J_sum + u_I * u_J * win_size
I_var = I2_sum - 2 * u_I * I_sum + u_I * u_I * win_size
J_var = J2_sum - 2 * u_J * J_sum + u_J * u_J * win_size
cc = cross * cross / (I_var * J_var + self.eps)
return -1.0 * torch.mean(cc)
class MSE:
"""
Mean squared error loss.
"""
def loss(self, y_true, y_pred):
return torch.mean((y_true - y_pred) ** 2)
class Dice:
"""
N-D dice for segmentation
"""
def loss(self, y_true, y_pred):
ndims = len(list(y_pred.size())) - 2
vol_axes = list(range(2, ndims + 2))
top = 2 * (y_true * y_pred).sum(dim=vol_axes)
bottom = torch.clamp((y_true + y_pred).sum(dim=vol_axes), min=1e-5)
dice = torch.mean(top / bottom)
return -dice
class Grad:
"""
N-D gradient loss.
"""
def __init__(self, penalty='l2', loss_mult=None):
self.penalty = penalty
self.loss_mult = loss_mult
def loss(self, y_pred):
dy = torch.abs(y_pred[:, :, 1:, :, :] - y_pred[:, :, :-1, :, :])
dx = torch.abs(y_pred[:, :, :, 1:, :] - y_pred[:, :, :, :-1, :])
dz = torch.abs(y_pred[:, :, :, :, 1:] - y_pred[:, :, :, :, :-1])
if self.penalty == 'l2':
dy = dy * dy
dx = dx * dx
dz = dz * dz
d = torch.mean(dx) + torch.mean(dy) + torch.mean(dz)
grad = d / 3.0
if self.loss_mult is not None:
grad *= self.loss_mult
return grad
class Grad_2d:
"""
N-D gradient loss.
"""
def __init__(self, penalty='l2', loss_mult=None):
self.penalty = penalty
self.loss_mult = loss_mult
def loss(self, y_pred):
dy = torch.abs(y_pred[:, :, 1:, :] - y_pred[:, :, :-1, :])
dx = torch.abs(y_pred[:, :, :, 1:] - y_pred[:, :, :, :-1])
if self.penalty == 'l2':
dy = dy * dy
dx = dx * dx
d = torch.mean(dx) + torch.mean(dy)
grad = d / 2.0
if self.loss_mult is not None:
grad *= self.loss_mult
return grad