ValueError: Input dimension should be at least 3

username: Selvam_Dharma
cooked: <pre><code class="lang-python">#adding the extra layers 
class added_layers(nn.Module):
  def __init__(self):
    super(added_layers,self).__init__()
    self.add = nn.Sequential(
        
        nn.AdaptiveMaxPool2d((1, 1)),
        nn.Linear(2048,256),nn.ReLU(),
        nn.Linear(256,3),nn.Softmax(dim=1)
    )

  def forward(self,x):
    add = self.add(x)

    return add

added_layer = added_layers().to('cuda')

model = nn.Sequential(model,added_layer) 
model.to('cuda')
</code></pre>
<p>and</p>
<pre><code class="lang-python">#removing the fully connected layer
class nochange(nn.Module):
    def __init__(self):
        super(nochange, self).__init__()
        
    def forward(self, x):
        return x
model.fc = nochange()

#freeze the trained layers
for parameters in model.parameters():
  parameters.requires_grad = False
</code></pre>
<p>these are my code for combining pretrained model exception with additional layers. I am getting the following error:</p>
<pre><code class="lang-python">---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
&lt;ipython-input-13-3b76556c6596&gt; in &lt;module&gt;
      1 #model training for num_ epoch
----&gt; 2 training_loss,val_loss  = training(model,optimizer,train_loader,val_loader,test_loader,num_epoch,device = 'cuda',save_dir = '/content/drive/MyDrive/Applied_Artificial_intelligence/saved_model')
      3 plt.plot(training_loss)

12 frames
/usr/local/lib/python3.9/dist-packages/torch/nn/modules/utils.py in _list_with_default(out_size, defaults)
     34         return out_size
     35     if len(defaults) &lt;= len(out_size):
---&gt; 36         raise ValueError(
     37             "Input dimension should be at least {}".format(len(out_size) + 1)
     38         )

ValueError: Input dimension should be at least 3
</code></pre>
<p>what would be the issue here,</p>
<pre><code class="lang-python">(conv2): DwsConvBlock(
          (activ): ReLU()
          (conv): DwsConv(
            (dw_conv): Conv2d(1536, 1536, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), groups=1536, bias=False)
            (pw_conv): Conv2d(1536, 2048, kernel_size=(1, 1), stride=(1, 1), bias=False)
          )
          (bn): BatchNorm2d(2048, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
        )
        (activ): ReLU(inplace=True)
        (pool): AvgPool2d(kernel_size=10, stride=1, padding=0)
      )
    )
    (output): Linear(in_features=2048, out_features=1000, bias=True)
    (fc): nochange()
  )
  (1): added_layers(
    (add): Sequential(
      (0): AdaptiveMaxPool2d(output_size=(1, 1))
      (1): Linear(in_features=2048, out_features=256, bias=True)
      (2): ReLU()
      (3): Linear(in_features=256, out_features=3, bias=True)
      (4): Softmax(dim=1)
    )
  )
)
</code></pre>
<p>my last few layers are looking like this</p>

username: ptrblck
cooked: <p>Your input seems to be missing a dimension and I guess <code>AdaptiveMaxPool2d</code> is raising the error as seen here:</p>
<pre><code class="lang-python">pool = nn.AdaptiveMaxPool2d((1, 1))

# works since [batch_size, channels, height, width] is provided
x = torch.randn(1, 3, 24, 24)
out = pool(x)
print(out.shape)
# torch.Size([1, 3, 1, 1])

# works since [channels, height, width] is provided
x = torch.randn(3, 24, 24)
out = pool(x)
print(out.shape)
# torch.Size([3, 1, 1])

# breaks
x = torch.randn(3, 24)
out = pool(x)
# ValueError: Input dimension should be at least 3
</code></pre>

username: Selvam_Dharma
cooked: <pre><code class="lang-python">for img, label in train_loader:
  print(img.shape)
  break
</code></pre>
<p>my dimensions are already as you mentioned  torch.Size([16, 3, 299, 299]), but still getting this error.</p>
<p>will it this model architecture Is correct,</p>
<pre><code class="lang-python">        (pool): AvgPool2d(kernel_size=10, stride=1, padding=0)
      )
    )
    (output): Linear(in_features=2048, out_features=1000, bias=True)
    (fc): nochange()
  )
  (1): added_layers(
    (add): Sequential(
      (0): AdaptiveMaxPool2d(output_size=(1, 1))
      (1): Linear(in_features=2048, out_features=256, bias=True)
      (2): ReLU()
      (3): Linear(in_features=256, out_features=3, bias=True)
      (4): Softmax(dim=1)
    )
  )
</code></pre>
<p>did my input and output features of added_layers correct?</p>

username: thecho7
cooked: <p>What is a dimension of the output tensor after</p>
<pre><code class="lang-auto">(output): Linear(in_features=2048, out_features=1000, bias=True)
</code></pre>
<p>According to <a href="https://pytorch.org/docs/stable/generated/torch.nn.AdaptiveMaxPool2d.html#torch.nn.AdaptiveMaxPool2d" class="inline-onebox" rel="noopener nofollow ugc">AdaptiveMaxPool2d — PyTorch 2.0 documentation</a>, 2D Pooling requires at least 3 dimensions as an input.</p>
<p>I guess the input of <code>added_layers</code> has 2 dimensions.<br>
Check this by,</p>
<pre><code class="lang-auto">class added_layers(nn.Module):
  def __init__(self):
    super(added_layers,self).__init__()
    self.add = nn.Sequential(
        
        nn.AdaptiveMaxPool2d((1, 1)),
        nn.Linear(2048,256),nn.ReLU(),
        nn.Linear(256,3),nn.Softmax(dim=1)
    )

  def forward(self,x):
    print(x.shape) ######## print ##########
    add = self.add(x)

    return add
</code></pre>

username: Selvam_Dharma
cooked: <p>I have sought out my issue. Actually, the problem was with the Fully Connected layer, where I Have added over the existing fully connected layer in my model. After deleting the existing layer it worked. Thank you for all the replies.</p>