In this project, you will implement and train generative models on the MNIST handwritten digit dataset using PyTorch.
You must complete:
- an Energy-Based Model (EBM) for image inpainting (mandatory)
- one class-conditional generative model: Conditional GAN or Conditional VAE
The goal is to implement the core training components, run experiments, and evaluate model quality with the provided scripts.
By the end of this project, you will be able to:
- implement Langevin dynamics and contrastive divergence for energy-based learning
- build and train class-conditional generative models on image data
- implement the objective functions behind VAEs and GANs
- evaluate inpainting with MSE and conditional generation with FID
Prerequisites: Familiarity with Python, PyTorch, and deep learning
Environment Setup
-
Install uv:
curl -LsSf https://astral.sh/uv/install.sh | shIf you encounter network issues, use the mirror:
curl -LsSf https://gitee.com/wangnov/uv-custom/releases/download/latest/uv-installer-custom.sh | sh -
Install dependencies:
uv sync
Data Preparation
- MNIST is downloaded automatically to
data/the first time you run training or evaluation. - Download the FID evaluation checkpoint before running conditional-model evaluation:
bash download_checkpoints.sh| Property | Value |
|---|---|
| Training images | 60,000 |
| Validation images | 10,000 |
| Image shape | (1, 28, 28) |
| Number of classes | 10 |
| Data directory | data/ |
Unless explicitly allowed below, do not modify files marked as read-only.
deep-learning-coding-project-3/
├── modules/
│ ├── ebm.py # [TODO] EBM model implementation
│ ├── gan.py # [TODO] GAN generator and discriminator
│ └── vae.py # [TODO] Conditional VAE model
├── evaluate_ebm.py # [Read-only] EBM evaluation script (inpainting MSE)
├── evaluate_fid.py # [Read-only] GAN/VAE evaluation script (per-class FID)
├── download_checkpoints.sh # [Read-only] Download FID evaluation checkpoint
├── train_ebm.py # [TODO] EBM training logic
├── train_gan.py # [TODO] GAN training logic
├── train_vae.py # [TODO] VAE training logic
├── pyproject.toml # Project configuration and dependencies
└── uv.lock # Dependency lock file
You must complete EBM and exactly one of GAN or VAE.
Global requirements:
- Do not add new source files.
- Keep the public interfaces expected by the evaluation scripts unchanged.
- For
model.inpaint()andmodel.generate(), images pixel values should be in[0, 1]. - The sum of all submitted checkpoint files, before compression, must be under 200 MB.
Global notes:
- Save the checkpoint you consider your best final model for each required component.
- Periodically inspect generated or reconstructed samples during training.
- Small adjustments outside TODO blocks are allowed when necessary to make your implementation work correctly.
Task 1: Energy-Based Model (modules/ebm.py, train_ebm.py)
Complete:
CustomEBMModelinmodules/ebm.pytrain()intrain_ebm.py
Requirements:
- Implement the EBM as an MLP.
- Train it on MNIST for image inpainting, where alternating rows are corrupted with noise.
Notes:
- Naive contrastive divergence often diverges quickly; add an L2 regularization term
alpha(E_theta(x+)^2 + E_theta(x-)^2)to stabilize training. - Inspect generated or reconstructed samples during training to monitor behavior.
- You may consult Implicit Generation and Generalization in Energy Based Models for useful training tricks.
Task 2: Conditional GAN (modules/gan.py, train_gan.py)
If you choose GAN, complete:
CustomGANGeneratorinmodules/gan.pyCustomGANDiscriminatorinmodules/gan.py- the training loop in
train_gan.py
Requirements:
- Implement a class-conditional DCGAN-style model; use fully convolutional networks for both generator and discriminator, except for linear projection heads if needed.
- Generate recognizable class-conditional MNIST digits with reasonable within-class diversity.
- Avoid severe mode collapse.
Notes:
- Monitor generated images during training.
- See this overview for common mode-collapse mitigation strategies.
Task 3: Conditional VAE (modules/vae.py, train_vae.py)
If you choose VAE, complete:
CustomVAEModelinmodules/vae.pytrain()intrain_vae.py
Requirements:
- Implement a class-conditional VAE with an MLP encoder and an MLP decoder.
- Generate recognizable class-conditional MNIST digits with reasonable within-class diversity.
Notes:
- Assume the prior is
p(z)=N(0, I). - Assume both
q(z|x, y)andp(x|z, y)are Gaussian distributions. - Since
p(x|z, y)is modeled as a real-valued Gaussian while images lie in[0, 1], you may need to transform or scalexwhen computing the reconstruction term. - Using pre-trained models for initialization is permitted, but you must disclose any external resources used in your report.
Task 4: Report (report.md or report.pdf)
Create report.md or report.pdf in the repository root with the following sections:
- Cover Information - Your name and student ID.
- Generative AI Usage Disclosure - State
Noneif you did not use AI. Otherwise, describe which tool(s) you used and how. - EBM Implementation - Describe your energy model and training choices.
- Conditional Model Implementation - Describe your GAN or VAE architecture and training choices.
- Hyperparameters - Document batch size, learning rate, optimizer, epochs, and other key settings.
- Results - Include EBM inpainting examples and MSE. For your chosen conditional model, include generated samples, FID, and per-class sample standard deviation.
Train the EBM
uv run python train_ebm.py checkpoints/ebm_best.pthEvaluate the EBM
uv run python evaluate_ebm.py checkpoints/ebm_best.pthTrain the GAN
uv run python train_gan.py checkpoints/gan_best.pthEvaluate the GAN
uv run python evaluate_fid.py checkpoints/gan_best.pth --arch ganTo also save generated images:
uv run python evaluate_fid.py checkpoints/gan_best.pth --arch gan --generateGenerated images are saved under generated/gan/<digit>/<digit>_<idx>.png.
Train the VAE
uv run python train_vae.py checkpoints/vae_best.pthEvaluate the VAE
uv run python evaluate_fid.py checkpoints/vae_best.pth --arch vaeTo also save generated images:
uv run python evaluate_fid.py checkpoints/vae_best.pth --arch vae --generateGenerated images are saved under generated/vae/<digit>/<digit>_<idx>.png.
Evaluation outputs:
evaluate_ebm.pyreports inpainting MSE on the MNIST test split.evaluate_fid.pyreports per-class FID over digits0through9on the MNIST test split; it also reports per-class sample standard deviation.
Follow these steps to prepare your submission.
-
Finalize the required code files and your report file (
report.mdorreport.pdf). -
Create a ZIP archive named
submission.zipinclude the following files:modules/ebm.pytrain_ebm.pyreport.mdorreport.pdf- either
modules/gan.pyandtrain_gan.pyormodules/vae.pyandtrain_vae.py - your best checkpoint files
- generated images for your chosen conditional model, produced by
evaluate_fid.py --generate
Requirements:
- The sum of all submitted checkpoint files, before compression, must be under 200 MB. Submissions that violate the checkpoint-size limit may receive no credit.
- Submit only the checkpoint files needed for grading.
- Do not include additional source files beyond the required ones listed here.
You may run the following command to package everything in one shot:
zip -r submission.zip report.* modules/*.py train_*.py checkpoints/*.pth generated/*
-
One valid archive layout is:
submission.zip ├── report.md or report.pdf ├── generated/ │ └── gan/ or vae/ │ └── 0/ ... 9/ ├── modules/ │ ├── ebm.py │ ├── gan.py or vae.py │ └── __init__.py ├── train_ebm.py ├── train_gan.py or train_vae.py └── checkpoints/ ├── ebm_best.pth └── gan_best.pth or vae_best.pth
Your project will be evaluated as follows:
| Criteria | Weight | Description |
|---|---|---|
| EBM Performance | 40% | Inpainting quality, including the reconstruction metric and whether the recovered images are visually reasonable. |
| Conditional Model Performance | 40% | Generation quality for your chosen GAN or VAE, including FID, visual quality, and diversity. |
| Report | 20% | Completeness and clarity of implementation details, hyperparameters, results, and AI usage disclosure. |
For conditional generation, evaluation uses per-class FID over digits 0 through 9, along with a check that samples are recognizable and reasonably diverse.
For GAN, the following per-digit diversity thresholds are provided as reference:
| Digit | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
|---|---|---|---|---|---|---|---|---|---|---|
| Minimum std | 0.17 | 0.08 | 0.17 | 0.15 | 0.14 | 0.16 | 0.15 | 0.13 | 0.15 | 0.13 |
Major penalties or zero credit may apply if:
- a GAN submission does not use the required FCN generator and FCN discriminator
- the conditional model shows severe mode collapse or fails basic diversity requirements
- an EBM submission does not use the required MLP-style design, or a VAE submission does not use the required MLP encoder/decoder
- The sum of all submitted checkpoint files, before compression, exceeds 200MB.
Examples of penalized issues include:
- EBM outputs that do not meaningfully recover corrupted rows
- conditional samples that are not visually recognizable as digits
- conditional outputs with obvious artifacts, incorrect intensity range, or near-identical samples within a class
Grading Environment
Your submission will be executed on a grading platform with at least the following specifications:
| Resource | Specification |
|---|---|
| GPU VRAM | 32 GB |
| System RAM | 64 GB |
| Running Time | 30 minutes (in total) |
TA Reference Baseline (Not a Scoring Criterion)
One TA implementation (completed in tens of minutes) reported the following final metrics for quick sanity check only:
- EBM inpainting: MSE
0.02110 - Conditional GAN: mean FID
3.8279 - Conditional VAE: mean FID
3.5479
Plagiarism in any form will result in an F for the course.
You must disclose any use of generative AI tools in the Generative AI Usage Disclosure section of your report. If you did not use AI, state None. If you did, describe which tool(s) you used and how. Undisclosed AI use is a violation of academic integrity.
All submitted code must be your own work. You may discuss high-level ideas with classmates, but sharing or copying code is strictly prohibited.