ResNet18 output shape depending on number of in_channels

snowe: <p>Hi everyone <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=12" title=":slight_smile:" class="emoji" alt=":slight_smile:" loading="lazy" width="20" height="20"></p>
<p>I am using the ResNet18 for a Deep Learning project on CIFAR10. However, I want to pass the grayscale version of the CIFAR10 images to the ResNet18. When I change the expected number of input channels and change the number of classes from 1000 to 10 I get output shapes that I don’t understand. Here is my code:</p>
<pre><code class="lang-auto">from torchsummary import summary
import torchvision.models as PyTorchModels
r = PyTorchModels.resnet18()
num_in_channels = 1
num_out_channels = r.conv1.out_channels
size_kernel = r.conv1.kernel_size
num_in_features = r.fc.in_features
classes = 10
r.conv1 = nn.Conv2d(in_channels=num_in_channels, out_channels=num_out_channels, kernel_size=size_kernel)
r.fc = nn.Linear(in_features=num_in_features, out_features=classes)
model = r
summary(model.cuda(), (1, 32, 32))
</code></pre>
<p>It produces the following output (only the first couple of layers are shown):<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/9/e/9e47a6da94615dfe62d1a68e97a10fce92b96af1.png" data-download-href="https://discuss.pytorch.org/uploads/default/9e47a6da94615dfe62d1a68e97a10fce92b96af1" title="resnet18-gs-output"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/9/e/9e47a6da94615dfe62d1a68e97a10fce92b96af1_2_578x500.png" alt="resnet18-gs-output" data-base62-sha1="mAcX7qJteNXNX53kYFmrilKn0uB" width="578" height="500" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/9/e/9e47a6da94615dfe62d1a68e97a10fce92b96af1_2_578x500.png, https://discuss.pytorch.org/uploads/default/optimized/3X/9/e/9e47a6da94615dfe62d1a68e97a10fce92b96af1_2_867x750.png 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/9/e/9e47a6da94615dfe62d1a68e97a10fce92b96af1_2_1156x1000.png 2x" data-dominant-color="F2F2F2"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">resnet18-gs-output</span><span class="informations">1362×1177 72.2 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>
<p>Why is the first output of shape [-1, 64, 26, 26]? Where does the 26 come from? I would expect it to be of shape [-1, 64, 16, 16]. More generally, how does the number of in_channels (e.g. RGB vs. grayscale) effect the output shape?</p>
<p>Any help is very much appreciated!</p>
<p>All the best<br>
snowe</p>

Toby: <p>For the first question:</p>
<p>Run <code>print(model)</code>, you should see the first layer is <code>(conv1): Conv2d(3, 64, kernel_size=(7, 7), stride=(2, 2), padding=(3, 3), bias=False)</code>. You modify your first layer without padding and stride, that why it became 26.</p>
<p>For the second question:</p>
<p>Because this is CNN models, so the number of <code>in_channels</code> will not affect to your output shape, the number of class do that.<br>
For example: with ImageNet, the output of ResNet18 is <code>1x1000</code>,  with CIFAR10, it is <code>1x10</code></p>

snowe: <p><a class="mention" href="/u/toby">@Toby</a> Thank you so much! That solved it. I totally forgot to add the padding, stride, and bias <img src="https://discuss.pytorch.org/images/emoji/apple/see_no_evil.png?v=9" title=":see_no_evil:" class="emoji" alt=":see_no_evil:"></p>
<p>Edit: a quick follow up question: does PyTorch automatically adjust the depth of the filters?</p>

Toby: <p>Next time, you should open a new question <img src="https://discuss.pytorch.org/images/emoji/apple/wink.png?v=9" title=":wink:" class="emoji" alt=":wink:"><br>
To answer your question: no, it doesn’t. You can take a look at the <a href="https://github.com/pytorch/pytorch/blob/master/torch/nn/modules/conv.py" rel="noopener nofollow ugc">source code</a></p>