-
Notifications
You must be signed in to change notification settings - Fork 0
/
sample.py
90 lines (72 loc) · 3.36 KB
/
sample.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
"""
This script is a modification of work originally created by Bill Peebles, Saining Xie, and Ikko Eltociear Ashimine
Original work licensed under Creative Commons Attribution-NonCommercial 4.0 International (CC BY-NC 4.0).
Original source: https://github.com/facebookresearch/DiT
License: https://creativecommons.org/licenses/by-nc/4.0/
"""
import os
import argparse
import torch
torch.backends.cuda.matmul.allow_tf32 = True
torch.backends.cudnn.allow_tf32 = True
from torchvision.utils import save_image
from diffusion import create_diffusion
from diffusers.models import AutoencoderKL
from models.edimt import EDiMT_models
def main(args):
# Setup PyTorch:
torch.manual_seed(args.seed)
torch.set_grad_enabled(False)
device = "cuda" if torch.cuda.is_available() else "cpu"
# Load model:
latent_size = args.image_size // 8
model = EDiMT_models[args.model](
input_size=latent_size,
num_classes=args.num_classes,
).to(device)
# Load a E-DiMT checkpoint:
state_dict = torch.load(args.ckpt, map_location=lambda storage, loc: storage)
if "ema" in state_dict: # supports checkpoints from train.py
state_dict = state_dict["ema"]
model.load_state_dict(state_dict)
model.eval() # important!
diffusion = create_diffusion(str(args.num_sampling_steps))
vae = AutoencoderKL.from_pretrained(f"stabilityai/sd-vae-ft-{args.vae}").to(device)
# Labels to condition the model with (feel free to change):
class_labels = [207, 360, 387, 974, 88, 979, 417, 279]
# Create sampling noise:
n = len(class_labels)
z = torch.randn(n, 4, latent_size, latent_size, device=device)
y = torch.tensor(class_labels, device=device)
# Setup classifier-free guidance:
z = torch.cat([z, z], 0)
y_null = torch.tensor([1000] * n, device=device)
y = torch.cat([y, y_null], 0)
model_kwargs = dict(y=y, cfg_scale=args.cfg_scale)
# Sample images:
samples = diffusion.p_sample_loop(
model.forward_with_cfg, z.shape, z, clip_denoised=False, model_kwargs=model_kwargs, progress=True, device=device
)
samples, _ = samples.chunk(2, dim=0) # Remove null class samples
samples = vae.decode(samples / 0.18215).sample
# Save images
samples_path = args.ckpt.replace("checkpoints", "samples")
samples_path = os.path.splitext(samples_path)[0] + ".png"
samples_dir = os.path.dirname(samples_path)
if not os.path.exists(samples_dir):
os.makedirs(samples_dir)
save_image(samples, samples_path, nrow=4, normalize=True, value_range=(-1, 1))
# To sample from the EMA weights of a custom 256x256 EDiM-L/2 model, run:
# python sample.py --model EDiMT-L/2 --image-size 256 --ckpt /path/to/model.pt
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--model", type=str, choices=list(EDiMT_models.keys()), default="EDiMT-L/2")
parser.add_argument("--vae", type=str, choices=["ema", "mse"], default="mse")
parser.add_argument("--image-size", type=int, choices=[256, 512], default=256)
parser.add_argument("--num-classes", type=int, default=1000)
parser.add_argument("--cfg-scale", type=float, default=4.0)
parser.add_argument("--num-sampling-steps", type=int, default=250)
parser.add_argument("--seed", type=int, default=0)
parser.add_argument("--ckpt", type=str, default=None)
args = parser.parse_args()
main(args)