tools / run_k_diffusion.py
patrickvonplaten's picture
up
62fc896
raw
history blame
3.05 kB
#!/usr/bin/env python3
from diffusers import DiffusionPipeline, StableDiffusionPipeline, KDPM2DiscreteScheduler, KDPM2AncestralDiscreteScheduler, HeunDiscreteScheduler, DDIMScheduler, EulerDiscreteScheduler, EulerAncestralDiscreteScheduler, PNDMScheduler, LMSDiscreteScheduler, DPMSolverMultistepScheduler
import torch
import os
seed = 33
inference_steps = 25
#old_pipe = DiffusionPipeline.from_pretrained("stabilityai/stable-diffusion-2-base", custom_pipeline="/home/patrick_huggingface_co/diffusers/examples/community/sd_text2img_k_diffusion.py")
#old_pipe = old_pipe.to("cuda")
#old_pipe.set_progress_bar_config(disable=True)
pipe = StableDiffusionPipeline.from_pretrained("stabilityai/stable-diffusion-2", torch_dtype=torch.float16)
#pipe = StableDiffusionPipeline.from_pretrained("stabilityai/stable-diffusion-2-base", torch_dtype=torch.float16)
pipe = pipe.to("cuda")
for prompt in ["astronaut riding horse", "whale falling from sky", "magical forest", "highly photorealistic picture of johnny depp"]:
for sampler in ["sample_dpm_2_ancestral", "euler_ancestral", "sample_dpm_2", "sample_heun", "lms", "ddim", "euler", "pndm", "dpm"]:
# for sampler in ["sample_dpm_2_ancestral"]:
# old_pipe.set_sampler(sampler)
# torch.manual_seed(0)
# image = old_pipe(prompt, height=512, width=512, num_inference_steps=inference_steps).images[0]
folder = f"a_{'_'.join(prompt.split())}"
os.makedirs(f"/home/patrick_huggingface_co/images/{folder}", exist_ok=True)
# image.save(f"/home/patrick_huggingface_co/images/{folder}/{sampler}.png")
# pipe = StableDiffusionPipeline(**old_pipe.components)
# pipe = pipe.to("cuda")
# pipe.set_progress_bar_config(disable=True)
if sampler == "sample_dpm_2":
pipe.scheduler = KDPM2DiscreteScheduler.from_config(pipe.scheduler.config)
elif sampler == "sample_dpm_2_ancestral":
pipe.scheduler = KDPM2AncestralDiscreteScheduler.from_config(pipe.scheduler.config)
elif sampler == "sample_heun":
pipe.scheduler = HeunDiscreteScheduler.from_config(pipe.scheduler.config)
elif sampler == "ddim":
pipe.scheduler = DDIMScheduler.from_config(pipe.scheduler.config)
elif sampler == "dpm":
pipe.scheduler = DPMSolverMultistepScheduler.from_config(pipe.scheduler.config)
elif sampler == "euler":
pipe.scheduler = EulerDiscreteScheduler.from_config(pipe.scheduler.config)
elif sampler == "euler_ancestral":
pipe.scheduler = EulerAncestralDiscreteScheduler.from_config(pipe.scheduler.config)
elif sampler == "pndm":
pipe.scheduler = PNDMScheduler.from_config(pipe.scheduler.config)
elif sampler == "lms":
pipe.scheduler = LMSDiscreteScheduler.from_config(pipe.scheduler.config)
torch.manual_seed(0)
image = pipe(prompt, num_inference_steps=inference_steps).images[0]
image.save(f"/home/patrick_huggingface_co/images/{folder}/hf_{sampler}.png")
break