Install any skill in seconds. Free to start, no credit card required.
Get Started Free →Text-to-image generation, inpainting, and img2img.
.claude/skills/nousresearch-stable-diffusion/SKILL.md| Test case | Without → With | Effect | Δ tokens | Δ turns |
|---|---|---|---|---|
| case-13 | ✗→✓ | ▲ Improved | 325% | 0% |
| case-15 | ✗→✓ | ▲ Improved | 177% | 0% |
| case-11 | ✓→✓ | = Same ✓ | 458% | 0% |
| case-01 | ✓→✓ | = Same ✓ | 145% | 0% |
| case-02 | ✓→✓ | = Same ✓ | 206% | 0% |
Guide to generating images with Stable Diffusion using the HuggingFace Diffusers library.
Use Stable Diffusion when:
Key features:
Use alternatives instead:
bashpip install diffusers transformers accelerate torch pip install xformers # Optional: memory-efficient attention
pythonfrom diffusers import DiffusionPipeline import torch # Load pipeline (auto-detects model type) pipe = DiffusionPipeline.from_pretrained( "stable-diffusion-v1-5/stable-diffusion-v1-5", torch_dtype=torch.float16 ) pipe.to("cuda") # Generate image image = pipe( "A serene mountain landscape at sunset, highly detailed", num_inference_steps=50, guidance_scale=7.5 ).images[0] image.save("output.png")
pythonfrom diffusers import AutoPipelineForText2Image import torch pipe = AutoPipelineForText2Image.from_pretrained( "stabilityai/stable-diffusion-xl-base-1.0", torch_dtype=torch.float16, variant="fp16" ) pipe.to("cuda") # Enable memory optimization pipe.enable_model_cpu_offload() image = pipe( prompt="A futuristic city with flying cars, cinematic lighting", height=1024, width=1024, num_inference_steps=30 ).images[0]
Diffusers is built around three core components:
Pipeline (orchestration)
├── Model (neural networks)
│ ├── UNet / Transformer (noise prediction)
│ ├── VAE (latent encoding/decoding)
│ └── Text Encoder (CLIP/T5)
└── Scheduler (denoising algorithm)Text Prompt → Text Encoder → Text Embeddings
↓
Random Noise → [Denoising Loop] ← Scheduler
↓
Predicted Noise
↓
VAE Decoder → Final ImagePipelines orchestrate complete workflows:
| Pipeline | Purpose | |----------|---------| | StableDiffusionPipeline | Text-to-image (SD 1.x/2.x) | | StableDiffusionXLPipeline | Text-to-image (SDXL) | | StableDiffusion3Pipeline | Text-to-image (SD 3.0) | | FluxPipeline | Text-to-image (Flux models) | | StableDiffusionImg2ImgPipeline | Image-to-image | | StableDiffusionInpaintPipeline | Inpainting |
Schedulers control the denoising process:
| Scheduler | Steps | Quality | Use Case | |-----------|-------|---------|----------| | EulerDiscreteScheduler | 20-50 | Good | Default choice | | EulerAncestralDiscreteScheduler | 20-50 | Good | More variation | | DPMSolverMultistepScheduler | 15-25 | Excellent | Fast, high quality | | DDIMScheduler | 50-100 | Good | Deterministic | | LCMScheduler | 4-8 | Good | Very fast | | UniPCMultistepScheduler | 15-25 | Excellent | Fast convergence |
pythonfrom diffusers import DPMSolverMultistepScheduler # Swap for faster generation pipe.scheduler = DPMSolverMultistepScheduler.from_config( pipe.scheduler.config ) # Now generate with fewer steps image = pipe(prompt, num_inference_steps=20).images[0]
| Parameter | Default | Description | |-----------|---------|-------------| | prompt | Required | Text description of desired image | | negative_prompt | None | What to avoid in the image | | num_inference_steps | 50 | Denoising steps (more = better quality) | | guidance_scale | 7.5 | Prompt adherence (7-12 typical) | | height, width | 512/1024 | Output dimensions (multiples of 8) | | generator | None | Torch generator for reproducibility | | num_images_per_prompt | 1 | Batch size |
pythonimport torch generator = torch.Generator(device="cuda").manual_seed(42) image = pipe( prompt="A cat wearing a top hat", generator=generator, num_inference_steps=50 ).images[0]
pythonimage = pipe( prompt="Professional photo of a dog in a garden", negative_prompt="blurry, low quality, distorted, ugly, bad anatomy", guidance_scale=7.5 ).images[0]
Transform existing images with text guidance:
pythonfrom diffusers import AutoPipelineForImage2Image from PIL import Image pipe = AutoPipelineForImage2Image.from_pretrained( "stable-diffusion-v1-5/stable-diffusion-v1-5", torch_dtype=torch.float16 ).to("cuda") init_image = Image.open("input.jpg").resize((512, 512)) image = pipe( prompt="A watercolor painting of the scene", image=init_image, strength=0.75, # How much to transform (0-1) num_inference_steps=50 ).images[0]
Fill masked regions:
pythonfrom diffusers import AutoPipelineForInpainting from PIL import Image pipe = AutoPipelineForInpainting.from_pretrained( "runwayml/stable-diffusion-inpainting", torch_dtype=torch.float16 ).to("cuda") image = Image.open("photo.jpg") mask = Image.open("mask.png") # White = inpaint region result = pipe( prompt="A red car parked on the street", image=image, mask_image=mask, num_inference_steps=50 ).images[0]
Add spatial conditioning for precise control:
pythonfrom diffusers import StableDiffusionControlNetPipeline, ControlNetModel import torch # Load ControlNet for edge conditioning controlnet = ControlNetModel.from_pretrained( "lllyasviel/control_v11p_sd15_canny", torch_dtype=torch.float16 ) pipe = StableDiffusionControlNetPipeline.from_pretrained( "stable-diffusion-v1-5/stable-diffusion-v1-5", controlnet=controlnet, torch_dtype=torch.float16 ).to("cuda") # Use Canny edge image as control control_image = get_canny_image(input_image) image = pipe( prompt="A beautiful house in the style of Van Gogh", image=control_image, num_inference_steps=30 ).images[0]
| ControlNet | Input Type | Use Case | |------------|------------|----------| | canny | Edge maps | Preserve structure | | openpose | Pose skeletons | Human poses | | depth | Depth maps | 3D-aware generation | | normal | Normal maps | Surface details | | mlsd | Line segments | Architectural lines | | scribble | Rough sketches | Sketch-to-image |
Load fine-tuned style adapters:
pythonfrom diffusers import DiffusionPipeline pipe = DiffusionPipeline.from_pretrained( "stable-diffusion-v1-5/stable-diffusion-v1-5", torch_dtype=torch.float16 ).to("cuda") # Load LoRA weights pipe.load_lora_weights("path/to/lora", weight_name="style.safetensors") # Generate with LoRA style image = pipe("A portrait in the trained style").images[0] # Adjust LoRA strength pipe.fuse_lora(lora_scale=0.8) # Unload LoRA pipe.unload_lora_weights()
python# Load multiple LoRAs pipe.load_lora_weights("lora1", adapter_name="style") pipe.load_lora_weights("lora2", adapter_name="character") # Set weights for each pipe.set_adapters(["style", "character"], adapter_weights=[0.7, 0.5]) image = pipe("A portrait").images[0]
python# Model CPU offload - moves models to CPU when not in use pipe.enable_model_cpu_offload() # Sequential CPU offload - more aggressive, slower pipe.enable_sequential_cpu_offload()
python# Reduce memory by computing attention in chunks pipe.enable_attention_slicing() # Or specific chunk size pipe.enable_attention_slicing("max")
python# Requires xformers package pipe.enable_xformers_memory_efficient_attention()
python# Decode latents in tiles for large images pipe.enable_vae_slicing() pipe.enable_vae_tiling()
python# FP16 (recommended for GPU) pipe = DiffusionPipeline.from_pretrained( "model-id", torch_dtype=torch.float16, variant="fp16" ) # BF16 (better precision, requires Ampere+ GPU) pipe = DiffusionPipeline.from_pretrained( "model-id", torch_dtype=torch.bfloat16 )
pythonfrom diffusers import UNet2DConditionModel, AutoencoderKL # Load custom VAE vae = AutoencoderKL.from_pretrained("stabilityai/sd-vae-ft-mse") # Use with pipeline pipe = DiffusionPipeline.from_pretrained( "stable-diffusion-v1-5/stable-diffusion-v1-5", vae=vae, torch_dtype=torch.float16 )
Generate multiple images efficiently:
python# Multiple prompts prompts = [ "A cat playing piano", "A dog reading a book", "A bird painting a picture" ] images = pipe(prompts, num_inference_steps=30).images # Multiple images per prompt images = pipe( "A beautiful sunset", num_images_per_prompt=4, num_inference_steps=30 ).images
pythonfrom diffusers import StableDiffusionXLPipeline, DPMSolverMultistepScheduler import torch # 1. Load SDXL with optimizations pipe = StableDiffusionXLPipeline.from_pretrained( "stabilityai/stable-diffusion-xl-base-1.0", torch_dtype=torch.float16, variant="fp16" ) pipe.to("cuda") pipe.scheduler = DPMSolverMultistepScheduler.from_config(pipe.scheduler.config) pipe.enable_model_cpu_offload() # 2. Generate with quality settings image = pipe( prompt="A majestic lion in the savanna, golden hour lighting, 8k, detailed fur", negative_prompt="blurry, low quality, cartoon, anime, sketch", num_inference_steps=30, guidance_scale=7.5, height=1024, width=1024 ).images[0]
pythonfrom diffusers import AutoPipelineForText2Image, LCMScheduler import torch # Use LCM for 4-8 step generation pipe = AutoPipelineForText2Image.from_pretrained( "stabilityai/stable-diffusion-xl-base-1.0", torch_dtype=torch.float16 ).to("cuda") # Load LCM LoRA for fast generation pipe.load_lora_weights("latent-consistency/lcm-lora-sdxl") pipe.scheduler = LCMScheduler.from_config(pipe.scheduler.config) pipe.fuse_lora() # Generate in ~1 second image = pipe( "A beautiful landscape", num_inference_steps=4, guidance_scale=1.0 ).images[0]
CUDA out of memory:
python# Enable memory optimizations pipe.enable_model_cpu_offload() pipe.enable_attention_slicing() pipe.enable_vae_slicing() # Or use lower precision pipe = DiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float16)
Black/noise images:
python# Check VAE configuration # Use safety checker bypass if needed pipe.safety_checker = None # Ensure proper dtype consistency pipe = pipe.to(dtype=torch.float16)
Slow generation:
python# Use faster scheduler from diffusers import DPMSolverMultistepScheduler pipe.scheduler = DPMSolverMultistepScheduler.from_config(pipe.scheduler.config) # Reduce steps image = pipe(prompt, num_inference_steps=20).images[0]
| Case | Status | Duration (ms) | Turns | Tokens | Tool calls | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Without | With | Δ | Without | With | Δ | Without | With | Δ | Without | With | Δ | ||
case-11 | pass→pass | 4,754 | 2,200 | -54% | 1 | 1 | 0% | 702 | 3,916 | +458% | 0 | 0 | — |
case-01 | pass→pass | 12,104 | 11,881 | -2% | 1 | 1 | 0% | 2,014 | 4,938 | +145% | 0 | 0 | — |
case-02 | pass→pass | 9,726 | 7,209 | -26% | 1 | 1 | 0% | 1,576 | 4,830 | +206% | 0 | 0 | — |
case-03 | pass→pass | 11,993 | 12,667 | +6% | 1 | 1 | 0% | 1,929 | 5,330 | +176% | 0 | 0 | — |
case-04 | pass→pass | 11,722 | 3,530 | -70% | 1 | 1 | 0% | 1,628 | 4,227 | +160% | 0 | 0 | — |
case-05 | pass→pass | 6,855 | 4,777 | -30% | 1 | 1 | 0% | 1,143 | 4,522 | +296% | 0 | 0 | — |
case-06 | pass→pass | 10,004 | 7,119 | -29% | 1 | 1 | 0% | 1,619 | 4,759 | +194% | 0 | 0 | — |
case-07 | pass→pass | 12,089 | 9,214 | -24% | 1 | 1 | 0% | 2,020 | 5,134 | +154% | 0 | 0 | — |
case-08 | pass→pass | 4,577 | 3,945 | -14% | 1 | 1 | 0% | 796 | 4,279 | +438% | 0 | 0 | — |
case-09 | pass→pass | 5,536 | 4,555 | -18% | 1 | 1 | 0% | 935 | 4,396 | +370% | 0 | 0 | — |
case-10 | pass→pass | 7,166 | 7,104 | -1% | 1 | 1 | 0% | 1,089 | 4,938 | +353% | 0 | 0 | — |
case-12 | pass→pass | 4,788 | 3,375 | -30% | 1 | 1 | 0% | 893 | 4,178 | +368% | 0 | 0 | — |
case-13 | fail→pass | 6,376 | 3,227 | -49% | 1 | 1 | 0% | 970 | 4,119 | +325% | 0 | 0 | — |
case-14 | fail→fail | 7,764 | 7,470 | -4% | 1 | 1 | 0% | 1,280 | 4,910 | +284% | 0 | 0 | — |
case-15 | fail→pass | 9,589 | 5,435 | -43% | 1 | 1 | 0% | 1,621 | 4,487 | +177% | 0 | 0 | — |
case-16 | pass→pass | 13,584 | 5,834 | -57% | 1 | 1 | 0% | 1,198 | 4,661 | +289% | 0 | 0 | — |
case-17 | pass→pass | 4,083 | 5,594 | +37% | 1 | 1 | 0% | 746 | 4,689 | +529% | 0 | 0 | — |
case-18 | pass→pass | 8,467 | 5,362 | -37% | 1 | 1 | 0% | 1,582 | 4,582 | +190% | 0 | 0 | — |
case-19 | pass→pass | 4,319 | 3,353 | -22% | 1 | 1 | 0% | 682 | 4,101 | +501% | 0 | 0 | — |
case-20 | pass→pass | 12,634 | 8,612 | -32% | 1 | 1 | 0% | 1,463 | 5,166 | +253% | 0 | 0 | — |
case-21 | pass→pass | 5,999 | 3,940 | -34% | 1 | 1 | 0% | 1,094 | 4,393 | +302% | 0 | 0 | — |
case-22 | pass→pass | 6,626 | 3,992 | -40% | 1 | 1 | 0% | 1,029 | 4,222 | +310% | 0 | 0 | — |
DecimalAI ran this skill against gemini-3.6-flash twice over the same eval suite — once with the skill loaded and once without — and compared the two runs case by case. 22 cases were attempted. The headline lift of +9 percentage points is the difference between those two pass rates over the 22 comparable cases.
Without the skill loaded, the model failed this case. With it loaded, the same prompt on the same model passed. This is one improved case from the latest verified run; every case, including any that regressed, is in the table above.
Other measured skills in the registry, with their headline benchmark lift.