PyTorch
Fold
Unfold
Deep Learning
Neural Networks

How does Pytorch's Fold and Unfold work?

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

PyTorch, an open-source machine learning library, provides various tools and operations to simplify deep learning tasks. Among these operations, torch.nn.functional.fold and torch.nn.functional.unfold play crucial roles in computer vision and image processing tasks. Understanding these functions is essential for efficiently manipulating mini-batches of image data during the forward and backward pass of neural networks.

Understanding Unfold

Unfold, also known as im2col (image to column), is particularly useful for extracting sliding local blocks from a batched input tensor. The operation returns all sliding blocks based on the parameters provided, such as kernel size and stride.

How Unfold Works

The Unfold operation takes a 4D tensor (batch size, channels, height, width) and extracts 2D patches from the input tensor based on specified parameters. These patches are stored as columns of a new 4D tensor.

Parameters

  • Kernel Size: Determines the height and width of each patch extracted from the input tensor.
  • Stride: The step size for moving the kernel across the input tensor.
  • Padding (optional): Determines the amount of padding added to both sides of the input.
  • Dilation (optional): Controls the spacing between kernel elements.

The operation can be expressed with the following equation:

 
(Output Size) = (frac(H + 2 × (Padding) - (Dilation) × ((Kernel Size) - 1) - 1)((Stride)) + 1)

Where H is the height (or width) of the image.

Example

python
1import torch
2
3# Define an input tensor with shape (batch_size, channels, height, width)
4input_tensor = torch.randn(1, 1, 4, 4)
5
6# Use unfold to extract 2x2 patches with a stride of 2
7patches = torch.nn.functional.unfold(input_tensor, kernel_size=(2, 2), stride=2)
8
9print("Unfolded Patches:", patches)

This code extracts 2x2 patches sliding over the input tensor.

Understanding Fold

Fold is the inverse of Unfold. After processing extracted patches, one might need to reconstruct the original structure of input data. Fold allows for the reconstruction of the original signal from its parts by reversing the im2col operation.

How Fold Works

Fold operation takes in a tensor of smaller patches, along with the original image dimensions, and reconstructs the image by placing these patches at the respective locations.

Parameters

  • Output Size: The original size of the spatial dimensions of the input tensor.
  • Kernel Size: Must correspond to the size used during Unfold.
  • Stride: Must match the stride in Unfold.
  • Padding (optional): Padding used during Unfold.

This operation is expressed based on:

 
(Reconstructed Output) = (Fold)( (Extracted Patches), (Output Shape))

Consider any overlapping regions during folding; these are typically added together.

Example

python
1# Define unfolded tensor (result from previous unfold operation)
2unfolded_tensor = torch.randn(1, 4, 4)  # Shape: [batch_size, channels * kernel_height * kernel_width, num_patches]
3
4# Fold back into the original shape
5original_tensor = torch.nn.functional.fold(unfolded_tensor, output_size=(4, 4), kernel_size=(2, 2), stride=2)
6
7print("Reconstructed Original Tensor:", original_tensor)

Applications in Deep Learning

The Unfold and Fold operations are vital for implementing efficient batch-wise operations:

  • Convolution Backward Pass: Involves using Unfold for extracting patches and performing matrix multiplication for gradient calculation.
  • Image Processing: Extract and reconstruct overlapping fields for smoothing, sharpening, or detection tasks.
  • Attention Mechanisms: Many attention-based networks implement Unfold and Fold to handle non-standard, context-aware patch operations.

Table: Key Differences Between Unfold and Fold

FeatureUnfoldFold
DirectionInput to PatchesPatches to Input
PurposeExtract local patchesReconstruct image from patches
Common UsageIm2col for ConvolutionCol2im for inversing Convolution operations
Input Shape(Batch, Channel, Height, Width)(Batch, Folded_Dim, Num_Patches)
Output Shape(Batch, Channel * Kernel, Num_Patches)(Batch, Channel, Height, Width)

Understanding Fold and Unfold within PyTorch allows for explicit control over data reshaping and manipulation of tensor dimensions, optimizing how convolutional neural networks process and learn from image data. This knowledge sets the groundwork for efficient and sophisticated model implementations.


Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

All Rights Reserved.