--- license: cc-by-nc-4.0 library_name: esp32-ai pipeline_tag: unconditional-image-generation tags: - esp32 - esp32-s3 - microcontroller - embedded - edge - per-layer-embeddings - int4 - pixel-art - sprites datasets: - Angais/pixel-art-atlas-1.4m --- # Model Card for ESP32 Sprite Maker A 717,000-parameter transformer that draws new 16 x 16 pixel-art game characters, one pixel at a time, on an ESP32-S3 microcontroller (M5Stack CoreS3), with nothing off the chip. ![Sprites drawn by the model](sprites.png) ## Model Details ### Model Description The model writes a sprite as a sequence of tokens: a character token (knight, wizard, witch, robot, slime, ghost, dragon, skeleton, zombie, goblin, pirate, ninja, alien, cat, frog, and monster, which the device's picker leaves out as too vague), then 256 pixels row by row, each either transparent or one of 32 colours. It runs on the ESP32-S3 with 4-bit weights, drawing each pixel on screen as it is written, in about 7 seconds a sprite. - **Developed by:** Justin McLean - **Model type:** decoder-only transformer with per-layer embeddings, autoregressive over pixel tokens - **License:** CC BY-NC 4.0 - **Inference code:** [justinmclean/esp32-ai](https://github.com/justinmclean/esp32-ai), a fork of [slvDev/esp32-ai](https://github.com/slvDev/esp32-ai) (MIT) ### Model Sources - **Repository:** https://github.com/justinmclean/esp32-sprite-maker ## Uses ### Direct Use Generating original 16 x 16 character sprites for non-commercial games, demos and learning, on an ESP32-S3 board or on a computer (the repository's `host/draw.c`). ### Out-of-Scope Use Commercial use: the training sprites were generated with Krea 2, whose commercial-use terms apply (see Licence). Characters other than the 16 it was trained on, or sizes and palettes other than 16 x 16 and 32 colours. ## Bias, Risks, and Limitations - Some characters are much more recognisable than others (see Evaluation): goblins and zombies are the weakest, and look-alike pairs (wizard and witch, pirate and ninja) get mixed up. - Sprites are sampled, so quality varies from one to the next. - The training sprites are themselves AI-generated, and their captions were not reviewed sprite by sprite. ### Recommendations Draw a few and keep the ones you like. Use temperature 0.8: higher gives more variety but more broken shapes. ## How to Get Started with the Model On a computer, with only a C compiler, from the GitHub repository: ```bash cc -O2 -o draw host/draw.c -lm ``` ```bash ./draw knight -n 8 -o knights.png ``` On an M5Stack CoreS3, from the repository's `firmware/` folder: ```bash pio run -e m5stack-cores3 -t upload_model ``` ```bash pio run -e m5stack-cores3 -t upload ``` ## Files - `model.bin`: the device model, 4-bit weights (0.38 MB). Flashed at 0x790000. - `tokenizer.json`: the token layout: the 32-colour palette (plus transparent), the 16 character tokens and the end-of-sprite token. - `sprite-maker.safetensors`: full-precision weights (2.9 MB), for fine-tuning. The repository's `training/weights.py` turns them into a checkpoint training can start from. ## Training Details ### Training Data [Pixel Art Atlas](https://huggingface.co/datasets/Angais/pixel-art-atlas-1.4m) (1.4 million 16 x 16 sprites, CC0). Each sprite is mapped to a fixed 32-colour palette fitted to the sprites' colours. Character sprites are those whose subject ends in one of the 16 character names (36,436 sprites), used with left-right mirrored copies. For the fine-tune, the fifth of them that a classifier (scoring each sprite out of fold) can't tell are their labelled character are left out, since the atlas's labels come from AI-written captions and some are wrong, and the rest are balanced so that each character's share is in proportion to the square root of its count (rare ones repeated). ### Training Procedure Three stages: 1. All 1.4 million sprites, with an "other" start token for non-characters and character sprites repeated 8 times (with mirrors): outlines, shading and solid shapes. 2. A fine-tune on the filtered, balanced character sprites only, so each character token means its character. 3. Quantisation-aware training on the same sprites: each step runs on the weights rounded to 4 bits as the device holds them, and updates the full-precision ones. Rounding cost the stage 2 model 0.035 in held-out loss and costs 0.009 after it. #### Training Hyperparameters - **Stage 1:** 12,000 steps, batch 32 x 257 tokens, AdamW, learning rate 1e-3 with 200 warmup steps and cosine decay, weight decay 0.1 - **Stage 2:** 1,500 steps from stage 1, learning rate 3e-4 with 50 warmup steps - **Stage 3:** 500 steps from stage 2, learning rate 1e-4 with 20 warmup steps - **Training regime:** fp32; each training window starts at the start of a sprite #### Speeds, Sizes, Times - 716,832 parameters (698,400 in the transformer core); 0.38 MB on the device at 4 bits - About 40 minutes of training on an Apple M3 MacBook (stage 1 about 35 minutes) - About 7 seconds a sprite on an ESP32-S3 at 240 MHz (36 pixels a second) ## Evaluation ### Testing Data, Factors & Metrics 2,000 sprites, 125 for each of the 16 character tokens, drawn at temperature 0.8 with the device model and arithmetic (`host/draw.c`), measured two ways: - **Memorisation:** the closest training sprite of the same character, in pixels different. - **Consistency:** how often a small classifier trained on the real character sprites recognises each generated sprite as its own character. On held-out real sprites the classifier scores 87%, the ceiling for these scores. ### Results **Memorisation:** none. No generated sprite is an exact or near copy (13 pixels or fewer different) of a training sprite; the closest typically differs by about 105 of 256 pixels. **Consistency** (the scores are the model fine-tuned on the unfiltered sprites, on the filtered ones, then after quantisation-aware training; the classifier is the same kind as the filter, so the filter's gain flatters it a little): | Character | Looks like itself | Most often mistaken for | |---|---|---| | skeleton | 65% → 92% → 89% | knight | | slime | 96% → 92% → 92% | monster | | cat | 82% → 88% → 93% | slime | | ghost | 57% → 81% → 88% | slime | | witch | 50% → 73% → 77% | wizard | | ninja | 51% → 66% → 85% | alien | | pirate | 50% → 66% → 70% | ninja | | wizard | 35% → 64% → 71% | witch | | knight | 68% → 62% → 75% | robot | | dragon | 62% → 61% → 67% | monster | | frog | 54% → 60% → 82% | monster | | robot | 58% → 56% → 81% | knight | | alien | 36% → 54% → 72% | slime | | zombie | 28% → 44% → 54% | alien | | goblin | 19% → 38% → 38% | knight | | monster (not on the picker) | 51% → 40% → not drawn | slime | | **average of the 15 on the picker** | **54% → 67% → 76%** | | ## Technical Specifications ### Model Architecture and Objective 5 transformer layers, width 128, 4 attention heads, SwiGLU feed-forward, RoPE positions, per-layer embeddings of width 32, output head tied to the token embedding. Trained with next-token cross-entropy over a 64-token vocabulary, context 257 tokens. ### Compute Infrastructure - **Training:** Apple M3 MacBook (PyTorch, MPS) - **Inference:** ESP32-S3 (M5Stack CoreS3) with 8 MB PSRAM, or any computer ## Licence CC BY-NC 4.0. Trained on the Pixel Art Atlas by Angais, released under CC0. Its sprites were generated with Krea 2: Krea claims no ownership of generated outputs, but the Krea 2 Community License's commercial-use terms apply to them, so this model is for non-commercial use.