Keras: ImportError: `save_model` requires h5py even thought the code already imported h5py

假如想象 提交于 2019-12-01 06:08:33

问题


I ran into some trouble when trying to save a Keras model:

Here is my code:

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')

But I got the following errors even though the code print '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.

回答1:


Make sure you use the latest version of Keras.

Also, this error has been reported here in the keras github: https://github.com/fchollet/keras/issues/3426

on linux:

sudo apt-get install libhdf5
sudo pip install h5py



回答2:


Have you tried directly installing h5py? http://docs.h5py.org/en/latest/build.html

Try running pip install h5py



来源:https://stackoverflow.com/questions/47422817/keras-importerror-save-model-requires-h5py-even-thought-the-code-already-imp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!