FlameF0X commited on
Commit
4649ea9
Β·
verified Β·
1 Parent(s): 8321f11

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +121 -0
README.md CHANGED
@@ -1,3 +1,124 @@
1
  ---
2
  license: apache-2.0
 
3
  ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  ---
2
  license: apache-2.0
3
+ pipeline_tag: text-to-image
4
  ---
5
+ # ShellD (Shell Diffusion)
6
+
7
+ **Small DiT-based Text-to-Image Latent Diffusion Model**
8
+
9
+ ShellD is a lightweight text-to-image model that generates **256Γ—256** images from natural language prompts. It uses a Diffusion Transformer (DiT) backbone operating in a compact VAE latent space, making it feasible to train and run on consumer GPUs.
10
+
11
+ ---
12
+
13
+ ## Model Architecture
14
+
15
+ ```
16
+ Text Prompt β†’ [MiniLM-L6-v2 (frozen)] β†’ Text Embedding (384-d)
17
+ ↓
18
+ Random Noise β†’ [VAE Encoder] β†’ Latent (16ch) β†’ [DiT (12 blocks)] β†’ Denoised Latent β†’ [VAE Decoder] β†’ 256Γ—256 Image
19
+ ```
20
+
21
+ | Component | Details | Params |
22
+ |-----------|---------|--------|
23
+ | **Text Encoder** | `sentence-transformers/all-MiniLM-L6-v2` (frozen) | 22.71M |
24
+ | **VAE** | Encoder + Decoder with residual blocks, 3 down/up stages, latent dim=16 | 23.43M |
25
+ | **DiT** | 12-layer Transformer with self-attention, cross-attention (text), and adaptive timestep conditioning. Patch size=4, hidden dim=256, 8 heads | 20.80M |
26
+ | **Total** | | **66.95M** (trainable: **44.23M**) |
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 small KL penalty keeps latents close to a standard normal distribution.
31
+
32
+ ### DiT (Diffusion Transformer)
33
+
34
+ The DiT operates on patched latents (patch size 4 β†’ 8Γ—8 = 64 patches). Each block includes:
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, cosine-like linear beta schedule (β₁=1e‑4, Ξ²α΅€=0.02). The model is trained to predict the added noise Ξ΅.
42
+
43
+ ---
44
+
45
+ ## Training
46
+
47
+ - **Dataset**: Midjourney v5 202304 Clean (~20K image-text pairs, with procedural fallback)
48
+ - **Image size**: 256Γ—256
49
+ - **Batch size**: 8
50
+ - **Optimizer**: Adam (β₁=0.9, Ξ²β‚‚=0.999, lr=1e‑4)
51
+ - **LR schedule**: Linear warmup (500 steps) + Cosine annealing
52
+ - **Gradient clipping**: 1.0
53
+ - **Mixed precision**: FP16 via GradScaler
54
+ - **VAE pretraining**: 1 epoch of reconstruction + KL loss (lr=1e‑3) before diffusion training
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
+
63
+ ## Usage
64
+
65
+ ### Requirements
66
+
67
+ ```bash
68
+ pip install torch safetensors sentence-transformers pillow numpy huggingface-hub
69
+ ```
70
+
71
+ ### Inference (standalone β€” loads from Hugging Face)
72
+
73
+ ```python
74
+ from inference import ShellDInference
75
+
76
+ pipe = ShellDInference("FlameF0X/ShellD")
77
+ image = pipe.generate("a serene lake surrounded by mountains")
78
+ image.save("output.png")
79
+ ```
80
+
81
+ `ShellDInference` will automatically download the weights from Hugging Face using `huggingface_hub` on first use, then cache them locally.
82
+
83
+ See [`inference.py`](./inference.py) for the complete standalone inference script.
84
+
85
+ ---
86
+
87
+ ## Model Files
88
+
89
+ | File | Description |
90
+ |------|-------------|
91
+ | `config.json` | Model hyperparameters (ShellDConfig) |
92
+ | `model.safetensors` | All weights: VAE, DiT, and text encoder |
93
+
94
+ ### Loading the weights manually
95
+
96
+ ```python
97
+ from huggingface_hub import snapshot_download
98
+ from safetensors.torch import load_file
99
+ import json
100
+
101
+ model_path = snapshot_download("FlameF0X/ShellD")
102
+
103
+ with open(f"{model_path}/config.json") as f:
104
+ config = json.load(f)
105
+
106
+ state_dict = load_file(f"{model_path}/model.safetensors")
107
+ print(state_dict.keys()) # prefixed: vae.*, dit.*, text_encoder.*
108
+ ```
109
+
110
+ ---
111
+
112
+ ## Intended Use
113
+
114
+ - Educational exploration of diffusion transformers
115
+ - Lightweight text-to-image generation on consumer hardware
116
+ - Starting point for fine-tuning on custom datasets
117
+
118
+ ## Limitations
119
+
120
+ - 256Γ—256 resolution only (no upscaling built in)
121
+ - Limited prompt understanding due to small DiT and frozen lightweight text encoder
122
+ - Procedural training data substitutes real images β€” quality depends on the real/fine-tuned checkpoints
123
+
124
+ ---