This project implements a Denoising Diffusion Probabilistic Model (DDPM) for generating handwritten digit images using the MNIST dataset.
- Python >= 3.10
- CUDA-capable GPU (recommended)
- Install the required packages:
pip install -r requirements.txtThe RGB MNIST dataset should be downloaded separately and is NOT included in this repository.
Download Instructions:
-
Download the RGB MNIST dataset from the course-provided Google Drive link (check your assignment description or course website)
-
Alternatively, you can download from this Google Drive link:
[Insert Google Drive Link Here]
3.Alternatively,there's another way to download:
This repository provides the MNIST dataset zip file (mnist.zip), which can be used for image generation or other machine learning experiments.
Please download the dataset from the Hugging Face repository:
You can either click the Download button or use Git + Git LFS:
git lfs install
git clone https://huggingface.co/grandmaeatsadumpling/Generate-Images-DatasetAfter downloading, the dataset file will be located at:
Generate-Images-Dataset/mnist.zip
Extract mnist.zip to use the dataset:
unzip mnist.zip -d ./mnist_data- Make sure Git LFS is installed; otherwise, large files cannot be downloaded correctly.
- The dataset file may be large, so please be patient while downloading.
-
Extract the downloaded dataset to create the following directory structure:
data/mnist_rgb/ ├── 0/ │ ├── image1.png │ ├── image2.png │ └── ... ├── 1/ │ ├── image1.png │ └── ... ├── 2/ │ ├── ... └── 9/ └── ... -
Update the
--data_pathargument when running the training script to point to your dataset location.
Important: The data/mnist_rgb/ directory containing training images should NOT be committed to the repository to reduce repository size.
To train the DDPM model, run:
cd src
python train.py --data_path /path/to/mnist_rgb --epochs 100 --batch_size 128--data_path: Path to the RGB MNIST dataset (default:./data/mnist_rgb)--image_size: Size of the images (default:28)--timesteps: Number of diffusion timesteps (default:1000)--beta_start: Starting value of beta schedule (default:0.0001)--beta_end: Ending value of beta schedule (default:0.02)--time_emb_dim: Dimension of time embedding (default:128)--base_dim: Base dimension for UNet (default:64)--epochs: Number of training epochs (default:100)--batch_size: Training batch size (default:128)--lr: Learning rate (default:2e-4)--checkpoint_dir: Directory to save checkpoints (default:./checkpoints)--sample_dir: Directory to save sample images during training (default:./samples)--save_interval: Checkpoint save interval in epochs (default:10)--sample_interval: Sample generation interval in steps (default:500)
python train.py \
--data_path ../data/mnist_rgb \
--epochs 100 \
--batch_size 128 \
--lr 2e-4 \
--checkpoint_dir ../checkpoints \
--sample_dir ../samplesTo generate 10,000 images for FID evaluation:
cd src
python generate.py \
--checkpoint ../checkpoints/best_model.pt \
--output_dir ../generated_images \
--num_images 10000 \
--batch_size 64 \
--mode generateThe generated images will be saved as 00001.png, 00002.png, ..., 10000.png in the output directory.
To visualize the diffusion process with intermediate steps:
python generate.py \
--checkpoint ../checkpoints/best_model.pt \
--output_dir ../diffusion_visualization \
--mode visualizeThis will generate a grid showing 8 samples across 8 timesteps (from noise to final image), which can be used in your report.
--checkpoint: Path to the trained model checkpoint (required)--output_dir: Directory to save generated images (default:./generated_images)--num_images: Number of images to generate (default:10000)--batch_size: Batch size for generation (default:64)--mode: Generation mode -generateorvisualize(default:generate)- Other model parameters should match those used during training
After generating images, you can calculate the FID score using pytorch-fid:
- Install pytorch-fid:
pip install pytorch-fid- Calculate FID score:
# Using the training dataset
python -m pytorch_fid generated_images data/mnist_rgb
# Or using the precalculated statistics
python -m pytorch_fid generated_images mnist.npz.
├── src/
│ ├── model.py # DDPM model implementation
│ ├── train.py # Training script
│ └── generate.py # Image generation script
├── requirements.txt # Python dependencies
└── readme.md # This file
The implementation uses a UNet architecture with:
- Time embeddings using sinusoidal position encodings
- GroupNorm for normalization
- SiLU (Swish) activation functions
- Skip connections between encoder and decoder
The diffusion process follows the DDPM paper:
- Forward process: Gradually adds Gaussian noise to images
- Reverse process: Learns to denoise images step by step
- Uses a linear beta schedule from
beta_starttobeta_end
- Training Duration: Train for at least 80-100 epochs for good results
- Learning Rate: Use a learning rate around 1e-4 to 2e-4
- Batch Size: Use the largest batch size your GPU can handle (128 or 256 recommended)
- Timesteps: 1000 timesteps is a good default
- Model Size: Adjust
base_dimbased on your GPU memory (64 is a good balance)
- Reduce
batch_size - Reduce
base_dimin the model - Use gradient accumulation
- Train for more epochs
- Adjust learning rate
- Try different beta schedules
- Increase model capacity (
base_dim)
- Increase
batch_sizeif possible - Reduce
num_workersin DataLoader - Use mixed precision training (requires code modification)
- DDPM Paper: Denoising Diffusion Probabilistic Models
- pytorch-fid: GitHub Repository