【问题标题】:Keras: ImportError: `save_model` requires h5py even thought the code already imported h5pyKeras:ImportError:`save_model` 需要 h5py,即使代码已经导入了 h5py
【发布时间】:2017-11-21 21:12:37
【问题描述】:

我在尝试保存 Keras 模型时遇到了一些麻烦:

这是我的代码:

import h5py
from keras.models import load_model

try:
    import h5py
    print ('import fine')
except ImportError:
    h5py = None

left.save('left.h5')  # creates a HDF5 file 'my_model.h5'
left_load = load_model('left.h5')

但即使代码打印'import fine',我也收到以下错误:

import fine
---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-145-b641e79036fa> in <module>()
      8     h5py = None
      9 
---> 10 left.save('left.h5')  # creates a HDF5 file 'my_model.h5'

/usr/local/lib/python3.4/dist-packages/keras/engine/topology.py in save(self, filepath, overwrite, include_optimizer)
   2504         """
   2505         from ..models import save_model
-> 2506         save_model(self, filepath, overwrite, include_optimizer)
   2507 
   2508     def save_weights(self, filepath, overwrite=True):

/usr/local/lib/python3.4/dist-packages/keras/models.py in save_model(model, filepath, overwrite, include_optimizer)
     53 
     54     if h5py is None:
---> 55         raise ImportError('`save_model` requires h5py.')
     56 
     57     def get_json_type(obj):

ImportError: `save_model` requires h5py.

【问题讨论】:

  • 如果您在/usr/local/lib/python3.4/dist-packages/keras/models.py 中删除import h5py 周围的“try-except”,会出现什么错误消息?

标签: keras h5py keras-2


【解决方案1】:

确保您使用的是最新版本的 Keras。

此外,keras github 中已报告此错误: https://github.com/fchollet/keras/issues/3426

在 Linux 上:

sudo apt-get install libhdf5
sudo pip install h5py

【讨论】:

  • 就这么做了。没有区别。
  • 您不必导入它。您是否尝试使用 pip install keras --upgrade 更新 keras?
  • 执行“pip install keras --upgrade”后仍然出现同样的错误
  • 先做 sudo apt-get install libhdf5
  • Ubuntu 16.04:$ sudo apt-get install libhdf5 E: Paket libhdf5 kann nicht gefunden werden.
【解决方案2】:

你试过直接安装h5py吗? http://docs.h5py.org/en/latest/build.html

尝试运行 pip install h5py

【讨论】:

    猜你喜欢
    • 2020-05-23
    • 2019-10-07
    • 1970-01-01
    • 2019-03-13
    • 2023-02-04
    • 2021-05-18
    • 2018-08-04
    • 2018-02-13
    • 2021-11-09
    相关资源
    最近更新 更多