1x1 and Depthwise Convolution
A full convolutional layer mixes information across space and across channels at once. Two special cases keep one kind of mixing and drop the other:
- A convolution, or pointwise convolution, has kernel size . It mixes the channels at each pixel and never looks at neighboring pixels.
- A depthwise convolution gives each of the channels its own filter. It filters each channel over space and never mixes channels.
For output channel at pixel , the convolution computes
It is the same dense layer applied to the channel vector at every pixel. It changes the number of channels without changing the height or width. Ignoring biases, the weight counts for input and output channels are:
- Full convolution: .
- convolution: .
- Depthwise convolution, with : .
Intuition
A full convolution answers two questions at once: which patterns appear nearby, and how the channels combine. Splitting the two questions saves weights. A convolution handles only the channels. It acts like a small dense layer that looks down through the stack of feature maps at one pixel. A depthwise convolution handles only space. It runs a separate filter over each feature map. This is like applying a blur or edge filter to each color of an image on its own. Chaining the two recovers most of what a full layer does, at a fraction of the cost.
Properties
(A Dense Layer at Every Pixel) A convolution applies the same small dense layer to the channel vector at every pixel.
With one input and one output channel it scales every pixel by a learned constant and adds a learned bias. With many channels it computes, at each pixel, a weighted sum of all channels for each output channel. It never looks at neighboring pixels, so it does not enlarge the receptive field. Networks use it in three roles:
- Matching shapes: it changes the channel count so that two tensors can be added across a residual connection.
- Reducing cost: it shrinks the channels before an expensive layer.
- Per-pixel outputs: at the end of a segmentation network it turns each pixel’s features into class scores.
(Bottleneck Blocks) Shrinking the channels with a layer before a layer, then restoring them, saves most of the weights.
A bottleneck residual block has three convolutions. The first is and reduces the channels, for example from to . The second is and works on the channels. The third is and restores channels so the result can be added to the block’s input. The weights are . One full layer at channels has , about times as many. The deep ResNets in Prince, such as ResNet-200, are built from bottleneck blocks.
(Depthwise Filters Each Channel Separately) A depthwise convolution divides the weight count by but removes all mixing between channels.
Each input channel is cross-correlated with its own filter, , and the results are stacked. A input filtered by three kernels without padding gives an output with weights. A full convolution with three output channels would need . No output channel sees more than one input channel. The layer therefore cannot detect a pattern that spans channels, such as a red-green edge. Mixing information across channels needs a separate layer, usually a convolution.
(A Spectrum of Channel Mixing) Grouped convolutions interpolate between a full convolution and a depthwise one.
Not Taught in Lecture
Grouped convolutions appear only in Prince’s chapter notes. Depthwise separable convolution was popularized by Xception (Chollet, 2017) and MobileNets (Howard et al., 2017).
A grouped convolution splits the input channels into groups. Each group is convolved only with its own share of the output channels. Its weight count is . Two settings of give familiar layers:
- is a full convolution.
- is a depthwise convolution.
AlexNet used to split some of its layers across two GPUs. A depthwise separable convolution follows a depthwise layer with a layer. It has weights, a fraction of a full layer. With and channels in and out, that is about . The factorization assumes that spatial filtering and channel mixing can be done one after the other. This is a constraint, though it costs little accuracy in practice.
Examples
(Easy) A 1x1 Convolution at One Pixel
A convolution maps input channels to output channels and uses a ReLU. Writing its weights as a matrix,
One pixel of the input has channel values .Compute the output at this pixel, and count the parameters for a input. The pre-activation is . The ReLU gives . Every other pixel uses the same and . The layer therefore has parameters for a input or any other size. The output is .
(Medium) Depthwise, Full and Separable
A input is filtered with kernels, no padding and stride . The goal is output channels.
Count the weights of a full convolution and of a depthwise convolution followed by a convolution. What are the output shapes? The spatial output is along each axis. A full layer has weights and outputs . The depthwise layer has weights and outputs . The layer then maps channels to with weights and keeps the size. The separable pair has weights in total, about a sixth of the full layer, and gives the same output shape. The full layer can learn any filter for each output channel. The separable pair is more limited. Each input channel gets one spatial pattern, and the output channels differ only in how they weight these patterns.
(Medium) Where the Bottleneck Saves
A residual block works on channels. One design uses two layers at channels. Another uses a bottleneck: from to , at channels, and back to .
Compare the weight counts and the receptive fields of the two blocks. weights. The bottleneck has , about times fewer. The layers add nothing to the receptive field. The bottleneck therefore grows it by pixels, while the plain block grows it by . The saved weights allow more blocks, which is how bottleneck ResNets reach hundreds of layers.
The plain block has
See Also
- 5.2.1. Convolutional Layer, the general layer these are special cases of.
- 5.2.5. Residual Networks, where convolutions match shapes and form bottleneck blocks.
- 5.2.4. CNNs, for the architectures that use them.
- 4.5.3. Group Normalization, which groups channels in the same way for normalization.