Foundations to Frontier
Tests
E. Deep Learning › E12 Transformer Architectures

Swin Transformer

The Swin Transformer (Liu and colleagues, ICCV 2021) brings CNN-like structure to vision Transformers. Attention is computed within local windows of 7 × 7 patches, so cost grows linearly with image size. Windows are shifted by half their size in alternate layers so information crosses window borders. Patch merging halves the resolution between stages, giving feature maps at strides 4–32 that drop into detection and segmentation frameworks. It set records on ImageNet, COCO and ADE20K and won the ICCV 2021 Marr Prize. This page covers window attention, shifted windows, patch merging and relative position bias. In the exercises you implement window partitioning and patch merging, then measure how far information travels on a 16 × 16 patch grid. Fixed windows never let more than 16 patches reach the centre, while shifted windows reach all 256 in four layers, at 1/64 of global attention's cost at Swin's real resolution.

Level 0: Foundation

★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.

Local windows that slide

Global attention over every patch of a large image is expensive. Swin splits the image into small windows and runs attention only inside each window, which is cheap. To stop windows from becoming isolated islands, every other layer moves the window grid by half a window, so neighbouring windows overlap and share information. Swin also merges patches between stages, building a pyramid of features like a CNN. The demo shows attention patterns and costs.

Transformer lab
Computed pairs: 136 of 256 for 16 tokens. Sparse patterns grow linearly with length instead of quadratically.
Why engineers care

Swin’s linear cost and multi-scale feature maps make Transformers practical for high-resolution detection and segmentation: aerial imagery, inspection, autonomous driving.

📝 Quick check (Level 0)
0/3 answered
  1. 1. Swin computes attention…
  2. 2. Windows are shifted so that…
  3. 3. Patch merging produces…

Level 1: Beginner

★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.

By hand

  • Patches: 4 × 4 patches turn a 224 × 224 image into a 56 × 56 token grid. Swin-T uses C = 96 channels.
  • Window attention:
    • windows of M × M = 7 × 7 tokens, so a 56 × 56 grid has 64 windows;
    • cost is 2·H·W·M²·d against 2(HW)²d for global attention, 64 times less at 56 × 56;
    • cost grows linearly with image area.
  • Shifted windows:
    • alternate layers shift the window grid by ⌊M/2⌋ = 3 tokens;
    • Swin implements this with a cyclic shift and an attention mask, so tokens wrapped from the opposite edge do not attend to each other.
  • Patch merging:
    • concatenate each 2 × 2 group of tokens (4C channels) and project to 2C, halving the resolution;
    • stage sides are 56, 28, 14 and 7 (strides 4, 8, 16, 32), like a ResNet.
  • Relative position bias: each head has a learned table of (2M − 1)² = 169 biases for the relative offsets within a window.
  • Sizes: Swin-T has stage depths 2, 2, 6, 2 and about 28 M parameters.
📝 Quick check (Level 1)
0/3 answered
  1. 1. Windows on a 56 × 56 grid with M = 7:
  2. 2. Swin's window shift for M = 7:
  3. 3. Relative bias table size for M = 7:

Level 2: Intermediate

★★★☆☆ Intermediate⏱ 25 minUndergraduate students and practising engineers.

Swin in code

Exercise 1.

  • Window partitioning of a 4 × 4 map into 2 × 2 windows grouped tokens correctly.
  • Patch merging turned 8 × 8 × 3 into 4 × 4 × 12.
  • Windowed attention at 56 × 56 with M = 7 cost 64 times less than global attention.

Practice.

  • timm.create_model("swin_tiny_patch4_window7_224"), or the Swin backbones in detection and segmentation libraries (Mask R-CNN, UPerNet).
  • Use torch.roll for the cyclic shift and an additive −∞ mask for wrapped regions.
  • Swin V2 scaled to 3 B parameters and higher resolutions with scaled cosine attention and log-spaced continuous position bias.
x = x.view(B, H, W, C)
if shift: x = torch.roll(x, shifts=(-M // 2, -M // 2), dims=(1, 2))
windows = x.view(B, H // M, M, W // M, M, C).permute(0, 1, 3, 2, 4, 5).reshape(-1, M * M, C)
out = window_attention(windows, mask=attn_mask if shift else None)    # attention inside each window
🐍 Exercise 1: windows and merging
Implement window partitioning, patch merging and the cost of window attention.
📝 Quick check (Level 2)
0/3 answered
  1. 1. The cyclic shift in PyTorch uses…
  2. 2. Swin's feature strides:
  3. 3. Swin V2 added…

Level 3: Advanced

★★★★☆ Advanced⏱ 25 minGraduate level, with full derivations and proofs.

Theory

Why local windows.

  • Images are dominated by local structure, and global attention over thousands of tokens is expensive.
  • Window attention keeps the locality prior of CNNs while staying content-adaptive (attention weights depend on the input).
  • Cost becomes O(HW·M²) instead of O((HW)²).

Why shifting.

  • With fixed windows, a token’s receptive field never grows beyond its window: windows are isolated, and stacking layers does not help.
  • Shifting by half a window in alternate layers connects each window to parts of its four neighbours.
  • Information then spreads across the image over layers, together with patch merging, which grows the effective receptive field like pooling in CNNs.

Hierarchy. Patch merging halves resolution and doubles channels, giving multi-scale maps. Detection and segmentation heads built for CNN pyramids (FPN, UPerNet) can use Swin directly, a key reason for its success.

📝 Quick check (Level 3)
0/3 answered
  1. 1. With fixed windows, receptive fields over layers…
  2. 2. Window attention cost:
  3. 3. Swin plugs into heads designed for…

Level 4: Expert

★★★★★ Expert⏱ 30 minResearchers: original papers and open problems.

How do shifted windows spread information?

History.

  • Z. Liu, Y. Lin, Y. Cao, H. Hu, Y. Wei, Z. Zhang, S. Lin and B. Guo, “Swin Transformer: hierarchical vision Transformer using shifted windows” (ICCV 2021, Marr Prize).
  • Swin V2 (2022) scaled the design to 3 B parameters.

Exercise 2.

  • Grid: 16 × 16 patches with 4 × 4 windows, using exact connectivity matrices for stacked attention layers with:
    • fixed windows;
    • windows shifted by 2 in alternate layers, with Swin’s mask for wrapped regions;
    • global attention.
  • Measurement: how many patches can influence the centre patch after each layer, plus the cost at Swin’s real 56 × 56 grid with 7 × 7 windows.
attention after layer 1 layer 2 layer 3 layer 4
fixed windows 16 16 16 16
shifted windows 16 64 144 256
global 256 256 256 256

Shifted windows, corner patch, layers 1–6: 16, 16, 64, 64, 144, 144.

Attention cost at 56 × 56 with 7 × 7 windows (d = 32): global 6.3 × 10⁸ multiply–adds, windowed 9.8 × 10⁶, 64× less.

  • Fixed windows isolate information: the centre patch is never influenced by more than its own 16-patch window, however deep the network.
  • Shifted windows grow the receptive field by about one window per layer and cover all 256 patches in 4 layers.
  • Corners spread more slowly, because the wrap-around mask blocks them in shifted layers (144 after 6 layers). Swin’s patch merging between stages helps close this gap.
  • The cost is a small fraction of global attention, linear rather than quadratic in image area.
🐍 Exercise 2 (research): how do shifted windows spread information?
Compute exact receptive fields over layers for fixed, shifted and global attention on a patch grid, and compare costs.

Swin in AI, robotics and aerospace

  • Aerial and satellite imagery: Swin backbones handle large, high-resolution tiles for detection and segmentation.
  • Driving perception: hierarchical Transformer backbones feed detection and BEV (bird’s-eye-view) models.
  • Design lesson: combine local computation with a mechanism for cross-region communication (shifts, merging) whenever global operations are too costly.

References

  1. Z. Liu et al. (2021). “Swin Transformer: hierarchical vision Transformer using shifted windows.” Proc. ICCV.
  2. Z. Liu et al. (2022). “Swin Transformer V2: scaling up capacity and resolution.” Proc. CVPR.