Pyinstaller created exe file can not load a keras nn model

时间:2017-08-23 03:34:32

标签: python keras pyinstaller

My python scrip includes:

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")

Then, I created an exe file using pyinstaller from aforementioned script. The exe file can not load the saved model. Any thought on that would be appreciated.

2 个答案:

答案 0 :(得分:2)

如果您收到有关h5py子模块的错误,请尝试使用collect_submodules function将其全部添加到hidden_imports

您可能已经注意到由pyinstaller生成的名为myscript.spec的文件。在这个文件里面是关于如何构建脚本的指令(它也只是一个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

然后针对该文件运行pyinstallerpyinstaller myscript.spec

答案 1 :(得分:1)

这解决了错误:

pyinstaller -w --hidden-import = h5py.defs --hidden-import = h5py.utils --hidden-import = h5py.h5ac --hidden-import = h5py._proxy myscript.py