-
Notifications
You must be signed in to change notification settings - Fork 2
/
sampling_hqmodel_txt2img.py
217 lines (176 loc) · 8.4 KB
/
sampling_hqmodel_txt2img.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
# ------------------------------------------------------------------------------------
# HQ-Transformer
# Copyright (c) 2022 KakaoBrain. All Rights Reserved.
# Licensed under the MIT License [see LICENSE for details]
# ------------------------------------------------------------------------------------
import os
import sys
import argparse
import pickle
import torch
import torchvision.transforms as transforms
from torch.utils.data.dataloader import DataLoader
from tqdm import tqdm
from einops import rearrange
from omegaconf import OmegaConf
from hqvae.models import ImageGPT2
from hqvae.datasets import CC3MTextOnly
from hqvae.utils.utils import set_seed
from hqvae.utils.config2 import get_base_config
from hqvae.utils.sampling import sampling_ihqgpt, sampling_hqtransformer
parser = argparse.ArgumentParser()
parser.add_argument('-r', '--result-path', type=str, required=True)
parser.add_argument('-m', '--model-path', type=str, default='', required=True)
parser.add_argument('--top-k', type=int, default=2048, required=False)
parser.add_argument('--top-p', type=float, default=1.0, required=False)
parser.add_argument('--temperature', type=float, default=1.0, required=False)
parser.add_argument('--temperature-decay', type=float, default=1.0, required=False)
parser.add_argument('--code-level', type=int, default=2, required=False)
parser.add_argument('--batch_size', type=int, default=32, required=False)
parser.add_argument('--top-resolution', type=int, default=8, required=False)
parser.add_argument('--bot-resolution', type=int, default=16, required=False)
parser.add_argument('--seed', type=int, default=0, required=False)
parser.add_argument('--dataset', type=str, default='cc3m', choices=['cc3m'])
args = parser.parse_args()
def get_text_loader(args, config):
valid_transform = [
transforms.Resize(size=(config.dataset.image_resolution, config.dataset.image_resolution)),
transforms.ToTensor(),
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
]
if args.dataset == 'cc3m':
dataset_val = CC3MTextOnly(
split='val',
tok_name=config.dataset.tokenizer_type,
transform=valid_transform,
context_length=config.dataset.context_length,
dropout=None,
)
else:
raise NotImplementedError
loader = DataLoader(dataset_val, shuffle=False, pin_memory=True, batch_size=args.batch_size, num_workers=16)
return loader
def load_model(result_path, device='cuda'):
if 'ckpt' in result_path:
config_path = os.path.join(os.path.dirname(result_path), '..', 'config.yaml')
else:
config_path = os.path.join(result_path, 'config.yaml')
result_path = os.path.join(result_path, 'ckpt/last.ckpt')
print(result_path)
config_base = get_base_config(use_default=False)
config = OmegaConf.load(config_path)
config = OmegaConf.merge(config_base, config)
model = ImageGPT2(config)
ckpt = torch.load(result_path, map_location='cpu')['state_dict']
model.load_state_dict(ckpt, strict=True)
model.to(device=device)
return model, config
def save_pickle(fname, data):
with open(fname, 'wb') as fp:
pickle.dump(data, fp, pickle.HIGHEST_PROTOCOL)
def sampling_top(model,
cond,
top_k,
top_p,
softmax_temperature,
num_candidates,
device='cuda',
use_fp16=True,
is_tqdm=False,
top_resolution=8,
bot_resolution=16,
hilbert_order=None,
model_stage1=None):
kerH = bot_resolution // top_resolution
codes_t, codes_b = sampling_ihqgpt(model,
cond=cond,
num_candidates=num_candidates,
top_k_top=top_k,
top_p_top=top_p,
top_k_bot=top_k,
top_p_bot=top_p,
softmax_temperature=softmax_temperature,
use_fp16=use_fp16,
is_tqdm=is_tqdm,
max_seq_len=top_resolution * top_resolution,
model_stage1=model_stage1)
codes_t = rearrange(codes_t, 'B (H W) -> B H W', H=top_resolution) # [B, 16, 16]
codes_b = rearrange(codes_b, 'B (H W) (kerH kerW) -> B (H kerH) (W kerW)', H=top_resolution, kerH=kerH)
return codes_t, codes_b
def sampling_level3(model,
cond,
top_k,
top_p,
softmax_temperatures,
num_candidates,
device='cuda',
use_fp16=True,
is_tqdm=False,
resolutions=16,
hilbert_order=None,
model_stage1=None):
codes_levels = sampling_hqtransformer(model,
num_candidates=num_candidates,
cond=cond,
top_k=[top_k for i in range(0, len(resolutions))],
top_p=[top_p for i in range(0, len(resolutions))],
softmax_temperature=softmax_temperatures,
use_fp16=use_fp16,
is_tqdm=is_tqdm,
max_seq_len=resolutions[0] * resolutions[0],
model_stage1=model_stage1)
K = resolutions[0]
codes_t = rearrange(codes_levels[0], 'B (H W) -> B H W', H=K) # [B, 16, 16]
codes_m = rearrange(codes_levels[1], 'B (H W) (kerH kerW) -> B (H kerH) (W kerW)',
H=K, W=K, kerH=resolutions[1]//resolutions[0])
codes_b = rearrange(codes_levels[2], 'B (H W) (kerH kerW) -> B (H kerH) (W kerW)',
H=K, W=K, kerH=resolutions[2]//resolutions[0])
return codes_t, codes_m, codes_b
# configurations
set_seed(args.seed)
TOP_RESOLUTION = args.top_resolution
BOT_RESOLUTION = args.bot_resolution
num_batches = args.batch_size
TEMP = args.temperature
T_DECAY = args.temperature_decay
temps = [TEMP * (T_DECAY ** i) for i in range(0, args.code_level)]
# construct consecutive directiries for intermediate results
result_path = args.result_path
if not os.path.exists(result_path):
os.makedirs(result_path)
# load model
model_top, config = load_model(args.model_path)
model_top.eval()
# data loader
loader = get_text_loader(args, config)
for batch_idx, (_, txts) in tqdm(enumerate(loader)):
if (args.code_level == 2):
codes_t, codes_b = sampling_top(model_top.stage2,
cond=txts.cuda(),
top_k=args.top_k,
top_p=args.top_p,
softmax_temperature=temps,
num_candidates=1,
top_resolution=TOP_RESOLUTION,
bot_resolution=BOT_RESOLUTION,
model_stage1=model_top.stage1)
pixels = [model_top.stage1.decode_code(codes_t[i:i+1], codes_b[i:i+1]) for i in range(0, num_batches)]
pixels = torch.cat(pixels, dim=0) * 0.5 + 0.5
pixels = torch.clamp(pixels, 0, 1)
elif (args.code_level == 3):
codes = sampling_level3(model_top.stage2,
cond=txts.cuda(),
top_k=args.top_k,
top_p=args.top_p,
softmax_temperatures=temps,
num_candidates=1,
resolutions=[TOP_RESOLUTION*(2**i) for i in range(0, args.code_level)],
model_stage1=model_top.stage1)
pixels = [model_top.stage1.decode_code([codes[0][i:i+1],
codes[1][i:i+1], codes[2][i:i+1]]) for i in range(0, num_batches)]
pixels = torch.cat(pixels, dim=0) * 0.5 + 0.5
pixels = torch.clamp(pixels, 0, 1)
save_pickle(
os.path.join(result_path, f'samples_({batch_idx+1}_{num_batches}).pkl'),
pixels.cpu().numpy(),
)