Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fastai learner not loading

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?

like image 527
M090009 Avatar asked Feb 27 '19 20:02

M090009


People also ask

What is Fastai Python?

A GPU-optimized computer vision library which can be extended in pure Python. An optimizer which refactors out the common functionality of modern optimizers into two basic pieces, allowing optimization algorithms to be implemented in 4–5 lines of code.


2 Answers

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)
like image 127
prosti Avatar answered Sep 21 '22 19:09

prosti


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

like image 37
Hamdi KAPTAN Avatar answered Sep 20 '22 19:09

Hamdi KAPTAN