Size mismatch error during VGG finetuning

I have been following the ants and bees transfer learning tutorial from the official PyTorch Docs ( I am trying to finetune a VGG19 model by changing the final layer to predict one of two classes. I am able to modify the last fc layer using the following code.

But I get an error when executing the train_model function. The error is “size mismatch at /opt/conda/conda-bld/pytorch_1513368888240/work/torch/lib/THC/generic/”. Any idea what the issue is ?

model_conv = torchvision.models.vgg19(pretrained=True)
for param in model_conv.parameters():
    param.requires_grad = False

model_conv = nn.Sequential(*list(model_conv.classifier.children())[:-1] +
                     [nn.Linear(in_features=4096, out_features=2)])
if use_gpu:
    model_conv = model_conv.cuda()

criterion = nn.CrossEntropyLoss()

optimizer_conv = optim.SGD(model_conv._modules['6'].parameters(), lr=0.001, momentum=0.9)
exp_lr_scheduler = lr_scheduler.StepLR(optimizer_conv, step_size=7, gamma=0.1)

model_conv = train_model(model_conv, criterion, optimizer_conv, exp_lr_scheduler, num_epochs=25)

Did you forget to flatten the output feature map of the final conv layer?

I did not flatten the output feature map. Why is it required ? And how do you do that ? Sorry am a newbie with Deep learning and PyTorch!

so you just want to change the last layer of vgg19 ?
you can do like this.

old_classifier = list(model_conv.classifier.children()) # get the classifier part alone
old_classifier.pop() # remove the last layer
old_classifier.append(nn.Linear(4096,2)) # add a new layer
model_conv.classifier = nn.Sequential(*old_classifier) # attach it to the original vgg model

It works now. Thanks a lot @ashwin.raju93 !