pyinstaller创建的exe文件无法加载keras nn模型 [英] Pyinstaller created exe file can not load a keras nn model

查看:128
本文介绍了pyinstaller创建的exe文件无法加载keras nn模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的python脚本包括:

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

然后,我从上述脚本中使用pyinstaller创建了一个exe文件. exe文件无法加载保存的模型.任何想法,将不胜感激.

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.

推荐答案

如果您收到有关h5py子模块的错误,请尝试使用 collect_submodules函数,将它们全部添加到hidden_imports.

If you get errors about h5py submodules, try to use collect_submodules function to add them all to hidden_imports.

您可能已经注意到pyinstaller生成的名为myscript.spec的文件.该文件中包含有关如何构建脚本的说明(它也只是python代码!).

You probably noticed a file called myscript.spec generated by a pyinstaller. Inside this file is an instruction on how to build you script (and it's just a python code too!).

因此,请尝试像这样编辑myscript.spec:

So try to edit this myscript.spec like this:

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 myscript.spec运行pyinstaller.

这篇关于pyinstaller创建的exe文件无法加载keras nn模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆