Friday, October 24, 2025
HomeLanguagesHow to Apply a 2D Average Pooling in PyTorch?

How to Apply a 2D Average Pooling in PyTorch?

In this article, we will see how to apply a 2D average pooling in PyTorch

AvgPool2d() method

AvgPool2d() method of torch.nn module is used to apply 2D average pooling over an input image composed of several input planes in PyTorch. The shape of the input 2D average pooling layer should be [N, C, H, W]. Where N represents the batch size, C represents the number of channels, and H, W represents the height and width of the input image respectively. The below syntax is used to apply 2D average pooling.

Syntax: torch.nn.AvgPool2d(kernel_size, stride)

Parameter:

  • kernel_size: This is size of the window
  • stride: This is stride of the window. The default value of stride is kernel_size.

Example 1:

Image used for demonstration:

neveropen

 

In this example, we are applying 2D average pooling over an input image.

Python3




import torch
from PIL import Image
import torchvision.transforms as T
  
image = Image.open('image1.png')
  
# convert input image to torch tensor
image = T.ToTensor()(image)
  
# unsqueeze image to make 4D
image = image.unsqueeze(0)
  
# define avg pooling with square window
# of (kernel_size=5, stride=3)
pooling = torch.nn.AvgPool2d(5, 3)
image = pooling(image)
  
# squeeze image
image = image.squeeze(0)
  
# convert tensor to image
image = T.ToPILImage()(image)
image.show()


Output:

 

Although the two images looks similar but if observed carefully then we can clearly say that the granularity of the image has been lost.

Example 2:

Image used for demonstration:

neveropen

 

In this example, we are applying 2D average pooling over an input image.

Python3




import torch
from PIL import Image
import torchvision.transforms as T
  
image = Image.open('image2.png')
  
# convert input image to torch tensor
image = T.ToTensor()(image)
  
# unsqueeze image to make 4D
image = image.unsqueeze(0)
  
# define avg pooling with square window 
# of (kernel_size=5, stride=3)
pooling = torch.nn.AvgPool2d(5, 3)
image = pooling(image)
  
# squeeze image
image = image.squeeze(0)
  
# convert tensor to image
image = T.ToPILImage()(image)
image.show()


Output:

 

Here as well we can observe that the granularity of the image has been lost.

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS