question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

"bad marshal data" when loading model that was saved with python 2.7 into python 3.4.

See original GitHub issue

Please make sure that the boxes below are checked before you submit your issue. If your issue is an implementation question, please ask your question on StackOverflow or join the Keras Slack channel and ask there instead of filing a GitHub issue.

Thank you!

  • Check that you are up-to-date with the master branch of Keras. You can update with: pip install git+git://github.com/fchollet/keras.git --upgrade --no-deps

  • If running on TensorFlow, check that you are up-to-date with the latest version. The installation instructions can be found here.

  • If running on Theano, check that you are up-to-date with the master branch of Theano. You can update with: pip install git+git://github.com/Theano/Theano.git --upgrade --no-deps

  • Provide a link to a GitHub Gist of a Python script that can reproduce your issue (or just copy the script here if it is short).

I trained a model using python 2.7, and now I need to load it using python 3.4. The model includes a simple Lambda layer. The simplified script below reproduces the error:

from keras.models import Model, load_model
from keras.layers import Input, Lambda
import sys

if sys.version_info < (3,4):
    inp = Input(shape=(28,28,1))
    x = Lambda(lambda x: x + 1)(inp)
    model = Model(inp, x)
    model.save('lambdamodel.hdf5')
else:
    model = load_model('lambdamodel.hdf5') # Error here.
    model.summary()

The model gets created and saved fine in a python2.7 virtualenv:

(py2keras) kzh@otter:tmp$ python --version
Python 2.7.12
(py2keras) kzh@otter:tmp$ pip list
DEPRECATION: The default format will switch to columns in the future. You can use --format=(legacy|columns) (or define a format=(legacy|columns) in your pip.conf under the [list] section) to disable this warning.
backports.weakref (1.0rc1)
bleach (1.5.0)
funcsigs (1.0.2)
h5py (2.7.0)
html5lib (0.9999999)
Keras (2.0.6)
Markdown (2.6.8)
mock (2.0.0)
numpy (1.13.1)
pbr (3.1.1)
pip (9.0.1)
protobuf (3.3.0)
PyYAML (3.12)
scipy (0.19.1)
setuptools (36.2.3)
six (1.10.0)
tensorflow (1.2.1)
Theano (0.9.0)
Werkzeug (0.12.2)
wheel (0.29.0)
(py2keras) kzh@otter:tmp$ python lambdabug.py 
Using TensorFlow backend.
Done

The error comes up when loading in a python3 virtualenv:

(py3keras) kzh@otter:tmp$ python --version
Python 3.5.2
(py3keras) kzh@otter:tmp$ pip list
backports.weakref (1.0rc1)
bleach (1.5.0)
h5py (2.7.0)
html5lib (0.9999999)
Keras (2.0.6)
Markdown (2.6.8)
numpy (1.13.1)
pip (9.0.1)
protobuf (3.3.0)
PyYAML (3.12)
scipy (0.19.1)
setuptools (36.2.3)
six (1.10.0)
tensorflow (1.2.1)
Theano (0.9.0)
Werkzeug (0.12.2)
wheel (0.29.0)
(py3keras) kzh@otter:tmp$ python lambdabug.py 
Using TensorFlow backend.
Traceback (most recent call last):
  File "lambdabug.py", line 11, in <module>
    model = load_model('lambdamodel.hdf5')
  File "/home/kzh/.envs/py3keras/lib/python3.5/site-packages/keras/models.py", line 233, in load_model
    model = model_from_config(model_config, custom_objects=custom_objects)
  File "/home/kzh/.envs/py3keras/lib/python3.5/site-packages/keras/models.py", line 307, in model_from_config
    return layer_module.deserialize(config, custom_objects=custom_objects)
  File "/home/kzh/.envs/py3keras/lib/python3.5/site-packages/keras/layers/__init__.py", line 54, in deserialize
    printable_module_name='layer')
  File "/home/kzh/.envs/py3keras/lib/python3.5/site-packages/keras/utils/generic_utils.py", line 139, in deserialize_keras_object
    list(custom_objects.items())))
  File "/home/kzh/.envs/py3keras/lib/python3.5/site-packages/keras/engine/topology.py", line 2476, in from_config
    process_layer(layer_data)
  File "/home/kzh/.envs/py3keras/lib/python3.5/site-packages/keras/engine/topology.py", line 2462, in process_layer
    custom_objects=custom_objects)
  File "/home/kzh/.envs/py3keras/lib/python3.5/site-packages/keras/layers/__init__.py", line 54, in deserialize
    printable_module_name='layer')
  File "/home/kzh/.envs/py3keras/lib/python3.5/site-packages/keras/utils/generic_utils.py", line 139, in deserialize_keras_object
    list(custom_objects.items())))
  File "/home/kzh/.envs/py3keras/lib/python3.5/site-packages/keras/layers/core.py", line 697, in from_config
    function = func_load(config['function'], globs=globs)
  File "/home/kzh/.envs/py3keras/lib/python3.5/site-packages/keras/utils/generic_utils.py", line 200, in func_load
    code = marshal.loads(code.encode('raw_unicode_escape'))
ValueError: bad marshal data (unknown type code)

I’ve never used marshal directly myself and don’t have time to dig much further into this. In the meantime I’ll keep using python2.7 for the code I was planning to move to 3.4. Any tips or fixes are appreciated.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:39 (1 by maintainers)

github_iconTop GitHub Comments

45reactions
MyadaRoshdicommented, Dec 4, 2017

Guys, it is solved when I updated both Keras and tensorflow to the latest versions pip install --upgrade tensorflow pip install --upgrade keras

39reactions
alexklibiszcommented, Aug 8, 2017

One (somewhat hacky) fix is the following: if you can recreate the architecture (i.e. you have the original code used to generate it), you can instantiate the model from that code and then use model.load_weights('your_model_file.hdf5') to load in the weights. This works for me but it isn’t an option if you don’t have the code used to create the original architecture.

Read more comments on GitHub >

github_iconTop Results From Across the Web

tensorflow load data: bad marshal data - python - Stack Overflow
Solution is to use the same Python Version using which the Model has been Built and Saved . Use the same version of...
Read more >
"bad marshal data" when loading model that was saved with ...
"bad marshal data" when loading model that was saved with python 2.7 into python 3.4.
Read more >
keras加载模型错误:“bad marshal data“ - CSDN博客
Solution is to use the same Python Version using which the Model has been Built and Saved . 使用相同的框架环境。Use the same version of...
Read more >
R kernels' tensorflow environments use python 2.7 ... - Kaggle
This sometimes causes issues when trying to load pretrained models in a kernel, because Python 3 serialization method marshal is not backwards compatible...
Read more >
What's New In Python 3.4 — Python 3.11.1 documentation
On other platforms, the system wide unversioned pip command typically refers to the separately installed Python 2 version. The pyvenv command line utility...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found