-
Notifications
You must be signed in to change notification settings - Fork 19
/
sample.py
186 lines (164 loc) · 5.98 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
186
import argparse
import copy
import datetime
import os
import random
import time
import numpy as np
import torch
import torchvision
from PIL import Image
from transformers import (
AutoConfig,
AutoModel,
AutoModelForCausalLM,
AutoTokenizer,
HfArgumentParser,
set_seed,
)
from hart.modules.models.transformer import HARTForT2I
from hart.utils import default_prompts, encode_prompts, llm_system_prompt, safety_check
def save_images(sample_imgs, sample_folder_dir, store_separately, prompts):
if not store_separately and len(sample_imgs) > 1:
grid = torchvision.utils.make_grid(sample_imgs, nrow=12)
grid_np = grid.to(torch.float16).permute(1, 2, 0).mul_(255).cpu().numpy()
os.makedirs(sample_folder_dir, exist_ok=True)
grid_np = Image.fromarray(grid_np.astype(np.uint8))
grid_np.save(os.path.join(sample_folder_dir, f"sample_images.png"))
print(f"Example images are saved to {sample_folder_dir}")
else:
# bs, 3, r, r
sample_imgs_np = sample_imgs.mul_(255).cpu().numpy()
num_imgs = sample_imgs_np.shape[0]
os.makedirs(sample_folder_dir, exist_ok=True)
for img_idx in range(num_imgs):
cur_img = sample_imgs_np[img_idx]
cur_img = cur_img.transpose(1, 2, 0).astype(np.uint8)
cur_img_store = Image.fromarray(cur_img)
cur_img_store.save(os.path.join(sample_folder_dir, f"{img_idx:06d}.png"))
print(f"Image {img_idx} saved.")
with open(os.path.join(sample_folder_dir, "prompt.txt"), "w") as f:
f.write("\n".join(prompts))
def main(args):
device = torch.device("cuda")
model = AutoModel.from_pretrained(args.model_path)
model = model.to(device)
model.eval()
if args.use_ema:
ema_model = copy.deepcopy(model)
ema_model.load_state_dict(
torch.load(os.path.join(args.model_path, "ema_model.bin"))
)
text_tokenizer = AutoTokenizer.from_pretrained(args.text_model_path)
text_model = AutoModel.from_pretrained(args.text_model_path).to(device)
text_model.eval()
text_tokenizer_max_length = args.max_token_length
safety_checker_tokenizer = AutoTokenizer.from_pretrained(args.shield_model_path)
safety_checker_model = AutoModelForCausalLM.from_pretrained(
args.shield_model_path,
device_map="auto",
torch_dtype=torch.bfloat16,
).to(device)
prompts = []
if args.prompt:
prompts = [args.prompt]
elif args.prompt_list:
prompts = args.prompts
else:
print(
"No prompt is provided. Will randomly sample 4 prompts from default prompts."
)
prompts = random.sample(default_prompts, 4)
for idx, prompt in enumerate(prompts):
if safety_check.is_dangerous(
safety_checker_tokenizer, safety_checker_model, prompt
):
prompts[idx] = random.sample(default_prompts, 1)[0]
print(
f"Detected Unsafe prompt with index {idx}, will replace by one of default prompts."
)
start_time = time.time()
with torch.inference_mode():
with torch.autocast(
"cuda", enabled=True, dtype=torch.float16, cache_enabled=True
):
(
context_tokens,
context_mask,
context_position_ids,
context_tensor,
) = encode_prompts(
prompts,
text_model,
text_tokenizer,
args.max_token_length,
llm_system_prompt,
args.use_llm_system_prompt,
)
infer_func = (
ema_model.autoregressive_infer_cfg
if args.use_ema
else model.autoregressive_infer_cfg
)
output_imgs = infer_func(
B=context_tensor.size(0),
label_B=context_tensor,
cfg=args.cfg,
g_seed=args.seed,
more_smooth=args.more_smooth,
context_position_ids=context_position_ids,
context_mask=context_mask,
)
total_time = time.time() - start_time
print(f"Generate {len(prompts)} images take {total_time:2f}s.")
save_images(
output_imgs.clone(), args.sample_folder_dir, args.store_seperately, prompts
)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument(
"--model_path",
type=str,
help="The path to HART model.",
default="pretrained_models/HART-1024",
)
parser.add_argument(
"--text_model_path",
type=str,
help="The path to text model, we employ Qwen2-VL-1.5B-Instruct by default.",
default="Qwen2-VL-1.5B-Instruct",
)
parser.add_argument(
"--shield_model_path",
type=str,
help="The path to shield model, we employ ShieldGemma-2B by default.",
default="pretrained_models/shieldgemma-2b",
)
parser.add_argument("--prompt", type=str, help="A single prompt.", default="")
parser.add_argument("--prompt_list", type=list[str], default=[])
parser.add_argument("--seed", type=int, default=1)
parser.add_argument("--use_ema", type=bool, default=True)
parser.add_argument("--max_token_length", type=int, default=300)
parser.add_argument("--use_llm_system_prompt", type=bool, default=True)
parser.add_argument(
"--cfg", type=float, help="Classifier-free guidance scale.", default=4.5
)
parser.add_argument(
"--more_smooth",
type=bool,
help="Turn on for more visually smooth samples.",
default=True,
)
parser.add_argument(
"--sample_folder_dir",
type=str,
help="The folder where the image samples are stored",
default="samples/",
)
parser.add_argument(
"--store_seperately",
help="Store image samples in a grid or separately, set to False by default.",
action="store_true",
)
args = parser.parse_args()
main(args)