A Generative Adversarial Network (GAN) is two neural networks locked in a game. One forges, the other investigates, and both get better by trying to beat each other.
This project builds a DCGAN (Deep Convolutional GAN) from scratch in PyTorch. It starts from nothing but random numbers and learns to paint realistic 64×64 human faces.
flowchart LR
Z(["🎲 Random noise<br/>z ∈ ℝ¹⁰⁰"]) --> G["🎨 Generator<br/><i>the forger</i>"]
G --> F(["🖼️ Fake face"])
R(["📷 Real face<br/>CelebA"]) --> D
F --> D["🕵️ Discriminator<br/><i>the detective</i>"]
D --> V{{"Real or Fake?"}}
V -. "feedback: get better at spotting" .-> D
V -. "feedback: get better at fooling" .-> G
style G fill:#7c3aed,stroke:#a78bfa,color:#fff
style D fill:#db2777,stroke:#f472b6,color:#fff
style Z fill:#1e293b,stroke:#64748b,color:#fff
style R fill:#1e293b,stroke:#64748b,color:#fff
style F fill:#1e293b,stroke:#64748b,color:#fff
style V fill:#0f766e,stroke:#2dd4bf,color:#fff
|
Transposed-conv generator and strided-conv discriminator, following Radford et al. (2015), with DCGAN weight init and BatchNorm. |
A fixed noise batch is rendered every epoch, and the notebook auto-builds a GIF of the generator improving. |
|
One-sided label smoothing, LeakyReLU and Adam (β₁ = 0.5) keep the two networks in balance. |
Loss curves plus D(x) and D(G(z)) tracking, so you can see who is winning the game. |
|
Spherical interpolation (slerp) smoothly morphs one generated face into another. |
Seeded runs, one |
| 🎨 Generator | 🕵️ Discriminator |
|---|---|
|
|
| Setting | Value |
|---|---|
| 🖼️ Image size | 64 × 64 RGB, normalised to [-1, 1] |
| 🎲 Latent dimension | 100 |
| 📦 Batch size | 128 |
| 🔁 Epochs | 30 |
| 🧮 Optimizer | Adam, lr = 2e-4, β₁ = 0.5 |
| 📉 Loss | Binary cross-entropy |
| 🎯 Label smoothing | Real target = 0.9 (discriminator only) |
🔍 Why DCGAN and not a simple fully-connected GAN?
A plain MLP GAN flattens the image into one long vector and loses all 2-D structure, so it tends to produce blurry, noisy blobs. Convolutions share weights across the image and learn local patterns (edges, then eyes and noses, then whole faces), which is why DCGAN became the standard baseline for image generation.
🔁 What happens in one training step?
- Train the Discriminator. Show it a batch of real faces (target 0.9) and a batch of fakes (target 0). Fakes go through
.detach()so this step doesn't update the generator. - Train the Generator. Pass fresh fakes through the updated discriminator and reward the generator when it says "real" (target 1).
- Log the losses,
D(x)andD(G(z)).
In a healthy run, D(x) sits a little above 0.5, D(G(z)) a little below, and neither network runs away with the game.
- Click the Open in Colab badge at the top
Runtime → Change runtime type → T4 GPURuntime → Run all
git clone https://github.com/sumitjhadev/Generative-Adversarial-Network-GAN.git
cd Generative-Adversarial-Network-GAN
python -m venv .venv
source .venv/bin/activate # Windows: .venv\Scripts\activate
pip install -r requirements.txt
jupyter notebook gan_pipeline.ipynb💡 The dataset downloads automatically via
kagglehub. Already have the images? SetCONFIG["data_dir"]to their folder.
g = Generator(100).to(device)
g.load_state_dict(torch.load("checkpoints/generator_latest.pt", map_location=device))
g.eval()
with torch.no_grad():
faces = g(torch.randn(32, 100, device=device)).cpu()📦 Generative-Adversarial-Network-GAN
┣ 📓 gan_pipeline.ipynb ← data → model → training → results
┣ 📂 assets ← figures shown in this README
┃ ┣ 🎞️ training_progress.gif
┃ ┣ 🖼️ final_samples.png
┃ ┣ 🖼️ real_vs_fake.png
┃ ┣ 🖼️ latent_interpolation.png
┃ ┗ 📈 loss_curve.png
┣ 📄 requirements.txt
┣ 📄 LICENSE
┗ 📄 README.md
- DCGAN generator and discriminator
- Fixed-noise progress tracking and training GIF
- Latent-space interpolation
- Train on the full 200k-image CelebA
- WGAN-GP / spectral normalisation for more stable training
- FID score for quantitative evaluation
- Conditional generation (smiling, glasses, hair colour)
- Higher resolution (128 × 128)
| 📄 | Goodfellow et al., Generative Adversarial Networks, 2014 |
| 📄 | Radford, Metz & Chintala, Unsupervised Representation Learning with Deep Convolutional GANs, 2015 |
| 🗂️ | Liu et al., Deep Learning Face Attributes in the Wild (CelebA), ICCV 2015 |




