wikillm

Paper · 2021-11-18 · cs.CV

SimMIM: A Simple Framework for Masked Image Modeling

SimMIM pre-trains vision Transformers by replacing randomly chosen 32x32 patches with a learnable mask token and regressing the raw pixels of the masked area with a single linear layer and an l1 loss. Author-reported: 83.8% ImageNet-1K fine-tuning top-1 with ViT-B (+0.6 over BEiT) and 87.1% with SwinV2-H at 512^2, using ImageNet-1K only.

Masked image modelingSelf-supervised learningVision Transformer (ViT) backbonesself-supervised-learningmasked-image-modelingvision-transformercomputer-visionpretraining

arXivPDFCodeMarkdown13 min read

AI-compiled by claude-opus-5-5 on 2026-10-18, not yet reviewed by a human. Every claim cites its source; check it when a detail matters.

In plain words

Language models such as BERT learn by filling in blanked-out words. SimMIM asks whether the same trick works for images: hide parts of a photo, ask a network to paint the missing parts back in, and hope that doing so teaches it what objects look like. No labels are needed, so the network can learn from plain images before it is fine-tuned for a real task such as classification.

Earlier attempts added extra machinery: a separate network that turns image pieces into "visual words", special block-shaped masks, or colour palettes. SimMIM drops all of it. It cuts the image into a grid of large squares, hides a random subset of them, and asks the network to output the original colour values of the hidden pixels. The part that turns the network's features back into pixels is a single linear layer, and the score is just how far the predicted colours are from the true ones, measured only on the hidden squares.

The paper's main lesson is that the masks must be big enough that the network cannot cheat by copying neighbouring pixels. Hiding small squares, or rebuilding the visible parts as well, leads to worse features. A heavier decoder paints nicer pictures but does not give a better network for the downstream task. This plain recipe matches or beats the more complicated methods it is compared against and was used to help train a 3-billion-parameter model.

TL;DR

SimMIM is masked image modeling stripped to four simple choices: patch-aligned random masking with a large masked patch (32×32, ratio 0.6), a learnable mask token replacing masked patch embeddings, a linear (1×1 conv) head that predicts all raw RGB pixels of each output feature's receptive patch, and an \ell_1 loss computed only on masked pixels §3 fig. 1. Ablations on Swin-B show heavier heads, classification targets (iGPT clusters, BEiT dVAE tokens, colour bins) and full-image reconstruction do no better or worse tab. 2 tab. 4 tab. 5. Author-reported ImageNet-1K fine-tuning: 83.8% top-1 for ViT-B (BEiT 83.2) tab. 6 and 87.1% for SwinV2-H at 512² tab. 7. Relevant if you need a cheap, tokenizer-free MIM pre-training recipe that works for hierarchical backbones (Swin) as well as ViT; it is not for linear-probe use (56.7% linear eval on ViT-B) tab. 6.

Problem

Masked signal prediction drives NLP pre-training, but in vision it was overshadowed by contrastive learning §1. The authors list three modality gaps: strong pixel locality lets a model fill holes by copying neighbours instead of reasoning; pixels are low-level signals unlike human-made word tokens; and pixels are continuous whereas MLM classifies discrete tokens §1. Prior MIM methods bridged these with special designs (iGPT colour clustering, BEiT's dVAE tokenizer plus block-wise masking) §1 §2. The paper asks which components actually matter and whether simple choices suffice §3.1.

Method

SimMIM has four components: masking strategy, encoder, prediction head, prediction target §3.1. The encoder is a standard ViT or Swin Transformer §3.1.

Masking §3.2

  1. Divide the input into a grid of masked patches of size s \times s (default s=32, which equals the last-stage patch size of Swin; also 32 for ViT) §3.2 §4.1.1. Each masked patch is either fully visible or fully masked §3.2.
  2. Choose a random subset of masked patches with ratio r (default 0.6) §4.1.1. Code picks exactly \lceil r \cdot N \rceil of the N=(H/s)^2 cells via a random permutation, then upsamples the mask to the model's patch-embedding grid (4×4 tokens for Swin, 16×16 for ViT) data_simmim.py.
  3. After patch embedding, replace every masked token with a single learnable mask-token vector of the embedding dimension §3.2. Code: x = x*(1-w) + mask_token*w, done before absolute position embeddings (Swin) or before prepending the class token and adding position embeddings (ViT); mask token is truncated-normal initialised with std 0.02 simmim.py simmim.py.

Encoder and head §3.3 §3.4

  1. Run the encoder on the full token sequence (masked tokens included) and take the final feature map, down-sampled 32× (Swin) or 16× (ViT) §3.4 simmim.py.
  2. Prediction head: a 1×1 convolution (a linear layer per feature vector) with output dimension \text{stride}^2 \times 3, e.g. 3072 = 32 \times 32 \times 3 for Swin, so each feature vector predicts the RGB values of its own 32\times32 pixel block §3.4. Code reshapes with PixelShuffle to a full-resolution image simmim.py.

Loss §3.4

  1. \ell_1 loss averaged over masked pixels only:

L = \frac{1}{\Omega(\mathbf{x}_M)}\|\mathbf{y}_M - \mathbf{x}_M\|_1 eq. 1

where \mathbf{x},\mathbf{y} \in \mathbb{R}^{3HW\times 1} are input and predicted RGB values, M the set of masked pixels and \Omega(\cdot) the number of elements §3.4. Code: element-wise L1, multiplied by the pixel-level mask, summed, divided by (mask pixel count + 1e-5) and by the 3 channels simmim.py.

Pseudocode
Algorithm: SimMIM pre-training step  [§3] [code: models/simmim.py#L114-L121]
Input: batch of images x (B,3,H,W), normalised; mask m (B, H/p, W/p) at model-patch grid p
1. tokens = patch_embed(x)                          # (B, L, C)
2. tokens = tokens*(1-m) + mask_token*m             # per-token replacement
3. z = encoder(tokens)  -> feature map (B, C', H/S, W/S), S = 32 (Swin) or 16 (ViT)
4. y = PixelShuffle_S( Conv1x1(C' -> S*S*3)(z) )    # (B,3,H,W)
5. M = upsample m by p to pixel resolution
6. loss = sum(|x - y| * M) / (sum(M) + 1e-5) / 3
Output: loss

Alternatives studied §3.2 §3.3 §3.4

  • Masking: square masking (a centrally placed region from Context Encoder, relaxed to random position) and BEiT block-wise masking at masked-patch sizes 16 and 32 §3.2.
  • Heads: 2-layer MLP, inverse Swin-T, inverse Swin-B §3.3.
  • Lower target resolutions: downsample the original image by 32×, 16×, 8×, 4×, 2× §3.4.
  • Classification targets: iGPT's 512 k-means colour clusters (the cluster centres learned in iGPT), BEiT dVAE tokens, and channel-wise equal-width colour bins (e.g. 8 or 256 bins per channel) §3.4.
  • Losses: \ell_2 and smooth-\ell_1 §3.4.
  • Reconstruction variant: also regress visible pixels (loss on the whole image) §4.1.5 tab. 4.

None of these alternatives is in the released code; it implements only the default linear head and masked \ell_1 loss simmim.py.

AvgDist diagnostic §4.1.2 fig. 3: the mean Euclidean distance from each masked pixel to the nearest visible pixel. Fine-tuning accuracy follows a ridge over AvgDist, with the best entries roughly in [10, 20] §4.1.2 fig. 3; the appendix calls an AvgDist of 15 empirically good app. C.2 fig. 7. The exact computation procedure (units, sampling) is not stated beyond the definition.

ConvNet variant app. E: for ResNet-50×4, the mask token replaces features after a stem of a 3×3 stride-2 convolution followed by 2×2 max-pooling.

Hyperparameters

Ablation default (Swin-B, ImageNet-1K) §4.1.1. Code LRs are specified per 512 images and scaled linearly by total batch / 512 main_simmim.py main_finetune.py.

NameValueWhereNotes
backboneSwin-B, window 6 at 192²§4.1.1 tab. 8embed 128, depths {2,2,18,2}, heads {4,8,16,32} simmim_pretrain__swin_base__img192_window6__100ep.yaml
pre-train input size192²§4.1.1
masked patch size32§4.1.1
mask ratio0.6§4.1.1code takes ceil, so 22 of 36 cells at 192² (0.61) data_simmim.py
headlinear, target 192²§4.1.1
loss\ell_1 on masked pixels§4.1.1 eq. 1
optimizerAdamW, β1=0.9, β2=0.999§4.1.1eps 1e-8 config.py
batch size2048§4.1.1README example: 16 GPUs × 128 README.md
base LR (pre-train)8e-4§4.1.1code 2e-4 per 512 → 8e-4 at 2048 simmim_pretrain__swin_base__img192_window6__100ep.yaml
LR schedule (pre-train)cosine, 10-epoch warm-up§4.1.1warm-up start 1e-6, min 1e-5 (per 512) in code only simmim_pretrain__swin_base__img192_window6__100ep.yaml
pre-train epochs100§4.1.1
weight decay0.05§4.1.1code exempts 1-D params, biases, mask token optimizer.py simmim.py
augmentation (pre-train)RandomResizedCrop scale [0.67, 1], ratio [3/4, 4/3], random flip, colour normalisation§4.1.1ImageNet mean/std data_simmim.py
drop path (pre-train)not stated0.0 for Swin configs, 0.1 for ViT simmim_pretrain__vit_base__img224__800ep.yaml
gradient clippingnot statedmax norm 5.0 config.py
mixed precisionnot statedapex AMP O1 by default main_simmim.py
fine-tune optimizerAdamW, β1=0.9, β2=0.999, wd 0.05§4.1.1
fine-tune batch / base LR2048 / 5e-3§4.1.1code 1.25e-3 per 512 simmim_finetune__swin_base__img224_window7__100ep.yaml
fine-tune schedule100 epochs, cosine, 10-epoch warm-up§4.1.1code uses 20 warm-up epochs (see Paper vs code)
stochastic depth (fine-tune)0.1§4.1.1
layer-wise LR decay0.9§4.1.1
fine-tune augmentationRandAug, Mixup, CutMix, label smoothing, random erasing (as BEiT)§4.1.1code: rand-m9-mstd0.5-inc1, mixup 0.8, cutmix 1.0, smoothing 0.1, erase p 0.25 config.py config.py

ViT-B system comparison §4.2:

NameValueWhereNotes
input size224²§4.2
pre-train epochs800§4.2
pre-train schedulecosine, 20-epoch linear warm-up§4.2code differs (see Paper vs code)
other pre-train settingsas ablation§4.2
fine-tune layer decay0.65§4.2simmim_finetune__vit_base__img224__800ep.yaml
linear probebest intermediate layer, 100 epochs, 5-epoch warm-up§4.2LR, batch size not stated

Swin scaling §4.3 tab. 8:

NameValueWhereNotes
modelsSwin-B (C=128), Swin-L (C=192), SwinV2-H (C=352), SwinV2-G; depths {2,2,18,2} for B/L/H§4.3 tab. 8H and G use the SwinV2 variants
pre-train window size6 / 12 / 12 (B/L/H) at 192²tab. 8
fine-tune window size7 / 14 / 14 at 224²tab. 8
pre-train epochs800 (ImageNet-1K)§4.3
pre-train LRstep: 4e-4 for first 7/8 of epochs, then ÷10§4.3code 1e-4 per 512, multistep at epoch 700 simmim_pretrain__swin_base__img192_window6__800ep.yaml
fine-tune epochs100; 50 for SwinV2-H§4.3
fine-tune resolution224²; SwinV2-H also 512²§4.3
layer decay0.8 / 0.75 / 0.7 (B/L/H)§4.3
SwinV2-G dataprivate ImageNet-22K-ext§4.3details deferred to the SwinV2 paper

Downstream (appendix) app. C.1:

TaskSettingsWhere
iNat-2018AdamW, 100 epochs, batch 2048, LR 1.6e-2, wd 0.05, β 0.9/0.999, drop path 0.1, layer decay 0.9, BEiT augmentationapp. C.1
COCO (Mask R-CNN)3× schedule (36 epochs), AdamW LR 6e-5, wd 0.05, batch 32, large-scale jitter 1024², scale [0.1, 2.0]; window 7 (Swin-B), 14 (Swin-L, SwinV2-H)app. C.1
ADE20K (UPerNet)AdamW, wd 0.05, batch 32, layer decay 0.9, LR searched in {1e-4, 3e-4}, 80K iters, 512², window 20; multi-scale test [0.75, 0.875, 1.0, 1.125, 1.25] × 512×2048; initialised from the ImageNet-1K fine-tuned weightsapp. C.1

Results

Author-reported. Ablations: Swin-B, 192², 100-epoch pre-training and fine-tuning on ImageNet-1K, top-1 §4.1.1.

Masking strategy tab. 1:

Mask typeMasked patch sizeRatioTop-1 (%)
square320.11 / 0.25 / 0.4482.6 / 82.5 / 82.5
block-wise16 / 320.482.7 / 82.7
block-wise16 / 320.682.6 / 82.6
block-wise16 / 320.882.4 / 82.5
random4 / 8 / 16 / 320.481.9 / 82.0 / 82.4 / 82.9
random4 / 8 / 16 / 320.682.0 / 82.1 / 82.7 / 82.8
random4 / 8 / 16 / 320.882.1 / 82.4 / 82.8 / 82.4
random640.1 / 0.282.6 / 82.6
random320.1 / 0.2 / 0.3 / 0.4 / 0.582.7 / 82.8 / 82.8 / 82.9 / 83.0
random320.6 / 0.7 / 0.8 / 0.982.8 / 82.7 / 82.4 / 82.4

Prediction head tab. 2 tab. 10:

Head#paramsTraining costImageNetiNat-2018COCO AP^{box}ADE20K mIoU
Linear89.9M1×82.875.249.950.0
2-layer MLP90.9M1.2×82.875.050.149.9
inverse Swin-T115.2M1.7×82.474.949.849.4
inverse Swin-B174.8M2.3×82.575.049.849.0

Heavier heads reach slightly lower pre-training loss (0.3722 inverse Swin-B vs 0.3743 linear) yet transfer worse §4.1.3.

Prediction resolution tab. 3:

Target size6²12²24²48²96²192²
Top-1 (%)82.382.782.882.782.882.8

Prediction target tab. 5 tab. 11:

LossResolutionImageNetiNat-2018COCO AP^{box}ADE20K mIoU
8-bin192²82.7
8-bin48²82.775.350.049.7
256-bin192²N/A
256-bin48²82.374.649.749.3
iGPT cluster192²N/A
iGPT cluster48²82.475.049.649.1
BEiT–82.775.250.148.8
\ell_2192²82.7
smooth-\ell_1192²82.7
\ell_1192²82.875.249.950.0
\ell_148²82.7
\ell_16²82.3

Channel-wise bin classification, Swin-B, 100 epochs tab. 13:

Resolution2 bins481632256
6²82.582.782.882.982.882.4
48²82.582.882.782.682.582.3
192²82.782.982.782.7N/AN/A

Loss scope tab. 4: masked area only 82.8 vs full image 81.7.

LR scheduler (Swin-B, both with 10-epoch warm-up; step: 8e-4, ÷10 at 90% and 95%) app. B tab. 9: cosine 82.8 (100 ep) / 83.0 (300 ep); step 82.9 / 83.3.

ViT-B system comparison, ImageNet-1K tab. 6:

MethodInputFine-tune top-1Linear eval top-1Pre-train cost (relative)
Supervised (DeiT)224²81.8––
DINO224²82.878.22.0×
MoCo v3224²83.276.71.8×
ViT384²79.9–~4.0×
BEiT224²83.256.71.5× (dVAE pre-training not counted)
SimMIM224²83.856.71.0×

Swin scaling, ImageNet-1K, pre-trained at 192² tab. 7:

MethodFine-tune res.BackboneTop-1Params
Supervised224²Swin-B83.388M
Supervised224²Swin-L83.5197M
Supervised224²SwinV2-H83.3658M
SimMIM224²Swin-B84.088M
SimMIM224²Swin-L85.4197M
SimMIM224²SwinV2-H85.7658M
SimMIM512²SwinV2-H87.1658M
SimMIM (ImageNet-22K-ext)640²SwinV2-G90.23.0B

Swin scaling on COCO and ADE20K tab. 12:

BackboneSup. COCO AP^{box}Sup. ADE20K mIoUSimMIM COCO AP^{box}SimMIM ADE20K mIoU
Swin-B50.250.452.352.8
Swin-L50.950.053.853.5
SwinV2-H50.249.854.454.2

SwinV2-G (3B), trained with SimMIM's help on data the authors describe as ~40× smaller than JFT-3B: 84.0% ImageNet-V2 top-1, 63.1/54.4 COCO box/mask mAP, 59.9 ADE20K mIoU, 86.8% Kinetics-400 top-1; details deferred to the SwinV2 paper §4.3.

ResNet-50×4: 81.6% ImageNet-1K top-1 after 300-epoch pre-training and 100-epoch fine-tuning, vs 80.7% supervised app. E.

Paper vs code

Checked at d3e29bc. The README notes SimMIM was later merged into the Swin Transformer repository README.md; this card uses the standalone repo.

Pitfalls

  • Learning rates are batch-scaled in code. Config BASE_LR is per 512 images and multiplied by total batch / 512 (and by gradient-accumulation steps) main_simmim.py. Copying 8e-4 into a config at batch 2048 gives 4× the intended LR.
  • "Masked patch" ≠ model patch. Masks are drawn on a 32×32 grid and then expanded to the 4×4 (Swin) or 16×16 (ViT) token grid; the image size must divide by 32 and 32 by the model patch size data_simmim.py. Using a 16-pixel mask with ViT/16 is the small-patch setting that scored worse tab. 1.
  • Mask ratio is rounded up. \lceil 0.6 \times 36 \rceil = 22 cells at 192² and \lceil 0.6 \times 49 \rceil = 30 at 224², so the actual ratio is about 0.61 data_simmim.py.
  • Encoder sees the full sequence. Masked positions stay in the sequence as mask tokens §3.2 simmim.py, so every pre-training step runs the encoder over all tokens of the image. The relative cost figures (1.0× vs BEiT 1.5×, DINO 2.0×) are attributed by the authors to the method's simplicity §4.2 tab. 6, not to skipping tokens.
  • Loss only on masked pixels. Including visible pixels drops fine-tuning from 82.8 to 81.7 tab. 4; visualisations of the visible area show checkerboard artifacts because it is never trained §4.4 fig. 4. Don't judge the model by full-image reconstructions.
  • Lower inpainting loss ≠ better features. Heavier heads lower the loss but transfer worse §4.1.3; select checkpoints by downstream fine-tuning, not pre-training loss.
  • Linear probing is weak. 56.7% on ViT-B tab. 6; SimMIM features are meant for fine-tuning, and the authors explicitly do not optimise for linear evaluation §3.5.
  • Table 1 caveat. The 0.5-ratio best (83.0) is not the default; 0.6 was chosen for stability §4.1.2. Differences across most settings are a few tenths of a point, and no variance over seeds is reported.
  • Classification targets at 192² are N/A for 256 bins and iGPT clusters tab. 5; the reason is not stated.
  • Table 6 linear eval values. SimMIM and BEiT are both listed at 56.7% tab. 6; the paper does not comment on the coincidence.
  • ADE20K numbers start from the supervised fine-tuned checkpoint, not the raw self-supervised weights, because that worked better app. C.1. Reproductions that initialise from pre-trained weights directly should expect lower numbers.
  • SwinV2-G results depend on a private dataset (ImageNet-22K-ext) and the SwinV2 paper's recipe §4.3; they cannot be reproduced from this paper alone.
  • Scaling recipe uses a step LR schedule, chosen so runs of different lengths can share the first stage §4.3; step was marginally better than cosine in the appendix tab. 9, so the cosine ablation numbers and step-scheduled scaling numbers are not perfectly comparable.

Lineage

  • Builds on: BEiT (arXiv:2106.08254), keeping the learnable mask token but dropping the dVAE tokenizer and block-wise masking §3.2 §3.4; Context Encoder (pixel inpainting with square masks) §2; iGPT colour clustering as an alternative target §3.4. Encoders: ViT and Swin Transformer / SwinV2 §3.1 §4.3.
  • Compared against: supervised DeiT/Swin baselines, DINO, MoCo v3, ViT's masked patch prediction, BEiT tab. 6 tab. 7.
  • Superseded by: not known from content in this wiki.

Source and license

Adapted from “SimMIM: A Simple Framework for Masked Image Modeling” by Zhenda Xie, Zheng Zhang, Yue Cao, Yutong Lin, Jianmin Bao, Zhuliang Yao, Qi Dai, Han Hu (arXiv:2111.09886), used under CC BY 4.0. Changes: condensed and restructured for implementation, with tables, equations and algorithms taken from the paper and its code, each cited to its source. Not affiliated with or endorsed by the authors. Code citations refer to https://github.com/microsoft/SimMIM at commit d3e29bcac950 (MIT). Any quoted code is shown under that license.