Keras:ImportError:`保存_model`需要h5py,即使代码已经导入了h5py

kxeu7u2r  于 2023-03-23  发布在  其他
关注(0)|答案(3)|浏览(458)

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

xpcnnkqh1#

请确保您使用最新版本的Keras。
此外,这个错误已经在Keras Github中报告:https://github.com/fchollet/keras/issues/3426
在Linux上:

sudo apt-get install libhdf5
sudo pip install h5py
bhmjp9jg

bhmjp9jg2#

有没有试过直接安装h5py?http://docs.h5py.org/en/latest/build.html
尝试运行pip install h5py

oiopk7p5

oiopk7p53#

h5格式的save_model()需要h5 py,无法导入h5 py

当h5 py和Keras版本不对齐时会发生此错误。我在使用时遇到了同样的问题:
Windows 11、Tensorflow 2.10、Keras 2.10和h5py 3.8
我必须做以下事情才能使它工作:

pip卸载h5 py

conda install -c conda-forge h5py=3.4**
如果您有最新版本的Tensorflow,您可以执行以下操作以将Keras和h5 py版本与最新版本对齐:

pip卸载h5 py
pip卸载keras
pip install keras

相关问题