-
Notifications
You must be signed in to change notification settings - Fork 0
/
sample.py
185 lines (162 loc) · 6.14 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
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
import argparse
import numpy as np
import torch
import torch.nn.functional as F
from torch.distributions.categorical import Categorical
from PIL import Image
from torchvision.utils import save_image
from taming.models.vqgan import VQModel
import time
import toml
import yaml
from math import sqrt
from types import SimpleNamespace
from pathlib import Path
from omegaconf import OmegaConf
from copy import deepcopy
from ptpt.trainer import Trainer, TrainerConfig
from ptpt.log import debug, info, warning, error, critical
from ptpt.callbacks import CallbackType
from ptpt.utils import set_seed, get_parameter_count, get_device
from x_transformers import TransformerWrapper, Encoder
from hourglass_transformer_pytorch import HourglassTransformerLM
from tqdm import tqdm
from utils import decode_vqgan
def main(args):
cfg = SimpleNamespace(**toml.load(args.cfg_path))
args.steps = args.steps if args.steps else cfg.sampling["steps"]
args.temperature = (
args.temperature if args.temperature else cfg.sampling["temperature"]
)
args.end_temperature = (
args.end_temperature if args.end_temperature else args.temperature
)
args.sample_proportion = (
args.sample_proportion
if args.sample_proportion
else cfg.sampling["sample_proportion"]
)
args.end_sample_proportion = (
args.end_sample_proportion
if args.end_sample_proportion
else args.sample_proportion
)
sample_dir = Path(args.out_dir)
sample_dir.mkdir(exist_ok=False) # skip this one if it exists
seed = set_seed(args.seed)
info(f"random seed: {seed}")
device = get_device(not args.no_cuda)
cfg.net["conditional"] = (
cfg.net["conditional"] if "conditional" in cfg.net else False
)
if cfg.net["type"] in ["transformer", "vanilla"]:
net = TransformerWrapper(
num_tokens=cfg.data["vocab_size"],
max_seq_len=cfg.data["sequence_length"],
attn_layers=Encoder(
dim=cfg.net["dim"],
depth=cfg.net["depth"],
head=cfg.net["nb_heads"],
use_scalenorm=cfg.net["use_scalenorm"],
ff_glu=cfg.net["use_glu"],
rotary_pos_emb=cfg.net["use_rotary"],
attn_dropout=cfg.net["attn_dropout"],
ff_dropout=cfg.net["ff_dropout"],
),
)
elif cfg.net["type"] in ["hourglass"]:
net = HourglassTransformerLM(
num_tokens=cfg.data["vocab_size"],
dim=cfg.net["dim"],
max_seq_len=cfg.data["sequence_length"],
heads=cfg.net["nb_heads"],
shorten_factor=cfg.net["shorten_factor"],
depth=tuple(cfg.net["depth"]),
causal=False,
rotary_pos_emb=cfg.net["use_rotary"],
attn_type="vanilla",
updown_sample_type="linear",
)
else:
msg = f"unrecogized model type '{cfg.net['type']}'"
error(msg)
raise ValueError(msg)
net = net.to(device)
info(f"number of parameters: {get_parameter_count(net):,}")
vqgan_cfg = OmegaConf.load(cfg.vqgan["config"])
sd = torch.load(cfg.vqgan["checkpoint"], map_location="cpu")["state_dict"]
vqgan = VQModel(
**(
vqgan_cfg.model.params.first_stage_config.params
if args.legacy_cfg
else vqgan_cfg.model.params
)
)
vqgan_dim = (
vqgan_cfg.model.params.first_stage_config.params.embed_dim
if args.legacy_cfg
else vqgan_cfg.model.params.embed_dim
)
if args.cuda_vqgan:
vqgan = vqgan.to(device)
if args.legacy_cfg:
sd = {
k.replace("first_stage_model.", ""): v
for k, v in sd.items()
if "first_stage_model." in k
}
vqgan.load_state_dict(sd)
if args.resume:
net.load_state_dict(torch.load(args.resume)["net"])
net.eval()
count = 0
pb = tqdm(total=args.nb_samples, disable=args.no_tqdm)
while count < args.nb_samples:
sample = net.sample(
steps=args.steps,
nb_samples=args.batch_size,
temperature=args.temperature,
end_temperature=args.end_temperature,
sample_proportion=args.sample_proportion,
end_sample_proportion=args.end_sample_proportion,
min_steps=args.min_steps,
early_stop=not args.no_early_stop,
status=False,
history=False,
device=device,
)
if not args.cuda_vqgan:
sample = sample.cpu()
img = decode_vqgan(sample, vqgan, (*cfg.vqgan["latent_shape"], vqgan_dim))
for sub_img in img:
save_image(
sub_img.unsqueeze(0),
sample_dir / f"sample-{str(count+args.count_offset).zfill(6)}.png",
padding=0,
)
count += 1
pb.update(img.shape[0])
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument(
"--cfg-path", type=str, default="config/ffhq256/ffhq256-hourglass.toml"
)
parser.add_argument("--resume", type=str, default=None)
parser.add_argument("--seed", type=int, default=None)
parser.add_argument("--no-cuda", action="store_true")
parser.add_argument("--steps", type=int, default=None)
parser.add_argument("--batch-size", type=int, default=16)
parser.add_argument("--nb-samples", type=int, default=10_000)
parser.add_argument("--temperature", type=float, default=None)
parser.add_argument("--end-temperature", type=float, default=None)
parser.add_argument("--sample-proportion", type=float, default=None)
parser.add_argument("--end-sample-proportion", type=float, default=None)
parser.add_argument("--no-tqdm", action="store_true")
parser.add_argument("--min-steps", type=int, default=10)
parser.add_argument("--no-early-stop", action="store_true")
parser.add_argument("--cuda-vqgan", action="store_true")
parser.add_argument("--out-dir", type=str, default="fid-samples")
parser.add_argument("--legacy-cfg", action="store_true")
parser.add_argument("--count-offset", type=int, default=0)
args = parser.parse_args()
main(args)