1

I am trying to save a tensorflow keras model with this summary:

Model: "sequential_2" etc.

using the following command:

model.save('my_model.h5')

I got the following error (in Jupyter Notebook):

ImportError: `save_model` requires h5py.

So, I installed h5py, using conda install h5py After the installation, I got the version of h5py in the Jupyter Notebook(same place I am trying to save the model):

h5py.__version__
'2.8.0'

Still, I got the same error. Even though, I imported h5py manually.

import h5py
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
filename = 'model.h5' 
model.save(filename)

---------------------------------------------------------------------------
**ImportError**                               Traceback (most recent call last)
<ipython-input-54-9160eee81fe6> in <module>
      5 from tensorflow.keras import layers
      6 filename = 'model.h5'
----> 7 model.save(filename)

~/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/network.py in save(self, filepath, overwrite, include_optimizer, save_format, signatures)

~/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/keras/saving/save.py in save_model(model, filepath, overwrite, include_optimizer, save_format, signatures)

~/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/keras/saving/hdf5_format.py in save_model_to_hdf5(model, filepath, overwrite, include_optimizer)

**ImportError:** save_model requires h5py.
Frank TR
  • 11
  • 3
  • always put full error message (starting at word "Traceback") in question (not comment) as text (not screenshot). There are other useful information. – furas Mar 02 '20 at 03:56
  • maybe you have two Pythons installed and you installed `h5py` for one Python but you run code with other Python. Every Python has own folder with modules and often can't share with other version. – furas Mar 02 '20 at 03:57
  • Check what @furas have commented. Check Which version of python you are running tensorflow with? Is conda install installing for the same python version? Are you using a virtual environment? If yes, then you have to install packages in that environment. – rajesh Mar 02 '20 at 04:44
  • Thank you, I updated my question, I think this should answer the questions, I greatly appreciate any help. – Frank TR Mar 02 '20 at 05:57
  • 1
    sudo apt-get install libhdf5. see this https://stackoverflow.com/questions/47422817/keras-importerror-save-model-requires-h5py-even-thought-the-code-already-imp – rajesh Mar 02 '20 at 07:50
  • That did not fix the problem, actually, 'sudo apt-get' does not work (and apprently there is no online doc to say how to install it, I went through all stack overflow and others for several hours) and I ran brew install for hdf5, but still the same problem. – Frank TR Mar 02 '20 at 15:50

1 Answers1

0

Have you tried directly installing h5py?
If you have an existing Python installation (e.g. a python.org download, or the one that comes with your OS), then on Windows, macOS/OSX, and Linux on Intel computers, pre-built h5py wheels can be installed via pip from PyPI:

$ pip install h5py

In Windows:
If that fails you can try installing cython
pip install h5py
pip install cython

In Linux/Ubuntu:
If that fails then you may also need libhdf5 installing libhdf5 and then installing h5py
1. sudo pip install cython
2. sudo apt-get install libhdf5-dev
3. sudo pip install h5py

Note: If the above installation didn't work try uninstalling h5py and then reinstall it and restart the jupyter notebook or anaconda.

TF_Support
  • 1,794
  • 1
  • 7
  • 16