keras: ImportError even though module is imported
Anyone ever came across this bug:
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
<ipython-input-23-cbcfc17289ab> in <module>()
2 print('Done!')
3 model_loaded = get_custom_CNN()
----> 4 model_loaded.load_weights('mdl.h5')
/usr/local/lib/python2.7/dist-packages/keras/models.pyc in load_weights(self, filepath, by_name)
691 def load_weights(self, filepath, by_name=False):
692 if h5py is None:
--> 693 raise ImportError('`load_weights` requires h5py.')
694 f = h5py.File(filepath, mode='r')
695 if 'layer_names' not in f.attrs and 'model_weights' in f:
ImportError: `load_weights` requires h5py.
after running this:
import h5py # version 2.7.0
print('Done!')
model_loaded = get_custom_CNN() #this can really be any network that fits mdl.h5
model_loaded.load_weights('mdl.h5')
I even checked the exact condition (h5py is None
is False
) after seeing the actual code here:
https://github.com/fchollet/keras/blob/master/keras/models.py
Keras 2.0.2
, tf (1.0.0
) backend.
Tried:
- Checking for mismatching environments
- Importing h5py from scratch
- Some old GH tickets suggested installing cython so i did that
- Searching online
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Comments: 18 (1 by maintainers)
Had same issue. I reinstalled h5py using pip and restarted the system. It worked.
So the way that i managed to get Keras to run was to add a cell with the below in it.
import h5py as h5py
then just re-run the cells above it after that and it should work.
EDIT: This was tested with the below versions: keras.version ‘2.0.3’ tf.version ‘1.1.0-rc0’
This issue is happening because of different version of keras and h5py.
Steps to solve
Remove “h5py” if you already installed
pip uninstall h5py
Remove keras
pip uninstall keras
3 . Now, Install keras, This will install updated and same version of keras and h5py
pip install keras
For me I had to adjust keras.models.py file. (I am using keras 2).
replace line 24-27:
try: import h5py except ImportError: h5py = None
with just
import h5py
make sure you have h5py installed in your environment. Restart your jupyter notebook and should be good to go.
just install h5py using pipe install h5py
When I first got this error, I use pip installed h5py, and then I still got this error. After reading all the comments above, I just closed my spyder IDLE and anaconda and reopened, then it works.
I also has same issue. it worked after installing h5py using pip