Non-local Neural Networks

Xiaolong Wang, Ross Girshick, Abhinav Gupta, Kaiming He

Introduction

Capturing long-range dependencies is of central importance in deep neural networks. For sequential data (e.g., in speech, language), recurrent operations are the dominant solution to long-range dependency modeling. For image data, long-distance dependencies are modeled by the large receptive fields formed by deep stacks of convolutional operations .

Convolutional and recurrent operations both process a local neighborhood, either in space or time; thus long-range dependencies can only be captured when these operations are applied repeatedly, propagating signals progressively through the data. Repeating local operations has several limitations. First, it is computationally inefficient. Second, it causes optimization difficulties that need to be carefully addressed . Finally, these challenges make multi-hop dependency modeling, e.g., when messages need to be delivered back and forth between distant positions, difficult.

In this paper, we present non-local operations as an efficient, simple, and generic component for capturing long-range dependencies with deep neural networks. Our proposed non-local operation is a generalization of the classical non-local mean operation in computer vision. Intuitively, a non-local operation computes the response at a position as a weighted sum of the features at all positions in the input feature maps (Figure 1). The set of positions can be in space, time, or spacetime, implying that our operations are applicable for image, sequence, and video problems.

There are several advantages of using non-local operations: (a) In contrast to the progressive behavior of recurrent and convolutional operations, non-local operations capture long-range dependencies directly by computing interactions between any two positions, regardless of their positional distance; (b) As we show in experiments, non-local operations are efficient and achieve their best results even with only a few layers (e.g., 5); (c) Finally, our non-local operations maintain the variable input sizes and can be easily combined with other operations (e.g., convolutions as we will use).

We showcase the effectiveness of non-local operations in the application of video classification. In videos, long-range interactions occur between distant pixels in space as well as time. A single non-local block, which is our basic unit, can directly capture these spacetime dependencies in a feedforward fashion. With a few non-local blocks, our architecures called non-local neural networks are more accurate for video classification than 2D and 3D convolutional networks (including the inflated variant ). In addition, non-local neural networks are more computationally economical than their 3D convolutional counterparts. Comprehensive ablation studies are presented on the Kinetics and Charades datasets. Using RGB only and without any bells and whistles (e.g., optical flow, multi-scale testing), our method achieves results on par with or better than the latest competitions winners on both datasets.

To demonstrate the generality of non-local operations, we further present object detection/segmentation and pose estimation experiments on the COCO dataset . On top of the strong Mask R-CNN baseline , our non-local blocks can increase accuracy on all three tasks at a small extra computational cost. Together with the evidence on videos, these image experiments show that non-local operations are generally useful and can become a basic building block in designing deep neural networks.

Related Work

Non-local means is a classical filtering algorithm that computes a weighted mean of all pixels in an image. It allows distant pixels to contribute to the filtered response at a location based on patch appearance similarity. This non-local filtering idea was later developed into BM3D (block-matching 3D) , which performs filtering on a group of similar, but non-local, patches. BM3D is a solid image denoising baseline even compared with deep neural networks . Block matching was used with neural networks for image denoising . Non-local matching is also the essence of successful texture synthesis , super-resolution , and inpainting algorithms.

Graphical models.

Long-range dependencies can be modeled by graphical models such as conditional random fields (CRF) . In the context of deep neural networks, a CRF can be exploited to post-process semantic segmentation predictions of a network . The iterative mean-field inference of CRF can be turned into a recurrent network and trained . In contrast, our method is a simpler feedforward block for computing non-local filtering. Unlike these methods that were developed for segmentation, our general-purpose component is applied for classification and detection. These methods and ours are also related to a more abstract model called graph neural networks .

Feedforward modeling for sequences.

Recently there emerged a trend of using feedforward (i.e., non-recurrent) networks for modeling sequences in speech and language . In these methods, long-term dependencies are captured by the large receptive fields contributed by very deep 1-D convolutions. These feedforward models are amenable to parallelized implementations and can be more efficient than widely used recurrent models.

Self-attention.

Our work is related to the recent self-attention method for machine translation. A self-attention module computes the response at a position in a sequence (e.g., a sentence) by attending to all positions and taking their weighted average in an embedding space. As we will discuss in the next, self-attention can be viewed as a form of the non-local mean , and in this sense our work bridges self-attention for machine translation to the more general class of non-local filtering operations that are applicable to image and video problems in computer vision.

Interaction networks.

Interaction Networks (IN) were proposed recently for modeling physical systems. They operate on graphs of objects involved in pairwise interactions. Hoshen presented the more efficient Vertex Attention IN (VAIN) in the context of multi-agent predictive modeling. Another variant, named Relation Networks , computes a function on the feature embeddings at all pairs of positions in its input. Our method also processes all pairs, as we will explain (f(xi,xj)f(\mathbf{x}_{i},\mathbf{x}_{j}) in Eq.(1)). While our non-local networks are connected to these approaches, our experiments indicate that the non-locality of the model, which is orthogonal to the ideas of attention/interaction/relation (e.g., a network can attend to a local region), is the key to their empirical success. Non-local modeling, a long-time crucial element of image processing (e.g., ), has been largely overlooked in recent neural networks for computer vision.

Video classification architectures.

A natural solution to video classification is to combine the success of CNNs for images and RNNs for sequences . In contrast, feedforward models are achieved by 3D convolutions (C3D) in spacetime, and the 3D filters can be formed by “inflating” pre-trained 2D filters. In addition to end-to-end modeling on raw video inputs, it has been found that optical flow and trajectories can be helpful. Both flow and trajectories are off-the-shelf modules that may find long-range, non-local dependency. A systematic comparison of video architectures can be found in .

Non-local Neural Networks

We first give a general definition of non-local operations and then we provide several specific instantiations of it.

Following the non-local mean operation , we define a generic non-local operation in deep neural networks as:

Here ii is the index of an output position (in space, time, or spacetime) whose response is to be computed and jj is the index that enumerates all possible positions. x\mathbf{x} is the input signal (image, sequence, video; often their features) and y\mathbf{y} is the output signal of the same size as x\mathbf{x}. A pairwise function ff computes a scalar (representing relationship such as affinity) between ii and all jj. The unary function gg computes a representation of the input signal at the position jj. The response is normalized by a factor C(x)\mathcal{C}(\mathbf{x}).

The non-local behavior in Eq.(1) is due to the fact that all positions (j\forall j) are considered in the operation. As a comparison, a convolutional operation sums up the weighted input in a local neighborhood (e.g., i1ji+1i-1\leq j\leq i+1 in a 1D case with kernel size 3), and a recurrent operation at time ii is often based only on the current and the latest time steps (e.g., j=ij=i or i1i-1).

The non-local operation is also different from a fully-connected (fc) layer. Eq.(1) computes responses based on relationships between different locations, whereas fc uses learned weights. In other words, the relationship between xj\mathbf{x}_{j} and xi\mathbf{x}_{i} is not a function of the input data in fc, unlike in non-local layers. Furthermore, our formulation in Eq.(1) supports inputs of variable sizes, and maintains the corresponding size in the output. On the contrary, an fc layer requires a fixed-size input/output and loses positional correspondence (e.g., that from xi\mathbf{x}_{i} to yi\mathbf{y}_{i} at the position ii).

A non-local operation is a flexible building block and can be easily used together with convolutional/recurrent layers. It can be added into the earlier part of deep neural networks, unlike fc layers that are often used in the end. This allows us to build a richer hierarchy that combines both non-local and local information.

2 Instantiations

Next we describe several versions of ff and gg. Interestingly, we will show by experiments (Table 2a) that our non-local models are not sensitive to these choices, indicating that the generic non-local behavior is the main reason for the observed improvements.

For simplicity, we only consider gg in the form of a linear embedding: g(xj)=Wgxjg(\mathbf{x}_{j})=W_{g}\mathbf{x}_{j}, where WgW_{g} is a weight matrix to be learned. This is implemented as, e.g., 1×\times1 convolution in space or 1×\times1×\times1 convolution in spacetime.

Next we discuss choices for the pairwise function ff.

Following the non-local mean and bilateral filters , a natural choice of ff is the Gaussian function. In this paper we consider:

Here xiTxj\mathbf{x}_{i}^{T}\mathbf{x}_{j} is dot-product similarity. Euclidean distance as used in is also applicable, but dot product is more implementation-friendly in modern deep learning platforms. The normalization factor is set as C(x)=jf(xi,xj)\mathcal{C}(\mathbf{x})=\sum_{\forall j}f(\mathbf{x}_{i},\mathbf{x}_{j}).

Embedded Gaussian.

A simple extension of the Gaussian function is to compute similarity in an embedding space. In this paper we consider:

Here θ(xi)=Wθxi\theta(\mathbf{x}_{i})=W_{\theta}\mathbf{x}_{i} and ϕ(xj)=Wϕxj\phi(\mathbf{x}_{j})=W_{\phi}\mathbf{x}_{j} are two embeddings. As above, we set C(x)=jf(xi,xj)\mathcal{C}(\mathbf{x})=\sum_{\forall j}f(\mathbf{x}_{i},\mathbf{x}_{j}).

We note that the self-attention module recently presented for machine translation is a special case of non-local operations in the embedded Gaussian version. This can be seen from the fact that for a given ii, 1C(x)f(xi,xj)\frac{1}{\mathcal{C}(\mathbf{x})}f(\mathbf{x}_{i},\mathbf{x}_{j}) becomes the softmax computation along the dimension jj. So we have y=softmax(xTWθTWϕx)g(x)\mathbf{y}=\text{\emph{softmax}}(\mathbf{x}^{T}W^{T}_{\theta}W_{\phi}\mathbf{x})g(\mathbf{x}), which is the self-attention form in . As such, our work provides insight by relating this recent self-attention model to the classic computer vision method of non-local means , and extends the sequential self-attention network in to a generic space/spacetime non-local network for image/video recognition in computer vision.

Despite the relation to , we show that the attentional behavior (due to softmax) is not essential in the applications we study. To show this, we describe two alternative versions of non-local operations next.

Dot product.

ff can be defined as a dot-product similarity:

Here we adopt the embedded version. In this case, we set the normalization factor as C(x)=N\mathcal{C}(\mathbf{x})=N, where NN is the number of positions in x\mathbf{x}, rather than the sum of ff, because it simplifies gradient computation. A normalization like this is necessary because the input can have variable size.

The main difference between the dot product and embedded Gaussian versions is the presence of softmax, which plays the role of an activation function.

Concatenation.

Concatenation is used by the pairwise function in Relation Networks for visual reasoning. We also evaluate a concatenation form of ff:

Here [,][\cdot,\cdot] denotes concatenation and wf\mathbf{w}_{f} is a weight vector that projects the concatenated vector to a scalar. As above, we set C(x)=N\mathcal{C}(\mathbf{x})=N. In this case, we adopt ReLU in ff.

The above several variants demonstrate the flexibility of our generic non-local operation. We believe alternative versions are possible and may improve results.

3 Non-local Block

We wrap the non-local operation in Eq.(1) into a non-local block that can be incorporated into many existing architectures. We define a non-local block as:

where yi\mathbf{y}_{i} is given in Eq.(1) and “+xi+\mathbf{x}_{i}” denotes a residual connection . The residual connection allows us to insert a new non-local block into any pre-trained model, without breaking its initial behavior (e.g., if WzW_{z} is initialized as zero). An example non-local block is illustrated in Figure 2. The pairwise computation in Eq.(2), (3), or (4) can be simply done by matrix multiplication as shown in Figure 2; the concatenation version in (5) is straightforward.

The pairwise computation of a non-local block is lightweight when it is used in high-level, sub-sampled feature maps. For example, typical values in Figure 2 are T=4T=4, H=W=14H=W=14 or 77. The pairwise computation as done by matrix multiplication is comparable to a typical convolutional layer in standard networks. We further adopt the following implementations that make it more efficient.

We set the number of channels represented by WgW_{g}, WθW_{\theta}, and WϕW_{\phi} to be half of the number of channels in x\mathbf{x}. This follows the bottleneck design of and reduces the computation of a block by about a half. The weight matrix WzW_{z} in Eq.(6) computes a position-wise embedding on yi\mathbf{y}_{i}, matching the number of channels to that of x\mathbf{x}. See Figure 2.

A subsampling trick can be used to further reduce computation. We modify Eq.(1) as: yi=1C(x^)jf(xi,x^j)g(x^j)\mathbf{y}_{i}=\frac{1}{\mathcal{C}(\mathbf{\hat{x}})}\sum_{\forall j}f(\mathbf{x}_{i},\mathbf{\hat{x}}_{j})g(\mathbf{\hat{x}}_{j}), where x^\mathbf{\hat{x}} is a subsampled version of x\mathbf{x} (e.g., by pooling). We perform this in the spatial domain, which can reduce the amount of pairwise computation by 1/4. This trick does not alter the non-local behavior, but only makes the computation sparser. This can be done by adding a max pooling layer after ϕ\phi and gg in Figure 2.

We use these efficient modifications for all non-local blocks studied in this paper.

Video Classification Models

To understand the behavior of non-local networks, we conduct comprehensive ablation experiments on video classification tasks. First we describe our baseline network architectures for this task, and then extend them into 3D ConvNets and our proposed non-local nets.

To isolate the temporal effects of our non-local nets vs. 3D ConvNets, we construct a simple 2D baseline architecture in which the temporal dimension is trivially addressed (i.e., only by pooling).

Table 1 shows our C2D baseline under a ResNet-50 backbone. The input video clip has 32 frames each with 224×\times224 pixels. All convolutions in Table 1 are in essence 2D kernels that process the input frame-by-frame (implemented as 1×<spanclass="katexdisplay"><spanclass="katex"><spanclass="katexmathml"><mathxmlns="http://www.w3.org/1998/Math/MathML"display="block"><semantics><mrow><mi>k</mi></mrow><annotationencoding="application/xtex">k</annotation></semantics></math></span><spanclass="katexhtml"ariahidden="true"><spanclass="base"><spanclass="strut"style="height:0.6944em;"></span><spanclass="mordmathnormal"style="marginright:0.0315em;">k</span></span></span></span></span>×\times<span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mrow><mi>k</mi></mrow><annotation encoding="application/x-tex">k</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.6944em;"></span><span class="mord mathnormal" style="margin-right:0.0315em;">k</span></span></span></span></span>\timeskk kernels). This model can be directly initialized from the ResNet weights pre-trained on ImageNet. A ResNet-101 counterpart is built in the same way.

The only operation involving the temporal domain are the pooling layers. In other words, this baseline simply aggregates temporal information.

Inflated 3D ConvNet (I3D).

As done in , one can turn the C2D model in Table 1 into a 3D convolutional counterpart by “inflating” the kernels. For example, a 2D k<spanclass="katexdisplay"><spanclass="katex"><spanclass="katexmathml"><mathxmlns="http://www.w3.org/1998/Math/MathML"display="block"><semantics><mrow><mo>×</mo></mrow><annotationencoding="application/xtex">×</annotation></semantics></math></span><spanclass="katexhtml"ariahidden="true"><spanclass="base"><spanclass="strut"style="height:0.6667em;verticalalign:0.0833em;"></span><spanclass="mord">×</span></span></span></span></span>kk<span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mrow><mo>×</mo></mrow><annotation encoding="application/x-tex">\times</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.6667em;vertical-align:-0.0833em;"></span><span class="mord">×</span></span></span></span></span>k kernel can be inflated as a 3D t<spanclass="katexdisplay"><spanclass="katex"><spanclass="katexmathml"><mathxmlns="http://www.w3.org/1998/Math/MathML"display="block"><semantics><mrow><mo>×</mo></mrow><annotationencoding="application/xtex">×</annotation></semantics></math></span><spanclass="katexhtml"ariahidden="true"><spanclass="base"><spanclass="strut"style="height:0.6667em;verticalalign:0.0833em;"></span><spanclass="mord">×</span></span></span></span></span>k<spanclass="katexdisplay"><spanclass="katex"><spanclass="katexmathml"><mathxmlns="http://www.w3.org/1998/Math/MathML"display="block"><semantics><mrow><mo>×</mo></mrow><annotationencoding="application/xtex">×</annotation></semantics></math></span><spanclass="katexhtml"ariahidden="true"><spanclass="base"><spanclass="strut"style="height:0.6667em;verticalalign:0.0833em;"></span><spanclass="mord">×</span></span></span></span></span>kt<span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mrow><mo>×</mo></mrow><annotation encoding="application/x-tex">\times</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.6667em;vertical-align:-0.0833em;"></span><span class="mord">×</span></span></span></span></span>k<span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mrow><mo>×</mo></mrow><annotation encoding="application/x-tex">\times</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.6667em;vertical-align:-0.0833em;"></span><span class="mord">×</span></span></span></span></span>k kernel that spans tt frames. This kernel can be initialized from 2D models (pre-trained on ImageNet): each of the tt planes in the t<spanclass="katexdisplay"><spanclass="katex"><spanclass="katexmathml"><mathxmlns="http://www.w3.org/1998/Math/MathML"display="block"><semantics><mrow><mo>×</mo></mrow><annotationencoding="application/xtex">×</annotation></semantics></math></span><spanclass="katexhtml"ariahidden="true"><spanclass="base"><spanclass="strut"style="height:0.6667em;verticalalign:0.0833em;"></span><spanclass="mord">×</span></span></span></span></span>k<spanclass="katexdisplay"><spanclass="katex"><spanclass="katexmathml"><mathxmlns="http://www.w3.org/1998/Math/MathML"display="block"><semantics><mrow><mo>×</mo></mrow><annotationencoding="application/xtex">×</annotation></semantics></math></span><spanclass="katexhtml"ariahidden="true"><spanclass="base"><spanclass="strut"style="height:0.6667em;verticalalign:0.0833em;"></span><spanclass="mord">×</span></span></span></span></span>kt<span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mrow><mo>×</mo></mrow><annotation encoding="application/x-tex">\times</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.6667em;vertical-align:-0.0833em;"></span><span class="mord">×</span></span></span></span></span>k<span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mrow><mo>×</mo></mrow><annotation encoding="application/x-tex">\times</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.6667em;vertical-align:-0.0833em;"></span><span class="mord">×</span></span></span></span></span>k kernel is initialized by the pre-trained k<spanclass="katexdisplay"><spanclass="katex"><spanclass="katexmathml"><mathxmlns="http://www.w3.org/1998/Math/MathML"display="block"><semantics><mrow><mo>×</mo></mrow><annotationencoding="application/xtex">×</annotation></semantics></math></span><spanclass="katexhtml"ariahidden="true"><spanclass="base"><spanclass="strut"style="height:0.6667em;verticalalign:0.0833em;"></span><spanclass="mord">×</span></span></span></span></span>kk<span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mrow><mo>×</mo></mrow><annotation encoding="application/x-tex">\times</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.6667em;vertical-align:-0.0833em;"></span><span class="mord">×</span></span></span></span></span>k weights, rescaled by 1/t1/t. If a video consists of a single static frame repeated in time, this initialization produces the same results as the 2D pre-trained model run on a static frame.

We study two cases of inflations: we either inflate the 3×\times3 kernel in a residual block to 3×\times3×\times3 (similar to ), or the first 1×\times1 kernel in a residual block to 3×\times1×\times1 (similar to ). We denote these as I3D3×3×3 and I3D3×1×1. As 3D convolutions are computationally intensive, we only inflate one kernel for every 2 residual blocks; inflating more layers shows diminishing return. We inflate conv1 to 5×\times7×\times7.

The authors of have shown that I3D models are more accurate than their CNN+LSTM counterparts.

Non-local network.

We insert non-local blocks into C2D or I3D to turn them into non-local nets. We investigate adding 1, 5, or 10 non-local blocks; the implementation details are described in the next section in context.

1 Implementation Details

Our models are pre-trained on ImageNet . Unless specified, we fine-tune our models using 32-frame input clips. These clips are formed by randomly cropping out 64 consecutive frames from the original full-length video and then dropping every other frame. The spatial size is 224×\times224 pixels, randomly cropped from a scaled video whose shorter side is randomly sampled in $$ pixels, following . We train on an 8-GPU machine and each GPU has 8 clips in a mini-batch (so in total with a mini-batch size of 64 clips). We train our models for 400k iterations in total, starting with a learning rate of 0.01 and reducing it by a factor of 10 at every 150k iterations (see also Figure 4). We use a momentum of 0.9 and a weight decay of 0.0001. We adopt dropout after the global pooling layer, with a dropout ratio of 0.5. We fine-tune our models with BatchNorm (BN) enabled when it is applied. This is in contrast to common practice of fine-tuning ResNets, where BN was frozen. We have found that enabling BN in our application reduces overfitting.

We adopt the method in to initialize the weight layers introduced in the non-local blocks. We add a BN layer right after the last 1×\times1×\times1 layer that represents WzW_{z}; we do not add BN to other layers in a non-local block. The scale parameter of this BN layer is initialized as zero, following . This ensures that the initial state of the entire non-local block is an identity mapping, so it can be inserted into any pre-trained networks while maintaining its initial behavior.

Inference.

Following we perform spatially fully-convolutional inference on videos whose shorter side is rescaled to 256. For the temporal domain, in our practice we sample 10 clips evenly from a full-length video and compute the softmax scores on them individually. The final prediction is the averaged softmax scores of all clips.

Experiments on Video Classification

We perform comprehensive studies on the challenging Kinetics dataset . We also report results on the Charades dataset to show the generality of our models.

Kinetics contains \scriptstyle\sim246k training videos and 20k validation videos. It is a classification task involving 400 human action categories. We train all models on the training set and test on the validation set.

Figure 4 shows the curves of the training procedure of a ResNet-50 C2D baseline vs. a non-local C2D with 5 blocks (more details in the following). Our non-local C2D model is consistently better than the C2D baseline throughout the training procedure, in both training and validation error.

Figure 1 and Figure 3 visualize several examples of the behavior of a non-local block computed by our models. Our network can learn to find meaningful relational clues regardless of the distance in space and time.

Table 2 shows the ablation results, analyzed as follows:

Table 2a compares different types of a single non-local block added to the C2D baseline (right before the last residual block of res4). Even adding one non-local block can lead to \scriptstyle\sim1% improvement over the baseline.

Interestingly, the embedded Gaussian, dot-product, and concatenation versions perform similarly, up to some random variations (72.7 to 72.9). As discussed in Sec. 3.2, the non-local operations with Gaussian kernels become similar to the self-attention module . However, our experiments show that the attentional (softmax) behavior of this module is not the key to the improvement in our applications; instead, it is more likely that the non-local behavior is important, and it is insensitive to the instantiations.

In the rest of this paper, we use the embedded Gaussian version by default. This version is easier to visualize as its softmax scores are in the range of $$.

Which stage to add non-local blocks?

Table 2b compares a single non-local block added to different stages of ResNet. The block is added to right before the last residual block of a stage. The improvement of a non-local block on res2, res3, or res4 is similar, and on res5 is slightly smaller. One possible explanation is that res5 has a small spatial size (7×\times7) and it is insufficient to provide precise spatial information. More evidence of a non-local block exploiting spatial information will be investigated in Table 2d.

Going deeper with non-local blocks.

Table 2c shows the results of more non-local blocks. We add 1 block (to res4), 5 blocks (3 to res4 and 2 to res3, to every other residual block), and 10 blocks (to every residual block in res3 and res4) in ResNet-50; in ResNet-101 we add them to the corresponding residual blocks. Table 2c shows that more non-local blocks in general lead to better results. We argue that multiple non-local blocks can perform long-range multi-hop communication. Messages can be delivered back and forth between distant positions in spacetime, which is hard to do via local models.

It is noteworthy that the improvement of non-local blocks is not just because they add depth to the baseline model. To see this, we note that in Table 2c the non-local 5-block ResNet-50 model has 73.8 accuracy, higher than the deeper ResNet-101 baseline’s 73.1. However, the 5-block ResNet-50 has only \scriptstyle\sim70% parameters and \scriptstyle\sim80% FLOPs of the ResNet-101 baseline, and is also shallower. This comparison shows that the improvement due to non-local blocks is complementary to going deeper in standard ways.

We have also tried to add standard residual blocks, instead of non-local blocks, to the baseline models. The accuracy is not increased. This again shows that the improvement of non-local blocks is not just because they add depth.

Non-local in spacetime.

Our method can naturally handle spacetime signals. This is a nice property: related objects in a video can present at distant space and long-term time interval, and their dependency can be captured by our model.

In Table 2d we study the effect of non-local blocks applied along space, time, or spacetime. For example, in the space-only version, the non-local dependency only happens within the same frame: i.e., in Eq.(1) it only sums over the index jj in the same frame of the index ii. The time-only version can be set up similarly. Table 2d shows that both the space-only and time-only versions improve over the C2D baseline, but are inferior to the spacetime version.

Non-local net vs. 3D ConvNet.

Table 2e compares our non-local C2D version with the inflated 3D ConvNets. Non-local operations and 3D convolutions can be seen as two ways of extending C2D to the temporal dimensions.

Table 2e also compares the number of parameters and FLOPs, relative to the baseline. Our non-local C2D model is more accurate than the I3D counterpart (e.g., 75.1 vs. 74.4), while having a smaller number of FLOPs (1.2×\times vs. 1.5×\times). This comparison shows that our method can be more effective than 3D convolutions when used alone.

Non-local 3D ConvNet.

Despite the above comparison, non-local operations and 3D convolutions can model different aspects of the problem: 3D convolutions can capture local dependency. Table 2f shows the results of inserting 5 non-local blocks into the I3D3×1×1 models. These non-local I3D (NL I3D) models improve over their I3D counterparts (+1.6 point accuracy), showing that non-local operations and 3D convolutions are complementary.

Longer sequences.

Finally we investigate the generality of our models on longer input videos. We use input clips consisting of 128 consecutive frames without subsampling. The sequences throughout all layers in the networks are thus 4×\times longer compared to the 32-frame counterparts. To fit this model into memory, we reduce the mini-batch size to 2 clips per GPU. As a result of using small mini-batches, we freeze all BN layers in this case. We initialize this model from the corresponding models trained with 32-frame inputs. We fine-tune on 128-frame inputs using the same number of iterations as the 32-frame case (though the mini-batch size is now smaller), starting with a learning rate of 0.0025. Other implementation details are the same as before.

Table 2g shows the results of 128-frame clips. Comparing with the 32-frame counterparts in Table 2f, all models have better results on longer inputs. We also find that our NL I3D can maintain its gain over the I3D counterparts, showing that our models work well on longer sequences.

Comparisons with state-of-the-art results.

Table 3 shows the results from the I3D authors and from the Kinetics 2017 competition winner . We note that these are comparisons of systems which can differ in many aspects. Nevertheless, our method surpasses all the existing RGB or RGB + flow based methods by a good margin. Without using optical flow and without any bells and whistles, our method is on par with the heavily engineered results of the 2017 competition winner.

2 Experiments on Charades

Charades is a video dataset with \scriptstyle\sim8k training, \scriptstyle\sim1.8k validation, and \scriptstyle\sim2k testing videos. It is a multi-label classification task with 157 action categories. We use a per-category sigmoid output to handle the multi-label property.

We initialize our models pre-trained on Kinetics (128-frame). The mini-batch size is set to 1 clip per GPU. We train our models for 200k iterations, starting from a learning rate of 0.00125 and reducing it by 10 every 75k iterations. We use a jittering strategy similar to that in Kinetics to determine the location of the 224×\times224 cropping window, but we rescale the video such that this cropping window outputs 288×\times288 pixels, on which we fine-tune our network. We test on a single scale of 320 pixels.

Table 4 shows the comparisons with the previous results on Charades. The result of is the 2017 competition winner in Charades, which was also fine-tuned from models pre-trained in Kinetics. Our I3D baseline is higher than previous results. As a controlled comparison, our non-local net improves over our I3D baseline by 2.3% on the test set.

Extension: Experiments on COCO

We also investigate our models on static image recognition. We experiment on the Mask R-CNN baseline for COCO object detection/segmentation and human pose estimation (keypoint detection). The models are trained on COCO train2017 (i.e., trainval35k in 2014) and tested on val2017 (i.e., minival in 2014).

We modify the Mask R-CNN backbone by adding one non-local block (right before the last residual block of res4). All models are fine-tuned from ImageNet pre-training. We evaluate on a standard baseline of ResNet-50/101 and a high baseline of ResNeXt-152 (X152) . Unlike the original paper that adopted stage-wise training regarding RPN, we use an improved implementation with end-to-end joint training similar to , which leads to higher baselines than .

Table 5 shows the box and mask AP on COCO. We see that a single non-local block improves all R50/101 and X152 baselines, on all metrics involving detection and segmentation. APbox{}^{\text{box}} is increased by \scriptstyle\sim1 point in all cases (e.g., +1.3 point in R101). Our non-local block is complementary to increasing the model capacity, even when the model is upgraded from R50/101 to X152. This comparison suggests that non-local dependency has not been sufficiently captured by existing models despite increased depth/capacity.

In addition, the above gain is at a very small cost. The single non-local block only adds <<5% computation to the baseline model. We also have tried to use more non-local blocks to the backbone, but found diminishing return.

Keypoint detection.

Next we evaluate non-local blocks in Mask R-CNN for keypoint detection. In , Mask R-CNN used a stack of 8 convolutional layers for predicting the keypoints as 1-hot masks. These layers are local operations and may overlook the dependency among keypoints across long distance. Motivated by this, we insert 4 non-local blocks into the keypoint head (after every 2 convolutional layers).

Table 6 shows the results on COCO. On a strong baseline of R101, adding 4 non-local blocks to the keypoint head leads to a \scriptstyle\sim1 point increase of keypoint AP. If we add one extra non-local block to the backbone as done for object detection, we observe an in total 1.4 points increase of keypoint AP over the baseline. In particular, we see that the stricter criterion of AP75 is boosted by 2.4 points, suggesting a stronger localization performance.

Conclusion

We presented a new class of neural networks which capture long-range dependencies via non-local operations. Our non-local blocks can be combined with any existing architectures. We show the significance of non-local modeling for the tasks of video classification, object detection and segmentation, and pose estimation. On all tasks, a simple addition of non-local blocks provides solid improvement over baselines. We hope non-local layers will become an important component of future network architectures.

Acknowledgement: This work was partially supported by ONR MURI N000141612007, Sloan, Okawa Fellowship to AG and NVIDIA Fellowship to XW. We would also like to thank Haoqi Fan, Du Tran, Heng Wang, Georgia Gkioxari and Piotr Dollar for many helpful discussions.

References