Foundations to Frontier
Tests
E. Deep Learning › E16 Self-Supervised and Contrastive Learning

Masked Autoencoders (MAE)

Masked Autoencoders (He and colleagues, CVPR 2022) bring BERT-style masked prediction to images. Split an image into patches and hide most of them (75%). A Vision Transformer encoder processes only the visible patches, and a light decoder reconstructs the missing pixels. High masking makes the task hard enough to require understanding, and processing only visible patches makes pre-training about 3× faster. MAE features fine-tune excellently but are weaker under linear probing. This page covers patch masking, the asymmetric encoder–decoder and the masking-ratio trade-off. In the exercises you implement patch masking, then train small masked autoencoders on noisy signals at masking ratios from 0 to 75%. The result is a negative replication. With MLPs and a nearest-centroid probe, more masking made features worse: 0.68 at 0% down to 0.54 at 75%, below raw signals (0.63), on all three seeds. MAE's benefits depend on scale, Transformer encoders and fine-tuning, which this toy lacks.

Level 0: Foundation

★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.

Hide most of the picture, then fill it in

A masked autoencoder covers up most of an image (three quarters of its patches) and learns to paint the missing parts back in from the few that remain. Filling in that much forces the network to understand what objects look like, not just to copy nearby pixels. Because the encoder only looks at the visible quarter, training is also much cheaper. The demo shows a patch grid with a chosen fraction hidden.

Self-supervised learning lab
Visible patches: 40 of 196. The MAE encoder sees only these, so its attention cost is about 4% of the unmasked cost; the decoder reconstructs the rest.
Why engineers care

Masked pre-training scales to huge unlabelled image sets cheaply, and the same idea fills gaps in sensor data such as occluded camera views, dropped telemetry and missing satellite pixels.

📝 Quick check (Level 0)
0/3 answered
  1. 1. MAE hides about what fraction of patches?
  2. 2. The encoder processes…
  3. 3. The decoder…

Level 1: Beginner

★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.

By hand

  • Patches: a 224 × 224 image in 16 × 16 patches gives 196 patches. At 75% masking, 49 are visible.
  • Asymmetric design:
    • the ViT encoder runs on visible patches only, so its attention cost is (1 − 0.75)² = 1/16 of full;
    • a light decoder takes the encoded patches plus mask tokens at all positions and predicts the pixels of the masked patches.
  • Loss: MSE on the (per-patch normalised) pixels of masked patches only. For example, errors (1, 3) give (1 + 9)/2 = 5.
  • Masking ratio: He and colleagues found 75% best for ImageNet. Images are spatially redundant, so low ratios can be solved by local interpolation.
  • Evaluation: MAE features shine after fine-tuning; linear probing lags considerably.
📝 Quick check (Level 1)
0/3 answered
  1. 1. Visible patches of 196 at 75% masking:
  2. 2. Encoder attention cost at 75% masking:
  3. 3. The loss is computed on…

Level 2: Intermediate

★★★☆☆ Intermediate⏱ 25 minUndergraduate students and practising engineers.

MAE in code

Exercise 1.

  • Patch masking hid exactly 147 of 196 patches at 75%.
  • The masked MSE averaged only over hidden patches.

Practice.

  • Use the official MAE code or timm ViTs, with mask ratio 0.75, a decoder 8 blocks deep and 512 wide, normalised pixel targets, and long pre-training (800–1600 epochs). Then fine-tune.
  • Random masking beats block or grid masking at high ratios.
  • For video, ratios of 90% or more work (VideoMAE), since frames are even more redundant.
x = patchify(imgs)                                       # (B, N, P*P*3)
noise = torch.rand(B, N); keep = noise.argsort(1)[:, : int(N * (1 - 0.75))]
latent = encoder(gather(x, keep))                        # only visible patches
pred = decoder(unshuffle(latent, keep, mask_token))      # all positions
mask = torch.ones(B, N).scatter_(1, keep, 0)
loss = (((pred - x) ** 2).mean(-1) * mask).sum() / mask.sum()
🐍 Exercise 1: patch masking
Implement random patch masking, the number of visible tokens and the masked reconstruction error.
📝 Quick check (Level 2)
0/3 answered
  1. 1. Best masking ratio found for ImageNet:
  2. 2. Video MAE uses masking ratios of…
  3. 3. MAE features are strongest after…

Level 3: Advanced

★★★★☆ Advanced⏱ 25 minGraduate level, with full derivations and proofs.

Theory

Why high masking.

  • Natural images are highly redundant: a missing patch can often be filled by interpolating its neighbours.
  • At low masking, reconstruction needs only local, low-level statistics. At high masking, with most context gone, the network must infer object-level structure.
  • The task becomes a semantic one, unlike BERT’s 15% masking, which is enough for language because words carry dense information.

Reconstruction versus invariance.

  • Contrastive and joint-embedding methods learn features invariant to augmentations, which suits linear probes.
  • MAE learns features that retain everything needed to reconstruct pixels, including low-level detail. These are rich but less linearly separable by class, which is why MAE fine-tunes better than it linearly probes.

When it fails. Without spatial redundancy, a high-capacity encoder (Transformers) and fine-tuning, masked reconstruction mostly learns to model nuisance variation. Pixel-level noise and style dominate the reconstruction loss and the features drift away from class content.

📝 Quick check (Level 3)
0/3 answered
  1. 1. Low masking ratios can be solved by…
  2. 2. MAE features are less linearly separable because they…
  3. 3. BERT masks about…

Level 4: Expert

★★★★★ Expert⏱ 30 minResearchers: original papers and open problems.

Does more masking help small models?

History.

  • K. He, X. Chen, S. Xie, Y. Li, P. Dollár and R. Girshick, “Masked autoencoders are scalable vision learners” (CVPR 2022).
  • Related work: BEiT (2021, tokenised targets), SimMIM (2022) and VideoMAE (2022).

Exercise 2.

  • World: 1-D signals of 32 samples in 8 patches. Signals are a random gain × a smooth class template (10 classes) + a random offset + strong noise.
  • Model: an MLP masked autoencoder (encoder 64 → 64 → 16, decoder 16 → 64 → 32). Whole patches are hidden at ratios of 0, 25, 50 or 75%, with loss on hidden samples, 1000 steps.
  • Evaluation: a nearest-centroid probe on the encoder output for full signals.
masking ratio probe accuracy
raw signals 0.628
0% (plain autoencoder) 0.680
25% 0.612
50% 0.598
75% 0.544
  • A negative replication: in this small setting, more masking gave worse features at every step, and 75% fell below raw signals. Seeds 1 and 2 showed the same monotone drop (0.71 → 0.52 and 0.63 → 0.48).
  • Why: the MLP must reconstruct noisy samples. At high masking it spends its capacity predicting gain, offset and noise-dominated values instead of separating classes, and nothing in a nearest-centroid probe rewards that.
  • What is missing compared with MAE: a Transformer encoder that processes visible patches as tokens, real image redundancy, very long training, and fine-tuning instead of probing. The headline result (75% best) should not be expected to transfer to small models.
🐍 Exercise 2 (research): does more masking help small models?
Train small masked autoencoders on patched signals at several masking ratios and compare nearest-centroid probe accuracy.

Masked modelling in AI, robotics and aerospace

  • Large-scale pre-training: ViT backbones for satellite and aerial imagery (SatMAE and similar).
  • Gap filling: reconstructing occluded camera regions, missing telemetry or cloud-covered pixels.
  • Design lesson: check that your setting has the ingredients a method relies on (redundancy, scale, architecture, evaluation protocol) before expecting its headline result.

References

  1. K. He, X. Chen, S. Xie, Y. Li, P. Dollár and R. Girshick (2022). “Masked autoencoders are scalable vision learners.” Proc. CVPR.
  2. H. Bao, L. Dong, S. Piao and F. Wei (2022). “BEiT: BERT pre-training of image transformers.” ICLR.
  3. Z. Tong, Y. Song, J. Wang and L. Wang (2022). “VideoMAE: masked autoencoders are data-efficient learners for self-supervised video pre-training.” NeurIPS.