RuntimeError: Given input size: (256x1x1). Calculated output size: (256x0x0). Output size is too small

tmc: <p>I have vgg19 pretrained model, and I have used this code :</p>
<pre><code class="lang-auto">model_name = 'vgg19' # could be fbresnet152 or inceptionresnetv2
model4 = pretrainedmodels.__dict__[model_name](num_classes=1000, pretrained='imagenet')

model4.eval()
</code></pre>
<p>Then I wanted to remove the last 3 layers so I did this :</p>
<pre><code class="lang-auto">model4=list(model4.children())[:-3]
model4=nn.Sequential(*model4)
</code></pre>
<p>Then when I feed the model this image with this code :</p>
<pre><code class="lang-auto">from torch.autograd import Variable

img = torch.Tensor(3, 224, 224).normal_() # random image
img = torch.unsqueeze(img, 0)  # Add dimension 0 to tensor  
img_var = Variable(img) # assign it to a variable
features_var = model4(img_var) # get the output from the last hidden layer of the pretrained resnet
features = features_var.data # get
</code></pre>
<p>I get this error message:</p>
<p><code>RuntimeError: Given input size: (256x1x1). Calculated output size: (256x0x0). Output size is too small</code></p>
<p>This error doesn’t appear when I use the model as it is, without removing the last 3 layers ( I removed the last linear, the dropout and the relu)</p>
<p>The model4.eval() before the removal is :</p>
<pre><code class="lang-auto">
... other layers are here (isn't relevant)
...
(35): ReLU(inplace=True)
    (36): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
  )
  (linear0): Linear(in_features=25088, out_features=4096, bias=True)
  (relu0): ReLU(inplace=True)
  (dropout0): Dropout(p=0.5, inplace=False)
  (linear1): Linear(in_features=4096, out_features=4096, bias=True)
  (relu1): ReLU(inplace=True)
  (dropout1): Dropout(p=0.5, inplace=False)
  (last_linear): Linear(in_features=4096, out_features=1000, bias=True)
) 

</code></pre>
<p>After the removal :</p>
<pre><code class="lang-auto">...
...
(34): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (35): ReLU(inplace=True)
    (36): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
  )
  (2): Linear(in_features=25088, out_features=4096, bias=True)
  (3): ReLU(inplace=True)
  (4): Dropout(p=0.5, inplace=False)
  (5): Linear(in_features=4096, out_features=4096, bias=True)
) 
</code></pre>

ptrblck: <p>Wrapping a model into an <code>nn.Sequential</code> container might create some problems, if functional calls are using in the original <code>forward</code> method.<br>
In your use case, you are losing the <code>torch.flatten()</code> operation from <a href="https://github.com/pytorch/vision/blob/f9ef235c402f48a335293c626e17bd8504d3af87/torchvision/models/vgg.py#L45">this line of code</a>.<br>
I would recommend to write a custom model class, derive from vgg as the base class, and manipulate the <code>forward</code> method as you wish.</p>

tmc: <p>I have troubles writing the class with the forward method. can you help me pls</p>
<p>There’s this code:</p>
<pre><code class="lang-auto">import torch
import torch.nn as nn
from torchvision.models import vgg19

class Vgg19(torch.nn.Module):
    def __init__(self):
        super(Vgg19, self).__init__()
        features = list(vgg19(pretrained=True).features)
        self.features = nn.ModuleList(features).eval() 
        
    def forward(self, x):
        results = []
        for i,model in enumerate(self.features):
            x = model(x)
            if i in {len(self.features)-1}:
                results.append(x)
        return results

model = Vgg19()
</code></pre>
<p>but after I do model.eval()</p>
<p>I don’t get the last 2 linear layers, I only get this :</p>
<pre><code class="lang-auto">(27): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
    (28): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (29): ReLU(inplace=True)
    (30): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (31): ReLU(inplace=True)
    (32): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (33): ReLU(inplace=True)
    (34): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (35): ReLU(inplace=True)
    (36): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
  )
)
</code></pre>