RuntimeError: Given groups=1, weight of size [512, 512, 3, 3], expected input[200, 256, 24, 24] to have 512 channels, but got 256 channels instead

I don’t understand why my model produces this error, even when the inputs passed into the convolutional layers are correct. Here is my code and the resulting error

Code:
class EmotionModel(ImageClassificationBase):
def init(self, channel_in, num_classes):
super().init()
self.conv1 = nn.Sequential(
nn.Conv2d(channel_in, 64, kernel_size = 3, padding = 1), # in: 3 * 96 * 96
nn.BatchNorm2d(64),
nn.ReLU() # out: 64 * 96 * 96
)
self.conv2 = nn.Sequential(
nn.Conv2d(64, 128, kernel_size = 3, padding = 1), # in: 64 * 96 * 96
nn.BatchNorm2d(128),
nn.ReLU(),
nn.MaxPool2d(2) # out: 128 * 48 * 48
)
self.res1 = nn.Sequential(
nn.Conv2d(128, 128, kernel_size = 3, padding = 1), # in: 128 * 48 * 48
nn.BatchNorm2d(128),
nn.ReLU(), # out: 128 * 48 * 48

        nn.Conv2d(128, 128, kernel_size = 3, padding = 1), # in: 128 * 48 * 48
        nn.BatchNorm2d(128),
        nn.ReLU(), # out: 128 * 48 * 48

        nn.Conv2d(128, 128, kernel_size = 3, padding = 1), # in: 128 * 48 * 48
        nn.BatchNorm2d(128),
        nn.ReLU() # out: 128 * 48 * 48
    )
    self.conv3 = nn.Sequential(
        nn.Conv2d(128, 256, kernel_size = 3, padding = 1), # in: 128 * 48 * 48
        nn.BatchNorm2d(256),
        nn.ReLU(),
        nn.MaxPool2d(2) # out: 256 * 24 * 24
    )
    self.conv4 = nn.Sequential(
        nn.Conv2d(256, 512, kernel_size = 3, padding = 1), # in: 256 * 24 * 24
        nn.BatchNorm2d(512),
        nn.ReLU(),
        nn.MaxPool2d(2) # out: 512 * 12 * 12
    )
    self.res2 = nn.Sequential(
        nn.Conv2d(512, 512, kernel_size = 3, padding = 1), # in: 512 * 12 * 12
        nn.BatchNorm2d(512),
        nn.ReLU(), # out: 512 * 12 * 12
        
        nn.Conv2d(512, 512, kernel_size = 3, padding = 1), # in: 512 * 12 * 12
        nn.BatchNorm2d(512),
        nn.ReLU(), # 512 * 12 * 12

        nn.Conv2d(512, 512, kernel_size = 3, padding = 1), # in: 512 * 12 * 12
        nn.BatchNorm2d(512),
        nn.ReLU() # out: 512 * 12 * 12
    )
    self.conv5 = nn.Sequential(
        nn.Conv2d(512, 512, kernel_size = 3, padding = 1), # in: 512 * 12 * 12
        nn.BatchNorm2d(512),
        nn.ReLU(),
        nn.MaxPool2d(3) # out: 512 * 4 * 4
    )
    self.classifier = nn.Sequential(
        nn.MaxPool2d(4), # in: 512 * 4 * 4
        nn.Flatten(),
        nn.Dropout(0.2),
        nn.Linear(512, num_classes) # out: 512 * 1 * 1
    )

def forward(self, x):
    out = self.conv1(x)
    out = self.conv2(out)
    out = self.res1(out) + out
    out = self.conv3(out)
    out = self.conv4(out)
    out = self.res2(out) + out
    out = self.conv5(out)
    out = self.classifier(out)
    return out

Error:

RuntimeError Traceback (most recent call last)
Cell In[26], line 1
----> 1 history = [evaluate(model, valid_dataloader)]

File ~\anaconda3\Lib\site-packages\torch\utils_contextlib.py:115, in context_decorator..decorate_context(*args, **kwargs)
112 @functools.wraps(func)
113 def decorate_context(*args, **kwargs):
114 with ctx_factory():
→ 115 return func(*args, **kwargs)

Cell In[18], line 4, in evaluate(model, val_loader)
1 @torch.no_grad()
2 def evaluate(model, val_loader):
3 model.eval()
----> 4 outputs = [model.validation_step(batch) for batch in val_loader]
5 return model.validation_epoch_end(outputs)

Cell In[18], line 4, in (.0)
1 @torch.no_grad()
2 def evaluate(model, val_loader):
3 model.eval()
----> 4 outputs = [model.validation_step(batch) for batch in val_loader]
5 return model.validation_epoch_end(outputs)

Cell In[15], line 10, in ImageClassificationBase.validation_step(self, batch)
8 def validation_step(self, batch):
9 images, labels = batch
—> 10 out = self(images)
11 loss = F.cross_entropy(out, labels)
12 acc = accuracy(out, labels)

File ~\anaconda3\Lib\site-packages\torch\nn\modules\module.py:1511, in Module._wrapped_call_impl(self, *args, **kwargs)
1509 return self._compiled_call_impl(*args, **kwargs) # type: ignore[misc]
1510 else:
→ 1511 return self._call_impl(*args, **kwargs)

File ~\anaconda3\Lib\site-packages\torch\nn\modules\module.py:1520, in Module._call_impl(self, *args, **kwargs)
1515 # If we don’t have any hooks, we want to skip the rest of the logic in
1516 # this function, and just call forward.
1517 if not (self._backward_hooks or self._backward_pre_hooks or self._forward_hooks or self._forward_pre_hooks
1518 or _global_backward_pre_hooks or _global_backward_hooks
1519 or _global_forward_hooks or _global_forward_pre_hooks):
→ 1520 return forward_call(*args, **kwargs)
1522 try:
1523 result = None

Cell In[16], line 71, in EmotionModel.forward(self, x)
69 out = self.res1(out) + out
70 out = self.conv3(out)
—> 71 out = self.conv4(out)
72 out = self.res2(out) + out
73 out = self.conv4(out)

File ~\anaconda3\Lib\site-packages\torch\nn\modules\module.py:1511, in Module._wrapped_call_impl(self, *args, **kwargs)
1509 return self._compiled_call_impl(*args, **kwargs) # type: ignore[misc]
1510 else:
→ 1511 return self._call_impl(*args, **kwargs)

File ~\anaconda3\Lib\site-packages\torch\nn\modules\module.py:1520, in Module._call_impl(self, *args, **kwargs)
1515 # If we don’t have any hooks, we want to skip the rest of the logic in
1516 # this function, and just call forward.
1517 if not (self._backward_hooks or self._backward_pre_hooks or self._forward_hooks or self._forward_pre_hooks
1518 or _global_backward_pre_hooks or _global_backward_hooks
1519 or _global_forward_hooks or _global_forward_pre_hooks):
→ 1520 return forward_call(*args, **kwargs)
1522 try:
1523 result = None

File ~\anaconda3\Lib\site-packages\torch\nn\modules\container.py:217, in Sequential.forward(self, input)
215 def forward(self, input):
216 for module in self:
→ 217 input = module(input)
218 return input

File ~\anaconda3\Lib\site-packages\torch\nn\modules\module.py:1511, in Module._wrapped_call_impl(self, *args, **kwargs)
1509 return self._compiled_call_impl(*args, **kwargs) # type: ignore[misc]
1510 else:
→ 1511 return self._call_impl(*args, **kwargs)

File ~\anaconda3\Lib\site-packages\torch\nn\modules\module.py:1520, in Module._call_impl(self, *args, **kwargs)
1515 # If we don’t have any hooks, we want to skip the rest of the logic in
1516 # this function, and just call forward.
1517 if not (self._backward_hooks or self._backward_pre_hooks or self._forward_hooks or self._forward_pre_hooks
1518 or _global_backward_pre_hooks or _global_backward_hooks
1519 or _global_forward_hooks or _global_forward_pre_hooks):
→ 1520 return forward_call(*args, **kwargs)
1522 try:
1523 result = None

File ~\anaconda3\Lib\site-packages\torch\nn\modules\conv.py:460, in Conv2d.forward(self, input)
459 def forward(self, input: Tensor) → Tensor:
→ 460 return self._conv_forward(input, self.weight, self.bias)

File ~\anaconda3\Lib\site-packages\torch\nn\modules\conv.py:456, in Conv2d._conv_forward(self, input, weight, bias)
452 if self.padding_mode != ‘zeros’:
453 return F.conv2d(F.pad(input, self._reversed_padding_repeated_twice, mode=self.padding_mode),
454 weight, bias, self.stride,
455 _pair(0), self.dilation, self.groups)
→ 456 return F.conv2d(input, weight, bias, self.stride,
457 self.padding, self.dilation, self.groups)

RuntimeError: Given groups=1, weight of size [512, 512, 3, 3], expected input[200, 256, 24, 24] to have 512 channels, but got 256 channels instead

The stacktrace does not match the error message as self.conv4 fails while it should expect 256 input channels. Are you able to reproduce the issue using your posted code? If so, could you format it and share the input shape?

This is the models architecture I built

class EmotionModel(ImageClassificationBase):
def init(self, channel_in, num_classes):
super().init()
self.conv1 = nn.Sequential(
nn.Conv2d(channel_in, 64, kernel_size = 3, padding = 1), # in: 3 * 96 * 96
nn.BatchNorm2d(64),
nn.ReLU() # out: 64 * 96 * 96
)
self.conv2 = nn.Sequential(
nn.Conv2d(64, 128, kernel_size = 3, padding = 1), # in: 64 * 96 * 96
nn.BatchNorm2d(128),
nn.ReLU(),
nn.MaxPool2d(2) # out: 128 * 48 * 48
)
self.res1 = nn.Sequential(
nn.Conv2d(128, 128, kernel_size = 3, padding = 1), # in: 128 * 48 * 48
nn.BatchNorm2d(128),
nn.ReLU(), # out: 128 * 48 * 48

nn.Conv2d(128, 128, kernel_size = 3, padding = 1), # in: 128 * 48 * 48
nn.BatchNorm2d(128),
nn.ReLU(), # out: 128 * 48 * 48

nn.Conv2d(128, 128, kernel_size = 3, padding = 1), # in: 128 * 48 * 48
nn.BatchNorm2d(128),
nn.ReLU() # out: 128 * 48 * 48
)
self.conv3 = nn.Sequential(
nn.Conv2d(128, 256, kernel_size = 3, padding = 1), # in: 128 * 48 * 48
nn.BatchNorm2d(256),
nn.ReLU(),
nn.MaxPool2d(2) # out: 256 * 24 * 24
)
self.conv4 = nn.Sequential(
nn.Conv2d(256, 512, kernel_size = 3, padding = 1), # in: 256 * 24 * 24
nn.BatchNorm2d(512),
nn.ReLU(),
nn.MaxPool2d(2) # out: 512 * 12 * 12
)
self.res2 = nn.Sequential(
nn.Conv2d(512, 512, kernel_size = 3, padding = 1), # in: 512 * 12 * 12
nn.BatchNorm2d(512),
nn.ReLU(), # out: 512 * 12 * 12

nn.Conv2d(512, 512, kernel_size = 3, padding = 1), # in: 512 * 12 * 12
nn.BatchNorm2d(512),
nn.ReLU(), # 512 * 12 * 12

nn.Conv2d(512, 512, kernel_size = 3, padding = 1), # in: 512 * 12 * 12
nn.BatchNorm2d(512),
nn.ReLU() # out: 512 * 12 * 12
)
self.conv5 = nn.Sequential(
nn.Conv2d(512, 512, kernel_size = 3, padding = 1), # in: 512 * 12 * 12
nn.BatchNorm2d(512),
nn.ReLU(),
nn.MaxPool2d(3) # out: 512 * 4 * 4
)
self.classifier = nn.Sequential(
nn.MaxPool2d(4), # in: 512 * 4 * 4
nn.Flatten(),
nn.Dropout(0.2),
nn.Linear(512, num_classes) # out: 8
)

def forward(self, x):
out = self.conv1(x)
out = self.conv2(out)

out = self.res1(out) + out

out = self.conv3(out)
out = self.conv4(out)

out = self.res2(out) + out

out = self.conv5(out)
out = self.classifier(out)
return out

So please where and where does that error comes from, also if I want to track the output shape after ach convolutional layer, how can I do that? Thanks

Your model works fine using:

model = EmotionModel(3, 10)
x = torch.randn(1, 3, 112, 112)
out = model(x)
print(out.shape)
# torch.Size([1, 10])

If you get stuck, please follow my previous request in trying to reproduce the issue using your own minimal code snippet first. Once you are able to reproduce it, post it in a formatted way by wrapping it into three backticks ```.

Hello, I wish I could get an understanding of what you mean, but here is a minimal code snippet passing through the model and giving out no error with the same shape as my image tensors

x = torch.randn(3, 96, 96)
print(x.shape)
x = to_device(x, device)
x = x.unsqueeze(0)
out = model(x)
out.shape
torch.Size([3, 96, 96])

[58]:

torch.Size([1, 8])

In this case your posted code snippet isn’t able to reproduce the issue and you would need to isolate the issue further as we cannot reproduce the error.