Capstone 6: A class-conditional latent flow model, from scratch
Each Part 6 notebook built one piece at small scale. This capstone builds the whole latent generative model once, properly, on images with colour and texture: the recipe behind Stable Diffusion 3 and similar models, small enough for a free Kaggle or Colab GPU. Your 6.6, 6.7 and 6.8 files supply the loss, the sampler, guidance and the DiT; you write the training scripts, run them, and measure what you get. Start on MNIST to debug in minutes, then move to CIFAR-10 (32 × 32 colour images in 10 classes).
1. The autoencoder
Section titled “1. The autoencoder”Train a convolutional autoencoder from 3 × 32 × 32 to a latent of 4 × 8 × 8 (f = 4), with the reparameterised encoder and the small KL weight of the 6.8 notebook (start around 10⁻⁶ to 10⁻⁴ per dimension relative to a per-pixel reconstruction loss, and tune it). Use a few residual blocks (3.7) with GroupNorm at each resolution.
- Report the test reconstruction error (mean squared error per pixel and PSNR, as in Capstone 1) and show originals beside reconstructions.
- Check the latents: their mean and standard deviation per channel, and the scale factor from your 6.8
latent_scale. Decode a few latents with Gaussian noise of standard deviation 0.1 added and confirm the images change only a little; if they turn to garbage, raise the KL weight. - Encode the whole training set once and save the latent means to disk, so the diffusion stage never runs the encoder.
2. Flow matching on latents
Section titled “2. Flow matching on latents”Train your 6.8 DiT (patch size 2, so 16 tokens; start with width 256, depth 6, 4 heads) on the scaled latents with your 6.7 cond_fm_loss (10% of labels dropped). Use AdamW, a learning-rate warm-up, gradient clipping and an exponential moving average of the weights (decay about 0.999) for sampling; compare samples from the raw and the averaged weights once.
- Log the loss, and every few thousand steps a fixed grid: the same noise and labels, decoded, so you can watch training improve.
- Sample with your 6.7
sample_cfg, 50 Euler steps, then decode with your autoencoder.
3. Measure it
Section titled “3. Measure it”- Class accuracy. Train a small CIFAR-10 classifier (the 3.7 ResNet reaches about 90% in well under an hour on a free GPU). Generate 1,000 images per class at guidance w = 1, 2, 4 and report how often each is classified as its label.
- FID. Compute the Fréchet Inception Distance between 10,000 generated images and the test set, with torchmetrics or clean-fid. Report it for w = 1, 2, 4 and for 10, 25 and 50 Euler steps, and for the decoded test set itself: that is the floor your autoencoder sets.
- Plot accuracy and FID against w on one figure, and explain the shape with what the 6.7 widget showed.
4. One ablation
Section titled “4. One ablation”Pick one, change only it, and compare FID and accuracy at the same training compute:
- Pixels against latents. Train the same DiT on pixels with patch size 4 (64 tokens) and compare quality and time per step.
- The latent. Retrain the autoencoder with a much larger and a much smaller KL weight, or with f = 2 or 8.
- The path. Replace the straight path with 6.5’s DDPM objective and sampler at the same number of network evaluations.
- The sampler. Heun against Euler at equal numbers of network evaluations.
Write-up
Section titled “Write-up”Two pages: the architecture and its parameter counts, training time on your GPU, the reconstruction and generation results with sample grids, the accuracy and FID against w figure, your ablation, and what you would change with ten times the compute. Name anything you could not run.
Learner log: time spent and points of confusion
Every confusion you note becomes a fix before the module is marked stable. Export it from the progress page and attach it to an issue.