Level 0: Foundation
★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.Writing an image as a sentence
A language model writes text one token at a time. To make it write images, you first need an alphabet for pictures. Image GPT used one token per pixel colour; modern systems first learn a codebook of small image pieces (like a set of jigsaw tiles), so a picture becomes a short sentence of tile numbers. A transformer then learns to write those sentences, and a decoder turns the tiles back into pixels. The demo shows how many tokens an image needs with and without such compression, and what that does to the cost of attention.
Tokenised imagery lets one transformer handle text, maps and sensor images together, the basis of multimodal assistants that can read and produce satellite or inspection images.
- 1. Image GPT predicts…
- 2. A VQ codebook turns an image into…
- 3. Fewer tokens per image make attention…
Level 1: Beginner
★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.By hand
- Image GPT (Chen et al., 2020): GPT-2 architecture on pixel sequences at 32×32 to 64×64; colours clustered by k-means into a 9-bit (512-entry) palette, one token per pixel; trained by next-pixel prediction (and BERT-style masking for comparison). Linear probes on its features reached 96.3% on CIFAR-10, showing generative pretraining learns useful representations, but compute limited resolution.
- Two-stage recipe: (1) a tokenizer, an autoencoder with a vector-quantised bottleneck (VQ-VAE, van den Oord et al., 2017; VQGAN, Esser et al., 2021, with perceptual and adversarial losses), maps an image to a grid of code indices (downsampling f = 8 or 16, codebooks of 1024 to 16384); (2) an autoregressive transformer models the code sequence; samples are decoded back to pixels.
- Systems: VQ-VAE-2 (2019, hierarchical codes with PixelCNN priors); DALL·E (2021, 32×32 dVAE tokens after 256 text tokens, 12B transformer); Parti (2022, ViT-VQGAN, up to 20B); LlamaGen (2024, plain Llama on VQGAN codes, class- and text-conditional); VAR (2024, predicts coarse-to-fine scales instead of raster order); Chameleon and Emu3 (2024, one model for text and image tokens).
- Sampling: temperature, top-k / top-p, and classifier-free guidance (mixing conditional and unconditional logits) control quality vs diversity.
- Alternatives: masked token models (MaskGIT, 2022) generate many tokens in parallel; continuous-token autoregression with a diffusion head (MAR, 2024) avoids quantisation; diffusion models remain the main competitor.
- 1. Image GPT reduced colours with…
- 2. VQGAN adds to VQ-VAE…
- 3. VAR predicts tokens…
Level 2: Intermediate
★★★☆☆ Intermediate⏱ 25 minUndergraduate students and practising engineers.Patches, codebooks and code length
Exercise 1.
- Cut images into p×p patches and reassemble them.
- Vector-quantise patches against a codebook.
- Count the code bits of a token grid under a uniform prior.
Practice.
- Use a pretrained tokenizer (VQGAN, the LlamaGen or Open-MAGVIT tokenizers) and train only the prior, unless the domain (for example SAR or multispectral) needs its own.
- Watch codebook usage: dead codes waste capacity; use k-means initialisation, codebook reset, low-dimensional or ℓ2-normalised codes, or lookup-free / finite scalar quantisation (LFQ, FSQ).
- Report reconstruction quality (rFID, PSNR) of the tokenizer separately from generation quality (FID) of the prior.
- Raster order is standard; add 2-D positional embeddings.
- Use classifier-free guidance and top-k sampling for conditional generation.
import torch
# Stage 1: tokenise with a pretrained VQ model (e.g. from the LlamaGen or taming-transformers repos)
codes = vq_model.encode_to_indices(images) # (B, 16, 16) integers in [0, 16384)
seq = codes.flatten(1) # raster order, 256 tokens
# Stage 2: next-token prediction with any decoder-only transformer
logits = gpt(torch.cat([class_token[:, None], seq[:, :-1]], 1))
loss = torch.nn.functional.cross_entropy(logits.transpose(1, 2), seq)- 1. Dead codes are fixed by…
- 2. Report tokenizer quality with…
- 3. FSQ and LFQ are alternatives to…
Level 3: Advanced
★★★★☆ Advanced⏱ 25 minGraduate level, with full derivations and proofs.Theory
Two-stage likelihood. With a deterministic encoder z = E(x) and decoder x̂ = D(z), the prior gives the exact probability of the code sequence, p(z) = Π_t p(z_t | z_before t), so −log₂ p(z) is the code length of the lossy representation; image quality is bounded by the tokenizer’s distortion ‖x − D(E(x))‖. This is rate–distortion: fewer or coarser tokens lower the rate (bits) and the cost of the prior, but raise the distortion.
Cost. Attention costs O(n²) for n tokens: 65536 pixels of a 256×256 image vs 256 VQ tokens with f = 16 is a factor of 65536 in attention scores, which is why Image GPT stopped at 64×64.
Vector quantisation. VQ-VAE trains with a straight-through gradient through argmin, a codebook loss ‖sg[E(x)] − e‖² and a commitment loss β‖E(x) − sg[e]‖²; codebook vectors can also be updated as exponential moving averages of their assigned encodings (k-means online).
| model | year | tokens for a 256² image | prior |
|---|---|---|---|
| Image GPT | 2020 | 65536 (palette pixels; used only up to 64²) | GPT-2 |
| VQ-VAE-2 | 2019 | 32² + 64² hierarchical | PixelCNN |
| DALL·E | 2021 | 32² = 1024 | 12B transformer |
| VQGAN + transformer | 2021 | 16² = 256 | GPT-2-like |
| LlamaGen | 2024 | 16² = 256 | Llama |
- 1. The prior gives the exact likelihood of…
- 2. VQ-VAE's gradient through argmin uses…
- 3. Coarser codes lower the rate but raise…
Level 4: Expert
★★★★★ Expert⏱ 30 minResearchers: original papers and open problems.How do pixel tokens and VQ tokens of different sizes trade sequence length, model bits and reconstruction quality?
History.
- M. Chen et al., “Generative pretraining from pixels” (Image GPT, ICML 2020).
- A. van den Oord, O. Vinyals and K. Kavukcuoglu, “Neural discrete representation learning” (VQ-VAE, 2017); A. Razavi et al., VQ-VAE-2 (2019).
- P. Esser, R. Rombach and B. Ommer, “Taming transformers for high-resolution image synthesis” (VQGAN, CVPR 2021); A. Ramesh et al., “Zero-shot text-to-image generation” (DALL·E, 2021).
- P. Sun et al., “Autoregressive model beats diffusion: Llama for scalable image generation” (LlamaGen, 2024); K. Tian et al., “Visual autoregressive modeling” (VAR, NeurIPS 2024).
Exercise 2. 2000 training and 500 test grey-level 8×8 digits (blurred seven-segment glyphs with noise of std 0.03).
- Tokenisers: an 8-level per-pixel palette by k-means (Image GPT style, 64 tokens); k-means codebooks of 2×2 patches with 16 or 64 codes (16 tokens); 4×4 patches with 64 codes (4 tokens). k-means stands in for a learned VQ encoder.
- Prior: the same one-block causal transformer (d = 32, single head, ReLU MLP, start token, learned positions) for every tokeniser; 200 Adam steps, batch 16; backpropagation by hand, checked by finite differences.
- Scores: reconstruction MSE of the decoded codes; test code length in bits per image under the transformer and under independent token frequencies.
| tokeniser | tokens | attention pairs | recon MSE | independent tokens (bits) | transformer prior (bits) |
|---|---|---|---|---|---|
| pixels, 8-level palette | 64 | 2080 | 0.0006 | 174.4 | 92.0 |
| VQ 2×2, K 16 | 16 | 136 | 0.0035 | 55.4 | 22.4 |
| VQ 2×2, K 64 | 16 | 136 | 0.0014 | 85.4 | 40.4 |
| VQ 4×4, K 64 | 4 | 10 | 0.0060 | 21.2 | 10.0 |
- The transformer learned the structure: every prior needed under 60% of the bits of independent tokens (from 40% for VQ 2×2 K 16 to 53% for pixels).
- Rate–distortion: each step to fewer or coarser tokens cut the bits to model (92.0 → 22.4 → 10.0) and the attention cost (2080 → 136 → 10 pairs), and raised the reconstruction error (0.0006 → 0.0035 → 0.0060).
- Codebook size at fixed length: 64 instead of 16 codes for 2×2 patches cut the error by 60% at the price of 18 more bits.
- Seeds 1 and 2: pixels 77.5 and 80.9 bits; VQ 2×2 K 16 21.4 and 21.7; VQ 4×4 K 64 10.5 and 10.2; reconstruction errors within 0.0005 of seed 0.
- Caveat: tiny images; k-means instead of a learned encoder with a perceptual decoder; the pixel prior was the least trained relative to its task (64 positions with the same 200 steps). Bits of lossy codes are not comparable with lossless bits/dim.
Image GPT and VQ transformers in AI, robotics and aerospace
- Earth observation: tokenizers trained on multispectral or SAR imagery let transformers generate, inpaint and compress scenes and share one model with text.
- World models for robots: video tokenizers plus autoregressive transformers predict future camera frames for planning.
- Compression: learned codebooks with an entropy-coded AR prior form neural image codecs for low-bandwidth downlinks.
References
- M. Chen et al. (2020). “Generative pretraining from pixels.” ICML.
- A. van den Oord, O. Vinyals and K. Kavukcuoglu (2017). “Neural discrete representation learning.” NeurIPS.
- P. Esser, R. Rombach and B. Ommer (2021). “Taming transformers for high-resolution image synthesis.” CVPR.
- A. Ramesh et al. (2021). “Zero-shot text-to-image generation.” ICML.
- P. Sun et al. (2024). “Autoregressive model beats diffusion: Llama for scalable image generation.” arXiv:2406.06525.
- K. Tian, Y. Jiang, Z. Yuan, B. Peng and L. Wang (2024). “Visual autoregressive modeling: scalable image generation via next-scale prediction.” NeurIPS.