Sub topic 9
Dilated Convolutions and Multi-Scale Context
Assume you know basic convolutional neural networks, feature maps, and gradient-based training. This chapter explains how segmentation models capture both fine boundaries and broad image context.
Why context matters
A segmentation model must answer two questions:
- What class does this pixel belong to?
- Which surrounding pixels provide useful evidence?
A small convolution sees local texture. A large receptive field sees more of the scene. The receptive field is the region of the input that can affect one feature value. For example, recognizing a road pixel may require nearby edges and a wider view of the road surface.
Downsampling increases context efficiently, but it reduces spatial detail. Upsampling can restore the feature map size, but it cannot fully recover details that were discarded. Dilated convolutions provide another option.
How dilation expands the receptive field
A dilated convolution inserts gaps between kernel elements. The dilation rate, usually written as r, controls the spacing. A 3-by-3 kernel has an effective size of:
effective_size = 3 + (3 - 1)(r - 1)
A rate of 1 is an ordinary convolution. A rate of 2 covers a 5-by-5 region, and a rate of 4 covers a 9-by-9 region. The layer still has only nine kernel weights, so it expands context without proportionally increasing parameter count.
| Dilation rate | Effective size of a 3-by-3 kernel | Typical role |
|---|---|---|
| 1 | 3 by 3 | Local edges and textures |
| 2 | 5 by 5 | Nearby object context |
| 4 | 9 by 9 | Wider scene context |
import torch
from torch import nn
class ContextBlock(nn.Module):
def __init__(self, channels):
super().__init__()
self.layers = nn.Sequential(
nn.Conv2d(channels, channels, 3, padding=1, dilation=1),
nn.ReLU(),
nn.Conv2d(channels, channels, 3, padding=2, dilation=2),
nn.ReLU(),
nn.Conv2d(channels, channels, 3, padding=4, dilation=4),
)
def forward(self, x):
return self.layers(x)
x = torch.randn(1, 32, 64, 64)
y = ContextBlock(32)(x)
print(y.shape) # torch.Size([1, 32, 64, 64])Padding must match the dilation so that the feature map keeps its height and width. For a 3-by-3 kernel, padding normally equals the dilation rate.
Multi-scale context
One dilation rate cannot fit every object. A small object benefits from local features, while a large object needs broader context. Models therefore combine several receptive-field sizes.
A common design is an atrous spatial pyramid pooling module, or ASPP. It applies parallel convolutions with different dilation rates and combines their outputs. Other designs use feature pyramids, encoder features at several resolutions, or image-level pooling.
Practical trade-offs
Dilated layers preserve feature-map resolution, but high-resolution maps consume memory. They also increase computation compared with aggressively downsampled features. Choose dilation when boundary detail matters and GPU memory allows it. Combine it with skip connections or multi-scale features when objects vary greatly in size.
Exercise
Why might a segmentation model combine dilation rates of 1, 6, and 12 instead of using only rate 12?
Show answer
The smaller rates capture local edges and thin structures, while the larger rates capture wider context. Combining them reduces the risk that a sparse, large-rate kernel misses important details.