forked from FutureXiang/Diffusion
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cifar_guide.py
190 lines (165 loc) · 6.81 KB
/
cifar_guide.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
import argparse
import os
import torch
import torch.distributed as dist
import yaml
from torchvision import transforms
from torchvision.datasets import CIFAR10
from torchvision.utils import make_grid, save_image
from tqdm import tqdm
from ema_pytorch import EMA
from model.models import get_models_class
from utils import Config, get_optimizer, init_seeds, reduce_tensor, DataLoaderDDP
# ===== Visualize (helper functions)
def get_real_samples_grid(x, c, size, opt):
# append some real images at bottom, order by class also
x_real = torch.Tensor(size)
for k in range(opt.network['n_classes']):
sample_label_k = torch.squeeze((c == k).nonzero())
n_sample_per_class = opt.n_sample // opt.network['n_classes']
for j in range(n_sample_per_class):
try:
idx = sample_label_k[j]
except:
idx = 0
x_real[k + (j * opt.network['n_classes'])] = x[idx]
return x_real
# ===== training =====
def train(opt):
yaml_path = opt.config
local_rank = opt.local_rank
use_amp = opt.use_amp
with open(yaml_path, 'r') as f:
opt = yaml.full_load(f)
print(opt)
opt = Config(opt)
model_dir = os.path.join(opt.save_dir, "ckpts")
vis_dir = os.path.join(opt.save_dir, "visual")
if local_rank == 0:
os.makedirs(model_dir, exist_ok=True)
os.makedirs(vis_dir, exist_ok=True)
device = "cuda:%d" % local_rank
DIFFUSION, NETWORK = get_models_class(opt.model_type, opt.net_type, guide=True)
diff = DIFFUSION(nn_model=NETWORK(**opt.network),
**opt.diffusion,
device=device,
drop_prob=0.1)
diff.to(device)
if local_rank == 0:
ema = EMA(diff, beta=opt.ema, update_after_step=0, update_every=1)
ema.to(device)
diff = torch.nn.SyncBatchNorm.convert_sync_batchnorm(diff)
diff = torch.nn.parallel.DistributedDataParallel(
diff, device_ids=[local_rank], output_device=local_rank)
tf = [transforms.ToTensor()]
if opt.flip:
tf = [transforms.RandomHorizontalFlip()] + tf
tf = transforms.Compose(tf)
train_set = CIFAR10("./data", train=True, download=False, transform=tf)
print("CIFAR10 train dataset:", len(train_set))
train_loader, sampler = DataLoaderDDP(train_set,
batch_size=opt.batch_size,
shuffle=True)
lr = opt.lrate
DDP_multiplier = dist.get_world_size()
print("Using DDP, lr = %f * %d" % (lr, DDP_multiplier))
lr *= DDP_multiplier
optim = get_optimizer(diff.parameters(), opt, lr=lr)
scaler = torch.cuda.amp.GradScaler(enabled=use_amp)
if opt.load_epoch != -1:
target = os.path.join(model_dir, f"model_{opt.load_epoch}.pth")
print("loading model at", target)
checkpoint = torch.load(target, map_location=device)
diff.load_state_dict(checkpoint['MODEL'])
if local_rank == 0:
ema.load_state_dict(checkpoint['EMA'])
optim.load_state_dict(checkpoint['opt'])
for ep in range(opt.load_epoch + 1, opt.n_epoch):
for g in optim.param_groups:
g['lr'] = lr * min((ep + 1.0) / opt.warm_epoch, 1.0) # warmup
sampler.set_epoch(ep)
dist.barrier()
# training
diff.train()
if local_rank == 0:
now_lr = optim.param_groups[0]['lr']
print(f'epoch {ep}, lr {now_lr:f}')
loss_ema = None
pbar = tqdm(train_loader)
else:
pbar = train_loader
for x, c in pbar:
optim.zero_grad()
x = x.to(device)
c = c.to(device)
loss = diff(x, c, use_amp=use_amp)
scaler.scale(loss).backward()
scaler.unscale_(optim)
torch.nn.utils.clip_grad_norm_(parameters=diff.parameters(), max_norm=1.0)
scaler.step(optim)
scaler.update()
# logging
dist.barrier()
loss = reduce_tensor(loss)
if local_rank == 0:
ema.update()
if loss_ema is None:
loss_ema = loss.item()
else:
loss_ema = 0.95 * loss_ema + 0.05 * loss.item()
pbar.set_description(f"loss: {loss_ema:.4f}")
# testing
if local_rank == 0:
if ep % 50 == 0 or ep == opt.n_epoch - 1:
pass
else:
continue
if opt.model_type == 'DDPM':
raw_sample_method = diff.module.ddim_sample
ema_sample_method = ema.ema_model.ddim_sample
elif opt.model_type == 'EDM':
raw_sample_method = diff.module.edm_sample
ema_sample_method = ema.ema_model.edm_sample
diff.eval()
with torch.no_grad():
x_gen = raw_sample_method(opt.n_sample, x.shape[1:], guide_w=opt.w)
# save an image of currently generated samples (top rows)
# followed by real images (bottom rows)
x_real = get_real_samples_grid(x, c, x_gen.shape, opt)
x_all = torch.cat([x_gen.cpu(), x_real])
grid = make_grid(x_all, nrow=10)
save_path = os.path.join(vis_dir, f"image_ep{ep}_w{opt.w}.png")
save_image(grid, save_path)
print('saved image at', save_path)
ema.ema_model.eval()
with torch.no_grad():
x_gen = ema_sample_method(opt.n_sample, x.shape[1:], guide_w=opt.w)
# save an image of currently generated samples (top rows)
# followed by real images (bottom rows)
x_real = get_real_samples_grid(x, c, x_gen.shape, opt)
x_all = torch.cat([x_gen.cpu(), x_real])
grid = make_grid(x_all, nrow=10)
save_path = os.path.join(vis_dir, f"image_ep{ep}_w{opt.w}_ema.png")
save_image(grid, save_path)
print('saved image at', save_path)
# optionally save model
if opt.save_model:
checkpoint = {
'MODEL': diff.state_dict(),
'EMA': ema.state_dict(),
'opt': optim.state_dict(),
}
save_path = os.path.join(model_dir, f"model_{ep}.pth")
torch.save(checkpoint, save_path)
print('saved model at', save_path)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--config", type=str)
parser.add_argument('--local_rank', default=-1, type=int,
help='node rank for distributed training')
parser.add_argument("--use_amp", action='store_true', default=False)
opt = parser.parse_args()
init_seeds(no=opt.local_rank)
dist.init_process_group(backend='nccl')
torch.cuda.set_device(opt.local_rank)
train(opt)