Attention over space and time: non-local blocks and video transformers
Let every space-time position attend to every other (non-local blocks), then attend more cheaply: across time then space (TimeSformer), in tubelets (ViViT), with pooled keys (MViT) or in shifted windows.
What runs in CodeSplash
- VID007Non-local Neural Networks for Video Explained here
- VID010TimeSformer Explained here
- VID011ViViT Explained here
- VID012Multiscale Vision Transformers (MViT) Explained here
- VID013Video Swin Transformer Explained here
You will be able to
- Write a non-local block and say what it costs
- Compare full, divided and windowed space-time attention by the number of token pairs
- Explain tubelet tokens and what a large tubelet averages away
- Say why attention to a distant frame isn't proof the model reasons about time
Every position, every frame
ReadA convolution only sees a neighborhood. A non-local blockNon-local blockA layer where every position in a clip takes a weighted average of all positions, in every frame, then adds it back. lets each position in a clip average the features of all positions , in every frame, weighted by how similar they are:
and adds the result back through a residual connection. With softmax weights this is self-attentionSelf-attentionEach token builds its next value as a weighted mix of all tokens, with weights from how well their keys match its query.. In the reference's three-position toy, the position that resembles both others ends up with 0.788 in both features: an even mix.
Attention weights, masks and heads on a handful of tokens.
DExplorable · Attention: scores, masks and heads
Where does each word look, and what do the scaling and the masks change?
With this mask, how much of each word's attention lands on <pad>?
How many of the positions are <pad>.
Width d
Heads
Mask
Without masks or positions the output rows shuffle the same way.
- Little weight
- Most weight
- Masked: score set to −∞
Commit your prediction above to see the result
Make your prediction to see the result.
Count the pairs
ReadA clip of frames, each cut into patches, gives tokens. Full attention compares every token with every other: pairs.
Divided space-time attentionDivided space-time attentionAttend across time at each patch location, then across space within each frame, instead of over all tokens at once. (TimeSformer) splits that in two. First each token attends to the same patch location in the other frames ( partners); then to the other patches in its own frame ( partners). That costs pairs.
Reference · VID010 · Explained hereTimeSformerApplies transformer attention to video patches, with divided temporal and spatial attention as an effective design.How much cheaper?
PredictPredict first
A clip has 16 frames of 196 patches each. Roughly how many fewer token pairs does divided attention compare than full attention?
Reveal
About 15 times fewer. Full attention compares pairs, close to ten million. Divided attention compares , about two-thirds of a million. The ratio is : it grows as clips get longer.
Divided attention in the toy
ReadIn the reference's program, a two-frame, two-patch clip passes through temporal attention and then spatial attention; the first token comes out as 0.411 and 0.536.
A patch location isn't an object. When a hand moves, it crosses patch locations, and temporal attention at one location follows the location, not the hand.
Tubelets as tokens
ReadViViT cuts the clip into tubeletsTubeletA small block of pixels spanning a few frames, flattened and projected into one token for a video transformer.: small blocks of pixels spanning several frames, each flattened and projected to one token, plus a position embedding. It then compares factorized designs, such as a spatial encoder followed by a temporal one.
In the reference's toy, two-frame tubelets are projected by averaging matching pixels: the first token is 1 and 2, the mean of its two frames. Fewer tokens, less memory, but motion within a tubelet is averaged away.
Patches become tokens; positions keep their order.
DExplorable · Vision transformer patches
How does a transformer read an image, and what does it lose without positions?
Image
Image size
Patch size P
A patch size that doesn't divide the image cuts off its border.
Each patch becomes a vector of this many numbers.
The sequence the transformer reads; numbers are each patch's place in the image
Attention pairs for each patch size (log scale)
Score 1.5622 · change when shuffled 0.1421
- Tokens
- 16
- Attention pairs
- 256
- Embedding weights
- 520
- Pixels dropped
- 0
- Score
- 1.5622
- Change when shuffled
- 0.1421
- Puzzle: Pick an image size and patch size that cut off part of the image.
32 × 32 pixels in 8 × 8 patches make 16 tokens, so attention compares 256 pairs. With position embeddings, shuffling the patches changes the score by 0.1421.
Pooled keys and shifted windows
ReadPooling attentionPooling attentionAverage-pool the queries, keys or values inside attention, so later layers work on fewer, wider tokens. (MViT) pools keys and values before attention: in the reference's toy, 4 queries attend to only 2 pooled keys. Stages trade resolution for channels.
Reference · VID013 · Explained hereVideo Swin TransformerRestricts attention to local 3D windows and alternates shifted windows so information crosses window boundaries.Shifted 3D window attentionShifted 3D window attentionAttention inside small space-time windows, with the windows shifted every other layer so information crosses their borders. (Video Swin) attends inside small space-time windows and shifts them every other layer. The shift wraps around: the reference's last shifted window pairs token 7 with token 0, the two ends of the clip. An attention maskAttention maskBlocks some query–key pairs before the softmax, such as future tokens (causal) or padding, so they get zero weight. must keep them apart.
Attention isn't an explanation
ReadA video transformer that attends to a frame far away hasn't proved it reasons about time. Background shortcuts are still available, and attention weights are one quantity inside a large computation.
Two checks are worth more:
- a shuffled-frame controlShuffled-frame controlScore the model again on clips with their frames in random order. If accuracy barely drops, it isn't using motion.: score clips with frames in random order;
- memory and accuracy at several clip lengths, since costs and sampling interact.
What runs here
Read| Entry | In CodeSplash |
|---|---|
| VID007 · Non-local Neural Networks for Video | Explained here |
| VID010 · TimeSformer | Explained here |
| VID011 · ViViT | Explained here |
| VID012 · Multiscale Vision Transformers (MViT) | Explained here |
| VID013 · Video Swin Transformer | Explained here |
Check: video transformers
CheckpointCheck yourself
What does a non-local block add to a convolutional video network?
Show the answer
Answer: a
It lets distant frames and places interact in one layer, at a cost that grows with the square of the number of positions.
Check yourself
How would you show that a non-local block helps?
Show the answer
Answer: b
The fair comparison is the same network with and without the block, at the same budget.
Check yourself
In divided space-time attention, what does the temporal step attend over?
Show the answer
Answer: b
Temporal attention links one location across frames; spatial attention then mixes patches within a frame.
Check yourself
Temporal attention in TimeSformer links one patch location across frames. Is that tracking an object?
Show the answer
Answer: b
It follows places, not things. Moving objects pass through different patches.
Check yourself
A ViViT model uses tubelets that span eight frames. What is a risk?
Show the answer
Answer: b
Longer tubelets mean fewer tokens and lose fine motion within each one.
Check yourself
How should two ViViT factorizations be compared?
Show the answer
Answer: a
Budgets must match, and motion-heavy and appearance-heavy datasets show different things.
Check yourself
What does MViT pool, and what can it cost?
Show the answer
Answer: b
Fewer tokens save memory, but pooling early can remove the small, fast things you want to recognize.
Check yourself
As an MViT network goes deeper, how do its tokens change?
Show the answer
Answer: b
Pooling shrinks resolution stage by stage while widening features, like a CNN's pyramid. Report memory and accuracy at several clip lengths to see what it buys.
Check yourself
Video Swin shifts its windows every other layer. Why does it need an attention mask?
Show the answer
Answer: b
Without a mask, the wrapped window mixes the first and last frames, which aren't neighbors.
Check yourself
What limits a windowed video transformer, even with shifted windows?
Show the answer
Answer: b
Windows keep cost down by keeping attention local; long-range structure must be built up layer by layer. Test it with long-range action controls.
Recap
Recap- A non-local block is attention over all space-time positions, with a residual.
- Full attention costs pairs; divided attention ; windows and pooling cut it further.
- Tubelets make fewer tokens and average away fast motion inside them.
- Shifted windows need a mask; attention maps aren't explanations. Use shuffled-frame controls.
Concepts in this lesson