File size: 7,653 Bytes
967b8e3
 
d37df3e
 
 
 
 
 
 
 
 
 
 
 
 
 
967b8e3
d37df3e
 
 
 
 
 
 
 
 
 
 
 
 
 
103f3eb
 
d37df3e
7ceded6
d37df3e
 
 
 
 
7ceded6
 
d37df3e
 
 
36f8f86
d37df3e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16bb1ea
 
d37df3e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
103f3eb
 
 
 
 
d37df3e
 
 
9eb9edd
d37df3e
 
 
103f3eb
 
9eb9edd
 
 
d37df3e
 
 
 
 
 
9eb9edd
d37df3e
 
 
 
 
 
7ceded6
d37df3e
 
 
 
 
103f3eb
d37df3e
 
 
 
 
 
 
 
 
 
 
9eb9edd
d37df3e
9eb9edd
 
 
d37df3e
 
 
9eb9edd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d37df3e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7ceded6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
---
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.