Sizes do not match

from PIL import Image
from torch.autograd import Variable
import torchvision
import torch.utils.data
from torchvision import transforms

dtype = torch.FloatTensor
loader = transforms.Compose([
transforms.ToTensor()
])
def Image_open(path):
image = Image.open(path)
image = Variable(loader(image))
image = image.unsqueeze(0)

return image.type(dtype)

model = torchvision.models.resnet18()
model.load_state_dict(torch.load(“resnet18.pkl”))

image = Image_open(“data/train/cat/cat.0.jpg”)
outputs = model(image)
_, pre = torch.max(outputs.data, 1)
print(torch.max(pre, 1))
classes = (‘cat’, ‘dog’)

print('Predicted: ‘, ’ ‘.join(’%5s’ % classes[pre[0][0]]))

Traceback (most recent call last):
File “/home/ctl/PycharmProjects/transfer_learning/model_test.py”, line 23, in
model.load_state_dict(torch.load(“resnet18.pkl”))
File “/home/ctl/anaconda3/lib/python3.5/site-packages/torch/nn/modules/module.py”, line 335, in load_state_dict
own_state[name].copy_(param)
RuntimeError: sizes do not match at /py/conda-bld/pytorch_1493670682084/work/torch/lib/THC/generic/THCTensorCopy.c:95

I use ResNet18 training, and my training set is 224 * 224 images, why will appear this error?

because resnet18.pkl is not the state_dict of resnet18 that you instantiated.