GANs for Vision
Module: Computer Vision | Difficulty: Advanced
GAN Training Objective
The minimax game between generator and discriminator :
Wasserstein GAN (WGAN)
Replaces JS divergence with Wasserstein distance:
Spectral Normalization
Stabilizes training by constraining the Lipschitz constant:
FID Score
import torch
import torch.nn as nn
class Generator(nn.Module):
def __init__(self, z_dim=128, channels=3):
super().__init__()
self.net = nn.Sequential(
nn.ConvTranspose2d(z_dim, 512, 4, 1, 0),
nn.BatchNorm2d(512), nn.ReLU(True),
nn.ConvTranspose2d(512, 256, 4, 2, 1),
nn.BatchNorm2d(256), nn.ReLU(True),
nn.ConvTranspose2d(256, 128, 4, 2, 1),
nn.BatchNorm2d(128), nn.ReLU(True),
nn.ConvTranspose2d(128, channels, 4, 2, 1),
nn.Tanh()
)
def forward(self, z):
return self.net(z.view(-1, z.size(1), 1, 1))
class Discriminator(nn.Module):
def __init__(self, channels=3):
super().__init__()
self.net = nn.Sequential(
nn.Conv2d(channels, 128, 4, 2, 1),
nn.LeakyReLU(0.2, True),
nn.Conv2d(128, 256, 4, 2, 1),
nn.BatchNorm2d(256), nn.LeakyReLU(0.2, True),
nn.Conv2d(256, 512, 4, 2, 1),
nn.BatchNorm2d(512), nn.LeakyReLU(0.2, True),
nn.Conv2d(512, 1, 4, 1, 0),
)
def forward(self, x):
return self.net(x).view(-1)
Key Takeaways
- GANs learn implicit data distributions for high-quality synthesis
- Spectral normalization and WGAN improve training stability
- FID is the standard metric for evaluating generated image quality