-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
262 lines (216 loc) · 8.44 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
# References:
# https://medium.com/mlearning-ai/enerating-images-with-ddpms-a-pytorch-implementation-cef5a2ba8cb1
# https://colab.research.google.com/github/huggingface/notebooks/blob/main/diffusers/training_example.ipynb#scrollTo=e3eb5811-c10b-4dae-a58d-9583c42e7f57
# https://github.com/tcapelle/Diffusion-Models-pytorch/blob/main/modules.py
import torch
from torch.optim import AdamW
import gc
import argparse
from pathlib import Path
import math
from time import time
from tqdm import tqdm
from timm.scheduler import CosineLRScheduler
from copy import deepcopy
import wandb
from utils import (
set_seed,
get_device,
get_grad_scaler,
get_elapsed_time,
modify_state_dict,
print_n_params,
image_to_grid,
save_image,
)
from data import get_train_and_val_dls
from unet import UNet
from ddpm import DDPM
def get_args():
parser = argparse.ArgumentParser()
parser.add_argument("--data_dir", type=str, required=True)
parser.add_argument("--save_dir", type=str, required=True)
parser.add_argument("--n_epochs", type=int, required=True)
parser.add_argument("--batch_size", type=int, required=True)
parser.add_argument("--lr", type=float, required=True)
parser.add_argument("--n_cpus", type=int, required=True)
parser.add_argument("--n_warmup_steps", type=int, required=True)
parser.add_argument("--img_size", type=int, required=True)
parser.add_argument("--seed", type=int, default=223, required=False)
args = parser.parse_args()
args_dict = vars(args)
new_args_dict = dict()
for k, v in args_dict.items():
new_args_dict[k.upper()] = v
args = argparse.Namespace(**new_args_dict)
return args
class EMA:
def __init__(self, weight, model):
super().__init__()
self.weight = weight
self.ema_model = deepcopy(model)
self.ema_model.eval()
self.ema_model.requires_grad_(False)
self.cur_step = 0
def _reset_model_prams(self, cur_model):
self.ema_model.load_state_dict(cur_model.state_dict())
def _get_ema(self, x, y):
if x is None:
return y
return self.weight * x + (1 - self.weight) * y
def _update_model_params(self, cur_model):
for cur_param, ema_param in zip(cur_model.parameters(), self.ema_model.parameters()):
ema_param.data = self._get_ema(ema_param.data, cur_param.data)
def step(self, cur_model, start_step=2000):
if self.cur_step < start_step:
self._reset_model_prams(cur_model)
else:
self._update_model_params(cur_model)
self.cur_step += 1
class Trainer(object):
def __init__(self, train_dl, val_dl, save_dir, device):
self.train_dl = train_dl
self.val_dl = val_dl
self.save_dir = Path(save_dir)
self.device = device
self.run = wandb.init(project="DDPM")
self.ckpt_path = self.save_dir/"ckpt.pth"
def train_for_one_epoch(self, epoch, model, optim, scaler):
train_loss = 0
pbar = tqdm(self.train_dl, leave=False)
for step_idx, ori_image in enumerate(pbar): # "$x_{0} \sim q(x_{0})$"
pbar.set_description("Training...")
ori_image = ori_image.to(self.device)
loss = model.get_loss(ori_image)
train_loss += (loss.item() / len(self.train_dl))
optim.zero_grad()
if scaler is not None:
scaler.scale(loss).backward()
scaler.step(optim)
scaler.update()
else:
loss.backward()
optim.step()
# self.ema.step(cur_model=model)
self.scheduler.step((epoch - 1) * len(self.train_dl) + step_idx)
return train_loss
@torch.inference_mode()
def validate(self, model):
val_loss = 0
pbar = tqdm(self.val_dl, leave=False)
for ori_image in pbar:
pbar.set_description("Validating...")
ori_image = ori_image.to(self.device)
loss = model.get_loss(ori_image.detach())
val_loss += (loss.item() / len(self.val_dl))
return val_loss
@staticmethod
def save_model_params(model, save_path):
Path(save_path).parent.mkdir(parents=True, exist_ok=True)
torch.save(modify_state_dict(model.state_dict()), str(save_path))
print(f"Saved model params as '{str(save_path)}'.")
def save_ckpt(self, epoch, model, optim, min_val_loss, scaler):
self.ckpt_path.parent.mkdir(parents=True, exist_ok=True)
ckpt = {
"epoch": epoch,
"model": modify_state_dict(model.state_dict()),
"optimizer": optim.state_dict(),
"min_val_loss": min_val_loss,
}
if scaler is not None:
ckpt["scaler"] = scaler.state_dict()
torch.save(ckpt, str(self.ckpt_path))
@torch.inference_mode()
def test_sampling(self, epoch, model, batch_size):
gen_image = model.sample(batch_size=batch_size)
gen_grid = image_to_grid(gen_image, n_cols=int(batch_size ** 0.5))
sample_path = str(self.save_dir/f"sample-epoch={epoch}.jpg")
save_image(gen_grid, save_path=sample_path)
wandb.log({"Samples": wandb.Image(sample_path)}, step=epoch)
def train(self, n_epochs, model, optim, scaler, n_warmup_steps):
for param in model.parameters():
try:
param.register_hook(lambda grad: torch.clip(grad, -1, 1))
except Exception:
continue
model = torch.compile(model)
# self.ema = EMA(weight=0.995, model=model)
self.scheduler = CosineLRScheduler(
optimizer=optim,
t_initial=n_epochs * len(self.train_dl),
warmup_t=n_warmup_steps,
warmup_lr_init=optim.param_groups[0]["lr"] * 0.1,
warmup_prefix=True,
t_in_epochs=False,
)
init_epoch = 0
min_val_loss = math.inf
for epoch in range(init_epoch + 1, n_epochs + 1):
start_time = time()
train_loss = self.train_for_one_epoch(
epoch=epoch, model=model, optim=optim, scaler=scaler,
)
# val_loss = self.validate(self.ema.ema_model)
val_loss = self.validate(model)
if val_loss < min_val_loss:
model_params_path = str(self.save_dir/f"epoch={epoch}-val_loss={val_loss:.4f}.pth")
# self.save_model_params(model=self.ema.ema_model, save_path=model_params_path)
self.save_model_params(model=model, save_path=model_params_path)
min_val_loss = val_loss
self.save_ckpt(
epoch=epoch,
# model=self.ema.ema_model,
model=model,
optim=optim,
min_val_loss=min_val_loss,
scaler=scaler,
)
# self.test_sampling(epoch=epoch, model=self.ema.ema_model, batch_size=16)
self.test_sampling(epoch=epoch, model=model, batch_size=16)
log = f"[ {get_elapsed_time(start_time)} ]"
log += f"[ {epoch}/{n_epochs} ]"
log += f"[ Train loss: {train_loss:.4f} ]"
log += f"[ Val loss: {val_loss:.4f} | Best: {min_val_loss:.4f} ]"
print(log)
wandb.log(
{"Train loss": train_loss, "Val loss": val_loss, "Min val loss": min_val_loss},
step=epoch,
)
def main():
torch.set_printoptions(linewidth=70)
DEVICE = get_device()
args = get_args()
set_seed(args.SEED)
print(f"[ DEVICE: {DEVICE} ]")
gc.collect()
if DEVICE.type == "cuda":
torch.cuda.empty_cache()
torch.cuda.reset_peak_memory_stats()
torch.cuda.synchronize()
train_dl, val_dl = get_train_and_val_dls(
data_dir=args.DATA_DIR,
img_size=args.IMG_SIZE,
batch_size=args.BATCH_SIZE,
n_cpus=args.N_CPUS,
)
trainer = Trainer(
train_dl=train_dl,
val_dl=val_dl,
save_dir=args.SAVE_DIR,
device=DEVICE,
)
net = UNet()
model = DDPM(model=net, img_size=args.IMG_SIZE, device=DEVICE)
print_n_params(model)
# "We set the batch size to 128 for CIFAR10 and 64 for larger images."
optim = AdamW(model.parameters(), lr=args.LR)
scaler = get_grad_scaler(device=DEVICE)
trainer.train(
n_epochs=args.N_EPOCHS,
model=model,
optim=optim,
scaler=scaler,
n_warmup_steps=args.N_WARMUP_STEPS,
)
if __name__ == "__main__":
main()