-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
369 lines (342 loc) · 13.4 KB
/
main.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
import pandas as pd
import argparse
import tqdm
from openai import OpenAI
import json
import os
from diffusers import DiffusionPipeline
from diffusers.utils import pt_to_pil
import torch
from pathlib import Path
import urllib.request
# from ipdb import set_trace as bp
from PIL import Image
import numpy as np
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument(
"--data",
type=str,
default="prompts/targets.csv",
help="path to the prompts",
)
parser.add_argument("--model", type=str, default="dall-e-3", help="model name")
parser.add_argument("--run_id", type=int, default=1, help="which run is this?")
parser.add_argument(
"--output_dir",
type=str,
default="results/human_description",
help="path to save the data",
)
parser.add_argument(
"--neg_prompt",
type=str,
choices=[
"none",
"copyright",
"target",
"keyword-1",
"keyword-3",
"keyword-5",
"keyword-5-greedy",
"keyword-5-embedding",
"keyword-5-laion",
"keyword-5-greedy-and-laion-5",
"keyword-5-and-laion-5",
"keyword-10",
"keyword-1-noname",
"keyword-3-noname",
"keyword-5-noname",
"keyword-10-noname",
"keyword-5-greedy-noname",
"keyword-5-embedding-noname",
"keyword-5-laion-noname",
"keyword-5-greedy-and-laion-5-noname",
],
default="none",
help="Negative prompt",
)
parser.add_argument(
"--dalle_rewrite",
type=bool,
default=False,
help="whether to apply Dalle rewrite to the prompt.",
)
args = parser.parse_args()
if not os.path.exists(args.output_dir):
os.makedirs(args.output_dir)
data_name = args.data.replace(".csv", "").split("/")[-1]
print(data_name)
log_folder = f"{args.output_dir}/{args.model}/{data_name}/run-{args.run_id}/neg_prompt_{args.neg_prompt}"
target_key = "target"
prompts = pd.read_csv(args.data, encoding="unicode_escape").to_dict("records")
prompt_key = "prompt"
targets = pd.read_csv("prompts/targets.csv")["target"].values.tolist()
print(len(targets))
if args.model in ["dall-e-3"]:
client = OpenAI()
elif args.model in [
"playground-v2.5-1024px-aesthetic",
"stable-diffusion-xl-base-1.0",
"PixArt-XL-2-512x512",
"damo-vilab/text-to-video-ms-1.7b",
"IF-I-XL-v1.0",
]:
if args.model == "playground-v2.5-1024px-aesthetic":
pipe = DiffusionPipeline.from_pretrained(
"playgroundai/playground-v2.5-1024px-aesthetic",
torch_dtype=torch.float16,
variant="fp16",
).to("cuda")
elif args.model == "PixArt-XL-2-512x512":
pipe = DiffusionPipeline.from_pretrained(
"PixArt-alpha/PixArt-XL-2-512x512",
).to("cuda")
elif args.model == "stable-diffusion-xl-base-1.0":
pipe = DiffusionPipeline.from_pretrained(
"stabilityai/stable-diffusion-xl-base-1.0"
).to("cuda")
elif args.model == "damo-vilab/text-to-video-ms-1.7b":
pipe = DiffusionPipeline.from_pretrained(
"damo-vilab/text-to-video-ms-1.7b",
torch_dtype=torch.float16,
variant="fp16",
).to("cuda")
pipe.enable_model_cpu_offload()
pipe.enable_vae_slicing()
elif args.model == "IF-I-XL-v1.0":
# stage 1
stage_1 = DiffusionPipeline.from_pretrained(
"DeepFloyd/IF-I-XL-v1.0", variant="fp16", torch_dtype=torch.float16
)
stage_1.enable_xformers_memory_efficient_attention() # remove line if torch.__version__ >= 2.0.0
stage_1.enable_model_cpu_offload()
# stage 2
stage_2 = DiffusionPipeline.from_pretrained(
"DeepFloyd/IF-II-L-v1.0",
text_encoder=None,
variant="fp16",
torch_dtype=torch.float16,
)
stage_2.enable_xformers_memory_efficient_attention() # remove line if torch.__version__ >= 2.0.0
stage_2.enable_model_cpu_offload()
# stage 3
safety_modules = {
"feature_extractor": stage_1.feature_extractor,
"safety_checker": stage_1.safety_checker,
"watermarker": stage_1.watermarker,
}
stage_3 = DiffusionPipeline.from_pretrained(
"stabilityai/stable-diffusion-x4-upscaler",
**safety_modules,
torch_dtype=torch.float16,
)
stage_3.enable_xformers_memory_efficient_attention() # remove line if torch.__version__ >= 2.0.0
stage_3.enable_model_cpu_offload()
pass
res_dict = []
if "keyword" in args.neg_prompt:
if args.neg_prompt == "keyword-5-greedy":
keywords = (
pd.read_csv("prompts/keywords_lm/n=5_related_greedy.csv")
.set_index("target")
.T.to_dict("list")
)
elif args.neg_prompt == "keyword-5-embedding":
keywords = (
pd.read_csv(f"prompts/keywords_embedding/n=5.csv")
.set_index("target")
.T.to_dict("list")
)
elif args.neg_prompt == "keyword-5-laion":
keywords = (
pd.read_csv(f"prompts/keywords_co-occurrence/50keywords_laion2b_top5.csv")
.set_index("target")
.T.to_dict("list")
)
else:
num_keywords = args.neg_prompt.split("-")[1]
keywords = (
pd.read_csv(f"prompts/keywords_lm/n={num_keywords}_related_greedy.csv")
.set_index("target")
.T.to_dict("list")
)
if "and-laion-5" in args.neg_prompt:
keywords_complementary = (
pd.read_csv("prompts/keywords_co-occurrence/50keywords_laion2b_top5.csv")
.set_index("target")
.T.to_dict("list")
)
for key in keywords.keys():
if key in keywords_complementary.keys():
keywords[key][1] = (
keywords[key][1] + "," + keywords_complementary[key][1]
)
else:
print(f"Character not found: {key}")
for ip, prompt_dict in tqdm.tqdm(enumerate(prompts)):
broken_prompt = {
"prompt": prompt_dict[prompt_key],
"target": prompt_dict[target_key],
}
has_error = False
if "video" in args.model:
img_path = f"{log_folder}/images/{ip}" + "_{}.png"
else:
img_path = f"{log_folder}/images/{ip}.png"
Path(img_path).parent.mkdir(parents=True, exist_ok=True)
if args.model in ["dall-e-3"]:
try:
response = client.images.generate(
model=args.model,
prompt=broken_prompt["prompt"],
size="1024x1024",
quality="standard",
n=1,
style="vivid",
)
revised_prompt = response.data[0].revised_prompt
url = response.data[0].url
error_msg = ""
urllib.request.urlretrieve(url, img_path)
except Exception as e:
print(f"Error with prompt: {broken_prompt}")
error_msg = str(e)
has_error = True
revised_prompt = ""
url = ""
img_path = ""
elif args.model in [
"playground-v2.5-1024px-aesthetic",
"stable-diffusion-xl-base-1.0",
"PixArt-XL-2-512x512",
"damo-vilab/text-to-video-ms-1.7b",
]:
if args.neg_prompt == "target":
negative_prompt = prompt_dict[target_key]
elif args.neg_prompt == "copyright":
negative_prompt = "copyrighted character"
elif "keyword" in args.neg_prompt:
target_name = prompt_dict[target_key]
if target_name in keywords.keys():
target_description = keywords[target_name][1]
else:
target_description = ""
print(f"{target_name}: no description found")
if "noname" not in args.neg_prompt:
negative_prompt = f"{target_name}, {target_description}"
else:
negative_prompt = target_description
negative_prompt = negative_prompt.replace("\n", "").replace(
", cartoon", ""
)
print(negative_prompt)
elif args.neg_prompt == "none":
negative_prompt = ""
# check video or image generation
if args.model == "damo-vilab/text-to-video-ms-1.7b":
video_frames = pipe(broken_prompt["prompt"]).frames[0]
for frame_index in [0, 6, 15]:
first_frame = video_frames[frame_index, ...]
first_frame = (first_frame * 255).astype(np.uint8)
image = Image.fromarray(first_frame, "RGB")
image.save(img_path.format(frame_index))
else:
image = pipe(
prompt=broken_prompt["prompt"],
negative_prompt=negative_prompt,
num_inference_steps=50,
guidance_scale=3,
).images[0]
image.save(img_path)
revised_prompt = ""
url = ""
error_msg = ""
image.save(img_path)
elif args.model in [
"IF-I-XL-v1.0",
]:
if args.neg_prompt == "target":
negative_prompt = prompt_dict[target_key]
elif args.neg_prompt == "copyright":
negative_prompt = "copyrighted character"
elif "keyword" in args.neg_prompt:
target_name = prompt_dict[target_key]
if target_name in keywords.keys():
target_description = keywords[target_name][1]
else:
target_description = ""
print(f"{target_name}: no description found")
if "noname" not in args.neg_prompt:
negative_prompt = f"{target_name}, {target_description}"
else:
negative_prompt = target_description
negative_prompt = negative_prompt.replace("\n", "").replace(
", cartoon", ""
)
print(negative_prompt)
elif args.neg_prompt == "none":
negative_prompt = ""
prompt = broken_prompt["prompt"]
# text embeds
generator = torch.manual_seed(args.run_id)
if negative_prompt == "":
prompt_embeds, negative_embeds = stage_1.encode_prompt(prompt)
image = stage_1(
prompt_embeds=prompt_embeds,
negative_prompt_embeds=negative_embeds,
generator=generator,
output_type="pt",
).images
pt_to_pil(image)[0].save("./if_stage_I.png")
print("passed stage 1")
# stage 2:
image = stage_2(
image=image,
prompt_embeds=prompt_embeds,
negative_prompt_embeds=negative_embeds,
generator=generator,
output_type="pt",
).images
pt_to_pil(image)[0].save("./if_stage_II.png")
print("passed stage 2")
else:
prompt_embeds, negative_embeds = stage_1.encode_prompt(
prompt=prompt, negative_prompt=negative_prompt
)
# stage 1:
image = stage_1(
prompt_embeds=prompt_embeds,
negative_prompt_embeds=negative_embeds,
generator=generator,
output_type="pt",
).images
pt_to_pil(image)[0].save("./if_stage_I.png")
print("passed stage 1")
# stage 2:
image = stage_2(
image=image,
prompt_embeds=prompt_embeds,
negative_prompt_embeds=negative_embeds,
generator=generator,
output_type="pt",
).images
pt_to_pil(image)[0].save("./if_stage_II.png")
print("passed stage 2")
# stage 3:
image = stage_3(
prompt=prompt, image=image, generator=generator, noise_level=100
).images
image[0].save(img_path)
print("passed stage 3")
res_dict.append(
{
"target": prompt_dict[target_key],
"prompt": broken_prompt['prompt'],
"negative prompt": negative_prompt,
"image_path": img_path,
"has_error": has_error,
}
)
with open(f"{log_folder}/log.json", "w") as file:
file.write(json.dumps(res_dict, indent=4))