-
Notifications
You must be signed in to change notification settings - Fork 1
/
train.py
193 lines (160 loc) · 7.59 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
import sde
import ml_collections
import torch
from torch import multiprocessing as mp
from dataset.dataset import get_dataset
from torchvision.utils import make_grid, save_image
import utils
import einops
from torch.utils._pytree import tree_map
import accelerate
from torch.utils.data import DataLoader
from tqdm.auto import tqdm
from dpm_solver_pytorch import NoiseScheduleVP, model_wrapper, DPM_Solver
import tempfile
from absl import logging
import builtins
import os
import wandb
def train(config):
if config.get('benchmark', False):
torch.backends.cudnn.benchmark = True
torch.backends.cudnn.deterministic = False
mp.set_start_method('spawn')
accelerator = accelerate.Accelerator()
device = accelerator.device
accelerate.utils.set_seed(config.seed, device_specific=True)
logging.info(f'Process {accelerator.process_index} using device: {device}')
config.mixed_precision = accelerator.mixed_precision
config = ml_collections.FrozenConfigDict(config)
assert config.train.batch_size % accelerator.num_processes == 0
mini_batch_size = config.train.batch_size // accelerator.num_processes
if accelerator.is_main_process:
os.makedirs(config.ckpt_root, exist_ok=True)
os.makedirs(config.sample_dir, exist_ok=True)
accelerator.wait_for_everyone()
if accelerator.is_main_process:
wandb.init(dir=os.path.abspath(config.workdir), project=f'uvit_{config.dataset.name}', config=config.to_dict(),
name=config.hparams, job_type='train', mode='offline')
utils.set_logger(log_level='info', fname=os.path.join(config.workdir, 'output.log'))
logging.info(config)
else:
utils.set_logger(log_level='error')
builtins.print = lambda *args: None
dataset = get_dataset(**config.dataset)
# assert os.path.exists(dataset.fid_stat)
train_dataset = dataset.get_split(split='train', labeled=config.train.mode == 'cond')
train_dataset_loader = DataLoader(train_dataset, batch_size=mini_batch_size, shuffle=True, drop_last=True,
num_workers=8, pin_memory=True, persistent_workers=True)
train_state = utils.initialize_train_state(config, device)
nnet, nnet_ema, optimizer, train_dataset_loader = accelerator.prepare(
train_state.nnet, train_state.nnet_ema, train_state.optimizer, train_dataset_loader)
lr_scheduler = train_state.lr_scheduler
train_state.resume(config.ckpt_root)
def get_data_generator():
while True:
for data in tqdm(train_dataset_loader, disable=not accelerator.is_main_process, desc='epoch'):
yield data
data_generator = get_data_generator()
# set the score_model to train
score_model = sde.ScoreModel(nnet, pred=config.pred, sde=sde.VPSDE())
score_model_ema = sde.ScoreModel(nnet_ema, pred=config.pred, sde=sde.VPSDE())
def train_step(_batch):
_batch = [_batch[i].float() for i in range(len(_batch))]
_metrics = dict()
optimizer.zero_grad()
if config.train.mode == 'uncond':
loss = sde.LSimple(score_model, _batch, pred=config.pred)
elif config.train.mode == 'cond':
loss = sde.LSimple(score_model, _batch[0], pred=config.pred, conditions=_batch[1:])
else:
raise NotImplementedError(config.train.mode)
_metrics['loss'] = accelerator.gather(loss.detach()).mean()
accelerator.backward(loss.mean())
if 'grad_clip' in config and config.grad_clip > 0:
accelerator.clip_grad_norm_(nnet.parameters(), max_norm=config.grad_clip)
optimizer.step()
lr_scheduler.step()
train_state.ema_update(config.get('ema_rate', 0.9999))
train_state.step += 1
return dict(lr=train_state.optimizer.param_groups[0]['lr'], **_metrics)
logging.info(f'Start fitting, step={train_state.step}, mixed_precision={config.mixed_precision}')
step_fid = []
while train_state.step < config.train.n_steps:
batch = tree_map(lambda x: x.to(device), next(data_generator))
nnet.train()
metrics = train_step(batch)
nnet.eval()
if accelerator.is_main_process and train_state.step % config.train.log_interval == 0:
logging.info(utils.dct2str(dict(step=train_state.step, **metrics)))
logging.info(config.workdir)
wandb.log(metrics, step=train_state.step)
if accelerator.is_main_process and train_state.step % config.train.eval_interval == 0:
logging.info('Save a grid of images...')
x_init = torch.randn(batch[0].size(), device=device)
if config.train.mode == 'uncond':
samples = sde.euler_maruyama(sde.ODE(score_model_ema), x_init=x_init, sample_steps=50)
elif config.train.mode == 'cond':
conditions=batch[1:]
target=batch[0]
samples = sde.euler_maruyama(sde.ODE(score_model_ema), x_init=x_init, sample_steps=50, conditions=conditions)
else:
raise NotImplementedError
samples = make_grid(dataset.unpreprocess(samples), 10)
target = make_grid(dataset.unpreprocess(target), 10)
anchor = make_grid(dataset.unpreprocess(conditions[0]), 10)
save_image(samples, os.path.join(config.sample_dir, f'predict-{train_state.step}.png'))
save_image(target, os.path.join(config.sample_dir, f'gt-{train_state.step}.png'))
save_image(anchor, os.path.join(config.sample_dir, f'anchor-{train_state.step}.png'))
wandb.log({'samples': wandb.Image(samples)}, step=train_state.step)
torch.cuda.empty_cache()
accelerator.wait_for_everyone()
if train_state.step % config.train.save_interval == 0 or train_state.step == config.train.n_steps:
logging.info(f'Save and eval checkpoint {train_state.step}...')
if accelerator.local_process_index == 0:
train_state.save(os.path.join(config.ckpt_root, f'{train_state.step}.ckpt'))
accelerator.wait_for_everyone()
torch.cuda.empty_cache()
accelerator.wait_for_everyone()
logging.info(f'Finish fitting, step={train_state.step}')
accelerator.wait_for_everyone()
from absl import flags
from absl import app
from ml_collections import config_flags
import sys
from pathlib import Path
FLAGS = flags.FLAGS
config_flags.DEFINE_config_file(
"config", None, "Training configuration.", lock_config=False)
flags.mark_flags_as_required(["config"])
flags.DEFINE_string("workdir", None, "Work unit directory.")
def get_config_name():
argv = sys.argv
for i in range(1, len(argv)):
if argv[i].startswith('--config='):
return Path(argv[i].split('=')[-1]).stem
def get_hparams():
argv = sys.argv
lst = []
for i in range(1, len(argv)):
assert '=' in argv[i]
if argv[i].startswith('--config.') and not argv[i].startswith('--config.dataset.path'):
hparam, val = argv[i].split('=')
hparam = hparam.split('.')[-1]
if hparam.endswith('path'):
val = Path(val).stem
lst.append(f'{hparam}={val}')
hparams = '-'.join(lst)
if hparams == '':
hparams = 'default'
return hparams
def main(argv):
config = FLAGS.config
config.config_name = get_config_name()
config.hparams = get_hparams()
config.workdir = FLAGS.workdir or os.path.join('workdir', config.config_name, config.hparams)
config.ckpt_root = os.path.join(config.workdir, 'ckpts')
config.sample_dir = os.path.join(config.workdir, 'samples')
train(config)
if __name__ == "__main__":
app.run(main)