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

Wells: <p>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</p>
<p>Code:<br>
class EmotionModel(ImageClassificationBase):<br>
def <strong>init</strong>(self, channel_in, num_classes):<br>
super().<strong>init</strong>()<br>
self.conv1 = nn.Sequential(<br>
nn.Conv2d(channel_in, 64, kernel_size = 3, padding = 1), # in: 3 * 96 * 96<br>
nn.BatchNorm2d(64),<br>
nn.ReLU() # out: 64 * 96 * 96<br>
)<br>
self.conv2 = nn.Sequential(<br>
nn.Conv2d(64, 128, kernel_size = 3, padding = 1), # in: 64 * 96 * 96<br>
nn.BatchNorm2d(128),<br>
nn.ReLU(),<br>
nn.MaxPool2d(2) # out: 128 * 48 * 48<br>
)<br>
self.res1 = nn.Sequential(<br>
nn.Conv2d(128, 128, kernel_size = 3, padding = 1), # in: 128 * 48 * 48<br>
nn.BatchNorm2d(128),<br>
nn.ReLU(), # out: 128 * 48 * 48</p>
<pre><code>        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
</code></pre>
<p>Error:</p>
<p>RuntimeError                              Traceback (most recent call last)<br>
Cell In[26], line 1<br>
----&gt; 1 history = [evaluate(model, valid_dataloader)]</p>
<p>File ~\anaconda3\Lib\site-packages\torch\utils_contextlib.py:115, in context_decorator..decorate_context(*args, **kwargs)<br>
112 <span class="mention">@functools.wraps</span>(func)<br>
113 def decorate_context(*args, **kwargs):<br>
114     with ctx_factory():<br>
 → 115         return func(*args, **kwargs)</p>
<p>Cell In[18], line 4, in evaluate(model, val_loader)<br>
1 <span class="mention">@torch.no_grad</span>()<br>
2 def evaluate(model, val_loader):<br>
3     model.eval()<br>
----&gt; 4     outputs = [model.validation_step(batch) for batch in val_loader]<br>
5     return model.validation_epoch_end(outputs)</p>
<p>Cell In[18], line 4, in (.0)<br>
1 <span class="mention">@torch.no_grad</span>()<br>
2 def evaluate(model, val_loader):<br>
3     model.eval()<br>
----&gt; 4     outputs = [model.validation_step(batch) for batch in val_loader]<br>
5     return model.validation_epoch_end(outputs)</p>
<p>Cell In[15], line 10, in ImageClassificationBase.validation_step(self, batch)<br>
8 def validation_step(self, batch):<br>
9     images, labels = batch<br>
—&gt; 10     out = self(images)<br>
11     loss = F.cross_entropy(out, labels)<br>
12     acc = accuracy(out, labels)</p>
<p>File ~\anaconda3\Lib\site-packages\torch\nn\modules\module.py:1511, in Module._wrapped_call_impl(self, *args, **kwargs)<br>
1509     return self._compiled_call_impl(*args, **kwargs)  # type: ignore[misc]<br>
1510 else:<br>
 → 1511     return self._call_impl(*args, **kwargs)</p>
<p>File ~\anaconda3\Lib\site-packages\torch\nn\modules\module.py:1520, in Module._call_impl(self, *args, **kwargs)<br>
1515 # If we don’t have any hooks, we want to skip the rest of the logic in<br>
1516 # this function, and just call forward.<br>
1517 if not (self._backward_hooks or self._backward_pre_hooks or self._forward_hooks or self._forward_pre_hooks<br>
1518         or _global_backward_pre_hooks or _global_backward_hooks<br>
1519         or _global_forward_hooks or _global_forward_pre_hooks):<br>
 → 1520     return forward_call(*args, **kwargs)<br>
1522 try:<br>
1523     result = None</p>
<p>Cell In[16], line 71, in EmotionModel.forward(self, x)<br>
69 out = self.res1(out) + out<br>
70 out = self.conv3(out)<br>
—&gt; 71 out = self.conv4(out)<br>
72 out = self.res2(out) + out<br>
73 out = self.conv4(out)</p>
<p>File ~\anaconda3\Lib\site-packages\torch\nn\modules\module.py:1511, in Module._wrapped_call_impl(self, *args, **kwargs)<br>
1509     return self._compiled_call_impl(*args, **kwargs)  # type: ignore[misc]<br>
1510 else:<br>
 → 1511     return self._call_impl(*args, **kwargs)</p>
<p>File ~\anaconda3\Lib\site-packages\torch\nn\modules\module.py:1520, in Module._call_impl(self, *args, **kwargs)<br>
1515 # If we don’t have any hooks, we want to skip the rest of the logic in<br>
1516 # this function, and just call forward.<br>
1517 if not (self._backward_hooks or self._backward_pre_hooks or self._forward_hooks or self._forward_pre_hooks<br>
1518         or _global_backward_pre_hooks or _global_backward_hooks<br>
1519         or _global_forward_hooks or _global_forward_pre_hooks):<br>
 → 1520     return forward_call(*args, **kwargs)<br>
1522 try:<br>
1523     result = None</p>
<p>File ~\anaconda3\Lib\site-packages\torch\nn\modules\container.py:217, in Sequential.forward(self, input)<br>
215 def forward(self, input):<br>
216     for module in self:<br>
 → 217         input = module(input)<br>
218     return input</p>
<p>File ~\anaconda3\Lib\site-packages\torch\nn\modules\module.py:1511, in Module._wrapped_call_impl(self, *args, **kwargs)<br>
1509     return self._compiled_call_impl(*args, **kwargs)  # type: ignore[misc]<br>
1510 else:<br>
 → 1511     return self._call_impl(*args, **kwargs)</p>
<p>File ~\anaconda3\Lib\site-packages\torch\nn\modules\module.py:1520, in Module._call_impl(self, *args, **kwargs)<br>
1515 # If we don’t have any hooks, we want to skip the rest of the logic in<br>
1516 # this function, and just call forward.<br>
1517 if not (self._backward_hooks or self._backward_pre_hooks or self._forward_hooks or self._forward_pre_hooks<br>
1518         or _global_backward_pre_hooks or _global_backward_hooks<br>
1519         or _global_forward_hooks or _global_forward_pre_hooks):<br>
 → 1520     return forward_call(*args, **kwargs)<br>
1522 try:<br>
1523     result = None</p>
<p>File ~\anaconda3\Lib\site-packages\torch\nn\modules\conv.py:460, in Conv2d.forward(self, input)<br>
459 def forward(self, input: Tensor) → Tensor:<br>
 → 460     return self._conv_forward(input, self.weight, self.bias)</p>
<p>File ~\anaconda3\Lib\site-packages\torch\nn\modules\conv.py:456, in Conv2d._conv_forward(self, input, weight, bias)<br>
452 if self.padding_mode != ‘zeros’:<br>
453     return F.conv2d(F.pad(input, self._reversed_padding_repeated_twice, mode=self.padding_mode),<br>
454                     weight, bias, self.stride,<br>
455                     _pair(0), self.dilation, self.groups)<br>
 → 456 return F.conv2d(input, weight, bias, self.stride,<br>
457                 self.padding, self.dilation, self.groups)</p>
<p>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</p>

ptrblck: <p>The stacktrace does not match the error message as <code>self.conv4</code> 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?</p>

Wells: <p>This is the models architecture I built</p>
<p>class EmotionModel(ImageClassificationBase):<br>
def <strong>init</strong>(self, channel_in, num_classes):<br>
super().<strong>init</strong>()<br>
self.conv1 = nn.Sequential(<br>
nn.Conv2d(channel_in, 64, kernel_size = 3, padding = 1), # in: 3 * 96 * 96<br>
nn.BatchNorm2d(64),<br>
nn.ReLU() # out: 64 * 96 * 96<br>
)<br>
self.conv2 = nn.Sequential(<br>
nn.Conv2d(64, 128, kernel_size = 3, padding = 1), # in: 64 * 96 * 96<br>
nn.BatchNorm2d(128),<br>
nn.ReLU(),<br>
nn.MaxPool2d(2) # out: 128 * 48 * 48<br>
)<br>
self.res1 = nn.Sequential(<br>
nn.Conv2d(128, 128, kernel_size = 3, padding = 1), # in: 128 * 48 * 48<br>
nn.BatchNorm2d(128),<br>
nn.ReLU(), # out: 128 * 48 * 48</p>
<p>nn.Conv2d(128, 128, kernel_size = 3, padding = 1), # in: 128 * 48 * 48<br>
nn.BatchNorm2d(128),<br>
nn.ReLU(), # out: 128 * 48 * 48</p>
<p>nn.Conv2d(128, 128, kernel_size = 3, padding = 1), # in: 128 * 48 * 48<br>
nn.BatchNorm2d(128),<br>
nn.ReLU() # out: 128 * 48 * 48<br>
)<br>
self.conv3 = nn.Sequential(<br>
nn.Conv2d(128, 256, kernel_size = 3, padding = 1), # in: 128 * 48 * 48<br>
nn.BatchNorm2d(256),<br>
nn.ReLU(),<br>
nn.MaxPool2d(2) # out: 256 * 24 * 24<br>
)<br>
self.conv4 = nn.Sequential(<br>
nn.Conv2d(256, 512, kernel_size = 3, padding = 1), # in: 256 * 24 * 24<br>
nn.BatchNorm2d(512),<br>
nn.ReLU(),<br>
nn.MaxPool2d(2) # out: 512 * 12 * 12<br>
)<br>
self.res2 = nn.Sequential(<br>
nn.Conv2d(512, 512, kernel_size = 3, padding = 1), # in: 512 * 12 * 12<br>
nn.BatchNorm2d(512),<br>
nn.ReLU(), # out: 512 * 12 * 12</p>
<p>nn.Conv2d(512, 512, kernel_size = 3, padding = 1), # in: 512 * 12 * 12<br>
nn.BatchNorm2d(512),<br>
nn.ReLU(), # 512 * 12 * 12</p>
<p>nn.Conv2d(512, 512, kernel_size = 3, padding = 1), # in: 512 * 12 * 12<br>
nn.BatchNorm2d(512),<br>
nn.ReLU() # out: 512 * 12 * 12<br>
)<br>
self.conv5 = nn.Sequential(<br>
nn.Conv2d(512, 512, kernel_size = 3, padding = 1), # in: 512 * 12 * 12<br>
nn.BatchNorm2d(512),<br>
nn.ReLU(),<br>
nn.MaxPool2d(3) # out: 512 * 4 * 4<br>
)<br>
self.classifier = nn.Sequential(<br>
nn.MaxPool2d(4), # in: 512 * 4 * 4<br>
nn.Flatten(),<br>
nn.Dropout(0.2),<br>
nn.Linear(512, num_classes) # out: 8<br>
)</p>
<p>def forward(self, x):<br>
out = self.conv1(x)<br>
out = self.conv2(out)</p>
<p>out = self.res1(out) + out</p>
<p>out = self.conv3(out)<br>
out = self.conv4(out)</p>
<p>out = self.res2(out) + out</p>
<p>out = self.conv5(out)<br>
out = self.classifier(out)<br>
return out</p>
<p>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</p>

ptrblck: <p>Your model works fine using:</p>
<pre data-code-wrap="python"><code class="lang-python">model = EmotionModel(3, 10)
x = torch.randn(1, 3, 112, 112)
out = model(x)
print(out.shape)
# torch.Size([1, 10])
</code></pre>
<p>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 ```.</p>

Wells: <p>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</p>
<pre><code class="lang-auto">x = torch.randn(3, 96, 96)
print(x.shape)
x = to_device(x, device)
x = x.unsqueeze(0)
out = model(x)
out.shape
</code></pre>
<pre><code class="lang-auto">torch.Size([3, 96, 96])

</code></pre>
<p>[58]:</p>
<pre><code class="lang-auto">torch.Size([1, 8])
</code></pre>

ptrblck: <aside class="quote no-group" data-username="Wells" data-post="5" data-topic="200815">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/wells/48/69216_2.png" class="avatar"> Wells:</div>
<blockquote>
<p>but here is a minimal code snippet passing through the model and giving out no error with the same shape as my image tensors</p>
</blockquote>
</aside>
<p>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.</p>