-
Notifications
You must be signed in to change notification settings - Fork 784
Expand file tree
/
Copy pathmutils.py
More file actions
217 lines (176 loc) · 6.36 KB
/
mutils.py
File metadata and controls
217 lines (176 loc) · 6.36 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
# Copyright (c) MONAI Consortium
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import os
from collections import OrderedDict
from pathlib import Path
import argparse
import numpy as np
import glob
import torch
import torch.fft as torch_fft
from torch import nn
import torchvision
def dict2namespace(config: dict):
"""
Utility function to convert a dictionary to a namespace
Args:
config - dictionary
Returns:
namespace - argparse.Namespace object
"""
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 get_sigmas(config):
if config.model.sigma_dist == "geometric":
sigmas = (
torch.tensor(
np.exp(
np.linspace(
np.log(config.model.sigma_begin), np.log(config.model.sigma_end), config.model.num_classes
)
)
)
.float()
.to(config.device)
)
elif config.model.sigma_dist == "uniform":
sigmas = (
torch.tensor(np.linspace(config.model.sigma_begin, config.model.sigma_end, config.model.num_classes))
.float()
.to(config.device)
)
else:
raise NotImplementedError("sigma distribution not supported")
return sigmas
def np_ifft2(kspace):
"""
Perform ifft2 on numpy array.
Args:
kspace (np.ndarray): kspace data (..., H, W)
Returns:
output (np.ndarray): complex valued image data (..., H, W)
"""
output = np.fft.fftshift(
np.fft.ifft2(np.fft.ifftshift(kspace, axes=(-1, -2)), axes=(-1, -2), norm="ortho"), axes=(-1, -2)
)
return output
def scale(img):
img = img.detach().cpu().numpy()
magnitude_vals = np.abs(img).reshape(-1)
if img.shape[0] == 320:
k = int(round(0.015 * torch.from_numpy(magnitude_vals).numel()))
else:
k = int(round(0.02 * torch.from_numpy(magnitude_vals).numel()))
scale = torch.min(torch.topk(torch.from_numpy(magnitude_vals), k).values)
img = torch.clip(img / scale, 0, 1)
return img
def normalize(gen_img, estimated_mvue):
"""
Estimate mvue from coils and normalize with 99% percentile.
"""
scaling = torch.quantile(estimated_mvue.abs(), 0.99)
return gen_img * scaling
def unnormalize(gen_img, estimated_mvue):
"""
Estimate mvue from coils and normalize with 99% percentile.
"""
scaling = torch.quantile(estimated_mvue.abs(), 0.99)
return gen_img / scaling
def ifft(x):
x = torch_fft.ifftshift(x, dim=(-2, -1))
x = torch_fft.ifft2(x, dim=(-2, -1), norm="ortho")
x = torch_fft.fftshift(x, dim=(-2, -1))
return x
def fft(x):
x = torch_fft.fftshift(x, dim=(-2, -1))
x = torch_fft.fft2(x, dim=(-2, -1), norm="ortho")
x = torch_fft.ifftshift(x, dim=(-2, -1))
return x
# Multicoil forward operator for MRI
class MulticoilForwardMRI(nn.Module):
def __init__(self, orientation):
self.orientation = orientation
super(MulticoilForwardMRI, self).__init__()
return
def forward(self, image, maps, mask):
"""
Inputs:
- image = [B, H, W] torch.complex64/128 in image domain
- maps = [B, C, H, W] torch.complex64/128 in image domain
- mask = [B, W] torch.complex64/128 w/ binary values
Outputs:
- ksp_coils = [B, C, H, W] torch.complex64/128 in kspace domain
"""
# Broadcast pointwise multiply
coils = image[:, None] * maps
# Convert to k-space data
ksp_coils = fft(coils)
if self.orientation == "vertical":
# Mask k-space phase encode lines
ksp_coils = ksp_coils * mask[:, None, None, :]
elif self.orientation == "horizontal":
# Mask k-space frequency encode lines
ksp_coils = ksp_coils * mask[:, None, :, None]
else:
if len(mask.shape) == 3:
ksp_coils = ksp_coils * mask[:, None, :, :]
else:
raise NotImplementedError("mask orientation not supported")
# Return downsampled k-space
return ksp_coils
def get_mvue(kspace, s_maps):
"""Get mvue estimate from coil measurements"""
ifft2_kspace = np_ifft2(kspace)
return np.sum(ifft2_kspace * np.conj(s_maps), axis=1) / np.sqrt(np.sum(np.square(np.abs(s_maps)), axis=1))
def get_all_files(folder, pattern="*"):
files = [x for x in glob.iglob(os.path.join(folder, pattern))]
return sorted(files)
# Source: https://stackoverflow.com/questions/3229419/how-to-pretty-print-nested-dictionaries
def pretty(d, indent=0):
"""Print dictionary"""
for key, value in d.items():
print("\t" * indent + str(key))
if isinstance(value, dict):
pretty(value, indent + 1)
else:
print("\t" * (indent + 1) + str(value))
def save_images(samples, loc, normalize=False):
# convert loc to pathlib.Path
loc = Path(loc)
torchvision.utils.save_image(
samples,
loc,
nrow=int(samples.shape[0] ** 0.5),
normalize=normalize,
scale_each=True,
)
def load_dict(model, ckpt, device="cuda"):
state_dict = torch.load(ckpt, map_location=device)
try:
model.load_state_dict(state_dict)
except:
print("Loading model failed... Trying to remove the module from the keys...")
new_state_dict = OrderedDict()
for key, value in state_dict.items():
new_state_dict[key[len("module.") :]] = value
model.load_state_dict(new_state_dict)
return model
def update_pbar_desc(pbar, metrics, labels):
pbar_string = ""
for metric, label in zip(metrics, labels):
pbar_string += f"{label}: {metric:.7f}; "
pbar.set_description(pbar_string)