4.pytorch learning: pool2d - 2d pooling

Table of contents

Build a tensor to understand pooling

Join the image dataset


Build a tensor to understand pooling

max pooling

nn.MaxPool2d
import torch
from torch import nn

input = torch.randn(1, 3, 5, 5)


class ZiDingYi(nn.Module):
    def __init__(self):
        super(ZiDingYi, self).__init__()
        self.maxpool1 = nn.MaxPool2d(kernel_size=(3, 3), stride=(2, 2))

    def forward(self, x):
        x = self.maxpool1(x)
        return x


zidingyi = ZiDingYi()
print(zidingyi)
output = zidingyi(input)
print(input)
print(output)

result:

The maximum pooling operation, the pooling area size is 3×3, and the step size is 2

ZiDingYi(
  (maxpool1): MaxPool2d(kernel_size=(3, 3), stride=(2, 2), padding=0, dilation=1, ceil_mode=False)
)
tensor([[[[ 1.5797,  0.1586, -1.6101, -1.2335, -0.0374],
          [-0.3541,  0.5332, -0.4232, -0.9439, -1.1685],
          [-1.5927, -1.4457,  1.2221,  0.2481,  0.1824],
          [-0.2517,  0.0480, -0.9640,  0.1537, -0.8519],
          [-1.5356, -0.9889, -0.6552,  0.0652,  0.1927]],

         [[-0.7988,  1.1190,  0.6354, -0.2665,  2.0248],
          [ 0.1559, -0.0030,  0.7831,  0.5012,  0.2387],
          [-0.4571, -0.6268, -0.8445, -1.6072,  0.5911],
          [ 0.1345, -0.5278, -1.1739,  0.9023,  1.4441],
          [ 0.1906,  1.1944, -1.2009,  1.0087, -0.8960]],

         [[ 0.5463,  0.8250,  0.1917,  0.3081, -1.2642],
          [-0.4377,  0.2050, -0.6741, -1.5405,  0.3701],
          [-1.1775, -1.5887, -0.4636,  0.9864, -0.3897],
          [ 1.3855, -1.3649,  0.9103,  0.6175, -1.2080],
          [ 1.5980, -0.8333,  0.9614,  1.3127, -2.3736]]]])
tensor([[[[1.5797, 1.2221],
          [1.2221, 1.2221]],

         [[1.1190, 2.0248],
          [1.1944, 1.4441]],

         [[0.8250, 0.9864],
          [1.5980, 1.3127]]]])

Process finished with exit code 0

Average pooling:

nn.AvgPool2d
import torch
from torch import nn

input = torch.randn(1, 3, 5, 5)


class ZiDingYi(nn.Module):
    def __init__(self):
        super(ZiDingYi, self).__init__()
        # self.maxpool1 = nn.MaxPool2d(kernel_size=(3, 3), stride=(2, 2))
        self.avgpool1 = nn.AvgPool2d(kernel_size=(3, 3), stride=(2, 2))

    def forward(self, x):
        # x = self.maxpool1(x)
        x = self.avgpool1(x)
        return x


zidingyi = ZiDingYi()
print(zidingyi)
output = zidingyi(input)
print(input)
print(output)

result:

Average pooling operation, the pooling area size is 3×3, and the step size is 2

ZiDingYi(
  (avgpool1): AvgPool2d(kernel_size=(3, 3), stride=(2, 2), padding=0)
)
tensor([[[[ 0.8619, -0.1549,  0.8348,  1.2231, -0.9563],
          [ 0.3299,  1.8903, -0.0950,  0.9231,  0.3421],
          [-0.0046,  1.6718, -2.0194,  0.9392, -0.8067],
          [-1.1392,  1.5445, -1.1157,  0.2914,  1.1111],
          [-0.4845, -0.9320,  0.1521, -0.2494,  3.0642]],

         [[-0.1516,  0.8342,  0.4512, -0.6028, -0.6233],
          [-0.8012, -0.9642,  1.4258,  0.2885,  0.6487],
          [ 0.1950,  0.3740,  0.0299, -0.0752,  0.3447],
          [ 1.1621,  0.7757,  0.6195, -0.0271,  0.9281],
          [-1.0458, -0.2665, -0.8065,  0.2777,  0.0473]],

         [[-0.3435, -1.1099,  0.5224, -0.7673, -1.0033],
          [-1.4557,  0.0472,  0.8938,  0.4616, -0.0227],
          [-0.4415, -0.6389, -0.9081,  1.5775, -0.2593],
          [-0.8944,  0.2530, -0.3738,  0.7166,  0.3662],
          [ 1.3281, -0.5218, -1.0847,  0.7990, -0.5499]]]])
tensor([[[[ 0.3683,  0.0428],
          [-0.2585,  0.1519]],

         [[ 0.1548,  0.2097],
          [ 0.1153,  0.1487]],

         [[-0.3816,  0.0550],
          [-0.3647,  0.0315]]]])

Process finished with exit code 0

Join the image dataset

import torch
import torchvision.datasets
from torch import nn
from torch.utils.data import DataLoader
from torch.utils.tensorboard import SummaryWriter

# input = torch.randn(1, 3, 5, 5)


class ZiDingYi(nn.Module):
    def __init__(self):
        super(ZiDingYi, self).__init__()
        self.maxpool1 = nn.MaxPool2d(kernel_size=(3, 3), stride=(2, 2))
        # self.maxpool1 = nn.MaxPool2d(kernel_size=(5, 5), stride=(5, 5), ceil_mode=False)
        # self.avgpool1 = nn.AvgPool2d(kernel_size=(3, 3), stride=(2, 2))

    def forward(self, x):
        x = self.maxpool1(x)
        # x = self.avgpool1(x)
        return x


zidingyi = ZiDingYi()
print(zidingyi)
# output = zidingyi(input)
# print(input)
# print(output)

dataset = torchvision.datasets.CIFAR10(root="./dataset", train=True, download=True,
                                       transform=torchvision.transforms.ToTensor())
dataloader = DataLoader(dataset=dataset, batch_size=64)

writer = SummaryWriter("logs_maxpool")
step = 0
for data in dataloader:
    imgs, targets = data
    writer.add_images(tag="input", img_tensor=imgs, global_step=step)
    output1 = zidingyi(imgs)
    writer.add_images(tag="output", img_tensor=output1, global_step=step)
    step += 1
writer.close()

open tensorboard

tensorboard --logdir=logs_maxpool

The maximum pooling area is too small and not obvious, modify the maximum pooling part.

self.maxpool1 = nn.MaxPool2d(kernel_size=(5, 5), stride=(5, 5), ceil_mode=False)

It can be seen from the figure that the pooling operation reduces the parameter amount of the feature map, improves the calculation speed, and increases the receptive field, which is a down-sampling operation. This dimensionality reduction process can preserve some important feature information.

Guess you like

Origin blog.csdn.net/wzfafabga/article/details/127704902