-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathtrain.py
executable file
·163 lines (124 loc) · 4.55 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
import os
import torch
from torch import nn, optim
from torch.utils import data
from torchvision import transforms
from tensorfn import load_arg_config, load_wandb
from tensorfn import distributed as dist
from tensorfn.optim import lr_scheduler
from tqdm import tqdm
from model import UNet
from diffusion import GaussianDiffusion, make_beta_schedule
from dataset import MultiResolutionDataset
from config import DiffusionConfig
def sample_data(loader):
loader_iter = iter(loader)
epoch = 0
while True:
try:
yield epoch, next(loader_iter)
except StopIteration:
epoch += 1
loader_iter = iter(loader)
yield epoch, next(loader_iter)
def accumulate(model1, model2, decay=0.9999):
par1 = dict(model1.named_parameters())
par2 = dict(model2.named_parameters())
for k in par1.keys():
par1[k].data.mul_(decay).add_(par2[k].data, alpha=1 - decay)
def train(conf, loader, model, ema, diffusion, optimizer, scheduler, device, wandb):
loader = sample_data(loader)
pbar = range(conf.training.n_iter + 1)
if dist.is_primary():
pbar = tqdm(pbar, dynamic_ncols=True)
for i in pbar:
epoch, img = next(loader)
img = img.to(device)
time = torch.randint(
0,
conf.diffusion.beta_schedule["n_timestep"],
(img.shape[0],),
device=device,
)
loss = diffusion.p_loss(model, img, time)
optimizer.zero_grad()
loss.backward()
nn.utils.clip_grad_norm_(model.parameters(), 1)
scheduler.step()
optimizer.step()
accumulate(
ema, model.module, 0 if i < conf.training.scheduler.warmup else 0.9999
)
if dist.is_primary():
lr = optimizer.param_groups[0]["lr"]
pbar.set_description(
f"epoch: {epoch}; loss: {loss.item():.4f}; lr: {lr:.5f}"
)
if wandb is not None and i % conf.evaluate.log_every == 0:
wandb.log({"epoch": epoch, "loss": loss.item(), "lr": lr}, step=i)
if i % conf.evaluate.save_every == 0:
if conf.distributed:
model_module = model.module
else:
model_module = model
torch.save(
{
"model": model_module.state_dict(),
"ema": ema.state_dict(),
"scheduler": scheduler.state_dict(),
"optimizer": optimizer.state_dict(),
"conf": conf,
},
f"checkpoint/diffusion_{str(i).zfill(6)}.pt",
)
def main(conf):
wandb = None
if dist.is_primary() and conf.evaluate.wandb:
wandb = load_wandb()
wandb.init(project="denoising diffusion")
device = "cuda"
beta_schedule = "linear"
conf.distributed = dist.get_world_size() > 1
transform = transforms.Compose(
[
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5), inplace=True),
]
)
train_set = MultiResolutionDataset(
conf.dataset.path, transform, conf.dataset.resolution
)
train_sampler = dist.data_sampler(
train_set, shuffle=True, distributed=conf.distributed
)
train_loader = conf.training.dataloader.make(train_set, sampler=train_sampler)
model = conf.model.make()
model = model.to(device)
ema = conf.model.make()
ema = ema.to(device)
if conf.distributed:
model = nn.parallel.DistributedDataParallel(
model,
device_ids=[dist.get_local_rank()],
output_device=dist.get_local_rank(),
)
optimizer = conf.training.optimizer.make(model.parameters())
scheduler = conf.training.scheduler.make(optimizer)
if conf.ckpt is not None:
ckpt = torch.load(conf.ckpt, map_location=lambda storage, loc: storage)
if conf.distributed:
model.module.load_state_dict(ckpt["model"])
else:
model.load_state_dict(ckpt["model"])
ema.load_state_dict(ckpt["ema"])
betas = conf.diffusion.beta_schedule.make()
diffusion = GaussianDiffusion(betas).to(device)
train(
conf, train_loader, model, ema, diffusion, optimizer, scheduler, device, wandb
)
if __name__ == "__main__":
conf = load_arg_config(DiffusionConfig)
dist.launch(
main, conf.n_gpu, conf.n_machine, conf.machine_rank, conf.dist_url, args=(conf,)
)