🤖 AI/ML

Latent Diffusion

Latent Diffusion Model

An efficient image generation model that performs diffusion in latent space. The basis of Stable Diffusion.

📖 Detailed Explanation

The Latent Diffusion Model (LDM) is a generative model that performs diffusion in a latent space rather than in pixel space. Proposed by Rombach et al. in 2022, it is the core technology behind Stable Diffusion, DALL-E 3, and Midjourney.

Conventional diffusion models (DDPM) processed 512x512 images directly, which required an enormous amount of computation. LDM first compresses the image into a 64x64 latent vector with an autoencoder, then performs diffusion in this smaller space, reducing computation by more than 50 times.

Architecture: VAE Encoder (image → latent space), U-Net (noise prediction), VAE Decoder (latent space → image), Text Encoder (text embedding with CLIP/T5). During training, noise is added to the latent vector, and the U-Net is trained to remove it.

Practical applications: Text-to-Image (prompt → image), Image-to-Image (image editing), Inpainting (partial modification), ControlNet (pose/edge-conditioned generation). Recent models such as SDXL and Stable Diffusion 3.5 offer higher quality and better prompt understanding.

💻 Code Example

# pip install diffusers transformers accelerate torch
from diffusers import StableDiffusionPipeline, StableDiffusionXLPipeline
from diffusers import StableDiffusionImg2ImgPipeline
import torch

# 1. Text-to-Image (기본 생성)
pipe = StableDiffusionXLPipeline.from_pretrained(
    "stabilityai/stable-diffusion-xl-base-1.0",
    torch_dtype=torch.float16,
    variant="fp16"
).to("cuda")

image = pipe(
    prompt="A futuristic city at sunset, cyberpunk style, highly detailed",
    negative_prompt="blurry, low quality, distorted",
    num_inference_steps=30,  # 스텝 수 (품질 vs 속도)
    guidance_scale=7.5,      # CFG scale (프롬프트 충실도)
    width=1024, height=1024
).images[0]
image.save("generated.png")

# 2. Image-to-Image (이미지 변환)
img2img_pipe = StableDiffusionImg2ImgPipeline.from_pretrained(
    "runwayml/stable-diffusion-v1-5", torch_dtype=torch.float16
).to("cuda")

from PIL import Image
init_image = Image.open("input.png").resize((512, 512))

result = img2img_pipe(
    prompt="Same scene but in watercolor painting style",
    image=init_image,
    strength=0.75,  # 0=원본 유지, 1=완전 새 이미지
    guidance_scale=7.5
).images[0]

# 3. ControlNet (포즈 조건부 생성)
from diffusers import ControlNetModel, StableDiffusionControlNetPipeline

controlnet = ControlNetModel.from_pretrained(
    "lllyasviel/control_v11p_sd15_openpose", torch_dtype=torch.float16
)
cn_pipe = StableDiffusionControlNetPipeline.from_pretrained(
    "runwayml/stable-diffusion-v1-5",
    controlnet=controlnet, torch_dtype=torch.float16
).to("cuda")

pose_image = Image.open("pose.png")  # OpenPose 스켈레톤
result = cn_pipe(prompt="A dancer in red dress", image=pose_image).images[0]

🗣️ Say It Like This in Practice

💬 In a Meeting
"We'll use SDXL for automatic product thumbnail generation. With ControlNet Canny, we can keep the product outline and change only the background. A single A100 can process 2 images per second, 100,000 images per month."
💬 In an Interview
"The key to Latent Diffusion is computational efficiency. Processing 512x512 directly means 262K pixels, but compressing it with a VAE to a 64x64 latent (4K elements) gives the same quality with 50 times less computation. We also balanced quality and diversity with Classifier-Free Guidance."
💬 In a Technical Discussion
"SD3's Rectified Flow samples faster than the conventional DDPM. 50 steps are reduced to 20 steps while quality is maintained. However, DALL-E 3 is still better at text rendering, and Midjourney is ahead in aesthetic quality."

Take a mock interview on this topic → WIA SOOM AI Interview · Start for free

⚠️ Common Mistakes & Cautions

❌
Setting the Guidance Scale too high

A CFG of 15 or higher causes oversaturated images and artifacts. A range of 7-9 is generally appropriate.

❌
Fine-tuning on copyrighted images

Training on the style of specific artists/characters is a legal risk. For commercial use, be sure to check the license.

✅
The right way

Remove unwanted elements with a negative prompt. If VRAM is insufficient, use xformers or torch.compile. For commercial use, use SDXL Turbo or a licensed model.

🔗 Related Terms

📚 Learn More