পাঠ ১৬ · ২৮-এর মধ্যে · মডিউল ৩
Home / AI Courses / Generative AI / Latent Diffusion & SD

Latent Diffusion ও Stable Diffusion

Latent Diffusion Models & Stable Diffusion
৯ মিনিট পড়া উন্নত · Advanced Text-to-image

এই পাঠে যা শিখবেন

  • Pixel diffusion-এর compute সমস্যা — কেন latent space দরকার
  • VAE-encoder + diffusion + decoder pipeline — Rombach et al. 2022
  • CLIP text encoder ও cross-attention — text → image conditioning
  • SD1.5, SDXL, SD3 — architecture evolution ও practical use

১ · Pixel diffusion-এর সমস্যা

DDPM, ImageGen-1, GLIDE — সব pixel-space-এ। $256\times 256$ ছবিতে diffusion U-Net-এ প্রতিটি timestep জুড়ে ১৯৬K-dim tensor process। Memory huge, compute prohibitive।

  • $256 \times 256 \times 3 = 196{,}608$ dim per image।
  • Batch 32, fp32 → ~25 MB activation per layer। U-Net-এ ১০+ layers — GB scale।
  • Training 256 V100 × 2 weeks (Imagen scale)।
  • Most "perceptual structure" — high-frequency texture — diffusion-এর strength নয়।

২ · Latent diffusion idea

Rombach, Blattmann et al. (CVPR 2022, "High-Resolution Image Synthesis with Latent Diffusion Models") — observation: pixel space contains semantic + perceptual info; diffusion-কে শুধু semantic নিয়ে কাজ করতে দিন।

Pipeline:

  1. Encoder $\mathcal{E}$ (VAE): $\mathbf{x} \in \mathbb{R}^{H\times W \times 3} \to \mathbf{z} \in \mathbb{R}^{h \times w \times c}$, যেখানে $h = H/f, w = W/f$, $f = 8$।
  2. Diffusion in latent: $\mathbf{z}_0 \to \mathbf{z}_T$ forward, $\mathbf{z}_T \to \hat{\mathbf{z}}_0$ reverse — DDPM-এর মতো।
  3. Decoder $\mathcal{D}$: $\hat{\mathbf{z}}_0 \to \hat{\mathbf{x}}$ — pixel-space ছবি।

$512\times 512\times 3$ → $64\times 64\times 4 = 16{,}384$ dim। ~48× compression। Diffusion-এর কাজ ৪৮× কম।

কেন কাজ করে

VAE perceptually-aware compression শেখে — fine pixel detail ছেড়ে দেয়, semantic structure preserve। Diffusion এই latent-এ বড় changes (object, layout) modeling-এ focus। Decoder shape, color, texture পুনর্নির্মাণ করে।

৩ · VAE training — perceptual + adversarial loss

Standard VAE blurry — diffusion-এর জন্য crisp latent দরকার। Rombach et al. দু'টি ingredient যোগ করলেন:

  • Perceptual loss (LPIPS): reconstruction in VGG feature space — sharp।
  • PatchGAN discriminator: pixel realism boost।
  • KL regularization (small weight): latent Gaussian-like কিন্তু not over-regularized।

৪ · Cross-attention — text conditioning

Stable Diffusion-এ U-Net-এ text condition যোগ করা হয় cross-attentionCross-AttentionSelf-attention-এর variant: query এক sequence থেকে, key/value অন্য sequence থেকে। Text-to-image-এ image features = query, text embeddings = key/value। Image-এর কোন region কোন word-এর সাথে relate তা শেখে। দিয়ে।

$$\text{Attn}(Q, K, V) = \text{softmax}\left(\frac{QK^\top}{\sqrt{d}}\right)V$$

  • $Q$: U-Net-এর spatial features ($h \cdot w$ tokens)।
  • $K, V$: CLIPCLIPOpenAI 2021. ছবি-text contrastive pretraining। Text encoder + image encoder same embedding space-এ। SD-এ text encoder-ই ব্যবহার হয়। text encoder থেকে token embeddings (77 tokens, 768 dim)।
  • প্রতিটি spatial position relevant text token-এর সাথে align করে।
  • U-Net-এর প্রতি ResBlock-এ এই cross-attention layer।
Pixel diffusion (slow) vs Latent diffusion = Stable Diffusion Pixel diffusion: 512×512×3 = 786K dim — heavy x_T noise 512²×3 U-Net 1000× image 512²×3 → Hours/day on GPU clusters Latent diffusion (Stable Diffusion): 64×64×4 = 16K dim — 48× faster CLIP text "a cat" cross-attention VAE 𝓔 image→latent (training only) z_T 64×64×4 U-Net ε_θ latent denoising + cross-attn 50 step DDIM ẑ_0 64×64×4 VAE 𝓓 latent→image image 512×512 ~3-5s/image SD = (frozen CLIP text) + (frozen VAE) + (trainable U-Net with cross-attention)
Stable Diffusion architecture: Text → CLIP → cross-attention → U-Net latent denoising → VAE decoder → image। Diffusion শুধু compact latent-এ কাজ করে।

৫ · Stable Diffusion-এর evolution

  • SD1.4/1.5 (Aug 2022): CompVis + RunwayML। 512×512। CLIP-ViT-L/14 text encoder। 860M U-Net params। Open weights, commercial OK।
  • SD2.0/2.1 (Nov 2022): 768×768। OpenCLIP-H text encoder। Quality similar, less popular due to license।
  • SDXL (July 2023): 1024×1024। Two text encoders (CLIP-L + OpenCLIP-G) — concatenated 2048-dim। 2.6B U-Net params। Refiner stage। SOTA quality 2023।
  • SDXL Turbo (Nov 2023): ADD distillation, 1-4 step inference।
  • SD3 (March 2024): MMDiT architecture (transformer-based)। T5-XXL + CLIP text encoders। Rectified Flow training। Better text rendering।
  • Flux.1 (Aug 2024): Black Forest Labs (original SD authors)। 12B params। DiT-based। Top quality 2024-25।

৬ · PyTorch — minimal SD inference

Python · diffusers
from diffusers import StableDiffusionPipeline, DDIMScheduler
import torch

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

prompt = "a beautiful Bangla calligraphy 'বাংলাদেশ', detailed, ornate"
neg    = "blurry, low quality, distorted text"

image = pipe(
    prompt,
    negative_prompt=neg,
    num_inference_steps=50,
    guidance_scale=7.5,
    generator=torch.manual_seed(42),
).images[0]

image.save("output.png")

    
VRAM: SD1.5 fp16 — ~4GB। SDXL — ~10GB। Flux — ~24GB। Colab T4 (15GB) SDXL run করতে পারে; Flux-এ A100 দরকার।

৭ · Inside the U-Net call

Python · PyTorch
# Manual sampling loop — শেখার জন্য
text_embeds = pipe.text_encoder(pipe.tokenizer(prompt, return_tensors="pt").input_ids.cuda())[0]
uncond_embeds = pipe.text_encoder(pipe.tokenizer("", return_tensors="pt").input_ids.cuda())[0]

z = torch.randn((1, 4, 64, 64), device="cuda", dtype=torch.float16)
pipe.scheduler.set_timesteps(50)

for t in pipe.scheduler.timesteps:
    z_in = torch.cat([z, z])                      # CFG: uncond + cond
    embeds = torch.cat([uncond_embeds, text_embeds])
    eps = pipe.unet(z_in, t, encoder_hidden_states=embeds).sample
    eps_uncond, eps_cond = eps.chunk(2)
    eps = eps_uncond + 7.5 * (eps_cond - eps_uncond)   # CFG (পাঠ ১৭)
    z = pipe.scheduler.step(eps, t, z).prev_sample

# decode latent → image
with torch.no_grad():
    image = pipe.vae.decode(z / pipe.vae.config.scaling_factor).sample

    
এই manual loop-এ পরিষ্কার দেখা যায় — diffusers internally কী করে। CFG (পাঠ ১৭) এই pipeline-এর integral part।

৮ · Practical tips

  • VAE choice: SDXL-এর "fp16-fix" VAE artifacts কমায়। MadeByOllin/sdxl-vae-fp16-fix জনপ্রিয়।
  • Memory optimization: enable_attention_slicing(), enable_xformers_memory_efficient_attention()।
  • CPU offload: enable_model_cpu_offload() — VRAM constrained-এ।
  • Tile VAE: Large image (2048+) decode tile-wise — VAE OOM avoidance।
Stable Diffusion ecosystem massive। ComfyUI, Automatic1111 — research থেকে production-ready। কিন্তু underlying mathematics একই — যা এই পাঠে শিখলেন।

ভাবনার প্রশ্ন

প্রতিটি প্রশ্ন নিজে কিছুক্ষণ ভাবুন — তারপর "→ উত্তর" চাপুন।

প্র ০১ VAE-এর latent space "perceptually equivalent" — মানে কী এবং কীভাবে verify করা যায়? এই কারণেই LDM কাজ করে — কিন্তু কোন ক্ষেত্রে break হয়?

"Perceptual equivalence" — LDM-এর সবচেয়ে subtle ও গুরুত্বপূর্ণ ধারণা।

সংজ্ঞা:

  • একটি ছবি $\mathbf{x}$ → encoder → $\mathbf{z}$ → decoder → $\hat{\mathbf{x}}$।
  • $\hat{\mathbf{x}}$ pixel-wise identical না, কিন্তু human perception-এ same scene।
  • LPIPS, SSIM low value — perceptually close।
  • Texture, fine detail কিছুটা ভিন্ন; structure, color, semantic preserved।

কেন SD-এর VAE-এ এটি achievable:

  • Massive training data: LAION-5B → diverse natural images। Learned compression natural image manifold-এর জন্য optimal।
  • VGG perceptual loss: Feature-space reconstruction — high-frequency details flexible, content rigid।
  • Adversarial loss: Discriminator pixel-realism enforce — natural texture।
  • Spatial preservation: $f=8$ downsample — local structure largely preserved।

Verification methods:

  • Reconstruction quality: Test set-এ encode→decode roundtrip। PSNR ~30dB, LPIPS < 0.1।
  • Diffusion sample quality: Encode→noise→denoise→decode pipeline-এ FID measure।
  • Visual comparison: Side-by-side original vs reconstructed।
  • Downstream task: Latent space-এ classifier — pixel-space classifier-এর সমান accuracy?

কোথায় break হয়:

  • Text in image: SD1.5-এর VAE text rendering-এ অপটিমাইজড নয় — letters distorted। SDXL/SD3 better।
  • Faces (small): $64\times 64$ latent-এ ছোট মুখ ($30\times 30$ pixel)-এর details lost।
  • Hands & fingers: Notorious — latent space-এ digit details inadequate। Major SD weakness।
  • Fine textures: Hair strands, cloth weave — somewhat lossy।
  • Color shifts: Reconstruction sometimes hue shift slight (especially saturated colors)।
  • Out-of-distribution images: Medical imaging, satellite, art styles uncommon — encoder struggles।

SDXL/SD3 improvements:

  • SDXL: better VAE, $128\times 128$ latent for $1024\times 1024$ — more detail capacity।
  • SD3: Improved VAE, 16-channel latent (vs 4)। Better text rendering।
  • Flux: similar 16-ch latent, higher capacity decoder।

Practical implications:

  • Same VAE shared across SD1.x checkpoints — community fine-tuning easy।
  • Different VAE → different latent statistics → require new diffusion training।
  • "VAE swap" — sometimes used for quality tweaks but rarely beneficial।

Theoretical questions:

  • Optimal compression ratio? $f=8$ empirical sweet spot, but theory underexplored।
  • Domain-specific VAE — medical imaging, microscopy, satellite — unexplored frontier।
  • Information bottleneck theory — what semantic info preserved at $z$?

মূল উপলব্ধি: LDM-এর সাফল্য VAE-এর "perceptual compression"-এ। Pixel-precision না — perceptual fidelity gap exploits করে compute কমায়। এই idea-র উপর ভিডিও diffusion (SVD), 3D diffusion, audio diffusion — সব দাঁড়িয়ে আছে।

প্র ০২ SD1.5 → SDXL → SD3 — quality jump dramatic. Architecture, dataset, training trick — কোনটির contribution বেশি? "Bigger is better" নাকি smarter approach?

২০২২-২৪ Stable Diffusion-এর এই দ্রুত evolution diffusion research-এর সবচেয়ে compressed period।

SD1.5 (Aug 2022):

  • U-Net 860M params, $512\times 512$।
  • CLIP ViT-L/14 text encoder (frozen)।
  • LAION-5B subset, ~600M images filtered।
  • Linear schedule, $T=1000$।
  • Training: 256× A100, several weeks ($600K)।

SDXL (July 2023):

  • Architecture: 2.6B U-Net (3× SD1.5)। Two text encoders (CLIP-L + OpenCLIP-G) concat 2048-dim।
  • Resolution: $1024\times 1024$, $128\times 128$ latent।
  • Dataset: Curated, aesthetic-scored। Multi-aspect ratio bucketing।
  • Training tricks:
    • Conditioning on resolution, crop coordinates, aesthetic score।
    • Zero-SNR schedule fine-tuning।
    • Refiner: separate model for last steps polishing।
  • Quality: human eval significantly preferred over SD1.5।

SD3 (March 2024):

  • Architecture: MMDiT (Multi-Modal Diffusion Transformer)। U-Net abandoned।
  • Text: T5-XXL (4.7B) + CLIP encoders। T5 text understanding much better।
  • Training: Rectified Flow (linear path, fewer steps)।
  • Latent: 16-channel (vs 4)। More information capacity।
  • Resolution: Native any aspect ratio।
  • Strong text-in-image rendering। Compositional fidelity improved।

Flux.1 (Aug 2024):

  • 12B parameters MMDiT।
  • Hybrid double-stream + single-stream attention।
  • Best human aesthetic eval 2024।
  • Schnell, Dev, Pro variants।

Contribution breakdown (rough):

  • Scale (params, data): ~30% — Necessary but not sufficient। Bigger model, no smart approach → diminishing return।
  • Architecture (DiT, MMDiT): ~25% — Transformer scaling laws cleaner than U-Net।
  • Text encoder (T5 vs CLIP): ~20% — Massive text understanding boost in SD3।
  • Training tricks (RF, schedule, conditioning): ~15%।
  • Data curation (aesthetic, captions): ~10%।

"Bigger is better" — partially true:

  • Scaling from 860M (SD1.5) to 12B (Flux) — drastic quality। Plain scaling effective।
  • BUT: Imagen (Google, 5B) was already strong in 2022 — scale alone wasn't enough at that time।
  • Compute/data scaling laws diffusion-এ now cleaner with DiT।

"Smarter approach" — equally critical:

  • T5-XXL adoption (SD3) — single biggest text understanding leap।
  • Rectified Flow — fewer artifacts, faster sampling।
  • Multi-aspect ratio training (SDXL) — practical UX improvement।
  • VAE 16-ch (SD3) — bottleneck removed।

Counter-examples (smaller, smarter):

  • Sana (NVIDIA 2024): smaller model, linear DiT, faster inference at SOTA quality।
  • Pixart-Σ: efficient DiT, comparable to SDXL with less compute।
  • Würstchen: cascaded approach, very efficient।

Failed bigger-is-better:

  • SD2.0 (Nov 2022): bigger text encoder (OpenCLIP-H), worse human preference vs SD1.5।
  • Reason: training on filtered LAION (NSFW removed too aggressively, distribution shift)।
  • Lesson: dataset quality > model size sometimes।

২০২৫-এর pattern:

  • Frontier: Flux Pro, SD3.5 Large, Imagen 3, DALL·E 3 — all 5-12B params।
  • Efficient frontier: Sana, Würstchen — 800M-2B with smart design।
  • Specialized: SDXL-based community fine-tunes — dominate aesthetics specific niches।

Open research questions:

  • Optimal scaling laws diffusion-এর জন্য (Kaplan-style, Chinchilla-style)?
  • Better text encoder than T5-XXL for visual concepts?
  • VAE architecture next leap?
  • Diffusion + autoregressive hybrid (Transfusion, MAR)?

মূল উপলব্ধি: "Bigger" + "Smarter" — both. SD evolution-এ each component (architecture, text encoder, training, data) iteratively improved। বাংলাদেশের researchers/companies-এর জন্য — full pretrain not feasible, but specialized fine-tune on under-served domains (Bangla, regional culture, agriculture) huge opportunity।

প্র ০৩ Cross-attention text-to-image-এর হৃদয়। SD-তে exact কোথায় inject হয়, কোন word কোন region-এ effect ফেলে — visualize করার techniques কী? Compositional understanding কেন এত fragile?

Cross-attention SD-র "language ↔ vision" bridge — কিন্তু এর internals subtle ও দুর্বলতা প্রকাশক।

SD U-Net-এ cross-attention layers:

  • প্রতিটি Spatial Transformer block-এ (encoder, mid, decoder)।
  • SD1.5: 16 cross-attention layers — multiple spatial scales (64², 32², 16², 8²)।
  • Self-attention preceding cross-attention each block।
  • $Q$ from spatial features (image), $K, V$ from CLIP token embeddings (77 tokens)।

Attention map visualization:

  • For token $w$ (e.g., "cat"), softmax attention weights $\alpha_{ij,w}$ across spatial positions।
  • Heatmap overlay original image — দেখায় কোন region "cat" word-এর প্রভাবে generate হলো।
  • Tools: diffusers + custom hooks; library "DAAM" (Diffusion Attentive Attribution Maps)।

Findings from attention analysis:

  • Concrete nouns ("cat", "table") — sharp localized attention।
  • Adjectives ("red", "fluffy") — broader, attribute spreads।
  • Spatial words ("on top", "left") — weakly localized, often confused।
  • Negation ("no cat") — SD often still generates it (negation handling poor)।

Compositional fragility:

  • Attribute binding: "Red car and blue ball" → often red ball, blue car। Cross-attention doesn't perfectly bind attribute-noun।
  • Counting: "Three cats" → 1, 2, 4, 7 cats randomly। SD has poor numerical control।
  • Spatial relationships: "Cat on top of dog" → cat next to dog mostly।
  • Negation: "Cat without tail" → almost always with tail।
  • Multi-subject: "Alice and Bob" — distinct identity hard।

Why fragile:

  • CLIP text encoder: Bag-of-words tendency — order/structure weakly encoded।
  • Token attention diffuse: Multiple tokens compete for same spatial region।
  • Training data: Captions web-scraped, often single-line short — compositional examples sparse।
  • No grounding signal: No bounding-box or segmentation supervision during training।

Improvements:

  • SDXL: Two text encoders, slightly better composition।
  • SD3: T5-XXL — much better. T5 trained on T5-style language tasks, more compositional।
  • DALL·E 3: "Caption recaptioning" with GPT-4V → richer training captions।
  • InstructPix2Pix, ControlNet: External structure conditioning bypasses text limitations।

Research techniques:

  • Attend-and-Excite (Chefer 2023): Test-time intervention — strengthen attention for under-represented tokens।
  • Structured Diffusion (Feng 2023): Parsing-based attention guidance।
  • Layout Diffusion: Bounding box conditioning during generation।
  • RegionalPrompt: Spatial mask + per-region prompt।

Practical workarounds:

  • Use ControlNet (পাঠ ১৮) for spatial control।
  • Use SDXL's MultiDiffusion for region-specific prompts।
  • SD3 / DALL·E 3 for compositional tasks।
  • Iterate inpainting for compositional accuracy।

Visualization code (sketch):

  • Hook into CrossAttention.forward
  • Save attention map: $\text{softmax}(QK^\top/\sqrt{d}) \in \mathbb{R}^{HW \times 77}$
  • For specific token index, reshape to spatial → upscale → overlay।

মূল উপলব্ধি: Cross-attention text-image alignment-এর mechanism, কিন্তু perfect grounding নয়। Compositional generation-এর জন্য external conditioning (ControlNet, layout, masks) often required। ২০২৫-এ frontier — better text encoders + grounding signals + multimodal training।

প্র ০৪ আপনি একটি বাংলাদেশী agency-তে designer/developer। ক্লায়েন্ট চান cultural-specific image generation (পহেলা বৈশাখ poster, ঐতিহ্যবাহী dress, রিকশা art)। SD ecosystem-এ practical workflow কী? Self-host vs API — কোনটি ভাল?

Real-world Bangladeshi creative agency-র জন্য — practical, cost-conscious workflow।

Step 1: Tool selection

  • Quick prototyping: ChatGPT (DALL·E 3), Midjourney — paid subscription ($20/মাস)।
  • Heavy production: Self-hosted SDXL/Flux — control + cost-effective।
  • Hybrid: Commercial API for fast turn, self-host for batch।

Step 2: Cultural specificity challenge

  • Generic SD prompts: "Bangladeshi village" → often generic Indian/SE Asian aesthetic।
  • Bangla text rendering: SD1.5 garbage; SDXL/SD3/Flux better but still imperfect।
  • Regional details: rickshaw art style, jamdani patterns, পহেলা বৈশাখ motifs — undertrained in base SD।

Step 3: Workflow recommendations

(A) Prompt engineering depth:

  • Detailed cultural reference: "Bangladeshi panta-ilish festival, rural village, traditional alpana floor art, fishing nets"।
  • Style anchors: "in style of Quamrul Hassan painting, Zainul Abedin ink art"।
  • Negative prompts: "Indian, Pakistani, generic Asian, stereotypical"।
  • Aspect ratio adjusted for posters (3:4 portrait)।

(B) LoRA fine-tuning (পাঠ ১৯):

  • 50-200 examples of target style (rickshaw art, jamdani patterns, etc.)।
  • 4-8 hours Colab Pro training ($10/মাস)।
  • Trigger word: "Bangla rickshaw art style", "jamdani pattern style"।
  • Result: SDXL + LoRA → highly cultural specific outputs।

(C) ControlNet for layout (পাঠ ১৮):

  • Sketch-based layout → ControlNet canny → generate styled image।
  • Pose-controlled traditional dancers, choreographed scenes।
  • Architectural reference (mosque, temple) preserve via depth ControlNet।

(D) Bangla text in image:

  • Direct generation unreliable — text often gibberish।
  • Workflow: Generate background → add Bangla text in Photoshop/Figma।
  • Or: TextDiffuser-2 (research model, Bangla support limited)।
  • Or: Inpaint specific region with text-aware fine-tuned model।

Step 4: Self-host vs API decision

API services (cost: ৳1-5/image):

  • OpenAI DALL·E 3: $0.04-0.08/image। Best prompt understanding।
  • Replicate (SDXL, Flux): $0.001-0.05/image।
  • Runway, Leonardo: Subscription, ~$10-30/মাস unlimited।
  • Stability AI API: SD3 access $0.04-0.065/image।

Self-hosted (cost: equipment + ~৳5-30/ঘন্টা cloud GPU):

  • Local GPU: RTX 4090 (24GB, ~৳2L taka, ~$1,800)। SDXL/Flux ভাল চলে।
  • Cloud GPU: RunPod, Vast.ai — $0.5-2/hour A100।
  • Software: Automatic1111, ComfyUI (free)। ComfyUI more flexible production।

Decision criteria:

  • Volume < 100/মাস: API simpler।
  • Volume 100-1000/মাস: Hybrid (API + Replicate batch)।
  • Volume > 1000/মাস বা special LoRA needs: Self-host।
  • Privacy/data sensitivity: Self-host। Client work-এ confidentiality important।

Step 5: Production pipeline (recommended)

  1. Brief from client → mood board।
  2. Generate 50-100 candidates with SDXL+LoRA in ComfyUI।
  3. Curate top 5-10 in collaboration with client।
  4. Refine via inpainting, ControlNet for specific changes।
  5. Upscale (RealESRGAN) → 4K poster-ready।
  6. Final touch in Photoshop (text, logo, branding)।

Cost example (পহেলা বৈশাখ campaign, 20 visuals):

  • API approach: ৳2,000-5,000 + designer time।
  • Self-host first time: ৳15,000-30,000 setup, ৳500-1,000 cloud cost।
  • After setup, repeat campaigns: ৳200-500/campaign cloud।

Legal & ethical considerations:

  • SDXL: CreativeML Open RAIL++ — commercial OK।
  • Flux Schnell: Apache 2.0 — fully commercial।
  • Flux Dev: non-commercial — careful।
  • Client-facing: disclose AI usage where required (advertising standards)।
  • Avoid training on copyrighted artist work without permission।

Bangladeshi-specific opportunities:

  • Underserved local style — first-mover LoRA advantage।
  • Government, NGO, telecom — bulk visual content needs।
  • E-commerce (Daraz) product photography augmentation।
  • Education content visualization।
  • Tourism marketing materials।

Pitfalls to avoid:

  • Over-reliance on AI — designer skill essential for curation।
  • Stereotyping — careful prompt engineering, sensitivity review।
  • Inconsistent brand identity — fix LoRA + ControlNet workflow।
  • Hidden costs (cloud, API) — track per-image cost।

মূল উপলব্ধি: SD ecosystem startup-friendly — minimal investment, fast iteration। বাংলাদেশের designer/agency-দের জন্য — global tool + local expertise = competitive advantage। Cultural specificity-তে fine-tune করা LoRA models সম্পদ — শুধু client work নয়, marketplace (Civitai)-এ-ও লাভজনক।

অনুশীলন

  1. Compression ratio: $1024\times 1024\times 3$ image SDXL-এ $128\times 128\times 4$ latent। Compression ratio? Diffusion compute কতটুকু কম?

    Pixel: $1024\times 1024\times 3 = 3{,}145{,}728$ dim। Latent: $128\times 128\times 4 = 65{,}536$ dim।

    Ratio: $3{,}145{,}728 / 65{,}536 = 48\times$ compression।

    U-Net memory ~quadratic spatial — actual compute saving ~24-48× depending on architecture।

  2. Run SD locally: diffusers + Colab T4-এ "a Bangladeshi village in summer evening" prompt চালান।
    !pip install diffusers transformers accelerate
    from diffusers import StableDiffusionPipeline
    import torch
    pipe = StableDiffusionPipeline.from_pretrained(
        "runwayml/stable-diffusion-v1-5", torch_dtype=torch.float16
    ).to("cuda")
    img = pipe("a Bangladeshi village in summer evening, rice fields, golden hour, palm trees, photo realistic").images[0]
    img.save("village.png")
  3. Cross-attention visualize: "DAAM" library দিয়ে "cat" token-এর attention map plot করুন একটি SD-generated image-এর উপর।
    !pip install daam
    from daam import trace
    with trace(pipe) as tc:
        out = pipe("a black cat on a red carpet")
    heatmap = tc.compute_global_heat_map().compute_word_heat_map("cat")
    heatmap.plot_overlay(out.images[0])

    Cat region red highlighted — text → spatial alignment দেখায়।

আরও পড়ুন · ABCL TECH-এ আপনার পরবর্তী পদক্ষেপ

Hands-on: Colab free T4-এ SD1.5 চলে। SDXL/Flux-এর জন্য Colab Pro বা RunPod recommended।
পূর্ববর্তী পাঠ
পাঠ ১৫ · DDIM — দ্রুত sampling