keras 我无法在QGIS中使用机器学习模型

kknvjkwl  于 2022-11-13  发布在  其他
关注(0)|答案(1)|浏览(182)

我训练了一个机器学习模型并将其保存为hdf5文件

model.save('landcover_100_epochs_RESNET_backbone_batch16.hdf5')

现在,当我尝试在jupyter notebook中加载模型时,它可以正常工作,没有错误

from keras.models import load_model

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

但是当我在QGIS插件的python文件中编写相同的代码时,它会出现以下错误:

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.

这里的modelPath存储了模型保存的位置。有人能帮我解决这个错误吗?

n6lpvg4x

n6lpvg4x1#

问题是load_model只能加载h5文件您可以尝试:

from keras.models import load_weights
model = load_weights("landcover_100_epochs_RESNET_backbone_batch16.hdf5", compile=False)

相关问题