Dragunflie-420 commited on
Commit
9e9d22a
1 Parent(s): f2b3292

Upload sample.py

Browse files
Files changed (1) hide show
  1. sample.py +83 -0
sample.py ADDED
@@ -0,0 +1,83 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Copyright (c) Meta Platforms, Inc. and affiliates.
2
+ # All rights reserved.
3
+
4
+ # This source code is licensed under the license found in the
5
+ # LICENSE file in the root directory of this source tree.
6
+
7
+ """
8
+ Sample new images from a pre-trained DiT.
9
+ """
10
+ import torch
11
+ torch.backends.cuda.matmul.allow_tf32 = True
12
+ torch.backends.cudnn.allow_tf32 = True
13
+ from torchvision.utils import save_image
14
+ from diffusion import create_diffusion
15
+ from diffusers.models import AutoencoderKL
16
+ from download import find_model
17
+ from models import DiT_models
18
+ import argparse
19
+
20
+
21
+ def main(args):
22
+ # Setup PyTorch:
23
+ torch.manual_seed(args.seed)
24
+ torch.set_grad_enabled(False)
25
+ device = "cuda" if torch.cuda.is_available() else "cpu"
26
+
27
+ if args.ckpt is None:
28
+ assert args.model == "DiT-XL/2", "Only DiT-XL/2 models are available for auto-download."
29
+ assert args.image_size in [256, 512]
30
+ assert args.num_classes == 1000
31
+
32
+ # Load model:
33
+ latent_size = args.image_size // 8
34
+ model = DiT_models[args.model](
35
+ input_size=latent_size,
36
+ num_classes=args.num_classes
37
+ ).to(device)
38
+ # Auto-download a pre-trained model or load a custom DiT checkpoint from train.py:
39
+ ckpt_path = args.ckpt or f"DiT-XL-2-{args.image_size}x{args.image_size}.pt"
40
+ state_dict = find_model(ckpt_path)
41
+ model.load_state_dict(state_dict)
42
+ model.eval() # important!
43
+ diffusion = create_diffusion(str(args.num_sampling_steps))
44
+ vae = AutoencoderKL.from_pretrained(f"stabilityai/sd-vae-ft-{args.vae}").to(device)
45
+
46
+ # Labels to condition the model with (feel free to change):
47
+ class_labels = [207, 360, 387, 974, 88, 979, 417, 279]
48
+
49
+ # Create sampling noise:
50
+ n = len(class_labels)
51
+ z = torch.randn(n, 4, latent_size, latent_size, device=device)
52
+ y = torch.tensor(class_labels, device=device)
53
+
54
+ # Setup classifier-free guidance:
55
+ z = torch.cat([z, z], 0)
56
+ y_null = torch.tensor([1000] * n, device=device)
57
+ y = torch.cat([y, y_null], 0)
58
+ model_kwargs = dict(y=y, cfg_scale=args.cfg_scale)
59
+
60
+ # Sample images:
61
+ samples = diffusion.p_sample_loop(
62
+ model.forward_with_cfg, z.shape, z, clip_denoised=False, model_kwargs=model_kwargs, progress=True, device=device
63
+ )
64
+ samples, _ = samples.chunk(2, dim=0) # Remove null class samples
65
+ samples = vae.decode(samples / 0.18215).sample
66
+
67
+ # Save and display images:
68
+ save_image(samples, "sample.png", nrow=4, normalize=True, value_range=(-1, 1))
69
+
70
+
71
+ if __name__ == "__main__":
72
+ parser = argparse.ArgumentParser()
73
+ parser.add_argument("--model", type=str, choices=list(DiT_models.keys()), default="DiT-XL/2")
74
+ parser.add_argument("--vae", type=str, choices=["ema", "mse"], default="mse")
75
+ parser.add_argument("--image-size", type=int, choices=[256, 512], default=256)
76
+ parser.add_argument("--num-classes", type=int, default=1000)
77
+ parser.add_argument("--cfg-scale", type=float, default=4.0)
78
+ parser.add_argument("--num-sampling-steps", type=int, default=250)
79
+ parser.add_argument("--seed", type=int, default=0)
80
+ parser.add_argument("--ckpt", type=str, default=None,
81
+ help="Optional path to a DiT checkpoint (default: auto-download a pre-trained DiT-XL/2 model).")
82
+ args = parser.parse_args()
83
+ main(args)