MLNet-Pytorch icon indicating copy to clipboard operation
MLNet-Pytorch copied to clipboard

Missing key(s) in state_dict: "prior"

Open BarCodeReader opened this issue 6 years ago • 9 comments

when i load your model, it gives error:

RuntimeError: Error(s) in loading state_dict for MLNet: Missing key(s) in state_dict: "prior".

code i am using: PATH = "./mlnet/pretrain.model" model.load_state_dict(torch.load(PATH,map_location=torch.device('cpu')))

thanks in advance

BarCodeReader avatar Dec 03 '19 04:12 BarCodeReader

The problem is because in you network, you define the "Prior" as a nn.Parameter. for those who directly import the model and load the *.model file, I just change the nn.Parameter into Variable. Then everything is fine.

I think this should be ok since in the *.model file, there is no info stored about this 'prior'

@immortal3 please let me know if my change will hurt. thanks

BarCodeReader avatar Dec 03 '19 04:12 BarCodeReader

Loading Model Looks fine but I think this issue can happen because of the Pytorch version difference. I need to look into more resources for the difference between variables and parameters. I will update you on this.

immortal3 avatar Dec 03 '19 04:12 immortal3

@BarCodeReader I haven't gotten time to look at this. Is the issue resolved?

immortal3 avatar Dec 17 '19 18:12 immortal3

in your jupyter notebook

class MLNet(nn.Module):
    def __init__(self,prior_size):
        super(MLNet, self).__init__()
        # some code
        # prior initialized to ones
        **self.prior = nn.Parameter(torch.ones((1,1,prior_size[0],prior_size[1]), requires_grad=True))**

I just change this from nn.Parameter into nn.Variable and the model works. because in your pretrained file, there is no prior information stored..thus when we try to fit the dictionary into the model, this error will come.

so if you set the prior as nn.Parameter for some purpose, please let me know. Else I think my current modification works fine.

BarCodeReader avatar Dec 18 '19 03:12 BarCodeReader

Tried with both parameter and with variable, it didn't work. What version of pytorch have you used?

guri avatar Mar 07 '21 16:03 guri

this is quite old code (> 2 yrs). So, I don't remember the exact version of PyTorch, and also it seems that there is no requirement.txt, too. (horrible practice from my side).

If you have no hurry, I might be able to port this to the latest PyTorch version in the upcoming weekend. But, cannot promise you.

immortal3 avatar Mar 08 '21 04:03 immortal3

Thanks, that would be great!

I thought about either trying to run it on an older versions of pytorch (after figuring out which), or trying to fix the priors parameters/variables issue directly (currently doesn't work).

BTW, Does the '2018-10-04 09_20_57.364231_5_epochs_v1.model' mentioned in the comment is actually your trained model? if not, can I get the trained network in some other way?

guri avatar Mar 08 '21 06:03 guri

Gave it another try... actually Variables instead of parameters does seem to allow loading the model, I just forgot to import - from torch.autograd import Variable.

guri avatar Mar 08 '21 06:03 guri

Gave it another try... actually Variables instead of parameters does seem to allow loading the model, I just forgot to import - from torch.autograd import Variable.

Excellent! It solves all the problems! Thank you!

LJOVO avatar Mar 30 '21 11:03 LJOVO