Skip to content

About

Three GAN architectures implemented from scratch using PyTorch Lihtning following original research papers.

Topics

Resources

Stars

7 stars

Watchers

1 watching

Forks

Repository files navigation

GANs from Scratch

Three GAN variants implemented with PyTorch, PyTorch Lightning, and Hydra: a fully connected GAN, a convolutional DCGAN, and a label-conditioned GAN. The repository includes training code and saved sample grids for MNIST, CIFAR-10, and Fashion-MNIST.

DCGAN Results

Quick Start

Requires Python 3.11+ and uv. The training wrappers currently set accelerator="mps", so the default command requires an available Apple MPS device. To use CPU or CUDA, change the trainer accelerator in src/training/train.py. Dataset loaders download data into ./data when it is not already present.

# Install dependencies
uv sync

# Configure which model to train
# Edit src/config/config.yaml, for example:
# model: gan   # or cgan, dcgan
# epochs: 100

# Run training
uv run python -m src.main

Implemented Models

  • Vanilla GAN - Basic adversarial training on MNIST
  • DCGAN - Convolutional architecture for CIFAR-10
  • Conditional GAN - Label-conditioned generation on Fashion-MNIST

Saved Training Samples

The grids below are saved outputs labeled by epoch. They provide examples of generated images, not a quantitative evaluation of quality or a reproduction of published benchmark results. The training configuration defaults to 100 epochs.

DCGAN (CIFAR-10)

32x32 RGB Images

Epoch 0 Epoch 30 Epoch 60 Epoch 90
DCGAN 0 DCGAN 30 DCGAN 60 DCGAN 90

Vanilla GAN (MNIST)

28x28 Grayscale

Epoch 0 Epoch 30 Epoch 60 Epoch 90
GAN 0 GAN 30 GAN 60 GAN 90

Conditional GAN (Fashion-MNIST)

28x28 Grayscale + Labels

Epoch 0 Epoch 30 Epoch 60 Epoch 90
CGAN 0 CGAN 30 CGAN 60 CGAN 90

Features

  • Model Implementations: Fully connected, convolutional, and conditional GAN variants
  • Multiple Datasets: MNIST, Fashion-MNIST, CIFAR-10
  • Real-time Monitoring: Progress displayed every epoch
  • Image Generation: Results saved every 10 epochs

Project Structure

gan-architectures-from-scratch-pytorch/
├── results/                 # Generated images (saved every 10 epochs)
│   ├── gan/                 # Vanilla GAN outputs
│   ├── dcgan/               # DCGAN outputs
│   └── cgan/                # Conditional GAN outputs
└── src/
    ├── main.py              # Training entry point (reads src/config/config.yaml)
    ├── config/
    │   └── config.yaml      # Minimal config: model, epochs
    ├── models/              # GAN architectures (LightningModule)
    │   ├── gan.py           # Vanilla GAN
    │   ├── dcgan.py         # Deep Convolutional GAN
    │   └── cgan.py          # Conditional GAN
    ├── training/
    │   ├── train.py         # Trainer wrappers (PyTorch Lightning)
    │   └── loss.py          # Loss functions
    ├── data/
    │   └── data.py          # Dataset loaders
    └── utils/
        └── utils.py         # Helper functions

Implementation Details

Generator Architectures

  • Vanilla GAN: MLP → 784 outputs (28×28)
  • DCGAN: ConvTranspose2d → 32×32×3 RGB
  • Conditional GAN: MLP with concatenated one-hot class labels → 784 outputs

Training Configuration

  • Optimizer: Adam (lr=0.0002)
  • Loss: Binary cross entropy for the discriminator and a non-saturating generator loss (fake samples are assigned real targets for the generator update)
  • Batch Size: 100
  • Epochs: 100 by default; configurable in src/config/config.yaml
  • Device: MPS is hard-coded in the training wrappers; CPU/CUDA requires changing the accelerator

Scope and Limitations

  • The saved grids show outputs for the three model/dataset combinations above. The repository does not report quantitative image-quality metrics.
  • The conditional model takes one-hot class labels during training and sample generation; its saved grids use randomly sampled labels and do not measure class consistency.
  • These are implementations of the model families described in the papers below, with local architecture and training choices. They do not establish exact reproduction of the papers' architectures or results.

Installation

git clone https://github.com/00200200/gan-architectures-from-scratch-pytorch
cd gan-architectures-from-scratch-pytorch
uv sync

Usage

# Select model in config
# src/config/config.yaml
# model: gan   # or cgan, dcgan
# epochs: 100

# Run
uv run python -m src.main

# Results are saved under results/<model>/ every 10 epochs

Training Progress

Sample grids are saved every 10 epochs in results/<model>/, starting at epoch index 0. Each grid uses newly sampled noise; conditional grids also use newly sampled labels. They are not fixed-input comparisons across epochs.

References

PyTorch defines the models; PyTorch Lightning handles the training loop and Hydra handles configuration.

About

Three GAN architectures implemented from scratch using PyTorch Lihtning following original research papers.

Topics

Resources

Stars

7 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages