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! πŸš€