Skip to content
AI360Xpert
Beta

BEiT Pretrains by Masked Visual Tokens

BEiT masks image patches and predicts discrete visual tokens, so transformers learn semantics before labels appear.

Masked patches predict discrete tokens from a codebook before fine-tuning
Masked patches predict discrete tokens from a codebook before fine-tuning

Why Does This Exist?

Labeling millions of photos costs months. Masked pretraining uses raw pixels instead, but pixels regress to blurry averages.

BEiT discretizes first. It feeds pretrained models, lifts image classification and semantic segmentation, and complements dinov2 distillation.

Think of It Like This

Fill-in blanks with a word bank

You blank out 40 percent of a paragraph and pick each missing word from a fixed bank of 8192 words. Guessing exact letters is vague, but picking bank words forces meaning.

Patches are words, the dVAE bank is vocab, and prediction is the quiz. Where it stops: a weak bank teaches weak words, just as a weak tokenizer caps BEiT.

How It Actually Works

Tokenize each 16×1616 \times 16 patch with a discrete VAE into z∈[0,8191]z \in [0, 8191]. Mask 4040 percent of 196196 tokens (about 7878). Train ViT to predict zz with cross entropy on masked spots only, then fine-tune with labels.

Worked example

196×0.40=78.4196 \times 0.40 = 78.4, so 7878 masked. Suppose model gives true token logit 3.03.0 versus two distractors 1.0,0.01.0, 0.0. Softmax is e3=20.09e^3 = 20.09, e1=2.72e^1 = 2.72, e0=1e^0 = 1, sum 23.8123.81, prob 20.09/23.81=0.84420.09/23.81 = 0.844. Loss −log⁡0.844=0.170-\log 0.844 = 0.170. Averaged over 7878 spots, semantics emerge without labels.

Watch Out For

Reusing a mismatched tokenizer for new domains

A natural-photo tokenizer on medical scans maps distinct lesions to one code. Symptom: fine-tune stalls below supervised baseline. Fix: retrain or adapt the tokenizer on target imagery before BEiT pretraining.

The Quick Version

  • BEiT predicts discrete visual tokens for masked patches, not raw pixels.
  • Masks cover 40 percent, about 78 of 196 tokens per image.
  • Fixture true-token prob 0.844 gives loss 0.170 per masked patch.
  • Tokenizer quality bounds the whole run.
  • Fine-tunes then transfer to classification and segmentation.