#!/usr/bin/env python3 from diffusers import DiffusionPipeline, DDIMScheduler import argparse import torch from datasets import load_dataset import PIL IMAGE_OUTPUT_SIZE = (256, 256) NUM_INFERENCE_STEPS = 100 def resize(image: PIL.Image): return image.resize(IMAGE_OUTPUT_SIZE, resample=PIL.Image.Resampling.LANCZOS) def get_sd_eval(ckpt, guidance_scale=7.5): pipe = DiffusionPipeline.from_pretrained(ckpt, torch_dtype=torch.float16) pipe.to("cuda") pipe.scheduler = DDIMScheduler.from_config(pipe.config) def sd_eval(prompt): images = pipe(prompt, num_inference_steps=100, guidance_scale=guidance_scale).images images = [resize(image) for image in images] return images return sd_eval def get_karlo_eval(ckpt): pipe = DiffusionPipeline.from_pretrained(ckpt, torch_dtype=torch.float16) pipe.to("cuda") def karlo_eval(prompt): images = pipe(prompt, prior_num_inference_steps=50, decoder_num_inference_steps=100).images return images return karlo_eval def get_if_eval(ckpt): pipe_low = DiffusionPipeline.from_pretrained(ckpt, torch_dtype=torch.float16) pipe_low.enable_model_cpu_offload() pipe_up = DiffusionPipeline.from_pretrained("DeepFloyd/IF-II-L-v1.0", text_encoder=pipe_low.text_encoder, torch_dtype=torch.float16) pipe_up.enable_model_cpu_offload() def sd_eval(prompt): images = pipe_low(prompt, num_inference_steps=100, output_type="pt").images images = pipe_up(promtp=prompt, images=images, num_inference_steps=100).images return images return sd_eval MODELS = { "runwayml/stable-diffusion-v1-5": get_sd_eval, "stabilityai/stable-diffusion-v2-1": get_sd_eval, "kakaobrain/karlo-alpha": get_karlo_eval, "DeepFloyd/IF-I-XL-v1.0": get_if_eval, } if __name__ == "__main__": parser = argparse.ArgumentParser(description='Run Parti Prompt Evaluation') parser.add_argument('model_repo_or_id', type=str, help='ID or URL of the model repository.', required=True) 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")') parser.add_argument('--batch_size', type=int, default=8, help="Batch size for the eval function") parser.add_argument('--upload_to_hub', action='store_true', help='whether to upload the dataset to the Hugging Face dataset hub') args = parser.parse_args() eval_fn = MODELS[args.model_repo_or_id](args.model_repo_or_id) dataset = load_dataset("nateraw/parti-prompts") def map_fn(batch): batch["images"] = eval_fn(batch["prompt"]) return batch dataset_images = dataset.map(map_fn, batched=True, batch_size=8) if args.upload_to_hub: dataset.push_to_hub(args.dataset_repo_or_id) else: dataset.save_to_disk(args.dataset_repo_or_id.split("/")[-1])