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

I have vgg19 pretrained model, and I have used this code :

model_name = 'vgg19' # could be fbresnet152 or inceptionresnetv2
model4 = pretrainedmodels.__dict__[model_name](num_classes=1000, pretrained='imagenet')

model4.eval()

Then I wanted to remove the last 3 layers so I did this :

model4=list(model4.children())[:-3]
model4=nn.Sequential(*model4)

Then when I feed the model this image with this code :

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

I get this error message:

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

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)

The model4.eval() before the removal is :


... 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)
) 

After the removal :

...
...
(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)
) 

Wrapping a model into an nn.Sequential container might create some problems, if functional calls are using in the original forward method.
In your use case, you are losing the torch.flatten() operation from this line of code.
I would recommend to write a custom model class, derive from vgg as the base class, and manipulate the forward method as you wish.

I have troubles writing the class with the forward method. can you help me pls

There’s this code:

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()

but after I do model.eval()

I don’t get the last 2 linear layers, I only get this :

(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)
  )
)