如何在 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