小白学Pytorch系列–Torch.nn API (3)

pytorch openpose教程 pytorch pooling_2d

方法

注释

nn.MaxPool1d

对由多个输入平面组成的输入信号应用1D最大池化。

nn.MaxPool2d

对由多个输入平面组成的输入信号应用二维最大池化。

nn.MaxPool3d

在由多个输入平面组成的输入信号上应用3D最大池化。

nn.MaxUnpool1d

计算MaxPool1d的偏逆。

nn.MaxUnpool2d

计算MaxPool2d的偏逆。

nn.MaxUnpool3d

计算MaxPool3d的偏逆。

nn.AvgPool1d

对由多个输入平面组成的输入信号应用1D平均池化。

nn.AvgPool2d

在由多个输入平面组成的输入信号上应用2D平均池。

nn.AvgPool3d

在由多个输入平面组成的输入信号上应用3D平均池。

nn.FractionalMaxPool2d

在由多个输入平面组成的输入信号上应用2D分数极大池。

nn.FractionalMaxPool3d

在由多个输入平面组成的输入信号上应用3D平均池。

nn.LPPool1d

在由多个输入平面组成的输入信号上应用1D功率平均池。

nn.LPPool2d

在由多个输入平面组成的输入信号上应用2D功率平均池。

nn.AdaptiveMaxPool1d

对由多个输入平面组成的输入信号应用1D自适应最大池化。

nn.AdaptiveMaxPool2d

在由多个输入平面组成的输入信号上应用二维自适应最大池化。

nn.AdaptiveMaxPool3d

在由多个输入平面组成的输入信号上应用3D自适应最大池化。

nn.AdaptiveAvgPool1d

在由多个输入平面组成的输入信号上应用1D自适应平均池。

nn.AdaptiveAvgPool2d

在由多个输入平面组成的输入信号上应用2D自适应平均池。

nn.AdaptiveAvgPool3d

在由多个输入平面组成的输入信号上应用3D自适应平均池。

nn.MaxPool1d

原文链接:

对于1维池化,池化维度可看作一行长度为pytorch openpose教程 pytorch pooling_人工智能_02的躺平的一维数组。

首先,把input的第二维度画出来,就是一个躺平的一维数组,元素值从1到10,由下图所示。

pytorch openpose教程 pytorch pooling_2d_03


具体池化操作总共计算了4次:从左到右,构建一个长度为3的窗口,首先cover的三个元素是1、2、3,得到一个最大值为3。

然后向右移动窗口,移动2步,也就是stride=2(从起始位置1移动到3),cover三个元素变成了3、4、5,得到一个最大值为5。

再次移动窗口得到最大值7。

再然后得到最大值9。

此时窗口已经不能再往后移动(再移动的话,从9开始cover的元素不够3个,就超边界了),因此pool结束,返回4个结果值[3, 5, 7, 9],与程序输出的output相符合。

从这里看出,元素10就因为是保底模式(floor)而没有被窗口cover到。如果设置ceil_mode模式为True,得到结果是[3, 5, 7, 9, 10],

pytorch openpose教程 pytorch pooling_2d_04


pytorch openpose教程 pytorch pooling_人工智能_05


pytorch openpose教程 pytorch pooling_pytorch openpose教程_06

>>> # pool of size=3, stride=2
>>> m = nn.MaxPool1d(3, stride=2)
>>> input = torch.randn(20, 16, 50)
>>> output = m(input)

nn.MaxPool2d

pytorch openpose教程 pytorch pooling_人工智能_07


pytorch openpose教程 pytorch pooling_人工智能_08

>>> # pool of square window of size=3, stride=2
>>> m = nn.MaxPool2d(3, stride=2)
>>> # pool of non-square window
>>> m = nn.MaxPool2d((3, 2), stride=(2, 1))
>>> input = torch.randn(20, 16, 50, 32)
>>> output = m(input)

nn.MaxPool3d

在由多个输入平面组成的输入信号上应用3D最大池化。

类似MaxPool1d

pytorch openpose教程 pytorch pooling_2d_09


pytorch openpose教程 pytorch pooling_pytorch_10

>>> # pool of square window of size=3, stride=2
>>> m = nn.MaxPool3d(3, stride=2)
>>> # pool of non-square window
>>> m = nn.MaxPool3d((3, 2, 2), stride=(2, 1, 2))
>>> input = torch.randn(20, 16, 50, 44, 31)
>>> output = m(input)

nn.MaxUnpool1d

计算MaxPool1d的偏逆。

原理参考: https://vimsky.com/zh-tw/examples/usage/python-torch.nn.MaxUnpool1d-pt.html

pytorch openpose教程 pytorch pooling_pytorch_11


pytorch openpose教程 pytorch pooling_人工智能_12

>>> pool = nn.MaxPool1d(2, stride=2, return_indices=True)
>>> unpool = nn.MaxUnpool1d(2, stride=2)
>>> input = torch.tensor([[[1., 2, 3, 4, 5, 6, 7, 8]]])
>>> output, indices = pool(input)
>>> unpool(output, indices)
tensor([[[ 0.,  2.,  0.,  4.,  0.,  6.,  0., 8.]]])

>>> # Example showcasing the use of output_size
>>> input = torch.tensor([[[1., 2, 3, 4, 5, 6, 7, 8, 9]]])
>>> output, indices = pool(input)
>>> unpool(output, indices, output_size=input.size())
tensor([[[ 0.,  2.,  0.,  4.,  0.,  6.,  0., 8.,  0.]]])

>>> unpool(output, indices)
tensor([[[ 0.,  2.,  0.,  4.,  0.,  6.,  0., 8.]]])

nn.MaxUnpool2d

计算MaxPool2d的偏逆。

pytorch openpose教程 pytorch pooling_pytorch_13


pytorch openpose教程 pytorch pooling_人工智能_14

>>> pool = nn.MaxPool2d(2, stride=2, return_indices=True)
>>> unpool = nn.MaxUnpool2d(2, stride=2)
>>> input = torch.tensor([[[[ 1.,  2.,  3.,  4.],
                            [ 5.,  6.,  7.,  8.],
                            [ 9., 10., 11., 12.],
                            [13., 14., 15., 16.]]]])
>>> output, indices = pool(input)
>>> unpool(output, indices)
tensor([[[[  0.,   0.,   0.,   0.],
          [  0.,   6.,   0.,   8.],
          [  0.,   0.,   0.,   0.],
          [  0.,  14.,   0.,  16.]]]])
>>> # Now using output_size to resolve an ambiguous size for the inverse
>>> input = torch.torch.tensor([[[[ 1.,  2.,  3., 4., 5.],
                                  [ 6.,  7.,  8., 9., 10.],
                                  [11., 12., 13., 14., 15.],
                                  [16., 17., 18., 19., 20.]]]])
>>> output, indices = pool(input)
>>> # This call will not work without specifying output_size
>>> unpool(output, indices, output_size=input.size())
tensor([[[[ 0.,  0.,  0.,  0.,  0.],
          [ 0.,  7.,  0.,  9.,  0.],
          [ 0.,  0.,  0.,  0.,  0.],
          [ 0., 17.,  0., 19.,  0.]]]])

nn.MaxUnpool3d

计算MaxPool3d的偏逆

pytorch openpose教程 pytorch pooling_2d_15


pytorch openpose教程 pytorch pooling_2d_16

>>> # pool of square window of size=3, stride=2
>>> pool = nn.MaxPool3d(3, stride=2, return_indices=True)
>>> unpool = nn.MaxUnpool3d(3, stride=2)
>>> output, indices = pool(torch.randn(20, 16, 51, 33, 15))
>>> unpooled_output = unpool(output, indices)
>>> unpooled_output.size()
torch.Size([20, 16, 51, 33, 15])

nn.AvgPool1d

对由多个输入平面组成的输入信号应用1D平均池化。

pytorch openpose教程 pytorch pooling_2d_17


pytorch openpose教程 pytorch pooling_2d_18

>>> # pool with window of size=3, stride=2
>>> m = nn.AvgPool1d(3, stride=2)
>>> m(torch.tensor([[[1., 2, 3, 4, 5, 6, 7]]]))
tensor([[[2., 4., 6.]]])

nn.AvgPool2d

pytorch openpose教程 pytorch pooling_人工智能_19


pytorch openpose教程 pytorch pooling_人工智能_20

>>> # pool of square window of size=3, stride=2
>>> m = nn.AvgPool2d(3, stride=2)
>>> # pool of non-square window
>>> m = nn.AvgPool2d((3, 2), stride=(2, 1))
>>> input = torch.randn(20, 16, 50, 32)
>>> output = m(input)

nn.AvgPool3d

pytorch openpose教程 pytorch pooling_深度学习_21


pytorch openpose教程 pytorch pooling_人工智能_22

>>> # pool of square window of size=3, stride=2
>>> m = nn.AvgPool3d(3, stride=2)
>>> # pool of non-square window
>>> m = nn.AvgPool3d((3, 2, 2), stride=(2, 1, 2))
>>> input = torch.randn(20, 16, 50, 44, 31)
>>> output = m(input)

nn.FractionalMaxPool2d

参考:

关于分数阶最大池化,它似乎并没有太多的实际用处。函数中仍需要kernel_size,如果指定output_size,它就可以输出指定大小的张量,类似于自适应最大池化;如果指定output_ratio,它就会根据输出大小与输入大小的比例output_ratio来决定输出大小,所以output_ratio为0~1。并且函数不能同时接受output_ratio与output_size,毕竟这两个参数都决定输出大小,同时使用势必会引起冲突。

pytorch openpose教程 pytorch pooling_2d_23


pytorch openpose教程 pytorch pooling_深度学习_24

>>> # pool of square window of size=3, and target output size 13x12
>>> m = nn.FractionalMaxPool2d(3, output_size=(13, 12))
>>> # pool of square window and target output size being half of input image size
>>> m = nn.FractionalMaxPool2d(3, output_ratio=(0.5, 0.5))
>>> input = torch.randn(20, 16, 50, 32)
>>> output = m(input)

nn.FractionalMaxPool3d

在由多个输入平面组成的输入信号上应用3D分数极大池化。

pytorch openpose教程 pytorch pooling_2d_25


pytorch openpose教程 pytorch pooling_人工智能_26

>>> # pool of cubic window of size=3, and target output size 13x12x11
>>> m = nn.FractionalMaxPool3d(3, output_size=(13, 12, 11))
>>> # pool of cubic window and target output size being half of input size
>>> m = nn.FractionalMaxPool3d(3, output_ratio=(0.5, 0.5, 0.5))
>>> input = torch.randn(20, 16, 50, 32, 16)
>>> output = m(input)

nn.LPPool1d

参考:

p即为norm_type,当p=\infty时,相当于最大池化;当p=1时,相当于Sum池化 kernel_size为滑动窗口大小,可以为一个整数,也可以为一个元组 stride为步幅,默认等于kernel_size

pytorch openpose教程 pytorch pooling_深度学习_27


pytorch openpose教程 pytorch pooling_pytorch openpose教程_28


pytorch openpose教程 pytorch pooling_人工智能_29

# 代码输入:
import torch
import torch.nn as nn
 
x = torch.range(1,120).reshape(2, 3, 4, 5)
print("原始数据:\n",x.shape, '\n', x)
 
pool = nn.LPPool2d(norm_type=2, kernel_size=2)
print("池化后数据:\n", pool(x))
print(pool(x).shape)

nn.LPPool2d

在由多个输入平面组成的输入信号上应用2D功率平均池。

pytorch openpose教程 pytorch pooling_人工智能_30


pytorch openpose教程 pytorch pooling_2d_31

>>> # power-2 pool of square window of size=3, stride=2
>>> m = nn.LPPool2d(2, 3, stride=2)
>>> # pool of non-square window of power 1.2
>>> m = nn.LPPool2d(1.2, (3, 2), stride=(2, 1))
>>> input = torch.randn(20, 16, 50, 32)
>>> output = m(input)

nn.AdaptiveMaxPool1d

自适应池化的特点是:无论输入数据的size是多少,最终输出的size始终是函数指定的size。而背后的原理是:该函数可以根据输入size与我们指定的输出size来自动调整池化窗口、步长等参数。所以该函数没有kernel_size,stride,padding等参数,只需要指定输出size大小即可。如果需要让某一维度为原来大小,可以直接指定该部分参数为None。

pytorch openpose教程 pytorch pooling_深度学习_32

>>> # target output size of 5
>>> m = nn.AdaptiveMaxPool1d(5)
>>> input = torch.randn(1, 64, 8)
>>> output = m(input)

nn.AdaptiveMaxPool2d

在由多个输入平面组成的输入信号上应用二维自适应最大池化。

pytorch openpose教程 pytorch pooling_2d_33

>>> # target output size of 5x7
>>> m = nn.AdaptiveMaxPool2d((5, 7))
>>> input = torch.randn(1, 64, 8, 9)
>>> output = m(input)
>>> # target output size of 7x7 (square)
>>> m = nn.AdaptiveMaxPool2d(7)
>>> input = torch.randn(1, 64, 10, 9)
>>> output = m(input)
>>> # target output size of 10x7
>>> m = nn.AdaptiveMaxPool2d((None, 7))
>>> input = torch.randn(1, 64, 10, 9)
>>> output = m(input)

注意这里和 FractionalMaxPool2d比较一下, FractionalMaxPool2d是指定卷积核大小,但是这个AdaptiveMaxPool2d,不设置卷积核大小,随缘,可学习,所以注意区分。

nn.AdaptiveMaxPool3d

pytorch openpose教程 pytorch pooling_2d_34

>>> # target output size of 5x7x9
>>> m = nn.AdaptiveMaxPool3d((5, 7, 9))
>>> input = torch.randn(1, 64, 8, 9, 10)
>>> output = m(input)
>>> # target output size of 7x7x7 (cube)
>>> m = nn.AdaptiveMaxPool3d(7)
>>> input = torch.randn(1, 64, 10, 9, 8)
>>> output = m(input)
>>> # target output size of 7x9x8
>>> m = nn.AdaptiveMaxPool3d((7, None, None))
>>> input = torch.randn(1, 64, 10, 9, 8)
>>> output = m(input)

nn.AdaptiveAvgPool1d

在由多个输入平面组成的输入信号上应用1D自适应平均池。

pytorch openpose教程 pytorch pooling_pytorch openpose教程_35

>>> # target output size of 5
>>> m = nn.AdaptiveAvgPool1d(5)
>>> input = torch.randn(1, 64, 8)
>>> output = m(input)

nn.AdaptiveAvgPool2d

在由多个输入平面组成的输入信号上应用二维自适应平均池。

pytorch openpose教程 pytorch pooling_深度学习_36

>>> # target output size of 5x7
>>> m = nn.AdaptiveAvgPool2d((5, 7))
>>> input = torch.randn(1, 64, 8, 9)
>>> output = m(input)
>>> # target output size of 7x7 (square)
>>> m = nn.AdaptiveAvgPool2d(7)
>>> input = torch.randn(1, 64, 10, 9)
>>> output = m(input)
>>> # target output size of 10x7
>>> m = nn.AdaptiveAvgPool2d((None, 7))
>>> input = torch.randn(1, 64, 10, 9)
>>> output = m(input)

nn.AdaptiveAvgPool3d

在由多个输入平面组成的输入信号上应用三维自适应平均池。

pytorch openpose教程 pytorch pooling_人工智能_37

>>> # target output size of 5x7x9
>>> m = nn.AdaptiveAvgPool3d((5, 7, 9))
>>> input = torch.randn(1, 64, 8, 9, 10)
>>> output = m(input)
>>> # target output size of 7x7x7 (cube)
>>> m = nn.AdaptiveAvgPool3d(7)
>>> input = torch.randn(1, 64, 10, 9, 8)
>>> output = m(input)
>>> # target output size of 7x9x8
>>> m = nn.AdaptiveAvgPool3d((7, None, None))
>>> input = torch.randn(1, 64, 10, 9, 8)
>>> output = m(input)