Swin Transformer

The paper introduced a hierarchical vision Transformer that uses shifted local windows for efficient multi-scale visual representation.

Metadata

FieldValue
PaperSwin Transformer: Hierarchical Vision Transformer using Shifted Windows
AuthorsZe Liu, Yutong Lin, Yue Cao, Han Hu, Yixuan Wei, Zheng Zhang, Stephen Lin, Baining Guo
Year2021
VenueICCV 2021
arXiv2103.14030
CVFICCV 2021 paper
Statusverified

Question

Vision Transformer treats an image as a sequence of fixed-size patches, but dense global attention is expensive for high-resolution images and does not naturally build a multi-scale feature pyramid. The question was how to make Transformer-based vision backbones more compatible with dense prediction tasks.

The deeper question is whether a vision Transformer should remain a flat global-token model. CNN backbones naturally create hierarchical feature maps. Object detection and segmentation often expect multi-resolution features. Swin asks whether a Transformer can recover that hierarchy while keeping attention efficient.

This reframes the ViT design:

Main Claim

A hierarchical Transformer with shifted window attention can provide efficient local attention, cross-window interaction, and multi-scale visual features.

Narrowed claim:

This changes the vision Transformer from a flat patch sequence classifier into a general-purpose vision backbone.

Architecture Contract

ItemContract
Inputimage tensor
Initial representationnon-overlapping patch tokens
Main token mixinglocal window self-attention
Cross-window communicationshifted window partition across alternating blocks
Hierarchypatch merging reduces resolution and increases channels
Position signalrelative position bias inside windows
Natural tasksclassification, detection, segmentation

If the feature map has spatial size and channel dimension , Swin keeps a 2D token map rather than collapsing the image into a purely flat sequence for the entire backbone:

The model repeatedly applies local attention and spatial downsampling:

This is why Swin behaves more like a general-purpose vision backbone than the original flat ViT classifier.

Method

Swin Transformer uses:

ComponentRole
window self-attentioncompute attention locally within windows
shifted windowsconnect neighboring windows across layers
patch mergingreduce resolution and increase channels hierarchically
relative position biasencode local spatial offsets

Window attention reduces the cost relative to dense attention over all image patches:

where is the window size.

Window Self-Attention

Instead of applying attention over all image tokens, Swin partitions the feature map into local windows. If each window has tokens, attention is computed within each window:

where is one window and is the number of windows.

Dense global attention over all image tokens has cost:

Window attention has approximate cost:

when is fixed. This makes the attention cost linear in image area for fixed window size.

Shifted Windows

Local windows alone isolate neighboring windows. Swin alternates regular windows and shifted windows so tokens can interact across previous window boundaries.

Conceptually:

The shift means a token’s local attention neighborhood changes across blocks. Cross-window communication emerges without paying full global attention cost.

DesignBenefitCost
fixed local windowsefficient local attentionwindows are isolated
shifted windowscross-window interactionmask and partition complexity
global attentiondirect all-pair interactionquadratic cost

Patch Merging and Hierarchy

Swin builds stages. Between stages, patch merging reduces spatial resolution and increases channel dimension.

Patch merging concatenates neighboring tokens:

then projects the concatenated vector:

This creates a multi-scale feature hierarchy similar in spirit to CNN backbones.

Stage BehaviorVision Meaning
high resolution, low channelslocal edges/textures/small objects
lower resolution, higher channelssemantic regions and object-level features
multi-scale outputsdetection and segmentation compatibility

Relative Position Bias

Inside each local window, Swin adds a relative position bias to attention logits:

where depends on the relative offset between tokens inside the window.

This matters because local spatial relations are central to vision. Swin does not rely only on absolute position embeddings from the original ViT recipe.

Relation to ViT

AxisViTSwin
token layoutflat patch sequencehierarchical 2D token maps
attentionglobal attentionlocal shifted-window attention
cost with resolutionquadratic in patch countlinear in image area for fixed window size
dense prediction fitneeds adaptationdesigned as general backbone
image biasweakerreintroduces locality and hierarchy

Swin can be read as a correction to the original ViT design for vision infrastructure. It keeps Transformer blocks but restores some of the structure that made CNN backbones useful.

Architecture Tradeoff

Swin gives up immediate global all-token attention in exchange for efficient local attention and staged hierarchy.

This tradeoff is acceptable when:

  • high-resolution images make dense attention expensive;
  • local visual neighborhoods are meaningful;
  • downstream tasks need multi-scale feature maps;
  • detection/segmentation heads expect backbone pyramids.

Evidence

ClaimEvidence in paperCaveat
Swin works as a general vision backboneimage classification, object detection, and segmentation experimentsvision-specific architecture and training recipe
Shifted windows enable cross-window communicationablations and dense prediction resultsnot the only possible local-attention design
Hierarchical features help dense predictionCOCO and ADE20K resultscomparisons depend on backbone and training setup

Benchmark Reading

Swin should be read as a backbone paper, not only an ImageNet classifier.

Benchmark TypeWhy it matters
ImageNet classificationchecks classification backbone quality
COCO detectiontests object-level dense prediction
ADE20K segmentationtests dense semantic prediction
throughput/complexitychecks whether local windows actually help

The strongest claim is that Swin works across classification, detection, and segmentation with one hierarchical Transformer backbone.

Benchmark Card

FieldValue
Main taskgeneral vision backbone evaluation
Input/output unitimage to class, boxes, masks, or segmentation
Architecture familyhierarchical vision Transformer
Main comparisonCNN backbones and flat vision Transformers
Not directly testednon-image sequence modeling, graph learning, molecular structure modeling

Ablation Reading

AxisWhat it testsReading
shifted vs non-shifted windowscross-window communicationcentral to the paper’s design
window sizelocality/compute tradeofftoo small limits context, too large increases cost
hierarchy/patch mergingdense prediction compatibilitymakes Swin backbone-like
relative position biaslocal spatial relation modelingimportant for vision attention
task transferclassification vs detection vs segmentationtests general backbone claim

The key ablation question is whether shifted windows recover enough cross-window information without returning to global attention.

Implementation Notes

  • Window partitioning and reverse partitioning must preserve spatial layout exactly.
  • Shifted windows require attention masks so wrapped tokens do not attend incorrectly.
  • Relative position bias tables are indexed by window-relative offsets.
  • Feature pyramid integration matters for detection and segmentation results.
  • Window size interacts with input resolution, memory, and throughput.
  • Swin is not a pure ViT; it deliberately reintroduces vision-specific structure.

Limitations

  • Swin reintroduces vision-specific locality and hierarchy, so it is not simply a pure Transformer over all patches.
  • Window size, shift pattern, resolution schedule, and training recipe are part of the claim.
  • Dense prediction success depends on integration with detection and segmentation heads.
  • Later architectures changed local attention, pooling, convolution hybrids, and scaling recipes.
  • Local windows can miss long-range interactions unless depth and shifts propagate information sufficiently.
  • Implementation complexity is higher than flat ViT.
  • Claims outside image grids need separate evidence.

Why It Matters

Swin Transformer is the anchor paper for hierarchical local-attention vision Transformers and clarifies why vision backbones need more than flat tokenization.

The reusable architecture pattern is:

For this wiki, Swin marks the point where Transformer vision models stop being only patch-sequence classifiers and become practical backbones.

Connections