Paper commentary
An Empirical Study of Training Self-Supervised Vision Transformers
Self-supervised ViT training is unstable, and accuracy numbers can hide it
- Problem Contrastive self-supervised training of Vision Transformers is often unstable. The damage can stay hidden behind final accuracy numbers that look reasonable.
- Method The authors train ViT backbones inside MoCo v3 and sweep batch size, learning rate and optimizer. They then freeze the patch projection layer, so it is never trained, which stabilizes training.
- Results With the frozen patch projection, the largest stable learning rate for SwAV rose and its accuracy improved from 65.8% to 66.4%.
Why ViT training fails quietly
Putting a ViT backbone into a contrastive Siamese framework looks like a simple swap, but in practice the training is often unstable. A drop of that size is hard to notice unless a stable run is available for comparison. Final accuracy alone does not expose the problem, so the authors track kNN accuracy over training, where the dips become visible.
The authors call this a partial failure: training keeps going, lands on a plausible score, and the degradation can stay fully hidden in exploratory work.
MoCo v3 as the training frame
MoCo v3 is an incremental revision of MoCo v1 and v2, aimed at a balance between simplicity, accuracy and scalability. Each image is cropped twice under random augmentation, and a query encoder and a momentum key encoder map the two views to vectors q and k. The learning goal is that each query retrieves its own matching key among the keys of other images.
The loss for a query is the InfoNCE form, written as \(L_q = -\log \frac{\exp(q\cdot k_+/\tau)}{\exp(q\cdot k_+/\tau)+\sum_{k^-}\exp(q\cdot k^-/\tau)}\). Here k+ is the key that the key encoder produces from the same image, and the negative keys come from the other images; the leading minus sign makes this a quantity to minimize. The vectors are l2-normalized and tau is a temperature hyper-parameter.
Two terms are computed with the views swapped and summed, so the loss is symmetrized.
Only the query encoder receives optimizer updates, and the key encoder is a moving average of it with momentum coefficient m. The default m is 0.99. The query encoder carries a prediction head that the key encoder does not have, and that head is left out of the moving average. The queue of past keys from the original MoCo is dropped, because it gives diminishing gain once the batch is large, such as 4096.
Batch size and learning rate effects
In the batch-size sweep, ViT-B/16 was trained with MoCo v3 for 100 epochs on ImageNet with AdamW at lr=1.0e-4. Batch sizes of 1024 and 2048 give smooth curves, reaching 71.5% and 72.6% linear probing accuracy. The authors hypothesize that training partly restarts and leaves its local optimum, so the final result depends on how good that restart is.
The base learning rate is scaled with batch size by the linear rule lr times BatchSize divided by 256, and the base lr is the value being tuned. The smaller learning rate is smoother but under-fits, and the larger one has more dips and lower accuracy, so the two ends fail for different reasons. The authors say lr=0.5e-4 is 1.8% below lr=1.0e-4 because of under-fitting, while at lr=1.5e-4 stability sets the accuracy.
AdamW and LAMB
AdamW is the default because it is the common choice for training ViT models. Recent self-supervised methods use LARS for large-batch training, and LAMB is the AdamW-style counterpart of LARS. LAMB was tested in the MoCo v3 setting with wd=1e-3 and batch 4096.
At lr=5e-4, LAMB reaches 72.5%, slightly above AdamW. Accuracy falls quickly above that point: lr=6e-4 is 1.6% lower and lr=8e-4 is 6.0% lower. The curves stay smooth but decline gradually in the middle, which the authors read as an accumulated effect of unreliable gradients.
LAMB can match AdamW when the learning rate is chosen carefully, but its narrow good range makes comparisons across architectures costly, so the rest of the study uses AdamW.
Freezing the patch projection
Gradient magnitudes were tracked layer by layer, and the spikes appear first in the patch projection layer and a few tens of iterations later in the last layers. A spike in the gradient lines up with a dip in the training curve. This pattern led the authors to hypothesize that instability starts in the shallower layers.
The fix replaces the learned patch projection with a fixed random projection that is never trained. In code this is a stop-gradient placed right after the projection layer. For the standard patch size, the projection maps a three-channel 16×16 patch to 768 dimensions, so a random matrix of that size is complete or overcomplete and should keep the patch information.
The gain grows with the learning rate, from 0.4% to 0.6% to 1.7%, and the authors also describe the random-projection curves as smoother.
SwAV's loss diverged to NaN when unstable; with the random projection it tolerated a larger learning rate and improved from 65.8% to 66.4% at its largest stable lr. Batch normalization or weight normalization on the learnable projection did not help, and gradient clipping helped only with a threshold small enough to freeze the layer.
The authors read this as an optimization issue: freezing narrows the solution space without changing the architecture, and the first layer is simply the easiest part to handle on its own.
ViT across self-supervised frameworks
Four frameworks were run with ViT-S/16 and ViT-B/16, all using the random projection trick and each with its own sweep of lr and wd.
The ResNet-50 entries in the same table, at 800 epochs, are 73.8, 70.4, 74.3 and 71.8 for MoCo v3, SimCLR, BYOL and SwAV. The ResNet-50 results for the other frameworks come from an improved implementation, and all runs use two 224×224 crops per image without multi-crop training. For ViT-B, MoCo v3 and SimCLR sit above the diagonal against ResNet-50, while BYOL and SwAV lie below it.
Position, class token, normalization and heads
Removing positions costs 1.6 points, which the authors read as evidence that the model learns from the patch set alone, while also showing that positional information is under-used.
With the class token removed and the final LayerNorm kept with pooling, linear accuracy is 69.7%. Global average pooling without the class token and without that LayerNorm reaches 76.3%. The authors conclude that the class token is not essential and that the choice of normalization layer matters.
The authors read this as BN not being required for contrastive learning, while appropriate BN still improves accuracy.
The authors note that m=0 resembles SimCLR, whose result in Table 4 is 73.9%.
The authors describe the gain on ViT-B as diminishing, and use 300 epochs for the larger ViT-L and ViT-H.
Against earlier self-supervised Transformers and ResNets
The authors say the MoCo-based models are more accurate and smaller than iGPT under the same linear probing protocol and training data.
For ViT-L, linear probing with MoCo v3 reaches 77.6%, compared with 76.53% for the supervised ViT-L trained on ImageNet-1k in the ViT work. The authors take this to mean self-supervised learning is less prone to over-fitting as models grow.
The ViT baseline reaches 73.4, 76.7, 77.6 and 78.1 for ViT-S, ViT-B, ViT-L and ViT-H, and ViT-BN reaches 74.1, 77.5, 78.6 and 79.1. The ViT baseline beats SimCLR v2 ResNets only in the small-model range and becomes comparable for larger models, while SK-ResNet and wider or deeper BYOL ResNets sit higher. ViT-BN replaces LayerNorm with BatchNorm only in the MLP blocks, which the authors report gives about a 1% gain.
Reducing the patch size to 7×7 keeps model size but raises FLOPs to roughly 6×, and the authors report a gain of about 2 to 3%.
End-to-end fine-tuning
Fine-tuning uses the DeiT codebase and its default settings unless stated otherwise. MoCo v3 with ViT-B reaches 83.2% top-1 after 150 epochs of fine-tuning, against 81.8% for DeiT ViT-B at 300 epochs. ViT-L with MoCo v3 reaches 84.1% after 100 epochs with a drop path rate of 0.5, which the authors take as evidence that MoCo pre-training works well on short schedules.
The fine-tuning schedules in the rows differ, so the comparisons cross different epoch counts. The authors also report that DeiT-L diverged under its default settings, so a different solution may be needed for that model.
Transfer to small datasets
The authors say accuracy stops improving at ViT-H, and that the from-scratch models overfit more as they grow.
The authors note that supervised ViT results from larger datasets such as ImageNet-21k or JFT-300M can transfer better to these small sets than their model does, and propose self-supervised pre-training on bigger data as future work.
Training cost and hardware
ViT-B/16 for 100 epochs takes 2.1 hours on TPUs, and each 300-epoch ablation run takes 6.3 hours. ViT-H/14 needs 9.8 hours per 100 epochs on 512 TPUs, and the 300-epoch run amounts to about 625 TPU·days. Per 100 epochs, FLOPs for ViT-B/16 are 17.5 G, 4.3 times ResNet-50, and for ViT-H/14 they are 166.7 G, 40.7 times ResNet-50.
The PyTorch run takes 24 hours for ViT-B on 128 GPUs, compared with 2.1 hours for the TPU run on 256 TPUs. The authors say TPUs scale more favorably than GPUs in their setup, and that further GPU engineering could narrow the gap.
Limitations
The patch projection trick reduces instability but does not remove it, and the model can still become unstable when lr is too large. Accuracy of the bigger self-supervised ViT models is saturated in the study, and the authors suggest that more data or harder pretext tasks may be needed. ViT-BN-H/7 ran out of memory in the unoptimized implementation, so that configuration is absent.
The kNN monitor uses a memory bank of 10% of ImageNet and class-token features, and the authors judge it sufficient to reflect training stability. The ResNet comparisons mix LayerNorm and BatchNorm choices with other design differences, which the authors acknowledge.
A postscript reports that an author of BiT and ViT saw the same behaviour for supervised BiT-ResNet on ImageNet-21k, so the instability may be more general than this study's scope.
Paper info
- Authors
- Xinlei Chen, Saining Xie, Kaiming He
- Venue
- arXiv · Preprint
- First published
- 2021-4-5
- Added
- 2026-11-12
- Updated
- 2021-8-16
- Version
- v4
- arXiv
- 2104.02057
- Category
- cs.CV · cs.LG
- Original paper terms
- CC BY 4.0
- What we show
- Paper info and commentary
AI commentary is a separate explanation from the original paper and may contain errors. The original paper and figures follow each source's terms of use and any rights noted separately.
Quote from the paper
Xinlei Chen, Saining Xie, Kaiming He (2021). An Empirical Study of Training Self-Supervised Vision Transformers. arXiv:2104.02057v4. https://arxiv.org/abs/2104.02057v4
BibTeX
@misc{chen2021empirical,
title = {{An Empirical Study of Training Self-Supervised Vision Transformers}},
author = {Xinlei Chen and Saining Xie and Kaiming He},
year = {2021},
eprint = {2104.02057},
archivePrefix = {arXiv},
url = {https://arxiv.org/abs/2104.02057v4}
}