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 below. There are no bias terms in this example.
-
Read the pixel. The three channel values at this pixel are , and . These form the vector . Check: has one entry for each input channel, which is 3.
-
Compute the first output channel. Take the first row of , which is , and form its dot product with : multiply entry by entry, then add. . Check: the first output channel uses the first row of and nothing else.
-
Compute the second output channel. Take the second row, which is : . Check: the output at this pixel is , which has 2 entries, one for each output channel.
-
Repeat at every pixel with the same weights. A image has four pixels. The same is applied at each of them, so the output has shape instead of . Check: the spatial size is unchanged, and only the channel count changed from 3 to 2.
-
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
Let the input be a grid of size with channels, and let the kernel have size with weights and bias . The output at pixel is the vector , where is the channel vector of the input at . In entries, the output channel is .
Here is the set of real matrices with rows and columns, and the subscript or 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 of is a vector, and the output channel at a pixel is its dot product with the channel vector , plus the bias. In the example, row 1 of dotted with gives .
Step 4 in symbols: the same weights everywhere
Because does not depend on , 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 image costs multiplications. A convolution with the same channel counts and the same output size costs times that. For , the factor is . 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
Use the pixel with the weight matrix and no bias.
- Output channel 1 uses row : .
- Output channel 2 uses row : .
- The output pixel is .
- 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.
Take a input with 8 channels, and produce 16 output channels with a convolution. Count multiplications, ignoring bias.
- Number of pixels: .
- Multiplications per pixel: .
- Total: multiplications.
- For comparison, a convolution with the same channel counts and the same output size would need multiplications.
- The ratio is .
Interpretation: on the same grid the pointwise layer does one ninth of the work of the layer that it replaces, and it changes the channel count in the same way.
Try it: A input with 8 channels is passed through a convolution with 16 output channels. How many multiplications does it need? Use the same method as above.
Answer: .
Compare two ways to build a block that maps 256 channels to 256 channels on a map. Count multiplications, ignoring bias and activations. There are pixels.
- Direct approach: one convolution with channels costs .
- Bottleneck step 1: a convolution reduces , costing .
- Bottleneck step 2: a convolution with channels costs .
- Bottleneck step 3: a convolution expands , costing .
- Total for the bottleneck: .
- Ratio: , so the bottleneck needs about 12 percent of the work.
Interpretation: the two pointwise steps cost little, and the expensive 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 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 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 , which is one ninth of a 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 . A pointwise convolution uses the row with no bias. What is the output value for this output channel?Show answer
The dot product is . The output value is 0.
QuestionWhy can a 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 convolution maps 64 channels to 32 channels on a map. How many multiplications does it need, ignoring bias?Show answer
There are pixels. Each pixel needs multiplications. The total is .
QuestionA designer wants to lower the cost of a block that has a convolution with 256 input and 256 output channels. She proposes a convolution from 256 to 64 channels before it, and a convolution from 64 back to 256 after it. Why does this reduce the cost, and what does it give up?Show answer
The step now works on 64 channels, and its cost scales with the product of its input and output channels, so it becomes about of the original. The two steps are cheap by comparison. What is given up is that the 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
- Min Lin, Qiang Chen, Shuicheng Yan, "Network In Network", arXiv: https://arxiv.org/abs/1312.4400
- Christian Szegedy et al., "Going Deeper with Convolutions", arXiv: https://arxiv.org/abs/1409.4842