Error while loading the densenet model

Hi ,

I’m having trouble while loading the densenet pretrained model. Did following steps to save and load:

save:
checkpoint = {‘state_dict’: model.state_dict()}

torch.save(checkpoint, ‘checkpoint_densenet.pth’)

load:
state_dict = torch.load(‘checkpoint_densenet.pth’)
model.load_state_dict(state_dict)

and got following error when running:

RuntimeError: Error(s) in loading state_dict for DenseNet:
Missing key(s) in state_dict: …
Unexpected key(s) in state_dict: “state_dict”.

Am i doing it right? or whats missing?

Regards
Ranjit

Can you instead try the following:

# saving the model
torch.save(model.state_dict(), 'checkpoint_densenet.pth')

and then load it:

# define the model
model = DenseNet() 

# load parameters of the model:
model.load_state_dict(torch.load('checkpoint_densenet.pth'))

That seems to working fine. Thanks for help