Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

problem with "keras_model" #19

Closed
Amirhk-dev opened this issue Aug 20, 2019 · 9 comments
Closed

problem with "keras_model" #19

Amirhk-dev opened this issue Aug 20, 2019 · 9 comments

Comments

@Amirhk-dev
Copy link

Thanks for providing the code!

I don't have any issue loading the models in pytorch (the code below),

import bagnets.pytorch
pytorch_model = bagnets.pytorch.bagnet17(pretrained=True)

But in keras the code below throws an error!

import bagnets.keras
keras_model = bagnets.keras.bagnet17()

Probably I'm doing something wrong.
I would appreciate your help.

The error is in the below:

keras_model = bagnets.keras.bagnet17()

Traceback (most recent call last):
File "", line 1, in
File "/home/kd/anaconda2/bin/py27_1/local/lib/python2.7/site-ackages/keras/engine/saving.py", line 419, in load_model
model = _deserialize_model(f, custom_objects, compile)
File "/home/kd/anaconda2/bin/py27_1/local/lib/python2.7/site-ackages/keras/engine/saving.py", line 225, in _deserialize_model
model = model_from_config(model_config, custom_objects=custom_objects)
File "/home/kd/anaconda2/bin/py27_1/local/lib/python2.7/site-ackages/keras/engine/saving.py", line 458, in model_from_config
return deserialize(config, custom_objects=custom_objects)
File "/home/kd/anaconda2/bin/py27_1/local/lib/python2.7/site-packages/keras/layers/init.py", line 55, in deserialize
printable_module_name='layer')
File "/home/kd/anaconda2/bin/py27_1/local/lib/python2.7/site-packages/keras/utils/generic_utils.py", line 145, in deserialize_keras_object
list(custom_objects.items())))
File "/home/kd/anaconda2/bin/py27_1/local/lib/python2.7/site-packages/keras/engine/network.py", line 1022, in from_config
process_layer(layer_data)
File "/home/kd/anaconda2/bin/py27_1/local/lib/python2.7/site-packages/keras/engine/network.py", line 1008, in process_layer
custom_objects=custom_objects)
File "/home/kd/anaconda2/bin/py27_1/local/lib/python2.7/site-packages/keras/layers/init.py", line 55, in deserialize
printable_module_name='layer')
File "/home/kd/anaconda2/bin/py27_1/local/lib/python2.7/site-packages/keras/utils/generic_utils.py", line 145, in deserialize_keras_object
list(custom_objects.items())))
File "/home/kd/anaconda2/bin/py27_1/local/lib/python2.7/site-packages/keras/layers/core.py", line 735, in from_config
function = func_load(config['function'], globs=globs)
File "/home/kd/anaconda2/bin/py27_1/local/lib/python2.7/site-packages/keras/utils/generic_utils.py", line 235, in func_load
code = marshal.loads(raw_code)
ValueError: bad marshal data (unknown type code)

@wielandbrendel
Copy link
Owner

That looks really odd. According to a quick search this might be related to a broken installation of the keras package (see e.g. https://stackoverflow.com/questions/30861493/how-to-fix-python-valueerrorbad-marshal-data). Please verify if your installation is correct. If that's the case please let me know what version of keras you are using.

@Amirhk-dev
Copy link
Author

Amirhk-dev commented Aug 20, 2019

After creating new virtual environment and installing keras (version: 2.2.4) and tensorflow (version:1.14.0) for python (version: 2.7) I get the same error!

Thanks a lot for providing the link.
I tried some of the solutions in the link that you provided but the problem persists.

Another issue that I see is the "keras.py" file in "bagnets" folder which probably needs to be renamed.

@wielandbrendel
Copy link
Owner

I investigated this in a bit more depth. With the latest tensorflow and keras in Python 3 everything works fine, but I can replicate the marshal error in Python 2.7. The issue seems to be related in the serialization of the internal model objects included in the h5 file, as marshal (which is a serialisation package) features different internal representations in the two Python versions (see keras-team/keras#7440 for an in-depth discussion). The right and only solution to this problem is to actually construct the model in the standard keras-way and to then load the weights into that model. In principle that shouldn't be too hard but it requires a bit of debugging. Alternatively, you can simply switch to Python 3.

@Amirhk-dev
Copy link
Author

Thanks a lot for the help.
As you suggested, I would access to the architecture by loading the model using pytorch and will create the same architecture in keras. After that I can load the weights to the model.

@wielandbrendel
Copy link
Owner

@AmirhosseinKardoost Sounds great! Would you mind contributing your code to this repo or to share it with me once you are done? That would be great!

@Amirhk-dev
Copy link
Author

Sure, I would be happy to contribute. I would write about it as soon as I'm done.

@Amirhk-dev
Copy link
Author

@wielandbrendel I need the features extracted from patches using your approach in my project. I could do this using PyTorch. If the performance of my method increase using these features I have to bring the model to Keras and load the corresponding weights. Therefore, It could probably take some time until I work on it (if the features improve the performance of my project).

@wielandbrendel
Copy link
Owner

@Amirhk-dev Thanks for letting me know! I might do it myself at some point but I am pretty busy at the moment. You can just let me know here once you actually start working on it. Thanks again!

@Amirhk-dev
Copy link
Author

@wielandbrendel Sure, I will inform you whenever I start working on it. Thanks for this cool project.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants