Skip to content

About

A PyTorch DCGAN that learns to generate realistic 64×64 human faces from random noise, trained on the CelebA dataset.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

23 Commits

Folders and files

Repository files navigation

banner Typing SVG



Open In Colab PyTorch Python CelebA MIT



Generator learning to draw faces

The same 64 noise vectors, rendered after every epoch. Watch static turn into faces.



📖  What is this?

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
Loading

✨  Highlights

🧠 Architecture

Transposed-conv generator and strided-conv discriminator, following Radford et al. (2015), with DCGAN weight init and BatchNorm.

🎞️ Watchable training

A fixed noise batch is rendered every epoch, and the notebook auto-builds a GIF of the generator improving.

🛡️ Stable training

One-sided label smoothing, LeakyReLU and Adam (β₁ = 0.5) keep the two networks in balance.

📊 Real diagnostics

Loss curves plus D(x) and D(G(z)) tracking, so you can see who is winning the game.

🌀 Latent-space morphing

Spherical interpolation (slerp) smoothly morphs one generated face into another.

♻️ Reproducible

Seeded runs, one CONFIG dictionary and saved checkpoints. Reload the generator and sample new faces any time.


🖼️  Results

Real vs. Generated

Real faces vs generated faces



Final samples

Generated faces grid



Latent-space interpolation

Each row morphs one generated face into another, left to right.

Latent space interpolation



Training curves

Loss curves and discriminator confidence

🏗️  Architecture

🎨 Generator 🕵️ Discriminator
z  (100)
 ↓  ConvT + BN + ReLU
512 × 4 × 4
 ↓  ConvT + BN + ReLU
256 × 8 × 8
 ↓  ConvT + BN + ReLU
128 × 16 × 16
 ↓  ConvT + BN + ReLU
 64 × 32 × 32
 ↓  ConvT + Tanh
  3 × 64 × 64
  3 × 64 × 64
 ↓  Conv + LeakyReLU
 64 × 32 × 32
 ↓  Conv + BN + LeakyReLU
128 × 16 × 16
 ↓  Conv + BN + LeakyReLU
256 × 8 × 8
 ↓  Conv + BN + LeakyReLU
512 × 4 × 4
 ↓  Conv + Sigmoid
  P(real)

⚙️ Training setup

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?
  1. 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.
  2. Train the Generator. Pass fresh fakes through the updated discriminator and reward the generator when it says "real" (target 1).
  3. Log the losses, D(x) and D(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.


🚀  Quick start

☁️ Option 1: Google Colab (easiest)

  1. Click the Open in Colab badge at the top
  2. Runtime → Change runtime type → T4 GPU
  3. Runtime → Run all

💻 Option 2: Run locally

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? Set CONFIG["data_dir"] to their folder.

🎲 Generate new faces from the saved model

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()

📁  Project structure

📦 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

🔭  Roadmap

  • 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)

📚  References

📄 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

⭐ If you found this useful, consider giving it a star!

Released under the MIT License

footer

About

A PyTorch DCGAN that learns to generate realistic 64×64 human faces from random noise, trained on the CelebA dataset.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages