RuntimeError: Given groups=1, weight of size [32, 16, 5, 5], expected input[16, 3, 448, 448] to have 16 channels, but got 3 channels instead

RuntimeError: Given groups=1, weight of size [32, 16, 5, 5], expected input[16, 3, 448, 448] to have 16 channels, but got 3 channels instead

我收到以下错误并且无法弄清楚原因。在将手电筒输入 CNN 之前,我打印了手电筒的输入尺寸:

torch.Size([16, 3, 448, 448])

这是我的错误信息:

---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-116-bfa18f2a99fd> in <module>()
     14     # Forward pass
     15     print(images.shape)
---> 16     outputs = modelll(images.float())
     17     loss = criterion(outputs, labels)
     18 

6 frames
/usr/local/lib/python3.7/dist-packages/torch/nn/modules/conv.py in _conv_forward(self, input, weight, bias)
    441                             _pair(0), self.dilation, self.groups)
    442         return F.conv2d(input, weight, bias, self.stride,
--> 443                         self.padding, self.dilation, self.groups)
    444 
    445     def forward(self, input: Tensor) -> Tensor:

RuntimeError: Given groups=1, weight of size [32, 16, 5, 5], expected input[16, 3, 448, 448] to have 16 channels, but got 3 channels instead

我定义了一个有 5 个卷积层和两个全连接层的 CNN。我以 16 个为一组进行喂食,并将图像调整为 (448x448)。图像是彩色的,所以我假设 torch.Size([16, 3, 448, 448]) 的输入是正确的。我是否需要将张量重新排列为 torch.Size([3, 448, 448, 16])?只是猜测,因为我对编码还很陌生。我在网上看过,但没能弄明白。任何帮助将不胜感激。

#Defining CNN
class ConvolutionalNet(nn.Module):
  def __init__(self, num_classes=182):
    super().__init__()

    self.layer1 = nn.Sequential(
        nn.Conv2d(3, 16, kernel_size=5, stride=1, padding=2),
        nn.BatchNorm2d(16),
        nn.ReLU(),
        nn.MaxPool2d(kernel_size=2, stride=2)
    )

    self.layer2 = nn.Sequential(
        nn.Conv2d(16, 32, kernel_size=5, stride=1, padding=2),
        nn.BatchNorm2d(32),
        nn.ReLU(),
        nn.MaxPool2d(kernel_size=2, stride=2)
    )

    self.layer3 = nn.Sequential(
        nn.Conv2d(32, 32, kernel_size=5, stride=1, padding=2),
        nn.BatchNorm2d(32),
        nn.ReLU(),
        nn.MaxPool2d(kernel_size=2, stride=2)
    )

    self.layer4 = nn.Sequential(
        nn.Conv2d(32, 64, kernel_size=5, stride=1, padding=2),
        nn.BatchNorm2d(64),
        nn.ReLU(),
        nn.MaxPool2d(kernel_size=2, stride=2)
    )

    self.layer5 = nn.Sequential(
        nn.Conv2d(64, 64, kernel_size=5, stride=1, padding=2),
        nn.BatchNorm2d(64),
        nn.ReLU(),
        nn.MaxPool2d(kernel_size=2, stride=2)
    )

    

    self.fc1 = nn.Linear(10*10*64, 240)
    self.fc2 = nn.Linear(240, 182)
  


  def forward(self, x):
    out = self.layer1(x)
    out = self.layer2(x)
    out = self.layer3(x)
    out = self.layer4(x)
    out = self.layer5(x)
    out = out.reshape(out.size(0), -1)
    out = F.relu(self.fc1((x)))
    out = self.fc3(x)

      
    return out


#Creating instance
modelll = ConvolutionalNet(num_classes).to(device)
modelll

num_classes = 182


criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(modelll.parameters(), lr=3e-4)

#Training loop
modelll.train() 
num_epochs = 5    # Set the model into `training` mode, because certain operators will perform differently during training and evaluation (e.g. dropout and batch normalization)
total_epochs = notebook.tqdm(range(num_epochs))


for epoch in total_epochs:
  for i, (images, labels, m) in enumerate(train_loader):  
    # Move tensors to the configured device
    images = images.to(device)
    labels = labels.to(device)
    # Forward pass
    print(images.shape)
    outputs = modelll(images.float()) 
    loss = criterion(outputs, labels)
    
    # Backward and optimize
    loss.backward()
    optimizer.step()
    optimizer.zero_grad()

    if (i + 1) % 10 == 0:
      total_epochs.set_description(
          'Epoch [{}/{}], Step [{}/{}], Loss: {:.4f}'.format(
              epoch + 1, num_epochs, i + 1, len(train_loader), loss.item()))

您还没有将输出传递给下一层的输入,您一直在使用输入。您应该将 forward 调用更改为:

  def forward(self, x):
    out = self.layer1(x)
    out = self.layer2(out)
    out = self.layer3(out)
    out = self.layer4(out)
    out = self.layer5(out)
    out = out.reshape(out.size(0), -1)
    out = F.relu(self.fc1((out)))
    out = self.fc3(out)
    return out