cnn池化层输入通道数_(pytorch-深度学习系列)CNN中的池化层-学习笔记

CNN中的池化层

首先,池化(pooling)层的提出是为了缓解卷积层对位置的过度敏感性。

什么意思?

比如在图像边缘检测问题中,实际图像里,我们的目标物体不会总出现在固定位置,即使我们连续拍摄同一个物体也极有可能出现像素位置上的偏移。这会导致同一个边缘对应的输出可能出现在卷积输出的边缘的不同位置,从而对模式识别造成影响。

与卷积层类似,池化层每次也是对输入数据的一个固定窗口内的元素计算输出

但是池化层不同于卷积层,它不考虑输入与核的互相关性,池化层直接计算池化窗口内数据的最大值或平均值,这分别对应与最大池化与平均池化。

对于二维最大池化,池化窗口从输入数组的最左上方开始,按从左往右、从上往下的顺序,依次在输入数组上滑动。当池化窗口滑动到某一位置时,窗口中的输入子数组的最大值即输出数组中相应位置的元素。

\[\begin{bmatrix}

0&1&2 \\

3&4&5 \\

6&7&8

\end{bmatrix} *

\begin{bmatrix}

2*2 \\ 最大池化层

\end{bmatrix} =

\begin{bmatrix}

4&5 \\

7&8

\end{bmatrix}

\]

由:

\[ \max(0,1,3,4)=4,\\

\max(1,2,4,5)=5,\\

\max(3,4,6,7)=7,\\

\max(4,5,7,8)=8. \]

·

构造一个实现最大池化和平均池化的层:

import torch

from torch import nn

def pool2d(X, pool_size, mode='max'):

X = X.float()

p_h, p_w = pool_size

Y = torch.zeros(X.shape[0] - p_h + 1, X.shape[1] - p_w + 1)

for i in range(Y.shape[0]):

for j in range(Y.shape[1]):

if mode == 'max':

Y[i, j] = X[i: i + p_h, j: j + p_w].max()

elif mode == 'avg':

Y[i, j] = X[i: i + p_h, j: j + p_w].mean()

return Y

对于物体边缘检测的例子:

检测图像中物体的边缘,即找到像素变化的位置。首先我们构造一张6×86×8的图像(即高和宽分别为6像素和8像素的图像)。它中间4列为黑(0),其余为白(1)。

X = torch.ones(6, 8)

X[:, 2:6] = 0

X

tensor([[1., 1., 0., 0., 0., 0., 1., 1.],

[1., 1., 0., 0., 0., 0., 1., 1.],

[1., 1., 0., 0., 0., 0., 1., 1.],

[1., 1., 0., 0., 0., 0., 1., 1.],

[1., 1., 0., 0., 0., 0., 1., 1.],

[1., 1., 0., 0., 0., 0., 1., 1.]])

然后我们构造一个高和宽分别为1和2的卷积核K。当它与输入做互相关运算时,如果横向相邻元素相同,输出为0;否则输出为非0。

K = torch.tensor([[1, -1]])

下面将输入X和我们设计的卷积核K做互相关运算。可以看出,我们将从白到黑的边缘和从黑到白的边缘分别检测成了1和-1。其余部分的输出全是0。

def corr2d(X, K):

h, w = K.shape

Y = torch.zeros((X.shape[0] - h + 1, X.shape[1] - w + 1))

for i in range(Y.shape[0]):

for j in range(Y.shape[1]):

Y[i, j] = (X[i: i + h, j: j + w] * K).sum()

return Y

Y = corr2d(X, K)

print(Y)

tensor([[ 0., 1., 0., 0., 0., -1., 0.],

[ 0., 1., 0., 0., 0., -1., 0.],

[ 0., 1., 0., 0., 0., -1., 0.],

[ 0., 1., 0., 0., 0., -1., 0.],

[ 0., 1., 0., 0., 0., -1., 0.],

[ 0., 1., 0., 0., 0., -1., 0.]])

现在我们将卷积层的输出作为2×2平均池化层的输入。设该卷积层输入是X、池化层输出为Y。

无论是X[i, j]和X[i, j+1]值不同,还是X[i, j+1]和X[i, j+2]不同,池化层输出均有Y[i, j]不等于0。也就是说,使用2×2平均池化层时,只要卷积层识别的模式在高和宽上移动不超过一个元素,我们依然可以将它检测出来。

池化层的填充和步幅:

同卷积层一样,池化层也可以在输入的高和宽两侧的填充并调整窗口的移动步幅来改变输出形状。

先构造一个形状为(1, 1, 4, 4)的输入数据,前两个维度分别是批量和通道。

X = torch.arange(16, dtype=torch.float).view((1, 1, 4, 4))

print(X)

tensor([[[[ 0., 1., 2., 3.],

[ 4., 5., 6., 7.],

[ 8., 9., 10., 11.],

[12., 13., 14., 15.]]]])

默认情况下,MaxPool2d实例里步幅和池化窗口形状相同。下面使用形状为(3, 3)的池化窗口,默认获得形状为(3, 3)的步幅。

pool2d = nn.MaxPool2d(3)

pool2d(X)

输出为:

\[\begin{bmatrix}

0&1&2&3 \\

4&5&6&7 \\

8&9&10&11 \\

12&13&14&15

\end{bmatrix} *

\begin{bmatrix}

3*3 \\ 最大池化层

\end{bmatrix} =

\begin{bmatrix}

10

\end{bmatrix}

\]

至于输出为什么是[10],根据之前的blog中讲过的卷积层的输入输出公式:

\[output = \lfloor(n_h-k_h+p_h+s_h)/s_h\rfloor \times \lfloor(n_w-k_w+p_w+s_w)/s_w\rfloor= \\

\lfloor(4-3+0\times2+3)/3\rfloor \times \lfloor(4-3+0\times2+3)/3\rfloor = 1 \times 1

\]

得出输出维度为(1*1)

如果手动指定步幅和填充:

pool2d = nn.MaxPool2d(3, padding=1, stride=2)

pool2d(X)

\[output = \lfloor(n_h-k_h+p_h+s_h)/s_h\rfloor \times \lfloor(n_w-k_w+p_w+s_w)/s_w\rfloor= \\

\lfloor(4-3+1\times2+2)/2\rfloor \times \lfloor(4-3+1\times2+2)/2\rfloor = 2 \times 2

\]

tensor([[[[ 5., 7.],

[13., 15.]]]])

指定非正矩形的窗口:

pool2d = nn.MaxPool2d((2, 4), padding=(1, 2), stride=(2, 3))

pool2d(X)

\[output = \lfloor(n_h-k_h+p_h+s_h)/s_h\rfloor \times \lfloor(n_w-k_w+p_w+s_w)/s_w\rfloor= \\

\lfloor(4-2+1\times2+2)/2\rfloor \times \lfloor(4-4+2\times2+3)/3\rfloor = 3 \times 2

\]

输出为:

\[\begin{bmatrix}

0&0&0&0&0&0&0&0\\

0&0&0&1&2&3&0&0 \\

0&0&4&5&6&7&0&0 \\

0&0&8&9&10&11&0&0 \\

0&0&12&13&14&15&0&0\\

0&0&0&0&0&0&0&0

\end{bmatrix} *

\begin{bmatrix}

2*4 \\ 最大池化层

\end{bmatrix} =

\begin{bmatrix}

1&3\\

9&11\\

13&15\\

\end{bmatrix}

\]

池化层中的多通道

在处理多通道输入数据时,池化层对每个输入通道分别池化,而不是像卷积层那样将各通道的输入按通道相加。这意味着池化层的输出通道数与输入通道数相等。

例如:

X = torch.cat((X, X + 1), dim=1)

print(X)

输出:

tensor([[[[ 0., 1., 2., 3.],

[ 4., 5., 6., 7.],

[ 8., 9., 10., 11.],

[12., 13., 14., 15.]],

[[ 1., 2., 3., 4.],

[ 5., 6., 7., 8.],

[ 9., 10., 11., 12.],

[13., 14., 15., 16.]]]])

pool2d = nn.MaxPool2d(3, padding=1, stride=2)

pool2d(X)

输出通道数仍然是2

tensor([[[[ 5., 7.],

[13., 15.]],

[[ 6., 8.],

[14., 16.]]]])

你可能感兴趣的:(cnn池化层输入通道数)