wikillm

Paper · 2021-11-18 · cs.CV

Swin Transformer V2: Scaling Up Capacity and Resolution

Scales the Swin vision transformer to 3 billion parameters and 1,536-pixel inputs. Residual post-norm with scaled cosine attention keeps training stable; a log-spaced continuous position bias transfers attention biases across window sizes; SimMIM pretraining cuts the labelled data needed. Author-reported: 84.0% ImageNet-V2 top-1, 63.1 COCO test-dev box AP, 59.9 ADE20K mIoU.

Vision Transformer (ViT) backbonesRelative positional encodingMasked image modelingSelf-attentionvision-transformerscalingtraining-stabilityposition-biasself-supervised-learning

arXivPDFCodeMarkdown14 min read

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

In plain words

Large language models got much bigger and kept improving, but image models lagged behind. This paper takes the Swin Transformer, a vision model that looks at an image in local windows, and makes it much larger: about 3 billion parameters, able to read images up to 1,536 by 1,536 pixels.

Scaling it up exposed three problems, and the paper fixes each one. First, the bigger model's internal values grew unevenly from layer to layer until training became unstable. A small change to where the normalisation layers sit, plus a different way of computing attention scores, calms this down. Second, a model trained on small images needs its position information stretched when it sees larger windows. Instead of stretching a lookup table by interpolation, the authors learn a small network that produces position biases for any offset, computed on a logarithmic scale so the stretch stays modest. Third, labelled images are scarce at this scale, so the model is first pretrained without labels by hiding parts of each image and predicting them.

The result is a model that sets new records on four vision benchmarks: image classification, object detection, semantic segmentation and video action recognition. The authors also point out that they used far fewer labelled images than the large Google vision model they compare against.

TL;DR

  • Swin Transformer V2 is a scaled, more stable version of the Swin vision transformer, evaluated up to 3B parameters and 1,536 px inputs §3.5 §4.2.
  • Three changes: residual post-norm plus scaled cosine attention for stability §3.2; a log-spaced continuous position bias for transferring between window sizes §3.3; SimMIM self-supervised pretraining to reduce labelled data §3.4.
  • Author-reported headline results for the 3B model are 84.0% ImageNet-V2 top-1, 63.1 COCO test-dev box AP, 59.9 ADE20K val mIoU and 86.8% Kinetics-400 top-1 tab. 2 tab. 3 tab. 4 tab. 5.
  • Relevant if you need a dense vision backbone that transfers across input resolutions, or are scaling a vision transformer and see training divergence.
  • The official code is MIT-licensed, but the 3B model's configs and its 70M-image pretraining set are not in the repository or public .

Problem

Language models had grown more than a thousandfold in a few years, while the largest vision models were about 1 to 2 billion parameters and were used only for image classification §1. Three obstacles block scaling a Swin-style vision backbone: (1) training becomes unstable as the model grows, and the largest model diverges §3.1 fig. 3; (2) a model pretrained at low resolution transfers poorly to larger windows when position biases are interpolated bi-cubically §3.1 tab. 1; (3) large models need very large labelled datasets, such as JFT-3B, which the authors avoid §1 §3.4. High-resolution fine-tuning also exhausts GPU memory unless the training implementation changes §3.5.

Method

Swin Transformer V2 keeps the original Swin stage, block and channel layout §3.6. Three architecture changes are made fig. 1, and one change is made to pretraining (item 4).

1. Residual post-norm §3.2. The original Swin block applies layer norm before each sub-layer (pre-norm). The authors move the layer norm to the output of each residual branch, before the branch is added to the main path. This keeps the main path's amplitude from growing with depth §3.2 fig. 2. In the largest models an extra layer norm is placed on the main branch every six blocks §3.2 §3.6.

2. Scaled cosine attention §3.2 eq. 2. The dot product of queries and keys is replaced by a cosine similarity divided by a learnable scalar τ. τ is separate for each head and layer, and the authors state it is kept above 0.01 §3.2. Position bias is added to the scaled cosine. The original attention with a parameterised bias is eq. 1.

3. Log-spaced continuous position bias (Log-CPB) §3.3 eq. 3 eq. 4. The learned bias table of Swin V1 is replaced by a small meta network G, a two-layer MLP with a ReLU in between by default §3.3. G takes the relative offset (Δx, Δy) between two positions and returns the bias for every head. Before G sees the offset, each coordinate is mapped to sign(x) · log(1 + |x|) eq. 4. Because G accepts any offset, a pretrained model can be run with a new window size without retraining §3.3. For inference, the biases can be computed once per relative position and stored as ordinary parameters §3.3. The authors give a worked case: moving from an 8×8 to a 16×16 window, the raw offset range extrapolates by a ratio of 8/7 ≈ 1.14, while the log-spaced range goes from ±2.079 to ±2.773, a ratio of 0.33 of the original range §3.3.

4. Self-supervised pretraining with SimMIM §3.4. The 3B model is first pretrained with SimMIM §3.4 (see 2111.09886) on the ImageNet-22K-ext set of about 70 million images, then pretrained again with classification labels on the same images §4.2. The authors report that this uses 1/40 of the labelled images used by JFT-3B §3.4.

Memory measures §3.5. (a) ZeRO stage-1 optimizer state sharding, via DeepSpeed; the authors report that a 3B model with AdamW and fp32 states needs 48 GB without it §3.5. (b) Activation checkpointing; the authors report up to 30% slower training §3.5. (c) Sequential instead of batched self-attention in the first two stages, for 1,536 px inputs with 32×32 windows on 40 GB A100s §3.5.

Model sizes §3.6. All sizes use four stages.

  • SwinV2-T: C = 96, blocks {2, 2, 6, 2}.
  • SwinV2-S, -B, -L: C = 96 / 128 / 192, blocks {2, 2, 18, 2}.
  • SwinV2-H: C = 352, blocks {2, 2, 18, 2}; 658 million parameters.
  • SwinV2-G: C = 512, blocks {2, 2, 42, 4}; 3 billion parameters. C is the channel count of the first stage §3.6.

Core equations, as written in the paper:

\text{Attention}(Q, K, V) = \text{SoftMax}(QK^T/\sqrt{d}+B)V eq. 1

\text{Sim}(\mathbf{q}_i, \mathbf{k}_j) = \cos(\mathbf{q}_i, \mathbf{k}_j) / \tau + B_{ij} eq. 2

B(\Delta x, \Delta y) = \mathcal{G}(\Delta x, \Delta y) eq. 3

\widehat{\Delta x} = \text{sign}(x) \cdot \log(1+|\Delta x|), \quad \widehat{\Delta y} = \text{sign}(y) \cdot \log(1+|\Delta y|) eq. 4

Block-level sketch, following the description above. Window partitioning is Swin V1's §3.1; the code's shifted-window handling is not shown.

Pseudocode
# Residual post-norm Swin V2 block  [§3.2] [fig. 1]
# Input:  features (B, H*W, C); window partitioning as in Swin V1  [§3.1]
# Output: features (B, H*W, C)

def swin_v2_block(features):
    # Attention branch: normalise its output before the residual add  [§3.2]
    attended = window_attention(features)
    features = features + norm_1(attended)
    # MLP branch with the same post-norm placement  [§3.2]
    features = features + norm_2(mlp(features))
    return features
Pseudocode
# Scaled cosine attention with continuous bias  [eq. 2] [eq. 3]
# Input:  queries, keys, values (N, d) per head, N = Wh*Ww
# Input:  tau (learnable scalar per head); bias (N, N) per head
# Output: (N, d)

def window_attention(queries, keys, values, tau, bias):
    # Unit-normalise queries and keys, so their product is a cosine  [eq. 2]
    queries = normalize(queries)  # (N, d)
    keys = normalize(keys)        # (N, d)
    # Divide by tau, which is kept at or above 0.01  [§3.2] [eq. 2]
    tau = max(tau, 0.01)
    logits = (queries @ keys.T) / tau  # (N, N)
    # Add the position bias for every pair of positions  [eq. 2]
    logits = logits + bias  # (N, N)
    weights = softmax(logits, axis=-1)
    return weights @ values
Pseudocode
# Log-spaced continuous position bias  [eq. 3] [eq. 4]
# Input:  offset (dx, dy) for one pair of positions in a window
# Output: one bias value per head

def continuous_bias(offset, meta_network):
    # Map each coordinate to log space, keeping its sign  [eq. 4]
    log_offset = sign(offset) * log(1 + abs(offset))  # (2,)
    # Two-layer MLP with ReLU in between, the default G  [§3.3]
    return meta_network(log_offset)

Hyperparameters

Values are as written in the paper unless the Where column names the code. "Not stated" means the paper does not give it. Learning rates in the code are per 512 samples and are scaled by batch size main.py. The code values shown are converted to the paper's batch size.

Ablation pretraining (ImageNet-1K, 256², window 8²) app. A1

NameValueWhereNotes
optimizerAdamWapp. A1
epochs300app. A1code default config.py
schedulecosine decay, 20 warm-up epochsapp. A1code default config.py
batch size1024app. A1
peak learning rate1e-3app. A1code base 5e-4 per 512 samples config.py
weight decay0.05app. A1config.py
gradient clip, max norm5.0app. A1config.py
stochastic depth0.2 tiny, 0.3 small, 0.5 baseapp. A1base config 0.5 swinv2_base_patch4_window8_256.yaml
augmentationRandAugment, Mixup, CutMix, random erasingapp. A1code: erasing 0.25 config.py; Mixup 0.8 config.py; CutMix 1.0 config.py. Alpha values not stated in paper
window size8×8 (even values preferred)app. A1 fn.odd sizes also work, per footnote

Fine-tuning for ablations app. A1

NameValueWhereNotes
ImageNet-1K fine-tune30 epochs, AdamW, cosineapp. A1applied only when the test resolution is larger
ImageNet-1K LR4e-5app. A1
ImageNet-1K weight decay1e-8app. A1
COCO detectorCascade Mask R-CNN in mmdetection; window 16²app. A1
COCO multi-scaleshorter side 480–800, longer side 1333app. A1
COCO optimizerAdamW, LR 1e-4, weight decay 0.05, batch 16, 3× scheduleapp. A1
ADE20K input512², window 16²app. A1
ADE20K optimizerAdamW, LR 4e-5, weight decay 0.05, batch 16, 160K iterationsapp. A1linear decay; 1,500-iteration warm-up
ADE20K regularizationstochastic depth 0.3; layer-wise LR decay 0.95app. A1

SwinV2-B and SwinV2-L, ImageNet-22K pretraining app. A2

NameValueWhereNotes
input size, window192², 12²app. A2
epochs90, cosine, 5 warm-up epochsapp. A2swinv2_base_patch4_window12_192_22k.yaml
batch size4096app. A2
peak learning rate1e-3app. A2code 1.25e-4 per 512 samples, equal to 1e-3 at 4096 swinv2_base_patch4_window12_192_22k.yaml
weight decay0.1app. A2swinv2_base_patch4_window12_192_22k.yaml
gradient clip, max norm5.0app. A2
stochastic depth0.2app. A2swinv2_base_patch4_window12_192_22k.yaml
augmentationRandAugment, Mixup, CutMix, random erasingapp. A2

SwinV2-B and SwinV2-L, ImageNet-1K fine-tuning app. A2

NameValueWhereNotes
input sizes256² and 384²app. A2
epochs, batch size30 epochs, batch 1024, cosineapp. A2
learning rate4e-5app. A2code 2e-5 per 512 samples swinv2_base_patch4_window12to16_192to256_22kto1k_ft.yaml
weight decay1e-8app. A2swinv2_base_patch4_window12to16_192to256_22kto1k_ft.yaml
warm-upNot statedapp. A2code uses 5 epochs swinv2_base_patch4_window12to16_192to256_22kto1k_ft.yaml

SwinV2-B and SwinV2-L, downstream tasks app. A2

NameValueWhereNotes
COCO (HTC++) input, window1536², 32²app. A2
COCO optimizerAdamW, LR 4e-4, weight decay 0.05, batch 64, 3× scheduleapp. A2backbone LR is 0.1× the head LR
COCO augmentationInstaboost; multi-scale training; random scale in [0.1, 2.0]app. A2soft-NMS at test
ADE20K input, window640², 40²app. A2
ADE20K optimizerAdamW, LR 6e-5, weight decay 0.05, batch 64, 40K iterationsapp. A2linear decay; 375-iteration warm-up

SwinV2-G (3B) app. A2

NameValueWhereNotes
stage 1, self-supervised20 epochs, input 192², batch 9216app. A2cited there as an anonymous key; main text says SimMIM §3.4
stage 1 optimizerAdamW, cosine, 30,000 warm-up steps, LR 1.4e-3, weight decay 0.1, clip 100app. A2
stage 1 augmentationrandom resized crop (scale [0.67, 1], ratio [3/4, 4/3]), flip, colour normalisationapp. A2
stage 2, supervised30 epochs, batch 9216, LR 1.4e-3, layer-wise LR decay 0.87, weight decay 0.1app. A220,000 warm-up steps; stochastic depth 0.3
stage 2 augmentationRandAugment, random erasingapp. A2
Objects365 V2 stageinput [800, 1024], window 32², 67,500 steps, batch 96app. A2AdamW, LR 1.2e-3, weight decay 0.05
ImageNet-1K fine-tuneinput 640², 10 epochs, batch 576app. A2AdamW, LR 2.1e-5, weight decay 0.1, clip 100, 2 warm-up epochs, stochastic depth 0.5
COCO fine-tuneinput 1536², window 32², 45,000 steps, batch 96app. A2AdamW, LR 6e-4, weight decay 0.05; test windows 32² and 48²
ADE20K fine-tuneinput 640², window 40², 80K iterations, batch 32app. A2AdamW, LR 4e-5, weight decay 0.05, 750 warm-up iterations, stochastic depth 0.4
Kinetics-400 stage 1256²×8 frames, window 16²×8, 20 epochs, batch 80app. A2AdamW, LR 3.6e-4, weight decay 0.1, 2.5 warm-up epochs
Kinetics-400 stage 2320²×8 frames, window 20²×8, 5 epochs, batch 64app. A2AdamW, LR 5e-5, weight decay 0.1, 1 warm-up epoch

Architecture

NameValueWhereNotes
Log-CPB meta network G2-layer MLP, ReLU in between§3.3hidden width not stated; code uses 512 swin_transformer_v2.py
τlearnable per head and layer; kept above 0.01§3.2initial value not stated; code initialises the logit scale to log(10), so τ = 0.1 swin_transformer_v2.py
attention heads per sizeNot stated§3.6base config uses 4, 8, 16, 32 with C = 128 swinv2_base_patch4_window12_192_22k.yaml
extra layer norm on main pathevery 6 blocks, H and G only§3.2 §3.6not present in the repository

Results

Author-reported. The setups differ across rows, and the authors state they did not align model complexity or pretraining data across comparisons §4.2.

Headline results (SwinV2-G, 3B)

SettingMetricBaselineThis paperSource
ImageNet-1K V2, pretrain 192², fine-tune 640², IN-22K-ext 70Mtop-1ViT-G 83.33 (JFT-3B)84.00tab. 2 §4.2
ImageNet-1K V1, same modeltop-1CoAtNet-7 90.8890.17 (marginally lower)tab. 2 §4.2
COCO test-dev, HTC++, multi-scale test, window 48²box / mask APSoftTeacher 61.3 / 53.063.1 / 54.4tab. 3 §4.2
COCO mini-val, same settingbox / mask APSoftTeacher 60.7 / 52.562.5 / 53.7tab. 3
ADE20K val, UperNet, multi-scale, window 56² testmIoUBEiT 58.459.9tab. 4 §4.2
Kinetics-400, Video-SwinV2-G, 4×5 viewstop-1TokenLearner 85.4 (4×3 views)86.8tab. 5 §4.2

SwinV2-B and SwinV2-L against Swin V1 (ImageNet-22K pretraining, ImageNet-1K)

SettingMetricBaselineThis paperSource
SwinV2-B, fine-tune 384²V1 / V2 top-1SwinV1-B 86.4 / 76.5887.1 / 78.08tab. 2
SwinV2-L, fine-tune 384²V1 / V2 top-1SwinV1-L 87.3 / 77.4687.7 / 78.31tab. 2

Ablations (ImageNet-1K pretraining unless stated)

SettingMetricBaselineThis paperSource
res-post-norm + scaled cosine, Swin-T / -S / -Btop-181.5 / 83.2 / 83.681.7 / 83.6 / 84.1tab. 6
ViT-B with the same two changestop-182.282.6tab. 6
normalisation variants on Swin-Stop-1pre-norm 83.2; sandwich 82.6; post-norm 83.3 (1/4 LR)83.6tab. 7
position bias, Swin-T, window 24², without fine-tunetop-1parameterised 68.7log-spaced CPB 79.1tab. 1 §4.3
position bias, Swin-T, window 24², with fine-tunetop-1parameterised 83.2log-spaced CPB 84.2tab. 1
position bias, Swin-T, COCO window 32²box APparameterised 50.9log-spaced CPB 51.8tab. 1
position bias, Swin-T, ADE20K window 32²mIoUparameterised 44.5log-spaced CPB 47.8tab. 1
Log-CPB on SwinV2-B, window 16², input 512² (without / with fine-tune)top-181.0 / 85.3 (no Log-CPB)83.8 / 85.6tab. 8

Paper vs code

Code paths are relative to the repository root at code_commit. The repository is the one the paper's abstract links [abstract] and is MIT-licensed LICENSE.

  • CPB coordinate transform. Paper: sign(x) · log(1 + |x|) on raw offsets eq. 4. Code: offsets are divided by (pretrained window − 1) when a pretrained window is set, else by (window − 1); then multiplied by 8; then sign(x) · log₂(1 + |x|) / log₂(8) swin_transformer_v2.py. The worked example in §3.3 (±2.079 and ±2.773) follows the paper's formula, not the code.
  • CPB output bounded. Paper: B = G(Δx, Δy) with no bound eq. 3. Code: 16 · sigmoid(G) swin_transformer_v2.py, so every bias lies in (0, 16).
  • Bias recomputed per forward pass. Paper: the biases can be precomputed and stored for inference §3.3. Code: the table is passed through the MLP on each forward call swin_transformer_v2.py. Precomputation is not in this file.
  • Residual gain initialisation. Not in the paper. Code sets the weight and bias of norm1 and norm2 in every block to zero at model init swin_transformer_v2.py, called from the model constructor swin_transformer_v2.py. Each residual branch therefore outputs zero at initialisation.
  • Label smoothing. The config default is 0.1 config.py, but it applies only when Mixup is off main.py. The repository's SwinV2 configs keep Mixup on config.py, so label smoothing is not used there. The paper does not mention label smoothing.
  • Learning-rate convention. Paper: absolute learning rates app. A1 app. A2. Code: per 512 samples, multiplied by batch size and world size over 512 main.py. The values agree at the paper's batch sizes: 1.25e-4 × 4096 / 512 = 1e-3, 5e-4 × 1024 / 512 = 1e-3, and 2e-5 × 1024 / 512 = 4e-5.
  • Attention heads. Not stated in the paper. Base config: 4, 8, 16, 32 swinv2_base_patch4_window12_192_22k.yaml.
  • Not in the repository. (a) ZeRO stage-1 via DeepSpeed (no DeepSpeed config found). (b) Sequential self-attention for the first two stages. (c) The extra layer norm every six blocks for H and G. (d) Configs for SwinV2-H and SwinV2-G, since configs/swinv2/ has tiny, small, base and large only. (e) The 70M-image ImageNet-22K-ext pretraining set, which the paper describes as privately collected §4.1. Activation checkpointing is present as a flag main.py and in the block module swin_transformer_v2.py.
  • SimMIM details. The repository includes SimMIM configs for Swin V2 base simmim_pretrain__swinv2_base__img192_window12__800ep.yaml. Its 800-epoch schedule is not the 20-epoch stage used for SwinV2-G app. A2.

Pitfalls

  • Text and table disagree on Log-CPB at window 24². The text gives 78.9% without fine-tuning §4.3; Table 1 gives 79.1% tab. 1. Use the table.
  • SwinV2-B gain over V1. The text gives +0.8% for ImageNet-1K V1 §4.2; Table 2 gives 87.1 − 86.4 = +0.7 tab. 2.
  • SwinV2-G pretraining is not reproducible from this release. The 70M-image set is private, the G configs are absent, and the appendix cites the self-supervised method with an anonymous key app. A2. The main text names SimMIM §3.4. Treat the G pretraining recipe as "SimMIM, details not stated here".
  • Unstated values that change results. Attention head counts per size §3.6; CPB hidden width §3.3; τ initialisation §3.2; warm-up for ImageNet-1K fine-tuning app. A2; Mixup and CutMix alphas app. A1.
  • Baseline stability. Post-norm diverges at the default learning rate; the paper uses one quarter of the default rate for that baseline tab. 7 caption.
  • Window divisibility. Even window sizes keep the shift offset divisible by the window size app. A1 fn.. Odd sizes such as 7 also work, per the same footnote.
  • Test-time window changes. The authors report +0.2 gains on ADE20K and Kinetics-400 from a larger test window §4.2, and a further gain on COCO from a different test window §4.2 tab. 3. They attribute these to Log-CPB; no control run isolates it.
  • Memory and compute are the hidden costs. Without ZeRO, a 3B model with AdamW and fp32 states needs 48 GB for states alone §3.5. Activation checkpointing can slow training by up to 30% §3.5. The G-scale pretraining cost is an estimate of "< 0.5k" TPUv3 core-days tab. 2 fn..
  • Comparisons are not controlled. The authors did not align complexity or pretraining data §4.2. Their ImageNet-1K V1 result is below CoAtNet-7 tab. 2.

Lineage

  • Builds on: Swin Transformer arXiv:2103.14030 (no card here). V2 keeps its stage and block layout and changes the normalisation, attention scores and position bias §3.2 §3.3 §3.6.
  • Builds on: SimMIM 2111.09886 for self-supervised pretraining §3.4.
  • Related: continuous convolution work (SchNet, continuous convolution networks, relation-based continuous convolution) that uses a meta network on irregular offsets §2.
  • Compared against: ViT-G, V-MoE and CoAtNet-7 (all JFT-3B) for ImageNet tab. 2; CopyPaste, SwinV1-L, YOLOR, CBNet, DyHead and SoftTeacher for COCO tab. 3; SwinV1-L, Focal-L, CSwin-L, MaskFormer, FaPN and BEiT for ADE20K tab. 4; ViViT, video SwinV1-L and TokenLearner for Kinetics-400 tab. 5.
  • Superseded by: not known from wiki content.

Source and license

Adapted from “Swin Transformer V2: Scaling Up Capacity and Resolution” by Ze Liu, Han Hu, Yutong Lin, Zhuliang Yao, Zhenda Xie, Yixuan Wei, Jia Ning, Yue Cao, Zheng Zhang, Li Dong, Furu Wei, Baining Guo (arXiv:2111.09883), 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/Swin-Transformer at commit f82860bfb522 (MIT). Any quoted code is shown under that license.