-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain_t2v.py
212 lines (183 loc) · 7.95 KB
/
train_t2v.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
import argparse
import logging
import numpy as np
import os
import torch
torch.backends.cuda.matmul.allow_tf32 = True
torch.backends.cudnn.allow_tf32 = True
from accelerate import Accelerator
from accelerate.utils import set_seed
from collections import OrderedDict
from copy import deepcopy
from diffusers.models import AutoencoderKL
from einops import rearrange, repeat
from glob import glob
from PIL import Image
from time import time
from torch.utils.data import DataLoader
from torchvision import transforms
from torchvision.datasets import ImageFolder
from transformers import AutoConfig, AutoTokenizer, AutoModelForSeq2SeqLM, CLIPTextModel
from data.msrvtt import MSRVTT
from data.webvid import WebVid
from diffusion import create_diffusion
from models import GenTron_models
@torch.no_grad()
def update_ema(ema_model, model, decay=0.9999):
ema_params = OrderedDict(ema_model.named_parameters())
model_params = OrderedDict(model.named_parameters())
for name, param in model_params.items():
name = name.replace("module.", "")
ema_params[name].mul_(decay).add_(param.data, alpha=1 - decay)
def requires_grad(model, flag=True):
for p in model.parameters():
p.requires_grad = flag
def create_logger(logging_dir):
logging.basicConfig(
level=logging.INFO,
format='[\033[34m%(asctime)s\033[0m] %(message)s',
datefmt='%Y-%m-%d %H:%M:%S',
handlers=[logging.StreamHandler(), logging.FileHandler(f"{logging_dir}/log.txt")]
)
logger = logging.getLogger(__name__)
return logger
def parse_args(input_args=None):
parser = argparse.ArgumentParser()
parser.add_argument("--meta_path", type=str, required=True)
parser.add_argument("--data_dir", type=str, required=True)
parser.add_argument("--results_dir", type=str, default="results")
parser.add_argument("--model", type=str, choices=list(GenTron_models.keys()), default="GenTron-T2V-XL/2")
parser.add_argument("--image_size", type=int, choices=[256, 512], default=512)
parser.add_argument("--video_length", type=int, default=8)
parser.add_argument("--frame_stride", type=int, default=6)
parser.add_argument("--num_classes", type=int, default=1000)
parser.add_argument("--epochs", type=int, default=1400)
parser.add_argument("--batch_size", type=int, default=4)
parser.add_argument("--seed", type=int, default=42)
parser.add_argument("--vae", type=str, default="stabilityai/sd-vae-ft-ema")
parser.add_argument("--text_encoder", type=str, default="openai/clip-vit-large-patch14")
parser.add_argument("--num_workers", type=int, default=0)
parser.add_argument("--log_every", type=int, default=100)
parser.add_argument("--ckpt_every", type=int, default=50_000)
if input_args is not None:
args = parser.parse_args(input_args)
else:
args = parser.parse_args()
return args
def main(args=None):
assert torch.cuda.is_available(), "Training currently requires at least one GPU."
accelerator = Accelerator()
device = accelerator.device
set_seed(args.seed)
if accelerator.is_main_process:
os.makedirs(args.results_dir, exist_ok=True)
experiment_index = len(glob(f"{args.results_dir}/*"))
model_string_name = args.model.replace("/", "-")
experiment_dir = f"{args.results_dir}/{experiment_index:03d}-{model_string_name}"
checkpoint_dir = f"{experiment_dir}/checkpoints"
os.makedirs(checkpoint_dir, exist_ok=True)
logger = create_logger(experiment_dir)
logger.info(f"Experiment directory created at {experiment_dir}")
assert args.image_size % 8 == 0, "Image size must be divisible by 8 (for the VAE encoder)."
latent_size = args.image_size // 8
text_config = AutoConfig.from_pretrained(args.text_encoder)
embed_dim = text_config.d_model if args.model == "GenTron-T2I-G/2" else text_config.projection_dim
model = GenTron_models[args.model](
input_size=latent_size,
embedding_dim=embed_dim,
)
ema = deepcopy(model).to(device)
requires_grad(ema, False)
model = model.to(device)
diffusion = create_diffusion(timestep_respacing="")
vae = AutoencoderKL.from_pretrained(args.vae).to(device)
requires_grad(vae, False)
tokenizer = AutoTokenizer.from_pretrained(args.text_encoder)
if args.model == "GenTron-T2V-G/2":
text_encoder = AutoModelForSeq2SeqLM.from_pretrained(args.text_encoder).to(device)
else:
text_encoder = CLIPTextModel.from_pretrained(args.text_encoder).to(device)
opt = torch.optim.AdamW(model.parameters(), lr=1e-4, weight_decay=0)
dataset = WebVid(
meta_path=args.meta_path,
data_dir=args.data_dir,
video_length=args.video_length,
resolution=args.image_size,
frame_stride=6,
spatial_transform="resize_center_crop",
fps_max=4,
load_raw_resolution=True,
random_fs=True,
)
loader = DataLoader(
dataset,
batch_size=args.batch_size,
shuffle=False,
num_workers=args.num_workers,
pin_memory=True,
drop_last=True
)
if accelerator.is_main_process:
logger.info(f"Dataset contains {len(dataset):,} videos ({args.data_dir})")
update_ema(ema, model, decay=0)
model.train()
ema.eval()
model, opt, loader = accelerator.prepare(model, opt, loader)
train_steps = 0
log_steps = 0
running_loss = 0
start_time = time()
if accelerator.is_main_process:
logger.info(f"Training for {args.epochs} epochs...")
for epoch in range(args.epochs):
if accelerator.is_main_process:
logger.info(f"Beginning epoch {epoch}...")
for data in loader:
x = data["video"].to(device)
with torch.no_grad():
b, _, _, _, _ = x.shape
x = rearrange(x, "b f c h w -> (b f) c h w").contiguous()
x = vae.encode(x).latent_dist.sample().mul_(0.18215)
x = rearrange(x, "(b f) c h w -> b f c h w", b=b).contiguous()
y = data["caption"]
y_inputs = tokenizer(y, padding="max_length", max_length=tokenizer.model_max_length, return_tensors="pt")
tokens = y_inputs["input_ids"].to(device)
y = text_encoder(input_ids=tokens).last_hidden_state
mask = y_inputs["attention_mask"].bool().to(device)
t = torch.randint(0, diffusion.num_timesteps, (x.shape[0],), device=device)
model_kwargs = dict(y=y, mask=mask)
loss_dict = diffusion.training_losses(model, x, t, model_kwargs)
loss = loss_dict["loss"].mean()
opt.zero_grad()
accelerator.backward(loss)
opt.step()
update_ema(ema, model)
running_loss += loss.item()
log_steps += 1
train_steps += 1
if train_steps % args.log_every == 0:
end_time = time()
steps_per_sec = log_steps / (end_time - start_time)
avg_loss = torch.tensor(running_loss / log_steps, device=device)
avg_loss = avg_loss.item()
if accelerator.is_main_process:
logger.info(f"(step={train_steps:07d}) Train Loss: {avg_loss:.4f}, Train Steps/Sec: {steps_per_sec:.2f}")
running_loss = 0
log_steps = 0
start_time = time()
if train_steps % args.ckpt_every == 0 and train_steps > 0:
checkpoint = {
"model": model.state_dict(),
"ema": ema.state_dict(),
"opt": opt.state_dict(),
"args": args
}
checkpoint_path = f"{checkpoint_dir}/{train_steps:07d}.pt"
torch.save(checkpoint, checkpoint_path)
logger.info(f"Saved checkpoint to {checkpoint_path}")
model.eval()
if accelerator.is_main_process:
logger.info("Done!")
if __name__ == "__main__":
args = parse_args()
main(args)