Video classification: convolution with a time axis
A video is photos stacked in time order. What makes it different from a photo is motion. Step through how a filter that also slides along time catches the motion you miss when you look at frames one by one.
Short answer. Video classification puts one label on 4D data made of stacked frames (time × height × width × channels). Looking at each frame with a 2D CNN and averaging (late fusion) is easy but cannot see which way things move. 3D convolution slides the filter along time too, two-stream networks look at motion separately as optical flow, I3D inflates 2D filters into 3D, and SlowFast splits the work into a slow path and a fast path.
| filter · mean of each frame's max | → clip | ← clip | still clip |
|---|
The top row is the input video and the bottom row is this step's filter output. In the output, green is positive, orange is negative and darker is larger. Orange borders show the input cells where the filter has a number, and the one output cell made by multiplying and adding them. The largest value in each output frame, averaged over frames, is the clip's score in the table. The bold green number is the clip you picked. The video is made up.
Try this. (1) As it starts (→), press "Next" once. The 2D filter gives 4 for the → clip, the ← clip and the still clip alike. (2) Press once more. The time difference filter gives 1, 1, 0. It separates still from moving, but → and ← are the same. (3) Press to the end. The → filter gives 3 and the ← filter 1, so the decision is "right". Press "← left" and you get 1, 3 → "left". "still" gives 1, 1 → "no direction".
Definitions
The text below explains with the small video in the lab. Here are the definitions of the ideas this page uses. Let T be the number of frames, and H, W, C the height, width and number of channels.
- frame, clip
- A frame is one H × W × C image in a video, and a video is the 4D array made by stacking frames x₁, …, xT in time order. A clip is a short run of consecutive frames cut from a video.
- late fusion
- Applying the same 2D classifier f to each frame on its own and combining the per-frame scores at the end. This page combines them by averaging. The sum does not depend on order, so the order of frames is not used.
- 3D convolution
- A convolution whose kernel, with kₜ cells in time and k cells in space, slides along time, height and width, and at each position multiplies kₜ × k × k × C input values by weights and adds them up. With padding pₜ and stride sₜ, the number of output frames is below. Height and width follow the same form.
- optical flow
- A record of which way and how far each place in the image moved between two consecutive frames. Computed for every cell, it is called dense optical flow.
- two-stream
- An architecture with two ConvNets side by side: a spatial stream that takes one frame's appearance as input, and a temporal stream that takes stacked dense optical flow between several frames. The scores of the two streams are combined to classify.
- inflation
- Turning a 2D filter learned on photos into a 3D filter by adding a time axis. I3D repeats the 2D filter's weights N times along time and divides by N. Then on a video made of the same photo stacked N times, it gives the same value as the 2D filter on the photo.
1. What a video is: 4D data made of stacked frames
A photo is a 3D block of numbers: height × width × channels (3 channels for color). A video stacks such photos, called frames, in time order, so it is 4D: time × height × width × channels. The video in the experiment has 6 frames of 6 × 6 cells in 1 gray channel, so 6 × 6 × 6 × 1 numbers. A 224 × 224 color video at 30 frames per second, for example, is 30 × 224 × 224 × 3 = 4,515,840 numbers per second. A short piece cut from a video is called a clip.
What sets it apart from photo classification is motion. The same frames in a different order are a different action. The → clip and the ← clip in the experiment are the very same 6 frames laid out in reverse, yet the answers are "right" and "left". The video classification in the sources on this page is mostly action recognition: telling what a person in the video is doing.
2. The simplest way: a 2D CNN on each frame, then average
Run an image CNN that already works well on each frame and average the per-frame scores, and you have a score for the video. Because the per-frame answers are combined at the very end, this is called late fusion.
video score ŷ = (f(x₁) + f(x₂) + … + f(x_T)) ÷ T
xₜ: frame number t (one photo).f: a 2D CNN that takes one photo and gives a score. The same one is used for every frame.T: the number of frames.ŷ: the score for the whole video.
Addition ignores order. However you shuffle the frames, ŷ stays the same. So two clips that differ only in order get the same score whatever f you use. That is why → and ← both get 4 in step 2 of the experiment. If one frame is enough to see the answer (what is in the picture), this may do. If direction or order decides the answer, it will not.
3. 3D convolution: the filter slides along time too
In a 2D convolution a small filter (kernel) slides across and down a photo, multiplying and adding. A 3D convolution gives the filter a time axis, say 3 × 3 × 3, and slides it along time as well as across and down. Then one output cell sees neighboring cells in the previous and next frames at once. The C3D paper reports that 3D ConvNets learn spatiotemporal features, features that hold time and space together, better than 2D ConvNets, and that a uniform design with small 3 × 3 × 3 kernels in every layer was the best of the ones they tried.
By hand. In the → clip, the dot is at columns 1, 2, 3 at t = 1, 2, 3. The slanted → filter has a 1 at "one cell left in the previous frame, the middle now, one cell right in the next frame". Center it at (t = 2, column 2) and its three cells are (1, 1), (2, 2), (3, 3), all on the dot: 1 + 1 + 1 = 3. In the ← clip the dot is at columns 4, 3, 2 at t = 1, 2, 3, so wherever you put the same filter, at most one cell is on the dot: 1.
Output size. The time axis uses the same formula as 2D. Count each axis on its own.
T' = ⌊(T + 2pₜ − kₜ) ÷ sₜ⌋ + 1
H' = ⌊(H + 2p − k) ÷ s⌋ + 1 (W' has the same form)
T, H, W: the input's number of frames, height and width.T', H', W': the output's number of frames, height and width.kₜ, k: the filter's size in time and in space (both 3 for 3 × 3 × 3).pₜ, p: padding in time and in space (how many rings of 0 go around the edge).sₜ, s: stride in time and in space (how many cells the filter moves each time).⌊ ⌋: drop what is after the decimal point.
The experiment has T = 6, kₜ = 3, pₜ = 0, sₜ = 1, so T' = (6 + 0 − 3) ÷ 1 + 1 = 4 frames. In space p = 1, so H' = (6 + 2 − 3) + 1 = 6 and the size stays the same. For another example, 8 frames × 32 × 32 with a 3 × 3 × 3 filter, padding 1 and stride 2 on every axis gives T' = ⌊7 ÷ 2⌋ + 1 = 4 and H' = W' = ⌊31 ÷ 2⌋ + 1 = 16, so 4 × 16 × 16. Pooling that shrinks the time axis follows the same formula.
Cost. With C input channels, one 2D filter has k × k × C weights and one 3D filter has kₜ × k × k × C, which is kₜ times as many (with C = 64 and 3 × 3, that is 576 versus 1,728). The multiplications for one output cell also go up kₜ times.
4. Two-stream: look at appearance and motion separately
Instead of letting the network find motion on its own, you can compute motion separately and hand it in. Optical flow records, between a frame and the next, which way and how far each place in the picture moved. When it is computed for every cell it is called dense optical flow. The two-stream design of Simonyan and Zisserman puts two networks side by side.
The paper reports that a ConvNet trained on stacked dense optical flow works well even with little training data, and evaluates on the action recognition datasets UCF-101 and HMDB-51. Because motion is handed in directly as optical flow, the network does not have to discover something like the experiment's slanted filter from scratch. In exchange, optical flow has to be computed separately.
5. I3D: inflate 2D filters learned on photos into 3D
Learning a 3D network from scratch takes a large video dataset. On the photo side there are already 2D networks trained well on ImageNet. I3D by Carreira and Zisserman inflates a 2D image ConvNet into 3D by adding a time axis, so it reuses ImageNet architectures and weights. I3D is also a two-stream model. The same paper introduces the Kinetics dataset of 400 human action classes, and after pre-training on Kinetics reaches 80.9% on HMDB-51 and 98.0% on UCF-101.
One way to inflate a 2D filter into 3D, written as a formula:
W3D[t][y][x] = W2D[y][x] ÷ N (t = 1, 2, …, N)
W2D: a 2D filter learned on photos.y, xare the row and column inside the filter.W3D: the inflated 3D filter. It has one more index, the time cellt.N: how many time cells were added (kₜ).
Apply W3D to a still video made of the same photo stacked N times, and you add the same value N times divided by N, which is exactly what W2D gives on the photo (this page's self-check confirms it with the experiment's dot-finding filter). So the inflated network starts with what the photo network learned, and learns the time side by training further on video. This is what the I3D paper does (section 2.4): it repeats the 2D filter weights N times along the time axis and divides by N, so that a "boring video" of one repeated photo gives the same answer as the photo network.
6. SlowFast: slow and sparse, fast and dense
SlowFast by Feichtenhofer et al. looks through two paths. The Slow pathway takes frames sparsely at a low frame rate and captures spatial content (what is there). The Fast pathway takes frames densely at a high frame rate and captures fine motion. The Fast pathway can be made very light by reducing its channels.
τ: the gap at which the Slow pathway picks frames: one frame every τ frames.α: how many times denser the Fast pathway picks than the Slow pathway. The Fast pathway takes one frame every τ ÷ α frames.
With the figure's values, from a 32-frame clip the Slow pathway sees 32 ÷ 8 = 4 frames and the Fast pathway 32 ÷ 2 = 16. In terms of the experiment, "what is there" can be seen from a few frames, like the 2D filter, while "where it goes" needs neighboring frames seen densely, like the slanted filter.
7. Which to use when
| Option | Good | Bad | When |
|---|---|---|---|
| 2D CNN on each frame + average (late fusion) | Uses an image model as it is. Lighter than 3D | Cannot see order or direction (experiment: → and ← are the same) | When one frame shows the answer. A baseline to measure first |
| 3D CNN (C3D) | One filter sees time and space together. Learns spatiotemporal features better than 2D | kₜ times the weights and multiplications | When motion decides the answer |
| Two-stream | Hands in motion directly as optical flow. Works well even with little training data | Optical flow must be computed separately. Two networks | When training data is small |
| I3D | Reuses ImageNet architectures and weights. After Kinetics pre-training, HMDB-51 80.9%, UCF-101 98.0% | Heavy because it is 3D, and as a two-stream model it also needs optical flow | When you want to start from a pre-trained 2D network |
| SlowFast | Splits appearance and motion into two pathways with different frame rates. The Fast pathway is light with fewer channels | Two pathways, and hyperparameters such as τ and α to choose | When fine motion matters |
The good and bad points come from what was checked in the sources below and from this page's experiment and formulas. The "When" column is a judgment drawn from those.
Where it is used
- Action recognition. Tell what a person in a video is doing. The sources on this page evaluate on action recognition datasets such as UCF-101, HMDB-51 and Kinetics (400 human action classes).
- Video classification with little data. Take a network pre-trained on a large dataset (Kinetics) and fit it to a small one: transfer learning (section 5).
Frequently asked
Can I classify each frame with an image model and average?
You can. That is the simplest late fusion. If one frame is enough to see the answer, it may work. But an average ignores the order of the frames, so it cannot tell apart two videos that differ only in order (like sitting down and standing up). In the experiment on this page the → clip and the ← clip got the same 2D score, 4.
Why not read the frames in order with an RNN or LSTM?
If you feed per-frame features from a 2D CNN into an RNN or LSTM in order, it does see the order, unlike an average. The sources on this page cover 3D convolution and two-stream networks and do not compare against RNNs, so no numbers are given here.
What if I have little training data?
The sources give two answers. The two-stream paper reports that a ConvNet trained on stacked dense optical flow works well even with little training data. The I3D paper pre-trains on the large Kinetics dataset (400 human action classes) and then reaches 80.9% on HMDB-51 and 98.0% on UCF-101. Moving what was learned on big data to small data is transfer learning.
Words
| Plain words used here | Textbook term |
|---|---|
| one photo of a video · a short piece | frame · clip |
| telling what a person in a video is doing | action recognition, video classification |
| look at each frame on its own, combine at the end | late fusion |
| a filter that also slides along time | 3D convolution, 3D kernel |
| features holding time and space together | spatiotemporal features |
| which way and how far each place moved between frames | optical flow, dense optical flow |
| one network for appearance and one for motion | two-stream, spatial stream · temporal stream |
| adding a time axis to a 2D filter to make it 3D | inflation, I3D |
| the sparse path · the dense, light path | Slow pathway · Fast pathway |
| frames per second | frame rate |
| learn on big data first · carry it over | pre-training · transfer learning |
| the 0s around the edge · cells moved each time | padding · stride |
Sources
- Simonyan, Zisserman. Two-Stream Convolutional Networks for Action Recognition in Videos, 2014 (two streams: one network for the appearance of still frames and one for motion between frames; a ConvNet trained on stacked dense optical flow works well even with little training data; evaluated on UCF-101 and HMDB-51).
- Tran et al. Learning Spatiotemporal Features with 3D Convolutional Networks (C3D), 2014 (3D ConvNets learn spatiotemporal features better than 2D ConvNets; a uniform design with 3 × 3 × 3 kernels in every layer).
- Carreira, Zisserman. Quo Vadis, Action Recognition? A New Model and the Kinetics Dataset (I3D), 2017 (Kinetics with 400 human action classes; a two-stream model that inflates 2D ConvNets into 3D to reuse ImageNet architectures and weights; HMDB-51 80.9%, UCF-101 98.0%).
- Feichtenhofer et al. SlowFast Networks for Video Recognition, 2018 (a Slow pathway at a low frame rate for spatial content, a Fast pathway at a high frame rate for fine motion, a light Fast pathway with fewer channels).