运行时错误:形状"[1, 1024]"对于大小为 50176 的输入无效



我正在尝试在CIFAR-10数据集上使用Alexnet。我已经将图像大小调整为 224x224,我猜这就是问题所在。但是,我收到以下错误:

<ipython-input-11-34884668038d> in forward(self, x)
37     def forward(self, x):
38         x = self.features(x)
---> 39         x = x.view(x.size(0), 256 * 2 * 2)
40         x = self.classifier(x)
41         return x
RuntimeError: shape '[1, 1024]' is invalid for input of size 50176

我的 Alexnet 模型代码如下:

NUM_CLASSES = 10

class AlexNet(nn.Module):
def __init__(self, num_classes=NUM_CLASSES):
super(AlexNet, self).__init__()
self.features = nn.Sequential(
nn.Conv2d(3, 64, kernel_size=3, stride=2, padding=1),
nn.ReLU(inplace=True),
nn.MaxPool2d(kernel_size=2),
nn.Conv2d(64, 192, kernel_size=3, padding=1),
nn.ReLU(inplace=True),
nn.MaxPool2d(kernel_size=2),
nn.Conv2d(192, 384, kernel_size=3, padding=1),
nn.ReLU(inplace=True),
nn.Conv2d(384, 256, kernel_size=3, padding=1),
nn.ReLU(inplace=True),
nn.Conv2d(256, 256, kernel_size=3, padding=1),
nn.ReLU(inplace=True),
nn.MaxPool2d(kernel_size=2),
)
self.classifier = nn.Sequential(
nn.Dropout(),
nn.Linear(256 * 2 * 2, 4096),
nn.ReLU(inplace=True),
nn.Dropout(),
nn.Linear(4096, 4096),
nn.ReLU(inplace=True),
nn.Linear(4096, num_classes),
)
def forward(self, x):
x = self.features(x)
x = x.view(x.size(0), 256 * 2 * 2)
x = self.classifier(x)
return x

任何帮助将不胜感激:)

CIFAR网络预计输入数据远小于224x224,通常为32x32。

最新更新