-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathgenerate_t2i.py
240 lines (213 loc) · 10.3 KB
/
generate_t2i.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
# Copyright (c) Meta Platforms, Inc. and affiliates.
# All rights reserved.
# This source code is licensed under the license found in the
# LICENSE file in the root directory of this source tree.
"""
Samples a large number of images from a pre-trained SiT model using DDP.
Subsequently saves a .npz file that can be used to compute FID and other
evaluation metrics via the ADM repo: https://github.com/openai/guided-diffusion/tree/main/evaluations
For a simple single-GPU/CPU sampling script, see sample.py.
"""
import torch
import torch.distributed as dist
from models.mmdit import MMDiT
from diffusers.models import AutoencoderKL
from tqdm import tqdm
import os
from PIL import Image
import numpy as np
import math
import argparse
from sampler_t2i import euler_sampler, euler_maruyama_sampler
from utils import load_legacy_checkpoints, download_model
from dataset import MSCOCO256Features
from torch.utils.data import DataLoader
from accelerate import Accelerator
from accelerate.logging import get_logger
from accelerate.utils import ProjectConfiguration, set_seed
def create_npz_from_sample_folder(sample_dir, num=50_000):
"""
Builds a single .npz file from a folder of .png samples.
"""
samples = []
for i in tqdm(range(num), desc="Building .npz file from samples"):
sample_pil = Image.open(f"{sample_dir}/{i:06d}.png")
sample_np = np.asarray(sample_pil).astype(np.uint8)
samples.append(sample_np)
samples = np.stack(samples)
assert samples.shape == (num, samples.shape[1], samples.shape[2], 3)
npz_path = f"{sample_dir}.npz"
np.savez(npz_path, arr_0=samples)
print(f"Saved .npz file to {npz_path} [shape={samples.shape}].")
return npz_path
def main(args):
"""
Run sampling.
"""
torch.backends.cuda.matmul.allow_tf32 = args.tf32 # True: fast but may lead to some small numerical differences
assert torch.cuda.is_available(), "Sampling with DDP requires at least one GPU. sample.py supports CPU-only usage"
torch.set_grad_enabled(False)
accelerator = Accelerator(mixed_precision=None)
device = accelerator.device
if args.global_seed is not None:
set_seed(args.global_seed + accelerator.process_index)
# Load model:
block_kwargs = {"fused_attn": args.fused_attn, "qk_norm": args.qk_norm}
latent_size = args.resolution // 8
model = MMDiT(
input_size=latent_size,
z_dims = [int(z_dim) for z_dim in args.projector_embed_dims.split(',')],
encoder_depth=args.encoder_depth,
).to(device)
# Setup data:
all_dataset = MSCOCO256Features(path='../data/coco256_features', mode='val', ret_caption=True)
val_dataset = all_dataset.test
y_null = torch.from_numpy(all_dataset.empty_context).to(device).unsqueeze(0)
local_batch_size = args.per_proc_batch_size
val_dataloader = DataLoader(
val_dataset,
batch_size=local_batch_size,
shuffle=False,
num_workers=4,
pin_memory=True,
drop_last=False
)
val_dataloader = accelerator.prepare(val_dataloader)
# Auto-download a pre-trained model or load a custom SiT checkpoint from train.py:
ckpt_path = args.ckpt
state_dict = torch.load(ckpt_path, map_location="cpu")['model']
model.load_state_dict(state_dict)
model.eval() # important!
vae = AutoencoderKL.from_pretrained(f"stabilityai/sd-vae-ft-{args.vae}").to(device)
assert args.cfg_scale >= 1.0, "In almost all cases, cfg_scale be >= 1.0"
using_cfg = args.cfg_scale > 1.0
# Create folder to save samples:
if args.prefix == "":
folder_name = f"coco-size-{args.resolution}-vae-{args.vae}-" \
f"cfg-{args.cfg_scale}-seed-{args.global_seed}-{args.mode}"
else:
folder_name = f"{args.prefix}-coco-size-{args.resolution}-vae-{args.vae}-" \
f"cfg-{args.cfg_scale}-seed-{args.global_seed}-{args.mode}"
sample_folder_dir = f"{args.sample_dir}/{folder_name}"
real_sample_folder_dir = f"{args.sample_dir}/{folder_name}_real"
if accelerator.is_main_process:
os.makedirs(sample_folder_dir, exist_ok=True)
os.makedirs(real_sample_folder_dir, exist_ok=True)
print(f"Saving .png samples at {sample_folder_dir}")
dist.barrier()
# Figure out how many samples we need to generate on each GPU and how many iterations we need to run:
n = args.per_proc_batch_size
global_batch_size = n * dist.get_world_size()
# To make things evenly-divisible, we'll sample a bit more than we need and then discard the extra samples:
total_samples = 40192
if accelerator.is_main_process:
print(f"Total number of images that will be sampled: {total_samples}")
print(f"SiT Parameters: {sum(p.numel() for p in model.parameters()):,}")
print(f"projector Parameters: {sum(p.numel() for p in model.projectors.parameters()):,}")
assert total_samples % dist.get_world_size() == 0, "total_samples must be divisible by world_size"
samples_needed_this_gpu = int(total_samples // dist.get_world_size())
assert samples_needed_this_gpu % n == 0, "samples_needed_this_gpu must be divisible by the per-GPU batch size"
iterations = int(samples_needed_this_gpu // n)
pbar = range(iterations)
pbar = tqdm(pbar) if accelerator.is_main_process else pbar
total = 0
clipsim_sum = 0.
from utils import ClipSimilarity
clipsim_fn = ClipSimilarity(device=device)
for raw_image, _, context, raw_captions in val_dataloader:
# Sample inputs:
z = torch.randn(n, model.in_channels, latent_size, latent_size, device=device)
# Sample images:
sampling_kwargs = dict(
model=model,
latents=z,
y=context,
y_null=y_null.repeat(context.shape[0], 1, 1),
num_steps=args.num_steps,
heun=args.heun,
cfg_scale=args.cfg_scale,
guidance_low=args.guidance_low,
guidance_high=args.guidance_high,
path_type=args.path_type,
)
with torch.no_grad():
if args.mode == "sde":
samples = euler_maruyama_sampler(**sampling_kwargs).to(torch.float32)
elif args.mode == "ode":
samples = euler_sampler(**sampling_kwargs).to(torch.float32)
else:
raise NotImplementedError()
latents_scale = torch.tensor(
[0.18215, 0.18215, 0.18215, 0.18215, ]
).view(1, 4, 1, 1).to(device)
latents_bias = -torch.tensor(
[0., 0., 0., 0.,]
).view(1, 4, 1, 1).to(device)
samples = vae.decode((samples - latents_bias) / latents_scale).sample
samples = (samples + 1) / 2.
samples = torch.clamp(
255. * samples, 0, 255
).permute(0, 2, 3, 1).to("cpu", dtype=torch.uint8).numpy()
# real_samples = (raw_image).permute(0, 2, 3, 1).to("cpu", dtype=torch.uint8).numpy()
# Save samples to disk as individual .png files
for i, sample in enumerate(samples):
index = i * accelerator.num_processes + accelerator.local_process_index + total
Image.fromarray(sample).save(f"{sample_folder_dir}/{index:06d}.png")
# Image.fromarray(real_sample).save(f"{real_sample_folder_dir}/{index:06d}.png")
batch_clipsim = clipsim_fn(
torch.from_numpy(samples/255.).to(device).permute(0, 3, 1, 2), raw_captions
)
total += global_batch_size
gather_clipsim_sum = [
torch.zeros_like(batch_clipsim) for _ in range(4)
]
torch.distributed.all_gather(gather_clipsim_sum, batch_clipsim)
gather_clipsim_sum = torch.cat(gather_clipsim_sum).sum()
clipsim_sum += gather_clipsim_sum
if accelerator.is_main_process:
print(f"{total}: {clipsim_sum / total}")
if accelerator.is_main_process:
pbar.update(1)
# Make sure all processes have finished saving their samples before attempting to convert to .npz
dist.barrier()
if accelerator.is_main_process:
create_npz_from_sample_folder(sample_folder_dir, 40192)
# create_npz_from_sample_folder(real_sample_folder_dir, 40192)
print("Done.")
dist.barrier()
dist.destroy_process_group()
if __name__ == "__main__":
parser = argparse.ArgumentParser()
# seed
parser.add_argument("--global-seed", type=int, default=0)
# precision
parser.add_argument("--tf32", action=argparse.BooleanOptionalAction, default=True,
help="By default, use TF32 matmuls. This massively accelerates sampling on Ampere GPUs.")
# logging/saving:
parser.add_argument("--ckpt", type=str, default=None, help="Optional path to a SiT checkpoint.")
parser.add_argument("--sample-dir", type=str, default="samples")
parser.add_argument("--prefix", type=str, default="")
# model
parser.add_argument("--num-classes", type=int, default=1000)
parser.add_argument("--encoder-depth", type=int, default=8)
parser.add_argument("--resolution", type=int, choices=[256, 512], default=256)
parser.add_argument("--fused-attn", action=argparse.BooleanOptionalAction, default=False)
parser.add_argument("--qk-norm", action=argparse.BooleanOptionalAction, default=False)
# vae
parser.add_argument("--vae", type=str, choices=["ema", "mse"], default="ema")
# number of samples
parser.add_argument("--per-proc-batch-size", type=int, default=32)
parser.add_argument("--num-fid-samples", type=int, default=50_000)
# sampling related hyperparameters
parser.add_argument("--mode", type=str, default="ode")
parser.add_argument("--cfg-scale", type=float, default=1.5)
parser.add_argument("--projector-embed-dims", type=str, default="768,1024")
parser.add_argument("--path-type", type=str, default="linear", choices=["linear", "cosine"])
parser.add_argument("--num-steps", type=int, default=50)
parser.add_argument("--heun", action=argparse.BooleanOptionalAction, default=False) # only for ode
parser.add_argument("--guidance-low", type=float, default=0.)
parser.add_argument("--guidance-high", type=float, default=1.)
# will be deprecated
parser.add_argument("--legacy", action=argparse.BooleanOptionalAction, default=False) # only for ode
args = parser.parse_args()
main(args)