-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
1256 lines (1033 loc) · 35.9 KB
/
Copy pathutils.py
File metadata and controls
1256 lines (1033 loc) · 35.9 KB
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
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import os
import torch
import numpy as np
import argparse
import torch.fft as FFT
import glob
import scipy.io as scio
import logging
sqrt = np.sqrt
import torch.nn.functional as F
import torchvision.transforms as T
from icecream import ic
from tqdm import tqdm
from scipy.linalg import null_space, svd
from optimal_thresh import optht
import sigpy as sp
import sigpy.mri.app as MR
from torch.utils.dlpack import to_dlpack, from_dlpack
from cupy import from_dlpack as cu_from_dlpack
class Aclass_sense:
def __init__(self, csm, mask, lam):
self.s = csm
self.mask = 1 - mask
self.lam = lam
def ATA(self, ksp):
Ax = sense(self.s, ksp)
AHAx = adjsense(self.s, Ax)
return AHAx
def A(self, ksp):
res = self.ATA(ksp * self.mask) * self.mask + self.lam * ksp
return res
def sense(csm, ksp):
m = Emat_xyt(c2r(ksp), True, c2r(csm), 1)
res = Emat_xyt(m, False, c2r(csm), 1)
return r2c(res) - ksp
def adjsense(csm, ksp):
m = Emat_xyt(c2r(ksp), True, c2r(csm), 1)
res = Emat_xyt(m, False, c2r(csm), 1)
return r2c(res) - ksp
class ConjGrad:
def __init__(self, A, rhs, max_iter=5, eps=1e-10):
self.A = A
self.b = rhs
self.max_iter = max_iter
self.eps = eps
def forward(self, x):
x = CG(x, self.b, self.A, max_iter=self.max_iter, eps=self.eps)
return x
def dot_batch(x1, x2):
batch = x1.shape[0]
res = torch.reshape(x1 * x2, (batch, -1))
return torch.sum(res, 1)
def CG(x, b, A, max_iter, eps):
r = b - A.A(x)
p = r
rTr = dot_batch(torch.conj(r), r)
reshape = (-1,) + (1,) * (len(x.shape) - 1)
num_iter = 0
for iter in range(max_iter):
if rTr.abs().max() < eps:
break
Ap = A.A(p)
alpha = rTr / dot_batch(torch.conj(p), Ap)
alpha = torch.reshape(alpha, reshape)
x = x + alpha * p
r = r - alpha * Ap
rTrNew = dot_batch(torch.conj(r), r)
beta = rTrNew / rTr
beta = torch.reshape(beta, reshape)
p = r + beta * p
rTr = rTrNew
num_iter += 1
return x
def cgSENSE(ksp, csm, mask, x0, niter, lam):
Aobj = Aclass_sense(csm, mask, lam)
y = - (1 - mask) * Aobj.ATA(ksp)
cg_iter = ConjGrad(Aobj, y, max_iter=niter)
x0 = Emat_xyt(x0, False, c2r(csm), 1)
x = cg_iter.forward(x=r2c(x0))
x = x * (1 - mask) + ksp
res = Emat_xyt(c2r(x), True, c2r(csm), 1)
return res
def init_seeds(seed=0):
np.random.seed(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
if seed == 0:
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
def save_mat(save_dict, variable, file_name, index=0, Complex=True, normalize=True):
if normalize:
if Complex:
variable = normalize_complex(variable)
else:
variable_abs = torch.abs(variable)
coeff = torch.max(variable_abs)
variable = variable / coeff
variable = variable.cpu().detach().numpy()
file = os.path.join(save_dict, str(file_name) +
'_' + str(index + 1) + '.mat')
datadict = {str(file_name): np.squeeze(variable)}
scio.savemat(file, datadict)
def hfssde_save_mat(config, variable, variable_name='recon', normalize=True):
if normalize:
variable = normalize_complex(variable)
variable = variable.cpu().detach().numpy()
save_dict = config.sampling.folder
file_name = config.training.sde + '_acc' + config.sampling.acc + '_acs' + config.sampling.acs \
+ '_epoch' + str(config.sampling.ckpt)
file = os.path.join(save_dict, str(file_name) + '.mat')
datadict = {variable_name: np.squeeze(variable)}
scio.savemat(file, datadict)
def get_all_files(folder, pattern='*'):
files = [x for x in glob.iglob(os.path.join(folder, pattern))]
return sorted(files)
def dict2namespace(config):
namespace = argparse.Namespace()
for key, value in config.items():
if isinstance(value, dict):
new_value = dict2namespace(value)
else:
new_value = value
setattr(namespace, key, new_value)
return namespace
def to_tensor(x):
re = np.real(x)
im = np.imag(x)
x = np.concatenate([re, im], 1)
del re, im
return torch.from_numpy(x)
def crop(img, cropc, cropx, cropy):
if img.ndim == 5:
nb, nc, c, x, y = img.size()
startc = c // 2 - cropc // 2
startx = x // 2 - cropx // 2
starty = y // 2 - cropy // 2
cimg = img[:, :, startc:startc + cropc, startx:startx + cropx, starty: starty + cropy]
elif img.ndim == 4:
nb, c, x, y = img.size()
startx = x // 2 - cropx // 2
starty = y // 2 - cropy // 2
cimg = img[:, :, startx:startx + cropx, starty: starty + cropy]
return cimg
def t_crop(img, cropx, cropy):
nb, c, x, y = img.size()
startx = x // 2 - cropx // 2
starty = y // 2 - cropy // 2
cimg = img[:, :, startx:startx + cropx, starty: starty + cropy]
return cimg
def acs_crop(img, cropx, cropy):
acs = torch.zeros_like(img)
nb, c, x, y = img.size()
startx = x // 2 - cropx // 2
starty = y // 2 - cropy // 2
acs[:, :, startx:startx + cropx, starty: starty + cropy] = img[:, :, startx:startx + cropx, starty: starty + cropy]
return acs
def inv_crop(target,center_tensor):
padded_tensor = torch.zeros_like(target)
pad_top = (padded_tensor.shape[0] - center_tensor.shape[0]) // 2
pad_bottom = padded_tensor.shape[0] - center_tensor.shape[0] - pad_top
pad_left = (padded_tensor.shape[1] - center_tensor.shape[1]) // 2
pad_right = padded_tensor.shape[1] - center_tensor.shape[1] - pad_left
pad_front = (padded_tensor.shape[2] - center_tensor.shape[2]) // 2
pad_back = padded_tensor.shape[2] - center_tensor.shape[2] - pad_front
pad_leftmost = (padded_tensor.shape[3] - center_tensor.shape[3]) // 2
pad_rightmost = padded_tensor.shape[3] - center_tensor.shape[3] - pad_leftmost
padded_tensor = F.pad(center_tensor, (pad_leftmost, pad_rightmost, pad_front, pad_back, pad_left, pad_right, pad_top, pad_bottom))
return padded_tensor
def inv_crop_numpy(target, tensor):
target_size = target.shape
tensor_shape = np.array(tensor.shape)
target_size = np.array(target_size)
pad_sizes = np.maximum(target_size - tensor_shape, 0)
pad_left = pad_sizes // 2
pad_right = pad_sizes - pad_left
padding = [(pad_left[i], pad_right[i]) for i in range(len(tensor_shape))]
padded_tensor = np.pad(tensor, padding, mode='constant')
return padded_tensor
def torch_crop(img, cropx, cropy):
nb, c, x, y = img.shape
startx = x // 2 - cropx // 2
starty = y // 2 - cropy // 2
if y>cropy and x>cropx:
img = crop(img, cropx, cropy)
elif y>cropy and x<cropx:
img = crop(img, x, cropy)
target = torch.zeros(nb,c,cropx,cropy)
img = inv_crop(target,img)
elif y<cropy and x>cropx:
img = crop(img, cropx, y)
target = torch.zeros(nb,c,cropx,cropy)
img = inv_crop(target,img)
else:
target = torch.zeros(nb,c,cropx,cropy)
img = inv_crop(target,img)
return img
def pad_or_crop_tensor(input_tensor, target_shape):
input_shape = input_tensor.shape
pad_width = []
for i in range(len(target_shape)):
diff = target_shape[i] - input_shape[i]
pad_before = max(0, diff // 2)
pad_after = max(0, diff - pad_before)
pad_width.append((pad_before, pad_after))
padded_tensor = np.pad(input_tensor, pad_width, mode='constant')
cropped_tensor = padded_tensor[:target_shape[0], :target_shape[1], :target_shape[2], :target_shape[3]]
return cropped_tensor
def normalize(img):
""" Normalize img in arbitrary range to [0, 1] """
img -= torch.min(img)
img /= torch.max(img)
return img
def normalize_np(img):
""" Normalize img in arbitrary range to [0, 1] """
img -= np.min(img)
img /= np.max(img)
return img
def normalize_complex(img):
""" normalizes the magnitude of complex-valued image to range [0, 1] """
abs_img = normalize(torch.abs(img))
ang_img = normalize(torch.angle(img))
return abs_img * torch.exp(1j * ang_img)
def normalize_l2(img):
minv = np.std(img)
img = img / minv
return img
def get_data_scaler(config):
"""Data normalizer. Assume data are always in [0, 1]."""
if config.data.centered:
# Rescale to [-1, 1]
return lambda x: x * 2. - 1.
else:
return lambda x: x
def get_data_inverse_scaler(config):
"""Inverse data normalizer."""
if config.data.centered:
# Rescale [-1, 1] to [0, 1]
return lambda x: (x + 1.) / 2.
else:
return lambda x: x
def get_mask(config, caller):
if caller == 'sde':
if config.training.mask_type == 'low_frequency':
mask_file = 'mask/' + config.training.mask_type + "_acs" + config.training.acs + '.mat'
elif config.training.mask_type == 'center':
mask_file = 'mask/' + config.training.mask_type + "_length" + config.training.acs + '.mat'
else:
mask_file = 'mask/' + config.training.mask_type + "_acc" + config.training.acc \
+ '_acs' + config.training.acs + '.mat'
elif caller == 'sample':
mask_file = 'mask/' + config.sampling.mask_type + "_acc" + config.sampling.acc \
+ '_acs' + config.sampling.acs + '.mat'
elif caller == 'acs':
mask_file = 'mask/low_frequency_acs18.mat'
mask = scio.loadmat(mask_file)['mask']
mask = mask.astype(np.complex)
mask = np.expand_dims(mask, axis=0)
mask = np.expand_dims(mask, axis=0)
mask = torch.from_numpy(mask).to(config.device)
return mask
def ifftshift(x, axes=None):
assert torch.is_tensor(x) == True
if axes is None:
axes = tuple(range(x.ndim))
shift = [-(dim // 2) for dim in x.shape]
elif isinstance(axes, int):
shift = -(x.shape[axes] // 2)
else:
shift = [-(x.shape[axis] // 2) for axis in axes]
return torch.roll(x, shift, axes)
def fftshift(x, axes=None):
assert torch.is_tensor(x) == True
if axes is None:
axes = tuple(range(x.ndim()))
shift = [dim // 2 for dim in x.shape]
elif isinstance(axes, int):
shift = x.shape[axes] // 2
else:
shift = [x.shape[axis] // 2 for axis in axes]
return torch.roll(x, shift, axes)
def fft2c(x):
device = x.device
nb, nc, nt, nx, ny = x.size()
ny = torch.Tensor([ny]).to(device)
nx = torch.Tensor([nx]).to(device)
x = ifftshift(x, axes=3)
x = torch.transpose(x, 3, 4)
x = FFT.fft(x)
x = torch.transpose(x, 3, 4)
x = torch.div(fftshift(x, axes=3), torch.sqrt(nx))
x = ifftshift(x, axes=4)
x = FFT.fft(x)
x = torch.div(fftshift(x, axes=4), torch.sqrt(ny))
return x
def fft2c_2d(x):
device = x.device
nb, nc, nx, ny = x.size()
ny = torch.Tensor([ny]).to(device)
nx = torch.Tensor([nx]).to(device)
x = ifftshift(x, axes=2)
x = torch.transpose(x, 2, 3)
x = FFT.fft(x)
x = torch.transpose(x, 2, 3)
x = torch.div(fftshift(x, axes=2), torch.sqrt(nx))
x = ifftshift(x, axes=3)
x = FFT.fft(x)
x = torch.div(fftshift(x, axes=3), torch.sqrt(ny))
return x
def FFT2c(x):
nb, nc, nx, ny = np.shape(x)
x = np.fft.ifftshift(x, axes=2)
x = np.transpose(x, [0, 1, 3, 2])
x = np.fft.fft(x, axis=-1)
x = np.transpose(x, [0, 1, 3, 2])
x = np.fft.fftshift(x, axes=2)/np.math.sqrt(nx)
x = np.fft.ifftshift(x, axes=3)
x = np.fft.fft(x, axis=-1)
x = np.fft.fftshift(x, axes=3)/np.math.sqrt(ny)
return x
def ifft2c(x):
device = x.device
nb, nc, nt, nx, ny = x.size()
ny = torch.Tensor([ny])
ny = ny.to(device)
nx = torch.Tensor([nx])
nx = nx.to(device)
x = ifftshift(x, axes=3)
x = torch.transpose(x, 3, 4)
x = FFT.ifft(x)
x = torch.transpose(x, 3, 4)
x = torch.mul(fftshift(x, axes=3), torch.sqrt(nx))
x = ifftshift(x, axes=4)
x = FFT.ifft(x)
x = torch.mul(fftshift(x, axes=4), torch.sqrt(ny))
return x
def ifft2c_2d(x):
device = x.device
nb, nc, nx, ny = x.size()
ny = torch.Tensor([ny])
ny = ny.to(device)
nx = torch.Tensor([nx])
nx = nx.to(device)
x = ifftshift(x, axes=2)
x = torch.transpose(x, 2, 3)
x = FFT.ifft(x)
x = torch.transpose(x, 2, 3)
x = torch.mul(fftshift(x, axes=2), torch.sqrt(nx))
x = ifftshift(x, axes=3)
x = FFT.ifft(x)
x = torch.mul(fftshift(x, axes=3), torch.sqrt(ny))
return x
def ifft2c_3d(x):
device = x.device
nb, nc, nz, nx, ny = x.size()
ny = torch.Tensor([ny])
ny = ny.to(device)
nx = torch.Tensor([nx])
nx = nx.to(device)
nz = torch.Tensor([nz])
nz = nz.to(device)
x = ifftshift(x, axes=2)
x = torch.transpose(x, 2, 4)
x = FFT.ifft(x)
x = torch.transpose(x, 2, 4)
x = torch.mul(fftshift(x, axes=2), torch.sqrt(nz))
x = ifftshift(x, axes=3)
x = torch.transpose(x, 3, 4)
x = FFT.ifft(x)
x = torch.transpose(x, 3, 4)
x = torch.mul(fftshift(x, axes=3), torch.sqrt(nx))
x = ifftshift(x, axes=4)
x = FFT.ifft(x)
x = torch.mul(fftshift(x, axes=4), torch.sqrt(ny))
return x
def fft2c_3d(x):
device = x.device
nb, nc, nz, nx, ny = x.size()
nx = torch.Tensor([nx]).to(device)
ny = torch.Tensor([ny]).to(device)
nz = torch.Tensor([nz]).to(device)
x = ifftshift(x, axes=2)
x = torch.transpose(x, 2, 4)
x = FFT.fft(x)
x = torch.transpose(x, 2, 4)
x = torch.div(fftshift(x, axes=2), torch.sqrt(nz))
x = ifftshift(x, axes=3)
x = torch.transpose(x, 3, 4)
x = FFT.fft(x)
x = torch.transpose(x, 3, 4)
x = torch.div(fftshift(x, axes=3), torch.sqrt(nx))
x = ifftshift(x, axes=4)
x = FFT.fft(x)
x = torch.div(fftshift(x, axes=4), torch.sqrt(ny))
return x
def IFFT2c(x):
nb, nc, nx, ny = np.shape(x)
x = np.fft.ifftshift(x, axes=2)
x = np.transpose(x, [0, 1, 3, 2])
x = np.fft.ifft(x, axis=-1)
x = np.transpose(x, [0, 1, 3, 2])
x = np.fft.fftshift(x, axes=2)*np.math.sqrt(nx)
x = np.fft.ifftshift(x, axes=3)
x = np.fft.ifft(x, axis=-1)
x = np.fft.fftshift(x, axes=3)*np.math.sqrt(ny)
return x
def Emat_xyt(b, inv, csm, mask):
if csm == None:
if inv:
b = r2c(b) * mask
if b.ndim == 4:
b = ifft2c_2d(b)
else:
b = ifft2c_3d(b)
x = c2r(b)
else:
b = r2c(b)
if b.ndim == 4:
b = fft2c_2d(b) * mask
else:
b = fft2c_3d(b) * mask
x = c2r(b)
else:
if inv:
csm = r2c(csm)
x = r2c(b) * mask
if b.ndim == 4:
x = ifft2c_2d(x)
else:
x = ifft2c_3d(x)
x = x*torch.conj(csm)
x = torch.sum(x, 1)
x = torch.unsqueeze(x, 1)
x = c2r(x)
else:
csm = r2c(csm)
b = r2c(b)
b = b*csm
if b.ndim == 4:
b = fft2c_2d(b)
else:
b = fft2c_3d(b)
x = mask*b
x = c2r(x)
return x
def SS_H(z,csm):
z = r2c(z)
csm = r2c(csm)
z = torch.sum(z*torch.conj(csm),dim=1,keepdim=True)
z = z*csm
return c2r(z)
def S_H(z,csm):
z = r2c(z)
csm = r2c(csm)
z = torch.sum(z*torch.conj(csm),dim=1,keepdim=True)
return c2r(z)
def SS_H_hat(z,csm):
z = r2c(z)
z = ifft2c_2d(z)
csm = r2c(csm)
z = torch.sum(z*torch.conj(csm),dim=1,keepdim=True)
z = z*csm
z = fft2c_2d(z)
return c2r(z)
def S_H_hat(z,csm):
z = r2c(z)
z = ifft2c_2d(z)
csm = r2c(csm)
z = torch.sum(z*torch.conj(csm),dim=1,keepdim=True)
z = fft2c_2d(z)
return c2r(z)
def ch_to_nb(z,filt=None):
z = r2c(z)
if filt==None:
z = torch.permute(z,(1,0,2,3))
else:
z = torch.permute(z,(1,0,2,3))/filt
return c2r(z)
def Emat_xyt_complex(b, inv, csm, mask):
if csm == None:
if inv:
b = b * mask
if b.ndim == 4:
x = ifft2c_2d(b)
else:
x = ifft2c(b)
else:
if b.ndim == 4:
x = fft2c_2d(b) * mask
else:
x = fft2c(b) * mask
else:
if inv:
x = b * mask
if b.ndim == 4:
x = ifft2c_2d(x)
else:
x = ifft2c(x)
x = x*torch.conj(csm)
x = torch.sum(x, 1)
x = torch.unsqueeze(x, 1)
else:
b = b*csm
if b.ndim == 4:
b = fft2c_2d(b)
else:
b = fft2c(b)
x = mask*b
return x
def r2c(x):
re, im = torch.chunk(x, 2, 1)
x = torch.complex(re, im)
return x
def c2r(x):
x = torch.cat([torch.real(x), torch.imag(x)], 1)
return x
def sos(x):
xr, xi = torch.chunk(x, 2, 1)
x = torch.pow(torch.abs(xr), 2)+torch.pow(torch.abs(xi), 2)
x = torch.sum(x, dim=1)
x = torch.pow(x, 0.5)
x = torch.unsqueeze(x, 1)
return x
def Abs(x):
x = r2c(x)
return torch.abs(x)
def l2mean(x):
result = torch.mean(torch.pow(torch.abs(x), 2))
return result
def TV(x, norm='L1'):
nb, nc, nx, ny = x.size()
Dx = torch.cat([x[:, :, 1:nx, :], x[:, :, 0:1, :]], 2)
Dy = torch.cat([x[:, :, :, 1:ny], x[:, :, :, 0:1]], 3)
Dx = Dx - x
Dy = Dy - x
tv = 0
if norm == 'L1':
tv = torch.mean(torch.abs(Dx)) + torch.mean(torch.abs(Dy))
elif norm == 'L2':
Dx = Dx * Dx
Dy = Dy * Dy
tv = torch.mean(Dx) + torch.mean(Dy)
return tv
def stdnormalize(x):
x = r2c(x)
result = c2r(x)/torch.std(x)
return result
def to_null_space(x,mask,csm):
Aobj = Aclass(csm, mask, torch.tensor(.01).cuda())
Rhs = Emat_xyt(x, False, csm, mask)
Rhs = Emat_xyt(Rhs, True, csm, mask)
x_null = x - myCG(Aobj, Rhs, x, 5)
return x_null
class Aclass:
"""
This class is created to do the data-consistency (DC) step as described in paper.
A^{T}A * X + \lamda *X
"""
def __init__(self, csm, mask, lam):
self.pixels = mask.shape[0] * mask.shape[1]
self.mask = mask
self.csm = csm
self.SF = torch.complex(torch.sqrt(torch.tensor(self.pixels).float()), torch.tensor(0.).float())
self.lam = lam
def myAtA(self, img):
x = Emat_xyt(img, False, self.csm, self.mask)
x = Emat_xyt(x, True, self.csm, self.mask)
return x + self.lam * img
def myCG(A, Rhs, x0, it):
"""
This is my implementation of CG algorithm in tensorflow that works on
complex data and runs on GPU. It takes the class object as input.
"""
x0 = torch.zeros_like(Rhs)
Rhs = r2c(Rhs) + A.lam * r2c(x0)
x = r2c(x0)
i = 0
r = Rhs - r2c(A.myAtA(x0))
p = r
rTr = torch.sum(torch.conj(r)*r).float()
while i < it:
Ap = r2c(A.myAtA(c2r(p)))
alpha = rTr / torch.sum(torch.conj(p)*Ap).float()
alpha = torch.complex(alpha, torch.tensor(0.).float().cuda())
x = x + alpha * p
r = r - alpha * Ap
rTrNew = torch.sum(torch.conj(r)*r).float()
beta = rTrNew / rTr
beta = torch.complex(beta, torch.tensor(0.).float().cuda())
p = r + beta * p
i = i + 1
rTr = rTrNew
return c2r(x)
def restore_checkpoint(ckpt_dir, state, device):
loaded_state = torch.load(ckpt_dir, map_location=device)
state['optimizer'].load_state_dict(loaded_state['optimizer'])
state['model'].load_state_dict(loaded_state['model'], strict=False)
state['ema'].load_state_dict(loaded_state['ema'])
state['step'] = loaded_state['step']
return state
def save_checkpoint(ckpt_dir, state):
saved_state = {
'optimizer': state['optimizer'].state_dict(),
'model': state['model'].state_dict(),
'ema': state['ema'].state_dict(),
'step': state['step']
}
torch.save(saved_state, ckpt_dir)
def complex_kernel_forward(filter, i):
filter = torch.squeeze(filter[i])
filter_real = torch.real(filter)
filter_img = torch.imag(filter)
kernel_real = torch.cat([filter_real, -filter_img], 1)
kernel_imag = torch.cat([filter_img, filter_real], 1)
kernel_complex = torch.cat([kernel_real, kernel_imag], 0)
return kernel_complex
def conv2(x1, x2):
return F.conv2d(x1, x2, padding='same')
def ksp2float(ksp, i):
kdata = torch.squeeze(ksp[i])
if len(kdata.shape) == 3:
kdata = torch.unsqueeze(kdata, 0)
kdata_float = torch.cat([torch.real(kdata), torch.imag(kdata)], 1)
return kdata_float
def spirit(kernel, ksp):
nb = ksp.shape[0]
if len(ksp.shape) == 5:
ksp = torch.permute(ksp, (0, 2, 1, 3, 4))
res_i = torch.stack([conv2(ksp2float(ksp, i), complex_kernel_forward(kernel, i)) for i in range(nb)], 0)
else:
res_i = torch.cat([conv2(ksp2float(ksp, i), complex_kernel_forward(kernel, i)) for i in range(nb)], 0)
if len(ksp.shape) == 5:
res_i = torch.permute(res_i, (0, 2, 1, 3, 4))
ksp = torch.permute(ksp, (0, 2, 1, 3, 4))
re, im = torch.chunk(res_i, 2, 1)
res = torch.complex(re, im) - ksp
return res
def adjspirit(kernel, ksp):
nb = kernel.shape[0]
filter = torch.permute(kernel, (0, 2, 1, 3, 4))
filter = torch.conj(filter.flip(dims=[-2, -1]))
if len(ksp.shape) == 5:
ksp = torch.permute(ksp, (0, 2, 1, 3, 4))
res_i = torch.stack([conv2(ksp2float(ksp, i), complex_kernel_forward(filter, i)) for i in range(nb)], 0)
else:
res_i = torch.cat([conv2(ksp2float(ksp, i), complex_kernel_forward(filter, i)) for i in range(nb)], 0)
if len(ksp.shape) == 5:
res_i = torch.permute(res_i, (0, 2, 1, 3, 4))
ksp = torch.permute(ksp, (0, 2, 1, 3, 4))
re, im = torch.chunk(res_i, 2, 1)
res = torch.complex(re, im) - ksp
return res
def dot_batch(x1, x2):
batch = x1.shape[0]
res = torch.reshape(x1 * x2, (batch, -1))
return torch.sum(res, 1)
class ConjGrad:
def __init__(self, A, rhs, max_iter=5, eps=1e-10):
self.A = A
self.b = rhs
self.max_iter = max_iter
self.eps = eps
def forward(self, x):
x = CG(x, self.b, self.A, max_iter=self.max_iter, eps=self.eps)
return x
def CG(x, b, A, max_iter, eps):
b = b + eps*x
r = b - A.A(x)
p = r
rTr = dot_batch(torch.conj(r), r)
reshape = (-1,) + (1,) * (len(x.shape) - 1)
num_iter = 0
for iter in range(max_iter):
if rTr.abs().max() < eps:
break
Ap = A.A(p)
alpha = rTr / dot_batch(torch.conj(p), Ap)
alpha = torch.reshape(alpha, reshape)
x = x + alpha * p
r = r - alpha * Ap
rTrNew = dot_batch(torch.conj(r), r)
beta = rTrNew / rTr
beta = torch.reshape(beta, reshape)
p = r + beta * p
rTr = rTrNew
num_iter += 1
return x
def dat2AtA(data, kernel_size):
'''Computes the calibration matrix from calibration data.
'''
tmp = im2row(data, kernel_size)
tsx, tsy, tsz = tmp.shape[:]
A = np.reshape(tmp, (tsx, tsy*tsz), order='F')
return np.dot(A.T.conj(), A)
def im2row(im, win_shape):
'''res = im2row(im, winSize)'''
sx, sy, sz = im.shape[:]
wx, wy = win_shape[:]
sh = (sx-wx+1)*(sy-wy+1)
res = np.zeros((sh, wx*wy, sz), dtype=im.dtype)
count = 0
for y in range(wy):
for x in range(wx):
res[:, count, :] = np.reshape(
im[x:sx-wx+x+1, y:sy-wy+y+1, :], (sh, sz))
count += 1
return res
def calibrate_single_coil(AtA, kernel_size, ncoils, coil, lamda, sampling=None):
kx, ky = kernel_size[:]
if sampling is None:
sampling = np.ones((*kernel_size, ncoils))
dummyK = np.zeros((kx, ky, ncoils))
dummyK[int(kx/2), int(ky/2), coil] = 1
idxY = np.where(dummyK)
idxY_flat = np.sort(
np.ravel_multi_index(idxY, dummyK.shape, order='F'))
sampling[idxY] = 0
idxA = np.where(sampling)
idxA_flat = np.sort(
np.ravel_multi_index(idxA, sampling.shape, order='F'))
Aty = AtA[:, idxY_flat]
Aty = Aty[idxA_flat]
AtA0 = AtA[idxA_flat, :]
AtA0 = AtA0[:, idxA_flat]
kernel = np.zeros(sampling.size, dtype=AtA0.dtype)
lamda = np.linalg.norm(AtA0)/AtA0.shape[0]*lamda
rawkernel = np.linalg.solve(AtA0 + np.eye(AtA0.shape[0])*lamda, Aty) # fast 1s
kernel[idxA_flat] = rawkernel.squeeze()
kernel = np.reshape(kernel, sampling.shape, order='F')
return(kernel, rawkernel)
def spirit_calibrate(acs, kSize, lamda=0.001, filtering=False, verbose=True): # lamda=0.01
nCoil = acs.shape[-1]
AtA = dat2AtA(acs,kSize)
if filtering:
if verbose:
ic('prefiltering w/ opth')
U,s,Vh = svd(AtA, full_matrices=False)
k = optht(AtA, sv=s, sigma=None)
if verbose:
print('{}/{} kernels used'.format(k, len(s)))
AtA= (U[:, :k] * s[:k] ).dot( Vh[:k,:])
spirit_kernel = np.zeros((nCoil,nCoil,*kSize),dtype='complex128')
for c in tqdm(range(nCoil)):
tmp, _ = calibrate_single_coil(AtA,kernel_size=kSize,ncoils=nCoil,coil=c,lamda=lamda)
spirit_kernel[c] = np.transpose(tmp,[2,0,1])
spirit_kernel = np.transpose(spirit_kernel,[2,3,1,0])
GOP = np.transpose(spirit_kernel[::-1,::-1],[3,2,0,1])
GOP = GOP.copy()
for n in range(nCoil):
GOP[n,n,kSize[0]//2,kSize[1]//2] = -1
return spirit_kernel
class Aclass_spirit:
def __init__(self, kernel, mask, lam):
self.kernel = kernel
self.mask = 1 - mask
self.lam = lam
def ATA(self, ksp):
ksp = spirit(self.kernel, ksp)
ksp = adjspirit(self.kernel, ksp)
return ksp
def A(self, ksp):
res = self.ATA(ksp * self.mask) * self.mask + self.lam * self.mask * ksp
return res
class Aclass_spirit_proj:
def __init__(self, kernel, mask, lam1, lam2, xi):
self.kernel = kernel
self.mask = mask
self.lam1 = lam1
self.lam2 = lam2
self.xi = xi
def ATA(self, ksp):
ksp = spirit(self.kernel, ksp)
ksp = adjspirit(self.kernel, ksp)
return ksp
def A(self, ksp):
res = self.mask*ksp + self.ATA(ksp) * self.lam1 + self.lam2 * torch.sum(self.xi * ksp)*self.xi
return res
def sense(csm, ksp):
m = torch.sum(ifft2c_2d(ksp) * torch.conj(csm), 1, keepdim=True)
res = fft2c_2d(m * csm)
return res - ksp
def sense3d(csm, ksp):
m = torch.sum(ifft2c_3d(ksp) * torch.conj(csm), 1, keepdim=True)
res = fft2c_3d(m * csm)
return res - ksp
def adjsense(csm, ksp):
m = torch.sum(ifft2c_2d(ksp) * torch.conj(csm), 1, keepdim=True)
res = fft2c_2d(m * csm)
return res - ksp
def adjsense3d(csm, ksp):
m = torch.sum(ifft2c_3d(ksp) * torch.conj(csm), 1, keepdim=True)
res = fft2c_3d(m * csm)
return res - ksp
class Aclass_sense:
def __init__(self, csm, mask, lam):
self.s = csm
self.mask = 1 - mask
self.lam = lam
def ATA(self, ksp):
Ax = sense(self.s, ksp)
AHAx = adjsense(self.s, Ax)
return AHAx
def A(self, ksp):
res = self.ATA(ksp * self.mask) * self.mask + self.lam * ksp
return res
class Aclass_sense3d:
def __init__(self, csm, mask, lam):
self.s = csm
self.mask = 1 - mask
self.lam = lam
def ATA(self, ksp):
Ax = sense3d(self.s, ksp)
AHAx = adjsense3d(self.s, Ax)
return AHAx
def A(self, ksp):
res = self.ATA(ksp * self.mask) * self.mask + self.lam * ksp
return res
class Aclass_sensev2:
def __init__(self, csm, mask, lam):
self.s = csm
self.mask = mask
self.lam = lam
def ATA(self, x):
Ax = Emat_xyt(x, False, self.s, self.mask)