Search

Search papers and lessons

Depthwise Separable Convolution

Level 3 · 11 min read

A depthwise separable convolution splits one convolution into a spatial filter per channel and a 1 by 1 channel mixer, cutting its cost by roughly the kernel area.

On this page

  • Colour = topic mix
  • Calculus
  • Discrete math
  • Linear algebra
  • Neural network basics
  • Architectures
  • Probability
  • Sign in to see your progress

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 1×11 \times 1 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 k×k×Ck \times k \times C 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 3×33 \times 3 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 1×11 \times 1 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 3×33 \times 3 filter that reads 64 input channels has 3⋅3⋅64=5763 \cdot 3 \cdot 64 = 576 weights, and it must learn a separate pattern for every combination of spatial shape and channel. The separable version gives the spatial part only 99 weights per channel, and the channel mixing only 6464 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 HH, width WW and CinC_{\text{in}} channels. The standard layer has CoutC_{\text{out}} filters, each of size k×k×Cink \times k \times C_{\text{in}}. Assume stride 1 and padding that keeps the spatial size, so the output is H×W×CoutH \times W \times C_{\text{out}}. Here kk 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

Mstd=H W k2 Cin CoutM_{\text{std}} = H\,W\,k^2\,C_{\text{in}}\,C_{\text{out}}

multiply-adds, and it has Pstd=k2 Cin CoutP_{\text{std}} = k^2\,C_{\text{in}}\,C_{\text{out}} weights.

The two steps

Depthwise step. Each input channel gets one k×kk \times k filter. At each of the HWH W positions, every channel costs k2k^2 multiply-adds:

Mdw=H W k2 Cin,Pdw=k2 Cin.M_{\text{dw}} = H\,W\,k^2\,C_{\text{in}}, \qquad P_{\text{dw}} = k^2\,C_{\text{in}}.

Pointwise step. A 1×11 \times 1 convolution takes the CinC_{\text{in}} channel values at a position and produces CoutC_{\text{out}} new values, a matrix product (see Matrix Multiplication) applied at every position:

Mpw=H W Cin Cout,Ppw=Cin Cout.M_{\text{pw}} = H\,W\,C_{\text{in}}\,C_{\text{out}}, \qquad P_{\text{pw}} = C_{\text{in}}\,C_{\text{out}}.

Adding the two gives the separable total:

Msep=H W Cin (k2+Cout),Psep=Cin (k2+Cout).M_{\text{sep}} = H\,W\,C_{\text{in}}\,(k^2 + C_{\text{out}}), \qquad P_{\text{sep}} = C_{\text{in}}\,(k^2 + C_{\text{out}}).

The ratio

Divide the separable cost by the standard cost. The factor H W CinH\,W\,C_{\text{in}} cancels:

MsepMstd=k2+Coutk2 Cout=1Cout+1k2.\frac{M_{\text{sep}}}{M_{\text{std}}} = \frac{k^2 + C_{\text{out}}}{k^2\,C_{\text{out}}} = \frac{1}{C_{\text{out}}} + \frac{1}{k^2}.

The same ratio holds for the number of weights. For a 3×33 \times 3 kernel with many output channels, the ratio approaches 1/91/9. For a 5×55 \times 5 kernel it approaches 1/251/25. The result does not depend on the image size, so the saving is the same at every resolution.

5010015020025000.20.40.60.811.2output channels C_outcost ratio
  • k = 3
  • k = 5
Cost of the separable layer divided by the cost of the standard layer. With C_out large, the ratio settles near 1 over k squared.

Worked examples

Example
Tiny: count the multiplications at one position

Take Cin=2C_{\text{in}} = 2 input channels, Cout=4C_{\text{out}} = 4 output channels, and a 3×33 \times 3 kernel. Count the multiplications needed to compute the output at one position.

  1. Standard: each of the 4 filters has 3⋅3⋅2=183 \cdot 3 \cdot 2 = 18 weights, so 4×18=724 \times 18 = 72 multiplications.
  2. Depthwise: each of the 2 channels has a 3×33 \times 3 filter, so 2×9=182 \times 9 = 18 multiplications.
  3. Pointwise: each of the 4 output channels takes a weighted sum of 2 values, so 4×2=84 \times 2 = 8 multiplications.
  4. Separable total: 18+8=2618 + 8 = 26.
  5. Check the ratio: 26/72≈0.36126 / 72 \approx 0.361, and the formula gives 1/4+1/9≈0.3611/4 + 1/9 \approx 0.361.

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.

Example
Standard: a 56 by 56 feature map with 64 to 128 channels

Take a feature map of size 56×5656 \times 56 with Cin=64C_{\text{in}} = 64, Cout=128C_{\text{out}} = 128 and k=3k = 3. There are 56×56=313656 \times 56 = 3136 positions.

  1. Standard weights: 9⋅64⋅128=73,7289 \cdot 64 \cdot 128 = 73{,}728.
  2. Separable weights: 9⋅64+64⋅128=576+8,192=8,7689 \cdot 64 + 64 \cdot 128 = 576 + 8{,}192 = 8{,}768.
  3. Standard multiply-adds per image: 73,728×3136=231,211,00873{,}728 \times 3136 = 231{,}211{,}008, about 2.3×1082.3 \times 10^8.
  4. Separable multiply-adds per image: 8,768×3136=27,496,4488{,}768 \times 3136 = 27{,}496{,}448, about 2.7×1072.7 \times 10^7.
  5. Ratio: 27,496,448/231,211,008≈0.11927{,}496{,}448 / 231{,}211{,}008 \approx 0.119. The formula gives 1/128+1/9≈0.1191/128 + 1/9 \approx 0.119.

Interpretation: the layer needs about one-eighth of the multiplications and weights. The spatial part is tiny, 576576 weights, and nearly all the remaining cost is the 1×11 \times 1 channel mixing.

Example
Realistic: a 512-channel block in a late stage

Deep image classifiers often have blocks with 512 channels, where the spatial size is small and the channel count dominates. Take Cin=Cout=512C_{\text{in}} = C_{\text{out}} = 512 and k=3k = 3.

  1. Standard weights: 9⋅512⋅512=2,359,2969 \cdot 512 \cdot 512 = 2{,}359{,}296.
  2. Separable weights: 9⋅512+512⋅512=4,608+262,144=266,7529 \cdot 512 + 512 \cdot 512 = 4{,}608 + 262{,}144 = 266{,}752.
  3. Ratio: 266,752/2,359,296≈0.113266{,}752 / 2{,}359{,}296 \approx 0.113, and the formula gives 1/512+1/9≈0.1131/512 + 1/9 \approx 0.113.
  4. The 1×11 \times 1 part holds 262,144/266,752≈98%262{,}144 / 266{,}752 \approx 98\% 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.

SituationProblemBetter choice
The kernel is already 1×11 \times 1Nothing spatial to separate, so no savingUse a plain 1×11 \times 1 convolution
Very few input channels, such as the RGB image inputThe saving is small and accuracy may sufferKeep a standard first layer
Fewer operations on a phone or GPUDepthwise layers read and write memory for little arithmetic, so latency may barely changeMeasure real latency on the target device
Accuracy must match a large standard networkThe restricted filter can lose some accuracy at the same costCompare at equal latency, or widen the network
Fixed budget of multiplicationsSeparable layers allow more channels for the same costTrade 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 1×11 \times 1 convolution, so its cost is 1/Cout+1/k21/C_{\text{out}} + 1/k^2 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 1×11 \times 1 convolution: at each position it takes a weighted sum of the channel values, with no spatial extent. The depthwise step uses a k×kk \times k filter on each channel separately and does not mix channels.

QuestionA layer has Cin=32C_{\text{in}} = 32, Cout=64C_{\text{out}} = 64, k=3k = 3 and a 10×1010 \times 10 feature map. How many multiply-adds does the standard layer need, and how many does the separable version need?Show answer

There are 100100 positions. Standard: 100⋅9⋅32⋅64=100⋅18,432=1,843,200100 \cdot 9 \cdot 32 \cdot 64 = 100 \cdot 18{,}432 = 1{,}843{,}200. Separable: 100⋅(9⋅32+32⋅64)=100⋅(288+2,048)=233,600100 \cdot (9 \cdot 32 + 32 \cdot 64) = 100 \cdot (288 + 2{,}048) = 233{,}600. The ratio is about 0.1270.127, which matches 1/64+1/91/64 + 1/9.

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