6.98 GB
17,351 files
Updated 6 months ago
Ctrl+K
| Name | Size | Uploaded | Xet hash |
|---|---|---|---|
| artifacts | 2 items | ||
| code | 2 items | ||
| description | 1 items | ||
| images | 47 items | ||
| report | 13 items | ||
| scripts | 2 items | ||
| .DS_Store | 8.2 kB xet | edfbfa1f | |
| CLAUDE.md | 3.99 kB xet | 35ec634c | |
| README.md | 5.23 kB xet | 0639af19 | |
| requirements.txt | 145 Bytes xet | d3042df0 |
CA2: GANs and Normalizing Flows on FashionMNIST
This project compares two generative modeling families on FashionMNIST:
- RealNVP Normalizing Flows (explicit likelihood, invertible mapping)
- DCGAN-style GAN (implicit likelihood, adversarial training)
It also includes a latent-space flow variant and FID-based evaluation.
Project Structure
code/CA2_GANs_and_NormalizingFlows_main.ipynb: main implementation notebook.code/CA2_question2_results.ipynb: results-focused notebook.requirements.txt: pinned dependencies.images/: exported figures and generated samples used in report.description/DGM_HW2.pdf: assignment prompt.report/: report files.
Setup
cd CA2_GANs_Normalizing_Flows
python -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt
Run notebook:
jupyter lab code/CA2_GANs_and_NormalizingFlows_main.ipynb
Frozen Results (No Re-Run)
This repository now supports a frozen-results workflow:
- No notebook execution
- No retraining
- Uses only saved
.ipynboutputs and existing files inimages/
Generate frozen metrics, figure manifest, and report include files:
python3 scripts/freeze_notebook_results.py
Verify report consistency and asset completeness:
python3 scripts/verify_frozen_results.py
Rebuild report PDFs (LaTeX only):
cd report
latexmk -pdf -interaction=nonstopmode CA2_Complete_Solutions.tex
cp -f CA2_Complete_Solutions.pdf DGM_CA2_final_EN.pdf
Frozen artifacts are written to:
artifacts/frozen_results/metrics.jsonartifacts/frozen_results/figure_manifest.jsonreport/generated_metrics.texreport/generated_fid_table.tex
Methods Used (Explained)
1) RealNVP in Pixel Space
Core classes/methods
class CouplingLayer:- Implements affine coupling transform with alternating masks.
forward(x): computes transformed outputyand log-determinant Jacobian.inverse(y): exact inverse mapping.
class RealNVP:- Stacks coupling layers.
forward(x): maps data -> latentz, accumulates log-det Jacobian.inverse(z): latent -> data generation.
Training method
- Negative log-likelihood optimization using change-of-variables:
z, log_det = model(x)log p(x) = log p(z) + log_detwith standard normal prior onz.
- Optimizer: Adam.
Evaluation/analysis methods
compute_log_likelihood(model, data_loader): estimates sample likelihoods.plot_kde(...): compares likelihood distributions across datasets.visualize_real_vs_generated(...): side-by-side samples.realnvp_latent_interpolation(...): interpolation in flow latent space.visualize_reconstruction_quality(...): checks invertibility/reconstruction.
2) Latent-Space Flow (Encoder/Decoder + RealNVP)
This branch first compresses images, then fits flow in latent space.
Core classes/methods
class Encoder: MLP mapping flattened image -> latent vector.class Decoder: MLP mapping latent vector -> reconstructed image.encode_dataset(encoder, data_loader): encodes full datasets.compute_log_likelihood_latent(model, data): latent-space likelihood analysis.
Training sequence
- Train
Encoder+Decoderwith MSE reconstruction loss. - Encode FashionMNIST/MNIST/KMNIST sets.
- Train RealNVP on encoded FashionMNIST latent vectors.
- Compare latent log-likelihood distributions across datasets.
3) GAN (DCGAN-style)
Core classes/methods
class Generator:- Transposed-convolution network from latent noise to image.
- BatchNorm + ReLU, final
tanhoutput.
class Discriminator:- Convolutional classifier producing real/fake probability.
- LeakyReLU + BatchNorm + sigmoid output.
class GAN:- Wraps generator/discriminator, BCE loss, Adam optimizers (
betas=(0.5, 0.999)).
- Wraps generator/discriminator, BCE loss, Adam optimizers (
Training method
- Alternating adversarial updates per batch:
- Update
Gto foolD(D(G(z)) -> real). - Update
Dwith real and detached fake batches.
- Update
- Tracks
G_lossesandD_lossesduring training.
GAN analysis methods
plot_enhanced_loss_analysis(...): detailed loss behavior visualization.visualize_discriminator_outputs(...): confidence distributions for real/fake.latent_space_interpolation(...): semantic interpolation in GAN latent space.
4) Quality Metric Method
- FID is computed via
pytorch-fid(calculate_fid_given_paths) using:- real image set
- generated image set
- Used to track image quality across epochs.
Hyperparameters Used in Notebook
RealNVP block
input_dim = 28*28hidden_dim = 1024num_coupling_layers = 8num_epochs = 10learning_rate = 1e-3batch_size = 128
GAN block
latent_dim = 100lr = 2e-4batch_size = 64epochs = 10
Outputs
- Generated and real image folders (created during FID workflow):
real_images/generated_images/
- Exported report figures:
images/.
Practical Notes
- The code is notebook-first; run cells in order.
- FID trends can fluctuate in GAN training; best epoch is not always final epoch.
- RealNVP offers exact likelihood and invertibility; GAN typically yields sharper samples.
- Total size
- 6.98 GB
- Files
- 17,351
- Last updated
- Jun 17
- Pre-warmed CDN
- US EU US EU