| --- |
| license: apache-2.0 |
| pipeline_tag: text-to-image |
| --- |
| # ShellD (Shell Diffusion) |
|
|
| **Small DiT-based Text-to-Image Latent Diffusion Model** |
|
|
| 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. |
|
|
| --- |
|
|
| ## Model Architecture |
|
|
| ``` |
| Text Prompt β [MiniLM-L6-v2 (frozen)] β Text Embedding (384-d) |
| β |
| Random Noise β [VAE Encoder] β Latent (16ch) β [DiT (12 blocks)] β Denoised Latent β [VAE Decoder] β 256Γ256 Image |
| ``` |
|
|
| | Component | Details | Params | |
| |-----------|---------|--------| |
| | **Text Encoder** | `sentence-transformers/all-MiniLM-L6-v2` (frozen) | 22.71M | |
| | **VAE** | Encoder + Decoder with residual blocks, 3 down/up stages, latent dim=16 | 23.43M | |
| | **DiT** | 12-layer Transformer with self-attention, cross-attention (text), and adaptive timestep conditioning. Patch size=4, hidden dim=256, 8 heads | 20.80M | |
| | **Total** | | **66.95M** (trainable: **44.23M**) | |
|
|
| ### VAE (Autoencoder) |
|
|
| 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. |
|
|
| ### DiT (Diffusion Transformer) |
|
|
| The DiT operates on patched latents (patch size 4 β 8Γ8 = 64 patches). Each block includes: |
| - **Self-attention** for spatial relationships |
| - **Cross-attention** conditioned on text embeddings |
| - **Adaptive timestep conditioning** via an MLP-projected sinusoidal embedding |
| - **Dropout** (0.1) in attention and MLP for regularization |
|
|
| ### Diffusion Process |
|
|
| 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%. |
|
|
| --- |
|
|
| ## Training |
|
|
| ### Dataset |
|
|
| **[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. |
|
|
| ### Data Augmentation |
|
|
| 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`. |
|
|
| ### Hyperparameters |
|
|
| | Parameter | Value | |
| |-----------|-------| |
| | **Image size** | 256Γ256 | |
| | **Batch size** | 8 | |
| | **Optimizer** | AdamW (Ξ²β=0.9, Ξ²β=0.999, lr=1eβ4, weight decay=0.01) | |
| | **LR schedule** | Linear warmup (500 steps) + Cosine annealing | |
| | **Gradient clipping** | 1.0 (norm) | |
| | **Mixed precision** | FP16 via `torch.cuda.amp.GradScaler` | |
| | **Dropout** | 0.1 (DiT attention + MLP) | |
| | **EMA** | Exponential moving average (decay=0.999) applied at every step; EMA weights used for validation and final checkpoint | |
| | **Early stopping** | Patience of 8 epochs on validation loss (10% held-out split) | |
| | **KL weight** | 0.1 (Ξ²-VAE style) | |
|
|
| ### VAE Pretraining |
|
|
| 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. |
|
|
| ### Training Phases |
|
|
| 1. **VAE Pretraining** (10 epochs) β Train encoder + decoder on image reconstruction to establish a meaningful latent space. DiT and text encoder are frozen. |
| 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. |
|
|
| --- |
|
|
| ## Usage |
|
|
| ### Requirements |
|
|
| ```bash |
| pip install torch safetensors sentence-transformers pillow numpy huggingface-hub |
| ``` |
|
|
| For training, also install: |
| ```bash |
| pip install torchvision datasets |
| ``` |
|
|
| ### Inference (standalone β loads from Hugging Face) |
|
|
| ```python |
| from inference import ShellDInference |
| |
| pipe = ShellDInference("FlameF0X/ShellD") |
| image = pipe.generate("a serene lake surrounded by mountains") |
| image.save("output.png") |
| ``` |
|
|
| `ShellDInference` automatically downloads weights from Hugging Face via `huggingface_hub` on first use and caches them locally. |
|
|
| ### Streaming Generation |
|
|
| View the diffusion process unfold step-by-step: |
|
|
| ```python |
| for img, step_info in pipe.generate_stream( |
| prompt="a futuristic city at night", |
| num_steps=250, |
| cfg_scale=3.0, |
| display_every=25, # emit an image every 25 steps |
| ): |
| print(f"Step {step_info['step']}/{step_info['total']}") |
| img.save(f"progress_{step_info['step']:04d}.png") |
| ``` |
|
|
| --- |
|
|
| ## Intended Use |
|
|
| - Educational exploration of diffusion transformers |
| - Lightweight text-to-image generation on consumer hardware |
| - Starting point for fine-tuning on custom datasets |
|
|
| ## Limitations |
|
|
| - 256Γ256 resolution only (no upscaling built in) |
| - Limited prompt understanding due to small DiT and frozen lightweight text encoder |
| - Quality depends on training data distribution β may not match large-scale models like SDXL or Flux |
| --- |