ResNet50 Multiple Output Nodes

username: Geeks_Sid
cooked: <p>Hi All,</p>
<p>I am trying to build two CNN’s on top of ResNet50, one as a regression node and one as a classification node.</p>
<pre><code class="lang-auto">class resnet50(nn.Module):
    def __init__(self):
        super(resnet50, self).__init__()
        self.left = nn.Sequential(
                                  nn.AdaptiveAvgPool2d(1024),
                                  nn.AdaptiveMaxPool2d(512),
                                  nn.Flatten(),
                                  nn.BatchNorm1d(512),
                                  nn.Dropout(0.25),
                                  nn.LeakyReLU(),
                                  nn.Linear(256, 64),
                                  nn.Dropout(0.5),
                                  nn.LeakyReLU(),
                                  nn.Linear(64, 1)
                                  )
        self.right = nn.Sequential(
                                  nn.AdaptiveAvgPool2d(1024),
                                  nn.AdaptiveMaxPool2d(512),
                                  nn.Flatten(),
                                  nn.BatchNorm1d(512),
                                  nn.Dropout(0.25),
                                  nn.LeakyReLU(),
                                  nn.Linear(256, 64),
                                  nn.Dropout(0.5),
                                  nn.LeakyReLU(),
                                  nn.Linear(64, 7)
                                  )
        self.model = models.resnet50(pretrained=True)
        self.model.fc = nn.Identity()
        
    def forward(self, x):
        x = self.model(x)
        print(x.shape)
        count_out = self.left(x)
        class_out = self.right(x)
        return count_out, class_out
</code></pre>
<p>I tried it in a way as given in this previous problem but i get the following error when i attempt a forward pass.</p>
<pre><code class="lang-auto">o1, o2 = model(x)
torch.Size([1, 2048])
Traceback (most recent call last):

  File "&lt;ipython-input-9-d7dc74ba0de2&gt;", line 1, in &lt;module&gt;
    o1, o2 = model(x)

  File "/home/siddhesh/.conda/envs/pytorch/lib/python3.6/site-packages/torch/nn/modules/module.py", line 541, in __call__
    result = self.forward(*input, **kwargs)

  File "/home/siddhesh/Work/Projects/LYSTO/Scripts/utils/new_models.py", line 55, in forward
    count_out = self.left(x)

  File "/home/siddhesh/.conda/envs/pytorch/lib/python3.6/site-packages/torch/nn/modules/module.py", line 541, in __call__
    result = self.forward(*input, **kwargs)

  File "/home/siddhesh/.conda/envs/pytorch/lib/python3.6/site-packages/torch/nn/modules/container.py", line 92, in forward
    input = module(input)

  File "/home/siddhesh/.conda/envs/pytorch/lib/python3.6/site-packages/torch/nn/modules/module.py", line 541, in __call__
    result = self.forward(*input, **kwargs)

  File "/home/siddhesh/.conda/envs/pytorch/lib/python3.6/site-packages/torch/nn/modules/pooling.py", line 1031, in forward
    return F.adaptive_avg_pool2d(input, self.output_size)

  File "/home/siddhesh/.conda/envs/pytorch/lib/python3.6/site-packages/torch/nn/functional.py", line 768, in adaptive_avg_pool2d
    return torch._C._nn.adaptive_avg_pool2d(input, _output_size)

RuntimeError: non-empty 3D or 4D (batch mode) tensor expected for input
</code></pre>
<p>Can someone help me as to where i might be ruining my forward pass with this?</p>
<p>Thanks</p>

username: Eta_C
cooked: <p><code>torch.nn.AdaptiveAvgPool2d</code>'s input is a 3D or 4D tensor.<br>
<code>x = self.model(x)</code> return a 2D tensor.</p>