tensorflow: h5py==3.0.0 causes issues with keras model loads in tensorflow 2.1.0
h5py released version 3.0.0 today and it causes this code to fail:
https://github.com/tensorflow/tensorflow/blob/master/tensorflow/python/keras/saving/hdf5_format.py#L182 with error:
File "/databricks/python/lib/python3.7/site-packages/tensorflow_core/python/keras/saving/save.py", line 146, in load_model
return hdf5_format.load_model_from_hdf5(filepath, custom_objects, compile)
File "/databricks/python/lib/python3.7/site-packages/tensorflow_core/python/keras/saving/hdf5_format.py", line 166, in load_model_from_hdf5
model_config = json.loads(model_config.decode('utf-8'))
AttributeError: 'str' object has no attribute 'decode'
It looks like in version 2.1.0 the h5py version is not pinned (it is pinned in master), which is causing the issue.
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Reactions: 53
- Comments: 55 (12 by maintainers)
Links to this issue
Commits related to this issue
- Fix h5py package version to 2.10. Addresses training and inference issues https://github.com/tensorflow/tensorflow/issues/44467 — committed to andreasbotsikas/ignite-learning-paths-training-aiml by rndazurescript 4 years ago
- h5py >3.x has tensorflow issue https://github.com/tensorflow/tensorflow/issues/44467 — committed to Open-EO/openeo-geopyspark-driver by jdries 4 years ago
- h5py >3.x has tensorflow issue https://github.com/tensorflow/tensorflow/issues/44467 — committed to Open-EO/openeo-geopyspark-driver by jdries 4 years ago
- Pin h5py < 3 h5py >= 3.0.0 causes model serialization issues with keras and tensorflow 1.x cf. https://github.com/tensorflow/tensorflow/issues/44467 — committed to CSBDeep/CSBDeep by uschmidt83 4 years ago
- require h5py < 3, qurator-spk/sbb_textline_detection#50, tensorflow/tensorflow#44467 — committed to qurator-spk/sbb_binarization by kba 4 years ago
- Pin h5py for older TensorFlow versions See https://github.com/tensorflow/tensorflow/issues/44467 — committed to larq/zoo by lgeiger 4 years ago
- Fix CI builds (#261) * :arrow_up: TensorFlow version on CI * Unpin official GitHub actions to reduce dependabot PRs * Pin h5py for older TensorFlow versions See https://github.com/tensorflow... — committed to larq/zoo by lgeiger 4 years ago
- Update tox.ini Added h5py < 3.0.0 dependency as work around fix. Ref: https://github.com/tensorflow/tensorflow/issues/44467 — committed to CoronaWhy/drug-lit-contradictory-claims by itsmemala 4 years ago
- Fix keras load_model causing exception With h5py 3.x causes AttributeError: 'str' object has no attribute 'decode' References https://github.com/tensorflow/tensorflow/issues/44467 — committed to jonnor/ESC-CNN-microcontroller by jonnor 4 years ago
- Pin h5py version (work around for https://github.com/tensorflow/tensorflow/issues/44467) — committed to janclemenslab/das by postpop 3 years ago
- Pin h5py version due to Tensorflow bug See https://github.com/tensorflow/tensorflow/issues/44467 — committed to openfoodfacts/robotoff by raphael0202 3 years ago
- pin h5py Better to pin h5py to 2.10.0 because of https://github.com/tensorflow/tensorflow/issues/44467 also with older version of tensorflow. — committed to cchwala/cnn_cml_wet-dry_example by cchwala 3 years ago
- add tensorflow and keras to env file pin tensorflow and h5py because tf2 is not yet on conda-forge and the h5py v3.x causes problems when used with keras and tensorflow, see https://github.com/tensor... — committed to cchwala/pycomlink by cchwala 3 years ago
- - pin the h5py version to < 3.0 (https://github.com/tensorflow/tensorflow/issues/44467) — committed to icbi-lab/nextNEOpi by riederd 4 years ago
- [Docker] Updated tensorflow/tflite version to 2.4.2 Tensorflow update required following update to cuda 11.0. Based on https://www.tensorflow.org/install/source#gpu, the 2.4 branch of tensorflow sho... — committed to Lunderberg/tvm by Lunderberg 3 years ago
- [Docker] Update tensorflow/tflite/xgboost versions (#8306) * [Docker] Updated tensorflow/tflite version to 2.4.2 Tensorflow update required following update to cuda 11.0. Based on https://www.te... — committed to apache/tvm by Lunderberg 3 years ago
- Bump modules and delete some conda/pip deps on Traverse Downgrade h5py for Keras model loading in TF 2.1.x https://github.com/tensorflow/tensorflow/issues/44467 — committed to PPPLDeepLearning/plasma-python by felker 3 years ago
- [Docker] Update tensorflow/tflite/xgboost versions (#8306) * [Docker] Updated tensorflow/tflite version to 2.4.2 Tensorflow update required following update to cuda 11.0. Based on https://www.te... — committed to ylc/tvm by Lunderberg 3 years ago
- meta.yaml: repin h5py Apparently https://github.com/tensorflow/tensorflow/issues/44467 will take a bit longer to really get sorted out and distributed. Sigh. — committed to regro-cf-autotick-bot/caiman-feedstock by pgunn 3 years ago
- Re-pin h5py, because tensorflow/tensorflow#44467 is a gift that keeps on giving — committed to flatironinstitute/CaImAn by pgunn 3 years ago
@bhack I’m not going to add any more messages after this, but I think you can see based on these other issues the Keras API is essentially broken because h5py new release. I’m not sure why the version was not pinned as it is in master, but I strongly advise that you pin to h5py==2.10.0 this for all TF >= 2.1.
We cannot pin the versions without doing a patch release. We only do patch releases for security issues.
This issue has a quick workaround:
pip install tensorflow h5py<3.0.0.We are in the process of releasing TF 2.4 which should not be affected by this issue.
FWIW, I got a similar error message with h5py 3.0, and removing
.decode('utf-8')fromtensorflow/python/keras/saving/hdf5_format.pyallows me to load Keras models as before.Just try pip install h5py==2.10.0
It completely worked in my case. Hope it will work for you.
Removing
.decode('utf-8')on lines 176 and 190 in/usr/lib/python3.9/site-packages/tensorflow/python/keras/saving/hdf5_format.pyworked for me.pip install 'h5py<3.0.0'That seems the case. I tried h5py 3.1.0, and the error
AttributeError: 'str' object has no attribute 'decode'still happens.Thought it might be pertinent to link to the h5py repo: This has been labeled as a bug in their 3.0.0 release (rather than a backwards incompatibility), and is slated to be fixed in an upcoming release: https://github.com/h5py/h5py/issues/1732
edit: Actually, there might be two separate but related issues here. The bug in the h5py thread occurs during the model-save codepath, but the bug in this TensorFlow thread is in the model-load codepath, which if I had to wildly guess might require a typecheck/cast on TensorFlow’s side
Downgrading to h5py worked some time ago. Now old h5py does not work and #40991 appears.
Posting here because Google search for this issue led me directly to this page. Presumably it will lead others here as well.
You get this same problem.
Solution was:
pip3 install h5py=2.10.0Why do I install TF 2.0.0 in March 2021, you ask? Because the book I am studying uses TF 2.0.0 and hard experience has taught me there can be subtle differences between TF minor versions that are hard to understand for TF noobs like me.
also incapable for tensorflow3.2.1
The recommended fix of downgrading the h5py version:
pip install 'h5py < 3.0.0'does not work on TensorFlow 2.3 because:
Please keep in mind that we don’t update older branches unless for security reasons. So, because our bounds on dependencies were too large (our mistake, fixed now, but see also #44654), you will have to manually downgrade
h5pyin your installation.You can do this in two ways:
Either you install with an upper bound, a la:
Or you install TF and then downgrade
h5py, a la:Alternatively, the issue should be fixed in nightly and in TF 2.4 RCs (in the sense that h5py is upper bounded to not get 3.0.0).
We will have people working on making TF work with
h5py >= 3in the future, but this will only land in TF 2.5 or later.of course, we cannot use tf3 right now unless you come from another universe
This error is also present in tensorflow 1.15: https://stackoverflow.com/questions/64795784/google-ai-platform-unexpected-error-when-loading-the-model-str-object-has-no
the new h5py version just came out today: https://pypi.org/project/h5py/#history
Pinning it to a version ❤️.0.0 (or at least throwing a warning) is a very lightweight change with no risk.
I am facing the same issue. Is there any solution came out to fix this bug:
File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/network.py", line 1424, in load_weights saving.load_weights_from_hdf5_group(f, self.layers) File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/saving/hdf5_format.py", line 711, in load_weights_from_hdf5_group original_keras_version = f.attrs['keras_version'].decode('utf8') AttributeError: 'str' object has no attribute 'decode' python-BaseExceptionHey guys, when Iam trying to downgrade the version of h5py I get the follwing error:
ERROR: Failed building wheel for h5py Running setup.py clean for h5py Failed to build h5py WARNING: Ignoring invalid distribution -ip (d:\tensorflow object detection\tfodcourse\tfod\lib\site-packages) Installing collected packages: h5py Attempting uninstall: h5py WARNING: Ignoring invalid distribution -ip (d:\tensorflow object detection\tfodcourse\tfod\lib\site-packages) Found existing installation: h5py 3.1.0 Uninstalling h5py-3.1.0: Successfully uninstalled h5py-3.1.0 Running setup.py install for h5py … error ERROR: Command errored out with exit status 1: command: ‘d:\tensorflow object detection\tfodcourse\tfod\scripts\python.exe’ -u -c ‘import io, os, sys, setuptools, tokenize; sys.argv[0] = "C:\Users\Arne’“'”‘s PC\AppData\Local\Temp\pip-install-8sioo065\h5py_f1f64cd207fd4e3fbbc73e1ddc4b40d2\setup.py"; file="C:\Users\Arne’“'”‘s PC\AppData\Local\Temp\pip-install-8sioo065\h5py_f1f64cd207fd4e3fbbc73e1ddc4b40d2\setup.py";f = getattr(tokenize, ‘"’“‘open’”’“‘, open)(file) if os.path.exists(file) else io.StringIO(’”‘“‘from setuptools import setup; setup()’”’“‘);code = f.read().replace(’”‘"’\r\n’“'”‘, ‘"’"’\n’“'”‘);f.close();exec(compile(code, file, ‘"’“‘exec’”’“‘))’ install --record ‘C:\Users\Arne’”'"‘s PC\AppData\Local\Temp\pip-record-prai98me\install-record.txt’ --single-version-externally-managed --compile --install-headers ‘d:\tensorflow object detection\tfodcourse\tfod\include\site\python3.9\h5py’ cwd: C:\Users\Arne’s PC\AppData\Local\Temp\pip-install-8sioo065\h5py_f1f64cd207fd4e3fbbc73e1ddc4b40d2\
Rolling back uninstall of h5py Moving to d:\tensorflow object detection\tfodcourse\tfod\lib\site-packages\h5py-3.1.0.dist-info
from D:\Tensorflow Object Detection\TFODCourse\tfod\Lib\site-packages~5py-3.1.0.dist-info Moving to d:\tensorflow object detection\tfodcourse\tfod\lib\site-packages\h5py
from D:\Tensorflow Object Detection\TFODCourse\tfod\Lib\site-packages~5py ERROR: Command errored out with exit status 1: ‘d:\tensorflow object detection\tfodcourse\tfod\scripts\python.exe’ -u -c ‘import io, os, sys, setuptools, tokenize; sys.argv[0] = "C:\Users\Arne’“'”‘s PC\AppData\Local\Temp\pip-install-8sioo065\h5py_f1f64cd207fd4e3fbbc73e1ddc4b40d2\setup.py"; file="C:\Users\Arne’“'”‘s PC\AppData\Local\Temp\pip-install-8sioo065\h5py_f1f64cd207fd4e3fbbc73e1ddc4b40d2\setup.py";f = getattr(tokenize, ‘"’“‘open’”’“‘, open)(file) if os.path.exists(file) else io.StringIO(’”‘“‘from setuptools import setup; setup()’”’“‘);code = f.read().replace(’”‘"’\r\n’“'”‘, ‘"’"’\n’“'”‘);f.close();exec(compile(code, file, ‘"’“‘exec’”’“‘))’ install --record ‘C:\Users\Arne’”'"‘s PC\AppData\Local\Temp\pip-record-prai98me\install-record.txt’ --single-version-externally-managed --compile --install-headers ‘d:\tensorflow object detection\tfodcourse\tfod\include\site\python3.9\h5py’ Check the logs for full command output. WARNING: Ignoring invalid distribution -ip (d:\tensorflow object detection\tfodcourse\tfod\lib\site-packages) WARNING: Ignoring invalid distribution -ip (d:\tensorflow object detection\tfodcourse\tfod\lib\site-packages)
Would be awesome if someone can help me! 😃
I think it will be hard to expect a backport on 2.1.0.