-
Notifications
You must be signed in to change notification settings - Fork 0
/
losses.py
715 lines (558 loc) · 26.2 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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
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
from pytorch_msssim import ssim, ms_ssim, SSIM, MS_SSIM
class NLLLoss(nn.Module):
def __init__(self, reduction: str = 'mean'):
super(NLLLoss, self).__init__()
self.reduction = reduction
def forward(self, mean, var, target):
mu = mean
neg_logvar = var
neg_logvar = torch.clamp(neg_logvar, min=-20, max=20)
loss = torch.exp(neg_logvar) * torch.pow(target - mu, 2) - neg_logvar
if self.reduction == 'mean':
return loss.mean()
elif self.reduction == 'sum':
return loss.sum()
else:
return loss
class SSIM_loss(torch.nn.Module):
def __init__(self, if_MS=True, win_size=11):
super(SSIM_loss, self).__init__()
if if_MS:
self.SSIM = MS_SSIM(win_size=win_size, data_range=255, size_average=True, channel=1)
else:
self.SSIM = SSIM(data_range=255, size_average=True, channel=1)
def forward(self, img1, img2):
return -self.SSIM(img1, img2)
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 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 Grad_hyper(torch.nn.Module):
"""
N-D gradient loss.
"""
def __init__(self, penalty='l1'):
super(Grad_hyper, self).__init__()
self.penalty = penalty
def forward(self, y_pred, wts):
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, dim=[1, 2, 3]) + torch.mean(dy, dim=[1, 2, 3])
grad = d / 2.0 * wts
print(wts)
return torch.mean(grad)
class GradiTV(torch.nn.Module):
"""
N-D gradient loss.
"""
def __init__(self):
super(GradiTV, self).__init__()
def forward(self, y_pred, y_true):
dy = torch.abs(y_pred[:, :, 1:, 1:] - y_pred[:, :, :-1, 1:])
dx = torch.abs(y_pred[:, :, 1:, 1:] - y_pred[:, :, 1:, :-1])
dy = dy * dy
dx = dx * dx
d = torch.mean(torch.sqrt(dx+dy+1e-6))
grad = d / 3.0
return grad
class Grad3DiTV(torch.nn.Module):
"""
N-D gradient loss.
"""
def __init__(self):
super(Grad3DiTV, self).__init__()
a = 1
def forward(self, y_pred, y_true):
dy = torch.abs(y_pred[:, :, 1:, 1:, 1:] - y_pred[:, :, :-1, 1:, 1:])
dx = torch.abs(y_pred[:, :, 1:, 1:, 1:] - y_pred[:, :, 1:, :-1, 1:])
dz = torch.abs(y_pred[:, :, 1:, 1:, 1:] - y_pred[:, :, 1:, 1:, :-1])
dy = dy * dy
dx = dx * dx
dz = dz * dz
d = torch.mean(torch.sqrt(dx+dy+dz+1e-6))
grad = d / 3.0
return grad
class DisplacementRegularizer2D(torch.nn.Module):
def __init__(self, energy_type):
super().__init__()
self.energy_type = energy_type
def gradient_dx(self, fv): return (fv[:, 2:, 1:-1] - fv[:, :-2, 1:-1]) / 2
def gradient_dy(self, fv): return (fv[:, 1:-1, 2:] - fv[:, 1:-1, :-2]) / 2
def gradient_txyz(self, Txyz, fn):
return torch.stack([fn(Txyz[:,i,...]) for i in [0, 1]], dim=1)
def compute_gradient_norm(self, displacement, flag_l1=False):
dTdx = self.gradient_txyz(displacement, self.gradient_dx)
dTdy = self.gradient_txyz(displacement, self.gradient_dy)
if flag_l1:
norms = torch.abs(dTdx) + torch.abs(dTdy)
else:
norms = dTdx**2 + dTdy**2
return torch.mean(norms)/2.0
def compute_bending_energy(self, displacement):
dTdx = self.gradient_txyz(displacement, self.gradient_dx)
dTdy = self.gradient_txyz(displacement, self.gradient_dy)
dTdxx = self.gradient_txyz(dTdx, self.gradient_dx)
dTdyy = self.gradient_txyz(dTdy, self.gradient_dy)
dTdxy = self.gradient_txyz(dTdx, self.gradient_dy)
return torch.mean(dTdxx**2 + dTdyy**2 + 2*dTdxy**2)
def forward(self, disp, _):
if self.energy_type == 'bending':
energy = self.compute_bending_energy(disp)
elif self.energy_type == 'gradient-l2':
energy = self.compute_gradient_norm(disp)
elif self.energy_type == 'gradient-l1':
energy = self.compute_gradient_norm(disp, flag_l1=True)
else:
raise Exception('Not recognised local regulariser!')
return energy
class DisplacementRegularizer(torch.nn.Module):
def __init__(self, energy_type):
super().__init__()
self.energy_type = energy_type
def gradient_dx(self, fv): return (fv[:, 2:, 1:-1, 1:-1] - fv[:, :-2, 1:-1, 1:-1]) / 2
def gradient_dy(self, fv): return (fv[:, 1:-1, 2:, 1:-1] - fv[:, 1:-1, :-2, 1:-1]) / 2
def gradient_dz(self, fv): return (fv[:, 1:-1, 1:-1, 2:] - fv[:, 1:-1, 1:-1, :-2]) / 2
def gradient_txyz(self, Txyz, fn):
return torch.stack([fn(Txyz[:,i,...]) for i in [0, 1, 2]], dim=1)
def compute_gradient_norm(self, displacement, flag_l1=False):
dTdx = self.gradient_txyz(displacement, self.gradient_dx)
dTdy = self.gradient_txyz(displacement, self.gradient_dy)
dTdz = self.gradient_txyz(displacement, self.gradient_dz)
if flag_l1:
norms = torch.abs(dTdx) + torch.abs(dTdy) + torch.abs(dTdz)
else:
norms = dTdx**2 + dTdy**2 + dTdz**2
return torch.mean(norms)/3.0
def compute_bending_energy(self, displacement):
dTdx = self.gradient_txyz(displacement, self.gradient_dx)
dTdy = self.gradient_txyz(displacement, self.gradient_dy)
dTdz = self.gradient_txyz(displacement, self.gradient_dz)
dTdxx = self.gradient_txyz(dTdx, self.gradient_dx)
dTdyy = self.gradient_txyz(dTdy, self.gradient_dy)
dTdzz = self.gradient_txyz(dTdz, self.gradient_dz)
dTdxy = self.gradient_txyz(dTdx, self.gradient_dy)
dTdyz = self.gradient_txyz(dTdy, self.gradient_dz)
dTdxz = self.gradient_txyz(dTdx, self.gradient_dz)
return torch.mean(dTdxx**2 + dTdyy**2 + dTdzz**2 + 2*dTdxy**2 + 2*dTdxz**2 + 2*dTdyz**2)
def forward(self, disp, _):
if self.energy_type == 'bending':
energy = self.compute_bending_energy(disp)
elif self.energy_type == 'gradient-l2':
energy = self.compute_gradient_norm(disp)
elif self.energy_type == 'gradient-l1':
energy = self.compute_gradient_norm(disp, flag_l1=True)
else:
raise Exception('Not recognised local regulariser!')
return energy
class NCC_vxm(torch.nn.Module):
"""
Local (over window) normalized cross correlation loss.
"""
def __init__(self, win=None):
super(NCC_vxm, self).__init__()
self.win = win
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 = [9] * ndims if self.win is None else self.win
# compute filters
sum_filt = torch.ones([1, 1, *win]).to("cuda")
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)
class crossCorrelation3D(nn.Module):
def __init__(self, in_ch=1, kernel=(9, 9), voxel_weights=None):
super(crossCorrelation3D, self).__init__()
self.in_ch = in_ch
self.kernel = kernel
self.voxel_weight = voxel_weights
self.filt = (torch.ones([1, in_ch, self.kernel[0], self.kernel[1]])).cuda()
def forward(self, input, target):
II = input * input
TT = target * target
IT = input * target
pad = (int((self.kernel[0]-1)/2), int((self.kernel[1]-1)/2))
T_sum = F.conv2d(target, self.filt, stride=1, padding=pad)
I_sum = F.conv2d(input, self.filt, stride=1, padding=pad)
TT_sum = F.conv2d(TT, self.filt, stride=1, padding=pad)
II_sum = F.conv2d(II, self.filt, stride=1, padding=pad)
IT_sum = F.conv2d(IT, self.filt, stride=1, padding=pad)
kernelSize = self.kernel[0] * self.kernel[1]
Ihat = I_sum / kernelSize
That = T_sum / kernelSize
# cross = (I-Ihat)(J-Jhat)
cross = IT_sum - Ihat*T_sum - That*I_sum + That*Ihat*kernelSize
T_var = TT_sum - 2*That*T_sum + That*That*kernelSize
I_var = II_sum - 2*Ihat*I_sum + Ihat*Ihat*kernelSize
cc = cross*cross / (T_var*I_var+1e-5)
loss = -1.0 * torch.mean(cc)
return loss
class PCC(torch.nn.Module):
def __init__(self,):
super(PCC, self).__init__()
def pcc(self, y_true, y_pred):
A_bar = torch.mean(y_pred, dim=[1, 2, 3], keepdim=True)
B_bar = torch.mean(y_true, dim=[1, 2, 3], keepdim=True)
top = torch.mean((y_pred - A_bar) * (y_true - B_bar), dim=[1, 2, 3], keepdim=True)
bottom = torch.sqrt(torch.mean((y_pred - A_bar) ** 2, dim=[1, 2, 3], keepdim=True) * torch.mean((y_true - B_bar) ** 2, dim=[1, 2, 3, 4], keepdim=True))
return torch.mean(top/bottom)
def forward(self, I, J):
return (1-self.pcc(I,J))
'''
Weighted PCC + SSIM
'''
class PCC_SSIM(torch.nn.Module):
def __init__(self, pcc_wt=0.5):
super(PCC_SSIM, self).__init__()
self.pcc_wt = pcc_wt
self.ssim_wt = 1.0 - pcc_wt
self.ssim = SSIM(window_size=9)
def pcc(self, y_true, y_pred):
A_bar = torch.mean(y_pred, dim=[1, 2, 3], keepdim=True)
B_bar = torch.mean(y_true, dim=[1, 2, 3], keepdim=True)
top = torch.mean((y_pred - A_bar) * (y_true - B_bar), dim=[1, 2, 3], keepdim=True)
bottom = torch.sqrt(torch.mean((y_pred - A_bar) ** 2, dim=[1, 2, 3], keepdim=True) * torch.mean((y_true - B_bar) ** 2, dim=[1, 2, 3], keepdim=True))
return torch.mean(top/bottom)
def ssim(self, I, J):
SSIM_idx = self.ssim(I, J)
return SSIM_idx
def forward(self, I, J):
return self.pcc_wt*(1-self.pcc(I,J)) + self.ssim_wt*(self.ssim(I,J))
class MIND_loss(torch.nn.Module):
"""
Local (over window) normalized cross correlation loss.
"""
def __init__(self, win=None):
super(MIND_loss, self).__init__()
self.win = win
def pdist_squared(self, x):
xx = (x ** 2).sum(dim=1).unsqueeze(2)
yy = xx.permute(0, 2, 1)
dist = xx + yy - 2.0 * torch.bmm(x.permute(0, 2, 1), x)
dist[dist != dist] = 0
dist = torch.clamp(dist, 0.0, np.inf)
return dist
def MINDSSC(self, img, radius=2, dilation=2):
# see http://mpheinrich.de/pub/miccai2013_943_mheinrich.pdf for details on the MIND-SSC descriptor
# kernel size
kernel_size = radius * 2 + 1
# define start and end locations for self-similarity pattern
six_neighbourhood = torch.Tensor([[0, 1, 1],
[1, 1, 0],
[1, 0, 1],
[1, 1, 2],
[2, 1, 1],
[1, 2, 1]]).long()
# squared distances
dist = self.pdist_squared(six_neighbourhood.t().unsqueeze(0)).squeeze(0)
# define comparison mask
x, y = torch.meshgrid(torch.arange(6), torch.arange(6))
mask = ((x > y).view(-1) & (dist == 2).view(-1))
# build kernel
idx_shift1 = six_neighbourhood.unsqueeze(1).repeat(1, 6, 1).view(-1, 3)[mask, :]
idx_shift2 = six_neighbourhood.unsqueeze(0).repeat(6, 1, 1).view(-1, 3)[mask, :]
mshift1 = torch.zeros(12, 1, 3, 3, 3).cuda()
mshift1.view(-1)[torch.arange(12) * 27 + idx_shift1[:, 0] * 9 + idx_shift1[:, 1] * 3 + idx_shift1[:, 2]] = 1
mshift2 = torch.zeros(12, 1, 3, 3, 3).cuda()
mshift2.view(-1)[torch.arange(12) * 27 + idx_shift2[:, 0] * 9 + idx_shift2[:, 1] * 3 + idx_shift2[:, 2]] = 1
rpad1 = nn.ReplicationPad3d(dilation)
rpad2 = nn.ReplicationPad3d(radius)
# compute patch-ssd
ssd = F.avg_pool3d(rpad2(
(F.conv3d(rpad1(img), mshift1, dilation=dilation) - F.conv3d(rpad1(img), mshift2, dilation=dilation)) ** 2),
kernel_size, stride=1)
# MIND equation
mind = ssd - torch.min(ssd, 1, keepdim=True)[0]
mind_var = torch.mean(mind, 1, keepdim=True)
mind_var = torch.clamp(mind_var, (mind_var.mean() * 0.001).item(), (mind_var.mean() * 1000).item())
mind /= mind_var
mind = torch.exp(-mind)
# permute to have same ordering as C++ code
mind = mind[:, torch.Tensor([6, 8, 1, 11, 2, 10, 0, 7, 9, 4, 5, 3]).long(), :, :, :]
return mind
def forward(self, y_pred, y_true):
return torch.mean((self.MINDSSC(y_pred) - self.MINDSSC(y_true)) ** 2)
class MutualInformation(torch.nn.Module):
"""
Mutual Information
"""
def __init__(self, sigma_ratio=1, minval=0., maxval=1., num_bin=32):
super(MutualInformation, self).__init__()
"""Create bin centers"""
bin_centers = np.linspace(minval, maxval, num=num_bin)
vol_bin_centers = Variable(torch.linspace(minval, maxval, num_bin), requires_grad=False).cuda()
num_bins = len(bin_centers)
"""Sigma for Gaussian approx."""
sigma = np.mean(np.diff(bin_centers)) * sigma_ratio
print(sigma)
self.preterm = 1 / (2 * sigma ** 2)
self.bin_centers = bin_centers
self.max_clip = maxval
self.num_bins = num_bins
self.vol_bin_centers = vol_bin_centers
def mi(self, y_true, y_pred):
y_pred = torch.clamp(y_pred, 0., self.max_clip)
y_true = torch.clamp(y_true, 0, self.max_clip)
y_true = y_true.view(y_true.shape[0], -1)
y_true = torch.unsqueeze(y_true, 2)
y_pred = y_pred.view(y_pred.shape[0], -1)
y_pred = torch.unsqueeze(y_pred, 2)
nb_voxels = y_pred.shape[1] # total num of voxels
"""Reshape bin centers"""
o = [1, 1, np.prod(self.vol_bin_centers.shape)]
vbc = torch.reshape(self.vol_bin_centers, o).cuda()
"""compute image terms by approx. Gaussian dist."""
I_a = torch.exp(- self.preterm * torch.square(y_true - vbc))
I_a = I_a / torch.sum(I_a, dim=-1, keepdim=True)
I_b = torch.exp(- self.preterm * torch.square(y_pred - vbc))
I_b = I_b / torch.sum(I_b, dim=-1, keepdim=True)
# compute probabilities
pab = torch.bmm(I_a.permute(0, 2, 1), I_b)
pab = pab / nb_voxels
pa = torch.mean(I_a, dim=1, keepdim=True)
pb = torch.mean(I_b, dim=1, keepdim=True)
papb = torch.bmm(pa.permute(0, 2, 1), pb) + 1e-6
mi = torch.sum(torch.sum(pab * torch.log(pab / papb + 1e-6), dim=1), dim=1)
return mi.mean() # average across batch
def forward(self, y_true, y_pred):
return -self.mi(y_true, y_pred)
class localMutualInformation(torch.nn.Module):
"""
Local Mutual Information for non-overlapping patches
"""
def __init__(self, sigma_ratio=1, minval=0., maxval=1., num_bin=32, patch_size=5):
super(localMutualInformation, self).__init__()
"""Create bin centers"""
bin_centers = np.linspace(minval, maxval, num=num_bin)
vol_bin_centers = Variable(torch.linspace(minval, maxval, num_bin), requires_grad=False).cuda()
num_bins = len(bin_centers)
"""Sigma for Gaussian approx."""
sigma = np.mean(np.diff(bin_centers)) * sigma_ratio
self.preterm = 1 / (2 * sigma ** 2)
self.bin_centers = bin_centers
self.max_clip = maxval
self.num_bins = num_bins
self.vol_bin_centers = vol_bin_centers
self.patch_size = patch_size
def local_mi(self, y_true, y_pred):
y_pred = torch.clamp(y_pred, 0., self.max_clip)
y_true = torch.clamp(y_true, 0, self.max_clip)
"""Reshape bin centers"""
o = [1, 1, np.prod(self.vol_bin_centers.shape)]
vbc = torch.reshape(self.vol_bin_centers, o).cuda()
"""Making image paddings"""
if len(list(y_pred.size())[2:]) == 3:
ndim = 3
x, y, z = list(y_pred.size())[2:]
# compute padding sizes
x_r = -x % self.patch_size
y_r = -y % self.patch_size
z_r = -z % self.patch_size
padding = (z_r // 2, z_r - z_r // 2, y_r // 2, y_r - y_r // 2, x_r // 2, x_r - x_r // 2, 0, 0, 0, 0)
elif len(list(y_pred.size())[2:]) == 2:
ndim = 2
x, y = list(y_pred.size())[2:]
# compute padding sizes
x_r = -x % self.patch_size
y_r = -y % self.patch_size
padding = (y_r // 2, y_r - y_r // 2, x_r // 2, x_r - x_r // 2, 0, 0, 0, 0)
else:
raise Exception('Supports 2D and 3D but not {}'.format(list(y_pred.size())))
y_true = F.pad(y_true, padding, "constant", 0)
y_pred = F.pad(y_pred, padding, "constant", 0)
"""Reshaping images into non-overlapping patches"""
if ndim == 3:
y_true_patch = torch.reshape(y_true, (y_true.shape[0], y_true.shape[1],
(x + x_r) // self.patch_size, self.patch_size,
(y + y_r) // self.patch_size, self.patch_size,
(z + z_r) // self.patch_size, self.patch_size))
y_true_patch = y_true_patch.permute(0, 1, 2, 4, 6, 3, 5, 7)
y_true_patch = torch.reshape(y_true_patch, (-1, self.patch_size ** 3, 1))
y_pred_patch = torch.reshape(y_pred, (y_pred.shape[0], y_pred.shape[1],
(x + x_r) // self.patch_size, self.patch_size,
(y + y_r) // self.patch_size, self.patch_size,
(z + z_r) // self.patch_size, self.patch_size))
y_pred_patch = y_pred_patch.permute(0, 1, 2, 4, 6, 3, 5, 7)
y_pred_patch = torch.reshape(y_pred_patch, (-1, self.patch_size ** 3, 1))
else:
y_true_patch = torch.reshape(y_true, (y_true.shape[0], y_true.shape[1],
(x + x_r) // self.patch_size, self.patch_size,
(y + y_r) // self.patch_size, self.patch_size))
y_true_patch = y_true_patch.permute(0, 1, 2, 4, 3, 5)
y_true_patch = torch.reshape(y_true_patch, (-1, self.patch_size ** 2, 1))
y_pred_patch = torch.reshape(y_pred, (y_pred.shape[0], y_pred.shape[1],
(x + x_r) // self.patch_size, self.patch_size,
(y + y_r) // self.patch_size, self.patch_size))
y_pred_patch = y_pred_patch.permute(0, 1, 2, 4, 3, 5)
y_pred_patch = torch.reshape(y_pred_patch, (-1, self.patch_size ** 2, 1))
"""Compute MI"""
I_a_patch = torch.exp(- self.preterm * torch.square(y_true_patch - vbc))
I_a_patch = I_a_patch / torch.sum(I_a_patch, dim=-1, keepdim=True)
I_b_patch = torch.exp(- self.preterm * torch.square(y_pred_patch - vbc))
I_b_patch = I_b_patch / torch.sum(I_b_patch, dim=-1, keepdim=True)
pab = torch.bmm(I_a_patch.permute(0, 2, 1), I_b_patch)
pab = pab / self.patch_size ** ndim
pa = torch.mean(I_a_patch, dim=1, keepdim=True)
pb = torch.mean(I_b_patch, dim=1, keepdim=True)
papb = torch.bmm(pa.permute(0, 2, 1), pb) + 1e-6
mi = torch.sum(torch.sum(pab * torch.log(pab / papb + 1e-6), dim=1), dim=1)
return mi.mean()
def forward(self, y_true, y_pred):
return -self.local_mi(y_true, y_pred)