Implementation of VGG16 on Pytorch giving size mismatch error
Snippet of my code implementation on PyTorch is:

model = models.vgg16(pretrained = False)
classifier = nn.Sequential(
        nn.Linear(25088, 128),
        nn.ReLU(True),
        nn.Dropout(),
        nn.Linear(128, 128),
        nn.ReLU(True),
        nn.Dropout(),
        nn.Linear(128, 20)
)
model.classifier = classifier


I'm feeding images of input size (60x60x3) and batch_size = 30.

When I run the code from Linux (Ubuntu) Terminal (with PyTorch Version:  1.0.0, Torchvision Version:  0.2.1) it gives me, the following error message:


  RuntimeError: size mismatch, m1: [30 x 512], m2: [25088 x 128]


While, when I run it from Spyder (Anaconda) on Windows (with PyTorch Version:  1.0.1, Torchvision Version:  0.2.2), it runs perfectly. 

Am I missing something or is this because of some version mismatch in Pytorch and Torchvision? Both, I'm running on Python 3.6. 
Please suggest.

[UPDATE: Mistakenly interchanged the version numbers for the error-case and error-free case. Thanks @Manoj Mohan for pointing it out]

It's probably the other way around. Things run perfectly on torchvision 0.2.2 and fails on torch vision 0.2.1. 

This change of using AdaptiveAvgPool2d that went into 0.2.2 is why you don't see the error. https://github.com/pytorch/vision/commit/83b2dfb2ebcd1b0694d46e3006ca96183c303706

&gt;&gt;&gt; import torch
&gt;&gt;&gt; model = models.vgg16(pretrained = False)
&gt;&gt;&gt; x = torch.randn(1,3,60,60) # random image
&gt;&gt;&gt; feat = model.features(x)
&gt;&gt;&gt; flat_feat = feat.view(feat.size(0), -1) # flatten
&gt;&gt;&gt; flat_feat.shape
torch.Size([1, 512])
&gt;&gt;&gt; model.classifier(flat_feat)


RuntimeError: size mismatch, m1: [1 x 512], m2: [25088 x 4096] at /pytorch/aten/src/TH/generic/THTensorMath.cpp:940

You see the error of size mismatch. After, adaptive average pooling, things work fine. 

&gt;&gt;&gt; import torch.nn.functional as F
&gt;&gt;&gt; avg = F.adaptive_avg_pool2d(feat, (7,7))
&gt;&gt;&gt; avg = avg.view(avg.size(0), -1)
&gt;&gt;&gt; output = model.classifier(avg)
&gt;&gt;&gt; output.shape
torch.Size([1, 1000])

