Diffusion model পরিচিতি
এই পাঠে যা শিখবেন
- Forward diffusion process — Markov chain noise schedule
- Reverse process — denoising network ($\epsilon$-prediction)
- Loss function — simple MSE on noise
- U-Net architecture — diffusion-এর backbone
- Conditioning ও classifier-free guidance
- Stable Diffusion, DALL-E, Sora — landscape
১ · মূল ধারণা — noise → image
একটি real image $x_0$ নিন। ধাপে ধাপে noise add করুন — $T$ step পরে $x_T \approx \mathcal{N}(0, I)$ (pure noise)। এখন প্রশ্ন — উল্টো করা যায় কি? Pure noise থেকে শুরু করে ধাপে ধাপে denoise করে real image-এ পৌঁছানো?
Diffusion-এর insight: প্রতিটি tiny denoising step relatively easy — ছোট noise estimate করো। ১০০০ step করে পুরো image তৈরি কঠিন তবু solvable। Iterative refinement-এর জাদু।
২ · Forward process — noise schedule
প্রতিটি step:
$$q(x_t | x_{t-1}) = \mathcal{N}(x_t; \sqrt{1 - \beta_t} \, x_{t-1}, \beta_t I)$$
$\beta_t$ — variance schedule (small, e.g., $0.0001 \to 0.02$)। Markov chain — current step শুধু previous-এর উপর depend।
Closed form to step $t$:
$$x_t = \sqrt{\bar{\alpha}_t} \, x_0 + \sqrt{1 - \bar{\alpha}_t} \, \epsilon, \quad \epsilon \sim \mathcal{N}(0, I)$$
যেখানে $\alpha_t = 1 - \beta_t$, $\bar{\alpha}_t = \prod_{s=1}^t \alpha_s$। সরাসরি $x_0$ থেকে $x_t$ — training-এ দ্রুত sample।
৩ · Reverse process — neural denoising
$p_\theta(x_{t-1} | x_t)$ — neural network parameterize। Ho et al. (২০২০) — সরল reformulation: model $\epsilon$-predict করুক:
$$\epsilon_\theta(x_t, t) \approx \epsilon$$
Loss simply MSE:
$$\mathcal{L}_{\text{simple}} = \mathbb{E}_{t, x_0, \epsilon} \left[ \| \epsilon - \epsilon_\theta(x_t, t) \|^2 \right]$$
Training algorithm:
- Random sample $t \in [1, T]$।
- Random sample $\epsilon \sim \mathcal{N}(0, I)$।
- Compute $x_t = \sqrt{\bar{\alpha}_t} x_0 + \sqrt{1-\bar{\alpha}_t} \epsilon$।
- Predict $\hat{\epsilon} = \epsilon_\theta(x_t, t)$।
- $\nabla \| \epsilon - \hat{\epsilon} \|^2$ — update।
৪ · U-Net — diffusion-এর backbone
Denoising network typically U-NetU-NetEncoder-decoder architecture with skip connections — segmentation-এ designed (Ronneberger ২০১৫), diffusion-এ adopted। Different scale-এ feature combine।:
- Encoder downsample — multi-scale features।
- Decoder upsample — image-size output।
- Skip connections — fine detail preservation।
- Time embedding $t$ — sinusoidal + MLP, every layer-এ inject।
- Self-attention — middle layer-এ global pattern।
৫ · PyTorch — minimal training step
import torch
import torch.nn as nn
T = 1000
betas = torch.linspace(1e-4, 0.02, T)
alphas = 1.0 - betas
alpha_bar = torch.cumprod(alphas, dim=0)
def q_sample(x0, t, noise):
"""Forward — sample x_t from x_0."""
a = alpha_bar[t].view(-1, 1, 1, 1).sqrt()
one_minus = (1 - alpha_bar[t]).view(-1, 1, 1, 1).sqrt()
return a * x0 + one_minus * noise
# Pretend we have a UNet — placeholder
unet = nn.Conv2d(1, 1, 3, padding=1) # toy stand-in
# One training step
x0 = torch.randn(8, 1, 28, 28) # MNIST-shaped
t = torch.randint(0, T, (8,)) # random step per sample
noise = torch.randn_like(x0)
xt = q_sample(x0, t, noise)
pred = unet(xt) # real UNet would also see t embedding
loss = ((pred - noise) ** 2).mean()
print(f"loss: {loss.item():.4f}")
৬ · Sampling — pure noise থেকে image
Trained model দিয়ে generation:
- $x_T \sim \mathcal{N}(0, I)$ (pure noise)।
- $t = T$ থেকে $1$ পর্যন্ত: predict $\hat{\epsilon} = \epsilon_\theta(x_t, t)$, compute $x_{t-1}$ via formula।
- $x_0$ — final image।
DDPM-এ ১০০০ step lots। DDIM (২০২০) — deterministic skip, ৫০ step-এই comparable quality। Modern samplers — DPM-Solver, Euler, Heun — ১০-২০ step-এই photorealistic।
৭ · Conditioning — text-to-image
Vanilla DDPM unconditional — random image। Conditioning দিয়ে controllable:
- Class label: "cat" — embedding inject।
- Text: CLIP encoder → text embedding → cross-attention।
- Image: ControlNet — pose, sketch, depth।
Classifier-free guidance (Ho-Salimans ২০২২): conditional + unconditional joint train। Sampling-এ:
$$\hat{\epsilon} = \epsilon_\theta(x_t, t, c) + s \cdot (\epsilon_\theta(x_t, t, c) - \epsilon_\theta(x_t, t, \emptyset))$$
Guidance scale $s$ — ৭-১০ typical। Higher $s$ — prompt-faithful কিন্তু diversity কমে।
৮ · Stable Diffusion — practical breakthrough
- Latent diffusion: VAE encode 5১২×5১২ → ৬৪×৬৪ latent — ৪৮x compute reduction।
- CLIP text encoder: prompt → text embedding।
- Cross-attention U-Net: text condition inject।
- Open-source: ২০২২ — community fine-tune flood (LoRA, ControlNet, Dreambooth)।
- Consumer GPU viable — ৬-৮GB VRAM-এ inference।
DALL-E ২, Midjourney, Imagen — সব diffusion-based। Sora (video) — diffusion + Transformer।
৯ · GAN বনাম Diffusion
- Quality: Diffusion সাধারণত winner — sharper, more diverse।
- Training stability: Diffusion >> GAN। One loss, no adversarial।
- Mode coverage: Diffusion natural। GAN-এ collapse risk।
- Inference speed: GAN single-pass, fast। Diffusion iterative, slower।
- Controllability: Diffusion — guidance, ControlNet — flexible।
- Likelihood: Diffusion — proper probabilistic model। GAN — implicit।
ভাবনার প্রশ্ন
প্রতিটি প্রশ্ন নিজে কিছুক্ষণ ভাবুন — তারপর "→ উত্তর" চাপুন।
প্র ০১ Diffusion-এর iterative process আপাতদৃষ্টিতে inefficient — ১০০০ step কেন? Mathematical ও practical justification কী?
Iterative refinement — diffusion-এর core insight, weakness নয়।
Mathematical reason:
- প্রতিটি step Gaussian transition — analytically tractable।
- Multiple small steps ≈ approximate complex distribution।
- Single step approximation poor (impossible practically)।
- Markov chain-এর gradient strong।
Score matching connection:
- $\epsilon$-prediction = score function approximation।
- Langevin dynamics — slow convergence।
- Noise schedule — fast traversal trick।
- SDE theory underpinning।
Why ১০০০ specifically:
- Original DDPM — empirical choice।
- Quality-step trade-off optimal।
- Beta schedule fine-grained।
- Modern reduced — ১০০-৫০-২০।
Acceleration techniques:
(১) DDIM (২০২০):
- Deterministic step skip।
- ৫০ step competitive।
- Reformulate as ODE।
(২) DPM-Solver:
- ODE high-order solver।
- ১০-২০ step photorealistic।
- SDXL, FLUX-এ default।
(৩) Consistency models (২০২৩):
- Single-step generation।
- Distillation from pretrained diffusion।
- GAN-like speed, diffusion-like quality।
(৪) Flow matching:
- SD3, FLUX architecture।
- Continuous time formulation।
- Faster inference।
Inherent trade-off:
- Quality $\leftrightarrow$ speed।
- Iterative refinement gives time to "think"।
- Single-pass inherently limited।
- Diffusion's compute = quality।
Comparison with GAN:
- GAN single forward pass — fast inference।
- Diffusion iterative — slower but better quality।
- Modern distilled diffusion — close to GAN speed।
- Quality gap closing fast।
Iterative refinement intuition:
- Hard problem decomposed into easier steps।
- Each step — local correction।
- Cumulative — global solution।
- Like writing — draft, revise, polish।
মূল উপলব্ধি: Iterative — diffusion-এর strength। Mathematical foundation — small step Gaussian tractable। Modern acceleration — distillation, ODE solver। Quality-speed trade-off intrinsic। Iterative refinement universal AI principle (chain-of-thought, beam search analog)।
প্র ০২ Classifier-free guidance scale $s$ — কেন low value-এ image diverse কিন্তু prompt-faithful কম? Mechanism কী?
CFG — practical diffusion-এর critical hyperparameter। Mechanism subtle।
Formula recap:
$$\hat{\epsilon} = \epsilon_{uncond} + s \cdot (\epsilon_{cond} - \epsilon_{uncond})$$
- $s = 0$: pure unconditional।
- $s = 1$: pure conditional।
- $s > 1$: extrapolation in conditional direction।
Why extrapolation works:
- Conditional model — average match।
- Extrapolate — push further toward prompt।
- Gradient direction amplify।
- Prompt-image distance reduce।
Trade-off mechanism:
Low $s$ (1-3):
- Sample broader distribution।
- Prompt loosely matched।
- Diverse output।
- Sometimes off-topic।
Medium $s$ (5-8):
- Sweet spot quality।
- Prompt-faithful।
- Reasonable diversity।
- Production default।
High $s$ (10-20):
- Highly prompt-aligned।
- Saturated colors।
- Less diverse।
- Sometimes artifacts।
Visual symptoms by scale:
- $s = 1$: blurry, generic।
- $s = 7.5$: clean, prompt-aligned।
- $s = 15$: oversaturated, "cartoonish"।
- $s = 30$: artifacts, distorted।
Why diversity loss at high $s$:
- Direction amplified — same target।
- Variance suppressed।
- Mode-collapse-like behavior।
- "Mean prompt image"।
Geometric intuition:
- Probability density shift।
- Conditional region narrow।
- High $s$ — peaked near mean।
- Variance reduction।
Application-specific:
- Photorealistic: $s = 5-7$।
- Stylized: $s = 8-12$।
- Specific reference: $s = 12-15$।
- Ad/marketing: experiment।
Recent improvements:
- Dynamic CFG — schedule across timesteps।
- Negative prompts — undesirable away।
- Guidance distillation — single-pass equivalent।
- SDXL turbo — guidance baked in।
মূল উপলব্ধি: CFG = direction extrapolation, prompt alignment vs diversity trade-off। Sweet spot $s = 7$। High $s$ — quality drop, mode collapse-like। Modern diffusion — guidance fundamental। Hyperparameter tuning art।
প্র ০৩ Bangladesh-এর একটি digital agency Stable Diffusion দিয়ে Bangla wedding card design করতে চাচ্ছে। Custom fine-tune approach কী?
Practical Bangladesh creative use — Bangla wedding invitation। Cultural specificity critical।
Use case:
- Diverse cultural styles — Bengali traditional, modern fusion।
- Bangla typography integration।
- Religious motifs (Hindu, Muslim, Christian)।
- Color palette specific।
- Repeating client orders — speed essential।
Approach 1 — LoRA fine-tune:
Pros:
- Cheap — single GPU, ১-২ ঘণ্টা।
- Small storage (~১০০MB)।
- Multiple style — multiple LoRA।
- Combine via merge।
Cons:
- Limited transformation।
- Base model bias retain।
- Bangla typography poor (text rendering hard)।
Approach 2 — Dreambooth:
- Full fine-tune small dataset।
- Specific style/object embedding।
- Higher quality LoRA-এর চেয়ে।
- Compute heavier।
Approach 3 — Textual inversion:
- New token train — concept embed।
- Lightweight (~কয়েক KB)।
- Limited stylistic control।
Approach 4 — ControlNet:
- Layout control — wedding card structure।
- Reference photo guide।
- Pose-based layout।
- Production-friendly।
Recommended pipeline:
Step 1 — Dataset:
- ৫০-১০০ high-quality wedding card image collect।
- Manual caption — "Bengali wedding invitation, traditional motifs..."।
- Style keyword consistent।
Step 2 — LoRA train:
# Using kohya_ss or diffusers LoRA
from diffusers import StableDiffusionPipeline
from peft import LoraConfig, get_peft_model
lora_config = LoraConfig(
r=16, lora_alpha=32,
target_modules=["to_q", "to_k", "to_v"],
lora_dropout=0.1)
# Train on wedding card dataset
# ~৫০০ steps, learning rate 1e-4
Step 3 — Bangla text rendering:
- Diffusion text quality poor — especially Bangla।
- Approach: generate background separately, overlay text।
- InstructPix2Pix for layout edit।
- Post-process Photoshop/PIL।
Step 4 — ControlNet for layout:
- Sketch input — model fill।
- Canny edge — structure preserve।
- Depth — layered design।
Workflow integration:
- Web UI (AUTOMATIC1111 / ComfyUI)।
- Designer-friendly interface।
- Batch generation।
- Manual selection ও refinement।
Cost analysis:
- Training: ~$10 (cloud GPU)।
- Inference: ~$0.01 per image।
- Storage: minimal (LoRA <1GB)।
- Per-customer cost dramatically low।
Quality concerns:
- Hands, faces — diffusion weakness।
- Wedding card mostly typography + decorative — manageable।
- Iterative — designer + AI combo।
Bangla-specific challenges:
- Religious motif diversity — multiple LoRA।
- Cultural subtlety — designer review essential।
- Color symbolism (red wedding, yellow Mehendi)।
- Conjunct character text rendering — separate pipeline।
Production lessons:
- AI-generated draft → designer refine।
- Speed boost ১০x.
- Cost reduction significant।
- Cultural accuracy — human in loop।
Alternative — Midjourney/DALL-E:
- No fine-tune needed।
- Higher cost per image।
- Less control।
- Quick start option।
মূল উপলব্ধি: Bangla wedding card — LoRA + ControlNet pipeline practical। Cultural data curation key। Bangla text — separate handle। AI + designer hybrid production winner। Bangladesh creative AI opportunity vast। Cost-effective scale-up।
প্র ০৪ Diffusion bias problems — race, gender stereotype। Sora-এর video generation-এ এই issue magnified। Practical mitigation কী?
AI ethics — diffusion deployment-এ critical concern। Mitigation evolving।
Bias sources:
- Training data — internet-scrape biased।
- "Doctor" → male, "nurse" → female stereotypes।
- Race/skin color — Western-centric।
- Beauty standard narrow।
Sora video amplification:
- Static bias → motion bias।
- Stereotypical action — "doctor diagnosing", "construction worker"।
- Cultural setting default Western।
- Movement pattern stereotype।
Documented issues:
- Stable Diffusion — racial skew documented।
- Bloomberg study (২০২৩) — occupation bias।
- "CEO" vs "secretary" gender split extreme।
Mitigation strategies:
(১) Data curation:
- Diverse demographic representation।
- Cultural variety include।
- Historical bias awareness।
- Manual auditing।
(২) Prompt engineering:
- Explicit demographic specification।
- "diverse" keyword often help।
- Negative prompts — stereotype avoid।
- Practical but inelegant।
(৩) Model fine-tuning:
- RLHF for bias reduction।
- Adversarial debiasing।
- Fairness-aware loss।
- Active research area।
(৪) Post-processing:
- Generation review pipeline।
- Demographic balancing।
- Manual filter।
- Slow but accurate।
(৫) System-level controls:
- Usage policy enforcement।
- Sensitive prompt blocking।
- Watermarking generated content।
- Provenance tracking।
Bangladesh-specific concerns:
- Bangla face under-represented training।
- Cultural attire stereotyped।
- Religious sensitivity — Islamic, Hindu visual norms।
- Wedding/festival inaccurate often।
Practical Bangladesh deployment:
- Local fine-tuning — Bangla data essential।
- Cultural review board।
- Religious advisor consultation।
- Designer human-in-loop।
Industry initiatives:
- Stability AI — model card disclosure।
- OpenAI — DALL-E 3 safety system।
- Adobe Firefly — licensed training data।
- Watermarking standard (C2PA)।
Regulatory landscape:
- EU AI Act — high-risk AI regulation।
- US executive order ২০২৩।
- Bangladesh — emerging framework।
- Compliance increasing burden।
Deepfake concerns:
- Sora — realistic video misuse risk।
- Election interference।
- Non-consensual content।
- Detection arms race।
Detection tools:
- Forensic AI — diffusion artifact identify।
- Watermark verification।
- Metadata authentication।
- Education public awareness।
Ethical use guidelines:
- Disclose AI-generated content।
- Avoid deceptive use।
- Respect consent।
- Cultural sensitivity priority।
মূল উপলব্ধি: Diffusion bias real, video amplifies। Mitigation multi-layer — data, prompt, fine-tune, policy। Bangladesh — local fine-tune + cultural review। Detection ও watermarking essential। Responsible deployment industry-wide concern। Tech ethics — practical engineering issue।
অনুশীলন
-
Schedule compute: $T = 4$, $\beta = (0.1, 0.2, 0.3, 0.4)$ — $\bar{\alpha}_T$ কত?
- $\alpha_t = (0.9, 0.8, 0.7, 0.6)$।
- $\bar{\alpha}_T = 0.9 \times 0.8 \times 0.7 \times 0.6 = 0.3024$।
- $\sqrt{\bar{\alpha}_T} \approx 0.55$ — original signal weight।
- $\sqrt{1 - \bar{\alpha}_T} \approx 0.835$ — noise weight।
- $T = 4$-এ already noise-dominated।
-
Code: Pretrained Stable Diffusion দিয়ে ১টি image generate করুন (huggingface diffusers library)।
from diffusers import StableDiffusionPipeline import torch pipe = StableDiffusionPipeline.from_pretrained( "runwayml/stable-diffusion-v1-5", torch_dtype=torch.float16 ).to("cuda") prompt = "A traditional Bengali village at sunrise, oil painting" image = pipe(prompt, guidance_scale=7.5, num_inference_steps=30).images[0] image.save("village.png") -
চিন্তা: "Image space" diffusion vs "latent space" diffusion — কেন latent diffusion practical breakthrough?
৫১২×৫১২×৩ = ৭৮৬K dim — directly diffuse করা — memory ও compute prohibitive। VAE দিয়ে compress 6৪×৬৪×৪ = ১৬K dim — ~৪৮x reduction। Latent space-এ diffusion fast, decoder একবার চালিয়ে image।
এই trick-ই Stable Diffusion-কে consumer GPU-তে চালানোর মূল কারণ। Pretrained VAE freeze করে শুধু diffusion train — modular design।
আরও পড়ুন · ABCL TECH-এ আপনার পরবর্তী পদক্ষেপ
- পাঠ ৩৮ · GPU programming ও CUDA পরবর্তী পাঠ Big diffusion model train-এর hardware foundation।
- পাঠ ৩৬ · GAN আগের পাঠ Diffusion-এর predecessor — generative paradigm।
- GenAI Track গভীরে যান Stable Diffusion, ControlNet, prompt engineering।
- সব AI Courses দেখুন ABCL TECH Python, ML, DL, NLP, CV, GenAI, RL, MLOps — সব AI কোর্স একসাথে।