TL;DR
A factorized convolution replaces one large filter with a sequence of smaller ones. A filter can be applied as a filter followed by a filter, and the result is the same whenever the big filter has the special structure that makes this possible. The chain covers the same region of the input with about 2/7 of the weights in this case, so the network is cheaper to run and to store. The idea works because many useful filters can be written as a product of simpler ones, which is the same low-rank structure found in low-rank matrices.
Builds on: Convolution, Low-Rank Matrix · Leads to: Depthwise Separable Convolution
Where it's used
Stacked filters in VGG. The 2014 VGG networks used only filters. Two of them stacked on top of each other see a patch of the input, the same reach as one filter, but with 18 weights per channel pair instead of 25. Three stacked layers see a patch with 27 instead of 49 weights. The stacking also inserts extra ReLU nonlinearities between the layers, which is a second benefit.
Asymmetric filters in Inception-v3. The 2015 Inception-v3 network replaces large square filters in its middle layers with pairs of thin ones, such as followed by . This reduces the cost of those layers substantially, and the authors reported that the factorization worked well on the medium-sized feature maps where they applied it.
Efficient mobile networks. The same principle, taken further and split across channels, gives the depthwise separable convolution used in mobile vision models. Those models must run on a phone's battery, so the saving in multiplications is the whole point.
How it works
Take one tiny instance: a filter that is the product of a column and a row, applied to a patch of pixels. The numbers are small so you can check every step by hand.
-
Input: a patch of pixel values, with rows , and . The filter is , with rows , and . Check: the filter has 9 weights, and computing its output at this one position needs 9 multiplications.
-
Spot the structure: the filter is the product of a column and a row . Each entry of is . For example the top-left entry is , and the centre is . Check: all nine entries of are products of one entry of and one entry of .
-
Direct computation: multiply each weight by the pixel under it and add. Row by row: , then , then . Add: . Check: the output at this position is 80.
-
Factorized computation, pass one: apply the row filter to each row of the patch. Row 1 gives , row 2 gives , row 3 gives . The result is a column of three numbers. Check: this pass needs 3 multiplications per row, 9 in total for the patch, but it produces a 3-value column for the next pass.
-
Factorized computation, pass two: apply the column filter to that column: . Check: the factorized result equals the direct result, 80. Each pass uses 3 weights, so the filter costs 6 weights in all instead of 9.
flowchart LR A["Image"] --> B["Pass 1: 1 by 3 row filter h"] B --> C["Pass 2: 3 by 1 column filter v"] C --> D["Same output as one 3 by 3 filter K"]
The math
A two-dimensional filter of size is separable when it can be written as an outer product of a column vector and a row vector , so that . The output of a convolution with is then the output of a row convolution with followed by a column convolution with .
Deep learning libraries compute "convolution" as cross-correlation, so the output at position is written as a sum over the filter offsets:
Step 2 in symbols
For the tiny example, with and . Substituting this into the sum and moving the factor outside the inner sum gives
The inner quantity is the intermediate result of pass one, and the outer sum is pass two. Step 4 of the walkthrough computed for the three rows, and step 5 applied the column sum.
Step 3 in symbols
Counting multiplications per output value, a direct filter costs and the factorized version costs . The ratio is
For this is , and for it is . The saving grows with the size of the filter.
Step 4 in symbols
A general filter is a matrix with independent entries, while a separable one has only free numbers. In matrix language, the matrix has rank 1, so a separable filter is exactly a rank-1 low-rank filter. Only filters of this special form can be factorized without loss.
Step 5 in symbols
For a dense layer with weight matrix , a rank- factorization with and stores numbers instead of . Convolutional layers are factorized in the same spirit, with the channel dimensions playing the role of and .
Worked examples
Let and , so has every row equal to . Apply to the patch
- Direct: each row of gives .
- Summing the three rows gives .
- Factorized, pass one: apply to each row of , which gives for each of the three rows.
- Factorized, pass two: apply to the column , which gives .
- Interpretation: the pixel values go up from left to right, and the filter responds with a negative value that measures that rise. This is a vertical-edge detector, and the two passes reproduce it exactly.
Answer: the output is both ways.
Compare one filter with two stacked filters, counting weights for one input channel and one output channel.
- Reach: two filters stacked see pixels in each direction, the same reach as one filter.
- Weights of the single filter: .
- Weights of the stacked pair: .
- Ratio: .
- Interpretation: the stacked version uses 28 percent fewer weights, and it also adds a ReLU between the two layers, so it can represent more functions than the single filter.
Answer: 18 weights instead of 25, a saving of 28 percent.
A layer maps 64 input channels to 64 output channels with a filter. Compare the direct layer with the asymmetric version, a layer followed by a layer, each with 64 channels in and out.
- Direct weights: .
- Asymmetric weights, first layer: .
- Asymmetric weights, second layer: also , so the pair has weights.
- Ratio: . The layer keeps about 29 percent of its weights.
- Interpretation: the layer is about 3.5 times smaller, and every output still depends on a region of the input. The cost is that the filter is restricted to the separable form.
Try it: the same comparison for a filter, again with 64 channels in and out, split into a layer and a layer. How many weights does the direct layer have, how many does the factorized pair have, and how many are saved?
Answer: the direct layer has weights. The factorized pair has . The saving is weights, one third of the original.
Common mistakes and tradeoffs
It is tempting to think that replacing a square filter with two thin ones gives the same function. In fact it does only when the square filter is separable. The identity-like filter with rows , and has rank 3, so no product of a column and a row can equal it, and the factorized version would lose information for that pattern.
It is tempting to think that stacking small filters is always the same as one big filter. In fact stacking gives the same reach, but there is a nonlinearity such as ReLU between the layers, so the stacked network computes a different and usually richer function. This is why VGG favoured stacks of filters.
It is tempting to apply the asymmetric form everywhere. In fact the first layer of a network sees raw pixels, where colour and corner patterns matter, and a rank-1 restriction there can hurt. The Inception-v3 authors applied the factorization to medium-sized feature maps deeper in the network.
Use factorized convolution when the operation is close to separable, when you need to cut the cost of large filters, or when you want the extra nonlinearity of stacking. Do not use it when the filter must detect diagonal or corner structure at the first layer. Alternatives include depthwise separable convolution, which factorizes across channels rather than space, and low-rank approximation of a trained filter with the Singular Value Decomposition (SVD).
History
Separable filters were used in image processing long before neural networks. Gaussian blur is separable: a 2D Gaussian is the product of two 1D Gaussians, so the blur can be done with a row pass and a column pass. Classical edge detectors such as the Sobel operator are also built from a smoothing column and a differencing row.
In 2014, Karen Simonyan and Andrew Zisserman showed in the VGG networks that a deep stack of convolutions, which is a factorization of larger receptive fields, performed very well on ImageNet. Their paper made small stacked filters a standard design choice.
In 2015, Christian Szegedy and colleagues introduced the asymmetric factorization of large filters in the Inception-v3 network, which reduced the cost of its middle layers. In 2017, the MobileNet family took the idea further and factorized across channels as well as space, which made the approach practical on phones.
Recap
- A separable filter is an outer product of a column and a row, so it is rank 1 and can be applied in two cheap passes.
- A filter costs multiplications per output, and its separable form costs .
- A stack of small filters has the same reach as one large filter, plus a nonlinearity in between.
- Factorization is exact only for separable filters; otherwise it is a restriction that must be tested.
- To go further, read Depthwise Separable Convolution for the channel-wise version and the Inception-v3 paper, arXiv 1512.00567 (see Sources), for how asymmetric factorization was used at scale.
Check yourself
QuestionRecall: what property must a filter have to be replaced exactly by a column pass followed by a row pass?Show answer
It must be separable, meaning it can be written as an outer product of a column and a row . Equivalently, the matrix must have rank 1.
QuestionComputation: a filter is applied to an image. How many multiplications per output value does the direct version need, and how many does the separable version need?Show answer
The direct version needs multiplications. The separable version needs multiplications, a ratio of .
QuestionWhy: why can't the identity-like filter with ones on the diagonal and zeros elsewhere be factorized into a column and a row?Show answer
Its matrix has rank 3, because its three rows are linearly independent. A product always has rank at most 1, so no choice of and can reproduce it.
QuestionApplied: a designer replaces a filter in the first layer of a network that reads raw RGB pixels with a and pair to save compute. What is the risk, and what would you check?Show answer
The factorized pair can only represent separable filters, and first-layer filters must detect general colour and corner patterns, so the restriction can cost accuracy. You would check the validation accuracy against the original layer, and consider leaving the first layer square while factorizing deeper layers where the feature maps are medium-sized.