immortal3 / MLNet-Pytorch

Implementation of A Deep Multi-Level Network for Saliency Prediction in Pytorch
Apache License 2.0
30 stars 6 forks source link

Missing key(s) in state_dict: "prior" #3

Open BarCodeReader opened 4 years ago

BarCodeReader commented 4 years ago

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 commented 4 years ago

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

immortal3 commented 4 years ago

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 commented 4 years ago

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

BarCodeReader commented 4 years ago

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.

guri commented 3 years ago

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

immortal3 commented 3 years ago

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.

guri commented 3 years ago

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 commented 3 years ago

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.

LJOVO commented 3 years ago

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!