-1

I have trained a machine learning model and saved it as an hdf5 file

model.save('landcover_100_epochs_RESNET_backbone_batch16.hdf5')

Now when I try to load the model in jupyter notebook, it works without errors

from keras.models import load_model

model = load_model("landcover_100_epochs_RESNET_backbone_batch16.hdf5", compile=False)

But when I write the same code in QGIS plugin's python file it gives the following error:

2022-11-10T18:47:20     WARNING    Traceback (most recent call last):
              File "C:/Users/hp/AppData/Roaming/QGIS/QGIS3\profiles\default/python/plugins\shpfilegenerator\ShpFileGenerator.py", line 204, in createSHP
              model = load_model(self.modelPath,compile=False)
              File "C:\PROGRA~1\QGIS3~1.4\apps\Python37\lib\site-packages\tensorflow\python\keras\saving\save.py", line 146, in load_model
              return hdf5_format.load_model_from_hdf5(filepath, custom_objects, compile)
              File "C:\PROGRA~1\QGIS3~1.4\apps\Python37\lib\site-packages\tensorflow\python\keras\saving\hdf5_format.py", line 168, in load_model_from_hdf5
              raise ImportError('`load_model` requires h5py.')
             ImportError: `load_model` requires h5py. 

Here modelPath stores the location where the model is saved. Can anyone please help me with this error?

desertnaut
  • 57,590
  • 26
  • 140
  • 166
Vanshika
  • 1
  • 6
  • 1
    you either do not have h5py installed or the native HDF5 library is not installed or cannot be loaded. And this is in the python interpreter that qgis uses. – Dr. Snoopy Nov 10 '22 at 14:21
  • @Dr.Snoopy I have h5py installed, (I installed it using pip install h5py), but still it doesn't work. Is there anything else that is needed to be installed or is there any step that I missed in the process? – Vanshika Nov 11 '22 at 11:18
  • 1
    Yes, this is why I mentioned the native HDF5 library too, as it is required by h5py. Try importing h5py in the qgis python environment. – Dr. Snoopy Nov 11 '22 at 11:54
  • And note that python packages are installed per-interpreter, you might have h5py installed in another interpreter, different than the one qgis is using. – Dr. Snoopy Nov 11 '22 at 11:55

1 Answers1

0

The thing is that load_model can only load h5 files You can try :

from keras.models import load_weights
model = load_weights("landcover_100_epochs_RESNET_backbone_batch16.hdf5", compile=False)
  • This does not solve the issue, problem is not having h5py – Dr. Snoopy Nov 10 '22 at 14:19
  • ImportError: `load_weights` requires h5py package when loading weights from HDF5. Try installing h5py. This error still comes up, despite the fact that I have h5py installed already – Vanshika Nov 11 '22 at 11:13