Video Understanding
3D convolutions, two-stream networks and space–time attention.
Ten seconds of video at 30 frames per second is 300 images, most of them nearly identical. A video model must decide what to do with that redundancy: look at a few frames and recognize the scene, compute the motion explicitly, or build time into every layer. Which choice wins has depended less on the architecture than on what the benchmark rewards, and for years the most popular benchmarks rewarded recognizing scenes.
What you’ll learn
- Explain what action-recognition benchmarks measure, and use controlled comparisons to separate scene recognition from temporal modeling.
- Describe two-stream networks and 3-D convolutions, and prove that I3D's inflation preserves a 2-D network's activations on a static video, with the exact condition at the clip's borders.
- Derive the parameter-matched (2+1)D factorization and the relative cost of SlowFast's two pathways, and explain temporal shifts as temporal modeling at zero FLOPs.
- Compute the cost of joint and divided space–time attention, compare TimeSformer's and ViViT's factorizations, and explain why masked video pretraining masks 90% of the input or more.
What video benchmarks measure
Video understanding covers several tasks. Action recognition gives one label to a short trimmed clip; temporal action localization finds the start and end of every action in a long untrimmed video; spatio-temporal action detection also boxes the actor; tracking follows object identities across frames. Recognition has driven architecture research, and its datasets differ in a way that matters more than their size:
| Dataset | Clips | Classes | What decides the label |
|---|---|---|---|
| UCF-101 (Soomro, Zamir & Shah, 2012) | about 13,000, cut from about 2,500 videos | 101 | largely scenes and objects; small, so models are pretrained elsewhere |
| HMDB-51 (Kuehne et al., 2011) | about 6,800 | 51 | similar, and even smaller |
| Kinetics-400 (Kay et al., 2017) | 306,245, about 10 s each, every one from a different YouTube video | 400 | often appearance alone: "playing basketball", "surfing" |
| Something-Something (Goyal et al., 2017) | 108,499 crowd-acted clips, about 4 s on average | 174 caption templates, e.g. "Putting something on something" | the order of frames: some classes differ only in direction |
| AVA (Gu et al., 2018) | clips from movies | atomic actions | who does what, where and when: actions boxed in space and time |
Whether a benchmark needs motion can be tested by holding everything else fixed. Lin, Gan & Han (2019) took the temporal segment network (TSN) baseline, a 2-D ResNet-50 that processes 8 sampled frames independently and averages their outputs, and inserted a temporal shift (the third step) that lets each frame's features mix with its neighbors', at no extra computation:
| Dataset | TSN (per frame) | TSM (temporal shift) | Gain |
|---|---|---|---|
| Kinetics-400 | 70.6 | 74.1 | +3.5 |
| UCF-101 | 91.7 | 95.9 | +4.2 |
| HMDB-51 | 64.7 | 73.5 | +8.8 |
| Something-Something V1 | 20.5 | 47.3 | +26.8 |
| Something-Something V2 | 30.4 | 61.7 | +31.3 |
Attention models show the same split. TimeSformer with attention over space only, frame by frame, reached 76.9% on Kinetics-400 but 36.6% on Something-Something V2, where attention over time as well reached 59.5% (Bertasius, Wang & Torresani, 2021). The bias is a property of the data, and it is learned: models trained on scene-correlated labels lean on the scene, which Choi et al. (2019) countered by training against scene prediction and against predicting the action when the person is masked out. It persists in video–language benchmarks too: with large-scale pretraining and a frame ensemble at test time, a model trained on single frames beat earlier multi-frame models on text-to-video retrieval and video question answering (Lei, Berg & Bansal, 2023).
You want to show that your new video benchmark requires temporal modeling. Which evidence is most convincing?
- The benchmark has more classes than Kinetics
- A large accuracy gain of a temporal model over an otherwise identical per-frame model on that benchmark
- The best published model reaches only modest accuracy on it
- Its clips are longer than those of other benchmarks
Show the answer
B. A large accuracy gain of a temporal model over an otherwise identical per-frame model on that benchmark
The TSN/TSM comparison isolates time: same backbone, frames and training, with and without temporal mixing. A gain of 3.5 points on Kinetics against 31.3 on Something-Something V2 says how much each benchmark depends on the order of frames; a hard benchmark is not necessarily a temporal one.
Two streams and inflated kernels
Two-stream networks (Simonyan & Zisserman, 2014) make motion an explicit input. A spatial stream classifies a single RGB frame; a temporal stream classifies a stack of optical flow fields, the horizontal and vertical displacement of every pixel between consecutive frames, for frame pairs, so its input has channels. The two streams' class scores are fused by averaging or by a linear SVM. On UCF-101, averaged over its three splits, the flow stream alone was more accurate than the RGB stream (83.7% against 73.0%) and the fusion reached 88.0%; on HMDB-51 the numbers were 54.6%, 40.5% and 59.4%. The flow had to be computed beforehand with a classical method and stored: 1.5 TB for UCF-101, which JPEG compression cut to 27 GB.
3-D convolutions learn motion features instead, with kernels of size that slide over time too. Comparing temporal kernel depths, C3D (Tran et al., 2015) found best throughout, and trained a network of 8 convolutional layers on 16-frame clips of pixels. But 3-D networks trained from scratch had no ImageNet to learn from. I3D (Carreira & Zisserman, 2017) took a pretrained 2-D network, inception with batch normalization, and inflated each kernel into (and most pooling windows likewise), together with Kinetics, a dataset large enough to train them. The inflation rule is chosen so that a video that does not move behaves exactly like the image it was copied from.
Suppose a layer's input is constant in time, for every tap. The temporal taps of the inflated kernel then see the same frame, and their weights sum to the 2-D kernel:
The right side is the 2-D layer's output and does not depend on , so the output is again constant in time. Pointwise nonlinearities keep a constant sequence constant, max or average pooling over time of equal values returns that value, spatial pooling acts frame by frame as in 2-D, and a temporal stride subsamples a constant sequence. By induction over the layers, every activation equals its 2-D counterpart.
Zero padding breaks the first step at the borders: taps read zeros instead of , so only of the equal terms remain. Repeating the border frames makes every tap read again.
import torch
import torch.nn as nn
def inflate(conv2d: nn.Conv2d, kt: int) -> nn.Conv3d:
"""I3D inflation: repeat a pretrained k x k kernel kt times along time and divide by kt."""
conv3d = nn.Conv3d(conv2d.in_channels, conv2d.out_channels, (kt, *conv2d.kernel_size),
stride=(1, *conv2d.stride), padding=(kt // 2, *conv2d.padding),
bias=conv2d.bias is not None)
with torch.no_grad():
conv3d.weight.copy_(conv2d.weight.unsqueeze(2).repeat(1, 1, kt, 1, 1) / kt)
if conv2d.bias is not None:
conv3d.bias.copy_(conv2d.bias)
return conv3d
conv2d = nn.Conv2d(3, 8, kernel_size=3, padding=1)
image = torch.randn(1, 3, 32, 32)
video = image.unsqueeze(2).repeat(1, 1, 16, 1, 1) # a "boring" clip: 16 copies of one image
out = inflate(conv2d, kt=3)(video) # (1, 8, 16, 32, 32)
print(torch.allclose(out[:, :, 1:-1], conv2d(image).unsqueeze(2).expand(-1, -1, 14, -1, -1), atol=1e-5)) # True
print(torch.allclose(out[:, :, 0], conv2d(image), atol=1e-5)) # False: zero padding in time at the borderThe interior frames match the 2-D output exactly; the first and last frames, with one tap in the zero padding, get of the response plus the bias. Note that padding_mode='replicate' would not repair this alone: it would also replace the spatial zero padding the 2-D network was trained with.
The fixed point only guarantees a sensible start: inflated filters respond to a static clip as the image network did, and training on video then teaches them motion. Pretrained on Kinetics, a two-stream I3D, with inflated networks on RGB and on flow, reached about 98% on UCF-101 and 81% on HMDB-51, and Kinetics pretraining became the default for video models. Explicit motion still helped: in I3D's comparison on Kinetics, with ImageNet-pretrained networks, adding the flow stream to the RGB network raised accuracy from 71.1% to 74.2%.
Factorizing space and time
A full kernel couples space and time in one linear map. Tran et al. (2018) factorized it: a spatial convolution from to channels, a ReLU, then a temporal convolution to channels. To compare fairly with full 3-D convolutions they chose so that the parameters match:
A full 3-D convolution from to channels, without biases, has
The factorized pair has a spatial part and a temporal part:
Setting the two equal and rounding down gives the rule of R(2+1)D:
With instead, the pair would keep only of the parameters, for . Matched, it has the same parameters and twice the nonlinearities, and R(2+1)D networks reached lower training and test error than 3-D ResNets of the same depth, which points to easier optimization rather than more capacity.
A convolution maps 64 channels to 64. How many intermediate channels does the parameter-matched (2+1)D factorization use?
Show the answer
144
. Check: , exactly the full kernel's . The intermediate layer is wider than the block's input and output.
Two designs spend almost nothing on time. The temporal shift module (TSM; Lin, Gan & Han, 2019) moves part of a 2-D network's channels along the time axis: inside each residual branch, 1/8 of the channels take their values from the previous frame and 1/8 from the next, while the shortcut keeps the unshifted features. A shift is pure data movement, no FLOPs and no parameters, yet the convolutions after it now mix three neighboring frames, which produced the gains in the table of the first step. Shifting more channels hurt the spatial features, and shifting only from past frames gives an online model that never waits for the future.
SlowFast (Feichtenhofer et al., 2019) splits the network into two pathways that read the same clip. The Slow pathway takes frames sampled 16 apart from 64, at full width, with temporal convolutions only in its last two stages: early layers see too small a region for frames 16 apart to be correlated. The Fast pathway takes times as many frames with of the channels, temporal kernels in every block and no temporal downsampling, and lateral connections feed it into the Slow pathway at every stage. The authors compare the split to the primate retina, where about 15–20% of ganglion cells are magnocellular, sensitive to fast change but not to color or fine detail.
Attention over space and time
A video transformer cuts each of frames into patches, or cuts the clip into tubelets of frames by pixels, a 3-D patch embedding that yields tokens. Full self-attention over all of them scales with the square of the token count, so every published design factorizes it somehow. TimeSformer (Bertasius, Wang & Torresani, 2021) compared five schemes on the same ViT:
Joint space–time attention compares each patch with every patch of the clip, plus the class token, so each layer computes about scores:
Divided attention applies two attentions in sequence within each block, first over the patches at the same position in all frames, then over the patches of the same frame, each with the class token:
For 8 frames of in patches, and tokens. Divided attention needs its own query, key and value projections for the temporal step, so it has more parameters (121.4 million against 85.9 million) at a far lower cost:
| Attention | Kinetics-400 | Something-Something V2 |
|---|---|---|
| space only (frame by frame) | 76.9 | 36.6 |
| joint space–time | 77.4 | 58.5 |
| divided: time, then space | 78.0 | 59.5 |
| sparse local and global | 75.9 | 56.3 |
| axial: time, width, height | 73.5 | 56.2 |
ViViT (Arnab et al., 2021) studied the factorizations with tubelets on a ViT-Base. To start from an image ViT it initialized the 3-D patch embedding either by inflation, , or by central frame initialization, at the middle temporal position and zeros elsewhere, so that at initialization the model behaves like sampling one frame per tubelet: 79.2% on Kinetics-400 against 77.6% for inflation and 73.2% for random initialization. Among the architectures, joint attention over all tokens was the most accurate (80.0%), while a factorized encoder, a spatial transformer per frame followed by a temporal transformer over the frames' class tokens, scored 78.8% with 62% of the FLOPs and ran 3.4 times faster; averaging the frames' class tokens instead of the temporal transformer gave 75.8%. That joint attention won here but lost in TimeSformer is a reminder that such rankings depend on backbone, tokenization, data and recipe.
Self-supervised pretraining needs a harder task for video than for images. VideoMAE (Tong et al., 2022) masks 90 to 95% of the space–time patches and reconstructs them, using the same spatial mask in every frame (tube masking): neighboring frames are so similar that a random mask would let the model copy a missing patch from the next frame. Pretrained this way, plain ViTs learned well even from small datasets such as HMDB-51, with about 3.5 thousand training videos, and when pretraining and target data differed, the quality of the pretraining data mattered more than its quantity. Masked Modeling Across Modalities compares masking ratios across images, video, speech and text.
Key takeaways
- Video benchmarks differ in how much of the label one frame reveals: under a controlled comparison, temporal modeling adds 3.5 points on Kinetics-400 but 26.8 to 31.3 on Something-Something, and space-only attention scores 76.9% on Kinetics-400 against 36.6% on Something-Something V2.
- Two-stream networks add stacked optical flow ( channels) and gained most from it on UCF-101 (flow 83.7%, RGB 73.0%, fused 88.0%); 3-D convolutions learn motion features, with kernels best in C3D's comparison.
- I3D inflates pretrained kernels as repeated times, which reproduces every 2-D activation on a static clip, exactly for output frames away from zero-padded clip borders, which get of the response.
- The parameter-matched (2+1)D factorization uses channels (144 for at ) and trains to lower error; TSM shifts a quarter of the channels in time at zero FLOPs; SlowFast's Fast pathway costs of the Slow pathway per layer of the same shape.
- Joint space–time attention needs keys per query and divided attention , 7.6 times fewer for 8 frames of 196 patches; TimeSformer found divided attention best while ViViT found joint attention most accurate and a factorized encoder 3.4 times faster, and VideoMAE pretrains with 90–95% tube masking because neighboring frames are redundant.
Check yourself
In the interactive lesson these are scored with your confidence, and what you miss comes back for review.
I3D inflates a pretrained kernel into by repeating it three times along time. Why are the repeated weights divided by 3?
- To keep the weight norm small for stable training
- So that on a static video the three taps sum to the original response, and the inflated network reproduces the image network's activations
- So that the temporal kernel sums to zero and responds only to motion
- To average away the noise between consecutive frames
Show the answer
B. So that on a static video the three taps sum to the original response, and the inflated network reproduces the image network's activations
On a boring video every temporal tap sees the same frame, so the three copies of add up to exactly : the boring-video fixed point. Without the division every inflated layer would triple its pre-activations, and the pretrained network's behavior would be lost.
R(2+1)D replaces every convolution by a spatial convolution into channels, a ReLU, and a temporal convolution, with chosen to match the parameter count. What did Tran et al. (2018) find, compared with full 3-D ResNets of the same depth?
- Lower training and test error: the factorized networks were easier to optimize, with twice as many nonlinearities
- The same accuracy with half the parameters
- Lower test error but higher training error, a regularization effect
- Worse accuracy, because factorized kernels cannot represent motion
Show the answer
A. Lower training and test error: the factorized networks were easier to optimize, with twice as many nonlinearities
The parameter counts were matched, so the gain is not capacity. Training error was lower as well as test error, which points to easier optimization; the extra ReLU between the spatial and temporal parts doubles the number of nonlinearities.
SlowFast's Fast pathway reads times as many frames as the Slow pathway with of its channels. For a convolution of the same shape in both pathways, how do their multiply–adds compare?
- The Fast layer costs 8 times as much
- They cost the same, since
- The Fast layer costs 1/8 as much
- The Fast layer costs 1/64 as much
Show the answer
C. The Fast layer costs 1/8 as much
A convolution's cost grows with the number of frames and with the product of its input and output widths, both multiplied by : . Temporal kernels in every Fast block raised its share in the paper's models to about 20% of the total.
In TimeSformer's divided space–time attention, with frames of patches and a class token, how many keys does each query attend to in one block?
Show the answer
D.
in the temporal attention, over the patches at the same position in every frame plus the class token, then in the spatial attention over the patches of the same frame: in all, against for joint attention. For and that is 206 against 1,569.
Further reading
- K. Simonyan & A. Zisserman, "Two-stream convolutional networks for action recognition in videos," NeurIPS (2014).
- J. Carreira & A. Zisserman, "Quo vadis, action recognition? A new model and the Kinetics dataset," CVPR (2017).
- D. Tran, H. Wang, L. Torresani, J. Ray, Y. LeCun & M. Paluri, "A closer look at spatiotemporal convolutions for action recognition," CVPR (2018).
- C. Feichtenhofer, H. Fan, J. Malik & K. He, "SlowFast networks for video recognition," ICCV (2019).
- G. Bertasius, H. Wang & L. Torresani, "Is space-time attention all you need for video understanding?," ICML (2021).