Search

Search papers and lessons

Factorized Convolution

Level 3 · 12 min read

A factorized convolution replaces one large filter with a chain of smaller ones, such as a 1 by 7 followed by a 7 by 1, which gives nearly the same reach with far fewer weights.

On this page

  • Colour = topic mix
  • Linear algebra
  • Calculus
  • Discrete math
  • Architectures
  • ReadReady to readNot yet

TL;DR

A factorized convolution replaces one large filter with a sequence of smaller ones. A 7×77 \times 7 filter can be applied as a 1×71 \times 7 filter followed by a 7×17 \times 1 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 3×33 \times 3 filters in VGG. The 2014 VGG networks used only 3×33 \times 3 filters. Two of them stacked on top of each other see a 5×55 \times 5 patch of the input, the same reach as one 5×55 \times 5 filter, but with 18 weights per channel pair instead of 25. Three stacked 3×33 \times 3 layers see a 7×77 \times 7 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 1×71 \times 7 followed by 7×17 \times 1. 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 3×33 \times 3 filter that is the product of a column and a row, applied to a 3×33 \times 3 patch of pixels. The numbers are small so you can check every step by hand.

  1. Input: a 3×33 \times 3 patch PP of pixel values, with rows [1,2,3][1, 2, 3], [4,5,6][4, 5, 6] and [7,8,9][7, 8, 9]. The filter is KK, with rows [1,2,1][1, 2, 1], [2,4,2][2, 4, 2] and [1,2,1][1, 2, 1]. Check: the filter has 9 weights, and computing its output at this one position needs 9 multiplications.

  2. Spot the structure: the filter is the product of a column v=(1,2,1)v = (1, 2, 1) and a row h=(1,2,1)h = (1, 2, 1). Each entry of KK is vihjv_i h_j. For example the top-left entry is 1×1=11 \times 1 = 1, and the centre is 2×2=42 \times 2 = 4. Check: all nine entries of KK are products of one entry of vv and one entry of hh.

  3. Direct computation: multiply each weight by the pixel under it and add. Row by row: 1⋅1+2⋅2+1⋅3=81 \cdot 1 + 2 \cdot 2 + 1 \cdot 3 = 8, then 2⋅4+4⋅5+2⋅6=402 \cdot 4 + 4 \cdot 5 + 2 \cdot 6 = 40, then 1⋅7+2⋅8+1⋅9=321 \cdot 7 + 2 \cdot 8 + 1 \cdot 9 = 32. Add: 8+40+32=808 + 40 + 32 = 80. Check: the output at this position is 80.

  4. Factorized computation, pass one: apply the row filter hh to each row of the patch. Row 1 gives 1+4+3=81 + 4 + 3 = 8, row 2 gives 4+10+6=204 + 10 + 6 = 20, row 3 gives 7+16+9=327 + 16 + 9 = 32. 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.

  5. Factorized computation, pass two: apply the column filter vv to that column: 8⋅1+20⋅2+32⋅1=808 \cdot 1 + 20 \cdot 2 + 32 \cdot 1 = 80. 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

Definition: Separable filter

A two-dimensional filter KK of size k×kk \times k is separable when it can be written as an outer product of a column vector v∈Rkv \in \mathbb{R}^{k} and a row vector h∈Rkh \in \mathbb{R}^{k}, so that Kab=vahbK_{ab} = v_a h_b. The output of a convolution with KK is then the output of a row convolution with hh followed by a column convolution with vv.

Deep learning libraries compute "convolution" as cross-correlation, so the output at position (i,j)(i, j) is written as a sum over the filter offsets:

yij=∑a∑bKab xi+a, j+b.y_{ij} = \sum_{a} \sum_{b} K_{ab}\, x_{i+a,\, j+b}.

Step 2 in symbols

For the tiny example, Kab=vahbK_{ab} = v_a h_b with v=(1,2,1)⊤v = (1, 2, 1)^\top and h=(1,2,1)h = (1, 2, 1). Substituting this into the sum and moving the factor vav_a outside the inner sum gives

yij=∑ava(∑bhb xi+a, j+b)=∑ava zi+a, j,zij=∑bhb xi, j+b.y_{ij} = \sum_{a} v_a \left( \sum_{b} h_b\, x_{i+a,\, j+b} \right) = \sum_{a} v_a\, z_{i+a,\, j}, \qquad z_{ij} = \sum_{b} h_b\, x_{i,\, j+b}.

The inner quantity zz is the intermediate result of pass one, and the outer sum is pass two. Step 4 of the walkthrough computed zz for the three rows, and step 5 applied the column sum.

Step 3 in symbols

Counting multiplications per output value, a direct k×kk \times k filter costs k2k^2 and the factorized version costs 2k2k. The ratio is

2kk2=2k.\frac{2k}{k^2} = \frac{2}{k}.

For k=3k = 3 this is 2/32/3, and for k=7k = 7 it is 2/72/7. The saving grows with the size of the filter.

Step 4 in symbols

A general k×kk \times k filter is a matrix with k2k^2 independent entries, while a separable one has only 2k2k free numbers. In matrix language, the k×kk \times k matrix K=vh⊤K = v h^\top 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 W∈Rm×nW \in \mathbb{R}^{m \times n}, a rank-rr factorization W≈UVW \approx U V with U∈Rm×rU \in \mathbb{R}^{m \times r} and V∈Rr×nV \in \mathbb{R}^{r \times n} stores r(m+n)r(m + n) numbers instead of mnmn. Convolutional layers are factorized in the same spirit, with the channel dimensions playing the role of mm and nn.

Worked examples

Example
1. A tiny edge detector

Let v=(1,1,1)⊤v = (1, 1, 1)^\top and h=(1,0,−1)h = (1, 0, -1), so K=vh⊤K = v h^\top has every row equal to (1,0,−1)(1, 0, -1). Apply KK to the patch

P=(123123123).P = \begin{pmatrix} 1 & 2 & 3 \\ 1 & 2 & 3 \\ 1 & 2 & 3 \end{pmatrix}.
  1. Direct: each row of PP gives 1⋅1+0⋅2+(−1)⋅3=−21 \cdot 1 + 0 \cdot 2 + (-1) \cdot 3 = -2.
  2. Summing the three rows gives −2+(−2)+(−2)=−6-2 + (-2) + (-2) = -6.
  3. Factorized, pass one: apply hh to each row of PP, which gives −2-2 for each of the three rows.
  4. Factorized, pass two: apply v=(1,1,1)v = (1, 1, 1) to the column (−2,−2,−2)(-2, -2, -2), which gives −6-6.
  5. 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 −6-6 both ways.

Example
2. The standard case: two 3 by 3 filters in place of one 5 by 5

Compare one 5×55 \times 5 filter with two stacked 3×33 \times 3 filters, counting weights for one input channel and one output channel.

  1. Reach: two 3×33 \times 3 filters stacked see 3+3−1=53 + 3 - 1 = 5 pixels in each direction, the same reach as one 5×55 \times 5 filter.
  2. Weights of the single filter: 5×5=255 \times 5 = 25.
  3. Weights of the stacked pair: 3×3+3×3=183 \times 3 + 3 \times 3 = 18.
  4. Ratio: 18/25=0.7218 / 25 = 0.72.
  5. 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.

Example
3. Realistic: a 7 by 7 layer with 64 channels

A layer maps 64 input channels to 64 output channels with a 7×77 \times 7 filter. Compare the direct layer with the asymmetric version, a 1×71 \times 7 layer followed by a 7×17 \times 1 layer, each with 64 channels in and out.

  1. Direct weights: 64×64×49=4096×49=200,70464 \times 64 \times 49 = 4096 \times 49 = 200{,}704.
  2. Asymmetric weights, first layer: 64×64×7=28,67264 \times 64 \times 7 = 28{,}672.
  3. Asymmetric weights, second layer: also 28,67228{,}672, so the pair has 57,34457{,}344 weights.
  4. Ratio: 57,344/200,704=2/7≈0.28657{,}344 / 200{,}704 = 2/7 \approx 0.286. The layer keeps about 29 percent of its weights.
  5. Interpretation: the layer is about 3.5 times smaller, and every output still depends on a 7×77 \times 7 region of the input. The cost is that the filter is restricted to the separable form.

Try it: the same comparison for a 3×33 \times 3 filter, again with 64 channels in and out, split into a 1×31 \times 3 layer and a 3×13 \times 1 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 64×64×9=36,86464 \times 64 \times 9 = 36{,}864 weights. The factorized pair has 2×64×64×3=24,5762 \times 64 \times 64 \times 3 = 24{,}576. The saving is 36,864−24,576=12,28836{,}864 - 24{,}576 = 12{,}288 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 (1,0,0)(1, 0, 0), (0,1,0)(0, 1, 0) and (0,0,1)(0, 0, 1) 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 3×33 \times 3 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 3×33 \times 3 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 k×kk \times k filter costs k2k^2 multiplications per output, and its separable form costs 2k2k.
  • 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 k×kk \times k 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 K=vh⊤K = v h^\top of a column vv and a row hh. Equivalently, the matrix KK must have rank 1.

QuestionComputation: a 9×99 \times 9 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 9×9=819 \times 9 = 81 multiplications. The separable version needs 2×9=182 \times 9 = 18 multiplications, a ratio of 18/81=2/918/81 = 2/9.

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 3×33 \times 3 matrix has rank 3, because its three rows are linearly independent. A product vh⊤v h^\top always has rank at most 1, so no choice of vv and hh can reproduce it.

QuestionApplied: a designer replaces a 7×77 \times 7 filter in the first layer of a network that reads raw RGB pixels with a 1×71 \times 7 and 7×17 \times 1 pair to save compute. What is the risk, and what would you check?Show answer

The factorized pair can only represent separable 7×77 \times 7 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.

Sources