Important Terminologies to Understand:
(1) grid of windows refers to total number of windows
- when we say a grid we mean the number of windows wrt both height and width
- A refers to the number of windows wrt the full height of the image
- B refers to the number of windows wrt the full width of the image
- grid usually means a literal grid of windows, similar to how we split an image up into 4 patches, we have a grid of images
(2) Image divided into patches
- the here refer to the actual size per patch
- meaning from an image of , we will get number of patches wrt the width, and number of patches wrt the height
- resulting in a total of number of patches
Part 1: Hierachical Feature Maps

- in ViT, images split into patches
- these patches remain the same size throughout every single layer of the network
- this produces a single-resolution feature map which is bad for dense predictions like detecting small objects
- swin transformers fixes this my mimicking the structure of traditional CNNs
- it build a hierachical feature map by merging patches as the network gets deeper
Input
- We start with an image of size (Height, Width, Channels - RGB)
Stage 1

- Images divided into small patches (usually pixels)
- This gives a feature map resolution of
Stage 2, 3 and 4
- As tokens move deeper into the network, neighbouring patches are systematically merged together (e.g. neighbouring patches become 1 token)
Final

- By the final stage, spatial resolution drops to
- just like CNN, swin transformer's feature map starts large with great details
- we say 'large' because with a patches in a image, we will end up with a large grid of individual results
- many pixels patches will show us great small level details such as corners, textures, sharp edges
- moving up to stage 4, we will end up with a high-level overview of the image with larger patches
- these large patches are able to look at features such as shape and whole objects
Part 2: Linear Embedding and Patch Merging
Stage 1: Patch Partition & Linear Embedding
- the patches (of values) are flattened
- resulting in raw pixel values in a vector form
- we pass this through a Linear Layer to a specified channel dimension, denoted as (an arbitrary hidden size, like 96)
- meaning each patch will now be a single 1D vector of length C
- this transforms our raw pixel grid into a clean token grid of shape:
Stages 2, 3, 4: Patch Merging

- to downsample the spatial grid and increase the channel depth as we move deeper, the network uses a Patch Merging layer
- it acts like a pooling layer in CNN, but it doesn't throw away any data
Between Stage 1 and Stage 2:
(1) Concatenate
- it takes groups of neighbouring patches and concatenates their features together
- gluing 4 neighbours together gives a single token with channels
(2) Downsample
- since we group patches into 1, the spatial grid resolution is cut into half (reduced)
- from down to
(3) Linear Reduction
- To keep channel count from blowing up too fast, a linear layer projects the down to
- meaning that initially our 4 combined patch has a combined length of 4C, but then because we linearly project it onto a linear layer, we reduce the length of vector to
(4) Pattern
- this pattern continues between stages
- every time you pass through a Patch Merging layer, the spatial resolution is halved, and the channel depth is doubled.
- Stage 1:
- Stage 2:
- Stage 3:
- Stage 4:
Note: refers to the Stage 1 output dimension, not the raw pixel vector length
Part 3: Window-based Self Attention
- Swin introduces Window-based Self-Attention
- this W-MSA runs inside every stage
How W-MSA works:
- swin divides the feature map into a grid of non-overlapping windows
- the original paper uses a standard window size of
- self-attention is only calculated within each individual window
- a patch is window A can only talk to other patches in window A
Math:
- since attention is confined to a fixed tokens
- the cost to calculate attention inside a single window is always constant
- if we scale up the image size, we can just add more windows
- this changes the computational cost from quadratic to linear
Part 4: Shifted Window Attention

- windows are isolated
- swin transformers come in a pair of 2 consecutive blocks:
- Block 1 uses Regular Windowing (W-MSA) which is explained in Part 3
- Block 2 uses Shifted Windowing (SW-MSA)
How SW-MSA works:

- SW-MSA starts from the same partition as W-MSA (many windows)
- when we shift, we slide the entire window grid down by pixels, i.e. shift amount is the floor of
- the paper used a window size of , which means we shift the grid by 3 patches down and 3 patches right
- By shifting the boundaries, the new windows are centered directly on top of the old boundaries.
- Patches that were once seperated into 2 different windows are now both inside the same window because of this shifting mechanism
- They can now run self-attention together, passing information across the old borders
Part 5: Cyclic Shifting & Masking Attention
Step 1: Cyclic Shifting (Realignment)
- when we shift windows down and right, the parts left over at the top and left edges are phyiscally moved to the opposite side
- so what we do is shift these orphaned patches around the edges to join other orphaned patches, forming a complete 7x7 window
- something like pac-man wrapping effect
- so the left orphan flies off the left edge to join the right orphan at the right side
- top orphan flies off the top and comes out at the bottom to join the bottom orphan
- same for the corner orphan flying off and joining diagonal orphan at the opposite corner
Step 2: Masked Attention (The Correction)
- since these "edge cases" sub-sections within those windows are spatially non-adjacent to other sections
- we apply something called Masked Attention onto these "edge cases"
- During the attention calculation (), the model applies a matrix mask
- masks sets those "edge cases" windows' attention scores to , effectively blocking them from communicating
- note that it only applies this mask on unrelated groups to
- for example, if a window contains 2 patches physically side by side, and 2 patches totally unrelated, mask will apply to the 2 unrelated ones only
Step 3: Shift Back
- Once the attention calculation is done inside the shifted window block, the tokens are cyclically shifted back to their original positions to keep the image structure intact for the next layers