πŸ€– AI/ML

Stable Diffusion

Stable Diffusion

Stability AI's open-source image generation model. Based on the Latent Diffusion Model (LDM) architecture, it generates high-quality images from text prompts, and its efficiency, which allows it to run even on local GPUs, led the popularization of image generation AI.

πŸ“– Detailed Explanation

Stable Diffusion is a text-to-image generation model developed by Stability AI, based on the Latent Diffusion Model (LDM). Whereas existing Diffusion Models operate directly in pixel space and therefore had high computational costs, Stable Diffusion significantly improved efficiency by performing the denoising process in a latent space compressed by a VAE (Variational Autoencoder).

The democratization of AI image generation began when Stable Diffusion 1.4 was released as open source in August 2022. Unlike DALL-E or Midjourney, anyone could download the model weights for free, run them locally, or perform custom training (fine-tuning). It has since continued to evolve through SD 1.5, SD 2.0, SDXL, SD3, and more, improving resolution, prompt understanding, and image quality.

The core architecture consists of three components: (1) VAE - encodes/decodes images to and from the latent space, (2) U-Net - predicts and removes noise in the latent space, (3) Text Encoder - converts text prompts into embeddings based on CLIP. The generation process is a reverse diffusion process that starts from pure noise and gradually removes noise to match the text conditioning.

In practice, Stable Diffusion is used in a variety of workflows: Text-to-Image (generating images from text prompts), Image-to-Image (transforming existing images), Inpainting (modifying parts of an image), ControlNet (conditional generation such as pose and edges), and LoRA/DreamBooth (learning specific styles or objects). Because it is open source, it is combined with various community tools such as ComfyUI, Automatic1111, and Forge and integrated into production pipelines.

πŸ’» Code Example

This is an example of image generation with Stable Diffusion using Hugging Face Diffusers.

# Stable Diffusion XL 이미지 생성 예제
from diffusers import StableDiffusionXLPipeline
import torch

# SDXL νŒŒμ΄ν”„λΌμΈ λ‘œλ“œ (μ•½ 6.5GB VRAM ν•„μš”)
pipe = StableDiffusionXLPipeline.from_pretrained(
    "stabilityai/stable-diffusion-xl-base-1.0",
    torch_dtype=torch.float16,
    use_safetensors=True,
    variant="fp16"
)
pipe.to("cuda")

# λ©”λͺ¨λ¦¬ μ΅œμ ν™” (VRAM이 λΆ€μ‘±ν•œ 경우)
pipe.enable_model_cpu_offload()
pipe.enable_vae_slicing()

# 이미지 생성
prompt = "A majestic mountain landscape at sunset, photorealistic, 8k uhd"
negative_prompt = "blurry, low quality, distorted, artifacts"

image = pipe(
    prompt=prompt,
    negative_prompt=negative_prompt,
    num_inference_steps=30,  # ν’ˆμ§ˆκ³Ό 속도 νŠΈλ ˆμ΄λ“œμ˜€ν”„
    guidance_scale=7.5,      # CFG scale: λ†’μ„μˆ˜λ‘ ν”„λ‘¬ν”„νŠΈμ— μΆ©μ‹€
    width=1024,
    height=1024,
    generator=torch.Generator("cuda").manual_seed(42)
).images[0]

image.save("output.png")


# Image-to-Image λ³€ν™˜
from diffusers import StableDiffusionXLImg2ImgPipeline
from PIL import Image

img2img_pipe = StableDiffusionXLImg2ImgPipeline.from_pretrained(
    "stabilityai/stable-diffusion-xl-base-1.0",
    torch_dtype=torch.float16,
    use_safetensors=True
).to("cuda")

init_image = Image.open("input.png").resize((1024, 1024))

result = img2img_pipe(
    prompt="Convert to anime style, vibrant colors",
    image=init_image,
    strength=0.7,  # 0-1: λ†’μ„μˆ˜λ‘ λ³€ν™”κ°€ 큼
    num_inference_steps=30
).images[0]


# ControlNet으둜 포즈 μ œμ–΄
from diffusers import ControlNetModel, StableDiffusionXLControlNetPipeline
from controlnet_aux import OpenposeDetector

controlnet = ControlNetModel.from_pretrained(
    "thibaud/controlnet-openpose-sdxl-1.0",
    torch_dtype=torch.float16
)

pipe = StableDiffusionXLControlNetPipeline.from_pretrained(
    "stabilityai/stable-diffusion-xl-base-1.0",
    controlnet=controlnet,
    torch_dtype=torch.float16
).to("cuda")

# 포즈 μΆ”μΆœ 및 쑰건뢀 생성
pose_detector = OpenposeDetector.from_pretrained("lllyasviel/ControlNet")
pose_image = pose_detector(Image.open("reference_pose.png"))

image = pipe(
    prompt="Professional dancer in elegant dress",
    image=pose_image,
    controlnet_conditioning_scale=0.8
).images[0]

πŸ“Š Performance & Cost

Performance Specs by Model

Model Default Resolution Parameter VRAM Requirements
SD 1.5 512x512 860M ~4GB (FP16)
SD 2.1 768x768 860M ~5GB (FP16)
SDXL 1.0 1024x1024 6.6B ~8GB (FP16)
SD 3 Medium 1024x1024 2B ~10GB
SD 3.5 Large 1024x1024 8B ~16GB

Stability AI API Pricing (as of 2025)

Services Price (per image) Features
SD 1.5 $0.006 Legacy, cheapest
SDXL $0.011 High-resolution base model
SD 3 $0.037 Improved prompt understanding
SD 3.5 Large $0.065 (6.5 credits) Latest high-quality model
Stable Image Core $0.03 Speed-optimized
Stable Image Ultra $0.08 Highest quality

Local Execution vs Cloud API

Option Upfront Cost Operating Cost Advantages
Local GPU (RTX 4090) ~$1,600 Electricity cost only Unlimited generation, custom models
Cloud GPU (A100) None $1-2/hour Flexible scaling
Stability AI API None Pay-per-image billing No management required

* Community License: Individuals and companies with annual revenue under $1M can self-host core models such as SDXL and SD 3.5 for free

πŸ—£οΈ Say It Like This in Practice

In a meeting

"I think we should generate the product images with SDXL and train and apply a LoRA for a consistent brand style."

In a technical interview

"Stable Diffusion performs diffusion in the latent space, so its computational efficiency is high. SDXL uses two Text Encoders, which improved prompt understanding."

In a technical discussion

"Set up the workflow in ComfyUI, control poses with ControlNet, and reference styles with IP-Adapter, and you can generate consistent characters."

Take a mock interview on this topic β†’ WIA SOOM AI Interview Β· Start for free

⚠️ Common Mistakes & Cautions

❌ "Stable Diffusion is unconditionally worse than Midjourney"

That may be true if you only compare the base models, but by using custom models, LoRA, ControlNet, and so on, it can produce better results in specific domains. It is particularly strong in commercial use cases that require a consistent style.

❌ "A higher CFG Scale is always better"

If you set guidance_scale (CFG) too high, colors become oversaturated or the image looks unnatural. A range of 7-10 is generally appropriate, and adjustments are needed depending on the style.

βœ… The right approach

Choose the model that fits your project requirements. Use SD 1.5 for fast prototyping, SDXL/SD3 for high-quality outputs, and fine-tuned models for specific styles. Settings such as the Negative Prompt and an appropriate Sampler (such as DPM++ 2M Karras) also have a major impact on quality.

πŸ”— Related Terms

πŸ“š Learn More