-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun_lib.py
319 lines (273 loc) · 10.8 KB
/
run_lib.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
"""Training and evaluation for score-based generative models. """
import os
import time
import tensorflow as tf
import logging
from torchmetrics import StructuralSimilarityIndexMeasure, PeakSignalNoiseRatio
# Keep the import below for registering all model definitions
from models import ncsnpp, ddpm
import losses
import sampling
from models import model_utils as mutils
from models.ema import ExponentialMovingAverage
import sde_lib
from absl import flags
import torch
from torch.utils import tensorboard
from utils.utils import *
import utils.datasets as datasets
FLAGS = flags.FLAGS
def nmse(recon, label):
nmse_value = (torch.norm(recon - label) ** 2) / (torch.norm(label) ** 2)
return nmse_value.double()
def train(config, workdir):
"""Runs the training pipeline.
Args:
config: Configuration to use.
workdir: Working directory for checkpoints and TF summaries. If this
contains checkpoint training will be resumed from the latest checkpoint.
"""
# The directory for saving test results during training
sample_dir = os.path.join(workdir, "samples_in_train")
tf.io.gfile.makedirs(sample_dir)
tb_dir = os.path.join(workdir, "tensorboard")
tf.io.gfile.makedirs(tb_dir)
# Initialize model.
score_model = mutils.create_model(config)
ema = ExponentialMovingAverage(
score_model.parameters(), decay=config.model.ema_rate
)
optimizer = losses.get_optimizer(config, score_model.parameters())
state = dict(optimizer=optimizer, model=score_model, ema=ema, step=0)
# Create checkpoints directory
checkpoint_dir = os.path.join(workdir, "checkpoints")
tf.io.gfile.makedirs(checkpoint_dir)
# Resume training when intermediate checkpoints are detected
initial_step = int(state["step"])
# Build pytorch dataloader for training
train_dl = datasets.get_dataset(config, "train")
# Create data scaler and its inverse
scaler = get_data_scaler(config)
# Setup SDEs
if config.training.sde.lower() == "vpsde":
sde = sde_lib.VPSDE(config)
elif config.training.sde.lower() == "subvpsde":
sde = sde_lib.subVPSDE(config)
elif config.training.sde.lower() == "vesde":
sde = sde_lib.VESDE(config)
elif config.training.sde.lower() == "spiritsde":
sde = sde_lib.SpiritSDE(config)
else:
raise NotImplementedError(f"SDE {config.training.sde} unknown.")
# Build one-step training and evaluation functions
optimize_fn = losses.optimization_manager(config)
continuous = config.training.continuous
reduce_mean = config.training.reduce_mean
likelihood_weighting = config.training.likelihood_weighting
train_step_fn = losses.get_step_fn(
config,
sde,
train=True,
optimize_fn=optimize_fn,
reduce_mean=reduce_mean,
continuous=continuous,
likelihood_weighting=likelihood_weighting,
)
# In case there are multiple hosts (e.g., TPU pods), only log to host 0
logging.info("Starting training loop at step %d." % (initial_step,))
for epoch in range(config.training.epochs):
loss_sum = 0
for step, batch in enumerate(train_dl):
t0 = time.time()
"""make sure that the size of k0, kernel and csm are following:"""
# k0: (batch_size,coil_map,kx,ky)
# kernel: (batch_size,coil_map,coil_map,kernel_size,kernel_size)
# csm: (batch_size,coil_map,kx,ky)
k0, csm, kernel = batch
k0 = k0.to(config.device)
csm = csm.to(config.device)
kernel = kernel.to(config.device)
if config.training.sde == "vesde" and config.training.csm:
label = Emat_xyt_complex(k0, True, csm, 1)
else:
k0 = torch.permute(k0, (1, 0, 2, 3))
label = Emat_xyt_complex(k0, True, None, 1.0)
label = c2r(label).type(torch.FloatTensor).to(config.device)
label = scaler(label)
loss = train_step_fn(state, label, kernel, csm)
loss_sum += loss
param_num = sum(param.numel() for param in state["model"].parameters())
if step % 10 == 0:
print(
"Epoch",
epoch + 1,
"/",
config.training.epochs,
"Step",
step,
"loss = ",
loss.cpu().data.numpy(),
"loss mean =",
loss_sum.cpu().data.numpy() / (step + 1),
"time",
time.time() - t0,
"param_num",
param_num,
)
# Report the loss on an evaluation dataset periodically
if step % config.training.eval_freq == 0:
pass
# Save a checkpoint for every 5 epochs
if (epoch + 1) % 5 == 0:
save_checkpoint(
os.path.join(checkpoint_dir, f"checkpoint_{epoch + 1}.pth"), state
)
def sample(config, workdir):
"""Generate samples.
Args:
config: Configuration to use.
workdir: Working directory.
"""
# Initialize model
score_model = mutils.create_model(config)
optimizer = losses.get_optimizer(config, score_model.parameters())
ema = ExponentialMovingAverage(
score_model.parameters(), decay=config.model.ema_rate
)
state = dict(optimizer=optimizer, model=score_model, ema=ema, step=0)
checkpoint_dir = os.path.join(workdir, "checkpoints")
ckpt_path = os.path.join(checkpoint_dir, f"checkpoint_{config.sampling.ckpt}.pth")
state = restore_checkpoint(ckpt_path, state, device=config.device)
print("load weights:", ckpt_path)
SAMPLING_FOLDER_ID = "_".join(
[
FLAGS.config.sampling.acc,
FLAGS.config.sampling.mode,
FLAGS.config.sampling.center,
FLAGS.config.sampling.mask_type,
"ckpt",
str(config.sampling.ckpt),
FLAGS.config.sampling.predictor,
FLAGS.config.sampling.corrector,
str(config.sampling.snr),
FLAGS.config.training.sde,
str(FLAGS.config.model.eta),
str(FLAGS.config.sampling.mse),
str(FLAGS.config.sampling.corrector_mse),
]
)
# Build data pipeline
if config.sampling.mode == "example":
test_dl = datasets.get_dataset(config, "example")
else:
test_dl = datasets.get_dataset(config, "test_t1_all")
FLAGS.config.sampling.folder = os.path.join(
FLAGS.workdir, config.training.estimate_csm + "_acc" + SAMPLING_FOLDER_ID
)
tf.io.gfile.makedirs(FLAGS.config.sampling.folder)
# Create data scaler and its inverse
inverse_scaler = get_data_inverse_scaler(config)
# Setup SDEs
if config.training.sde.lower() == "vpsde":
sde = sde_lib.VPSDE(config)
sampling_eps = 1e-3
elif config.training.sde.lower() == "subvpsde":
sde = sde_lib.subVPSDE(config)
sampling_eps = 1e-3
elif config.training.sde.lower() == "vesde":
sde = sde_lib.VESDE(config)
sampling_eps = 1e-5
elif config.training.sde.lower() == "spiritsde":
sde = sde_lib.SpiritSDE(config)
sampling_eps = 1e-5 # TODO
else:
raise NotImplementedError(f"SDE {config.training.sde} unknown.")
"""Build the sampling function when sampling is enabled, number stands for the number of coil map"""
if config.training.sde == "vesde" and config.training.csm:
sampling_shape = (
1,
config.data.num_channels,
config.data.image_size,
config.data.image_size,
)
elif config.sampling.mode == "fastMRI":
sampling_shape = (
15,
config.data.num_channels,
config.data.image_size,
config.data.image_size,
)
else:
sampling_shape = (
18,
config.data.num_channels,
config.data.image_size,
config.data.image_size,
)
sampling_fn = sampling.get_sampling_fn(
config, sde, sampling_shape, inverse_scaler, sampling_eps
)
if config.sampling.mode != "prospective":
print("============no prospective mask!!!============")
mask = get_mask(config, "sample")
f = open(os.path.join(workdir, "snr_results.txt"), "a")
ssim = StructuralSimilarityIndexMeasure()
psnr = PeakSignalNoiseRatio()
for index, point in enumerate(test_dl):
print("index:", index)
k0, csm, kernel = point
k0 = k0.to(config.device)
kernel = kernel.to(config.device)
csm = csm.to(config.device)
k0 = torch.permute(k0, (1, 0, 2, 3)) # 18x1x320x320
if config.sampling.mode == "prospective":
print("============prospective mask!!!============")
mask = k0[0, 0, :, :].clone()
mask[mask != 0] = 1
mask = torch.unsqueeze(mask, 0)
mask = torch.unsqueeze(mask, 0)
if config.training.estimate_csm == "sos":
label = Emat_xyt_complex(k0, True, None, 1.0).to(config.device)
label = sos(label, dim=0).type(torch.FloatTensor)
else:
label = Emat_xyt_complex(k0.permute(1, 0, 2, 3), True, csm, 1.0).to(
config.device
)
atb = k0 * mask
if config.training.sde == "vesde" and config.training.csm:
atb = torch.permute(atb, (1, 0, 2, 3))
recon, n = sampling_fn(score_model, atb, kernel, mask, csm)
recon = r2c(recon)
if config.training.estimate_csm == "sos":
recon = sos(recon, dim=0)
else:
if config.training.sde == "spiritsde":
recon = fft2c_2d(recon)
recon = Emat_xyt_complex(recon.permute(1, 0, 2, 3), True, csm, 1.0).to(
config.device
)
save_mat(FLAGS.config.sampling.folder, recon, "recon", index, normalize=False)
SSIM = ssim(recon.type(torch.FloatTensor), label.type(torch.FloatTensor))
PSNR = psnr(recon.type(torch.FloatTensor), label.type(torch.FloatTensor))
NMSE = nmse(recon.type(torch.FloatTensor), label.type(torch.FloatTensor))
print("PSNR:", PSNR, "SSIM:", SSIM, "NMSE:", NMSE)
print(FLAGS.config.sampling.folder)
f.write(
"eta="
+ str(FLAGS.config.model.eta)
+ ", mse="
+ str(FLAGS.config.sampling.mse)
+ ", corrector_mse="
+ str(FLAGS.config.sampling.corrector_mse)
+ ", snr="
+ str(FLAGS.config.sampling.snr)
+ ": PSNR = "
+ str(PSNR)
+ ", SSIM = "
+ str(SSIM)
+ "\n"
)
f.write(
"-----------------------------------------------------------------------------------\n"
)
f.close()