Pixel Sprite Diffusion
Pixel Sprite Diffusion makes 16x16 pixel-art sprites from a text prompt. This repository has three models: small, large, and xl. Each model is an ONNX file. It runs in a browser, in Python, or in an ONNX Runtime app. The runtime code does not need PyTorch.
Each row shows ten sprites from one prompt, made by the xl model with 32 steps. The prompts are: red dragon, blue potion, knight with sword, castle, treasure chest, green slime.
- Try it in a browser: https://huggingface.co/spaces/Hypersniper/pixel-sprite-diffusion
- Code and training scripts: https://github.com/hypersniper05/pixel-sprite-diffusion
Models
| Model | Parameters | File | Size | Total download | Held-out loss |
|---|---|---|---|---|---|
| small | 13.4M | onnx/unet_small.onnx |
25.8 MB | 88 MB | 0.0709 |
| large | 90.5M | onnx/unet_large.onnx |
173 MB | 235 MB | 0.0674 |
| xl | 201.5M | onnx/unet_xl.onnx |
385 MB | 447 MB | 0.0663 |
The total download includes the text encoder and the tokenizer data. All three models share them. The held-out loss is the noise-prediction error on 8,192 sprites that the training did not use. A lower value is better.
Files
| File | Contents |
|---|---|
onnx/unet_small.onnx, onnx/unet_large.onnx, onnx/unet_xl.onnx |
The U-Nets that remove the noise |
onnx/clip_text.onnx |
The CLIP ViT-B/32 text encoder, 61 MB |
onnx/clip_merges.txt |
The tokenizer data, 0.5 MB |
onnx/config.json |
The noise schedule, the default settings, the palette, and the file list |
pixelart_onnx.py |
The Python runtime: tokenizer, sampler, command line |
pixelart.js |
The JavaScript runtime for ONNX Runtime Web |
pytorch/small.pt, pytorch/large.pt, pytorch/xl.pt |
The PyTorch weights (EMA), for more training |
Run it in Python
You need Python 3.10 or later.
Install the packages.
pip install onnxruntime numpy pillow huggingface_hubDownload the model files. This command downloads the small model only.
hf download Hypersniper/pixel-sprite-diffusion pixelart_onnx.py onnx/config.json onnx/clip_text.onnx onnx/clip_merges.txt onnx/unet_small.onnx --local-dir pixel-sprite-diffusionMake sprites.
cd pixel-sprite-diffusion python pixelart_onnx.py "red dragon" --model small --n 8 --out dragon.png
The command writes one PNG file with eight sprites.
To use a different model, change onnx/unet_small.onnx in step 2 and --model small in step 3.
Use --seed 1 to get the same sprites each time.
Use --steps and --cfg to change the number of steps and the guidance strength.
You can also call the code from Python. Run it in the pixel-sprite-diffusion folder:
from pixelart_onnx import PixelArt, sheet
pa = PixelArt("onnx", model="small")
sprites = pa.sample("blue potion", n=8, steps=32, cfg=1.5, seed=1) # uint8 array [8, 16, 16, 3]
sheet(sprites, scale=16).save("potion.png")
Run it in JavaScript
pixelart.js contains the tokenizer and the sampler. It has no dependencies.
Download pixelart.js and put it next to your page.
import * as ort from "https://cdn.jsdelivr.net/npm/onnxruntime-web@1.31.1/dist/ort.webgpu.min.mjs";
import { ClipTokenizer, PixelArt } from "./pixelart.js";
const base = "https://huggingface.co/Hypersniper/pixel-sprite-diffusion/resolve/main/onnx/";
const cfg = await (await fetch(base + "config.json")).json();
const tokenizer = new ClipTokenizer(await (await fetch(base + cfg.merges)).text());
const clip = await ort.InferenceSession.create(base + cfg.clip, { executionProviders: ["wasm"] });
const unet = await ort.InferenceSession.create(base + cfg.models.small.file, { executionProviders: ["webgpu", "wasm"] });
const engine = new PixelArt(ort, cfg, tokenizer, clip, unet);
const rgb = await engine.sample("red dragon", { n: 4, steps: 32, cfg: 1.5, seed: 1 }); // Uint8Array, 4 x 16 x 16 x 3
ONNX inputs and outputs
unet_<size>.onnx
| Name | Type | Shape | Contents |
|---|---|---|---|
x |
float32 | [B, 3, 16, 16] | The noisy image, values from -1 to 1 |
t |
int64 | [B] | The noise step, 0 to 999 |
y |
float32 | [B, 512] | pooled from the text encoder |
ctx |
float32 | [B, 20, 512] | hidden from the text encoder |
mask |
bool | [B, 20] | True for each real token |
eps (output) |
float32 | [B, 3, 16, 16] | The predicted noise |
clip_text.onnx
| Name | Type | Shape | Contents |
|---|---|---|---|
input_ids |
int64 | [B, 20] | The CLIP token numbers, padded with 49407 |
attention_mask |
int64 | [B, 20] | 1 for each real token |
pooled (output) |
float32 | [B, 512] | One vector for the prompt |
hidden (output) |
float32 | [B, 20, 512] | One vector for each token |
For the "no prompt" batch of classifier-free guidance, set y and ctx to zero.
Set mask to True for token 0 only.
Prompts
Short prompts give the best results. Name a subject. You can add a color or one detail.
Examples: castle, blue potion, knight with sword, small house with red roof.
The models read English only. The models read a maximum of 18 tokens, which is about 12 words. The models ignore the rest of a longer prompt.
How it works
- The tokenizer splits the prompt into CLIP tokens.
- The CLIP ViT-B/32 text encoder reads the tokens.
- The sampler starts with random noise.
- A U-Net removes the noise in steps. Cross-attention to the prompt tokens guides each step.
- The sampler moves each pixel to the nearest color of the training palette.
The sampler is DDIM with classifier-free guidance. The default is 32 steps with guidance 1.5. The noise schedule is a cosine schedule with 1000 steps and a log-SNR shift of 2.77.
Training
The training data is the Pixel Art Atlas. The data has 1.4 million captioned 16x16 sprites with a CC0 license. The training used 1,397,114 sprites. It kept 8,192 sprites apart to measure the held-out loss.
| Setting | small | large | xl |
|---|---|---|---|
| Base width | 96 | 128 | 192 |
| Width multipliers | 1, 2, 2 | 1, 2, 4 | 1, 2, 4 |
| Residual blocks for each level | 2 | 4 | 4 |
| Epochs | 25 | 25 | 20 |
| Learning rate | 4e-4 | 2e-4 | 1.5e-4 |
All three models use batch size 128, AdamW, an EMA of 0.999, and 10% prompt dropout.
For 15% of the samples, the caption is the subject only, for example castle.
All three models have self-attention at 16x16, 8x8, and 4x4.
The text encoder is the original CLIP ViT-B/32. The training did not change it.
Size of the ONNX files
The export keeps the files small with two methods.
- U-Net: the file stores the weights as 16-bit floats. ONNX Runtime changes them to 32-bit floats when it loads the file.
- Text encoder: the file stores each large weight matrix as 8-bit numbers with one scale for each output. This decreases the size from about 240 MB to 61 MB.
The calculations use 32-bit floats in both cases. The files run on each ONNX Runtime backend. The smaller storage changes the results a small amount. With the same start noise, 76% to 78% of the pixels are identical to full-precision PyTorch. The mean color difference is about 1 of 255.
Limits
- The sprites are 16x16 pixels. The models cannot make other sizes.
- The captions of the training data are approximate. A color in the prompt does not always match the sprite.
- A computer program made the training sprites. The models copy that style.
- The models do not write text and do not draw exact numbers of objects.
- The background is a flat color. It is not transparent.
Credits
- Training data: Pixel Art Atlas by Angais, CC0.
- Text encoder: CLIP ViT-B/32 by OpenAI, MIT license.
- Runtime: ONNX Runtime, MIT license.
License
The code and the model weights have the MIT license.