-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlosses.py
397 lines (303 loc) · 11.4 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
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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
import torch
import torch.nn.functional as F
from torch.autograd import Variable
import numpy as np
from math import exp
import math
import torch.nn as nn
def gaussian(window_size, sigma):
gauss = torch.Tensor(
[
exp(-((x - window_size // 2) ** 2) / float(2 * sigma**2))
for x in range(window_size)
]
)
return gauss / gauss.sum()
def create_window(window_size, channel):
_1D_window = gaussian(window_size, 1.5).unsqueeze(1)
_2D_window = _1D_window.mm(_1D_window.t()).float().unsqueeze(0).unsqueeze(0)
window = Variable(
_2D_window.expand(channel, 1, window_size, window_size).contiguous()
)
return window
def create_window_3D(window_size, channel):
_1D_window = gaussian(window_size, 1.5).unsqueeze(1)
_2D_window = _1D_window.mm(_1D_window.t())
_3D_window = (
_1D_window.mm(_2D_window.reshape(1, -1))
.reshape(window_size, window_size, window_size)
.float()
.unsqueeze(0)
.unsqueeze(0)
)
window = Variable(
_3D_window.expand(
channel, 1, window_size, window_size, window_size
).contiguous()
)
return window
def _ssim(img1, img2, window, window_size, channel, size_average=True):
mu1 = F.conv2d(img1, window, padding=window_size // 2, groups=channel)
mu2 = F.conv2d(img2, window, padding=window_size // 2, groups=channel)
mu1_sq = mu1.pow(2)
mu2_sq = mu2.pow(2)
mu1_mu2 = mu1 * mu2
sigma1_sq = (
F.conv2d(img1 * img1, window, padding=window_size // 2, groups=channel) - mu1_sq
)
sigma2_sq = (
F.conv2d(img2 * img2, window, padding=window_size // 2, groups=channel) - mu2_sq
)
sigma12 = (
F.conv2d(img1 * img2, window, padding=window_size // 2, groups=channel)
- mu1_mu2
)
C1 = 0.01**2
C2 = 0.03**2
ssim_map = ((2 * mu1_mu2 + C1) * (2 * sigma12 + C2)) / (
(mu1_sq + mu2_sq + C1) * (sigma1_sq + sigma2_sq + C2)
)
if size_average:
return ssim_map.mean()
else:
return ssim_map.mean(1).mean(1).mean(1)
def _ssim_3D(img1, img2, window, window_size, channel, size_average=True):
mu1 = F.conv3d(img1, window, padding=window_size // 2, groups=channel)
mu2 = F.conv3d(img2, window, padding=window_size // 2, groups=channel)
mu1_sq = mu1.pow(2)
mu2_sq = mu2.pow(2)
mu1_mu2 = mu1 * mu2
sigma1_sq = (
F.conv3d(img1 * img1, window, padding=window_size // 2, groups=channel) - mu1_sq
)
sigma2_sq = (
F.conv3d(img2 * img2, window, padding=window_size // 2, groups=channel) - mu2_sq
)
sigma12 = (
F.conv3d(img1 * img2, window, padding=window_size // 2, groups=channel)
- mu1_mu2
)
C1 = 0.01**2
C2 = 0.03**2
ssim_map = ((2 * mu1_mu2 + C1) * (2 * sigma12 + C2)) / (
(mu1_sq + mu2_sq + C1) * (sigma1_sq + sigma2_sq + C2)
)
if size_average:
return ssim_map.mean()
else:
return ssim_map.mean(1).mean(1).mean(1)
class MSE(torch.nn.Module):
def __init__(self):
super(MSE, self).__init__()
def forward(self, img1, img2):
return torch.mean((img1 - img2) ** 2)
class L1(torch.nn.Module):
def __init__(self):
super(L1, self).__init__()
def forward(self, img1, img2):
return torch.mean(torch.abs(img1 - img2))
class L1_norm(torch.nn.Module):
def __init__(self):
super(L1_norm, self).__init__()
def forward(self, img1):
return torch.mean(torch.abs(img1))
class SSIM(torch.nn.Module):
def __init__(self, window_size=11, size_average=True):
super(SSIM, self).__init__()
self.window_size = window_size
self.size_average = size_average
self.channel = 1
self.window = create_window(window_size, self.channel)
def forward(self, img1, img2):
(_, channel, _, _) = img1.size()
if channel == self.channel and self.window.data.type() == img1.data.type():
window = self.window
else:
window = create_window(self.window_size, channel)
if img1.is_cuda:
window = window.cuda(img1.get_device())
window = window.type_as(img1)
self.window = window
self.channel = channel
return _ssim(img1, img2, window, self.window_size, channel, self.size_average)
class SSIM3D(torch.nn.Module):
def __init__(self, window_size=11, size_average=True):
super(SSIM3D, self).__init__()
self.window_size = window_size
self.size_average = size_average
self.channel = 1
self.window = create_window_3D(window_size, self.channel)
def forward(self, img1, img2):
(_, channel, _, _, _) = img1.size()
if channel == self.channel and self.window.data.type() == img1.data.type():
window = self.window
else:
window = create_window_3D(self.window_size, channel)
if img1.is_cuda:
window = window.cuda(img1.get_device())
window = window.type_as(img1)
self.window = window
self.channel = channel
return 1 - _ssim_3D(
img1, img2, window, self.window_size, channel, self.size_average
)
def ssim(img1, img2, window_size=11, size_average=True):
(_, channel, _, _) = img1.size()
window = create_window(window_size, channel)
if img1.is_cuda:
window = window.cuda(img1.get_device())
window = window.type_as(img1)
return _ssim(img1, img2, window, window_size, channel, size_average)
def ssim3D(img1, img2, window_size=11, size_average=True):
(_, channel, _, _, _) = img1.size()
window = create_window_3D(window_size, channel)
if img1.is_cuda:
window = window.cuda(img1.get_device())
window = window.type_as(img1)
return _ssim_3D(img1, img2, window, window_size, channel, size_average)
class Grad(torch.nn.Module):
"""
N-D gradient loss.
"""
def __init__(self, penalty="l1", loss_mult=None):
super(Grad, self).__init__()
self.penalty = penalty
self.loss_mult = loss_mult
def forward(self, y_pred, y_true):
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
class Grad3d(torch.nn.Module):
"""
N-D gradient loss.
"""
def __init__(self, penalty="l1", loss_mult=None):
super(Grad3d, self).__init__()
self.penalty = penalty
self.loss_mult = loss_mult
def forward(self, y_pred, y_true):
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 DiceLoss(nn.Module):
"""Dice and Xentropy loss"""
def __init__(self, num_class=36):
super().__init__()
self.num_class = num_class
def forward(self, y_pred, y_true):
# y_pred = torch.round(y_pred)
# y_pred = nn.functional.one_hot(torch.round(y_pred).long(), num_classes=7)
# y_pred = torch.squeeze(y_pred, 1)
# y_pred = y_pred.permute(0, 4, 1, 2, 3).contiguous()
y_true = nn.functional.one_hot(y_true, num_classes=self.num_class)
y_true = torch.squeeze(y_true, 1)
y_true = y_true.permute(0, 4, 1, 2, 3).contiguous()
intersection = y_pred * y_true
intersection = intersection.sum(dim=[2, 3, 4])
union = torch.pow(y_pred, 2).sum(dim=[2, 3, 4]) + torch.pow(y_true, 2).sum(
dim=[2, 3, 4]
)
dsc = (2.0 * intersection) / (union + 1e-5)
dsc = 1 - torch.mean(dsc)
return dsc
class NCC(torch.nn.Module):
"""
Local (over window) normalized cross correlation loss.
"""
def __init__(self, win=9, gpu=True):
super(NCC, self).__init__()
self.win = win
if gpu:
self.device = "cuda:0"
else:
self.device = "cpu"
def forward(self, y_true, y_pred):
Ii = y_true
Ji = y_pred
# get dimension of volume
# assumes Ii, Ji are sized [batch_size, *vol_shape, nb_feats]
ndims = len(list(Ii.size())) - 2
assert ndims in [1, 2, 3], (
"volumes should be 1 to 3 dimensions. found: %d" % ndims
)
# set window size
win = [self.win] * ndims
# compute filters
sum_filt = torch.ones([1, 1, *win]).to(self.device)
pad_no = math.floor(win[0] / 2)
if ndims == 1:
stride = 1
padding = pad_no
elif ndims == 2:
stride = (1, 1)
padding = (pad_no, pad_no)
else:
stride = (1, 1, 1)
padding = (pad_no, pad_no, pad_no)
# get convolution function
conv_fn = getattr(F, "conv%dd" % ndims)
# compute CC squares
I2 = Ii * Ii
J2 = Ji * Ji
IJ = Ii * Ji
I_sum = conv_fn(Ii, sum_filt, stride=stride, padding=padding)
J_sum = conv_fn(Ji, sum_filt, stride=stride, padding=padding)
I2_sum = conv_fn(I2, sum_filt, stride=stride, padding=padding)
J2_sum = conv_fn(J2, sum_filt, stride=stride, padding=padding)
IJ_sum = conv_fn(IJ, sum_filt, stride=stride, padding=padding)
win_size = np.prod(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 + 1e-5)
return -torch.mean(cc)
def CharbonnierLoss(predict, target, eps=1e-3):
return torch.mean(torch.sqrt((predict - target) ** 2 + eps**2))
class CensusLoss(nn.Module):
def __init__(self, patch_size=7):
super(CensusLoss, self).__init__()
patch_size = patch_size
out_channels = patch_size * patch_size * patch_size
self.w = np.eye(out_channels).reshape(
(patch_size, patch_size, patch_size, 1, out_channels)
)
self.w = np.transpose(self.w, (4, 3, 2, 0, 1))
self.w = torch.tensor(self.w).float().to("cuda:0")
def transform(self, img):
patches = F.conv3d(img, self.w, padding=3, bias=None)
transf = patches - img
transf_norm = transf / torch.sqrt(0.81 + transf**2)
return transf_norm
def hamming(self, t1, t2):
dist = (t1 - t2) ** 2
dist_norm = torch.mean(dist / (0.1 + dist), 1, True)
return dist_norm
def valid_mask(self, t, padding):
n, _, d, h, w = t.size()
inner = torch.ones(
n, 1, d - 2 * padding, h - 2 * padding, w - 2 * padding
).type_as(t)
mask = F.pad(inner, [padding] * 6)
return mask
def forward(self, img0, img1):
img0 = self.transform(img0)
img1 = self.transform(img1)
return (self.hamming(img0, img1) * self.valid_mask(img0, 1)).mean()