- Introduced by
- Ze Liu
- Year
- 2021
- Type
- Neural network architecture
- Family
- Deep Learning, Vision Transformers
What does it do?
The Swin Transformer is a hierarchical vision transformer introduced by Liu et al. in 2021.
It uses shifted window attention, which restricts self-attention to local windows and changes the window partition between layers, so that computation grows linearly with image size while the model keeps a multi-scale structure. Unlike the original Vision Transformer, which applies global self-attention across all patches, Swin is designed to serve as a general-purpose backbone for vision tasks.
The intuition behind the method
Global self-attention compares every patch with every other patch, so its cost grows quadratically with the number of tokens, which becomes impractical for high-resolution images. Swin instead divides the image into windows and computes self-attention only within each window, so the cost of each window is fixed and the total cost grows with the number of windows, that is, linearly with image area. Attention alone would never connect neighbouring windows, so successive blocks shift the window boundaries; this lets information flow across the earlier window borders. The hierarchical design also mirrors convolutional networks: spatial resolution is progressively reduced while the channel dimension increases, producing feature maps at several scales that downstream detection and segmentation heads can use.
History
Liu and colleagues introduced the Swin Transformer in a paper published in the proceedings of the 2021 IEEE/CVF International Conference on Computer Vision. The name refers to the shifted windows that are central to the design. The architecture has since been used as a backbone in a range of detection and segmentation work, and the authors present it explicitly as a general-purpose backbone for vision.
When to use it
Choose Swin when the task is dense prediction, such as object detection, instance segmentation or semantic segmentation, or when a multi-scale backbone is needed for a downstream head. It is a natural fit for high-resolution images, where global attention over all tokens would be too costly. The original paper also reports strong image classification results, so Swin is not restricted to dense tasks. Consider a plain convolutional baseline or a global-attention model instead when the implementation effort or the dependence on a specialised attention kernel is a concern, or when the input is so small that the windowing scheme offers little benefit.
Limitations and considerations
- Attention within a window cannot reach distant tokens directly, so long-range relations must be propagated through the shifted windows and stacked blocks
- The windowed attention with cyclic shifting and masking is more complex to implement and optimise than standard convolutions
- For classification-only tasks, the advantage over simpler architectures depends on the resolution and on the comparison baseline
Source conditions do not replace independent scientific review or validation by an analysis engine.
Strengths
- Cost of self-attention grows linearly with image size because attention is computed inside fixed-size windows rather than across all tokens
- The shift of window boundaries between consecutive blocks lets information cross window borders without global attention
- The hierarchical stages produce multi-scale feature maps that suit detection and segmentation heads
- The window size is a parameter that can be adjusted to trade local detail against the extent of each attention region
Applications
- Object detection and instance segmentation, where Swin serves as the backbone for the task head; the original paper reports 58.7 box AP and 51.1 mask AP on COCO test-dev
- Semantic segmentation with Swin features; the original paper reports 53.5 mIoU on ADE20K validation
- Image classification using the Swin backbone; the original paper reports 87.3% top-1 accuracy on ImageNet-1K
- Use as a general-purpose vision backbone in other detection and segmentation systems, where its multi-scale feature maps are the main reason for the choice
How it works
How it works
The input image is split into non-overlapping patches, each treated as a token, as in the Vision Transformer. This forms the initial token grid on which the subsequent window operations act.
The token grid is divided into local windows, and successive blocks alternate between regular and shifted window partitions. The shift moves the window boundaries so that tokens that were separated by a boundary in one block can attend to each other in the next. In common implementations, the shifted partition is realised by cyclically shifting the feature map, and an attention mask prevents tokens that were not neighbours in the original partition from attending to each other within the same shifted window.
Multi-head self-attention is computed independently inside each window. Because each window contains a fixed number of tokens, the cost per window is constant, and the total cost scales linearly with the number of windows, that is, with image size.
After a set of blocks, neighbouring patches are merged, which reduces spatial resolution and increases channel dimension. Repeating this across stages yields a feature pyramid with decreasing resolution and richer semantic content at each level.
Common pitfalls
- The window size is expressed in patch tokens, not in pixels; the input resolution and the patch size together determine the feature-map size that the window must divide, so the window setting should be checked against the feature-map dimensions at each stage
- Shifted windows are only beneficial if the shift is implemented correctly with the attention mask; an incorrect implementation removes the cross-window connections the design relies on
- Comparisons with the Vision Transformer are only meaningful when pretraining data, input resolution and training schedule are matched
Frequently asked questions
How does Swin Transformer differ from Vision Transformer?
The Vision Transformer applies global self-attention to all patches, so its cost grows quadratically with the number of tokens. Swin restricts self-attention to local windows and shifts the window boundaries between blocks, so its cost grows linearly with image size. Swin also builds a hierarchy of progressively coarser feature maps, which the Vision Transformer does not do by default. Both architectures can be used for classification; the hierarchical features make Swin particularly convenient for dense prediction.
What is the shifted window and why is it necessary?
Windows partition the image into local regions for efficient attention. Shifting the windows between consecutive blocks displaces their boundaries so that tokens on either side of an earlier boundary can attend to each other. Without shifting, tokens in different windows would never interact within a block, which would limit the model's ability to capture context that spans window boundaries.
How is the window size chosen?
Window size is a hyperparameter that trades off the extent of each attention region against computational cost. Larger windows allow each token to interact with more neighbours but increase the cost per window; smaller windows are cheaper but restrict each attention region. The window is measured in patch tokens and is best chosen so that it divides the feature-map dimensions at each stage; when it does not, common implementations pad the feature map before attention. The value should be set according to the input resolution and the available compute, and the setting reported in the original paper is the natural starting point.
Related methods
Sources
Liu, Z., Lin, Y., Cao, Y., Hu, H., Wei, Y., Zhang, Z., Lin, S., & Guo, B. (2021). Swin Transformer: Hierarchical vision transformer using shifted windows. In Proceedings of the IEEE/CVF International Conference on Computer Vision (pp. 10012-10022).
Open DOI reference