9

So I'm trying to load a model using:

learn = create_cnn(data, models.resnet50, lin_ftrs=[2048], metrics=accuracy) 
learn.clip_grad();
learn.load(f'{name}-stage-2.1')

But I get the following error

RuntimeError: Error(s) in loading state_dict for Sequential:
size mismatch for 1.8.weight: copying a param with shape torch.Size([5004, 2048]) from checkpoint, the shape in current model is torch.Size([4542, 2048]).
size mismatch for 1.8.bias: copying a param with shape torch.Size([5004]) from checkpoint, the shape in current model is torch.Size([4542]).

The only thing that is different thing is that I added a random validation split that wasn't there in the stage-2.1 model, when I remove the split and have no validation set as the stage-2.1 was trained all goes well.

Whats happening?

npostavs
  • 4,877
  • 1
  • 24
  • 43
M090009
  • 1,129
  • 11
  • 20

2 Answers2

4

Use cnn_learner method and latest Pytorch with latest FastAI. There was a breaking change and discontinuity so you suffer now.

The fastai website has many examples such as this one.

learn = cnn_learner(data, models.resnet50, metrics=accuracy)
prosti
  • 42,291
  • 14
  • 186
  • 151
2

Actually your torch.Size([5004, 2048]) from checkpoint, the shape in current model is torch.Size([4542, 2048]) you have to change it.