← All answersVision transformers

How does the Swin Transformer utilize shifted windows to compute localized attention?

Short answer

Global attention over every patch is too expensive at high resolution, so Swin computes attention only within small non-overlapping windows, which makes cost grow linearly with image size instead of quadratically. To stop windows from being isolated, the next layer shifts the window grid by half a window, so patches that were on opposite sides of a boundary now share a window. Alternating regular and shifted windows spreads information globally over depth.

Swin's central problem: full self-attention on a big image is quadratic in the number of patches, which is ruinous at detection/segmentation resolutions. Its answer is to keep attention local but let locality 'leak' across layers.

The two-step pattern

  • Windowed attention: partition the feature map into fixed windows and attend only inside each - cheap and linear in image size.
  • Shifted windows: in the following block, slide the window partition by half a window so tokens that were split apart now attend together, connecting neighboring windows.

Stack these alternating blocks and information flows across the whole image without ever paying for full global attention. Swin also merges patches between stages to build a feature pyramid (fine to coarse), which is exactly what detectors and segmenters want from a backbone - and a big reason Swin became a popular replacement for CNN backbones.

In practice

My rule of thumb: Swin is a great backbone when accuracy is the priority and you have a decent GPU for offline or batch analysis, but I don't reach for it on a Jetson or for real-time video - it's noticeably heavier than a YOLO backbone. So Swin for high-accuracy offline processing, a YOLO CSP backbone when frames-per-second is the hard constraint. Benchmark both on your actual footage before you decide.

Stuck on this in a real project?

Book a free call and I’ll give you a straight answer on your specific case.

Book a consultation