Implemented Diffusers architechture from scratch!
Browse files
README.md
ADDED
|
@@ -0,0 +1,135 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
# DDPM — Fashion-MNIST
|
| 3 |
+
|
| 4 |
+
A simple DDPM implementation built from scratch using PyTorch.
|
| 5 |
+
|
| 6 |
+
The model learns to predict the noise added to a real Fashion-MNIST image.
|
| 7 |
+
|
| 8 |
+
## Pipeline
|
| 9 |
+
|
| 10 |
+
Training:
|
| 11 |
+
|
| 12 |
+
x₀ → add noise → xₜ → U-Net → predicted noise
|
| 13 |
+
↓
|
| 14 |
+
MSE Loss
|
| 15 |
+
|
| 16 |
+
Sampling:
|
| 17 |
+
|
| 18 |
+
Gaussian Noise
|
| 19 |
+
↓
|
| 20 |
+
U-Net
|
| 21 |
+
↓
|
| 22 |
+
Denoising
|
| 23 |
+
↓
|
| 24 |
+
U-Net
|
| 25 |
+
↓
|
| 26 |
+
...
|
| 27 |
+
↓
|
| 28 |
+
Generated Image
|
| 29 |
+
|
| 30 |
+
## Model
|
| 31 |
+
|
| 32 |
+
The U-Net takes:
|
| 33 |
+
|
| 34 |
+
(xₜ, t)
|
| 35 |
+
|
| 36 |
+
and predicts:
|
| 37 |
+
|
| 38 |
+
εθ(xₜ, t)
|
| 39 |
+
|
| 40 |
+
The U-Net contains:
|
| 41 |
+
|
| 42 |
+
- Time embedding
|
| 43 |
+
- Input convolution
|
| 44 |
+
- 2 downsampling blocks
|
| 45 |
+
- 2 middle ResBlocks
|
| 46 |
+
- 2 upsampling blocks
|
| 47 |
+
- Skip connections
|
| 48 |
+
- GroupNorm
|
| 49 |
+
- SiLU activation
|
| 50 |
+
- Final output convolution
|
| 51 |
+
|
| 52 |
+
Input:
|
| 53 |
+
|
| 54 |
+
[B, 1, 28, 28]
|
| 55 |
+
|
| 56 |
+
Output:
|
| 57 |
+
|
| 58 |
+
[B, 1, 28, 28]
|
| 59 |
+
|
| 60 |
+
The output represents predicted Gaussian noise.
|
| 61 |
+
|
| 62 |
+
## Forward Diffusion
|
| 63 |
+
|
| 64 |
+
We use:
|
| 65 |
+
|
| 66 |
+
xₜ = √ᾱₜ x₀ + √(1 - ᾱₜ) ε
|
| 67 |
+
|
| 68 |
+
where:
|
| 69 |
+
|
| 70 |
+
ε ~ N(0, I)
|
| 71 |
+
|
| 72 |
+
## Training
|
| 73 |
+
|
| 74 |
+
For every batch:
|
| 75 |
+
|
| 76 |
+
1. Take a real image xâ‚€.
|
| 77 |
+
2. Select a random timestep t.
|
| 78 |
+
3. Generate Gaussian noise ε.
|
| 79 |
+
4. Create xₜ using the forward diffusion process.
|
| 80 |
+
5. Give xₜ and t to the U-Net.
|
| 81 |
+
6. Predict the noise.
|
| 82 |
+
7. Calculate MSE between predicted and actual noise.
|
| 83 |
+
8. Update the U-Net.
|
| 84 |
+
|
| 85 |
+
## Reverse Diffusion
|
| 86 |
+
|
| 87 |
+
During generation, we start from:
|
| 88 |
+
|
| 89 |
+
xT ~ N(0, I)
|
| 90 |
+
|
| 91 |
+
and move backwards:
|
| 92 |
+
|
| 93 |
+
xT → xT-1 → ... → x1 → x0
|
| 94 |
+
|
| 95 |
+
At every timestep, the U-Net predicts the noise and the DDPM scheduler performs one reverse step.
|
| 96 |
+
|
| 97 |
+
## Checkpoint
|
| 98 |
+
|
| 99 |
+
Model weights are saved using:
|
| 100 |
+
|
| 101 |
+
.safetensors
|
| 102 |
+
|
| 103 |
+
Example:
|
| 104 |
+
|
| 105 |
+
checkpoints/ddpm_fashion_mnist.safetensors
|
| 106 |
+
|
| 107 |
+
The model architecture must be recreated before loading the weights.
|
| 108 |
+
|
| 109 |
+
## Dataset
|
| 110 |
+
|
| 111 |
+
Dataset:
|
| 112 |
+
|
| 113 |
+
Fashion-MNIST
|
| 114 |
+
|
| 115 |
+
Image size:
|
| 116 |
+
|
| 117 |
+
28 × 28
|
| 118 |
+
|
| 119 |
+
Channels:
|
| 120 |
+
|
| 121 |
+
1
|
| 122 |
+
|
| 123 |
+
## Goal
|
| 124 |
+
|
| 125 |
+
The goal of this project is to understand DDPM from first principles:
|
| 126 |
+
|
| 127 |
+
Forward Diffusion
|
| 128 |
+
↓
|
| 129 |
+
U-Net
|
| 130 |
+
↓
|
| 131 |
+
Noise Prediction
|
| 132 |
+
↓
|
| 133 |
+
Reverse Diffusion
|
| 134 |
+
↓
|
| 135 |
+
Image Generation
|
config.py
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
import torch
|
| 3 |
+
|
| 4 |
+
|
| 5 |
+
# =========================
|
| 6 |
+
# Device
|
| 7 |
+
# =========================
|
| 8 |
+
|
| 9 |
+
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
# =========================
|
| 13 |
+
# Dataset
|
| 14 |
+
# =========================
|
| 15 |
+
|
| 16 |
+
IMAGE_SIZE = 28
|
| 17 |
+
CHANNELS = 1
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
# =========================
|
| 21 |
+
# DDPM
|
| 22 |
+
# =========================
|
| 23 |
+
|
| 24 |
+
TIMESTEPS = 1000
|
| 25 |
+
|
| 26 |
+
BETA_START = 1e-4
|
| 27 |
+
BETA_END = 0.02
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
# =========================
|
| 31 |
+
# Model
|
| 32 |
+
# =========================
|
| 33 |
+
|
| 34 |
+
TIME_EMBED_DIM = 128
|
| 35 |
+
|
| 36 |
+
BASE_CHANNELS = 64
|
| 37 |
+
MID_CHANNELS = 128
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
# =========================
|
| 41 |
+
# Training
|
| 42 |
+
# =========================
|
| 43 |
+
|
| 44 |
+
BATCH_SIZE = 128
|
| 45 |
+
EPOCHS = 20
|
| 46 |
+
|
| 47 |
+
LEARNING_RATE = 2e-4
|
| 48 |
+
WEIGHT_DECAY = 1e-4
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
# =========================
|
| 52 |
+
# Checkpoints
|
| 53 |
+
# =========================
|
| 54 |
+
|
| 55 |
+
CHECKPOINT_DIR = "checkpoints"
|
| 56 |
+
|
| 57 |
+
MODEL_NAME = "ddpm_fashion_mnist.safetensors"
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
# =========================
|
| 61 |
+
# Sampling
|
| 62 |
+
# =========================
|
| 63 |
+
|
| 64 |
+
NUM_SAMPLES = 16
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
# =========================
|
| 68 |
+
# Random Seed
|
| 69 |
+
# =========================
|
| 70 |
+
|
| 71 |
+
SEED = 42
|