Add README.md from the overnight run
Browse files
README.md
ADDED
|
@@ -0,0 +1,139 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
license: other
|
| 3 |
+
license_name: celeba-non-commercial-research
|
| 4 |
+
license_link: https://mmlab.ie.cuhk.edu.hk/projects/CelebA.html
|
| 5 |
+
tags:
|
| 6 |
+
- diffusion
|
| 7 |
+
- ddpm
|
| 8 |
+
- ddim
|
| 9 |
+
- unconditional-image-generation
|
| 10 |
+
- from-scratch
|
| 11 |
+
- pytorch
|
| 12 |
+
- mps
|
| 13 |
+
datasets:
|
| 14 |
+
- flwrlabs/celeba
|
| 15 |
+
pipeline_tag: unconditional-image-generation
|
| 16 |
+
library_name: pytorch
|
| 17 |
+
---
|
| 18 |
+
|
| 19 |
+
# mini-diffusion β an 18.5M-parameter DDPM trained overnight on a laptop
|
| 20 |
+
|
| 21 |
+
A denoising diffusion model written from scratch in plain PyTorch. The U-Net, the noise schedule,
|
| 22 |
+
four samplers and the training loop are all in the repository β `diffusers` is never imported.
|
| 23 |
+
|
| 24 |
+
**Code: https://github.com/vous99/mini-diffusion**
|
| 25 |
+
|
| 26 |
+
Trained on one MacBook Pro (M5 Pro, 24 GB, MPS) in a single overnight run. Every number below was
|
| 27 |
+
measured on that run, not quoted from a paper.
|
| 28 |
+
|
| 29 |
+
## Results
|
| 30 |
+
|
| 31 |
+
| Metric | Value |
|
| 32 |
+
|---|---|
|
| 33 |
+
| **FID-tv** | **30.12** (10,000 samples, DDIM-50, EMA weights) |
|
| 34 |
+
| Steps | 22,098 |
|
| 35 |
+
| Images seen | 2.83M (17.4 epochs of CelebA train) |
|
| 36 |
+
| Best val loss | 0.0352 (`L_simple`, MSE on epsilon) |
|
| 37 |
+
| Training time | ~9 hours of compute at 87 images/s |
|
| 38 |
+
| Parameters | 18,538,371 |
|
| 39 |
+
|
| 40 |
+
FID over training: 73.35 (step 2,500) β 50.58 β 43.10 β 39.37 β 37.69 β 35.57 β 34.67 β 35.17
|
| 41 |
+
(step 20,000). Most of the gain lands in the first third of the run; the curve flattens after
|
| 42 |
+
roughly step 15,000.
|
| 43 |
+
|
| 44 |
+
**FID-tv is not the FID of the literature.** It uses torchvision's Inception-v3 weights rather than
|
| 45 |
+
the TensorFlow-ported weights every published FID is built on. The numbers here are internally
|
| 46 |
+
consistent β valid for comparing checkpoints, samplers and guidance scales β but not directly
|
| 47 |
+
comparable to a paper's "FID 3.5".
|
| 48 |
+
|
| 49 |
+
## What the model does
|
| 50 |
+
|
| 51 |
+
- Unconditional face generation at 64Γ64.
|
| 52 |
+
- Conditional generation on six CelebA attributes: `Male`, `Smiling`, `Young`, `Eyeglasses`,
|
| 53 |
+
`Blond_Hair`, `Bangs`.
|
| 54 |
+
- Classifier-free guidance from the same weights β trained with 15% condition dropout against a
|
| 55 |
+
learned null embedding, so one checkpoint serves both the conditional and unconditional branch.
|
| 56 |
+
- Deterministic DDIM with an `eta` parameter that reproduces ancestral DDPM exactly at `eta=1`.
|
| 57 |
+
- Karras sigma schedule with Heun's method.
|
| 58 |
+
- DDIM inversion, and therefore spherical interpolation between two real photographs.
|
| 59 |
+
|
| 60 |
+
## Usage
|
| 61 |
+
|
| 62 |
+
```bash
|
| 63 |
+
git clone https://github.com/vous99/mini-diffusion && cd mini-diffusion
|
| 64 |
+
pip install -r requirements.txt
|
| 65 |
+
|
| 66 |
+
python -c "
|
| 67 |
+
from huggingface_hub import hf_hub_download
|
| 68 |
+
import shutil, os
|
| 69 |
+
os.makedirs('ckpt', exist_ok=True)
|
| 70 |
+
shutil.copy(hf_hub_download('vous99/mini-diffusion', 'best.pt'), 'ckpt/best.pt')
|
| 71 |
+
"
|
| 72 |
+
|
| 73 |
+
python sample.py # an 8x8 grid
|
| 74 |
+
python sample.py --attrs "Male=1,Eyeglasses=1" # conditional
|
| 75 |
+
python sample.py --guidance-sweep # the guidance-scale figure
|
| 76 |
+
python sample.py --sampler-comparison # DDPM / DDIM / Heun
|
| 77 |
+
```
|
| 78 |
+
|
| 79 |
+
Sampling needs no dataset. Retraining does β `prepare_data.py` rebuilds it from the HuggingFace
|
| 80 |
+
CelebA shards.
|
| 81 |
+
|
| 82 |
+
## Architecture
|
| 83 |
+
|
| 84 |
+
U-Net over four resolutions, `base_ch=64`, `channel_mults=(1,2,2,4)` β 64@64Β² 128@32Β² 128@16Β²
|
| 85 |
+
256@8Β². Two ResBlocks per level down, three up, self-attention at 16Β² and 8Β² and in the middle
|
| 86 |
+
block, `head_dim=32`, GroupNorm with 32 groups.
|
| 87 |
+
|
| 88 |
+
Conditioning is a `Linear(6, 256)` projection added to the sinusoidal timestep embedding, plus a
|
| 89 |
+
learned null vector. Stable Diffusion instead cross-attends to a *sequence* of text tokens; six
|
| 90 |
+
fixed flags are not a sequence, so addition is the honest analogue β the same mechanism, a simpler
|
| 91 |
+
carrier.
|
| 92 |
+
|
| 93 |
+
The output convolution is zero-initialised, so the loss at step 0 is exactly `E||eps||Β² = 1.0000`.
|
| 94 |
+
That single number confirms the target is epsilon, the data is scaled to [-1,1], and the reduction
|
| 95 |
+
is a mean.
|
| 96 |
+
|
| 97 |
+
## Training details
|
| 98 |
+
|
| 99 |
+
| | |
|
| 100 |
+
|---|---|
|
| 101 |
+
| Schedule | cosine (Nichol & Dhariwal), T=1000 |
|
| 102 |
+
| Prediction target | epsilon |
|
| 103 |
+
| Timestep sampling | stratified over the batch, not i.i.d. uniform |
|
| 104 |
+
| Optimizer | AdamW, lr 2e-4, betas (0.9, 0.999), weight decay 0, grad clip 1.0 |
|
| 105 |
+
| LR schedule | linear warmup 500 steps β cosine decay to 2e-5 |
|
| 106 |
+
| Batch | 64 Γ 2 gradient accumulation = 128 effective |
|
| 107 |
+
| EMA | 0.999 with warmup |
|
| 108 |
+
| Dropout | 0.0 |
|
| 109 |
+
| Augmentation | horizontal flip, p=0.5 |
|
| 110 |
+
| Precision | bfloat16 autocast (GroupNorm stays fp32) |
|
| 111 |
+
|
| 112 |
+
## Limitations
|
| 113 |
+
|
| 114 |
+
- **64Γ64 only.** Faces at higher resolution are out of scope for this budget.
|
| 115 |
+
- **No text conditioning.** The condition is six binary flags, not a prompt.
|
| 116 |
+
- **Aligned frontal portraits only.** CelebA is centred faces; profiles, multiple people and full
|
| 117 |
+
bodies are out of distribution.
|
| 118 |
+
- **Roughly 15β20% of samples collapse into noise.** This is the dominant remaining defect.
|
| 119 |
+
- **Colour casts.** Even the cosine schedule ends at `abar_T β 2.4e-9` rather than 0, so the model
|
| 120 |
+
never trains on pure noise but is handed pure noise at sampling time. With epsilon-prediction
|
| 121 |
+
this shows up as a per-image brightness and colour bias. It receded substantially over training
|
| 122 |
+
but has not disappeared; v-prediction with zero terminal SNR is the proper fix.
|
| 123 |
+
- **This is 4.4% of the compute budget of the DDIM paper's CelebA-64 run.** The register is a good
|
| 124 |
+
2016 GAN, not "indistinguishable from a photograph".
|
| 125 |
+
|
| 126 |
+
## Licence and intended use
|
| 127 |
+
|
| 128 |
+
The weights inherit the **CelebA licence: non-commercial research use only**. This is a study
|
| 129 |
+
artifact for understanding how diffusion models work, not a product component.
|
| 130 |
+
|
| 131 |
+
It generates synthetic faces of people who do not exist. Do not use it to impersonate real people
|
| 132 |
+
or to produce material presented as a genuine photograph of anyone.
|
| 133 |
+
|
| 134 |
+
## Checkpoint contents
|
| 135 |
+
|
| 136 |
+
`best.pt` is a `torch.save` dict with `model` (raw weights), `ema` (averaged weights β what you
|
| 137 |
+
should sample from), `cfg` (the `UNetConfig`), `diffusion` (the `DiffusionConfig`), `attributes`
|
| 138 |
+
(the six conditioning names, in order), `iter` and `val_loss`. Load it with `checkpoint.py` from
|
| 139 |
+
the GitHub repository.
|