TL;DR
A standard convolution layer does two jobs at once: it looks at neighbouring pixels in space, and it combines information across channels, such as the red, green and blue values or the feature maps (the grids of numbers, one per channel, that the previous layer produced) from the previous layer. A depthwise separable convolution splits those jobs into two cheaper steps. First, a separate small spatial filter is applied to each channel. Second, a convolution mixes the channels at each position. The pair gives nearly the same kind of output for a small fraction of the cost.
Where you'll see it: in mobile and embedded image classifiers such as MobileNet, in the efficient blocks of detection networks, and in any convolutional network where the number of multiplications per image matters more than the last point of accuracy.
This lesson explains a neural network layer design. It is not advice about choosing a model for any particular device or task.
Intuition
Two questions, asked separately
Look at one spot in a photograph. A convolution filter has to answer two questions about it at once. The first is spatial: what do the pixels around this spot look like, as a pattern? The second is about channels: how do the colours or features at this spot relate to each other? A standard filter answers both in one step, using a full block of weights for every output channel. That is powerful, but expensive.
The depthwise separable version asks the questions in order. Start with a colour image that has three channels. Give each channel its own filter, so the red channel is filtered only by its own weights, and so on. Nothing mixes the colours yet. Then, at each position, use a convolution, which is a weighted sum across channels with no spatial extent, to combine the three filtered values into new output channels.
flowchart LR A["Input with C channels"] --> B["Depthwise: one k by k filter per channel, no mixing"] B --> C["Pointwise: 1 by 1 convolution, mixes channels"] C --> D["Output with C_out channels"]
Why this saves work
A standard filter that reads 64 input channels has weights, and it must learn a separate pattern for every combination of spatial shape and channel. The separable version gives the spatial part only weights per channel, and the channel mixing only weights per output. Most of the cost sits in the channel mixing when the number of channels is large, and the saving is roughly the area of the kernel.
The name
Depthwise refers to filtering each channel separately, one filter per depth slice. Separable refers to splitting the filter into a spatial part and a channel part that run in sequence. The same idea is used in signal processing, where a two-dimensional filter that factors into a row filter and a column filter can be applied in two cheaper passes.
The math
Setup
Take an input of height , width and channels. The standard layer has filters, each of size . Assume stride 1 and padding that keeps the spatial size, so the output is . Here is the kernel width, and padding means extra border values added so the filter fits at the edges. Count only the multiply-add operations and ignore biases, which are negligible.
The standard layer costs
multiply-adds, and it has weights.
The two steps
Depthwise step. Each input channel gets one filter. At each of the positions, every channel costs multiply-adds:
Pointwise step. A convolution takes the channel values at a position and produces new values, a matrix product (see Matrix Multiplication) applied at every position:
Adding the two gives the separable total:
The ratio
Divide the separable cost by the standard cost. The factor cancels:
The same ratio holds for the number of weights. For a kernel with many output channels, the ratio approaches . For a kernel it approaches . The result does not depend on the image size, so the saving is the same at every resolution.
- k = 3
- k = 5
Worked examples
Take input channels, output channels, and a kernel. Count the multiplications needed to compute the output at one position.
- Standard: each of the 4 filters has weights, so multiplications.
- Depthwise: each of the 2 channels has a filter, so multiplications.
- Pointwise: each of the 4 output channels takes a weighted sum of 2 values, so multiplications.
- Separable total: .
- Check the ratio: , and the formula gives .
Interpretation: at this tiny size the separable layer already needs about a third of the work. The formula is not a rough estimate; the counts above are exact.
Take a feature map of size with , and . There are positions.
- Standard weights: .
- Separable weights: .
- Standard multiply-adds per image: , about .
- Separable multiply-adds per image: , about .
- Ratio: . The formula gives .
Interpretation: the layer needs about one-eighth of the multiplications and weights. The spatial part is tiny, weights, and nearly all the remaining cost is the channel mixing.
Deep image classifiers often have blocks with 512 channels, where the spatial size is small and the channel count dominates. Take and .
- Standard weights: .
- Separable weights: .
- Ratio: , and the formula gives .
- The part holds of the remaining weights.
Interpretation: the block is cut to about a ninth of its size. But almost all the remaining cost is now channel mixing. Further savings have to come from reducing the channel mixing itself, not from the spatial filter, which is already cheap.
Tradeoffs and failure modes
Depthwise separable layers trade a restricted form of the filter for a large drop in cost. A standard filter can learn any combination of spatial pattern and channel mixing at once; the factored version can only apply spatial filters first and channel mixing second. Whether that restriction hurts depends on the task and the network.
| Situation | Problem | Better choice |
|---|---|---|
| The kernel is already | Nothing spatial to separate, so no saving | Use a plain convolution |
| Very few input channels, such as the RGB image input | The saving is small and accuracy may suffer | Keep a standard first layer |
| Fewer operations on a phone or GPU | Depthwise layers read and write memory for little arithmetic, so latency may barely change | Measure real latency on the target device |
| Accuracy must match a large standard network | The restricted filter can lose some accuracy at the same cost | Compare at equal latency, or widen the network |
| Fixed budget of multiplications | Separable layers allow more channels for the same cost | Trade the saved cost for width or depth |
Practical points:
- Nonlinearity between the steps matters. Without an activation such as a ReLU between the two steps, they compose into one linear map, which is a standard convolution with restricted weights. The saving in cost remains, but the layer gains no extra expressive power from the split. Practice varies: MobileNet puts a batch normalization and a ReLU after the depthwise step, while the Xception paper reported that leaving out the nonlinearity between the two steps trained faster and gave better accuracy in its network. So test this choice rather than assuming it.
- Operation counts are not the whole story. A depthwise layer does few multiply-adds per byte it moves, so on hardware tuned for dense matrix products it can run slower than its operation count suggests. Measure the target device.
- The saving is an estimate for one layer. The total network speedup depends on how many layers are separable and on everything else, such as pooling and the final classifier.
History
Factorised convolutions had appeared in earlier work, but the idea became a standard tool for efficient image networks in 2016 and 2017. Francois Chollet's Xception (2016) built a deep image classifier entirely from depthwise separable convolutions and reported that it slightly outperformed Inception V3, a standard-convolution network of similar size, on image classification. Howard and colleagues' MobileNets (2017) used the same blocks to build networks for phones, with a width multiplier and a resolution multiplier to trade accuracy for cost.
These papers caught on because the saving was large and the accuracy loss was small on standard benchmarks, and because the design fit the needs of mobile and embedded hardware. Later networks adopted the same block as a default when the budget for multiplications is tight.
Recap
A standard convolution combines spatial filtering and channel mixing in one step, and its cost is the product of kernel area, input channels and output channels at every position. A depthwise separable convolution does the spatial filtering per channel and the channel mixing with a convolution, so its cost is times the standard cost. The restriction costs some expressive power, so it pays to check the choice of nonlinearity between the steps and to measure real speed. The same block is used in Conv-TasNet, a speech separation model that stacks many dilated and separable layers.
Check yourself
QuestionIn a depthwise separable convolution, which step mixes information across channels, and what kernel size does that step use?Show answer
The pointwise step mixes channels. It is a convolution: at each position it takes a weighted sum of the channel values, with no spatial extent. The depthwise step uses a filter on each channel separately and does not mix channels.
QuestionA layer has , , and a feature map. How many multiply-adds does the standard layer need, and how many does the separable version need?Show answer
There are positions. Standard: . Separable: . The ratio is about , which matches .
QuestionWhat does a nonlinearity such as a ReLU between the depthwise and pointwise steps add, and what does leaving it out cost?Show answer
Without a nonlinearity, the two linear steps compose into one linear map. That map is a standard convolution with restricted weights, so the layer gains nothing in expressive power from being split, although it still has the lower cost. A nonlinearity between the steps means the channel mixing sees a nonlinear function of the spatial features, so the two steps really do add capacity. The saving in cost is kept either way. (In practice the benefit is not guaranteed: Xception reported good results without it.)
QuestionA team replaces the standard layers in a mobile image model with depthwise separable layers. The multiply-add count drops to about one-eighth, but on their phone the latency barely changes. Give one plausible reason and what they should measure.Show answer
Depthwise layers do little arithmetic per value they move through memory, and hardware libraries are tuned for dense operations, so memory traffic and overheads can dominate. Fewer multiply-adds does not guarantee less time. They should measure end-to-end latency and accuracy on the target device, and compare configurations at equal latency rather than equal multiply-add counts.
Sources
- Chollet, Xception: Deep Learning with Depthwise Separable Convolutions (2016): https://arxiv.org/abs/1610.02357
- Howard et al., MobileNets: Efficient Convolutional Neural Networks for Mobile Vision Applications (2017): https://arxiv.org/abs/1704.04861
- Goodfellow, Bengio, Courville, Deep Learning, Chapter 9: https://www.deeplearningbook.org/