-
Notifications
You must be signed in to change notification settings - Fork 1
/
train.py
333 lines (265 loc) · 8.33 KB
/
train.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
#!/usr/bin/env python
import inox
import inox.nn as nn
import jax
import numpy as np
import optax
import wandb
from datasets import Array3D, Features, concatenate_datasets, load_from_disk
from dawgz import job, schedule
from functools import partial
from tqdm import trange
from typing import *
# isort: split
from utils import *
CONFIG = {
# Data
'duplicate': 2,
# Architecture
'hid_channels': (128, 256, 384, 512),
'hid_blocks': (3, 3, 3, 3),
'kernel_size': (3, 3),
'emb_features': 256,
'heads': {3: 4},
'dropout': 0.1,
# Sampling
'sampler': 'ddpm',
'heuristic': None,
'sde': {'a': 1e-3, 'b': 1e2},
'discrete': 64,
'maxiter': 3,
# Training
'epochs': 64,
'batch_size': 256,
'scheduler': 'constant',
'lr_init': 1e-4,
'lr_end': 1e-6,
'lr_warmup': 0.0,
'optimizer': 'adam',
'weight_decay': None,
'clip': 1.0,
'ema_decay': 0.999,
}
def generate(model, dataset, rng, batch_size, **kwargs):
def transform(batch):
y, A = batch['y'], batch['A']
x = sample(model, y, A, rng.split(), **kwargs)
x = np.asarray(x)
return {'x': x}
types = {'x': Array3D(shape=(320, 320, 1), dtype='float32')}
return dataset.map(
transform,
features=Features(types),
remove_columns=['y', 'A'],
keep_in_memory=True,
batched=True,
batch_size=batch_size,
drop_last_batch=True,
)
def train(runid: int, lap: int):
run = wandb.init(
project='priors-fastmri-kspace',
id=runid,
resume='allow',
dir=PATH,
config=CONFIG,
)
runpath = PATH / f'runs/{run.name}_{run.id}'
runpath.mkdir(parents=True, exist_ok=True)
config = run.config
# Sharding
jax.config.update('jax_threefry_partitionable', True)
mesh = jax.sharding.Mesh(jax.devices(), 'i')
replicated = jax.sharding.NamedSharding(mesh, jax.sharding.PartitionSpec())
distributed = jax.sharding.NamedSharding(mesh, jax.sharding.PartitionSpec('i'))
# RNG
seed = hash((runpath, lap)) % 2**16
rng = inox.random.PRNG(seed)
# SDE
sde = VESDE(**CONFIG.get('sde'))
# Data
dataset = load_from_disk(PATH / 'hf/fastmri-kspace')
dataset.set_format('numpy')
trainset_yA = dataset['train']
trainset_yA = concatenate_datasets([trainset_yA] * config.duplicate)
testset_yA = dataset['val']
y_eval, A_eval = testset_yA[:1024:256]['y'], testset_yA[:1024:256]['A']
y_eval, A_eval = jax.device_put((y_eval, A_eval), distributed)
# Previous
if lap > 0:
previous = load_module(runpath / f'checkpoint_{lap - 1}.pkl')
else:
y_fit, A_fit = trainset_yA[:16384:4]['y'], trainset_yA[:16384:4]['A']
y_fit, A_fit = jax.device_put((y_fit, A_fit), distributed)
mu_x, cov_x = fit_moments(
features=320 * 320 * 1,
rank=64,
shard=True,
A=inox.Partial(measure, A_fit, shard=True),
y=flatten(y_fit),
cov_y=1e-2**2,
sampler='ddim',
sde=sde,
steps=256,
maxiter=5,
key=rng.split(),
)
del y_fit, A_fit
previous = GaussianDenoiser(mu_x, cov_x)
## Generate
static, arrays = previous.partition()
arrays = jax.device_put(arrays, replicated)
previous = static(arrays)
trainset = generate(
model=previous,
dataset=trainset_yA,
rng=rng,
batch_size=config.batch_size,
shard=True,
sampler=config.sampler,
sde=sde,
steps=config.discrete,
maxiter=config.maxiter,
)
testset = generate(
model=previous,
dataset=testset_yA,
rng=rng,
batch_size=config.batch_size,
shard=True,
sampler=config.sampler,
sde=sde,
steps=config.discrete,
maxiter=config.maxiter,
)
## Moments
x_fit = trainset[:16384]['x']
x_fit = flatten(x_fit)
x_fit = jax.device_put(x_fit, distributed)
mu_x, cov_x = ppca(x_fit, rank=64, key=rng.split())
del x_fit
# Model
if lap > 0:
model = previous
else:
model = make_model(key=rng.split(), **CONFIG)
model.mu_x = mu_x
if config.heuristic == 'zeros':
model.cov_x = jnp.zeros_like(mu_x)
elif config.heuristic == 'ones':
model.cov_x = jnp.ones_like(mu_x)
elif config.heuristic == 'cov_t':
model.cov_x = jnp.ones_like(mu_x) * 1e6
elif config.heuristic == 'cov_x':
model.cov_x = cov_x
model.train(True)
static, params, others = model.partition(nn.Parameter)
# Objective
objective = DenoiserLoss(sde=sde)
# Optimizer
steps = config.epochs * len(dataset) // config.batch_size
optimizer = Adam(steps=steps, **config)
opt_state = optimizer.init(params)
# EMA
ema = EMA(decay=config.ema_decay)
avrg = params
# Training
avrg, params, others, opt_state = jax.device_put((avrg, params, others, opt_state), replicated)
@jax.jit
@jax.vmap
def augment(x, key):
keys = jax.random.split(key, 2)
x = random_flip(x, keys[0], axis=-2)
x = random_shake(x, keys[1], delta=4)
return x
@jax.jit
def ell(params, others, x, key):
keys = jax.random.split(key, 3)
z = jax.random.normal(keys[0], shape=x.shape)
t = jax.random.beta(keys[1], a=3, b=3, shape=x.shape[:1])
return objective(static(params, others), x, z, t, key=keys[2])
@jax.jit
def sgd_step(avrg, params, others, opt_state, x, key):
loss, grads = jax.value_and_grad(ell)(params, others, x, key)
updates, opt_state = optimizer.update(grads, opt_state, params)
params = optax.apply_updates(params, updates)
avrg = ema(avrg, params)
return loss, avrg, params, opt_state
for epoch in (bar := trange(config.epochs, ncols=88)):
loader = trainset.shuffle(seed=seed + lap * config.epochs + epoch).iter(
batch_size=config.batch_size, drop_last_batch=True
)
losses = []
for batch in prefetch(loader):
x = batch['x']
x = jax.device_put(x, distributed)
x = augment(x, rng.split(len(x)))
x = flatten(x)
loss, avrg, params, opt_state = sgd_step(avrg, params, others, opt_state, x, key=rng.split())
losses.append(loss)
loss_train = np.stack(losses).mean()
## Validation
loader = testset.iter(batch_size=config.batch_size, drop_last_batch=True)
losses = []
for batch in prefetch(loader):
x = batch['x']
x = jax.device_put(x, distributed)
x = flatten(x)
loss = ell(avrg, others, x, key=rng.split())
losses.append(loss)
loss_val = np.stack(losses).mean()
bar.set_postfix(loss=loss_train, loss_val=loss_val)
## Eval
if (epoch + 1) % 16 == 0:
model = static(avrg, others)
model.train(False)
x = sample(
model=model,
y=y_eval,
A=A_eval,
key=rng.split(),
shard=True,
sampler=config.sampler,
steps=config.discrete,
maxiter=config.maxiter,
)
x = x.reshape(2, 2, 320, 320, 1)
run.log({
'loss': loss_train,
'loss_val': loss_val,
'samples': wandb.Image(to_pil(x)),
})
else:
run.log({
'loss': loss_train,
'loss_val': loss_val,
})
## Checkpoint
model = static(avrg, others)
model.train(False)
dump_module(model, runpath / f'checkpoint_{lap}.pkl')
if __name__ == '__main__':
runid = wandb.util.generate_id()
jobs = []
for lap in range(16):
jobs.append(
job(
partial(train, runid=runid, lap=lap),
name=f'train_{lap}',
cpus=4,
gpus=4,
ram='192GB',
time='1-00:00:00',
partition='gpu',
)
)
if len(jobs) > 1:
jobs[-1].after(jobs[-2])
schedule(
*jobs,
name=f'Training {runid}',
backend='slurm',
export='ALL',
account='ariacpg',
env=['export WANDB_SILENT=true'],
)