!pip install -qq diffusers transformers scipy ftfy accelerate
Stable Diffusion Inference (high-level)
Create an image using Diffusers library.
Install and Import Libraries
import torch
= "cuda" if torch.cuda.is_available() else "cpu"
device device
'cuda'
= ["a photograph of an astronaut riding a horse"]
prompt
= 512 # default height of Stable Diffusion
height = 512 # default width of Stable Diffusion
width
= 50 # Number of denoising steps
num_inference_steps
= 7.5 # Scale for classifier-free guidance
guidance_scale
= torch.manual_seed(256) # Seed generator to create the inital latent noise
generator
= 2 batch_size
from diffusers import LMSDiscreteScheduler, StableDiffusionPipeline
= LMSDiscreteScheduler.from_pretrained(
scheduler "CompVis/stable-diffusion-v1-4", subfolder="scheduler"
)= StableDiffusionPipeline.from_pretrained(
pipe "CompVis/stable-diffusion-v1-4", scheduler=scheduler
)
pipe
StableDiffusionPipeline {
"_class_name": "StableDiffusionPipeline",
"_diffusers_version": "0.20.2",
"_name_or_path": "CompVis/stable-diffusion-v1-4",
"feature_extractor": [
"transformers",
"CLIPImageProcessor"
],
"requires_safety_checker": true,
"safety_checker": [
"stable_diffusion",
"StableDiffusionSafetyChecker"
],
"scheduler": [
"diffusers",
"LMSDiscreteScheduler"
],
"text_encoder": [
"transformers",
"CLIPTextModel"
],
"tokenizer": [
"transformers",
"CLIPTokenizer"
],
"unet": [
"diffusers",
"UNet2DConditionModel"
],
"vae": [
"diffusers",
"AutoencoderKL"
]
}
= pipe.to(device) pipe
High-level
= pipe(
pil_images =prompt * batch_size,
prompt=height,
height=width,
width=num_inference_steps,
num_inference_steps=guidance_scale,
guidance_scale=generator,
generator ).images
for pil_image in pil_images:
display(pil_image)
References
- Patil et al. (2022) Stable Diffusion with ๐งจ Diffusers, https://huggingface.co/blog/stable_diffusion