Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
595 views
in Technique[技术] by (71.8m points)

python - Pyinstaller created exe file can not load a keras nn model

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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!).

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

Then run pyinstaller against that file: pyinstaller myscript.spec.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...