How to pad the input tensor boundaries with a constant value in PyTorch?


The torch.nn.ConstantPad2D() pads the input tensor boundaries with constant value. The size of the input tensor must be in 3D or 4D in (C,H,W) or (N,C,H,W) format respectively. Where N,C,H,W represents the mini batch size, number of channels, height and width respectively. The padding is done along the height and width of the input tensor.

It takes the size of padding (padding) and constant values (value) as the parameters. The size of padding may be an integer or a tuple. The padding may be the same for all boundaries or different for each boundary.

The padding may be an integer or a tuple in (left, right, top, bottom) format. If padding is an integer, the padding along all boundaries are the same.

The height of the padded tensor is increased by top+bottom, whereas the width of the padded tensor is increased by left+right. It does not change the channel size or batch size. Padding is generally used in convolutional neural networks (CNNs) after Pooling layers to maintain the input size.

Syntax

torch.nn.ConstantPad2D(padding, value)

Parameters

  • padding – Desired size of padding. An integer or a tuple in (left, right, top, bottom) format.

  • value – The constant values. The input tensor is padded with this value.

Steps

We could use the following steps to pad the input tensor boundaries with a constant value −

  • Import the required library. In all the following examples, the required Python library is torch. Make sure you have already installed it.

import torch
  • Define the input tensor. We define a 4D tensor as below.

input = torch.randn(2, 1, 3, 3)
  • Define a padding size padding and constant value value, and pass it to torch.nn.ConstantPad2D() and create an instance pad to pad the tensor with constant value value. The padding size may be the same or different padding size.

padding = (2,1)
value = 3
pad = nn.ConstantPad2d(padding, value)
  • Pad the input tensor with constant value value using the above-created instance pad.

output = pad(input)
  • Print the final padded tensor.

print("Padded Ternsor:
", output)

Example 1

In the following Python example, we pad 3D and 4D tensors with a constant value value=3 and integer padding size of 2, i.e., padding=2

# Import the required library
import torch
import torch.nn as nn

# define 3D tensor (C,H,W)
input = torch.tensor([[[ 1, 2],[ 3, 4]]])
print("Input Tensor:
",input) # define padding same for all sides (left, right, top, bottom) # nn.ConstantPad(padding, value) pad = nn.ConstantPad2d(2, 3) # pad the input tensor output = pad(input) print("Padded Ternsor:
", output) # define same padding only for left and right sides pad = nn.ConstantPad2d((2,2), 3) # pad the input tensor output = pad(input) print("Padded Tensor:
", output)

Output

Input Tensor:
   tensor([[[1, 2],
      [3, 4]]])
Padded Tensor:
   tensor([[[3, 3, 3, 3, 3, 3],
      [3, 3, 3, 3, 3, 3],
      [3, 3, 1, 2, 3, 3],
      [3, 3, 3, 4, 3, 3],
      [3, 3, 3, 3, 3, 3],
      [3, 3, 3, 3, 3, 3]]])
Padded Tensor:
   tensor([[[3, 3, 1, 2, 3, 3],
      [3, 3, 3, 4, 3, 3]]])

Example 2

In the following Python example, we pad 3D and 4D tensors with a constant value using padding sizes different for all boundaries of the input tensor.

# Import the required library
import torch
import torch.nn as nn

# define 3D tensor (C,H,W)
input = torch.tensor([[[ 1, 2],[ 3, 4]]])
print("Input Tensor:
", input) # define padding different for different sides padding = (2,1,2,1) value = 3 pad = nn.ConstantPad2d(padding,value) # pad the input tensor output = pad(input) print("Padded Ternsor:
", output) input = torch.tensor([[[ 1, 2],[ 3, 4]]]) print("Input Tensor:
",input) # define padding different for left and right sides padding = (2,1) value = 3 pad = nn.ConstantPad2d(padding,value) # pad the input tensor output = pad(input) print("Padded Tensor:
", output) # define 4D tensor (N,C,H,W)->for a batch of N tensors input = torch.tensor([[[ 1, 2],[ 3, 4]],       [[ 1, 2],[ 3, 4]]]) print("Input Tensor:
",input) # define padding different for different sides padding = (2,2,1,1) value = 7 pad = nn.ConstantPad2d(padding, value) # pad the input tensor output = pad(input) print("Padded Tensor:
", output)

Output

Input Tensor:
   tensor([[[1, 2],
      [3, 4]]])
Padded Tensor:
   tensor([[[3, 3, 3, 3, 3],
      [3, 3, 3, 3, 3],
      [3, 3, 1, 2, 3],
      [3, 3, 3, 4, 3],
      [3, 3, 3, 3, 3]]])
Input Tensor:
   tensor([[[1, 2],
      [3, 4]]])
Padded Tensor:
   tensor([[[3, 3, 1, 2, 3],
      [3, 3, 3, 4, 3]]])
Input Tensor:
   tensor([[[1, 2],
      [3, 4]],
      [[1, 2],
      [3, 4]]])
Padded Ternsor:
   tensor([[[7, 7, 7, 7, 7, 7],
      [7, 7, 1, 2, 7, 7],
      [7, 7, 3, 4, 7, 7],
      [7, 7, 7, 7, 7, 7]],
      [[7, 7, 7, 7, 7, 7],
      [7, 7, 1, 2, 7, 7],
      [7, 7, 3, 4, 7, 7],
      [7, 7, 7, 7, 7, 7]]])

Updated on: 25-Jan-2022

445 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements