AI Logic

Architectures

I3D

Updated:

Architecture of I3D. The upper left shows the two 3D streams (64 frames of RGB and of optical flow are each fed into their own I3D, and their predictions are averaged at test time). The lower left shows the inflation method, in which a 2D filter is repeated N times along the time axis and scaled by 1/N to make it 3D. The right shows the layer sequence of the inflated Inception-v1 (from a 7×7×7 convolution, through two pooling layers that do not shrink time, 2, 5, and 2 Inception modules, and 2×7×7 average pooling, to a 1×1×1 convolution)

Background and overview

I3D (Two-Stream Inflated 3D ConvNet) is a video recognition model that inflates the filters and pooling of a 2D CNN with a proven track record in image classification (Inception-v1) along the time axis into 3D, and reuses the weights trained on ImageNet as they are. It is used for each of two streams, RGB and optical flow. It was presented at CVPR in 2017 by Carreira and Zisserman of DeepMind and the University of Oxford.

The paper also answers another question. For images, models pre-trained on ImageNet proved very useful for other tasks, but does the same happen for video? Earlier action recognition datasets (UCF-101 and HMDB-51) had only about 10,000 videos, too small both for pre-training and for comparing models. The paper therefore compared five representative model types under the same conditions on the newly built Kinetics (400 classes, at least 400 clips each) and examined the effect of pre-training on Kinetics.

The key points of I3D are the following two.

  • Bringing 2D designs and weights into 3D: earlier 3D CNNs (such as C3D) had many parameters and could not use ImageNet weights, so shallow networks of about 8 layers were trained from scratch. I3D inflates the deep Inception-v1 into 3D and initializes it with ImageNet weights.
  • Two streams help even in 3D: a 3D CNN should be able to learn motion from RGB, yet adding an optical flow stream improved results substantially further.

The main results of the paper are as follows.

  • Two-stream I3D pre-trained on Kinetics reached 98.0% on UCF-101 and 80.9% on HMDB-51 (average over 3 splits), far above the previous best (94.6% and 70.3%).
  • Pre-training on Kinetics helped every model type, but its effect was especially large for 3D CNNs.

These results established the practice of pre-training video models on Kinetics before using them for other tasks. I3D features are widely used as input to MS-TCN for action segmentation and in long-video tasks such as ActivityNet.

Theory: inflating a 2D CNN along the time axis

Inflating filters and pooling (Section 2.4)

A time dimension is added to the filters and pooling kernels of an image-classification network. A square N×NN \times N filter becomes a cubic N×N×NN \times N \times N one. This way, image network designs refined through trial and error can be used directly as video networks.

Initializing 3D filters with 2D weights

Duplicating a single image many times in a row creates a "boring video" with no motion. The paper sets the weights of the 3D filters so that feeding this boring video gives the same output as feeding the original image into the 2D network. Specifically, the 2D filter weights W2DW^{2D} are repeated NN times along the time axis and divided by NN.

W3D(t,y,x)=1NW2D(y,x),t=1,…,NW^{3D}(t, y, x) = \frac{1}{N} W^{2D}(y, x), \qquad t = 1, \dots, N

In a boring video, the frame at every time step is the same image XX, so the output of the 3D convolution equals the 2D output as follows.

∑t=1N1N(W2D∗X)=W2D∗X\sum_{t=1}^{N} \frac{1}{N} \left( W^{2D} * X \right) = W^{2D} * X

If the convolution output is constant along time, the outputs of ReLU and pooling are also the same as in the 2D case, so the output of the whole network matches the 2D network. In this way, the 3D network is implicitly pre-trained on ImageNet.

How the temporal receptive field grows

The boring-video condition alone does not determine temporal pooling or strides. Image networks treat height and width equally, but if the temporal receptive field grows faster than the spatial one, it may merge edges of different objects, and if it grows too slowly, it may fail to capture scene dynamics. For videos at 25 frames per second, the paper chose the following.

  1. The first two max-pooling layers do not shrink time (1 × 3 × 3, temporal stride 1).
  2. The other max-pooling layers use the same size and stride for height, width, and time.
  3. The final average pooling is 2 × 7 × 7.

Inflated Inception-v1 (Figure 3 of the paper; right side of the figure at the top of the article)

StageLayers
EntryConv 7×7×7 (stride 2) → MaxPool 1×3×3 (stride 1, 2, 2) → Conv 1×1×1 → Conv 3×3×3 → MaxPool 1×3×3 (stride 1, 2, 2)
MiddleInception module × 2 → MaxPool 3×3×3 (stride 2) → Inception module × 5 → MaxPool 2×2×2 (stride 2) → Inception module × 2
ExitAvgPool 2×7×7 → Conv 1×1×1 (class scores) → average over time

An Inception module concatenates the outputs of four branches: a 1×1×1 convolution, 1×1×1 → 3×3×3 convolutions (2 branches), and 3×3×3 max pooling → 1×1×1 convolution. Each convolution is followed by Batch Normalization and ReLU (except the final layer that outputs class scores).

Two 3D streams

An I3D that takes RGB and an I3D that takes optical flow (TV-L1) are trained separately. At test time, the two predictions are averaged (left side of the figure at the top of the article). A 3D CNN should be able to learn motion from RGB, but the paper showed that adding flow still improves results substantially. The paper suggests this may be because computing flow involves iterative optimization, making it "recurrent," so it carries information that a purely feed-forward 3D CNN lacks.

Training and testing (Sections 2.4 to 2.5)

  • Input: 64-frame snippets during training (2.56 seconds at 25 frames per second). Videos are resized so the shorter side is 256, a 224 × 224 crop is taken at random, the starting frame is also chosen at random, and frames are flipped horizontally. Short videos are looped as many times as needed.
  • Optimization: SGD with momentum 0.9. On Kinetics, training runs for 110k steps on 32 GPUs, and the learning rate is reduced by a factor of 10 when the validation loss stops decreasing.
  • Testing: the model is applied convolutionally over the whole video (250 frames in evaluation) with a center 224 × 224 crop, and predictions are averaged over time.

Why inflation works

1. Deep, yet with few parameters

A 3D CNN close to C3D has 79 million parameters, while two-stream I3D has 25 million for both streams combined (Table 1 of the paper). I3D keeps its parameter count low relative to its depth through Inception's 1×1×1 convolutions and branching. Fewer parameters also make overfitting on small datasets less likely.

2. It can start from ImageNet weights

Training a 3D CNN from scratch requires a large amount of video. Because I3D transfers ImageNet weights to 3D under the boring-video condition, it starts out already able to recognize appearance and can move on to learning motion. In the paper's experiments on Kinetics, ImageNet pre-training helped in every case, with an especially large effect on the RGB stream (top-1 for RGB I3D went from 68.4% to 71.1%).

3. It looks at a long time span in fine detail

I3D trains on 64 frames at 25 frames per second and sees every frame at test time. The paper cites this high temporal resolution as the reason I3D features transfer better than those of other model types. The idea is that for models that only look sparsely, video is not very different from ImageNet images, so they gain little from pre-training on a video dataset.

As an everyday analogy, rather than teaching someone who already knows how to look at photos (the ImageNet weights) how to look at a "stack" of photos arranged in time from scratch, it extends their way of looking at photos directly to the stack.

Looking at the first-layer filters (7 × 7 × 7) of I3D trained on Kinetics, the RGB network's filters had developed rich temporal structure and had changed so much that they were unrecognizable compared with the original Inception-v1 filters. The flow network's filters, by contrast, stayed close to the original ImageNet filters (Figure 4 of the paper).

Experimental results

The paper implemented five model types (ConvNet + LSTM, a 3D ConvNet close to C3D, Two-Stream, two-stream with 3D fusion, and two-stream I3D), all built on Inception-v1 (except the 3D ConvNet, which has its own design), and compared them.

Model size and temporal extent (Table 1 of the paper)

ModelParametersTraining inputTime seen in trainingTime seen at test
ConvNet + LSTM9M25 RGB frames5 s10 s
3D ConvNet79M16 RGB frames0.64 s9.6 s
Two-Stream12M1 RGB, 10 flow0.4 s10 s
3D-fused39M5 RGB, 50 flow2 s10 s
Two-stream I3D25M64 RGB, 64 flow2.56 s10 s

Comparison by dataset (top-1 accuracy. UCF-101 and HMDB-51 are split 1. Table 2 of the paper)

ModelUCF-101 (RGB + flow)HMDB-51 (RGB + flow)Kinetics (RGB)Kinetics (flow)Kinetics (RGB + flow)
ConvNet + LSTM81.0 (RGB)36.0 (RGB)63.3――
3D ConvNet51.6 (RGB)24.3 (RGB)56.1――
Two-Stream91.258.362.252.465.6
3D-fused89.356.8――67.2
Two-stream I3D93.466.471.163.474.2

I3D gave the best results on every dataset and every input. On Kinetics, flow-only accuracy is much lower than RGB. The paper attributes this to the large amount of camera motion in Kinetics videos, which makes the motion stream harder.

Effect of pre-training on Kinetics (split 1. "Original" is trained only on UCF-101 or HMDB-51, "Fixed" trains only the last layer on top of Kinetics features, and "Full" fine-tunes the whole network from Kinetics pre-training. Left value with ImageNet weights, right value without. Table 4 of the paper)

ModelUCF-101 OriginalUCF-101 FixedUCF-101 FullHMDB-51 OriginalHMDB-51 FixedHMDB-51 Full
ConvNet + LSTM81.0 / 54.288.1 / 82.691.0 / 86.836.0 / 18.350.8 / 47.153.4 / 49.7
3D ConvNet― / 51.6― / 76.0― / 79.9― / 24.3― / 47.0― / 49.4
Two-Stream91.2 / 83.693.9 / 93.394.2 / 93.858.3 / 47.166.6 / 65.966.6 / 64.3
3D-fused89.3 / 69.594.3 / 89.894.2 / 91.556.8 / 37.369.9 / 64.671.0 / 66.5
Two-stream I3D93.4 / 88.897.7 / 97.498.0 / 97.666.4 / 62.279.7 / 78.681.2 / 81.3

Every model improved with pre-training on Kinetics, with especially large gains for I3D and the 3D ConvNet. Even in the "Fixed" setting, training only the last layer, I3D far exceeded its original training.

Comparison with prior work (average over 3 splits. Excerpt from Table 5 of the paper)

MethodUCF-101HMDB-51
Two-Stream88.059.4
TSN94.269.4
ST-ResNet + IDT94.670.3
C3D (3 networks + IDT, pre-trained on Sports-1M)90.1―
RGB I3D (pre-trained on ImageNet + Kinetics)95.674.8
Flow I3D (pre-trained on ImageNet + Kinetics)96.777.1
Two-stream I3D (pre-trained on ImageNet + Kinetics)98.080.7
Two-stream I3D (pre-trained on Kinetics only)97.880.9

Compared with the previous best (ST-ResNet + IDT), errors were reduced by 63% on UCF-101 and 35% on HMDB-51.

Caveats in practice

When using I3D, keep the following points in mind.

  • Heavy computation: applying 3D convolutions to 64 frames makes it much heavier than a 2D CNN. In measurements from the TSM paper, I3D with a 3D ResNet-50 took 306 GFLOPs and 165.3 ms per video on a P100, about 10 times an 8-frame TSM (33 GFLOPs, 17.4 ms).
  • Flow must be computed separately: the two-stream form that gives the best accuracy requires TV-L1 optical flow to be computed in advance. Even RGB-only I3D reaches 95.6% on UCF-101 with Kinetics pre-training.
  • The temporal receptive field is set by hand: the design that does not shrink time in the first two pooling layers was chosen for videos at 25 frames per second. For videos with very different frame rates, the receptive field grows differently.

The paper's numbers come from pre-training on the original Kinetics (Kinetics-400). Kinetics distributes the locations of YouTube videos, so videos disappear and the number obtainable decreases year by year. When trying to reproduce the same pre-training, the training data will be smaller than in the paper, and the numbers may not match. When comparing, check which version of the data was used for pre-training.

Comparison with other models

Comparing video action recognition models from the same angles gives the following.

ModelConvolutionsImageNet weightsFrames seen at onceOptical flowCompute
Two-Stream ConvNets2DUsed in the spatial stream1 RGB frame + 10 flow pairsRequiredLight (flow is separate)
C3D3D (custom 8 layers)Cannot be used16Not neededHeavy
TSN2DUsed1 snippet per segmentUsed (RGB-only also possible)Light (flow is separate)
I3D3D (inflated Inception-v1)Used after inflation64Used (RGB-only also possible)Heavy
TSM2D (shifts channels)Used8 to 16Not neededSame as a 2D CNN
SlowFast3D (partly 2D)Not used (training from scratch is comparable)4 + 32 (a 64-frame clip)Not neededModerate

Rough guidelines for choosing are as follows.

  • For general-purpose video features: I3D features trained on Kinetics are widely used in downstream tasks such as action segmentation, for example as input to MS-TCN.
  • To bring 2D CNN assets into 3D: the inflation method also works for networks other than Inception-v1 (such as ResNet).
  • To keep compute down: consider TSM, which reaches similar accuracy at 2D cost, or SlowFast, which does not use flow. In the SlowFast paper, it outperforms two-stream I3D (75.7%) on Kinetics-400 (75.6 to 79.8% with RGB only).

PyTorch example

Show code
python
import torch
import torch.nn as nn

"""
The core of I3D (Carreira & Zisserman 2017, Section 2.4): an example implementation that inflates 2D layers into 3D
Input: (N, C, T, H, W)
"""


def inflate_conv(conv2d: nn.Conv2d, time_dim: int, time_stride: int = 1) -> nn.Conv3d:
  """Inflate a 2D convolution (k×k) into 3D (time_dim×k×k).
  The weights are repeated time_dim times along time and divided by time_dim (giving the same output as 2D on a boring video)."""
  kh, kw = conv2d.kernel_size
  conv3d = nn.Conv3d(
    conv2d.in_channels, conv2d.out_channels,
    kernel_size=(time_dim, kh, kw),
    stride=(time_stride, *conv2d.stride),
    padding=(time_dim // 2, *conv2d.padding),
    bias=conv2d.bias is not None,
  )
  with torch.no_grad():
    w = conv2d.weight.unsqueeze(2).repeat(1, 1, time_dim, 1, 1) / time_dim
    conv3d.weight.copy_(w)
    if conv2d.bias is not None:
      conv3d.bias.copy_(conv2d.bias)
  return conv3d


def inflate_bn(bn2d: nn.BatchNorm2d) -> nn.BatchNorm3d:
  """BN only changes shape (the learned statistics and affine weights are copied as is)."""
  bn3d = nn.BatchNorm3d(bn2d.num_features, eps=bn2d.eps, momentum=bn2d.momentum)
  bn3d.load_state_dict(bn2d.state_dict())
  return bn3d


# Check: on a "boring video" made by repeating the same image T times, 2D and 3D outputs match
conv2d = nn.Conv2d(3, 64, kernel_size=7, stride=2, padding=3)   # stands in for a first layer trained on ImageNet
conv3d = inflate_conv(conv2d, time_dim=7, time_stride=2)         # I3D entry: 7×7×7, stride 2
image = torch.randn(1, 3, 224, 224)
boring_video = image.unsqueeze(2).repeat(1, 1, 16, 1, 1)         # (1, 3, 16, 224, 224)
# The temporal edges are affected by zero padding, so compare at the central time step
out2d = conv2d(image)
out3d = conv3d(boring_video)
print(torch.allclose(out2d, out3d[:, :, out3d.shape[2] // 2], atol=1e-5))   # True

# The first two max-pooling layers do not shrink time (1×3×3, stride 1, 2, 2)
pool_early = nn.MaxPool3d(kernel_size=(1, 3, 3), stride=(1, 2, 2), padding=(0, 1, 1))
# The two streams are trained separately, and their predictions (softmax probabilities) are averaged at test time
# prob = (rgb_i3d(rgb).softmax(1) + flow_i3d(flow).softmax(1)) / 2

FAQ

Q. What does "inflating" mean in I3D?
A.

It means adding a time dimension to the filters and pooling of an image-classification 2D CNN, turning N×NN \times N into N×N×NN \times N \times N. The weights are made by repeating the 2D weights NN times along time and dividing by NN. Then, when a motionless video made by repeating the same image is fed in, the output is the same as the original 2D network, so weights trained on ImageNet can be used directly as the initial values in 3D.

Q. Why use an optical flow stream even though it is a 3D CNN?
A.

A 3D CNN should be able to learn motion from RGB, but in the paper's experiments, adding a flow stream improved results substantially (on Kinetics, RGB only 71.1% → RGB + flow 74.2%). The paper suggests this may be because optical flow algorithms perform iterative optimization, a "recurrent" computation, giving well-structured motion information that a purely feed-forward 3D CNN lacks.

Q. Which is better, I3D or C3D?
A.

In the paper's comparison, I3D was far better (on Kinetics, 71.1% for RGB I3D versus 56.1% for the RGB model close to C3D). The paper attributes this to I3D being deeper yet having fewer parameters, starting from ImageNet weights, and being trained on 4 times longer videos at twice the spatial resolution. That said, C3D has a simple structure and needs no flow, so it is still used as a convenient source of video features.

References

  1. Carreira, J., & Zisserman, A. (2017). Quo Vadis, Action Recognition? A New Model and the Kinetics Dataset. CVPR 2017. arXiv:1705.07750.
  2. Kay, W., Carreira, J., Simonyan, K., et al. (2017). The Kinetics Human Action Video Dataset. arXiv:1705.06950.
  3. Lin, J., Gan, C., & Han, S. (2019). TSM: Temporal Shift Module for Efficient Video Understanding. ICCV 2019. arXiv:1811.08383.
  4. Feichtenhofer, C., Fan, H., Malik, J., & He, K. (2019). SlowFast Networks for Video Recognition. ICCV 2019. arXiv:1812.03982.