Search

Search papers and lessons

Pointwise (1x1) Convolution

Level 3 · 11 min read

A pointwise convolution uses a 1 by 1 kernel to mix the channels at each pixel separately, so it changes the channel count without looking at neighbouring pixels.

On this page

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

TL;DR

A pointwise convolution is a convolution whose kernel covers a single pixel, so its size is 1 by 1. At each pixel it takes the vector of channel values and produces a new vector through a weighted sum. It does not look at neighbouring pixels, so it is the cheapest way to mix information across channels and to change how many channels a layer has.

Builds on: Convolution, Matrix Multiplication · Leads to: Depthwise Separable Convolution, Inception Module

Where it's used

Inception networks. The Inception modules in GoogLeNet place 1 by 1 convolutions in front of the larger 3 by 3 and 5 by 5 convolutions. They shrink the number of channels first, so the expensive larger filters work on fewer channels and the whole block costs less.

MobileNet-style networks. A depthwise separable convolution splits one standard convolution into two steps, which keeps convolutional networks small enough for phones. The first filters each channel spatially, and the second is a pointwise convolution that mixes the channels. The second step is where most of the channel information is combined.

Residual bottlenecks. The bottleneck blocks used in deep residual networks wrap a 3 by 3 convolution between two 1 by 1 convolutions. The first reduces the channel count, and the second restores it, so the block can be deep without a large cost in computation.

How it works

Take one pixel of an image that has three channels, for example red, green and blue. Suppose we want two output channels from the same pixel, using the weights in the matrix WW below. There are no bias terms in this example.

x=[123],W=[10−10.510]x = \begin{bmatrix} 1 \\ 2 \\ 3 \end{bmatrix}, \qquad W = \begin{bmatrix} 1 & 0 & -1 \\ 0.5 & 1 & 0 \end{bmatrix}
  1. Read the pixel. The three channel values at this pixel are 11, 22 and 33. These form the vector xx. Check: xx has one entry for each input channel, which is 3.

  2. Compute the first output channel. Take the first row of WW, which is (1,0,−1)(1, 0, -1), and form its dot product with xx: multiply entry by entry, then add. 1⋅1+0⋅2+(−1)⋅3=−21 \cdot 1 + 0 \cdot 2 + (-1) \cdot 3 = -2. Check: the first output channel uses the first row of WW and nothing else.

  3. Compute the second output channel. Take the second row, which is (0.5,1,0)(0.5, 1, 0): 0.5⋅1+1⋅2+0⋅3=2.50.5 \cdot 1 + 1 \cdot 2 + 0 \cdot 3 = 2.5. Check: the output at this pixel is (−2,2.5)(-2, 2.5), which has 2 entries, one for each output channel.

  4. Repeat at every pixel with the same weights. A 2×22 \times 2 image has four pixels. The same WW is applied at each of them, so the output has shape 2×2×22 \times 2 \times 2 instead of 2×2×32 \times 2 \times 3. Check: the spatial size is unchanged, and only the channel count changed from 3 to 2.

  5. Note what did not happen. No pixel looked at a neighbour. Each output depends only on the channel values at its own position. Check: this is why the operation is cheap, and why it cannot find local shapes such as edges.

The rule in step 2 is the whole operation: at each pixel, the output is a matrix times the channel vector. Step 5 is the limit that makes it cheap.

flowchart LR
  A["Pixel with 3 channels"] --> B["Multiply by 2 by 3 weight matrix W"]
  B --> C["Pixel with 2 channels"]
  D["Same W at every pixel"] --> B

The math

Definition: Pointwise convolution

Let the input be a grid of size H×WH \times W with CinC_{\text{in}} channels, and let the kernel have size 1×11 \times 1 with weights W∈RCout×CinW \in \mathbb{R}^{C_{\text{out}} \times C_{\text{in}}} and bias b∈RCoutb \in \mathbb{R}^{C_{\text{out}}}. The output at pixel (h,w)(h, w) is the vector yh,w=Wxh,w+by_{h,w} = W x_{h,w} + b, where xh,wx_{h,w} is the channel vector of the input at (h,w)(h, w). In entries, the output channel kk is yh,w,k=∑cWk,c xh,w,c+bky_{h,w,k} = \sum_{c} W_{k,c}\, x_{h,w,c} + b_k.

Here Rm×n\mathbb{R}^{m \times n} is the set of real matrices with mm rows and nn columns, and the subscript in\text{in} or out\text{out} marks the input or output channel count. The math below follows the steps above.

Step 2 in symbols: each output is a dot product

Row kk of WW is a vector, and the output channel kk at a pixel is its dot product with the channel vector xh,wx_{h,w}, plus the bias. In the example, row 1 of WW dotted with xx gives −2-2.

Step 4 in symbols: the same weights everywhere

Because WW does not depend on (h,w)(h, w), the layer is a matrix multiplication applied to each pixel separately. A 1 by 1 convolution is therefore the same as a fully connected layer applied independently at every pixel. This is why it is also described as a feedforward layer that runs over the whole image.

Step 5 in symbols: the cost

Count the multiplications in a layer with no bias. A pointwise convolution on an H×WH \times W image costs H⋅W⋅Cin⋅CoutH \cdot W \cdot C_{\text{in}} \cdot C_{\text{out}} multiplications. A k×kk \times k convolution with the same channel counts and the same output size costs k2k^2 times that. For k=3k = 3, the factor is 99. The 1 by 1 kernel has the smallest possible spatial cost, which is why it is used to change channel counts before the larger filters.

Worked examples

Example
Example 1: one pixel, two outputs (tiny)

Use the pixel x=(2,−1,0)x = (2, -1, 0) with the weight matrix W=[11102−1]W = \begin{bmatrix} 1 & 1 & 1 \\ 0 & 2 & -1 \end{bmatrix} and no bias.

  1. Output channel 1 uses row (1,1,1)(1, 1, 1): 1⋅2+1⋅(−1)+1⋅0=11 \cdot 2 + 1 \cdot (-1) + 1 \cdot 0 = 1.
  2. Output channel 2 uses row (0,2,−1)(0, 2, -1): 0⋅2+2⋅(−1)+(−1)⋅0=−20 \cdot 2 + 2 \cdot (-1) + (-1) \cdot 0 = -2.
  3. The output pixel is (1,−2)(1, -2).
  4. Check the sizes: 3 input channels and 2 output rows give 2 output channels.

Interpretation: each output channel is a weighted sum of the input channels at one pixel, and no other pixel was used.

Example
Example 2: the cost on a 4 by 4 image (standard)

Take a 4×44 \times 4 input with 8 channels, and produce 16 output channels with a 1×11 \times 1 convolution. Count multiplications, ignoring bias.

  1. Number of pixels: H⋅W=4⋅4=16H \cdot W = 4 \cdot 4 = 16.
  2. Multiplications per pixel: Cin⋅Cout=8⋅16=128C_{\text{in}} \cdot C_{\text{out}} = 8 \cdot 16 = 128.
  3. Total: 16⋅128=204816 \cdot 128 = 2048 multiplications.
  4. For comparison, a 3×33 \times 3 convolution with the same channel counts and the same output size would need 9⋅2048=184329 \cdot 2048 = 18432 multiplications.
  5. The ratio is 2048/18432=1/92048 / 18432 = 1/9.

Interpretation: on the same grid the pointwise layer does one ninth of the work of the 3×33 \times 3 layer that it replaces, and it changes the channel count in the same way.

Try it: A 4×44 \times 4 input with 8 channels is passed through a 3×33 \times 3 convolution with 16 output channels. How many multiplications does it need? Use the same method as above.

Answer: 16⋅9⋅8⋅16=1843216 \cdot 9 \cdot 8 \cdot 16 = 18432.

Example
Example 3: a bottleneck block on a 28 by 28 map (realistic)

Compare two ways to build a block that maps 256 channels to 256 channels on a 28×2828 \times 28 map. Count multiplications, ignoring bias and activations. There are 28⋅28=78428 \cdot 28 = 784 pixels.

  1. Direct approach: one 3×33 \times 3 convolution with 256→256256 \to 256 channels costs 784⋅9⋅256⋅256=462,422,016784 \cdot 9 \cdot 256 \cdot 256 = 462{,}422{,}016.
  2. Bottleneck step 1: a 1×11 \times 1 convolution reduces 256→64256 \to 64, costing 784⋅256⋅64=12,845,056784 \cdot 256 \cdot 64 = 12{,}845{,}056.
  3. Bottleneck step 2: a 3×33 \times 3 convolution with 64→6464 \to 64 channels costs 784⋅9⋅64⋅64=28,901,376784 \cdot 9 \cdot 64 \cdot 64 = 28{,}901{,}376.
  4. Bottleneck step 3: a 1×11 \times 1 convolution expands 64→25664 \to 256, costing 784⋅64⋅256=12,845,056784 \cdot 64 \cdot 256 = 12{,}845{,}056.
  5. Total for the bottleneck: 12,845,056+28,901,376+12,845,056=54,591,48812{,}845{,}056 + 28{,}901{,}376 + 12{,}845{,}056 = 54{,}591{,}488.
  6. Ratio: 54,591,488/462,422,016≈0.11854{,}591{,}488 / 462{,}422{,}016 \approx 0.118, so the bottleneck needs about 12 percent of the work.

Interpretation: the two pointwise steps cost little, and the expensive 3×33 \times 3 step now works on 64 channels instead of 256.

Common mistakes and tradeoffs

It is tempting to think that a 1 by 1 Convolution does nothing useful, because it looks at a single pixel. In fact it mixes channels, which is a real operation, and it can change the channel count. Its job is different from a spatial filter, not absent.

It is tempting to think that a 1 by 1 convolution can detect edges or textures. In fact each output depends only on the channel values at one pixel, so it cannot see neighbours. Shapes and edges need a kernel larger than 1 by 1, which is why networks combine it with other convolutions.

It is tempting to think that the pointwise step is free because it is small. In fact the cost scales with the product of the input and output channel counts, so wide layers still need many multiplications. The saving comes from using it together with a cheaper spatial step, as in depthwise separable convolutions.

When to use a pointwise convolution: you want to change the channel count, mix channels cheaply, or reduce cost before a larger filter. When not to use one: the task needs spatial context, such as edges or local texture, because a 1 by 1 kernel cannot see neighbours. Alternatives: a 3×33 \times 3 or larger convolution when neighbours matter, a depthwise separable convolution when cost matters more than the last bit of accuracy, and a plain activation applied to each channel when no mixing is wanted.

History

Min Lin, Qiang Chen and Shuicheng Yan proposed the network in network in 2013. They used small multilayer networks at each position, and the 1 by 1 convolution is the same computation, so the idea of mixing channels per pixel dates from that work.

In 2014 Christian Szegedy and colleagues used 1 by 1 convolutions in the Inception architecture, to reduce the number of channels before larger filters. This made deep convolutional networks cheaper to run, and the same trick spread to other designs.

The 1 by 1 convolution caught on because it is cheap, simple to implement as a matrix multiplication, and useful in almost every modern convolutional network. It is now a standard building block for channel changes.

Recap

  • A pointwise convolution is a 1×11 \times 1 kernel. At each pixel it computes a matrix times the channel vector.
  • It mixes channels and changes their number, but it does not see neighbouring pixels.
  • Its cost is H⋅W⋅Cin⋅CoutH \cdot W \cdot C_{\text{in}} \cdot C_{\text{out}}, which is one ninth of a 3×33 \times 3 convolution with the same channels.
  • It is used for channel reduction in Inception and bottleneck blocks, and as the channel mixer in depthwise separable convolutions.
  • Read next: Depthwise Separable Convolution for the cheap spatial plus channel split, and Convolutional Neural Network for how these layers stack into a full network.

Check yourself

QuestionA pixel has 4 channels with values (1,0,2,−1)(1, 0, 2, -1). A pointwise convolution uses the row (1,1,0,1)(1, 1, 0, 1) with no bias. What is the output value for this output channel?Show answer

The dot product is 1⋅1+1⋅0+0⋅2+1⋅(−1)=1+0+0−1=01 \cdot 1 + 1 \cdot 0 + 0 \cdot 2 + 1 \cdot (-1) = 1 + 0 + 0 - 1 = 0. The output value is 0.

QuestionWhy can a 1×11 \times 1 convolution not detect an edge in an image?Show answer

Each output depends only on the channel values at one pixel. An edge is a change between neighbouring pixels, and the kernel never reads a neighbour, so the layer cannot compare pixels with each other.

QuestionA 1×11 \times 1 convolution maps 64 channels to 32 channels on a 10×1010 \times 10 map. How many multiplications does it need, ignoring bias?Show answer

There are 10⋅10=10010 \cdot 10 = 100 pixels. Each pixel needs 64⋅32=204864 \cdot 32 = 2048 multiplications. The total is 100⋅2048=204,800100 \cdot 2048 = 204{,}800.

QuestionA designer wants to lower the cost of a block that has a 3×33 \times 3 convolution with 256 input and 256 output channels. She proposes a 1×11 \times 1 convolution from 256 to 64 channels before it, and a 1×11 \times 1 convolution from 64 back to 256 after it. Why does this reduce the cost, and what does it give up?Show answer

The 3×33 \times 3 step now works on 64 channels, and its cost scales with the product of its input and output channels, so it becomes about (64⋅64)/(256⋅256)=1/16(64 \cdot 64)/(256 \cdot 256) = 1/16 of the original. The two 1×11 \times 1 steps are cheap by comparison. What is given up is that the 3×33 \times 3 step sees only 64 channels, so it has less capacity to represent patterns, and the reduction can lower accuracy if it is too strong.

Sources