简体   繁体   English

RuntimeError:预期 3D(未批处理)或 4D(批处理)输入到 conv2d,但输入的大小为:[64, 2]

[英]RuntimeError: Expected 3D (unbatched) or 4D (batched) input to conv2d, but got input of size: [64, 2]

I'm trying to create a custom CNN model using PyTorch for binary image classification of RGB images, but I keep getting a runtime error saying that my original input shape [64,3,128,128] is being output as [64,2]. I'm trying to create a custom CNN model using PyTorch for binary image classification of RGB images, but I keep getting a runtime error saying that my original input shape [64,3,128,128] is being output as [64,2]. I've been trying to fix it for 2 days now, but I'm still clueless about what's wrong with the code.我已经尝试修复它 2 天了,但我仍然对代码有什么问题一无所知。

Here's the code of the network:这是网络的代码:

class MyCNN(nn.Module):
  def __init__(self):
    super(MyCNN, self).__init__()
    self.network = nn.Sequential(
        nn.Conv2d(in_channels=3, out_channels=32, kernel_size=3),
        nn.ReLU(),
        nn.MaxPool2d(kernel_size=2),

        nn.Conv2d(32, 64, 3),
        nn.ReLU(),
        nn.MaxPool2d(2),

        nn.Conv2d(64, 128, 3),
        nn.ReLU(),
        nn.MaxPool2d(2),

        nn.Flatten(),
        nn.Linear(in_features=25088, out_features=2048),
        nn.ReLU(),
        nn.Linear(2048, 1024),
        nn.ReLU(),
        nn.Linear(1024, 2),
    )

  def forward(self, x):
    return self.network(x)

It's being called here:它在这里被调用:

for epoch in range(num_epochs):
    for images, labels in data_loader:  
        images, labels = images.to(device), labels.to(device)

        optimizer.zero_grad()

        # Forward pass
        outputs = model(images)
        loss = criterion(outputs, labels)
        
        # Backward and optimize
        loss.backward()
        optimizer.step()

    print('Epoch [{}/{}], Loss: {:.4f}'.format(epoch+1, num_epochs, loss.item()))

Here's the stack trace:这是堆栈跟踪:

---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-30-fb9ee290e1d6> in <module>()
      7 
      8         # Forward pass
----> 9         outputs = model(images)
     10         loss = criterion(outputs, labels)
     11 

6 frames
/usr/local/lib/python3.7/dist-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
   1128         if not (self._backward_hooks or self._forward_hooks or self._forward_pre_hooks or _global_backward_hooks
   1129                 or _global_forward_hooks or _global_forward_pre_hooks):
-> 1130             return forward_call(*input, **kwargs)
   1131         # Do not call functions when jit is used
   1132         full_backward_hooks, non_full_backward_hooks = [], []

<ipython-input-29-09c58015e865> in forward(self, x)
     27         x = layer(x)
     28         print(x.shape)
---> 29     return self.network(x)
     30 
     31 model = MyCNN()

/usr/local/lib/python3.7/dist-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
   1128         if not (self._backward_hooks or self._forward_hooks or self._forward_pre_hooks or _global_backward_hooks
   1129                 or _global_forward_hooks or _global_forward_pre_hooks):
-> 1130             return forward_call(*input, **kwargs)
   1131         # Do not call functions when jit is used
   1132         full_backward_hooks, non_full_backward_hooks = [], []

/usr/local/lib/python3.7/dist-packages/torch/nn/modules/container.py in forward(self, input)
    137     def forward(self, input):
    138         for module in self:
--> 139             input = module(input)
    140         return input
    141 

/usr/local/lib/python3.7/dist-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
   1128         if not (self._backward_hooks or self._forward_hooks or self._forward_pre_hooks or _global_backward_hooks
   1129                 or _global_forward_hooks or _global_forward_pre_hooks):
-> 1130             return forward_call(*input, **kwargs)
   1131         # Do not call functions when jit is used
   1132         full_backward_hooks, non_full_backward_hooks = [], []

/usr/local/lib/python3.7/dist-packages/torch/nn/modules/conv.py in forward(self, input)
    455 
    456     def forward(self, input: Tensor) -> Tensor:
--> 457         return self._conv_forward(input, self.weight, self.bias)
    458 
    459 class Conv3d(_ConvNd):

/usr/local/lib/python3.7/dist-packages/torch/nn/modules/conv.py in _conv_forward(self, input, weight, bias)
    452                             _pair(0), self.dilation, self.groups)
    453         return F.conv2d(input, weight, bias, self.stride,
--> 454                         self.padding, self.dilation, self.groups)
    455 
    456     def forward(self, input: Tensor) -> Tensor:

RuntimeError: Expected 3D (unbatched) or 4D (batched) input to conv2d, but got input of size: [64, 2]

I really appreciate the help.我真的很感激帮助。 I apologize if the solution is simple but I didn't see it easily.如果解决方案很简单,我深表歉意,但我没有轻易看到它。 Cheers.干杯。

The data seems to have changed because the size of the images is (64, 3, 512, 512) and the labels are (64,2).数据似乎发生了变化,因为图像的大小为 (64, 3, 512, 512),标签为 (64,2)。 And if the shape fits well, it works fine.如果形状合适,它就可以正常工作。 Here is my code.这是我的代码。

Code:代码:

import torch
import torch.nn as nn
import torch.optim as optim

class MyCNN(nn.Module):
  def __init__(self):
    super(MyCNN, self).__init__()
    self.network = nn.Sequential(
        nn.Conv2d(in_channels=3, out_channels=32, kernel_size=3),
        nn.ReLU(),
        nn.MaxPool2d(kernel_size=2),

        nn.Conv2d(32, 64, 3),
        nn.ReLU(),
        nn.MaxPool2d(2),

        nn.Conv2d(64, 128, 3),
        nn.ReLU(),
        nn.MaxPool2d(2),

        nn.Flatten(),
        nn.Linear(in_features=25088, out_features=2048),
        nn.ReLU(),
        nn.Linear(2048, 1024),
        nn.ReLU(),
        nn.Linear(1024, 2),
    )

  def forward(self, x):
    return self.network(x)

model = MyCNN()

criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(model.parameters(), lr = 0.001)

optimizer.zero_grad()

# Forward pass
images = torch.randn(64, 3, 128, 128)
labels = torch.randn(64, 2)
outputs = model(images)
loss = criterion(outputs, labels)
        
# Backward and optimize
loss.backward()
optimizer.step()

I recommend to change this line我建议更改此行

for images, labels in data_loader:  
        images, labels = images.to(device), labels.to(device)

to this对此

for labels, images in data_loader:  
        images, labels = images.to(device), labels.to(device)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 了解 Conv2d 的输入和 output 大小 - Understanding input and output size for Conv2d model.fit 给出 ValueError :检查输入时出错:预期的 conv2d 得到了形状为 () 的数组 - model.fit giving ValueError : Error when checking input: expected conv2d got array with shape () 预期 conv2d 输入具有形状 (5665,445,3) 但得到形状为 (1,445,3) 的阵列 - expected conv2d input to have shape (5665,445,3) but got aray with shape (1,445,3) conv2d 层的输入 0 与层不兼容:输入形状的预期轴 -1 具有值 1,但接收到形状为 [None, 64, 64, 3] 的输入 - Input 0 of layer conv2d is incompatible with layer: expected axis -1 of input shape to have value 1 but received input with shape [None, 64, 64, 3] 在 PyTorch 中计算用于图像分类的 Conv2d 的输入和输出大小 - Calculating input and output size for Conv2d in PyTorch for image classification 如何使用 Conv2d 解决输入大小错误? - How can i solve the Input Size Error with Conv2d? 输入到tf.keras的Conv2D层的大小不正确 - Input to tf.keras Conv2D layer not of appropriate size 预期以4D张量作为输入,取而代之的是2D张量 - Expected 4D tensor as input, got 2D tensor instead Keras Conv2D 输入形状 - Keras Conv2D input Shape Keras中Conv2D输入错误 - Incorrect input to Conv2D in Keras
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM