【问题标题】:RuntimeError: Given input size: (10x7x7). Calculated output size: (10x0x0). Output size is too smallRuntimeError:给定输入大小:(10x7x7)。计算的输出大小:(10x0x0)。输出尺寸太小
【发布时间】:2020-04-11 18:35:22
【问题描述】:

尝试训练 mnist 28x28x1 图像

我的模型是

   def __init__(self):
        super(CNN_mnist, self).__init__()
        self.conv = nn.Sequential(
            # 3 x 128 x 128
            nn.Conv2d(1, 32, 3, 1, 1),
            nn.BatchNorm2d(32),
            nn.LeakyReLU(0.2),

            # 32 x 128 x 128
            nn.Conv2d(32, 64, 3, 1, 1),
            nn.BatchNorm2d(64),
            nn.LeakyReLU(0.2),

            # 64 x 128 x 128
            nn.MaxPool2d(2, 2),

            # 64 x 64 x 64
            nn.Conv2d(64, 128, 3, 1, 1),
            nn.BatchNorm2d(128),
            nn.LeakyReLU(0.2),

            # 128 x 64 x 64
            nn.Conv2d(128, 256, 3, 1, 1),
            nn.BatchNorm2d(256),
            nn.LeakyReLU(0.2),

            # 256 x 64 x 64
            nn.MaxPool2d(2, 2),

            # 256 x 32 x 32
            nn.Conv2d(256, 10, 3, 1, 1),
            nn.BatchNorm2d(10),
            nn.LeakyReLU(0.2)
        )
        # 256 x 32 x 32
        self.avg_pool = nn.AvgPool2d(32)
        # 256 x 1 x 1
        self.classifier = nn.Linear(10, 10)

    def forward(self, x):
        features = self.conv(x)
        flatten = self.avg_pool(features).view(features.size(0), -1)
        output = self.classifier(flatten)
        return output, features

我收到以下错误

RuntimeError:给定输入大小:(10x7x7)。计算输出大小: (10x0x0)。输出尺寸太小

不确定这个错误是什么意思,我应该在哪里修复它?

【问题讨论】:

  • 请检查您的输入形状x应该是Batch x Channels x Height x Width

标签: python image-processing computer-vision conv-neural-network pytorch


【解决方案1】:

您的 [avg_pool] 层期望其输入大小(至少)为 32x32,因为为此层定义的内核大小为 32。
然而,考虑到输入的大小,这个池化层得到的特征图只有 7x7 大小。这对于 32 的内核大小来说太小了。

您应该增加输入大小,或者为avg_pooling 层定义更小的(例如,7)内核大小。

【讨论】:

    猜你喜欢
    • 2021-04-13
    • 2018-07-02
    • 1970-01-01
    • 2021-07-04
    • 2021-08-02
    • 1970-01-01
    • 1970-01-01
    • 2019-05-03
    • 1970-01-01
    相关资源
    最近更新 更多