Chapter 38

Stable Diffusion

Stable Diffusion
🎬 Latent Diffusion-এর বিপ্লব
Stable Diffusion (Rombach et al., 2022) diffusion-কে pixel space থেকে compressed latent space-এ সরিয়ে আনে — ফলে বড় ইমেজ generation সম্ভব হয় একটিমাত্র GPU-তে। Open-weights — পুরো ecosystem।

তিনটি কোর উপাদান

  1. VAE Encoder/Decoder: 512×512 image ↔ 64×64×4 latent।
  2. U-Net (Denoiser): latent-এ noise predict, text-এ conditioned।
  3. Text Encoder: CLIP / T5 / OpenCLIP — prompt → embedding।

Inference Pipeline

text → CLIP encoder → embedding
random noise (64×64×4 latent)
   ↓ ×N steps: U-Net + scheduler + CFG
clean latent
   ↓ VAE Decoder
512×512 RGB image

Classifier-Free Guidance (CFG)

# একই step-এ conditional ও unconditional দুটিই predict
ε_cond   = unet(x_t, t, text_emb)
ε_uncond = unet(x_t, t, null_emb)

# guidance scale w (সাধারণত 7–9)
ε = ε_uncond + w * (ε_cond - ε_uncond)

HuggingFace Diffusers দিয়ে দ্রুত শুরু

from diffusers import StableDiffusionPipeline
import torch

pipe = StableDiffusionPipeline.from_pretrained(
    "runwayml/stable-diffusion-v1-5", torch_dtype=torch.float16
).to("cuda")

img = pipe(
    prompt="a cinematic photo of a tiger in moonlight, ultra-detailed",
    negative_prompt="blurry, low quality, distorted",
    num_inference_steps=30,
    guidance_scale=7.5,
    height=512, width=512,
).images[0]
img.save("tiger.png")

Img2Img — ইমেজ থেকে ইমেজ

from diffusers import StableDiffusionImg2ImgPipeline
pipe = StableDiffusionImg2ImgPipeline.from_pretrained("...sd-v1-5").to("cuda")

out = pipe(prompt="ghibli style", image=init_img,
           strength=0.6, guidance_scale=7.5).images[0]

Inpainting

from diffusers import StableDiffusionInpaintPipeline
pipe = StableDiffusionInpaintPipeline.from_pretrained("...inpainting").to("cuda")
out = pipe(prompt="a red hat", image=img, mask_image=mask).images[0]

Conditioning Extensions

  • ControlNet: canny, depth, pose থেকে structural control।
  • IP-Adapter: reference image-এর style/identity inject।
  • LoRA: ছোট adapter দিয়ে custom style/character।
  • Textual Inversion: নতুন token শেখানো (~few image)।
  • DreamBooth: নির্দিষ্ট subject পুরো fine-tune।

মডেল প্রজন্ম

সংস্করণ
Latent / Resolution
মূল বৈশিষ্ট্য
SD 1.5
512
baseline, বিশাল community
SD 2.1
768
OpenCLIP, better quality
SDXL
1024
two-stage refiner, sharp
SD 3 / Flux
1024+
MM-DiT, better text rendering

Prompt Crafting টিপ

  • Subject + style + lighting + camera + quality keyword।
  • Negative prompt-এ বাজে artifact বাদ দিন।
  • (word:1.3) দিয়ে weight বাড়ান।
  • Seed fix করে reproducible variation।
🔑 কেন latent?
VAE 8x compression → diffusion 64x কম compute। তাই SD ১টি consumer GPU-তেও চলে — Imagen-এর মতো TPU-array দরকার পড়ে না।

অনুশীলন

১. SD 1.5 দিয়ে ৫টি prompt-এ ছবি বানান, CFG 3/7/12 তুলনা।

২. ControlNet (canny) দিয়ে sketch → ছবি।

৩. LoRA train করুন নিজের style-এ।

সারসংক্ষেপ

✨ এই অধ্যায়ে যা শিখলাম
  • SD = VAE + U-Net + CLIP — latent diffusion।
  • CFG দিয়ে prompt প্রভাব নিয়ন্ত্রণ।
  • ControlNet/LoRA/IP-Adapter দিয়ে fine-grained control।