VerifiedarXiv:2103.1403028 min
Vision · Backbone architecture

Swin Transformer: Hierarchical Vision Transformer using Shifted Windows

A Transformer for images that attends only within small windows, shifts those windows every other layer so information crosses them, and merges patches into a pyramid of feature maps like a CNN's.

Swin computes self-attention inside non-overlapping 7 × 7 windows of patches, so its cost grows linearly with the number of pixels instead of quadratically. Alternate layers move the window grid by 3 patches, which connects neighbouring windows, and a cyclic shift with a mask keeps that move as cheap as the unshifted layer. Between stages, groups of 2 × 2 patches merge into one, giving feature maps at 1/4, 1/8, 1/16 and 1/32 of the image resolution.

Explaining the paperSwin Transformer: Hierarchical Vision Transformer using Shifted WindowsLiu, Lin, Cao, Hu, Wei, Zhang, Lin, Guo · ICCV 2021 (Marr Prize) · Microsoft Research Asia · arXiv:2103.14030 ↗

In early 2021 the Vision Transformer classified images well, but object detectors and segmentation networks still ran on ResNets. They need feature maps at several resolutions, and on a detection-sized image with fine patches, global attention would cost over a hundred times as much as the rest of each layer.

This paper, from Microsoft Research Asia in March 2021, builds a Transformer that slots into those systems in place of a ResNet. On COCO object detection its largest model reached 58.7 box AP and 51.1 mask AP on test-dev, and on ADE20K semantic segmentation 53.5 mIoU, both above every published result at the time; on ImageNet classification it reached 87.3% top-1 after pre-training on ImageNet-22K. The code and weights are public.

A few ideas explain it: why ViT does not work as a detection backbone; what restricting attention to fixed windows does to the cost, from the paper's equations (1) and (2); how shifting the windows in alternate layers lets information cross window borders; how a cyclic shift and an attention mask compute the shifted layer without extra windows; the relative position bias; and how patch merging builds the four-stage pyramid. Training recipes, results and ablations follow.

What ViT lacks as a detection backbone

The Vision Transformer (ViT, Dosovitskiy et al., 2020) cuts a 224 × 224 image into 16 × 16-pixel patches, which gives a 14 × 14 grid of 196 tokens, embeds each patch as a 768-dimensional vector, adds a learned position embedding, and runs a standard Transformer encoder. In every layer every token attends to every other token: for each pair it computes a dot product between a query vector and a key vector, turns each query's row of scores into weights with a softmax, and returns the weighted average of value vectors. All 12 layers of ViT-B/16 work on the same 14 × 14 grid.

Detection and segmentation backbones have a different shape. A ResNet produces four feature maps, at strides 4, 8, 16 and 32 (one feature vector per 4 × 4, 8 × 8, 16 × 16 and 32 × 32 pixels). Systems such as the feature pyramid network (FPN) and Mask R-CNN read small objects from the fine maps and large objects from the coarse ones, and segmentation decoders such as UperNet or a U-Net combine all four to label each pixel. ViT has one map, at stride 16, so earlier attempts to use it for detection added deconvolution layers to produce the other scales, which made them slower and less accurate than a backbone that produces several scales itself (DeiT-S this way: 48.0 box AP at 10.4 FPS, against 50.5 at 15.3 for Swin-T, below).

The second problem is cost. Global attention compares every pair of tokens, so its cost grows with the square of the token count. A stride-4 map of a 224 × 224 image already has 56 × 56 = 3,136 tokens, and the attention matrix of one head in one layer has 3,136² ≈ 9.8 million entries. A COCO image is resized to about 800 × 1333 pixels, which at stride 4 is 200 × 333 = 66,600 tokens and 4.4 billion entries per head per layer. Swin answers both problems with one design: attention restricted to small windows, plus a merging step that halves the grid between stages.

Attention inside fixed windows, and what it costs

Swin partitions the token map into non-overlapping windows of M×MM \times M tokens, M=7M = 7 by default, and computes self-attention only among the 49 tokens of each window. The windows tile the map evenly, starting from the top-left corner: a 56 × 56 map becomes an 8 × 8 grid of 64 windows. Each window is an independent attention problem of the same size, so all 64 run as one batched matrix multiply.

The paper counts the cost of one multi-head attention module on an h×wh \times w map of tokens with CC channels, in multiply-adds and ignoring the softmax. For global attention (MSA) and window attention (W-MSA):

Ω(MSA)=4hwC2+2(hw)2C\Omega(\text{MSA}) = 4hwC^2 + 2(hw)^2C
(1)
Ω(W-MSA)=4hwC2+2M2hwC\Omega(\text{W-MSA}) = 4hwC^2 + 2M^2hwC
(2)

The first term is the same in both. Each of the hwhw tokens goes through four C×CC \times C linear maps, the query, key and value projections and the output projection, at C2C^2 multiply-adds each. The second term is the attention itself. Global attention computes (hw)2(hw)^2 query-key dot products of length CC (summed over the heads, the head dimensions add up to CC) and then the same number of weight-times-value products, which gives 2(hw)2C2(hw)^2C. In window attention each of the hw/M2hw/M^2 windows does that computation on M2M^2 tokens, costing 2(M2)2C2(M^2)^2C; multiplied by the number of windows that is 2M2hwC2M^2hwC. With MM fixed, every term of (2) is proportional to the token count hwhw, which is what the paper means by linear complexity.

Take Swin-T's first stage at 224 × 224: h=w=56h = w = 56, C=96C = 96, M=7M = 7. The projections cost 4 · 3,136 · 96² = 0.116 billion multiply-adds. Global attention adds 2 · 3,136² · 96 = 1.888 billion, for 2.004 G in total. Window attention adds 2 · 49 · 3,136 · 96 = 0.030 billion, for 0.145 G. The attention term shrinks by a factor of hw/M2hw/M^2 = 3,136 / 49 = 64, and the whole module by 13.8×. On the 200 × 333 map of a COCO image the same layer costs 854 G globally against 3.08 G with windows, a factor of 277.

Figure 1 · the cost of one attention module, equations (1) and (2)
224 px
7 x 7
Drag the image side and the window size. Both axes are logarithmic, so the global curve rises with slope 2 in the token count and the window curve with slope 1. A window as large as the map is clipped to it, and the two curves meet.

Two settings of the window slider show the trade. At M=1M = 1 each token attends only to itself and the attention term nearly vanishes, but the layer no longer mixes information between positions at all. At M=56M = 56 on a 224 × 224 image the window is the whole map and (2) equals (1). Swin's M=7M = 7 gives every token 49 positions of context, the extent of a 7 × 7 convolution, at a cost that stays a fixed multiple of the projection cost however large the image gets.

The obvious way to restrict attention to a neighbourhood is a sliding window: every token attends to the 7 × 7 tokens centred on itself, as in stand-alone self-attention (Ramachandran et al., 2019) and local relation networks (Hu et al., 2019), two earlier attempts to replace convolutions in a ResNet. The multiply-add count is about the same as (2). The problem is speed: neighbouring queries have different key sets, so there is no single key matrix to multiply against, and the gather of each query's neighbours makes memory access the bottleneck. In Swin's partition all 49 queries of a window share the same 49 keys, which is one dense matrix product per window. The ablations put a number on the difference in Table 5, below.

How Swin Transformer's shifted windows work

Windows that never change would split the image into islands. A token in one window would never exchange information with a token one position away across a window border, however many layers ran, until the next patch merging. Swin alternates between two partitions in consecutive blocks. The first block uses the regular partition from the top-left corner. The second moves the window grid by (⌊M/2⌋,⌊M/2⌋)(\lfloor M/2 \rfloor, \lfloor M/2 \rfloor) tokens, 3 for M=7M = 7, down and to the right, so each new window straddles parts of four old ones.

Apart from the attention module, a Swin block is a standard pre-norm Transformer block: LayerNorm (see the Layer Normalization explainer), attention, a residual connection, then LayerNorm, a two-layer MLP that widens to 4C4C with a GELU (a smooth variant of ReLU) between the layers, and another residual. Two consecutive blocks are written

z^l=W-MSA(LN(zl−1))+zl−1zl=MLP(LN(z^l))+z^lz^l+1=SW-MSA(LN(zl))+zlzl+1=MLP(LN(z^l+1))+z^l+1\begin{aligned} \hat{z}^{l} &= \text{W-MSA}(\text{LN}(z^{l-1})) + z^{l-1} \\ z^{l} &= \text{MLP}(\text{LN}(\hat{z}^{l})) + \hat{z}^{l} \\ \hat{z}^{l+1} &= \text{SW-MSA}(\text{LN}(z^{l})) + z^{l} \\ z^{l+1} &= \text{MLP}(\text{LN}(\hat{z}^{l+1})) + \hat{z}^{l+1} \end{aligned}
(3)

where z^l\hat{z}^{l} is the output of the attention module of block ll, zlz^l the output of its MLP, and W-MSA and SW-MSA are window attention with the regular and the shifted partition. Every stage stacks an even number of blocks, so the blocks come in such pairs.

Follow one token through a pair of blocks in Swin-T's first stage. Suppose it sits in the window covering rows and columns 7 to 13 of the 56 × 56 map. After the first block its vector depends on all 49 tokens of that window. The shifted partition has borders at 3, 10, 17 and so on, so the rows 7 to 13 fall into two shifted windows, rows 3 to 9 and 10 to 16, and the same holds for the columns. After the second block the token has mixed, through the first block, with every token in rows and columns 3 to 16: a 14 × 14 square of 196 tokens, which is 56 × 56 pixels of the image. Without the shift it would still be confined to its 7 × 7 window, 28 × 28 pixels.

Figure 2 · how far one patch's information spreads
2 / 7
Click a patch to make it the source (or focus the grid and use the arrow keys), then step through the blocks. A 16 × 16 map with 4 × 4 windows and a shift of 2. Tokens the source has reached are teal, brightest for the block just applied. Switch to "no shifting" to see the reached set stop at the first window.

With shifting, the reached region grows by about half a window on each side per block: from a central patch it covers the 16 × 16 map after five blocks, from a corner patch after seven. Without the shift the count stays at 16. In the real network the growth also comes from patch merging: each stage halves the grid, so a 7 × 7 window covers 28, 56, 112 and finally 224 pixels per side in stages 1 to 4. Removing the shift costs Swin-T 1.1 points of ImageNet top-1 accuracy (80.2% against 81.3%), 2.8 box AP and 2.2 mask AP on COCO, and 2.8 mIoU on ADE20K (Table 4).

Computing the shifted windows in one batch

Shifting the grid by 3 leaves partial windows along the edges. On an h×wh \times w map the regular partition has ⌈h/M⌉×⌈w/M⌉\lceil h/M \rceil \times \lceil w/M \rceil windows and the shifted one has (⌈h/M⌉+1)×(⌈w/M⌉+1)(\lceil h/M \rceil + 1) \times (\lceil w/M \rceil + 1), some of them smaller than M×MM \times M. Padding every small window to full size and masking the padding works, but costs extra windows: in Swin-T's third stage, a 14 × 14 map, it turns 2 × 2 = 4 windows into 3 × 3 = 9, or 2.25 times the attention work.

The paper's alternative is a cyclic shift. Before the shifted block, roll the whole map up and to the left by 3 tokens, so that the top 3 rows reappear at the bottom and the left 3 columns at the right, then cut the rolled map with the regular partition. A batched window in the interior contains exactly one shifted window. A batched window on the bottom or right edge contains pieces that were not adjacent in the original image, for example the bottom of the image next to its top. A mask stops those pieces from attending to each other: every token gets a label for which shifted window it belongs to, and the logit of any query-key pair with different labels gets −100 added before the softmax, which makes its weight effectively zero. After attention, the outputs are rolled back down and to the right by 3. The batched computation has as many windows as the regular partition and gives the same output as attending inside the shifted windows directly; a NumPy reimplementation of the released code matches the direct computation to within 10⁻¹⁵.

Figure 3 · the cyclic shift and the attention mask
Step through the computation on an 8 × 8 map with 4 × 4 windows, the case of the paper's Figure 4. Letters mark the nine shifted windows. In step 3, click a batched window (or use the buttons) to see its 16 × 16 mask: coloured cells are allowed query-key pairs, dark cells get −100.

In step 3 the first batched window holds only window E and its mask is all allowed. The second holds F and D, two pieces of 8 tokens each (4 rows × 2 columns), so 2 · 8² = 128 of its 256 pairs are allowed and the other 128 are masked. The fourth, at the bottom right, holds four pieces (I, G, C and A) and only 64 of its 256 pairs are allowed. On Swin-T's 56 × 56 first-stage map with M=7M = 7 and a shift of 3, 49 of the 64 batched windows contain a single shifted window, 14 along the bottom and right edges contain two pieces, and the corner window contains four, of 16, 12, 12 and 9 tokens.

Masked pairs are still computed and then discarded, so the cyclic shift does a little wasted arithmetic in 15 of the 64 windows, but the batched computation keeps the shape of the unshifted block. On a V100, Table 5 times the shifted attention in stage 1 at 3.0 ms with the cyclic shift against 3.3 ms with padding and 2.8 ms for unshifted windows; for whole networks the cyclic version is 13% faster than padding for Swin-T and 18% for Swin-S and Swin-B. Here is one block in code, with the shapes of Swin-T's first stage:

# one Swin block on a stage-1 map of Swin-T: x is [B, 56, 56, 96], M = 7
def swin_block(x, shift):
    h = ln1(x)
    if shift:                                    # SW-MSA: roll up-left by M // 2
        h = torch.roll(h, shifts=(-3, -3), dims=(1, 2))
    w = partition(h, 7)                          # [B*64, 49, 96]: 64 windows
    q, k, v = split_heads(qkv(w), heads=3)       # each [B*64, 3, 49, 32]
    a = q @ k.transpose(-2, -1) * 32 ** -0.5     # [B*64, 3, 49, 49]
    a = a + table[rel_index].permute(2, 0, 1)    # B: [3, 49, 49] from [169, 3]
    if shift:
        a = a + mask                             # [64, 49, 49], 0 or -100
    o = proj(merge_heads(a.softmax(-1) @ v))     # [B*64, 49, 96]
    o = unpartition(o, 7, 56, 56)                # [B, 56, 56, 96]
    if shift:
        o = torch.roll(o, shifts=(3, 3), dims=(1, 2))
    x = x + o                                    # residual
    return x + mlp(ln2(x))                       # 96 -> 384 -> GELU -> 96

The mask is a fixed buffer, computed from the map size when a shifted block is built. It has one 49 × 49 slice per window, shape [64, 49, 49], and the same slice applies to all three heads. The table[rel_index] line is the relative position bias of the next section.

Relative position bias

Attention by itself ignores where tokens are: permute the tokens of a window and the outputs permute the same way. ViT restores position by adding a learned embedding for each absolute position to the input tokens. Swin instead adds a learned scalar to every attention logit that depends only on the offset between the query and the key:

Attention(Q,K,V)=SoftMax(QKT/d+B)V\text{Attention}(Q,K,V) = \text{SoftMax}(QK^T/\sqrt{d} + B)VAttention(Q,K,V)=SoftMax(QKT/d+B)V\begin{aligned} &\text{Attention}(Q,K,V) \\ &= \text{SoftMax}(QK^T/\sqrt{d} + B)V \end{aligned}
(4)

Here Q,K,VQ, K, V are the M2×dM^2 \times d query, key and value matrices of one head in one window, d=32d = 32 is the per-head dimension in every Swin model, and BB is an M2×M2M^2 \times M^2 matrix of biases, one per query-key pair. Within a window the row offset between two tokens runs from −6 to +6 when M=7M = 7, and so does the column offset, so there are only 13 × 13 = 169 distinct offsets. Each head of each block learns a table B^\hat{B} of 169 numbers, and the 49 × 49 = 2,401 entries of BB are looked up in it by offset. The same table serves every window of the map, so a token pair 2 rows apart gets the same bias in the top-left window as in the bottom-right one.

Figure 4 · one head's bias table and the crop a query reads
Click a patch in the 7 × 7 window to make it the query; hover another (on a touch screen, tap it) to read its bias as a key, and tap it again to make it the query. The amber frame on the 13 × 13 table is the crop this query reads. Table values are illustrative, not learned weights; switch heads to see different patterns.

Moving the query moves the amber frame across the table: a query in the top-left corner reads only offsets pointing down and right, and the centre query reads the middle 7 × 7 block. Teal cells raise a key's logit and amber cells lower it, so a head whose table peaks at offset (0, 0) prefers nearby keys whatever their content. The learned tables are added to QKT/dQK^T/\sqrt{d} before the softmax, so they shift attention toward or away from positions while the dot products still carry the content.

The idea comes from language models. T5 (Raffel et al., 2020) adds a learned scalar per head to each logit, one per bucket of relative distances, shared by all layers. Swin learns a separate table in every block and gives every exact 2D offset its own entry, which is affordable because a 7 × 7 window has only 169 offsets. In Table 4, relative bias beats no position information by 1.2 points of top-1, 1.3 box AP and 2.3 mIoU, and beats ViT-style absolute embeddings by 0.8 top-1, 1.5 box AP and 2.9 mIoU. Absolute embeddings help classification over no position information (80.5% against 80.1%) but hurt detection and segmentation (−0.2 box AP, −0.6 mIoU), and adding them on top of the relative bias lowers ADE20K mIoU from 46.1 to 44.0, so the released model has none. The paper reads this as evidence that some translation invariance, the same treatment of a pattern wherever it appears, still helps dense prediction.

Because the table is indexed by offset, it can be resized. The 384 × 384 models use M=12M = 12, which has 23 × 23 offsets; they start from the 224 × 224 weights and stretch each 13 × 13 table to 23 × 23 by bicubic interpolation before fine-tuning.

Patch merging and the four stages

The first layer cuts the image into 4 × 4-pixel patches, smaller than ViT's 16 × 16, and treats the 4 · 4 · 3 = 48 raw RGB values of each patch as its feature vector. A linear layer maps those 48 numbers to CC channels (96 for Swin-T), and a LayerNorm follows; the released code implements this as a 4 × 4 convolution with stride 4. A 224 × 224 image becomes a 56 × 56 map of 96-dimensional tokens, and the blocks of stage 1 run on it.

Each later stage starts with patch merging. Take every 2 × 2 group of neighbouring tokens, concatenate their four vectors into one of 4C4C channels, normalize it, and project it to 2C2C channels with a linear layer. The grid halves in each direction and the channel count doubles: 56 × 56 × 96 becomes 28 × 28 × 192, then 14 × 14 × 384, then 7 × 7 × 768. This is the same bookkeeping as a strided convolution in a ResNet stage, and the four stage outputs have strides 4, 8, 16 and 32, so a detector built for ResNet's C2 to C5 maps can read them directly. The window size stays 7 × 7 tokens throughout, so the windows cover four times as much image in each stage. The number of heads grows with the channels to keep 32 dimensions per head: 3, 6, 12 and 24 in Swin-T.

Figure 5 · the four stages, with parameter and multiply-add counts
Pick a model and an input size, then click a stage (or use the buttons below the canvas). Every panel covers the same image; ink lines are windows, the amber square is one token. The counts are computed from the released architecture and reproduce Table 1 of the paper.

The four variants differ only in the stage-1 width CC and the depth of stage 3:

ModelCblocks per stageparamsFLOPs (224²)throughputtop-1
Swin-T962, 2, 6, 229M4.5G755 img/s81.3
Swin-S962, 2, 18, 250M8.7G437 img/s83.0
Swin-B1282, 2, 18, 288M15.4G278 img/s83.5
Swin-L1922, 2, 18, 2197M34.5G141 img/s(22K only)
DeiT-S38412, one map22M4.6G940 img/s79.8
DeiT-B76812, one map86M17.5G292 img/s81.8

Parameters, FLOPs, throughput (V100, images per second) and ImageNet-1K top-1 accuracy are from Table 1, except Swin-L at 224², which the paper evaluates only at 384² after ImageNet-22K pre-training; its 224² numbers are from the released model card. The paper's "FLOPs" count multiply-adds, the convention of the released flops() functions, and Figure 5 reproduces them to within 0.1G. Swin-T's 29M is the exception: the architecture has 28.29M parameters. Swin-T and Swin-S sit at the cost of ResNet-50 and ResNet-101, and Swin-B at the cost of ViT-B / DeiT-B. Most of the parameters are in the two widest stages: in Swin-T, counting each stage's patch merging, stage 3 holds 10.96M of the 28.29M and stage 4 holds 15.37M.

At 224 × 224 input, stage 4 is a 7 × 7 map, exactly one window. A shifted partition of that map would cut the one window into four pieces that could not attend to each other, so the released code turns the shift off when the map is no larger than the window, and both blocks of stage 4 run plain global attention over 49 tokens. For classification the 49 output vectors are layer-normalized and averaged into one 768-dimensional vector, which a linear layer maps to the 1,000 ImageNet classes; there is no class token, and the paper reports that average pooling is as accurate as ViT's class token. The whole forward pass, in shapes:

# Swin-T forward pass, 224 x 224 input
x = patch_embed(img)        # [B, 3, 224, 224] -> 4x4 stride-4 conv, LN -> [B, 56, 56, 96]
feats = []
for i, depth in enumerate([2, 2, 6, 2]):
    if i > 0:
        x = patch_merge(x)  # [B, H, W, C] -> [B, H/2, W/2, 4C] -> LN -> linear -> 2C
    for j in range(depth):
        x = swin_block(x, shift=(j % 2 == 1))   # stage 4: 7x7 map, one window, no shift
    feats.append(x)         # 56x56x96, 28x28x192, 14x14x384, 7x7x768
logits = head(ln(x).mean(dim=(1, 2)))   # average over the 49 tokens -> [B, 768] -> 1000
loss = cross_entropy(logits, labels)
loss.backward()   # gradients reach every weight, including each block's bias table;
                  # rel_index and mask are fixed buffers

Training recipes

On ImageNet-1K (1.28 million images, 1,000 classes) every model trains from scratch at 224 × 224 for 300 epochs with AdamW: batch size 1,024, learning rate 0.001 with 20 epochs of linear warm-up and cosine decay, weight decay 0.05, gradient clipping at norm 1. The augmentation and regularization follow DeiT (Touvron et al., 2020), the recipe that first trained ViT well on ImageNet-1K alone: RandAugment, Mixup, CutMix, random erasing, and stochastic depth (randomly skipping whole residual blocks during training) at rate 0.2, 0.3 and 0.5 for Swin-T, S and B. DeiT's repeated augmentation and weight averaging (EMA) did not help Swin and are left out; the paper notes that DeiT needed repeated augmentation to train ViT stably. Models at 384 × 384 are fine-tuned from the 224 × 224 weights for 30 epochs at a constant learning rate of 10⁻⁵.

The larger models also pre-train on ImageNet-22K (14.2 million images, about 22,000 classes) for 90 epochs at batch size 4,096, then fine-tune on ImageNet-1K for 30 epochs. For COCO the backbones go into four detectors from the mmdetection library with one shared recipe: multi-scale training (short side 480 to 800 pixels, long side at most 1,333), AdamW at learning rate 10⁻⁴, weight decay 0.05, batch 16, 36 epochs. ResNet baselines get the same AdamW recipe, which Table 9 shows is at least as good for them as their default SGD except for ResNeXt-101-64x4d. ADE20K (20,000 training images, 150 classes) uses UperNet for 160,000 iterations at batch 16 and 512 × 512 crops, or 640 × 640 for the ImageNet-22K models.

Results

On ImageNet-1K without extra data, Swin-T reaches 81.3% top-1 at 4.5G FLOPs against 79.8% for DeiT-S at 4.6G, and Swin-B 83.5% at 15.4G against DeiT-B's 81.8% at 17.5G. Convolutional networks found by architecture search come closer: RegNetY-8G gets 81.7% at 8.0G and 592 images per second, and EfficientNet-B3 81.6% at 1.8G (with 300 × 300 inputs) and 732 per second, against Swin-T's 755 per second. After ImageNet-22K pre-training, Swin-B at 384² reaches 86.4% and Swin-L 87.3%; ViT-B/16 pre-trained the same way gets 84.0% at nearly the same speed (85.9 against 84.7 images per second) and higher FLOPs (55.4G against 47.0G).

In the COCO comparisons the backbone is swapped with everything else held fixed. Box AP is COCO's average precision of the predicted boxes, averaged over the 80 classes and over overlap thresholds from 0.5 to 0.95 and reported out of 100; mask AP scores the predicted object masks the same way. In Cascade Mask R-CNN, Swin-T scores 50.5 box AP and 43.7 mask AP against 46.3 and 40.1 for ResNet-50, at 86M against 82M parameters and 15.3 against 18.0 frames per second. Across the four detectors Swin-T gains 3.4 to 4.2 box AP over ResNet-50. DeiT-S, given deconvolution layers to produce a pyramid, scores 48.0 box AP and runs at 10.4 frames per second, slower than Swin-T because its global attention is quadratic in the image size.

Cascade Mask R-CNN, COCO valbox APmask APparamsFLOPsFPS
DeiT-S (+ deconv)48.041.480M889G10.4
ResNet-5046.340.182M739G18.0
Swin-T50.543.786M745G15.3
ResNeXt-101-32x4d48.141.6101M819G12.8
Swin-S51.844.7107M838G12.0
ResNeXt-101-64x4d48.341.7140M972G10.4
Swin-B51.945.0145M982G11.6

Swin-B gains 3.6 box AP over ResNeXt-101-64x4d at similar size and speed; for comparison, going from ResNeXt-101-32x4d to the 64x4d model adds 0.2 box AP for 39M more parameters. The system-level entry, Swin-L in an improved Hybrid Task Cascade (HTC++) with ImageNet-22K pre-training, an extra global attention layer after stage 4 and a 72-epoch schedule, scores 57.7 box AP and 50.2 mask AP on test-dev with single-scale testing, and 58.7 and 51.1 with multi-scale testing, against 56.0 box AP for Copy-paste and 48.5 mask AP for DetectoRS. The paper notes that its implementation uses standard PyTorch operations while the ResNets run on heavily optimized cuDNN kernels, so the frame rates understate what Swin could reach.

On ADE20K with UperNet, Swin-S reaches 49.3 mIoU against 44.0 for DeiT-S and 44.9 for ResNet-101 at similar cost, and Swin-L with ImageNet-22K pre-training 53.5, against 50.3 for SETR, a ViT-Large-based segmenter with more parameters (308M against 234M). mIoU is the intersection over union of predicted and true pixels for each class, averaged over the 150 classes, so a gain of 3.2 means the predicted regions overlap the true ones noticeably better on average.

What the ablations show

All ablations use Swin-T. Table 4 isolates the shift and the position scheme on all three tasks:

Swin-T variantImageNet top-1COCO box APmask APADE20K mIoU
no shifting80.247.741.543.3
no position information80.149.242.643.8
absolute position embedding80.549.042.443.2
absolute + relative81.350.243.444.0
relative bias only, no QKᵀ term79.348.241.944.1
shifted windows + relative bias81.350.543.746.1

The "no QKᵀ term" row drops the content part of (4) and attends by position alone, which makes each layer a fixed spatial filter per head, close to a depthwise convolution. It still reaches 79.3%, only two points below the full model, so a fixed positional filter already recovers most of the accuracy; the content-based weights add the last 2.0 points of top-1, 2.3 box AP and 2.0 mIoU.

Table 5 times the attention modules on a V100 and Table 6 compares accuracy, which together test the design argument against sliding windows:

attention methodstage 1 msstage 4 msSwin-T img/stop-1
sliding window, naive122.57.618381.4
sliding window, CUDA kernel7.61.8488
Performer (linear attention)4.81.563879.0
windows, no shift2.80.977080.2
shifted windows, padding3.32.267081.3
shifted windows, cyclic shift3.01.075581.3

Sliding windows reach the same accuracy, 81.4% against 81.3%, but even with a hand-written kernel the attention in stage 1 is 2.5 times slower and the whole Swin-T 1.5 times slower; the naive version is 40.8 times slower in stage 1 and 4.1 times slower overall. Performer, a linear-time approximation of global attention, is slower than shifted windows and 2.3 points less accurate. The cyclic shift costs 0.2 ms over no shift in stage 1 and buys 1.1 points of accuracy.

Appendix A3.3 applies the same hierarchy and shifted windows to MLP-Mixer, an all-MLP model that mixes tokens with a fully connected layer instead of attention. Swin-Mixer-B/D24 reaches 81.3% at 10.4G FLOPs against 76.4% for MLP-Mixer-B/16 at 12.7G, and removing the shift drops it to 80.3%.

Limits and what came next

Swin trades ViT's uniform design for a more complicated one. The window size has to divide the map at every stage or the map is padded, the shift and mask need custom bookkeeping, and changing MM requires resizing every bias table. The paper reports no ablation over the window size or the shift size. Its frame rates trail ResNets in detection (15.3 against 18.0 FPS for the tiny models), which the authors attribute to unoptimized kernels. Long-range interactions arrive slowly. On an 800 × 1333 COCO image the stage-4 map is 25 × 42 tokens, larger than one window, so no stage attends globally. Tracing which tokens can influence which through Swin-T's twelve blocks and three mergings, a token at the left edge never mixes with anything more than about 544 pixels to its right. The HTC++ system appends one global attention layer after stage 4 for its best COCO results.

Swin V2 (Liu et al., 2021) scaled the design to 3 billion parameters and 1,536 × 1,536 inputs, replacing pre-norm with normalization after each residual branch and dot products with cosine similarity for stability, and the bias table with a small network on log-spaced coordinates so window sizes transfer. ConvNeXt (Liu et al., 2022) modernized a ResNet step by step toward Swin's macro design and training recipe, and with convolutions only it outperformed Swin on COCO and ADE20K. Video Swin extended the windows to 3D, and SimMIM pre-trained Swin with masked image modelling, the same idea as MAE. ViTDet (Li et al., 2022) went back to a plain single-scale ViT for detection, with window attention without shifting plus a few cross-window propagation blocks, and built a simple pyramid from its last map.

Provenance Verified against primary literatureHow we verify
Swin Transformer, Liu, Lin, Cao, Hu, Wei, Zhang, Lin, Guo (arXiv 2103.14030)Equations (1) to (4) are numbered as in the paper. Accuracy, AP, mIoU, FLOPs and throughput numbers are from Tables 1 to 6 and 8 as printed; throughput is measured on one V100 GPU.
microsoft/Swin-Transformer, models/swin_transformer.py, WindowAttentionrelative_position_bias_table has shape ((2M − 1)², heads), initialised with trunc_normal std 0.02, one table per block. relative_position_index is a fixed buffer built from coordinate differences query − key, shifted by M − 1 and flattened as (Δy + M − 1)(2M − 1) + (Δx + M − 1). This page indexes the table by key − query, which stores the same values flipped. Logits are q · k scaled by head_dim^−0.5, plus the bias, plus the mask when there is one.
models/swin_transformer.py, SwinTransformerBlockIf min(input_resolution) ≤ window_size, the block sets shift_size = 0 and window_size = min(input_resolution): one window over the whole map. For SW-MSA it labels the map with 9 regions using the slices (0, −M), (−M, −s), (−s, None) per axis, partitions the labels like the features, and adds −100 to every logit whose query and key labels differ. torch.roll by (−s, −s) before attention and by (+s, +s) after.
models/swin_transformer.py, BasicLayer and SwinTransformerBlock i of a stage uses shift_size 0 when i is even and window_size // 2 when i is odd. The released code attaches patch merging at the END of each stage except the last, where the paper describes it as the start of the next stage; the computation is the same. No absolute position embedding (ape=False), LayerNorm after patch embedding (patch_norm=True), final LayerNorm, average pool, linear head.
models/swin_transformer.py, PatchMerging and PatchEmbedMerging concatenates x[0::2, 0::2], x[1::2, 0::2], x[0::2, 1::2], x[1::2, 1::2] to 4C channels, applies LayerNorm(4C), then Linear(4C, 2C, bias=False). Table 7 of the paper lists the linear layer before LN. The patch embedding is Conv2d(3, C, kernel 4, stride 4), which is the paper's linear layer on 48 raw values.
models/swin_transformer.py, flops() methodsThe flops() methods count multiply-adds. Summed over the model they give 4.49G for Swin-T, 8.75G for Swin-S, 15.44G for Swin-B at 224² and 47.1G at 384² with window 12, and 103.95G for Swin-L at 384², matching Table 1. Parameter counts from the same architecture: 28.29M, 49.61M, 87.77M, 196.74M.
Swin-Transformer README and configsSwin-T 28M parameters, Swin-B 224² ImageNet-1K 83.5% top-1, ICCV 2021 Marr Prize. The 384² checkpoints are named window12_384: they use M = 12, initialised from the 224² model with the bias table resized by bicubic interpolation (Section 3.2).
Our check (not from the paper)A NumPy reimplementation on an 8 × 8 map with M = 4, s = 2: roll, partition, mask with −100, attend, unroll, compared against attention computed directly inside the nine shifted windows. Largest difference 6.7 × 10⁻¹⁶.
Raffel et al. (2020), T5, Section 2.1Relative position "embedding" is a scalar added to the attention logit, learned per head, shared across all layers, and bucketed by offset. Swin keeps the per-head scalar but learns one table per block and one entry per exact 2D offset.
Ramachandran et al. (2019), Stand-Alone Self-Attention, Section 3.1Each pixel attends over a k × k neighbourhood centred on itself (k = 7 in the main models): the sliding-window baseline of Tables 5 and 6, where every query has its own key set.
correctionTwo printed numbers disagree with the released model or with the rest of the paper. Table 1 gives Swin-T 29M parameters; the released architecture has 28.29M (the README says 28M), while every other size in Table 1 reproduces from the code. Swin-B at 224² is 83.5% in Table 1 and the README but 83.3% in the text of Section 4.1 and in Table 8, and the text's "+1.5% over DeiT-B" uses 83.3. Equation (3) alternates W-MSA and SW-MSA in every stage, but at 224² input the last stage is a 7 × 7 map, a single window, and the code switches the shift off there: stage 4 is plain global attention. Finally, the headline +2.7 box AP on COCO test-dev compares Swin-L with multi-scale testing (58.7) against a Copy-paste entry not marked multi-scale (56.0); Swin-L's single-scale test-dev result is 57.7, which is +1.7.

Questions you might still have

?

Does any Swin token ever see the whole image?
Yes, by the end. Each pair of blocks widens the region a token has mixed with by about one window in each direction, each patch merging doubles the pixel size of a token, and at 224 × 224 input the last stage is a single 7 × 7 window, so its two blocks attend globally over all 49 tokens. For COCO detection the HTC++ system adds one more global self-attention layer after the last stage.

?

Why not slide a window around every token, like a convolution does?
That was the prior approach (stand-alone self-attention, local relation networks). Every query then has its own set of keys, so the keys cannot be shared in one dense matrix multiply. Table 5 measures it: the naive sliding-window attention in stage 1 takes 122.5 ms against 3.0 ms for shifted windows, and even a custom CUDA kernel takes 7.6 ms. Accuracy is about the same (81.4 against 81.3 top-1 for Swin-T).

?

Why is the window 7 × 7?
At 224 × 224 input the four stages have 56, 28, 14 and 7 tokens per side, and 7 divides all four, so no stage needs padding and the last stage is exactly one window. For 384 × 384 inputs the paper's models switch to M = 12, which divides 96, 48, 24 and 12 the same way. The paper does not report an ablation over M.

?

How does Swin plug into a detector or a segmentation network?
Its four stage outputs have strides 4, 8, 16 and 32 relative to the image, the same as the C2 to C5 maps of a ResNet, so they feed a feature pyramid network (FPN) unchanged. The COCO experiments swap Swin in for ResNet inside Cascade Mask R-CNN, ATSS, RepPoints v2 and Sparse R-CNN with nothing else changed, and ADE20K uses UperNet the same way. The Mask R-CNN explainer on this site covers how a detector reads such a pyramid.

?

Is Swin a convolutional network in disguise?
It shares three properties with one: each layer mixes only nearby positions, the feature maps shrink by 2 per stage while channels double, and position enters only as a relative offset. What differs is that the mixing weights inside a window are computed from the content (attention) rather than fixed after training. ConvNeXt (Z. Liu et al., 2022) took a ResNet and changed it step by step toward Swin's design and training recipe; the resulting pure convolutional network outperformed Swin on COCO detection and ADE20K segmentation, which suggests the hierarchy and the recipe account for much of the gain.

?

Why add −100 instead of minus infinity in the mask?
The released code uses −100. Adding −100 to a logit multiplies that pair's unnormalized softmax weight by e^−100 ≈ 3.7 × 10^−44, which is zero for any practical purpose in float32, so the output equals attention restricted to the sub-window. The comment in the code still describes the mask as 0 / −inf.

?

Can a trained Swin take a different input size?
For dense tasks, yes: the map is padded at the bottom and right to a multiple of the window size, and the windows tile a larger map, so COCO images of up to 800 × 1333 pixels run through the ImageNet-trained weights. Changing the window size itself requires resizing each block's bias table, which the paper does by bicubic interpolation when it fine-tunes at 384 × 384 with M = 12.

Footnotes & further reading

  1. The paper: Liu, Lin, Cao, Hu, Wei, Zhang, Lin & Guo, Swin Transformer: Hierarchical Vision Transformer using Shifted Windows (ICCV 2021). Code and weights: microsoft/Swin-Transformer (the model is models/swin_transformer.py), with detection and segmentation code in the SwinTransformer GitHub organization.
  2. The Transformers it builds on: Vaswani et al., Attention Is All You Need (see the explainer); Dosovitskiy et al., An Image is Worth 16x16 Words: Transformers for Image Recognition at Scale (see the ViT explainer); Touvron et al., Training data-efficient image transformers & distillation through attention (DeiT).
  3. Local self-attention before Swin: Ramachandran et al., Stand-Alone Self-Attention in Vision Models; Hu, Zhang, Xie & Lin, Local Relation Networks for Image Recognition. Relative position bias: Raffel et al., Exploring the Limits of Transfer Learning with a Unified Text-to-Text Transformer (T5).
  4. The dense-prediction frameworks: Lin et al., Feature Pyramid Networks for Object Detection; He et al., Mask R-CNN (see the Mask R-CNN explainer); Cai & Vasconcelos, Cascade R-CNN; Chen et al., Hybrid Task Cascade for Instance Segmentation; Xiao et al., Unified Perceptual Parsing for Scene Understanding (UperNet).
  5. What came next: Liu et al., Swin Transformer V2: Scaling Up Capacity and Resolution; Liu et al., A ConvNet for the 2020s (ConvNeXt); Liu et al., Video Swin Transformer; Li, Mao, Girshick & He, Exploring Plain Vision Transformer Backbones for Object Detection (ViTDet).