Back to paper library
Paper notesarXiv 2016CVPR 2017

Xception

Where Inception Ends

Original paper

Xception: Deep Learning with Depthwise Separable Convolutions

Paper authors

François Chollet

Publication
CVPR 2017Conference
First submitted to arXiv
arXiv:1610.02357
Cite this paper · BibTeX

The citation identifies the linked source version. It cites the original paper and its authors.

@misc{classiccnnsxception2016,
  title = {{Xception: Deep Learning with Depthwise Separable Convolutions}},
  author = {François Chollet},
  year = {2016},
  eprint = {1610.02357},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/1610.02357}
}
On this page

By 2016, Inception was already in its fourth generation, from GoogLeNet, through Inception-v2 with BN added and Inception-v3 with factorized convolutions, all the way to Inception-ResNet with residuals wired in. Every generation ranked near the top on ImageNet, but the modules grew more complicated each time, with branches, channel counts, and kernel shapes all needing to be tuned one by one.

François Chollet, the creator of Keras, was at Google at the time. He wanted to understand why Inception modules work, how they differ from ordinary convolutions, and where things could go after Inception.

This paper is his answer.

Defining the Problem

In an ordinary convolutional layer, each kernel is a 3D filter, with a channel dimension on top of width and height. A 3×33\times3 kernel looking at 256 input channels has 3×3×2563\times3\times256 weights, and it handles at the same time the relationships between different channels at the same position (cross-channel correlations) and the relationships between neighboring positions (spatial correlations).

The Inception module handles these two things separately. It first uses a few 1×11\times1 convolutions to deal with cross-channel relationships, projecting the input into 3 or 4 smaller spaces, then uses 3×33\times3 or 5×55\times5 convolutions within each smaller space to deal with spatial relationships.

The author calls the idea behind this design the Inception hypothesis, namely that cross-channel correlations and spatial correlations are independent enough that handling them separately works better than handling them together.

If “partly separating them” works, what about separating them more thoroughly?

Solving the Problem

Inception from Another Angle

The author first simplifies the Inception module, keeping only one kind of convolution, 3×33\times3, and removing the average-pooling branch. That leaves three branches, each a 1×11\times1 followed by a 3×33\times3.

These three 1×11\times1 convolutions all look at the same input, so they can be merged into a single 1×11\times1 convolution with more output channels. After the merge, each of the three 3×33\times3 convolutions handles its own segment of those output channels:

The equivalent form of the simplified Inception module, from bottom to top: Input goes into one 1x1 conv, whose Output channels are split by braces into three segments, each feeding its own 3x3 conv, and finally Concat
Figure 3 from the paper, excerpted from page 2 of the v3 PDF. A strictly equivalent reformulation of the simplified Inception module.

The two forms are exactly equivalent. PyTorch's groups=3 means exactly “split the channels into three segments and convolve each one separately,” so this simplified Inception module is really just a 1×11\times1 convolution plus a 3×33\times3 convolution split into three groups.

Seen this way, the simplified Inception module has only one number left to tune, the number of segments the channels are split into.

Splitting All the Way

The more segments, the fewer channels in each. Taken to the extreme, each segment has just one channel, and every output channel gets a 3×33\times3 convolution of its own:

The extreme version of the Inception module, from bottom to top: Input goes into one 1x1 conv, the output channels are split into many segments, each feeding a single 3x3, and finally Concat
Figure 4 from the paper, excerpted from page 2 of the v3 PDF. An “extreme” version of the Inception module, with one spatial convolution per output channel of the 1×11\times1 convolution.

Fix the channels at 96 and raise the number of segments from 1 to 96: the 3×33\times3 layer's weight shape goes from (96, 96, 3, 3) to (96, 1, 3, 3), and its parameters drop from 82,944 to 864. The second number in the weight shape is how many input channels each kernel can see. groups=1 is an ordinary convolution, with every kernel seeing all 96 channels; at groups=96, each kernel sees only one channel, leaving just the spatial 3×33\times3.

The author views this whole series as a spectrum. At one end is an ordinary convolution (preceded by a 1×11\times1), with a single segment; at the other end, there is one segment per channel. Inception modules sit in between, splitting a few hundred channels into 3 or 4 segments.

The Other End of the Spectrum

The author points out that the extreme module at the other end of the spectrum is almost identical to an operation that already existed, the depthwise separable convolution. It works in two steps:

  1. Depthwise convolution: each input channel gets its own spatial convolution, with no exchange between channels.
  2. Pointwise convolution: a 1×11\times1 convolution projects the previous step's output onto a new channel space.

This is what TensorFlow and Keras call a separable convolution. Image processing also uses the name separable convolution, for spatial factorizations such as splitting a 7×77\times7 into a 1×71\times7 and a 7×17\times1, which Inception-v3 used; don't mix the two up.

There are still two small differences between the “extreme” Inception module and the depthwise separable convolution:

  1. Order. A depthwise separable convolution does the depthwise step first, then the 1×11\times1; Inception does the 1×11\times1 first. The author considers this unimportant, because networks are built by stacking layers, so one module's 1×11\times1 is followed by the next module's depthwise step, and the order differs only at the two ends.
  2. Whether there's a nonlinearity in between. In Inception, both steps are followed by ReLU; a depthwise separable convolution usually has no activation function between its two steps. The author thinks this one might matter and leaves it for the experiments.

Xception

Since the Inception hypothesis says that “cross-channel and spatial relationships can be partly separated,” the author goes all the way with a stronger hypothesis, that the two can be completely separated. The network based on this hypothesis is built entirely from depthwise separable convolutions and is named Xception, after Extreme Inception.

The Xception architecture in three columns. Entry flow: 299x299x3 images pass through Conv 32 3x3 stride 2 and Conv 64 3x3, then three modules, each with two SeparableConv layers (128, 256, and 728 channels) plus MaxPooling 3x3 stride 2, with a Conv 1x1 stride 2 shortcut alongside, outputting 19x19x728. Middle flow: three layers of ReLU plus SeparableConv 728, wrapped by an identity shortcut, repeated 8 times. Exit flow: SeparableConv 728, SeparableConv 1024, and MaxPooling, with a Conv 1x1 stride 2 shortcut alongside; then SeparableConv 1536, SeparableConv 2048, and GlobalAveragePooling to get 2048-dimensional vectors, optional fully connected layers, and finally Logistic regression
Figure 5 from the paper, excerpted from page 5 of the v3 PDF. Every Conv and SeparableConv is followed by BN (omitted from the figure), and the depthwise part of SeparableConv doesn't expand the channels.

Data moves through three sections from left to right. The entry flow starts from a 299×299299\times299 image; the first two layers are ordinary convolutions, followed by three modules, each of which halves the feature map and widens the channels, ending at 19×19×72819\times19\times728. The middle flow repeats the same module 8 times without changing the size. The exit flow shrinks it once more and raises the channels to 2048, and global average pooling is followed by the classification layer.

The whole network has 36 convolutional layers organized into 14 modules, and every module except the first and the last is wrapped by a residual connection. Modules that don't shrink the feature map use an identity shortcut; those that do put a stride-2 1×11\times1 convolution on the shortcut.

Compared with Inception-v3, this diagram is much simpler, just depthwise separable convolutions and residual connections stacked layer by layer; the author says it takes only 30 to 40 lines in Keras. Let's build it in PyTorch following the figure:

Python
import torch
import torch.nn as nn

class SeparableConv(nn.Sequential):
    """Per-channel 3×3 convolution followed by a 1×1 convolution; no activation function in between, BN at the end"""
    def __init__(self, cin, cout):
        super().__init__(
            nn.Conv2d(cin, cin, 3, padding=1, groups=cin, bias=False),   # depthwise
            nn.Conv2d(cin, cout, 1, bias=False),                          # pointwise
            nn.BatchNorm2d(cout))

class Block(nn.Module):
    def __init__(self, cin, widths, relu_first=True, downsample=True):
        super().__init__()
        layers, c = [], cin
        for i, w in enumerate(widths):
            if relu_first or i > 0:
                layers.append(nn.ReLU())
            layers.append(SeparableConv(c, w))
            c = w
        if downsample:
            layers.append(nn.MaxPool2d(3, 2, padding=1))
            self.shortcut = nn.Sequential(nn.Conv2d(cin, c, 1, 2, bias=False), nn.BatchNorm2d(c))
        else:
            self.shortcut = nn.Identity()
        self.body = nn.Sequential(*layers)

    def forward(self, x):
        return self.body(x) + self.shortcut(x)

def conv_bn_relu(cin, cout, stride):
    return nn.Sequential(nn.Conv2d(cin, cout, 3, stride, bias=False), nn.BatchNorm2d(cout), nn.ReLU())

entry = nn.Sequential(conv_bn_relu(3, 32, 2), conv_bn_relu(32, 64, 1),
                      Block(64, [128, 128], relu_first=False), Block(128, [256, 256]), Block(256, [728, 728]))
middle = nn.Sequential(*[Block(728, [728, 728, 728], downsample=False) for _ in range(8)])
exit_ = nn.Sequential(Block(728, [728, 1024]),
                      SeparableConv(1024, 1536), nn.ReLU(), SeparableConv(1536, 2048), nn.ReLU(),
                      nn.AdaptiveAvgPool2d(1), nn.Flatten(), nn.Linear(2048, 1000))
model = nn.Sequential(entry, middle, exit_)

x = torch.randn(1, 3, 299, 299)
for name, part in [("entry flow", entry), ("middle flow", middle)]:
    x = part(x)
    print(f"After {name}:", tuple(x.shape[1:]))
print("After exit flow:", tuple(exit_(x).shape[1:]))

params = sum(p.numel() for p in model.parameters())
print(f"Parameters: {params:,}")
Text
After entry flow: (728, 19, 19)
After middle flow: (728, 19, 19)
After exit flow: (1000,)
Parameters: 22,855,952

The parameter count is exactly the 22,855,952 reported in the paper. PyTorch has no ready-made separable convolution layer; as above, a 3×33\times3 convolution with groups set to the number of input channels, followed by a 1×11\times1 convolution, does the job. Keras has SeparableConv2D built in, and Xception itself is included in Keras Applications.

Following the details in the figure, each SeparableConv has a ReLU in front and BN behind, with nothing between the depthwise and pointwise steps. The first SeparableConv of the entry flow's first residual module has no ReLU in front, because the Conv 64 before it already ends with a ReLU; in the last two layers of the exit flow, the ReLU comes after.

Discussion

The author compares against Inception-v3 because the two have almost the same number of parameters, so any gap can't be attributed to model capacity. The comparison is run on two datasets:

  • ImageNet: single-label classification over 1000 classes.
  • JFT: an internal Google dataset of over 350 million high-resolution images in 17,000 classes, where each image can have multiple labels. Evaluation uses a separate set of 14,000 images, FastEval14k, averaging 36.5 labels per image; the metric is MAP@100 over the top 100 predictions, weighted by how common each class is on social media.

Both networks use the same training setup, one carefully tuned for Inception-v3, and the author didn't tune it separately for Xception. The one exception is weight decay, where Inception-v3's 4e-5 is clearly poor for Xception, so 1e-5 is used instead. All models are trained on 60 NVIDIA K80 GPUs; each ImageNet run takes about 3 days, and each JFT run over a month, without being trained to convergence.

ImageNet

Table: Excerpted from Table 1 of the paper. Single-model, single-crop results on the validation set. Inception-v3 doesn't use the auxiliary classifier.
NetworkTop-1 accuracyTop-5 accuracy
VGG-160.7150.901
ResNet-1520.7700.933
Inception-v30.7820.941
Xception0.7900.945

Xception is 0.8 percentage points higher than Inception-v3, a small gap. VGG-16 and ResNet-152 are there only for reference.

JFT

Table: Excerpted from Table 2 of the paper, results after 30 million iterations (about a month) of training. “With fully connected layers” means two 4096-dimensional fully connected layers are added before the classification layer.
NetworkFastEval14k MAP@100
Inception-v3, no fully connected layers6.36
Xception, no fully connected layers6.70
Inception-v3, with fully connected layers6.50
Xception, with fully connected layers6.78

On JFT the gap is much larger, a 4.3% relative improvement for the versions with fully connected layers. The author's explanation is that Inception-v3 was developed with ImageNet in mind, so its design may already be overfit to that dataset, while neither network was tuned for JFT.

Similar Parameters, a Bit Slower

Table: Excerpted from Table 3 of the paper, with synchronous training on ImageNet on 60 K80 GPUs.
NetworkParametersTraining steps per second
Inception-v323,626,72831
Xception22,855,95228

The two differ in parameter count by less than 3.5%, so the author says Xception's gains come from using its parameters more efficiently, not from added capacity.

In speed, Xception is actually a little slower. Depthwise convolutions have few parameters and little computation, but on GPUs it's hard to get a matching speedup out of them.

Residual Connections

The author also trained a version of Xception with all the residual connections removed. On ImageNet, the version without residuals converges much more slowly and ends at clearly lower accuracy.

The author also cautions that the non-residual version used the same training setup, which may not be quite fair to it, and that this only shows residual connections matter for this particular architecture. Separately, he tried VGG-style networks with every convolution replaced by a depthwise separable convolution and no residuals at all, and on JFT they likewise beat an Inception-v3 with the same parameter count.

An Activation in Between?

Back to the question left open earlier. Should there be a nonlinearity between the depthwise and pointwise steps? The author tried three options, none, ReLU, and ELU:

Training curves of ImageNet validation accuracy against gradient descent steps. The red No intermediate activation rises fastest and ends near 0.79; the green Intermediate ELU comes next, leveling off around 0.78; the blue Intermediate ReLU is slowest, leveling off around 0.77
Figure 10 from the paper, excerpted from page 7 of the v3 PDF. Training curves with different activation functions between the depthwise and pointwise steps of SeparableConv.

The red line, with no activation function at all, converges fastest and ends with the highest accuracy; the green line with ELU is in the middle, and the blue line with ReLU is the worst.

This is the opposite of what the Inception-v3 paper found. When Inception-v3 factorized a 5×55\times5 into two 3×33\times3 layers, it found that adding ReLU in between worked better. The author's guess is that the key lies in how “thick” the feature space is in which the spatial convolution operates. In an Inception module, each segment has dozens to hundreds of channels, and the nonlinearity helps; in a depthwise separable convolution, each segment has just one channel, and when ReLU chops off the negative values, information may simply be lost.

MobileNetV2 later removed ReLU from the narrowest layers of the network, on the same grounds that “nonlinearities in low-dimensional spaces lose information”; you can continue with MobileNet-V2.

Conclusion

Xception looks at Inception from a new angle. Simplified, an Inception module is just a 1×11\times1 convolution plus a 3×33\times3 convolution split into a few groups; turn the group count from 1 up to the number of channels, and you go from an ordinary convolution to a depthwise separable convolution. Xception goes straight to the end, with almost the same parameter count as Inception-v3, slightly better on ImageNet and quite a bit better on JFT.

It may be called Extreme Inception, but there's nothing extreme about its speed; it even manages 3 fewer training steps per second than Inception-v3. The author expected it to pull ahead once depthwise convolution implementations were optimized. That wait lasted years, and EfficientNetV2 eventually just replaced the depthwise convolutions in the early part of the network.

As for whether the end is the best place to be, even the author wasn't sure; in his closing section he writes that some points in the middle of the spectrum might be better. A little over a month later, ResNeXt used convolutions split into 32 groups, landing right in the middle.