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

請注意,由於權重和偏差的隨機初始化,您在不同的執行中可能會得到不同的輸出影像。

更新於:2022年1月25日

2K+ 瀏覽量

啟動你的職業生涯

透過完成課程獲得認證

開始
廣告