Press ESC to exit fullscreen
📖 Lesson ⏱️ 90 minutes

Dilation and Upconvolution in PyTorch

Implementing dilation and upconvolution in PyTorch

Introduction

Dilation and upconvolution (transposed convolution) extend CNN capabilities in PyTorch, helping capture large context or reconstruct higher-resolution feature maps.


1️⃣ Dilation in PyTorch

What is Dilation?

Dilation expands the receptive field by inserting spaces between kernel elements without increasing parameters, helping capture larger context.

Implementation:

import torch
import torch.nn as nn

dilated_conv = nn.Conv2d(in_channels=3, out_channels=16, kernel_size=3, dilation=2, padding=2)
x = torch.randn(1, 3, 64, 64)
output = dilated_conv(x)
print("Dilated convolution output shape:", output.shape)

Here:

dilation=2 doubles the receptive field.
padding=2 preserves the spatial dimension.


2️⃣ Upconvolution (Transposed Convolution) in PyTorch

What is Upconvolution?

Upconvolution (transposed convolution) learns how to upsample feature maps, increasing spatial dimensions, and is commonly used in:

✅ Semantic segmentation (U-Net, DeepLab).
✅ Image generation (GANs).
✅ Super-resolution.

Implementation:

upconv = nn.ConvTranspose2d(in_channels=16, out_channels=3, kernel_size=2, stride=2)
x_up = torch.randn(1, 16, 32, 32)
output_up = upconv(x_up)
print("Upconvolution output shape:", output_up.shape)

Here:

kernel_size=2, stride=2 will double the spatial dimensions.


3️⃣ Combined Example

# Dilated convolution followed by upconvolution
x = torch.randn(1, 3, 64, 64)
dilated_output = dilated_conv(x)

upconv = nn.ConvTranspose2d(in_channels=16, out_channels=3, kernel_size=2, stride=2)
upconv_output = upconv(dilated_output)

print("Dilated output shape:", dilated_output.shape)
print("Upconvolution output shape:", upconv_output.shape)

4️⃣ Use Cases

Segmentation: Preserve spatial resolution while expanding context.
Generative models: Reconstruct images from feature maps.
Super-resolution: Upsample low-resolution inputs effectively.


Conclusion

Dilation helps capture a larger context efficiently.
Upconvolution allows learnable upsampling for high-resolution tasks.
✅ Mastering these PyTorch techniques expands your toolkit for advanced deep learning projects.


What’s Next?

✅ Implement these techniques in your segmentation pipelines.
✅ Experiment with different dilation rates and transposed convolution configurations.
✅ Continue structured deep learning on superml.org.


Join the SuperML Community to share your experiments and projects.


Happy Building with PyTorch! 🚀