ProGamerGov / neural-style-pt

PyTorch implementation of neural style transfer algorithm
MIT License
838 stars 175 forks source link

Error: `Missing Keys` when loading VGG weights #5

Closed rrmina closed 5 years ago

rrmina commented 5 years ago

System Environment OS: Ubuntu 18.04 Python Version: 3.6.7 PyTorch Version: 0.4.1

Attached is a screenshot of the error message when loading VGG weights.

screenshot from 2018-12-18 11-49-24

ajhool commented 5 years ago

Which VGG model are you using?

rrmina commented 5 years ago

@ajhool Hi! I got it here https://github.com/jcjohnson/pytorch-vgg

It's also the default argument for model_file

rrmina commented 5 years ago

By the way, here's a relevant issue

https://github.com/jcjohnson/pytorch-vgg/issues/5

and a fix (not just a workaround)

https://github.com/jcjohnson/pytorch-vgg/issues/3

ajhool commented 5 years ago

My understanding of that issue and the fix is that it needs to be executed before creating / in order to create the model, is that right?

ProGamerGov commented 5 years ago

I have the fix being executed when the model is downloaded: https://github.com/ProGamerGov/neural-style-pt/blob/master/models/download_models.py, so you shouldn't need to perform that fix yourself.

ProGamerGov commented 5 years ago

@iamRusty If you download the models directly from Justin Johnson's repository, you'll get these errors:

With the VGG-19 version directly downloaded from pytorch-vgg:

ubuntu@ip-Address:~/neural-style-pt$ python neural_style.py -backend cudnn -model_file vgg19-d01eb7cb.pth
VGG-19 Architecture Detected
Traceback (most recent call last):
  File "neural_style.py", line 409, in <module>
    main()
  File "neural_style.py", line 56, in main
    cnn, layerList = loadCaffemodel(params.model_file, params.pooling, params.gpu)
  File "/home/ubuntu/neural-style-pt/CaffeLoader.py", line 136, in loadCaffemodel
    cnn.load_state_dict(torch.load(model_file))
  File "/usr/local/lib/python2.7/dist-packages/torch/nn/modules/module.py", line 769, in load_state_dict
    self.__class__.__name__, "\n\t".join(error_msgs)))
RuntimeError: Error(s) in loading state_dict for VGG:
        Missing key(s) in state_dict: "classifier.0.bias", "classifier.0.weight", "classifier.3.bias", "classifier.3.weight".
        Unexpected key(s) in state_dict: "classifier.1.weight", "classifier.1.bias", "classifier.4.weight", "classifier.4.bias".
ubuntu@ip-Address:~/neural-style-pt$

With the VGG-16 version directly downloaded from pytorch-vgg:

ubuntu@ip-Address:~/neural-style-pt$ python neural_style.py -backend cudnn -model_file vgg16-00b39a1b.pth
VGG-16 Architecture Detected
Traceback (most recent call last):
  File "neural_style.py", line 409, in <module>
    main()
  File "neural_style.py", line 56, in main
    cnn, layerList = loadCaffemodel(params.model_file, params.pooling, params.gpu)
  File "/home/ubuntu/neural-style-pt/CaffeLoader.py", line 136, in loadCaffemodel
    cnn.load_state_dict(torch.load(model_file))
  File "/usr/local/lib/python2.7/dist-packages/torch/nn/modules/module.py", line 769, in load_state_dict
    self.__class__.__name__, "\n\t".join(error_msgs)))
RuntimeError: Error(s) in loading state_dict for VGG:
        Missing key(s) in state_dict: "classifier.0.bias", "classifier.0.weight", "classifier.3.bias", "classifier.3.weight".
        Unexpected key(s) in state_dict: "classifier.1.weight", "classifier.1.bias", "classifier.4.weight", "classifier.4.bias".
ubuntu@ip-Address:~/neural-style-pt$

If you want to directly download theses models without my download script, you'll have to perform the fixes yourself. The fixes are required because of a mix up in how PyTorch originally defined VGG models.

rrmina commented 5 years ago

@ProGamerGov You are right. I downloaded the models directly. I didn't use download_models.py (actually I never saw it until now). Thanks for the fix!

ProGamerGov commented 5 years ago

Because this issue appears to get a constant amount of traffic, I have added a new parameter called -disable_check into the pip package and multi-gpu branch (which hopefully will soon be merged into the master branch.

https://github.com/ProGamerGov/neural-style-pt/commit/5cd5c46ce3ada57f5b37106ad733e429bb9543ab https://github.com/ProGamerGov/neural-style-pt/commit/2035ea982126d20bc0744cd72d6fd55c18826f89

Though, you should really try to use the fixed models from the download script, or the models that I have converted here: https://github.com/ProGamerGov/neural-style-pt/wiki/Other-Models, unless you know what you are doing.

ProGamerGov commented 4 years ago

If try to use a model that should work and the layers are in the correct order, but the weight and bias names are different, then you can use this script to correct the names: https://gist.github.com/ProGamerGov/1bc833a8ae91f81e7e40037d052f8193