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