Resnet conv2d output has 2 dimensions (batch size and the tensor size)?

Oguzhan: <p>Hi, I would like to use just the beginnig part of resnet, so I did the thing below:</p>
<pre><code class="lang-auto">feature_map_extractor = torch.hub.load('pytorch/vision:v0.10.0', 'resnet50', pretrained=True)
feature_map_extractor.layer1 = nn.Sequential()
feature_map_extractor.layer2 = nn.Sequential()
feature_map_extractor.layer3 = nn.Sequential()
feature_map_extractor.layer4 = nn.Sequential()
feature_map_extractor.fc = nn.Sequential()
feature_map_extractor.avgpool = nn.Sequential()
feature_map_extractor.maxpool = nn.Sequential()
</code></pre>
<p>and the model is like this as I expected:<br>
ResNet(<br>
(conv1): Conv2d(3, 64, kernel_size=(7, 7), stride=(2, 2), padding=(3, 3), bias=False)<br>
(bn1): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)<br>
(relu): ReLU(inplace=True)<br>
(maxpool): Sequential()<br>
(layer1): Sequential()<br>
(layer2): Sequential()<br>
(layer3): Sequential()<br>
(layer4): Sequential()<br>
(avgpool): Sequential()<br>
(fc): Sequential()</p>
<p>and I would like to get the things that has a shape like square cause my image has square shape and resnet applies conv2d. But I got the shape below:</p>
<pre><code class="lang-auto">image = torch.randn((1,3,256,256))
a = feature_map_extractor(image)
a.shape
</code></pre>
<p>torch.Size([1, 1048576])</p>
<p>Why is that ?</p>

Karthik_Ganesan: <p>The final layer is a <code>FC</code> layer, which outputs <code>[M, N]</code>, where <code>M</code> is the batch size (1 in your case) and <code>N</code>, which is the total number of neurons in the final FC layer.</p>
<aside class="quote no-group" data-username="Oguzhan" data-post="1" data-topic="161403">
<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/oguzhan/48/34324_2.png" class="avatar"> Oguzhan:</div>
<blockquote>
<p>I would like to get the things that has a shape like square cause my image has square shape and resnet applies conv2d</p>
</blockquote>
</aside>
<p>ResNet does not JUST apply Conv2D. It also applies several layers afterwards, including the final FC layer. when you do <code>a = feature_map_extractor(image)</code>, it is the final FC layer which determines the final output shape.</p>

Oguzhan: <p><a class="mention" href="/u/karthik_ganesan">@Karthik_Ganesan</a>  But I define it as nn.Sequential(), so I must be bypass it. And as we see the output of the model architecture it is just Sequential() layer so it should do nothing.</p>

Oguzhan: <p>I did</p>
<pre><code class="lang-auto">feature_map_extractor = nn.Sequential(feature_map_extractor.conv1,feature_map_extractor.bn1,feature_map_extractor.relu)
</code></pre>
<p>and works fine. I think resnet50 implementation has F.flatten or something like that. So I could not remove it.</p>

Karthik_Ganesan: <p>Yes, models typically have a <code>flatten</code> or <code>view</code> function before the first <code>FC</code> layer. To take a look at all the layers and the shapes of their inputs and outputs, you can use something like <a href="https://pypi.org/project/torch-summary/" rel="noopener nofollow ugc">Torch Summary</a>.</p>