forked from NVlabs/edm2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate_images.py
317 lines (269 loc) · 17.7 KB
/
generate_images.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
# Copyright (c) 2024, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
#
# This work is licensed under a Creative Commons
# Attribution-NonCommercial-ShareAlike 4.0 International License.
# You should have received a copy of the license along with this
# work. If not, see http://creativecommons.org/licenses/by-nc-sa/4.0/
"""Generate random images using the given model."""
import os
import re
import warnings
import click
import tqdm
import pickle
import numpy as np
import torch
import PIL.Image
import dnnlib
from torch_utils import distributed as dist
warnings.filterwarnings('ignore', '`resume_download` is deprecated')
#----------------------------------------------------------------------------
# Configuration presets.
model_root = 'https://nvlabs-fi-cdn.nvidia.com/edm2/posthoc-reconstructions'
config_presets = {
'edm2-img512-xs-fid': dnnlib.EasyDict(net=f'{model_root}/edm2-img512-xs-2147483-0.135.pkl'), # fid = 3.53
'edm2-img512-s-fid': dnnlib.EasyDict(net=f'{model_root}/edm2-img512-s-2147483-0.130.pkl'), # fid = 2.56
'edm2-img512-m-fid': dnnlib.EasyDict(net=f'{model_root}/edm2-img512-m-2147483-0.100.pkl'), # fid = 2.25
'edm2-img512-l-fid': dnnlib.EasyDict(net=f'{model_root}/edm2-img512-l-1879048-0.085.pkl'), # fid = 2.06
'edm2-img512-xl-fid': dnnlib.EasyDict(net=f'{model_root}/edm2-img512-xl-1342177-0.085.pkl'), # fid = 1.96
'edm2-img512-xxl-fid': dnnlib.EasyDict(net=f'{model_root}/edm2-img512-xxl-0939524-0.070.pkl'), # fid = 1.91
'edm2-img64-s-fid': dnnlib.EasyDict(net=f'{model_root}/edm2-img64-s-1073741-0.075.pkl'), # fid = 1.58
'edm2-img64-m-fid': dnnlib.EasyDict(net=f'{model_root}/edm2-img64-m-2147483-0.060.pkl'), # fid = 1.43
'edm2-img64-l-fid': dnnlib.EasyDict(net=f'{model_root}/edm2-img64-l-1073741-0.040.pkl'), # fid = 1.33
'edm2-img64-xl-fid': dnnlib.EasyDict(net=f'{model_root}/edm2-img64-xl-0671088-0.040.pkl'), # fid = 1.33
'edm2-img512-xs-dino': dnnlib.EasyDict(net=f'{model_root}/edm2-img512-xs-2147483-0.200.pkl'), # fd_dinov2 = 103.39
'edm2-img512-s-dino': dnnlib.EasyDict(net=f'{model_root}/edm2-img512-s-2147483-0.190.pkl'), # fd_dinov2 = 68.64
'edm2-img512-m-dino': dnnlib.EasyDict(net=f'{model_root}/edm2-img512-m-2147483-0.155.pkl'), # fd_dinov2 = 58.44
'edm2-img512-l-dino': dnnlib.EasyDict(net=f'{model_root}/edm2-img512-l-1879048-0.155.pkl'), # fd_dinov2 = 52.25
'edm2-img512-xl-dino': dnnlib.EasyDict(net=f'{model_root}/edm2-img512-xl-1342177-0.155.pkl'), # fd_dinov2 = 45.96
'edm2-img512-xxl-dino': dnnlib.EasyDict(net=f'{model_root}/edm2-img512-xxl-0939524-0.150.pkl'), # fd_dinov2 = 42.84
'edm2-img512-xs-guid-fid': dnnlib.EasyDict(net=f'{model_root}/edm2-img512-xs-2147483-0.045.pkl', gnet=f'{model_root}/edm2-img512-xs-uncond-2147483-0.045.pkl', guidance=1.4), # fid = 2.91
'edm2-img512-s-guid-fid': dnnlib.EasyDict(net=f'{model_root}/edm2-img512-s-2147483-0.025.pkl', gnet=f'{model_root}/edm2-img512-xs-uncond-2147483-0.025.pkl', guidance=1.4), # fid = 2.23
'edm2-img512-m-guid-fid': dnnlib.EasyDict(net=f'{model_root}/edm2-img512-m-2147483-0.030.pkl', gnet=f'{model_root}/edm2-img512-xs-uncond-2147483-0.030.pkl', guidance=1.2), # fid = 2.01
'edm2-img512-l-guid-fid': dnnlib.EasyDict(net=f'{model_root}/edm2-img512-l-1879048-0.015.pkl', gnet=f'{model_root}/edm2-img512-xs-uncond-2147483-0.015.pkl', guidance=1.2), # fid = 1.88
'edm2-img512-xl-guid-fid': dnnlib.EasyDict(net=f'{model_root}/edm2-img512-xl-1342177-0.020.pkl', gnet=f'{model_root}/edm2-img512-xs-uncond-2147483-0.020.pkl', guidance=1.2), # fid = 1.85
'edm2-img512-xxl-guid-fid': dnnlib.EasyDict(net=f'{model_root}/edm2-img512-xxl-0939524-0.015.pkl', gnet=f'{model_root}/edm2-img512-xs-uncond-2147483-0.015.pkl', guidance=1.2), # fid = 1.81
'edm2-img512-xs-guid-dino': dnnlib.EasyDict(net=f'{model_root}/edm2-img512-xs-2147483-0.150.pkl', gnet=f'{model_root}/edm2-img512-xs-uncond-2147483-0.150.pkl', guidance=1.7), # fd_dinov2 = 79.94
'edm2-img512-s-guid-dino': dnnlib.EasyDict(net=f'{model_root}/edm2-img512-s-2147483-0.085.pkl', gnet=f'{model_root}/edm2-img512-xs-uncond-2147483-0.085.pkl', guidance=1.9), # fd_dinov2 = 52.32
'edm2-img512-m-guid-dino': dnnlib.EasyDict(net=f'{model_root}/edm2-img512-m-2147483-0.015.pkl', gnet=f'{model_root}/edm2-img512-xs-uncond-2147483-0.015.pkl', guidance=2.0), # fd_dinov2 = 41.98
'edm2-img512-l-guid-dino': dnnlib.EasyDict(net=f'{model_root}/edm2-img512-l-1879048-0.035.pkl', gnet=f'{model_root}/edm2-img512-xs-uncond-2147483-0.035.pkl', guidance=1.7), # fd_dinov2 = 38.20
'edm2-img512-xl-guid-dino': dnnlib.EasyDict(net=f'{model_root}/edm2-img512-xl-1342177-0.030.pkl', gnet=f'{model_root}/edm2-img512-xs-uncond-2147483-0.030.pkl', guidance=1.7), # fd_dinov2 = 35.67
'edm2-img512-xxl-guid-dino': dnnlib.EasyDict(net=f'{model_root}/edm2-img512-xxl-0939524-0.015.pkl', gnet=f'{model_root}/edm2-img512-xs-uncond-2147483-0.015.pkl', guidance=1.7), # fd_dinov2 = 33.09
}
#----------------------------------------------------------------------------
# EDM sampler from the paper
# "Elucidating the Design Space of Diffusion-Based Generative Models",
# extended to support classifier-free guidance.
def edm_sampler(
net, noise, labels=None, gnet=None,
num_steps=32, sigma_min=0.002, sigma_max=80, rho=7, guidance=1,
S_churn=0, S_min=0, S_max=float('inf'), S_noise=1,
dtype=torch.float32, randn_like=torch.randn_like,
):
# Guided denoiser.
def denoise(x, t):
Dx = net(x, t, labels).to(dtype)
if guidance == 1:
return Dx
ref_Dx = gnet(x, t).to(dtype)
return ref_Dx.lerp(Dx, guidance)
# Time step discretization.
step_indices = torch.arange(num_steps, dtype=dtype, device=noise.device)
t_steps = (sigma_max ** (1 / rho) + step_indices / (num_steps - 1) * (sigma_min ** (1 / rho) - sigma_max ** (1 / rho))) ** rho
t_steps = torch.cat([t_steps, torch.zeros_like(t_steps[:1])]) # t_N = 0
# Main sampling loop.
x_next = noise.to(dtype) * t_steps[0]
for i, (t_cur, t_next) in enumerate(zip(t_steps[:-1], t_steps[1:])): # 0, ..., N-1
x_cur = x_next
# Increase noise temporarily.
if S_churn > 0 and S_min <= t_cur <= S_max:
gamma = min(S_churn / num_steps, np.sqrt(2) - 1)
t_hat = t_cur + gamma * t_cur
x_hat = x_cur + (t_hat ** 2 - t_cur ** 2).sqrt() * S_noise * randn_like(x_cur)
else:
t_hat = t_cur
x_hat = x_cur
# Euler step.
d_cur = (x_hat - denoise(x_hat, t_hat)) / t_hat
x_next = x_hat + (t_next - t_hat) * d_cur
# Apply 2nd order correction.
if i < num_steps - 1:
d_prime = (x_next - denoise(x_next, t_next)) / t_next
x_next = x_hat + (t_next - t_hat) * (0.5 * d_cur + 0.5 * d_prime)
return x_next
#----------------------------------------------------------------------------
# Wrapper for torch.Generator that allows specifying a different random seed
# for each sample in a minibatch.
class StackedRandomGenerator:
def __init__(self, device, seeds):
super().__init__()
self.generators = [torch.Generator(device).manual_seed(int(seed) % (1 << 32)) for seed in seeds]
def randn(self, size, **kwargs):
assert size[0] == len(self.generators)
return torch.stack([torch.randn(size[1:], generator=gen, **kwargs) for gen in self.generators])
def randn_like(self, input):
return self.randn(input.shape, dtype=input.dtype, layout=input.layout, device=input.device)
def randint(self, *args, size, **kwargs):
assert size[0] == len(self.generators)
return torch.stack([torch.randint(*args, size=size[1:], generator=gen, **kwargs) for gen in self.generators])
#----------------------------------------------------------------------------
# Generate images for the given seeds in a distributed fashion.
# Returns an iterable that yields
# dnnlib.EasyDict(images, labels, noise, batch_idx, num_batches, indices, seeds)
def generate_images(
net, # Main network. Path, URL, or torch.nn.Module.
gnet = None, # Reference network for guidance. None = same as main network.
encoder = None, # Instance of training.encoders.Encoder. None = load from network pickle.
outdir = None, # Where to save the output images. None = do not save.
subdirs = False, # Create subdirectory for every 1000 seeds?
seeds = range(16, 24), # List of random seeds.
class_idx = None, # Class label. None = select randomly.
max_batch_size = 32, # Maximum batch size for the diffusion model.
encoder_batch_size = 4, # Maximum batch size for the encoder. None = default.
verbose = True, # Enable status prints?
device = torch.device('cuda'), # Which compute device to use.
sampler_fn = edm_sampler, # Which sampler function to use.
**sampler_kwargs, # Additional arguments for the sampler function.
):
# Rank 0 goes first.
if dist.get_rank() != 0:
torch.distributed.barrier()
# Load main network.
if isinstance(net, str):
if verbose:
dist.print0(f'Loading network from {net} ...')
with dnnlib.util.open_url(net, verbose=(verbose and dist.get_rank() == 0)) as f:
data = pickle.load(f)
net = data['ema'].to(device)
if encoder is None:
encoder = data.get('encoder', None)
if encoder is None:
encoder = dnnlib.util.construct_class_by_name(class_name='training.encoders.StandardRGBEncoder')
assert net is not None
# Load guidance network.
if isinstance(gnet, str):
if verbose:
dist.print0(f'Loading guidance network from {gnet} ...')
with dnnlib.util.open_url(gnet, verbose=(verbose and dist.get_rank() == 0)) as f:
gnet = pickle.load(f)['ema'].to(device)
if gnet is None:
gnet = net
# Initialize encoder.
assert encoder is not None
if verbose:
dist.print0(f'Setting up {type(encoder).__name__}...')
encoder.init(device)
if encoder_batch_size is not None and hasattr(encoder, 'batch_size'):
encoder.batch_size = encoder_batch_size
# Other ranks follow.
if dist.get_rank() == 0:
torch.distributed.barrier()
# Divide seeds into batches.
num_batches = max((len(seeds) - 1) // (max_batch_size * dist.get_world_size()) + 1, 1) * dist.get_world_size()
rank_batches = np.array_split(np.arange(len(seeds)), num_batches)[dist.get_rank() :: dist.get_world_size()]
if verbose:
dist.print0(f'Generating {len(seeds)} images...')
# Return an iterable over the batches.
class ImageIterable:
def __len__(self):
return len(rank_batches)
def __iter__(self):
# Loop over batches.
for batch_idx, indices in enumerate(rank_batches):
r = dnnlib.EasyDict(images=None, labels=None, noise=None, batch_idx=batch_idx, num_batches=len(rank_batches), indices=indices)
r.seeds = [seeds[idx] for idx in indices]
if len(r.seeds) > 0:
# Pick noise and labels.
rnd = StackedRandomGenerator(device, r.seeds)
r.noise = rnd.randn([len(r.seeds), net.img_channels, net.img_resolution, net.img_resolution], device=device)
r.labels = None
if net.label_dim > 0:
r.labels = torch.eye(net.label_dim, device=device)[rnd.randint(net.label_dim, size=[len(r.seeds)], device=device)]
if class_idx is not None:
r.labels[:, :] = 0
r.labels[:, class_idx] = 1
# Generate images.
latents = dnnlib.util.call_func_by_name(func_name=sampler_fn, net=net, noise=r.noise,
labels=r.labels, gnet=gnet, randn_like=rnd.randn_like, **sampler_kwargs)
r.images = encoder.decode(latents)
# Save images.
if outdir is not None:
for seed, image in zip(r.seeds, r.images.permute(0, 2, 3, 1).cpu().numpy()):
image_dir = os.path.join(outdir, f'{seed//1000*1000:06d}') if subdirs else outdir
os.makedirs(image_dir, exist_ok=True)
PIL.Image.fromarray(image, 'RGB').save(os.path.join(image_dir, f'{seed:06d}.png'))
# Yield results.
torch.distributed.barrier() # keep the ranks in sync
yield r
return ImageIterable()
#----------------------------------------------------------------------------
# Parse a comma separated list of numbers or ranges and return a list of ints.
# Example: '1,2,5-10' returns [1, 2, 5, 6, 7, 8, 9, 10]
def parse_int_list(s):
if isinstance(s, list):
return s
ranges = []
range_re = re.compile(r'^(\d+)-(\d+)$')
for p in s.split(','):
m = range_re.match(p)
if m:
ranges.extend(range(int(m.group(1)), int(m.group(2))+1))
else:
ranges.append(int(p))
return ranges
#----------------------------------------------------------------------------
# Command line interface.
@click.command()
@click.option('--preset', help='Configuration preset', metavar='STR', type=str, default=None)
@click.option('--net', help='Network pickle filename', metavar='PATH|URL', type=str, default=None)
@click.option('--gnet', help='Reference network for guidance', metavar='PATH|URL', type=str, default=None)
@click.option('--outdir', help='Where to save the output images', metavar='DIR', type=str, required=True)
@click.option('--subdirs', help='Create subdirectory for every 1000 seeds', is_flag=True)
@click.option('--seeds', help='List of random seeds (e.g. 1,2,5-10)', metavar='LIST', type=parse_int_list, default='16-19', show_default=True)
@click.option('--class', 'class_idx', help='Class label [default: random]', metavar='INT', type=click.IntRange(min=0), default=None)
@click.option('--batch', 'max_batch_size', help='Maximum batch size', metavar='INT', type=click.IntRange(min=1), default=32, show_default=True)
@click.option('--steps', 'num_steps', help='Number of sampling steps', metavar='INT', type=click.IntRange(min=1), default=32, show_default=True)
@click.option('--sigma_min', help='Lowest noise level', metavar='FLOAT', type=click.FloatRange(min=0, min_open=True), default=0.002, show_default=True)
@click.option('--sigma_max', help='Highest noise level', metavar='FLOAT', type=click.FloatRange(min=0, min_open=True), default=80, show_default=True)
@click.option('--rho', help='Time step exponent', metavar='FLOAT', type=click.FloatRange(min=0, min_open=True), default=7, show_default=True)
@click.option('--guidance', help='Guidance strength [default: 1; no guidance]', metavar='FLOAT', type=float, default=None)
@click.option('--S_churn', 'S_churn', help='Stochasticity strength', metavar='FLOAT', type=click.FloatRange(min=0), default=0, show_default=True)
@click.option('--S_min', 'S_min', help='Stoch. min noise level', metavar='FLOAT', type=click.FloatRange(min=0), default=0, show_default=True)
@click.option('--S_max', 'S_max', help='Stoch. max noise level', metavar='FLOAT', type=click.FloatRange(min=0), default='inf', show_default=True)
@click.option('--S_noise', 'S_noise', help='Stoch. noise inflation', metavar='FLOAT', type=float, default=1, show_default=True)
def cmdline(preset, **opts):
"""Generate random images using the given model.
Examples:
\b
# Generate a couple of images and save them as out/*.png
python generate_images.py --preset=edm2-img512-s-guid-dino --outdir=out
\b
# Generate 50000 images using 8 GPUs and save them as out/*/*.png
torchrun --standalone --nproc_per_node=8 generate_images.py \\
--preset=edm2-img64-s-fid --outdir=out --subdirs --seeds=0-49999
"""
opts = dnnlib.EasyDict(opts)
# Apply preset.
if preset is not None:
if preset not in config_presets:
raise click.ClickException(f'Invalid configuration preset "{preset}"')
for key, value in config_presets[preset].items():
if opts[key] is None:
opts[key] = value
# Validate options.
if opts.net is None:
raise click.ClickException('Please specify either --preset or --net')
if opts.guidance is None or opts.guidance == 1:
opts.guidance = 1
opts.gnet = None
elif opts.gnet is None:
raise click.ClickException('Please specify --gnet when using guidance')
# Generate.
dist.init()
image_iter = generate_images(**opts)
for _r in tqdm.tqdm(image_iter, unit='batch', disable=(dist.get_rank() != 0)):
pass
#----------------------------------------------------------------------------
if __name__ == "__main__":
cmdline()
#----------------------------------------------------------------------------