“运行时错误:4 维权重 32 3 3 的预期 4 维输入,但得到大小为 [3, 224, 224] 的 3 维输入”?

Gol*_*ame 29 machine-learning computer-vision conv-neural-network pytorch torchvision

我正在尝试使用预先训练的模型。这就是问题发生的地方

模型不是应该接收简单的彩色图像吗?为什么它需要 4 维输入?

RuntimeError                              Traceback (most recent call last)
<ipython-input-51-d7abe3ef1355> in <module>()
     33 
     34 # Forward pass the data through the model
---> 35 output = model(data)
     36 init_pred = output.max(1, keepdim=True)[1] # get the index of the max log-probability
     37 

5 frames
/usr/local/lib/python3.6/dist-packages/torch/nn/modules/conv.py in forward(self, input)
    336                             _pair(0), self.dilation, self.groups)
    337         return F.conv2d(input, self.weight, self.bias, self.stride,
--> 338                         self.padding, self.dilation, self.groups)
    339 
    340 

RuntimeError: Expected 4-dimensional input for 4-dimensional weight 32 3 3, but got 3-dimensional input of size [3, 224, 224] instead
Run Code Online (Sandbox Code Playgroud)

在哪里

inception = models.inception_v3()
model = inception.to(device)
Run Code Online (Sandbox Code Playgroud)

Sha*_*hai 30

正如Usman Ali在他的评论中所写的那样,pytorch(和大多数其他 DL 工具箱)需要一批图像作为输入。因此你需要打电话

output = model(data[None, ...])  
Run Code Online (Sandbox Code Playgroud)

将单例“批量”维度插入到您的输入中 data

另请注意,您使用的模型可能需要不同的输入尺寸 (3x229x229) 而不是 3x224x224。


Nic*_*ais 21

从关于卷积层的 Pytorch文档中Conv2d层期望输入具有形状

(n_samples, channels, height, width) # e.g., (1000, 1, 224, 224)
Run Code Online (Sandbox Code Playgroud)

以通常的格式 ( 224, 224 )传递灰度图像将不起作用。

要获得正确的形状,您需要添加通道尺寸。你可以这样做:

x = np.expand_dims(x, 1)      # if numpy array
tensor = tensor.unsqueeze(1)  # if torch tensor
Run Code Online (Sandbox Code Playgroud)

unsqueeze()方法在指定的索引处添加一个维度。结果将具有以下形状:

(1000, 1, 224, 224)
Run Code Online (Sandbox Code Playgroud)

  • 对于灰度图像,你是对的。然而,对于需要被视为一批 1 个图像的 RGB 图像,这将是“.unsqueeze(0)”。 (3认同)