Vibe Engines
YouTube

Vibe Engines is free, interactive AI engineering & system design — runs in your browser. Pick a learning path

~11 min readCVPR 2022

Masked autoencoders,
explained.

BERT taught language models by hiding words and asking them to fill in the blanks. For years, that trick stubbornly refused to work as well for images. Masked Autoencoders found out why — and the fix was almost rude: hide three quarters of the picture, feed the model only the sliver that's left, and make it paint back the rest. The task got hard enough to be worth solving, the encoder got cheap because it saw so little, and vision finally got its BERT. Here's the recipe and the arithmetic.

The delta

What changes against SimCLR: no pairs, no negatives, no augmentation design

SimCLR's signal comes from comparing images, which means it needs a batch full of negatives and an augmentation pipeline tuned hard enough to close every shortcut. MAE's signal comes from a single image compared with itself: hide most of the patches and reconstruct the missing pixels. No pairs, no negatives, no batch-size requirement, and the augmentation reduces to random cropping.

That is BERT's objective moved to vision, and the interesting part is the number that had to change. BERT masks around 15% of tokens; MAE masks 75% of patches. The reason is redundancy: a missing word is often unrecoverable without understanding the sentence, whereas a missing image patch can usually be interpolated from its neighbours. At a low mask ratio the task is solvable by smoothing and teaches nothing about content. Three quarters removed makes interpolation useless.

Stated in units the ladder can compare: the high mask ratio is also what makes it cheap, through an asymmetry that is the paper's real engineering contribution. The encoder runs on the visible patches only — the masked ones are not fed to it at all, so the expensive network processes roughly a quarter of the sequence — and a deliberately small decoder reassembles the full set and predicts pixels for the masked positions only. Where SimCLR bought representation quality with batch size, MAE buys it with a large multiple in pre-training throughput at the same quality.

01

BERT for images

Self-supervised learning — training on raw data with no labels — transformed language through masked prediction: hide some words, predict them, and the model learns deep structure for free. The obvious dream was to do the same for vision: mask parts of an image, reconstruct them, get a powerful encoder without labels. But early attempts underwhelmed. Something about images made naive masking a weak teacher.

MAE diagnosed the mismatch and fixed it with three coupled choices: mask a very high fraction of the image, use an asymmetric encoder-decoder where the heavy encoder sees only visible patches, and reconstruct raw pixels. Together they turned masked image modeling from a curiosity into a state-of-the-art pre-training method — the image analogue of BERT it had been chasing.

The one-sentence version

Hide most of the image, encode only the little that's visible, and reconstruct the rest — the difficulty forces real understanding, and the sparsity makes it cheap.

02

Why mask 75%

Here is the key insight. Language is dense with meaning — every word matters, so BERT masks only ~15% and the task is already hard. Images are the opposite: hugely redundant. A missing patch of sky or grass is trivially guessed from its neighbors. Mask just a little and the model "solves" reconstruction by interpolating nearby pixels, learning nothing about objects, scenes, or structure.

MAE's answer is to mask 75% of the patches — enough that local interpolation no longer works. To fill in three-quarters of a scene from the remaining quarter, the model has to grasp what is actually in the image and how it fits together. The high mask ratio isn't a tuning detail; it is the mechanism that makes the task demand understanding rather than pixel-copying. And it doubles as an efficiency gift, because the encoder then has far fewer patches to process.

03

The asymmetric design

The architecture is deliberately lopsided. The encoder — a full Vision Transformer, the part you keep — runs only on the visible patches. The masked patches aren't fed in at all; they are simply dropped. A small, lightweight decoder then takes the encoded visible patches plus placeholder "mask tokens" for the missing ones and reconstructs the image. After training, the decoder is thrown away.

Because transformer self-attention scales with the square of the token count, encoding only the visible fraction is a big saving:

At a 75% mask (r = 0.75) the encoder sees a quarter of the patches, so its attention costs (1/4)² = 1/16 of processing them all — for 196 patches, 49 visible instead of 196. That is what lets MAE pre-train large encoders economically: the expensive part runs on a sliver of the input, and the cheap decoder handles the rest. That 1/16 is the attention-matrix saving alone; end to end the paper measures a smaller 2.8 to 4.1 times speedup in wall-clock training. The runnable version below computes the visible count and this attention speedup for any mask ratio.

04

Reconstruct the pixels

The target is refreshingly plain: the decoder predicts the raw pixel values of each masked patch, and the loss is mean-squared error — but computed only on the masked patches. The visible patches, which the encoder already saw, are not scored; grading the model on copying what it was given would teach nothing.

The MAE forward pass
Patch & maskSplit the image into patches; randomly hide 75%.
Encode visibleThe ViT encoder runs on the 25% visible patches only.
Decode allA light decoder + mask tokens reconstruct the full image.
Loss on maskedMSE on the masked patches' pixels only — visible ones ignored.

Reconstructing pixels rather than higher-level features kept the objective simple and, somewhat surprisingly, worked very well. The reconstructions themselves can look blurry — the model isn't a great artist — but that was never the point. The blur is fine; the features the encoder learned on the way are what you take with you.

How it works

How MAE masks, encodes and reconstructs, step by step

MAE pre-trains a plain Vision Transformer with no labels, no pairs and no negatives. The ablations use ViT-L/16 on ImageNet-1K images at 224×224 resolution, so each image is 196 patches.

25% of patches visible ViT encoder visible tokens only 49 of 196 tokens add mask tokens unshuffle, add pos small decoder 8 blocks, 512 wide predicted pixels loss: MSE masked patches only target normalized pixels after training the decoder is dropped the encoder then sees all 196 patches
Only the visible quarter of the patches enters the heavy encoder; the lightweight decoder sees the full set with mask tokens, and the loss counts masked patches only.
  1. Patchify and embed. The image is cut into non-overlapping 16×16 patches. Each is linearly projected to a token and given a fixed sine-cosine position embedding. A dummy token is appended to serve as a class token later; the method works about as well with average pooling instead.
  2. Mask by shuffling. The token list is randomly shuffled and the last 75 percent is dropped. That is uniform sampling without replacement, so 49 tokens survive and 147 are removed. No sparse operations are needed, only a shuffle.
  3. Encode only what is visible. The ViT encoder runs on the 49 visible tokens and nothing else. Mask tokens are never fed to it, so the expensive network does about a quarter of the per-token work and the attention matrix shrinks sixteenfold.
  4. Rebuild the full sequence. A linear layer maps encoder outputs to the decoder's width. One shared, learned mask vector is appended for each removed patch, the list is unshuffled back into image order, and position embeddings are added to all 196 tokens. Without them the mask tokens would not know where they sit.
  5. Decode. A small Transformer, by default 8 blocks of width 512 and under a tenth of the encoder's compute per token, processes all 196 tokens. A final linear layer outputs 16·16·3 = 768 pixel values per patch.
  6. Score the masked patches only. The loss is mean squared error between predicted and true pixels over the 147 masked patches. In the best variant each target patch is first normalized by its own mean and standard deviation, which adds local contrast.
  7. Train cheaply. Augmentation is just a random resized crop and flip. Color jitter hurt, and even no augmentation worked decently, because a fresh random mask each time already varies the task. Ablations run 800 epochs; the headline models run 1,600, and linear-probe accuracy had not saturated by then.
  8. Drop the decoder. For recognition, the encoder processes all 196 patches of an uncorrupted image, either fine-tuned end to end or frozen under a linear classifier.

Why skipping the mask token matters

The tempting design feeds mask tokens through the encoder as BERT does. The paper tested it. Compute rose by 3.3 times, fine-tuning accuracy fell slightly and linear-probe accuracy fell by 14 points. The explanation is a gap between pre-training, where most of the encoder's input is mask tokens, and use, where none is. Leaving them out keeps the encoder's inputs real in both phases.

What a high mask ratio does

Fine-tuning accuracy was flat across masking ratios from 40 to 80 percent, but linear-probe accuracy climbed toward 75 percent. That is why 75 percent is the default, and well above the 15 percent BERT uses for words.

The math

The MAE objective and its cost

z = Enc( { xiW + pi : i ∈ V } ), |V| = (1 − r) N
ŷ = Dec( unshuffle( [ Pz ; m ; … ; m ] ) + p )
L = (1 / |M|) Σi ∈ M ‖ ŷi − (xi − μi) / σi ‖²
  • V, M — the visible and masked patch index sets; N = 196 patches and ratio r = 0.75, so |V| = 49 and |M| = 147.
  • W, pi — the shared patch projection and the fixed sine-cosine position embedding of patch i.
  • P, m — the linear map to the decoder's width and the one shared learned mask vector.
  • μi, σi — the mean and standard deviation of the pixels inside patch i; the target is each patch normalized by its own statistics.

Only masked patches enter the sum. At r = 0.75 the encoder's attention scores number 49² = 2,401 instead of 196² = 38,416, sixteen times fewer, while per-token layers shrink fourfold. At r = 0.25 the encoder would see 147 tokens, so scores would number 21,609, nine times as many as at 75 percent.

The evidence

What MAE measured: fine-tuning, linear probing and ablations

MAE is pre-trained on ImageNet-1K images without labels, then evaluated by fine-tuning the whole encoder or by training a linear classifier on frozen features. Fine-tuning top-1 accuracy, with the headline models pre-trained for 1,600 epochs:

MethodViT-BViT-LViT-HViT-H, 448 px
Supervised, from scratch82.382.683.1—
MoCo v383.284.1——
BEiT83.285.2——
MAE83.685.986.987.8

The 87.8 used only ImageNet-1K data. Transfer carried over: on COCO detection, ViT-L reached 53.3 box AP against 49.3 for supervised pre-training, and on ADE20K segmentation 53.6 mIoU against 49.9.

Ablations on ViT-L, 800 epochs

VariantFine-tuneLinear probe
Default (75% random, 8-block decoder, raw pixels)84.973.5
Mask tokens fed to encoder84.259.6
1-block decoder84.865.5
Normalized-pixel target85.473.9
Block masking, 75%82.863.9
Grid masking84.066.0
No augmentation84.065.7

Speed: with 8-block decoders, ViT-L trained 2.8 times faster and ViT-H 3.5 times faster than with mask tokens in the encoder. The honest caveat is linear probing: 73.5 is well behind fine-tuning, though tuning just the last block lifted ViT-L from 73.5 to 81.0.

Since then

What the authors admit, and what happened next

The paper is straightforward about the trade that matters when choosing between rungs 3, 4 and 5: MAE's features are excellent when you fine-tune the whole encoder and comparatively weak under a linear probe, where contrastive methods do better. Both halves are real. Reconstruction teaches the model a great deal about image structure, but it does not organise the representation so that categories fall on either side of a hyperplane, because nothing in the objective ever asks it to compare two images.

It is also honest that pixel reconstruction is an odd target: a pixel-level loss spends capacity on texture and high-frequency detail that carries no semantic weight, and the paper discusses normalising the target to take some of that sting out rather than claiming pixels are the right thing to predict. Later work replaced the target with features from another network, which concedes the point.

How it held up: masked image modelling became a standard pre-training family, and the asymmetric encoder is the part that is copied without argument because it is close to free. The fine-tune-versus-probe split persisted as a real choice rather than being resolved — it is the reason rung 5 exists as a separate rung rather than a refinement, and the reason a practitioner picking a frozen backbone today usually picks rung 5's and one fine-tuning a detector often starts from this one.

SOLVE IT YOURSELF

The mask ratio, and the encoder saving

MAE's efficiency is a square law you can compute. This counts visible and masked patches for a 14×14 grid, and shows the encoder's attention cost — which scales with tokens² — dropping to about a sixteenth when it processes only the visible 25% (49 patches instead of 196). Push the mask ratio higher and the encoder gets cheaper still. It also checks the loss: error on a visible patch is ignored, error on a masked patch is counted. Change the mask ratio or the patch grid and watch the saving move.

YOUR TASK

Write the body of recon_loss. Everything else in the file is already written; this is the function the paper is about. Press ▶ Run to check it against the 5 assertions below — they start red.

WHAT IT HAS TO SATISFY — 5 ASSERTIONS
  1. Visible patches = round((1 - r) * total)
  2. Masked + visible = total
  3. Encoder attention speedup is ~1/(1-r)^2
  4. A higher mask ratio makes the encoder cheaper
  5. Reconstruction loss counts only masked patches
CPython · WebAssembly
06

Why it landed

MAE closed the gap between vision and language pre-training with a method that was simple, scalable, and effective — and it reframed a stalled idea by finding the one parameter that mattered. Masked image modeling had been tried; what MAE contributed was the realization that redundancy was the enemy, that a high mask ratio was the cure, and that the asymmetry it enabled made the whole thing efficient. Masked pre-training is now a standard tool across vision, and later multimodal and video models build directly on the recipe.

The takeaway generalizes past images: a self-supervised task is only as good as it is hard. If the model can cheat — copy a neighbor, interpolate, exploit redundancy — it learns the cheat, not the structure. Make the task genuinely require understanding, and rich representations follow. MAE's 75% is that principle turned into a number.

Worth knowing

Because the encoder never sees mask tokens during pre-training (they only enter the small decoder), there's no train/inference mismatch — at downstream time the encoder just processes all the real patches normally.

Questions

Questions people ask about Masked Autoencoders

Why does MAE mask 75 percent of the patches when BERT masks only 15 percent of words?

Images are far more redundant than text. A missing word usually needs the sentence to be recovered, but a missing patch of sky or grass can be guessed from its neighbors, so a low ratio lets a model succeed by smoothing without learning anything about objects. In the paper's sweep, fine-tuning accuracy stayed flat between 40 and 80 percent masking, while linear-probe accuracy kept climbing up to about 75 percent. Seventy-five percent also means the encoder handles only 49 of 196 patches, so the same ratio that makes the task harder makes pre-training cheaper. Earlier masked-image work had used 20 to 50 percent.

Why is MAE's encoder cheaper to train than a normal Vision Transformer?

The encoder never sees the masked patches or mask tokens. At a 75 percent ratio it runs on 49 tokens rather than 196, which cuts its per-token layers by four times and its attention score matrix by sixteen. The decoder, which does see all 196 tokens, is deliberately small: by default 8 blocks of width 512 with under a tenth of the encoder's compute per token. Measured against an encoder fed mask tokens, the paper reports a 3.3 times drop in overall training compute and a wall-clock speedup of 2.8 times for ViT-L, rising to between 3.5 and 4.1 times for a larger encoder or smaller decoder.

Why does MAE do better under fine-tuning than under a linear probe?

A pixel-reconstruction objective never asks the model to place categories on opposite sides of a hyperplane, so the features are rich but not linearly organized. ViT-L reached 73.5 percent in linear probing and 84.9 in fine-tuning. The authors argue the two protocols are largely uncorrelated, and note that fine-tuning only the last Transformer block lifts ViT-L from 73.5 to 81.0 percent. Decoder depth also matters for the probe: a one-block decoder gave 65.5, while eight blocks gave 73.5, because the last layers of an autoencoder specialize in reconstruction. A deeper decoder absorbs that so the encoder stays abstract.

Does MAE reconstruct tokens like BEiT or raw pixels?

Pixels. BEiT predicts discrete tokens from a pre-trained dVAE tokenizer, which needs an extra pre-training stage on additional data. MAE tried that variant too: tokens improved fine-tuning by 0.4 points over raw pixels but gave no advantage over pixels normalized per patch, and they lowered linear-probe accuracy, 71.6 against 73.5. Normalizing each target patch by its own mean and standard deviation helped slightly, 85.4 against 84.9 on fine-tuning. A PCA target, which discards high-frequency detail, did worse. In the final comparison MAE reached 85.9 for ViT-L against 85.2 for BEiT, with a much simpler pipeline.

Does MAE need heavy data augmentation the way SimCLR does?

No. MAE works with random resized cropping and flipping alone, and adding color jitter reduced accuracy, from 84.9 to 84.3 in fine-tuning. Using no augmentation at all, just a center crop, still gave 84.0 percent, which the authors call a striking difference from contrastive methods that depend heavily on augmentation. The reason they give is that masking is itself a strong augmentation: a different random 75 percent of the patches is hidden each time, so the model rarely sees the same task twice. That also removes the large-batch and negatives requirements of contrastive training.

Check yourself

4 questions to test what you just read

Work each one out before you open it — a number, a prediction or a reason, not a definition. Producing the answer is what makes it stick. Your progress is kept on this device.

ViT-H/14 on a 224×224 image yields 256 patches. At a 75 percent mask ratio, how many tokens does the encoder see, and how do the attention score counts compare with the unmasked case?

Show answer

The encoder sees 64 tokens. Attention scores number 64² = 4,096 against 256² = 65,536 for all tokens, a factor of 16 fewer. The per-token layers shrink by only 4 times, from 256 tokens to 64, which is why the whole-pipeline speedup is far below 16.

A patch has mean pixel value 100 and standard deviation 20. What normalized target does a pixel of 140 get, and why use this target?

Show answer

(140 − 100) / 20 = 2.0. Normalizing per patch emphasizes local contrast rather than absolute brightness. In the paper it raised ViT-L fine-tuning from 84.9 to 85.4.

Feeding mask tokens into the encoder keeps all the information, yet linear-probe accuracy falls from 73.5 to 59.6. Why?

Show answer

The encoder is trained on inputs that are mostly mask tokens, but at deployment it sees only real patches. That train and test mismatch hurts the representation. It also costs 3.3 times more compute.

If you cut the mask ratio from 75 to 25 percent for a 196-patch image, by what factor do the encoder's attention scores grow, and what happens to the task?

Show answer

The encoder sees 147 tokens instead of 49, so scores grow from 2,401 to 21,609, about 9 times. The task also gets easier, since a missing patch can be interpolated from its neighbors, so features should be weaker.

Where this sits

Learning to see

From the network that started the deep-learning era to one you can segment anything with — and the shift from labels to self-supervision in between.

  1. 1AlexNet2012
  2. 2Vision Transformer2021
  3. 3SimCLR2020
  4. 4Masked Autoencoders2022
  5. 5DINOv22023
  6. 6Segment Anything2023

All 110 papers, in 19 ladders →

Finished this one? 0 / 110 Paper Breakdowns done
NextDINOv2 Next on the “Learning to see” ladder · rung 5 of 6

Explore the topic

See this alongside everything else on the same subject — handbooks, system designs, challenges and tools, in one place.

More Paper Breakdowns