我的 python 脚本包括:
from keras.models import model_from_json
model = model_from_json(open("test.json").read())
model.load_weights("test.h5")
model.compile(loss="mean_squared_error", optimizer = "adam")
然后,我使用上述脚本中的 pyinstaller 创建了一个 exe 文件。 exe 文件无法加载保存的模型。对此有任何想法,我们将不胜感激。
最佳答案
如果你得到关于 h5py
子模块的错误,尝试使用 collect_submodules
function将它们全部添加到 hidden_imports
。
您可能注意到一个名为 myscript.spec
的文件是由 pyinstaller 生成的。在这个文件中是关于如何构建脚本的说明(它也只是一个 python 代码!)。
所以尝试像这样编辑这个 myscript.spec
:
from PyInstaller.utils.hooks import collect_submodules
hidden_imports = collect_submodules('h5py')
a = Analysis(['myscript.py'],
binaries=None,
datas=[],
hiddenimports=hidden_imports,
hookspath=[],
runtime_hooks=[],
excludes=[],
win_no_prefer_redirects=False,
win_private_assemblies=False,
cipher=None)
# ... rest of a file untouched
然后针对该文件运行 pyinstaller
:pyinstaller myscript.spec
。
关于python - Pyinstaller 创建的 exe 文件无法加载 keras nn 模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45830206/