size mismatch for linear.weight and linear.bias in Torch model

Viewed 28

I am using the following code to load the model.

model.to(device)
checkpoint = torch.load("weights/vgg.pth")
if 'state_dict' in checkpoint:
    checkpoint = checkpoint['state_dict']
ckpt = {k.replace('module.', ''):v for k,v in checkpoint.items()}
model.load_state_dict(ckpt)

I am getting the error:

self.__class__.__name__, "\n\t".join(error_msgs)))
RuntimeError: Error(s) in loading state_dict for RepVGG:
  size mismatch for linear.weight: copying a param with shape torch.Size([1000, 1280]) from checkpoint, the shape in current model is torch.Size([8, 1280]).
  size mismatch for linear.bias: copying a param with shape torch.Size([1000]) from checkpoint, the shape in current model is torch.Size([8]).
1 Answers

It seems like your current model is configured to provide classification into 8 classes (num_class=8). However, the checkpoint you are loading is of a VGG model pre-trained on ImageNet that has 1000 classes. Therefore, there is a mismatch in the dimensions of the weights and bias in the last layer.

Related