pythondeep-learningpytorchresnet

Why is number of in_channels in downsample Conv2d different from num_channels in bn2, shouldn't they be same?


The value in num_channels in bn2 and in_channels in Conv2d in downsample should be the same because the number of channels do not change at this point, so how come in resnet18 architecture they have different values.

enter image description here

Generally what happens is the in_channels is equal to out_channels of the previous channel.

Thank you for helping.


Solution

  • The number of channels is different because you're looking at a skip connection.

    You can't see it by printing your model (I'm assuming it's ResNet18 from Torchvision), but if you look up the implementation of the BasicBlock class that you've printed, it's the following:

        def forward(self, x: Tensor) -> Tensor:
            identity = x
    
            out = self.conv1(x)
            out = self.bn1(out)
            out = self.relu(out)
    
            out = self.conv2(out)
            out = self.bn2(out)
    
            if self.downsample is not None:
                identity = self.downsample(x)
    
            out += identity
            out = self.relu(out)
    
            return out
    

    In your case, downsample is not None. downsample takes as input the same x tensor the provided to the forward method, so the number of input channels must match the number of input channels that conv1 has, since conv1 acts on the same tensor.

    The output of downsample gets added to the output of bn2, so the number of output channels of downsample needs to match the number of output channels of bn2. Looking at the print you provided, you can see that does.