Keras:ImportError:`save_model`需要h5py甚至认为代码已经导入了h5py

时间:2017-11-21 21:12:38

标签: keras h5py keras-2

在尝试保存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.

2 个答案:

答案 0 :(得分:5)

确保使用最新版本的Keras。

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

在linux上:

sudo apt-get install libhdf5
sudo pip install h5py

答案 1 :(得分:0)

您是否尝试过直接安装h5py? http://docs.h5py.org/en/latest/build.html

尝试运行pip install h5py