# How to Apply a 2D Average Pooling in PyTorch?

Last Updated : 09 Oct, 2022

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:

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:

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.

Previous
Next
Share your thoughts in the comments