Size mismatched when forward

Hi there,

I load the model from torchvision.models.vgg16_bn(pretrained=True).
When forward the image batch to extract the feature, it leads to RunTimeError: size mismatch at /opt/conda/conda-bld/pytorch_1503966894950/work/torch/lib/THC/generic/THCTensorMathBlas.cu:243

The shape of input is torch.Size([50, 3, 224, 224]).

So… what’s wrong with this?

2 Likes

Hi,

did you change something in your model? Maybe an output layer for finetuning to your specific problem?
I suppose the size mismatch error could be located in another layer besides the input layer.

This code sample works fine for me:

model = vgg16_bn()
x = torch.FloatTensor((10, 3, 224, 224))
x = Variable(x, volatile=True)
out = model(x)

Could you give a working code snipped throwing your error?

Sorry, I omitted to post my modification to the vggnet16.

The error is fixed by the following way .

model = torchvision.models.vgg16_bn(pretrained=True)
model.classifier = nn.Sequential(*(model.classifier[i] for i in
                                range(4)))

However, if I modify the pretrained VGGNET model in the following way, the error will occur.

 model = torchvision.models.vgg16_bn(pretrained=True)
 model_finetune = list(model.features)
 model_finetune.extend((list(model.classifier)[:4]))
 model_finetune = nn.Sequential(*model_finetune)

In my view (just for reference)
This is due to the Module (Sequential is a subclass of Module) . You can see the def __setattr__(self, name, value): in the nn.modules.module.py, it can not save the list element to the self._modules or self.parameters.

I was getting the same error while using resent with input image size other than 224X224. Pretrained resnet model has a FC layer of size 512. So associated pre-trained weight for this full connected layer is of size 512X1000. When we use existing implementation of resenet then it perform Avg pooling with kernel size 7X7 for generating input for this FC layer.

(torchvision/models/resnet.py line number 110)

        self.avgpool = nn.AvgPool2d(7, stride=1) #input size : 512X7X7 output size 512X1
        self.fc = nn.Linear(512 * block.expansion, num_classes) 
        # Perform X.W+b, here weight matrix is size of 512XNum_Classes

Since in my case input for the avgpool layer was not of size 512X7X7 (it was size of 512XNXM) hence output of avgpool layer was not 512X1 which result into failure in the matrix multiplication.

One way to fix it is using adaptive avg pooling layer which perform avg pooling in such a way that it produces any pre defined output size. As we are interested in getting output of size 1 so
self.avgpool = nn.AvgPool2d(7, stride=1) can be replaced with self.avgpool = nn.AdaptiveAvgPool2d(1).

There is all ready a PR request for it https://github.com/pytorch/vision/pull/155 but it is still in process.

6 Likes

I have added the adaptive avg pooling but error still remain the same. Please help?
RuntimeError: size mismatch, m1: [512 x 1], m2: [512 x 2] at /pytorch/aten/src/THC/generic/THCTensorMathBlas.cu:249

Detailed Error

Exception NameError: “global name ‘FileNotFoundError’ is not defined” in <bound method _DataLoaderIter.del of <torch.utils.data.dataloader._DataLoaderIter object at 0x7fcf3cf65990>> ignored

RuntimeError Traceback (most recent call last)
in ()
1 model_conv = train_model(model_conv, criterion, optimizer_ft, exp_lr_scheduler,
----> 2 num_epochs=25)

in train_model(model, criterion, optimizer, scheduler, num_epochs)
34 # print (inputs.shape)
35 # print (model)
—> 36 outputs = model(inputs)
37 _, preds = torch.max(outputs, 1)
38 # print (outputs)

/usr/local/lib/python2.7/dist-packages/torch/nn/modules/module.pyc in call(self, *input, **kwargs)
489 result = self._slow_forward(*input, **kwargs)
490 else:
–> 491 result = self.forward(*input, **kwargs)
492 for hook in self._forward_hooks.values():
493 hook_result = hook(self, input, result)

in forward(self, x)
62 # x = F.relu(F.max_pool2d(self.conv1(x), 2))
63 # x = F.relu(F.max_pool2d(self.conv2_drop(self.conv2(x)), 2))
—> 64 x = self.model_f(x)
65 # x = x.view(-1, 14336)
66 # x = F.relu(self.fc1(x))

/usr/local/lib/python2.7/dist-packages/torch/nn/modules/module.pyc in call(self, *input, **kwargs)
489 result = self._slow_forward(*input, **kwargs)
490 else:
–> 491 result = self.forward(*input, **kwargs)
492 for hook in self._forward_hooks.values():
493 hook_result = hook(self, input, result)

/usr/local/lib/python2.7/dist-packages/torch/nn/modules/container.pyc in forward(self, input)
89 def forward(self, input):
90 for module in self._modules.values():
—> 91 input = module(input)
92 return input
93

/usr/local/lib/python2.7/dist-packages/torch/nn/modules/module.pyc in call(self, *input, **kwargs)
489 result = self._slow_forward(*input, **kwargs)
490 else:
–> 491 result = self.forward(*input, **kwargs)
492 for hook in self._forward_hooks.values():
493 hook_result = hook(self, input, result)

/usr/local/lib/python2.7/dist-packages/torch/nn/modules/linear.pyc in forward(self, input)
53
54 def forward(self, input):
—> 55 return F.linear(input, self.weight, self.bias)
56
57 def extra_repr(self):

/usr/local/lib/python2.7/dist-packages/torch/nn/functional.pyc in linear(input, weight, bias)
992 return torch.addmm(bias, input, weight.t())
993
–> 994 output = input.matmul(weight.t())
995 if bias is not None:
996 output += bias

RuntimeError: size mismatch, m1: [512 x 1], m2: [512 x 2] at /pytorch/aten/src/THC/generic/THCTensorMathBlas.cu:249

Late to the discussion, but for future reference because it took me a while to figure this out:
Some of the models (Alexnet, VGG) will run into this error because they are expecting a certain image size. If you include:

transforms.Resize([224, 224])

in your transforms (224 specifically is what works for me) you will avoid this error. See: https://www.pyimagesearch.com/2017/03/20/imagenet-vggnet-resnet-inception-xception-keras/

hi
Could you help me how to use vgg16 pretrained for 7 classes in pytorch please? I write code like follow, but I received an error like that
" RuntimeError: size mismatch at c:\anaconda2\conda-bld\pytorch_1519496000060\work\torch\lib\thc\generic/THCTensorMathBlas.cu:247"

dset_classes_number = 7
model = models.vgg16(pretrained=True)
model.classifier = nn.Sequential(
nn.Dropout(),
nn.Linear(256 * 6 * 6, 4096),
nn.ReLU(inplace=True),
nn.Dropout(),
nn.Linear(4096, 4096),
nn.ReLU(inplace=True),
nn.Linear(4096, dset_classes_number),
)
criterion = nn.CrossEntropyLoss()
lr = 0.0002
optimizer = optim.Adam(model.parameters(), lr=0.0002)
scheduler = optim.lr_scheduler.StepLR(optimizer, step_size=2, gamma=0.95)