Paper commentary
Swin Transformer V2: Scaling Up Capacity and Resolution
Swin Transformer V2 trains a 3 billion-parameter vision model at high resolution
- Problem Scaling the Swin Transformer up made training unstable, and position biases learned at low resolution transferred poorly to larger windows. The authors also found that very large vision models needed huge labelled datasets.
- Method Swin Transformer V2 uses post-norm, cosine attention, and log-spaced position bias.
- Results The 3 billion-parameter SwinV2-G reached 84.00 top-1 accuracy on ImageNet-V2, above 83.33 for ViT-G. The model was trained with about 1/40 of the labelled data used for JFT-3B.
Three obstacles to scaling a vision Transformer
Making the original Swin Transformer much larger exposes a training problem: the activation amplitudes at deeper layers grow far beyond those at shallow layers, and at the largest size training cannot finish. The authors trace this to the way each residual output is added back into the main branch, so the amplitude accumulates with depth.
A second obstacle appears when a model pre-trained on small images is used for dense tasks with larger windows. The usual remedy is to resize the learned bias map by bi-cubic interpolation, which the authors describe as ad hoc and often sub-optimal.
The third obstacle is cost. Large models need much more GPU memory than regular implementations provide, and large vision models have relied on very large labelled sets such as JFT-3B. The study addresses all three in one scaled-up model, a 3 billion-parameter Swin Transformer V2 trained with images up to 1,536×1,536.
Residual post-norm and scaled cosine attention
In the original pre-norm layout, layer norm sits at the start of each block and each residual output is added straight back to the main branch, so the main branch keeps growing with depth. Residual post-norm keeps the same residual structure but moves the layer norm to the output of each residual branch, before the addition, so the main branch no longer accumulates amplitude layer by layer.
In the largest models an extra layer norm is also placed on the main branch every 6 blocks, to further stabilize training.
The second change replaces the dot product of queries and keys with a cosine similarity, which the authors use because dot-product attention maps in large models were often dominated by a few pixel pairs. The attention logit is cos(q_i, k_j) divided by a learnable scalar τ, with the relative position bias B_ij added: \\( \\mathrm{Sim}(q_i,k_j)=\\cos(q_i,k_j)/\\tau+B_{ij} \\).
Here τ is non-shared across heads and layers and is set larger than 0.01. Because the cosine is normalized, the logits no longer scale with the magnitude of the block inputs, so the attention values are less likely to reach extremes.
How Log-CPB reuses a position bias across window sizes
Log-CPB replaces the stored bias table with a small meta network G that takes relative coordinates and returns a bias value, with a two-layer MLP and a ReLU activation as the default. Since G accepts any coordinate, its weights carry over when the window size changes, and fine-tuning can use any window range.
Before the meta network sees the offsets, each relative coordinate is mapped through a signed logarithm, with the formula \\( \\hat{\\Delta x}=\\mathrm{sign}(x)\\cdot\\log(1+|\\Delta x|) \\) and the analogous expression for the y axis. The mapping compresses large offsets, so a bigger window requires extrapolating over a much narrower range of inputs than with linear coordinates.
For a move from 8×8 to 16×16 windows, raw coordinates extend from [−7, 7]×[−7, 7] to [−15, 15]×[−15, 15], an extrapolation ratio of 1.14×. With log-spaced coordinates the range goes from [−2.079, 2.079]×[−2.079, 2.079] to [−2.773, 2.773]×[−2.773, 2.773], an extrapolation ratio of 0.33×, which is about four times smaller. Because the biases can be computed in advance and stored as parameters, inference runs the same way as with a parameterized bias table.
Memory measures for 3 billion parameters at high resolution
In a standard data-parallel setup every GPU holds the full parameters and optimizer states. The authors estimate that a 3 billion-parameter model would need 48G of GPU memory with AdamW and fp32 weights and states. The ZeRO stage-1 option splits the parameters and optimizer states across GPUs, which lowers memory use with little effect on training speed.
Activation check-pointing recomputes feature maps during the backward pass instead of storing them, which removes a large share of the memory used by Transformer feature maps. The authors report that this can make training up to 30% slower.
At 1,536 resolution with 32×32 windows, self-attention in the first two stages became the bottleneck, so the authors compute it sequentially, one window pass at a time, rather than as a single batch. With these measures the 3 billion-parameter model was trained on Nvidia A100-40G GPUs for COCO detection at 1,536×1,536 and for Kinetics-400 at 320×320×8.
Model configurations and the SwinV2-G pre-training recipe
The stage, block and channel settings follow the original Swin Transformer for the small configurations, with C set to 96, 128 or 192 for T, S/B and L. The huge and giant models use C = 352 and C = 512, with the giant SwinV2-G at 3 billion parameters and the huge model at 658 million parameters.
The dataset has about 70 million images, which the authors describe as 1/40 of the labelled data used for JFT-3B. Overlapping images with the ImageNet-1K V1 and V2 validation sets were removed by a duplicate removal process.
For the largest model an additional detection pre-training phase on Objects365 v2 sits between the classification pre-training and the COCO fine-tuning. The authors did not align complexity or pre-training data with the compared models, because their main goal was feasibility of scaling.
Effect of the normalization and attention changes at fixed size
On ImageNet-1K, adding res-post-norm and scaled cosine attention raised top-1 accuracy from 81.5 to 81.7 for Swin-T, from 83.2 to 83.6 for Swin-S and from 83.6 to 84.1 for Swin-B. The overall gains are +0.2%, +0.4% and +0.5% for the tiny, small and base sizes, which the authors read as larger benefit for larger models. The same change gave ViT-B 82.2 to 82.6.
On Swin-S the proposed normalization reached 83.6, above 83.2 for pre-norm, 82.6 for sandwich and 83.3 for post-norm. Post-norm diverged at the default learning rate, so it was run at one quarter of that rate. On Swin-B the proposed normalization reached 84.1, compared with 83.6 for both pre-norm and post-norm. The authors attribute sandwich's lower score to lost expressiveness from too many normalization layers.
The authors report that on a huge model self-supervised pre-training diverges with the original Swin Transformer but trains with Swin Transformer V2. In the original architecture the activation values at deeper layers at large size were almost exploded, while the new version shows much milder behavior.
Position bias approaches under window transfer
Linear-spaced CPB scored 77.6 at W24 I768 without fine-tuning, so the log-spaced version is slightly better than the linear-spaced one.
With fine-tuning at W24 I768 the three approaches reached 83.2, 83.7 and 84.2 for parameterized, linear-spaced and log-spaced, and the gap between the two CPB variants stays small. The authors also observe that the log-spaced approach helps more as the resolution change grows, and that a window of 12 without fine-tuning can score 0.4 higher than the original accuracy, which they read as a sign that test-time window adjustment can help.
ImageNet-1K results for SwinV2-G
SwinV2-G reached 84.00 top-1 accuracy on ImageNet-V2, above 83.33 for ViT-G, which the authors describe as the largest dense vision model presented.
On ImageNet-1K V1 SwinV2-G reached 90.17, below 90.88 for CoAtNet-7. The authors suggest the gap may come from different degrees of dataset overtuning, and they note fewer training iterations and lower image resolutions than in earlier efforts. The compared models differ in pre-training data and size, with CoAtNet-7 pre-trained on JFT-3B and SwinV2-G on ImageNet-22K-ext, so the table does not isolate architecture effects.
COCO object detection results
The detector is HTC++ with the mask branch, using a 1536×1536 input and 32×32 or 48×48 windows, with multi-scale testing for the headline number. On COCO test-dev SwinV2-G reached 63.1 box AP and 54.4 mask AP, above the 61.3 and 53.0 reported for SoftTeacher, which was trained at 1280 with a window of 12.
On mini-val, single-scale testing at 1100 with a window of 32 gave 61.7 box AP and 53.3 mask AP, and a window of 48 gave 61.9 and 53.4. The authors attribute the test-time benefit of a larger window to the log-spaced CPB approach.
ADE20K semantic segmentation results
SwinV2-G with UperNet trained at 640 with a window of 40 reached 59.9 mIoU on the ADE20K validation set with multi-scale testing, against 58.4 for BEiT.
Kinetics-400 video action recognition
The model is fine-tuned in two stages, first at 256×256×8 with a 16×16×8 window and then at 320×320×8 with a 20×20×8 window. Under 4×5 views and a 384 test input SwinV2-G reached 86.8 top-1, and under a single 1×1 view it reached 83.4, so the multi-view protocol accounts for most of the number.
Limitations
The gain from SwinV2 over SwinV1 is +0.8% for base and +0.4% for large, so it shrinks with size. The authors suggest that larger sizes may need more labelled data, stronger regularization or better self-supervised methods. SwinV2-G does not exceed CoAtNet-7 on ImageNet-1K V1, and its comparisons with other models do not use matched pre-training data.
Activation check-pointing cuts memory but can make training up to 30% slower, and the paper gives no cost figures for the other memory measures. The window-size adjustment gain at test time is small, at +0.2 on ADE20K and +0.2% on Kinetics-400.
Ablations on window transfer use Swin-T for the main comparison and a separate table for SwinV2-S and SwinV2-B, and the paper does not test window transfer beyond the sizes reported.
Paper info
- Authors
- Ze Liu, Han Hu, Yutong Lin, Zhuliang Yao, Zhenda Xie, Yixuan Wei, Jia Ning, Yue Cao, Zheng Zhang, Li Dong, Furu Wei, Baining Guo
- Venue
- arXiv · Preprint
- First published
- 2021-11-18
- Added
- 2026-11-12
- Updated
- 2022-4-11
- Version
- v2
- arXiv
- 2111.09883
- Category
- cs.CV
- 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
Ze Liu, Han Hu, Yutong Lin, Zhuliang Yao, Zhenda Xie, Yixuan Wei, Jia Ning, Yue Cao, Zheng Zhang, Li Dong, Furu Wei, Baining Guo (2021). Swin Transformer V2: Scaling Up Capacity and Resolution. arXiv:2111.09883v2. https://arxiv.org/abs/2111.09883v2
BibTeX
@misc{liu2021swin,
title = {{Swin Transformer V2: Scaling Up Capacity and Resolution}},
author = {Ze Liu and Han Hu and Yutong Lin and Zhuliang Yao and Zhenda Xie and Yixuan Wei and Jia Ning and Yue Cao and Zheng Zhang and Li Dong and Furu Wei and Baining Guo},
year = {2021},
eprint = {2111.09883},
archivePrefix = {arXiv},
url = {https://arxiv.org/abs/2111.09883v2}
}