📚 সমস্ত অধ্যায় দেখুন
অধ্যায়/ফেজ 6 · Phase 6 · GenAI Advanced
6.1৩০ মিনিট পড়া37 / 68

GAN — দুই network এর লড়াই

GAN

Generator vs Discriminator।

Hook — দুই network এর লড়াই

Ian Goodfellow ২০১৪ সালে GAN আবিষ্কার করেন। Generator নকল ছবি বানায়, Discriminator আসল-নকল আলাদা করে। দুজনের লড়াইয়ে দুজনই উন্নত হয় — শেষে Generator এমন ছবি বানায় যা প্রায় বাস্তব।

Min-Max Game

min_G max_D E[log D(x)] + E[log(1 − D(G(z)))]
  • D চায় D(x)=1 (real) ও D(G(z))=0 (fake)।
  • G চায় D(G(z))=1 — D কে ফাঁকি দিতে।
  • Equilibrium এ G real distribution শিখে ফেলে।

GAN Variants

  • DCGAN — Conv layer দিয়ে stable training।
  • Conditional GAN (cGAN) — label-aware generation।
  • Pix2Pix — image-to-image translation।
  • CycleGAN — unpaired translation (horse↔zebra)।
  • StyleGAN 1/2/3 — high-quality face (thispersondoesnotexist)।
  • BigGAN — class-conditional, ImageNet scale।

Code — Minimal DCGAN (PyTorch)

dcgan.py
import torch, torch.nn as nn

class G(nn.Module):
    def __init__(self, z=100):
        super().__init__()
        self.net = nn.Sequential(
            nn.ConvTranspose2d(z, 256, 4, 1, 0), nn.BatchNorm2d(256), nn.ReLU(True),
            nn.ConvTranspose2d(256,128, 4, 2, 1), nn.BatchNorm2d(128), nn.ReLU(True),
            nn.ConvTranspose2d(128, 64, 4, 2, 1), nn.BatchNorm2d(64),  nn.ReLU(True),
            nn.ConvTranspose2d(64,   3, 4, 2, 1), nn.Tanh(),
        )
    def forward(self, z): return self.net(z)

class D(nn.Module):
    def __init__(self):
        super().__init__()
        self.net = nn.Sequential(
            nn.Conv2d(3, 64, 4, 2, 1), nn.LeakyReLU(0.2, True),
            nn.Conv2d(64,128, 4, 2, 1), nn.BatchNorm2d(128), nn.LeakyReLU(0.2, True),
            nn.Conv2d(128,256,4, 2, 1), nn.BatchNorm2d(256), nn.LeakyReLU(0.2, True),
            nn.Conv2d(256, 1, 4, 1, 0), nn.Sigmoid(),
        )
    def forward(self, x): return self.net(x).view(-1)

Common Problems

Mode Collapse

G শুধু কয়েক ধরনের sample বানাতে শেখে — diversity হারায়। WGAN-GP, spectral norm দিয়ে কমে।

  • Training instability — balance রাখা কঠিন।
  • Vanishing gradient — D খুব শক্তিশালী হলে G শিখতে পারে না।
  • Evaluation কঠিন — FID, IS metric।

Applications

  • Face/avatar generation।
  • Super-resolution (ESRGAN)।
  • Image-to-image (sketch→photo)।
  • Data augmentation, medical imaging।
  • Deepfake (ethical concern)।

Summary

এক নজরে

GAN = G ও D এর min-max লড়াই। StyleGAN এ চূড়া। আজ Diffusion বেশি জনপ্রিয়, কিন্তু GAN এখনও fast inference এ এগিয়ে।