如何在PyTorch中应用二维平均池化?
我们可以使用**torch.nn.AvgPool2d()**模块对包含多个输入平面的输入图像应用二维平均池化。二维平均池化层的输入大小必须为[**N,C,H,W**],其中**N**是批次大小,C是通道数,**H**和**W**是输入图像的高度和宽度。
平均池化操作的主要特征是过滤器或内核大小和步幅。此模块支持**TensorFloat32**。
语法
torch.nn.AvgPool2d(kernel_size)
参数
**kernel_size** – 平均窗口的大小。
除了这个参数,还有一些可选参数,例如**stride、padding、dilation**等。我们将在下面的Python示例中详细介绍这些参数。
步骤
您可以使用以下步骤应用二维平均池化:
导入所需的库。在所有以下示例中,所需的Python库是**torch**。确保您已安装它。要在图像上应用二维平均池化,我们还需要**torchvision**和**Pillow**。
import torch import torchvision from PIL import Image
定义**输入**张量或读取输入图像。如果输入是图像,则我们首先将其转换为torch张量。
定义**kernel_size、stride**和其他参数。
接下来,通过将上述定义的参数传递给**torch.nn.AvgPool2d()**来定义平均池化**pooling**。
pooling = nn.AvgPool2d(kernel_size)
将平均池化**pooling**应用于输入张量或图像张量。
output = pooling(input)
接下来打印平均池化后的张量。如果输入是图像张量,则要可视化图像,我们首先将平均池化后获得的张量转换为PIL图像,然后可视化图像。
让我们来看几个例子,以便更好地理解它的工作原理。
输入图像
我们将在第二个示例中使用以下图像作为输入文件。

示例1
在下面的Python示例中,我们对输入张量执行二维平均池化。我们应用了**kernel_size、stride、padding**和**dilation**的不同组合。
# Python 3 program to perform 2D Avg Pooling
# Import the required libraries
import torch
import torch.nn as nn
'''input of size = [N,C,H, W] or [C,H, W]
N==>batch size,
C==> number of channels,
H==> height of input planes in pixels,
W==> width in pixels.
'''
input = torch.empty(3, 4, 4).random_(256)
print("Input Tensor:
", input)
print("Input Size:",input.size())
# pool of square window of size=3, stride=1
pooling1 = nn.AvgPool2d(3, stride=1)
# Perform Average Pooling
output = pooling1(input)
print("Output Tensor:
", output)
print("Output Size:",output.size())
# pool of non-square window
pooling2 = nn.AvgPool2d((2, 1), stride=(1, 2))
# Perform average Pool
output = pooling2(input)
print("Output Tensor:
", output)
print("Output Size:",output.size())输出
Input Tensor: tensor([[[194., 159., 7., 90.], [128., 173., 28., 211.], [252., 123., 248., 147.], [144., 107., 28., 17.]], [[122., 140., 117., 52.], [252., 118., 216., 101.], [ 88., 121., 25., 210.], [223., 162., 39., 125.]], [[168., 113., 53., 246.], [199., 23., 54., 74.], [ 95., 246., 245., 48.], [222., 175., 144., 127.]]]) Input Size: torch.Size([3, 4, 4]) Output Tensor: tensor([[[145.7778, 131.7778], [136.7778, 120.2222]], [[133.2222, 122.2222], [138.2222, 124.1111]], [[132.8889, 122.4444], [155.8889, 126.2222]]]) Output Size: torch.Size([3, 2, 2]) Output Tensor: tensor([[[161.0000, 17.5000], [190.0000, 138.0000], [198.0000, 138.0000]], [[187.0000, 166.5000], [170.0000, 120.5000], [155.5000, 32.0000]], [[183.5000, 53.5000], [147.0000, 149.5000], [158.5000, 194.5000]]]) Output Size: torch.Size([3, 3, 2])
示例2
在下面的Python示例中,我们对输入图像执行二维平均池化。为了应用二维平均池化,我们首先将图像转换为torch张量,并在平均池化之后再次将其转换为PIL图像以进行可视化。
# Python 3 program to perform 2D Average Pooling on image
# Import the required libraries
import torch
import torchvision
from PIL import Image
import torchvision.transforms as T
import torch.nn.functional as F
# read the input image
img = Image.open('panda.jpg')
# convert the image to torch tensor
img = T.ToTensor()(img)
print("Original size of Image:", img.size()) #Size([3, 466, 700])
# unsqueeze to make 4D
img = img.unsqueeze(0)
# define avg pool with square window of size=4, stride=1
pool = torch.nn.AvgPool2d(4, 1)
img = pool(img)
img = img.squeeze(0)
print("Size after AvgPool:",img.size())
img = T.ToPILImage()(img)
img.show()输出
Original size of Image: torch.Size([3, 466, 700]) Size after AvgPool: torch.Size([3, 463, 697])

请注意,由于权重和偏差的随机初始化,您在不同的运行中可能会得到不同的输出图像。
数据结构
网络
关系数据库管理系统(RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP