AI Logic

Convolution Types

Dilated Convolution (Atrous Convolution)

Updated:

How the dilation rate changes which input cells a kernel uses. From the left: which cells of a 9×9 input a 3×3 kernel with dilation rate 1, 2, and 3 uses, and which output cell it produces. On the far right: stacking layers with dilation rates 1, 2, and 4 grows the receptive field to 3×3, 7×7, and 15×15

Background and Overview

Dilated convolution is a convolution that applies the kernel weights to the input with gaps, such as every other cell or every third cell. It keeps the same number of weights as an ordinary convolution while widening the range a single convolution can see (the receptive field). The value that sets the spacing is called the dilation rate (dilation factor) ll; with l=1l = 1 the operation is an ordinary convolution.

Sweeping a desk with your fingers held together touches only a palm-width strip, but spreading your fingers lets the same five fingers cover a much wider area. Dilated convolution applies this "spread the fingers" idea to a convolution kernel.

Image classification networks gather information from wide regions while shrinking the feature maps with pooling and strides. Dense prediction tasks such as semantic segmentation, which assigns a class to every pixel, need the output to stay at a fine resolution. Yu and Koltun (2016) used dilated convolution systematically as a way to aggregate wide context without lowering resolution, and showed that stacking layers with dilation rates 1, 2, 4, … makes the receptive field grow exponentially.

3×3 kernelNumber of weightsRange seen by one layer
Ordinary convolution (l=1l = 1)93 × 3
Dilated convolution (l=2l = 2)95 × 5
Dilated convolution (l=3l = 3)97 × 7

Dilated convolution is also known as "atrous convolution." The name comes from the French à trous ("with holes") and refers to the operation used in the "algorithme à trous" for wavelet decomposition. The DeepLab family uses this name.

It is sometimes described as "building a larger kernel by inserting zeros between the weights," but Yu and Koltun define it as changing how the convolution operator applies the weights, without ever building a larger kernel. The result is the same, but skipping the multiplications by zero makes it more efficient to compute.

Definition and Computation

Let the input be a discrete function FF and let kk be a kernel of radius rr (size (2r+1)2(2r+1)^2). An ordinary convolution can be written as follows (Equation 1 in Yu and Koltun).

(F∗k)(p)=∑s+t=pF(s) k(t)(F * k)(\boldsymbol{p}) = \sum_{\boldsymbol{s} + \boldsymbol{t} = \boldsymbol{p}} F(\boldsymbol{s})\, k(\boldsymbol{t})

A dilated convolution with dilation rate ll multiplies the kernel position t\boldsymbol{t} by ll before applying it to the input (Equation 2).

(F∗lk)(p)=∑s+lt=pF(s) k(t)(F *_l k)(\boldsymbol{p}) = \sum_{\boldsymbol{s} + l\boldsymbol{t} = \boldsymbol{p}} F(\boldsymbol{s})\, k(\boldsymbol{t})

Each output value is computed in the following steps.

  1. Pick the center: The input position corresponding to the output position p\boldsymbol{p} becomes the center of the kernel.
  2. Gather inputs with gaps: For each kernel position t\boldsymbol{t}, take the input value located ltl\boldsymbol{t} away from the center. A 3×3 kernel gathers 9 cells, with l−1l - 1 cells skipped between neighbors.
  3. Multiply by the weights and sum: Multiply the 9 gathered values by the kernel weights and add them up to get one output cell.

The range actually covered by a k×kk \times k kernel (the effective kernel size) is as follows (Wang et al., 2018).

kd=k+(k−1)(l−1)k_d = k + (k - 1)(l - 1)

The output size follows the formula in the PyTorch nn.Conv2d documentation (HH is the height; the width uses the same formula).

Hout=⌊Hin+2×padding−dilation×(kernel_size−1)−1stride+1⌋H_{\text{out}} = \left\lfloor \frac{H_{\text{in}} + 2 \times \text{padding} - \text{dilation} \times (\text{kernel\_size} - 1) - 1}{\text{stride}} + 1 \right\rfloor

To keep the size unchanged at stride 1, set padding=dilation×(kernel_size−1)/2\text{padding} = \text{dilation} \times (\text{kernel\_size} - 1) / 2. For a 3×3 kernel, setting the padding equal to the dilation rate makes the input and output sizes match (for example, with Hin=64H_{\text{in}} = 64, l=2l = 2, and padding 2, ⌊(64+4−4−1)/1+1⌋=64\lfloor (64 + 4 - 4 - 1)/1 + 1 \rfloor = 64).

Why the Receptive Field Grows While Resolution Is Preserved

The advantage of dilated convolution is that stacked layers grow the receptive field exponentially, without leaving gaps. Yu and Koltun considered stacking 3×3 kernels with dilation rates 2i2^i (Equation 3).

Fi+1=Fi∗2iki(i=0,1,…,n−2)F_{i+1} = F_i *_{2^i} k_i \quad (i = 0, 1, \ldots, n-2)

The receptive field of each cell of Fi+1F_{i+1} is then (2i+2−1)×(2i+2−1)(2^{i+2} - 1) \times (2^{i+2} - 1). Stacking three layers with dilation rates 1, 2, and 4 grows the receptive field to 3×3, 7×7, and 15×15. Since every layer has the same number of weights, the parameters grow only linearly with the number of layers, while the receptive field grows exponentially.

The paper used this to build a "context module": a stack of 3×3 convolutions with increasing dilation rates that never changes the size of the feature map (Table 1 in the paper).

Layer12345678
Kernel3×33×33×33×33×33×33×31×1
Dilation112481611
Receptive field3×35×59×917×1733×3365×6567×6767×67

Each layer is followed by a ReLU (max⁡(⋅,0)\max(\cdot, 0)), except the final 1×1 convolution. Because the front-end network outputs 64×64 maps, the receptive field stops growing at layer 6. In the basic form every layer has CC channels, and the whole module has only about 64C264C^2 parameters.

Compared with lowering the resolution by pooling and restoring it later, dilated convolution differs in that it never discards fine positional information. The paper applied the same idea when adapting the classification network VGG-16 to dense prediction: it removed the last two pooling layers and strides, and for each removed pooling layer doubled the dilation rate of all subsequent convolutions. This raises the output resolution while still allowing the original classification weights to be used as initialization.

In the paper, training the context module with ordinary random initialization did not improve accuracy. Switching to identity initialization, which sets the weights so that each layer passes its input straight through (kb(t,a)=1[t=0]1[a=b]k^b(\boldsymbol{t}, a) = 1_{[\boldsymbol{t} = 0]} 1_{[a = b]}, where aa is the input channel and bb the output channel), let training make effective use of context. If a deep stack of dilated convolutions is hard to train, revisit the initialization.

Experimental Results

Yu and Koltun evaluated on Pascal VOC 2012 semantic segmentation, using mean IoU (%) as the metric.

First, the front-end network alone, built from VGG-16 by removing the extra pooling layers and replacing them with dilated convolutions, outperformed earlier adaptations (trained on VOC 2012, test set, Table 2 in the paper).

ModelMean IoU
FCN-8s62.2
DeepLab62.1
DeepLab-Msc62.9
Front end (dilated convolution)67.6

Next, they added the context module to a front end trained with additional Microsoft COCO images and compared on the validation set (Table 3 in the paper). With or without post-processing (CRF, CRF-RNN), adding the context module improves accuracy.

Post-processingFront end only+ Basic context module+ Large context module
None69.871.372.1
CRF71.672.773.3
CRF-RNN72.573.173.9

On the test set (Table 4 in the paper), adding the large context module raised the score from 71.3 for the front end alone to 73.5, and combining it with CRF-RNN reached 75.3. For comparison, CRF-RNN alone scored 74.7 and DeepLab-CRF-COCO-LargeFOV scored 72.7.

Practical Considerations

Stacking the same dilation rate causes grid-like holes (gridding). A 3×3 kernel with dilation rate 2 uses only 9 cells out of a 5×5 region. Stacking layers with the same rate keeps the used cells scattered like a checkerboard, so neighboring outputs can be computed from entirely different sets of cells (Wang et al., 2018). Wang et al. propose varying the dilation rates of consecutive layers, such as 1, 2, 3 or 1, 2, 5, so that the rates share no common factor greater than 1 (avoiding combinations like 2, 4, 8). This is called Hybrid Dilated Convolution.

  • Computation and memory do not shrink: The number of weights stays the same, but preserving resolution keeps the feature maps large. According to Wang et al., the common practice is to apply dilated convolution to feature maps that have already been downsampled to some extent, balancing efficiency and accuracy.
  • Match the padding to the dilation rate: To keep the size with a 3×3 kernel, set the padding equal to the dilation rate. Leaving padding=1 as in an ordinary convolution shrinks the output.
  • It may not help with small objects or thin lines: The larger the dilation rate, the more nearby information is skipped. The usual approach is to keep dilation rate 1 in shallow layers and widen it in deeper layers (the paper's context module also uses dilation rate 1 in layers 1 and 2).
  • Watch the borders: Because the receptive field is large, many cells near the image border fall on the padding. The paper padded the context module's input with a 33-cell margin and reported that zero padding and reflection padding gave similar results.

Comparison with Other Methods and How to Choose

The table below compares methods that enlarge the receptive field or change how the kernel is applied, from the same viewpoints.

MethodHow the receptive field growsResolutionWeights (3×3 equivalent)Caveats
Ordinary convolution (large kernel)Larger kernelPreservedGrows as k2k^2 (49 for 7×7)More weights and computation
Pooling / strideDownsample, then convolveReduced9Loses fine positional information; restoring it needs transposed convolution or similar
Dilated convolutionGaps between kernel tapsPreserved9Gridding; feature maps stay large, so computation does not drop
Deformable convolutionLearns per-input offsets for the sampling positionsPreserved9 (+ a layer that predicts offsets)Heavier implementation and more computation

Rough guidelines for choosing:

  • Per-pixel prediction that needs wide context at full resolution: Use dilated convolution. It is a standard choice for semantic segmentation.
  • Classification networks where computation should stay low: Downsample with pooling or strides. Dilated convolution is heavier because its feature maps stay large.
  • Adapting to changes in object shape or orientation: Consider deformable convolution. In dilated convolution, the sampling positions are fixed to a grid determined by the dilation rate.

Dilated convolution is also combined with depthwise separable convolution. For example, the search space of DARTS includes 3×3 and 5×5 dilated separable convolutions. Outside images, WaveNet, which generates audio, stacks dilated convolutions to refer to a long span of past samples (Dumoulin and Visin, 2016).

PyTorch example

Show code
python
import torch
import torch.nn as nn

# Dilated convolution: in PyTorch, set the dilation rate with the dilation argument of nn.Conv2d

x = torch.randn(1, 16, 64, 64)  # [batch, channels, height, width]

# 3×3 convolution with dilation rate 2. To keep the size, set the padding equal to the dilation rate
conv = nn.Conv2d(16, 16, kernel_size=3, dilation=2, padding=2)
print(conv(x).shape)  # torch.Size([1, 16, 64, 64])

# With padding=1, the size shrinks to (64 + 2 - 2*2 - 1) / 1 + 1 = 62
print(nn.Conv2d(16, 16, kernel_size=3, dilation=2, padding=1)(x).shape)  # [1, 16, 62, 62]

# The number of weights does not depend on the dilation rate (16 * 16 * 3 * 3 + 16)
count = lambda m: sum(p.numel() for p in m.parameters())
print(count(nn.Conv2d(16, 16, 3, dilation=1, padding=1)), count(conv))  # 2320 2320


# Yu & Koltun's basic context module (Table 1): 3×3 with dilation 1, 1, 2, 4, 8, 16, 1, then a final 1×1
# The paper pads the input and uses no padding in between; here each layer pads to keep the size
class ContextModule(nn.Module):
  def __init__(self, channels, dilations=(1, 1, 2, 4, 8, 16, 1)):
    super().__init__()
    layers = []
    for l in dilations:
      layers += [nn.Conv2d(channels, channels, 3, dilation=l, padding=l), nn.ReLU()]
    layers.append(nn.Conv2d(channels, channels, 1))  # no ReLU after the last layer
    self.body = nn.Sequential(*layers)
    self.identity_init()

  def identity_init(self):
    # Identity initialization from Equation 4: only the center weight between matching channels is 1
    for m in self.body:
      if isinstance(m, nn.Conv2d):
        nn.init.zeros_(m.weight)
        nn.init.zeros_(m.bias)
        c = m.kernel_size[0] // 2
        for i in range(m.out_channels):
          m.weight.data[i, i, c, c] = 1.0

  def forward(self, x):
    return self.body(x)


# Feeding a 21-class score map (example) leaves the size unchanged
module = ContextModule(21)
scores = torch.relu(torch.randn(1, 21, 64, 64))
print(module(scores).shape)                   # torch.Size([1, 21, 64, 64])
print(torch.allclose(module(scores), scores))  # at initialization it returns the input as is: True

FAQ

Q. How does dilated convolution differ from a convolution with a large kernel?
A.

Even when they cover the same range, they use a different number of cells. A 3×3 kernel with dilation rate 3 looks at a 7×7 region with 9 weights, while a 7×7 kernel looks at every cell with 49 weights. Dilated convolution uses fewer weights and less computation, but it does not look directly at the cells in between.

Q. How should the dilation rate be chosen?
A.

Use larger rates in deeper layers where the receptive field should grow. Yu and Koltun's context module doubles the rate: 1, 1, 2, 4, 8, 16, 1. Stacking many layers with the same rate causes grid-like holes, so Wang et al. recommend combinations without a common factor, such as 1, 2, 3.

Q. Are atrous convolution and dilated convolution the same thing?
A.

Yes, they are the same operation. The DeepLab family calls it atrous, while Yu and Koltun call it dilated. In PyTorch it is set with the dilation argument of nn.Conv2d.

References

  1. F. Yu and V. Koltun, "Multi-Scale Context Aggregation by Dilated Convolutions," ICLR, 2016.
  2. P. Wang, P. Chen, Y. Yuan, D. Liu, Z. Huang, X. Hou, and G. Cottrell, "Understanding Convolution for Semantic Segmentation," WACV, 2018.
  3. V. Dumoulin and F. Visin, "A guide to convolution arithmetic for deep learning," arXiv:1603.07285, 2016.
  4. PyTorch Documentation, "torch.nn.Conv2d."