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

示例 1
在以下 Python 示例中,我们对输入张量执行 2D 最大池化。我们应用了 **kernel_size、stride、padding** 和 **dilation** 的不同组合。
# Python 3 program to perform 2D Max 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.MaxPool2d(3, stride=1)
# Perform Max Pool
output = pooling1(input)
print("Output Tensor:
", output)
print("Output Size:",output.size())
# pool of non-square window
pooling2 = nn.MaxPool2d((2, 1), stride=(1, 2))
# Perform Max Pool
output = pooling2(input)
print("Output Tensor:
", output)
print("Output Size:",output.size())输出
Input Tensor: tensor([[[129., 61., 166., 156.], [130., 5., 15., 73.], [ 73., 173., 146., 11.], [ 62., 103., 118., 50.]], [[ 35., 147., 95., 127.], [ 79., 15., 109., 27.], [105., 51., 157., 137.], [142., 187., 95., 240.]], [[ 60., 36., 195., 167.], [181., 207., 244., 71.], [172., 242., 13., 228.], [144., 238., 222., 174.]]]) Input Size: torch.Size([3, 4, 4]) Output Tensor: tensor([[[173., 173.], [173., 173.]], [[157., 157.], [187., 240.]], [[244., 244.], [244., 244.]]]) Output Size: torch.Size([3, 2, 2]) Output Tensor: tensor([[[130., 166.], [130., 146.], [ 73., 146.]], [[ 79., 109.], [105., 157.], [142., 157.]], [[181., 244.], [181., 244.], [172., 222.]]]) Output Size: torch.Size([3, 3, 2])
示例 2
在以下 Python 示例中,我们对输入图像执行 2D 最大池化。为了应用 2D 最大池化,我们首先将图像转换为 torch 张量,并在最大池化后再次将其转换为 PIL 图像以进行可视化
# Python 3 program to perform 2D Max 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('elephant.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 max pool with square window of size=4, stride=1
pool = torch.nn.MaxPool2d(4, 1)
img = pool(img)
img = img.squeeze(0)
print("Size after MaxPool:",img.size())
img = T.ToPILImage()(img)
img.show()输出
Original size of Image: torch.Size([3, 466, 700]) Size after MaxPool: torch.Size([3, 463, 697])

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