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 WindowsIn 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 tokens, 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 map of tokens with channels, in multiply-adds and ignoring the softmax. For global attention (MSA) and window attention (W-MSA):
The first term is the same in both. Each of the tokens goes through four linear maps, the query, key and value projections and the output projection, at multiply-adds each. The second term is the attention itself. Global attention computes query-key dot products of length (summed over the heads, the head dimensions add up to ) and then the same number of weight-times-value products, which gives . In window attention each of the windows does that computation on tokens, costing ; multiplied by the number of windows that is . With fixed, every term of (2) is proportional to the token count , which is what the paper means by linear complexity.
Take Swin-T's first stage at 224 × 224: , , . 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 = 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.
Two settings of the window slider show the trade. At each token attends only to itself and the attention term nearly vanishes, but the layer no longer mixes information between positions at all. At on a 224 × 224 image the window is the whole map and (2) equals (1). Swin's 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 tokens, 3 for , 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 with a GELU (a smooth variant of ReLU) between the layers, and another residual. Two consecutive blocks are written
where is the output of the attention module of block , 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.
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 map the regular partition has windows and the shifted one has , some of them smaller than . 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⁻¹⁵.
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 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 -> 96The 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:
Here are the query, key and value matrices of one head in one window, is the per-head dimension in every Swin model, and is an matrix of biases, one per query-key pair. Within a window the row offset between two tokens runs from −6 to +6 when , and so does the column offset, so there are only 13 × 13 = 169 distinct offsets. Each head of each block learns a table of 169 numbers, and the 49 × 49 = 2,401 entries of 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.
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 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 , 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 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 channels, normalize it, and project it to 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.
The four variants differ only in the stage-1 width and the depth of stage 3:
| Model | C | blocks per stage | params | FLOPs (224²) | throughput | top-1 |
|---|---|---|---|---|---|---|
| Swin-T | 96 | 2, 2, 6, 2 | 29M | 4.5G | 755 img/s | 81.3 |
| Swin-S | 96 | 2, 2, 18, 2 | 50M | 8.7G | 437 img/s | 83.0 |
| Swin-B | 128 | 2, 2, 18, 2 | 88M | 15.4G | 278 img/s | 83.5 |
| Swin-L | 192 | 2, 2, 18, 2 | 197M | 34.5G | 141 img/s | (22K only) |
| DeiT-S | 384 | 12, one map | 22M | 4.6G | 940 img/s | 79.8 |
| DeiT-B | 768 | 12, one map | 86M | 17.5G | 292 img/s | 81.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 buffersTraining 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 val | box AP | mask AP | params | FLOPs | FPS |
|---|---|---|---|---|---|
| DeiT-S (+ deconv) | 48.0 | 41.4 | 80M | 889G | 10.4 |
| ResNet-50 | 46.3 | 40.1 | 82M | 739G | 18.0 |
| Swin-T | 50.5 | 43.7 | 86M | 745G | 15.3 |
| ResNeXt-101-32x4d | 48.1 | 41.6 | 101M | 819G | 12.8 |
| Swin-S | 51.8 | 44.7 | 107M | 838G | 12.0 |
| ResNeXt-101-64x4d | 48.3 | 41.7 | 140M | 972G | 10.4 |
| Swin-B | 51.9 | 45.0 | 145M | 982G | 11.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 variant | ImageNet top-1 | COCO box AP | mask AP | ADE20K mIoU |
|---|---|---|---|---|
| no shifting | 80.2 | 47.7 | 41.5 | 43.3 |
| no position information | 80.1 | 49.2 | 42.6 | 43.8 |
| absolute position embedding | 80.5 | 49.0 | 42.4 | 43.2 |
| absolute + relative | 81.3 | 50.2 | 43.4 | 44.0 |
| relative bias only, no QKᵀ term | 79.3 | 48.2 | 41.9 | 44.1 |
| shifted windows + relative bias | 81.3 | 50.5 | 43.7 | 46.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 method | stage 1 ms | stage 4 ms | Swin-T img/s | top-1 |
|---|---|---|---|---|
| sliding window, naive | 122.5 | 7.6 | 183 | 81.4 |
| sliding window, CUDA kernel | 7.6 | 1.8 | 488 | |
| Performer (linear attention) | 4.8 | 1.5 | 638 | 79.0 |
| windows, no shift | 2.8 | 0.9 | 770 | 80.2 |
| shifted windows, padding | 3.3 | 2.2 | 670 | 81.3 |
| shifted windows, cyclic shift | 3.0 | 1.0 | 755 | 81.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 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.
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
- 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.
- 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).
- 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).
- 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).
- 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).
How could this explainer be improved? Found an error, or something unclear? I read every message.