-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain_tae.py
361 lines (316 loc) · 14.3 KB
/
train_tae.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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
from dataclasses import asdict
from pathlib import Path
import argparse
import random
import signal
import time
import os
from torch.nn.parallel import DistributedDataParallel as DDP
from torch.utils.data.distributed import DistributedSampler
from torch.distributed import init_process_group
from torch.utils.data import DataLoader
from PIL import Image
# import torch.distributed as dist
import numpy as np
import torchvision
import torch
import cv2
from metrics import collect_metrics
from tae import TAE, TAEConfig
from util import (
dump_dict_to_yaml, asspath, mkpath, print0, cleanup, signal_handler)
class Dataset:
def __init__(self, root: Path, T: int, size: int = 64, train: bool = True):
self.train = train
self.T = T
self.files = list()
for ext in ('*.mp4', '*.avi', '*.mov', '*.mkv', '*.webm', '*.gif'):
self.files.extend(root.glob(ext))
self.files = sorted(self.files) # Sort for consistency
self.transforms = torchvision.transforms.Compose([
torchvision.transforms.Resize((size, size)),
torchvision.transforms.PILToTensor(),
torchvision.transforms.ConvertImageDtype(torch.float32),
torchvision.transforms.Normalize([0.5], [0.5]),
])
def __len__(self):
return len(self.files)
def load_video(self, fname):
vidcap = cv2.VideoCapture(fname)
ret, x = vidcap.read()
x = Image.fromarray(cv2.cvtColor(x, cv2.COLOR_BGR2RGB))
x = self.transforms(x)[None, :]
while ret:
ret, frame = vidcap.read()
if ret:
frame = Image.fromarray(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
x = torch.cat([x, self.transforms(frame)[None, :]])
return x
def __getitem__(self, index):
mask = torch.ones([self.T])
video = self.load_video(self.files[index])
vlen = video.shape[0]
# 1:3 image/video ratio from paper
if random.random() < 0.25 and self.train:
id = random.randint(0, vlen - 1) if self.train else 0
frame = video[id]
zero_pad = torch.zeros(self.T - 1, *frame.shape,
dtype=frame.dtype)
x = torch.cat([frame[None, :], zero_pad], dim=0)
mask[1:] = 0
else:
id = 0
if self.train:
id = random.randint(0, max(vlen - self.T - 1, 0))
x = video[id:id + self.T]
if x.shape[0] < self.T:
zero_pad = torch.zeros(self.T - x.shape[0],
*x.shape[1:],
dtype=frame.dtype)
x = torch.cat([x, zero_pad], dim=0)
mask[x.shape[1]:] = 0
return x, mask
"""
-------------------------------------------------------------------------------
args
-------------------------------------------------------------------------------
"""
parser = argparse.ArgumentParser()
# io
# yes you can use parser types like this
parser.add_argument("--output-dir", type=mkpath, default="")
parser.add_argument("--train-dir", type=asspath,
default="dev/data/train-overfit")
parser.add_argument("--val-dir", type=asspath, default="dev/data/val-overfit")
# checkpointing
parser.add_argument("--ckpt", type=asspath, required=False)
parser.add_argument("--ckpt-from-ldm", type=int, default=0, choices=[0, 1])
parser.add_argument("--resume", type=int, default=0, choices=[0, 1])
parser.add_argument("--ckpt-freq", type=int, default=-1)
parser.add_argument("--device", type=str, default="cuda")
# optimization
parser.add_argument("--lr", type=float, default=1e-5)
parser.add_argument("--weight-decay", type=float, default=0.0)
parser.add_argument("--grad-clip", type=float, default=1.0)
parser.add_argument("--batch-size", type=int, default=1)
parser.add_argument("--max-frames", type=int, default=32)
parser.add_argument("--resolution", type=int, default=64)
parser.add_argument("--num-iterations", type=int, default=10)
parser.add_argument("--val-loss-every", type=int, default=0)
parser.add_argument("--val-max-steps", type=int, default=20)
parser.add_argument("--overfit-batch", default=1, type=int)
parser.add_argument("--inference-only", default=0, type=int, choices=[0, 1])
parser.add_argument("--verbose-loss", default=0, type=int, choices=[0, 1])
parser.add_argument("--seed", default=420, type=int)
# memory management
parser.add_argument("--dtype", type=str, default="float32")
parser.add_argument("--compile", default=0, type=int)
if __name__ == "__main__":
signal.signal(signal.SIGINT, signal_handler)
args = parser.parse_args()
# args error checking
assert args.dtype in {"float32"}
train_logfile, val_logfile = None, None
if args.output_dir:
train_logfile = args.output_dir / "train.log"
val_logfile = args.output_dir / "val.log"
if args.resume == 0:
open(train_logfile, 'w').close()
open(val_logfile, 'w').close()
else:
args.output_dir = mkpath("tmp")
ddp = int(os.environ.get('RANK', -1)) != -1 # is this a ddp run?
if ddp:
assert torch.cuda.is_available(), "We need CUDA for DDP"
init_process_group(backend='nccl')
ddp_rank = int(os.environ['RANK'])
ddp_local_rank = int(os.environ['LOCAL_RANK'])
ddp_world_size = int(os.environ['WORLD_SIZE'])
device = f'cuda:{ddp_local_rank}'
torch.cuda.set_device(device)
master_process = ddp_rank == 0
else:
ddp_rank = 0
ddp_local_rank = 0
ddp_world_size = 1
master_process = True
device = torch.device(args.device)
torch.manual_seed(args.seed)
if torch.cuda.is_available():
torch.cuda.manual_seed(args.seed)
config = TAEConfig()
config_dict = {'TAEConfig': asdict(config), 'CLIArgs': vars(args)}
dump_dict_to_yaml(args.output_dir / "config_dump.yaml", config_dict)
model = TAE(config)
total_params = sum(p.numel() for p in model.parameters())
trainable_params = sum(p.numel()
for p in model.parameters() if p.requires_grad)
print0(f"Total Parameters: {total_params:,}")
print0(f"Trainable Parameters: {trainable_params:,}")
if args.ckpt:
ignore_keys = list()
# The checkpoint from LDM needs to ignore certain layers
if args.ckpt_from_ldm == 1:
ignore_keys = [
"encoder.conv_out",
"decoder.conv_in",
"quant_conv",
"post_quant_conv",
]
# if args.resume == 0:
# ignore_keys.append("loss")
model.from_pretrained(args.ckpt, ignore_keys=ignore_keys)
print0(f"Loaded ckpt {args.ckpt}")
model.train()
if args.compile:
model = torch.compile(model)
model.to(device)
trainset = Dataset(args.train_dir, T=args.max_frames, size=args.resolution)
valset = Dataset(args.val_dir, T=args.max_frames,
size=args.resolution, train=False)
train_sampler = DistributedSampler(trainset, shuffle=True) if ddp else None
val_sampler = DistributedSampler(valset) if ddp else None
batch_size = args.batch_size // ddp_world_size
train_loader = DataLoader(trainset, batch_size=batch_size,
shuffle=(train_sampler is None),
num_workers=4, sampler=train_sampler)
val_loader = DataLoader(valset, batch_size=batch_size,
shuffle=False, num_workers=4, sampler=val_sampler)
if ddp:
model = DDP(model, device_ids=[ddp_local_rank],
find_unused_parameters=True)
unwrapped_model = model.module if ddp else model
optimizer_ae, optimizer_disc = unwrapped_model.configure_optimizers(
lr=args.lr, weight_decay=args.weight_decay,
betas=(0.5, 0.9))
torch.cuda.reset_peak_memory_stats()
timings = list()
if args.inference_only:
print0("Starting inference only.")
else:
print0("Starting training.")
start_step = 0
if args.resume == 1:
start_step = torch.load(args.ckpt)["step"]
trainset_size = len(trainset) // ddp_world_size if ddp else len(trainset)
for step in range(start_step, args.num_iterations + 1):
if step % trainset_size == 0:
train_iter = iter(train_loader)
if train_sampler is not None:
train_sampler.set_epoch(step % len(trainset))
t0 = time.time()
last_step = (step == args.num_iterations - 1)
# once in a while evaluate the validation dataset
if ((args.val_loss_every > 0 and step % args.val_loss_every == 0) or last_step or args.inference_only) and (val_loader is not None) and master_process: # noqa
model.eval()
with torch.no_grad():
val_loss = 0.
metrics = {
"psnr_image": list(),
"ssim_image": list(),
"psnr_video": list(),
"ssim_video": list(),
}
for vali, (x, mask) in enumerate(val_loader):
if vali >= args.val_max_steps:
break
x, mask = x.to(device), mask.to(device)
dec, _, loss, loss_dict = model(x, mask, "val", 1, step)
val_loss += loss.item()
metrics = collect_metrics(
dec.permute(0, 1, 3, 4, 2).cpu().numpy(),
x.permute(0, 1, 3, 4, 2).cpu().numpy(),
metrics)
for b in range(dec.shape[0]):
for t in range(dec.shape[1]):
fn = args.output_dir /\
f"val_dec_{vali}_{b}_{t}.png"
torchvision.transforms.ToPILImage()(
(dec[b, t] + 1) * 0.5).save(fn)
fn = args.output_dir / f"val_x_{vali}_{b}_{t}.png"
torchvision.transforms.ToPILImage()(
(x[b, t] + 1) * 0.5).save(fn)
val_loss /= args.val_max_steps
# log to console and to file
print0(f"val loss {val_loss}")
metrics = {k: np.mean(v) for k, v in metrics.items()}
x = " | ".join([f'{x}: {y:.4f}' for x, y in metrics.items()])
print0(f" val metrics: {x}")
if args.verbose_loss:
x = " | ".join([f'{x}: {y.item():.4f}' for x, y in loss_dict.items()]) # noqa
print0(f" val verbose loss: {x}")
if val_logfile is not None:
with open(val_logfile, "a") as f:
f.write(f"{step},")
f.write(f"val/loss,{val_loss},")
f.write(",".join([f'{x},{y.item():.4f}' for x, y in loss_dict.items()])) # noqa
f.write("\n")
f.write(",".join([f'{x},{y:.4f}' for x, y in metrics.items()])) # noqa
f.write("\n")
if last_step or args.inference_only:
break
model.train()
# --------------- VAE TRAINING SECTION BEGIN -----------------
optimizer_ae.zero_grad(set_to_none=True)
# fetch a batch
x, mask = next(train_iter)
x, mask = x.to(device), mask.to(device)
dec, post, loss_ae, loss_dict_ae = model(x, mask, "train", 0, step)
loss_ae.backward()
# if ddp:
# dist.all_reduce(loss_ae, op=dist.ReduceOp.AVG)
norm = torch.nn.utils.clip_grad_norm_(
model.parameters(), args.grad_clip)
# step the optimizer
optimizer_ae.step()
# --------------- DISC TRAINING SECTION BEGIN -----------------
optimizer_disc.zero_grad(set_to_none=True)
# fetch a batch
# REVISIT: should I be getting the next batch here?
# x, mask = train_loader.next_batch()
# x, mask = x.to(device), mask.to(device)
dec, post, loss_disc, loss_dict_disc = model(x, mask, "train", 1, step)
loss_disc.backward()
# # if ddp:
# # dist.all_reduce(loss_disc, op=dist.ReduceOp.AVG)
norm = torch.nn.utils.clip_grad_norm_(
model.parameters(), args.grad_clip)
# step the optimizer
optimizer_disc.step()
# --------------- TRAINING SECTION END -------------------
torch.cuda.synchronize()
t1 = time.time()
print0(f"""step {step+1:4d}/{args.num_iterations} | \
train ae loss {loss_ae.item():.6f} | \
train disc loss {loss_disc.item():.6f} | \
norm {norm:.4f} | \
""")
if args.verbose_loss:
x = " | ".join([f'{x}: {y.item():.4f}' for x, y in loss_dict_ae.items()]) # noqa
print0(f" ae verbose loss: {x}")
x = " | ".join([f'{x}: {y.item():.4f}' for x, y in loss_dict_disc.items()]) # noqa
print0(f" disc verbose loss: {x}")
if master_process and train_logfile is not None:
with open(train_logfile, "a") as f:
f.write(f"{step},")
f.write(f"train/loss_ae,{loss_ae.item()},")
f.write(f"train/loss_disc,{loss_disc.item()},")
f.write(",".join([f'{x},{y.item():.4f}' for x, y in loss_dict_ae.items()])) # noqa
f.write(",")
f.write(",".join([f'{x},{y.item():.4f}' for x, y in loss_dict_disc.items()])) # noqa
f.write("\n")
# keep track of smooth timings, last 20 iterations
if step > 0 and step > args.num_iterations - 20:
timings.append(t1 - t0)
if master_process and step > 0 and args.ckpt_freq > 0 and step % args.ckpt_freq == 0:
torch.save({"step": step, "state_dict": unwrapped_model.state_dict()},
args.output_dir / f"tae_{step}.ckpt")
# print the average of the last 20 timings, to get something smooth-ish
timings = timings[-20:]
print0(f"final {len(timings)} iters avg: {np.mean(timings)*1000:.3f}ms")
print0(f"peak memory consumption: {torch.cuda.max_memory_allocated() // 1024 // 1024} MiB") # NOQA
if master_process:
torch.save({"step": step, "state_dict": model.state_dict()},
args.output_dir / "tae_last.ckpt")
cleanup()
# -------------------------------------------------------------------------