Search code examples
pythondeep-learningpytorch

how to solve a RuntimeError: expected input to have 64 channels, but got 16 channels instead in Pytorch


i test a relatively simple model with pytorch on the cifar 10 dataset.

However i get this strange error and i can't wrap my head around why :

x_shape: torch.Size([64, 64, 8, 8])
[...]
x_shape: torch.Size([16, 64, 8, 8])
---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-6-74e3c9f1b35c> in <cell line: 53>()
     62         image, label = image.to(device), label.to(device)
     63         optimizer.zero_grad()
---> 64         pred = model(image)
     65         loss = criterion(pred, label)
     66         total_train_loss += loss.item()

6 frames
/usr/local/lib/python3.10/dist-packages/torch/nn/modules/conv.py in _conv_forward(self, input, weight, bias)
    603                 self.groups,
    604             )
--> 605         return F.conv3d(
    606             input, weight, bias, self.stride, self.padding, self.dilation, self.groups
    607         )

Here the model i used, i can send the whole test / data initialisation if stackoverflow would let me, but basicly i reduce the image by two, two time before checking and it's look like where i get the error...

class Net(nn.Module):
    def __init__(self):
        super(Net, self).__init__()
        self.conv1 = nn.Conv2d(
            in_channels = 3,
            out_channels = 32,
            kernel_size = 5,
            stride = 1,
            padding = 2
        )
        self.conv2 = nn.Conv2d(
            in_channels=32,
            out_channels=64,
            kernel_size=5,
            stride=1,
            padding=2
        )
        self.conv3 = nn.Conv3d(
            in_channels=64,
            out_channels=64,
            kernel_size=5,
            stride=1,
            padding=2
        )
        self.pool = nn.MaxPool2d(2,2)
        self.fc1 = nn.Linear(1024, 0)
        self.fc2 = nn.Linear(0, 1024)
        self.fc3 = nn.Linear(1024, 10)

    def forward(self, x):
        x = self.pool(F.relu(self.conv1(x)))
        x = self.pool(F.relu(self.conv2(x)))
        print('x_shape:',x.shape)
        x = self.pool(F.relu(self.conv3(x)))
        x = torch.flatten(x, 1) # flatten all dimensions except batch
        x = F.relu(self.fc1(x))
        x = F.relu(self.fc2(x))
        x = self.fc3(x)
        return (x)

i try to Change the value on the model but to no avail.


Solution

  • Karl was right to change self.conv3 = nn.Conv3d to self.conv3 = nn.Conv2d and switch some value have fix the issue thank a lots it mith have take a while otherwise.