【问题标题】:Understanding input and output size for Conv2d了解 Conv2d 的输入和输出大小
【发布时间】:2021-06-25 05:25:30
【问题描述】:

我正在学习使用 PyTorch(使用 CIFAR-10 数据集)following this link 进行图像分类。

我正在尝试了解给定 Conv2d 代码的输入和输出参数:

import torch.nn as nn
import torch.nn.functional as F

class Net(nn.Module):
    def __init__(self):
        super(Net, self).__init__()
        self.conv1 = nn.Conv2d(3, 6, 5)
        self.pool = nn.MaxPool2d(2, 2)
        self.conv2 = nn.Conv2d(6, 16, 5)
        self.fc1 = nn.Linear(16 * 5 * 5, 120)
        self.fc2 = nn.Linear(120, 84)
        self.fc3 = nn.Linear(84, 10)

    def forward(self, x):
        x = self.pool(F.relu(self.conv1(x)))
        x = self.pool(F.relu(self.conv2(x)))
        x = x.view(-1, 16 * 5 * 5)
        x = F.relu(self.fc1(x))
        x = F.relu(self.fc2(x))
        x = self.fc3(x)
        return x

net = Net()

我对@9​​87654324@的理解(如果我错了/遗漏了什么,请纠正):

  • 因为图像有 3 个通道,所以第一个参数是 36 没有过滤器(随机选择)
  • 5 是内核大小 (5, 5)(随机选择)
  • 同样我们创建下一层(上一层输出是该层的输入)
  • 现在使用linear 函数创建一个全连接层: self.fc1 = nn.Linear(16 * 5 * 5, 120)

16 * 5 * 5:这里16是最后一个conv2d层的输出,但是这里面的5 * 5是什么?

这是内核大小吗?或者是其他东西?如何知道我们需要乘以5*5 or 4*4 or 3*3.....

我研究并知道,由于图像大小为32*32,应用 max pool(2) 2 次,所以图像大小将是 32 -> 16 -> 8,所以我们应该乘以 last_ouput_size * 8 * 8 但是在此链接中为5*5

谁能解释一下?

【问题讨论】:

    标签: python deep-learning pytorch conv-neural-network


    【解决方案1】:

    这些是图像大小本身的尺寸(即高度 x 宽度)。

    未填充的卷积

    除非您用零填充图像,否则卷积过滤器会将输出图像的大小在高度和宽度上缩小filter_size - 1

    3-filter takes a 5x5 image to a (5-(3-1) x 5-(3-1)) image Zero padding preserves image dimensions

    您可以通过设置Conv2d(padding=...) 在 Pytorch 中添加填充。

    转换链

    既然已经过去了:

    Layer Shape Transformation
    one conv layer (without padding) (h, w) -> (h-4, w-4)
    a MaxPool -> ((h-4)//2, (w-4)//2)
    another conv layer (without padding) -> ((h-8)//2, (w-8)//2)
    another MaxPool -> ((h-8)//4, (w-8)//4)
    a Flatten -> ((h-8)//4 * (w-8)//4)

    我们从(32,32)的原始图像大小到(28,28)(14,14)(10,10)(5,5)(5x5)


    要可视化这一点,您可以使用 torchsummary 包:

    from torchsummary import summary
    
    input_shape = (3,32,32)
    summary(Net(), input_shape)
    
    ----------------------------------------------------------------
            Layer (type)               Output Shape         Param #
    ================================================================
                Conv2d-1            [-1, 6, 28, 28]             456
             MaxPool2d-2            [-1, 6, 14, 14]               0
                Conv2d-3           [-1, 16, 10, 10]           2,416
             MaxPool2d-4             [-1, 16, 5, 5]               0
                Linear-5                  [-1, 120]          48,120
                Linear-6                   [-1, 84]          10,164
                Linear-7                   [-1, 10]             850
    ================================================================
    

    【讨论】:

    猜你喜欢
    • 2018-04-18
    • 2020-06-20
    • 2021-05-12
    • 2015-04-14
    • 1970-01-01
    • 1970-01-01
    • 2017-09-04
    • 2017-04-14
    • 2016-10-21
    相关资源
    最近更新 更多