【问题标题】:PyTorch: How to calculate output size of the CNN?PyTorch:如何计算 CNN 的输出大小?
【发布时间】:2022-11-22 15:29:20
【问题描述】:

我在这里查看了这个 PyTorch CNN 实现:https://machinelearningknowledge.ai/pytorch-conv2d-explained-with-examples/

我无法理解他们如何替换“?”有一定的价值。 CNN层输出的计算公式是什么?

这对于在 PyTorch 中计算是必不可少的;在 Tensorflow - Keras 中并非如此。如果有任何其他博客对此进行了很好的解释,请将其放入 cmets。

# Implementation of CNN/ConvNet Model
class CNN(torch.nn.Module):

    def __init__(self):
        super(CNN, self).__init__()
        # L1 ImgIn shape=(?, 28, 28, 1)
        # Conv -> (?, 28, 28, 32)
        # Pool -> (?, 14, 14, 32)
        self.layer1 = torch.nn.Sequential(
            torch.nn.Conv2d(1, 32, kernel_size=3, stride=1, padding=1),
            torch.nn.ReLU(),
            torch.nn.MaxPool2d(kernel_size=2, stride=2),
            torch.nn.Dropout(p=1 - keep_prob))
        # L2 ImgIn shape=(?, 14, 14, 32)
        # Conv      ->(?, 14, 14, 64)
        # Pool      ->(?, 7, 7, 64)
        self.layer2 = torch.nn.Sequential(
            torch.nn.Conv2d(32, 64, kernel_size=3, stride=1, padding=1),
            torch.nn.ReLU(),
            torch.nn.MaxPool2d(kernel_size=2, stride=2),
            torch.nn.Dropout(p=1 - keep_prob))
        # L3 ImgIn shape=(?, 7, 7, 64)
        # Conv ->(?, 7, 7, 128)
        # Pool ->(?, 4, 4, 128)
        self.layer3 = torch.nn.Sequential(
            torch.nn.Conv2d(64, 128, kernel_size=3, stride=1, padding=1),
            torch.nn.ReLU(),
            torch.nn.MaxPool2d(kernel_size=2, stride=2, padding=1),
            torch.nn.Dropout(p=1 - keep_prob))

        # L4 FC 4x4x128 inputs -> 625 outputs
        self.fc1 = torch.nn.Linear(4 * 4 * 128, 625, bias=True)
        torch.nn.init.xavier_uniform(self.fc1.weight)
        self.layer4 = torch.nn.Sequential(
            self.fc1,
            torch.nn.ReLU(),
            torch.nn.Dropout(p=1 - keep_prob))
        # L5 Final FC 625 inputs -> 10 outputs
        self.fc2 = torch.nn.Linear(625, 10, bias=True)
        torch.nn.init.xavier_uniform_(self.fc2.weight) # initialize parameters

    def forward(self, x):
        out = self.layer1(x)
        out = self.layer2(out)
        out = self.layer3(out)
        out = out.view(out.size(0), -1)   # Flatten them for FC
        out = self.fc1(out)
        out = self.fc2(out)
        return out


#instantiate CNN model

model = CNN()
model

谢谢!

【问题讨论】:

  • 试试这个calculator。因此,您需要知道 conv2d 输出的通道数,才能将其作为输入通道传递给下一个 conv2d。通道数基本上是您的 conv2d 的数字过滤器。

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


【解决方案1】:

我假设你的计算是错误的,因为:

  1. Pytorch 支持格式为 C * H * W 的图像(例如 3x32x32 而不是 32x32x3)
  2. 第一个维度总是批量维度,在计算中必须省略,因为所有 nn.Modules 默认处理它

    所以如果你想计算第一个线性层的输入大小,你可以使用这个技巧:

    conv = nn.Sequential(self.layer1,self.layer2, self.layer3, nn.Flatten())
    out = conv(torch.randn(1,im_height,im_width).unsqueeze(0)) 
    # fc_layer_in_channels  = out.shape[1]
    self.fc1 = torch.nn.Linear(out.shape[1], 625, bias=True)
    

    但前提是你知道im_heightim_width

    最佳做法是使用torch.nn.AdaptiveAvgPool2d。 使用此层,您始终可以获得固定空间大小的输出。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-03-06
    • 2021-08-19
    • 2011-11-24
    • 1970-01-01
    • 1970-01-01
    • 2020-09-30
    • 2020-04-29
    • 2021-07-04
    相关资源
    最近更新 更多