How to use StableDiffusionPipeline
Quick answer
Use the
StableDiffusionPipeline from the diffusers library to generate images from text prompts in Python. Install the library, load a pretrained model, and call the pipeline with your prompt to get generated images.PREREQUISITES
Python 3.8+pip install diffusers>=0.20.0pip install torch (with CUDA for GPU acceleration recommended)Hugging Face account and access token (if using private models)
Setup
Install the diffusers library and torch for model inference. Optionally, set your Hugging Face token as an environment variable for access to pretrained models.
pip install diffusers torch Step by step
This example shows how to load the Stable Diffusion model and generate an image from a text prompt.
from diffusers import StableDiffusionPipeline
import torch
import os
# Set your Hugging Face token if needed
# os.environ["HUGGINGFACE_TOKEN"] = "your_token_here"
# Load the pipeline with the pretrained model
pipe = StableDiffusionPipeline.from_pretrained(
"runwayml/stable-diffusion-v1-5",
torch_dtype=torch.float16
)
pipe = pipe.to("cuda") # Use GPU if available
# Generate an image from a prompt
prompt = "A beautiful sunset over mountains"
image = pipe(prompt).images[0]
# Save the image
image.save("output.png")
print("Image saved as output.png") output
Image saved as output.png
Common variations
- Use CPU by setting
pipe.to("cpu")if no GPU is available. - Change model by specifying a different pretrained checkpoint in
from_pretrained(). - Generate multiple images by passing
num_inference_stepsandnum_images_per_promptparameters. - Use
pipe(prompt, guidance_scale=7.5)to control image creativity and adherence to prompt.
images = pipe(prompt, num_inference_steps=50, num_images_per_prompt=3).images
for i, img in enumerate(images):
img.save(f"output_{i}.png")
print("Saved 3 images") output
Saved 3 images
Troubleshooting
- If you get CUDA out of memory errors, reduce batch size or switch to CPU.
- Ensure
torchversion matches your CUDA version. - If model download fails, verify your Hugging Face token and internet connection.
- For slow generation, use GPU acceleration or optimize with
torch_dtype=torch.float16.
Key Takeaways
- Use
StableDiffusionPipelinefromdiffusersfor easy text-to-image generation. - GPU acceleration with
torchandfloat16improves speed and reduces memory use. - Customize generation with parameters like
num_inference_stepsandguidance_scale. - Set up Hugging Face authentication for private or large models.
- Troubleshoot memory and download issues by adjusting environment and dependencies.