小土堆pytorch学习—P19-最大池化的使用

MaxPool官方文档

一般用MaxPool2d

参数👇

  • kernel_size (Union[int, Tuple[int, int]**]) – the size of the window to take a max over——卷积核,一般3$\times$3
  • stride (Union[int, Tuple[int, int]**]) – the stride of the window. Default value is kernel_size——步长一般与卷积核大小一致
  • padding (Union[int, Tuple[int, int]**]) – Implicit negative infinity padding to be added on both sides——是否在图像边缘进行填充
  • dilation (Union[int, Tuple[int, int]**]) – a parameter that controls the stride of elements in the window——是否在进行卷积时,隔一个取一个值?
  • return_indices (bool) – if True, will return the max indices along with the outputs. Useful for torch.nn.MaxUnpool2d later
  • ceil_mode (bool) – when True, will use ceil instead of floor to compute the output shape——如果设置为true,则保留边缘的特征,否则不保留

image-20230705105527142

简单的使用代码示例👇

import torch
from torch import nn

input = torch.tensor([[1,2,0,3,1],
                    [0,1,2,3,1],
                    [1,2,1,0,0],
                    [5,2,3,1,1],
                    [2,1,0,1,1]])

class Tudui(nn.Module):
    def __init__(self):#这里忘记怎么写
        super(Tudui ,self).__init__()
        self.maxpool = nn.MaxPool2d(kernel_size = 3,
                                    ceil_mode =True)
        print(f"self.maxpool is {self.maxpool}")

    def forward(self , input):
        output = self.maxpool(input)
        return output

tudui = Tudui()
output = tudui(input)
print(f"output = {output}")
print(f"type of output = {type(output)}")
print("="*111)

直接运行会出错,运行结果👇

image-20230705112105805

问题:给定张量输入不对,需要3维/4维输入张量,但是现在我只给定了2维张量,需要对其重构。知道长宽和宽度,但不知道每次的minibatch[1]

解决方法有两种:

  1. input = torch.tensor([[1,2,0,3,1],
                        [0,1,2,3,1],
                        [1,2,1,0,0],
                        [5,2,3,1,1],
                        [2,1,0,1,1]])
    print(F"the orignal shape of input is {input.shape}")
    # input = torch.reshape(input ,(-1 ,1,5,5) )#如果没有加这一行,会有报错
    print(F"the final shape of input is {input.shape}")
    # 增加一个维度,将2D张量转换为3D张量
    
  2. input = torch.tensor([[1,2,0,3,1],
                        [0,1,2,3,1],
                        [1,2,1,0,0],
                        [5,2,3,1,1],
                        [2,1,0,1,1]])
    print(F"the orignal shape of input is {input.shape}")
    input= input.unsqueeze(0).unsqueeze(0).float()#增加维度
    print(F"the final shape of input is {input.shape}")
    

使用数据集在tensorboard中查看👇

test_data = torchvision.datasets.CIFAR10(root = "",
											train = False,
                                        transforms = torchvison.transforms.ToTensor())
data_loader = DataLoader(dataset =test_data ,  batch_size = 64)

class Tudui(nn.Module):
    def __init__(self):
        super(Tudui, self).__init__()
        self.maxpool1 = MaxPool2d(kernel_size = 3,ceil_mode = False)
    
    def forward(self , input):
        output = self.maxpool1(input)
        return output

tudui = Tudui()
writer = SummaryWriter("tb_logs/maxpool_log")
step = 0
for data in data_loader:
    imgs , targets = data
    print(F"imgs = {imgs}")
    print(F"imgs.shape =  {imgs.shape}")
    print(F"imgs.type = {type(imgs)}")
    writer.add_images("maxpool_input" ,imgs, step )
    print(" "*111)
    output = tudui(imgs)
    print(F"output = {output}")
    print(F"output.shape = {output.shape}")
    print(F"output.type = {type(output)}")
    writer.add_images("maxpool_output",output, step)
    step+=1
    
writer.close() 

运行结果👇

image-20230705143710646

image-20230705144206439



  1. https://pytorch.org/docs/stable/generated/torch.nn.functional.conv2d.html#torch.nn.functional.conv2d ↩︎

posted @ 2023-07-10 16:04  西红柿爆炒鸡蛋  阅读(109)  评论(0)    收藏  举报