A Conv layer in my ResNet has the wrong number of input channels, and I can't find it

Hi there

I’m new to Pytorch, and I’m solving an image classification problem. I’ve built a ResNet (as opposed to loading it from Pytorch hub because I wanted to learn how they work). Here’s the code. The relevant functions begin at line #257.

One of the convolutional layers has the wrong number of input channels, and I’m struggling to locate the exact source of the problem.

The error reads:

Given groups=1, weight of size [128, 64, 1, 1], expected input[20, 128, 45, 45] to have 64 channels, but got 128 channels instead

I realise that many similar questions have been asked, and I’ve tried to implement relevant answers to those questions to no avail.

When I run into these I normally add a print(x.shape) after line 281 and see which ConvBlock call is initiating the error. It’s a good starting point. Break the after the first few prints.

1 Like

Thanks for the tip. In addition to printing x.shape, I decided to print self.conv, which is the nn.Sequential object that corresponds to that layer’s contents. Things broke down when the model arrived at the following:

torch.Size([20, 64, 90, 90])
Sequential(
  (0): Conv2d(128, 32, kernel_size=(1, 1), stride=(1, 1))
  (1): BatchNorm2d(32, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
)

So x has 64 input channels, but the layer that’s supposed to be accepting it is set up for 128.

I somehow need to change the code to resolve this discrepancy. The trouble is that I don’t know how.