patrickvonplaten
commited on
Commit
•
452abeb
1
Parent(s):
01ee377
up
Browse files- parti_prompts.py +24 -19
parti_prompts.py
CHANGED
@@ -1,6 +1,7 @@
|
|
1 |
#!/usr/bin/env python3
|
2 |
from diffusers import DiffusionPipeline, DDIMScheduler
|
3 |
import argparse
|
|
|
4 |
import torch
|
5 |
from datasets import load_dataset
|
6 |
import PIL
|
@@ -12,12 +13,12 @@ def resize(image: PIL.Image):
|
|
12 |
return image.resize(IMAGE_OUTPUT_SIZE, resample=PIL.Image.Resampling.LANCZOS)
|
13 |
|
14 |
def get_sd_eval(ckpt, guidance_scale=7.5):
|
15 |
-
pipe = DiffusionPipeline.from_pretrained(ckpt, torch_dtype=torch.float16)
|
16 |
pipe.to("cuda")
|
17 |
-
pipe.scheduler = DDIMScheduler.from_config(pipe.config)
|
18 |
|
19 |
-
def sd_eval(prompt):
|
20 |
-
images = pipe(prompt, num_inference_steps=
|
21 |
images = [resize(image) for image in images]
|
22 |
return images
|
23 |
|
@@ -28,28 +29,28 @@ def get_karlo_eval(ckpt):
|
|
28 |
pipe.to("cuda")
|
29 |
|
30 |
def karlo_eval(prompt):
|
31 |
-
images = pipe(prompt, prior_num_inference_steps=50, decoder_num_inference_steps=
|
32 |
return images
|
33 |
|
34 |
return karlo_eval
|
35 |
|
36 |
def get_if_eval(ckpt):
|
37 |
-
pipe_low = DiffusionPipeline.from_pretrained(ckpt, torch_dtype=torch.float16)
|
38 |
pipe_low.enable_model_cpu_offload()
|
39 |
|
40 |
-
pipe_up = DiffusionPipeline.from_pretrained("DeepFloyd/IF-II-L-v1.0", text_encoder=pipe_low.text_encoder, torch_dtype=torch.float16)
|
41 |
pipe_up.enable_model_cpu_offload()
|
42 |
|
43 |
-
def
|
44 |
-
images = pipe_low(prompt, num_inference_steps=
|
45 |
-
images = pipe_up(promtp=prompt, images=images, num_inference_steps=
|
46 |
return images
|
47 |
|
48 |
-
return
|
49 |
|
50 |
MODELS = {
|
51 |
"runwayml/stable-diffusion-v1-5": get_sd_eval,
|
52 |
-
"stabilityai/stable-diffusion-
|
53 |
"kakaobrain/karlo-alpha": get_karlo_eval,
|
54 |
"DeepFloyd/IF-I-XL-v1.0": get_if_eval,
|
55 |
}
|
@@ -59,24 +60,28 @@ MODELS = {
|
|
59 |
|
60 |
if __name__ == "__main__":
|
61 |
parser = argparse.ArgumentParser(description='Run Parti Prompt Evaluation')
|
62 |
-
parser.add_argument('model_repo_or_id', type=str, help='ID or URL of the model repository.'
|
63 |
parser.add_argument('--dataset_repo_or_id', type=str, default='diffusers/prompt_generations', help='ID or URL of the dataset repository (default: "diffusers/prompt_generations")')
|
64 |
parser.add_argument('--batch_size', type=int, default=8, help="Batch size for the eval function")
|
65 |
parser.add_argument('--upload_to_hub', action='store_true', help='whether to upload the dataset to the Hugging Face dataset hub')
|
|
|
66 |
|
67 |
args = parser.parse_args()
|
68 |
|
69 |
-
|
70 |
|
71 |
-
|
72 |
|
73 |
def map_fn(batch):
|
74 |
-
|
|
|
|
|
|
|
75 |
return batch
|
76 |
|
77 |
-
dataset_images = dataset.map(map_fn, batched=True, batch_size=
|
78 |
|
79 |
if args.upload_to_hub:
|
80 |
-
|
81 |
else:
|
82 |
-
|
|
|
1 |
#!/usr/bin/env python3
|
2 |
from diffusers import DiffusionPipeline, DDIMScheduler
|
3 |
import argparse
|
4 |
+
from diffusers.pipelines.stable_diffusion import safety_checker
|
5 |
import torch
|
6 |
from datasets import load_dataset
|
7 |
import PIL
|
|
|
13 |
return image.resize(IMAGE_OUTPUT_SIZE, resample=PIL.Image.Resampling.LANCZOS)
|
14 |
|
15 |
def get_sd_eval(ckpt, guidance_scale=7.5):
|
16 |
+
pipe = DiffusionPipeline.from_pretrained(ckpt, torch_dtype=torch.float16, safety_checker=None)
|
17 |
pipe.to("cuda")
|
18 |
+
pipe.scheduler = DDIMScheduler.from_config(pipe.scheduler.config)
|
19 |
|
20 |
+
def sd_eval(prompt, generator=None):
|
21 |
+
images = pipe(prompt, generator=generator, num_inference_steps=NUM_INFERENCE_STEPS, guidance_scale=guidance_scale).images
|
22 |
images = [resize(image) for image in images]
|
23 |
return images
|
24 |
|
|
|
29 |
pipe.to("cuda")
|
30 |
|
31 |
def karlo_eval(prompt):
|
32 |
+
images = pipe(prompt, prior_num_inference_steps=50, decoder_num_inference_steps=NUM_INFERENCE_STEPS).images
|
33 |
return images
|
34 |
|
35 |
return karlo_eval
|
36 |
|
37 |
def get_if_eval(ckpt):
|
38 |
+
pipe_low = DiffusionPipeline.from_pretrained(ckpt, safety_checker=None, torch_dtype=torch.float16)
|
39 |
pipe_low.enable_model_cpu_offload()
|
40 |
|
41 |
+
pipe_up = DiffusionPipeline.from_pretrained("DeepFloyd/IF-II-L-v1.0", safety_checker=None, text_encoder=pipe_low.text_encoder, torch_dtype=torch.float16)
|
42 |
pipe_up.enable_model_cpu_offload()
|
43 |
|
44 |
+
def if_eval(prompt, generator=None):
|
45 |
+
images = pipe_low(prompt, num_inference_steps=NUM_INFERENCE_STEPS, generator=generator, output_type="pt").images
|
46 |
+
images = pipe_up(promtp=prompt, images=images, num_inference_steps=NUM_INFERENCE_STEPS).images
|
47 |
return images
|
48 |
|
49 |
+
return if_eval
|
50 |
|
51 |
MODELS = {
|
52 |
"runwayml/stable-diffusion-v1-5": get_sd_eval,
|
53 |
+
"stabilityai/stable-diffusion-2-1": get_sd_eval,
|
54 |
"kakaobrain/karlo-alpha": get_karlo_eval,
|
55 |
"DeepFloyd/IF-I-XL-v1.0": get_if_eval,
|
56 |
}
|
|
|
60 |
|
61 |
if __name__ == "__main__":
|
62 |
parser = argparse.ArgumentParser(description='Run Parti Prompt Evaluation')
|
63 |
+
parser.add_argument('model_repo_or_id', type=str, help='ID or URL of the model repository.')
|
64 |
parser.add_argument('--dataset_repo_or_id', type=str, default='diffusers/prompt_generations', help='ID or URL of the dataset repository (default: "diffusers/prompt_generations")')
|
65 |
parser.add_argument('--batch_size', type=int, default=8, help="Batch size for the eval function")
|
66 |
parser.add_argument('--upload_to_hub', action='store_true', help='whether to upload the dataset to the Hugging Face dataset hub')
|
67 |
+
parser.add_argument('--seed', type=int, default=0, help='Random seed')
|
68 |
|
69 |
args = parser.parse_args()
|
70 |
|
71 |
+
dataset = load_dataset("nateraw/parti-prompts")["train"]
|
72 |
|
73 |
+
eval_fn = MODELS[args.model_repo_or_id](args.model_repo_or_id)
|
74 |
|
75 |
def map_fn(batch):
|
76 |
+
generators = [torch.Generator(device="cuda").manual_seed(args.seed) for _ in range(args.batch_size)]
|
77 |
+
batch["images"] = eval_fn(batch["Prompt"], generator=generators)
|
78 |
+
batch["model_name"] = len(batch["images"]) * [args.model_repo_or_id]
|
79 |
+
batch["seed"] = len(batch["images"]) * [args.seed]
|
80 |
return batch
|
81 |
|
82 |
+
dataset_images = dataset.map(map_fn, batched=True, batch_size=args.batch_size)
|
83 |
|
84 |
if args.upload_to_hub:
|
85 |
+
dataset_images.push_to_hub(args.dataset_repo_or_id)
|
86 |
else:
|
87 |
+
dataset_images.save_to_disk(args.dataset_repo_or_id.split("/")[-1])
|