Update README.md
Browse files
README.md
CHANGED
|
@@ -27,7 +27,7 @@ Random Noise → [VAE Encoder] → Latent (16ch) → [DiT (12 blocks)] → Denoi
|
|
| 27 |
|
| 28 |
### VAE (Autoencoder)
|
| 29 |
|
| 30 |
-
The VAE compresses 256×256 RGB images into a **16-channel latent** with spatial size 32×32 (downsampled by 8×). It uses residual blocks with GroupNorm and SiLU activations. During training, a
|
| 31 |
|
| 32 |
### DiT (Diffusion Transformer)
|
| 33 |
|
|
@@ -35,28 +35,47 @@ The DiT operates on patched latents (patch size 4 → 8×8 = 64 patches). Each b
|
|
| 35 |
- **Self-attention** for spatial relationships
|
| 36 |
- **Cross-attention** conditioned on text embeddings
|
| 37 |
- **Adaptive timestep conditioning** via an MLP-projected sinusoidal embedding
|
|
|
|
| 38 |
|
| 39 |
### Diffusion Process
|
| 40 |
|
| 41 |
-
Standard DDPM (Denoising Diffusion Probabilistic Model) with 1000 timesteps
|
| 42 |
|
| 43 |
---
|
| 44 |
|
| 45 |
## Training
|
| 46 |
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
- **
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
|
| 56 |
### Training Phases
|
| 57 |
|
| 58 |
-
1. **VAE Pretraining** — Train encoder + decoder on image reconstruction to establish a meaningful latent space.
|
| 59 |
-
2. **DiT Diffusion Training** — Freeze VAE, train DiT to denoise latents conditioned on text embeddings.
|
| 60 |
|
| 61 |
---
|
| 62 |
|
|
@@ -68,6 +87,11 @@ Standard DDPM (Denoising Diffusion Probabilistic Model) with 1000 timesteps, cos
|
|
| 68 |
pip install torch safetensors sentence-transformers pillow numpy huggingface-hub
|
| 69 |
```
|
| 70 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
### Inference (standalone — loads from Hugging Face)
|
| 72 |
|
| 73 |
```python
|
|
@@ -78,33 +102,21 @@ image = pipe.generate("a serene lake surrounded by mountains")
|
|
| 78 |
image.save("output.png")
|
| 79 |
```
|
| 80 |
|
| 81 |
-
`ShellDInference`
|
| 82 |
-
|
| 83 |
-
See [`inference.py`](./inference.py) for the complete standalone inference script.
|
| 84 |
-
|
| 85 |
-
---
|
| 86 |
-
|
| 87 |
-
## Model Files
|
| 88 |
|
| 89 |
-
|
| 90 |
-
|------|-------------|
|
| 91 |
-
| `config.json` | Model hyperparameters (ShellDConfig) |
|
| 92 |
-
| `model.safetensors` | All weights: VAE, DiT, and text encoder |
|
| 93 |
|
| 94 |
-
|
| 95 |
|
| 96 |
```python
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
state_dict = load_file(f"{model_path}/model.safetensors")
|
| 107 |
-
print(state_dict.keys()) # prefixed: vae.*, dit.*, text_encoder.*
|
| 108 |
```
|
| 109 |
|
| 110 |
---
|
|
@@ -119,6 +131,5 @@ print(state_dict.keys()) # prefixed: vae.*, dit.*, text_encoder.*
|
|
| 119 |
|
| 120 |
- 256×256 resolution only (no upscaling built in)
|
| 121 |
- Limited prompt understanding due to small DiT and frozen lightweight text encoder
|
| 122 |
-
-
|
| 123 |
-
|
| 124 |
---
|
|
|
|
| 27 |
|
| 28 |
### VAE (Autoencoder)
|
| 29 |
|
| 30 |
+
The VAE compresses 256×256 RGB images into a **16-channel latent** with spatial size 32×32 (downsampled by 8×). It uses residual blocks with GroupNorm and SiLU activations. During training, a KL penalty (β=0.1) keeps latents close to a standard normal distribution.
|
| 31 |
|
| 32 |
### DiT (Diffusion Transformer)
|
| 33 |
|
|
|
|
| 35 |
- **Self-attention** for spatial relationships
|
| 36 |
- **Cross-attention** conditioned on text embeddings
|
| 37 |
- **Adaptive timestep conditioning** via an MLP-projected sinusoidal embedding
|
| 38 |
+
- **Dropout** (0.1) in attention and MLP for regularization
|
| 39 |
|
| 40 |
### Diffusion Process
|
| 41 |
|
| 42 |
+
Standard DDPM (Denoising Diffusion Probabilistic Model) with 1000 timesteps and a linear beta schedule (β₁=1e‑4, βᵀ=0.02). The model is trained to predict the added noise ε. Classifier-free guidance (CFG) is used during training with a text-conditioning dropout probability of 15%.
|
| 43 |
|
| 44 |
---
|
| 45 |
|
| 46 |
## Training
|
| 47 |
|
| 48 |
+
### Dataset
|
| 49 |
+
|
| 50 |
+
**[jackyhate/text-to-image-2M](https://huggingface.co/datasets/jackyhate/text-to-image-2M)** — \~2M high-quality text-image pairs in webdataset format. Loaded via the `datasets` library with **streaming** to avoid materializing the full 2TB+ dataset into memory. A rotating **2000-image in-memory buffer** (\~400 MB RAM) is refreshed each epoch from a fresh random stream to provide shuffle diversity without disk I/O bottlenecks.
|
| 51 |
+
|
| 52 |
+
### Data Augmentation
|
| 53 |
+
|
| 54 |
+
Random horizontal flip (p=0.5), color jitter (brightness/contrast/saturation ±0.2, hue ±0.05), and random affine transforms (rotation ±10°, translation ±5%, scale 0.9–1.1) via `torchvision`.
|
| 55 |
+
|
| 56 |
+
### Hyperparameters
|
| 57 |
+
|
| 58 |
+
| Parameter | Value |
|
| 59 |
+
|-----------|-------|
|
| 60 |
+
| **Image size** | 256×256 |
|
| 61 |
+
| **Batch size** | 8 |
|
| 62 |
+
| **Optimizer** | AdamW (β₁=0.9, β₂=0.999, lr=1e‑4, weight decay=0.01) |
|
| 63 |
+
| **LR schedule** | Linear warmup (500 steps) + Cosine annealing |
|
| 64 |
+
| **Gradient clipping** | 1.0 (norm) |
|
| 65 |
+
| **Mixed precision** | FP16 via `torch.cuda.amp.GradScaler` |
|
| 66 |
+
| **Dropout** | 0.1 (DiT attention + MLP) |
|
| 67 |
+
| **EMA** | Exponential moving average (decay=0.999) applied at every step; EMA weights used for validation and final checkpoint |
|
| 68 |
+
| **Early stopping** | Patience of 8 epochs on validation loss (10% held-out split) |
|
| 69 |
+
| **KL weight** | 0.1 (β-VAE style) |
|
| 70 |
+
|
| 71 |
+
### VAE Pretraining
|
| 72 |
+
|
| 73 |
+
Before diffusion training, the VAE is pretrained for **10 epochs** on reconstruction + KL loss with a higher learning rate (lr=1e‑3) to establish a meaningful latent space. During this phase the DiT and text encoder are frozen.
|
| 74 |
|
| 75 |
### Training Phases
|
| 76 |
|
| 77 |
+
1. **VAE Pretraining** (10 epochs) — Train encoder + decoder on image reconstruction to establish a meaningful latent space. DiT and text encoder are frozen.
|
| 78 |
+
2. **DiT Diffusion Training** (up to 30 epochs, early-stopped) — Freeze VAE, train DiT to denoise latents conditioned on text embeddings. CFG dropout randomly replaces text embeddings with a learned null embedding to enable classifier-free guidance at inference time.
|
| 79 |
|
| 80 |
---
|
| 81 |
|
|
|
|
| 87 |
pip install torch safetensors sentence-transformers pillow numpy huggingface-hub
|
| 88 |
```
|
| 89 |
|
| 90 |
+
For training, also install:
|
| 91 |
+
```bash
|
| 92 |
+
pip install torchvision datasets
|
| 93 |
+
```
|
| 94 |
+
|
| 95 |
### Inference (standalone — loads from Hugging Face)
|
| 96 |
|
| 97 |
```python
|
|
|
|
| 102 |
image.save("output.png")
|
| 103 |
```
|
| 104 |
|
| 105 |
+
`ShellDInference` automatically downloads weights from Hugging Face via `huggingface_hub` on first use and caches them locally.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 106 |
|
| 107 |
+
### Streaming Generation
|
|
|
|
|
|
|
|
|
|
| 108 |
|
| 109 |
+
View the diffusion process unfold step-by-step:
|
| 110 |
|
| 111 |
```python
|
| 112 |
+
for img, step_info in pipe.generate_stream(
|
| 113 |
+
prompt="a futuristic city at night",
|
| 114 |
+
num_steps=250,
|
| 115 |
+
cfg_scale=3.0,
|
| 116 |
+
display_every=25, # emit an image every 25 steps
|
| 117 |
+
):
|
| 118 |
+
print(f"Step {step_info['step']}/{step_info['total']}")
|
| 119 |
+
img.save(f"progress_{step_info['step']:04d}.png")
|
|
|
|
|
|
|
|
|
|
| 120 |
```
|
| 121 |
|
| 122 |
---
|
|
|
|
| 131 |
|
| 132 |
- 256×256 resolution only (no upscaling built in)
|
| 133 |
- Limited prompt understanding due to small DiT and frozen lightweight text encoder
|
| 134 |
+
- Quality depends on training data distribution — may not match large-scale models like SDXL or Flux
|
|
|
|
| 135 |
---
|