我是一名学生,正在尝试创建一个结合Keras ML模型的Flask应用。我已经使用python app.py
使代码在本地运行,但是当我将代码推送到Heroku时,它会出现以下错误:
2019-04-15T08:28:58.355650+00:00 app[web.1]: Traceback (most recent call last):
2019-04-15T08:28:58.355653+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.5/site-packages/flask/app.py", line 2292, in wsgi_app
2019-04-15T08:28:58.355654+00:00 app[web.1]: response = self.full_dispatch_request()
2019-04-15T08:28:58.355656+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.5/site-packages/flask/app.py", line 1815, in full_dispatch_request
2019-04-15T08:28:58.355658+00:00 app[web.1]: rv = self.handle_user_exception(e)
2019-04-15T08:28:58.355660+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.5/site-packages/flask/app.py", line 1718, in handle_user_exception
2019-04-15T08:28:58.355661+00:00 app[web.1]: reraise(exc_type, exc_value, tb)
2019-04-15T08:28:58.355663+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.5/site-packages/flask/_compat.py", line 35, in reraise
2019-04-15T08:28:58.355665+00:00 app[web.1]: raise value
2019-04-15T08:28:58.355667+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.5/site-packages/flask/app.py", line 1813, in full_dispatch_request
2019-04-15T08:28:58.355668+00:00 app[web.1]: rv = self.dispatch_request()
2019-04-15T08:28:58.355670+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.5/site-packages/flask/app.py", line 1799, in dispatch_request
2019-04-15T08:28:58.355672+00:00 app[web.1]: return self.view_functions[rule.endpoint](**req.view_args)
2019-04-15T08:28:58.355674+00:00 app[web.1]: File "app.py", line 62, in demo
2019-04-15T08:28:58.355675+00:00 app[web.1]: model = load_model('./model.h5')
2019-04-15T08:28:58.355677+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.5/site-packages/keras/engine/saving.py", line 249, in load_model
2019-04-15T08:28:58.355678+00:00 app[web.1]: f = h5py.File(filepath, mode='r')
2019-04-15T08:28:58.355680+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.5/site-packages/h5py/_hl/files.py", line 312, in __init__
2019-04-15T08:28:58.355682+00:00 app[web.1]: fid = make_fid(name, mode, userblock_size, fapl, swmr=swmr)
2019-04-15T08:28:58.355683+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.5/site-packages/h5py/_hl/files.py", line 142, in make_fid
2019-04-15T08:28:58.355685+00:00 app[web.1]: fid = h5f.open(name, flags, fapl=fapl)
2019-04-15T08:28:58.355687+00:00 app[web.1]: File "h5py/_objects.pyx", line 54, in h5py._objects.with_phil.wrapper
2019-04-15T08:28:58.355688+00:00 app[web.1]: File "h5py/_objects.pyx", line 55, in h5py._objects.with_phil.wrapper
2019-04-15T08:28:58.355690+00:00 app[web.1]: File "h5py/h5f.pyx", line 78, in h5py.h5f.open
2019-04-15T08:28:58.355697+00:00 app[web.1]: OSError: Unable to open file (file signature not found)
我的理解是我的Keras模型model.h5
的文件未正确加载。在解决了其他一些Github问题和Stack Overflow答案之后,我已经验证了git存储库中是否包含该文件,并通过从repo下载文件并在本地运行该文件来检查该文件是否未损坏,但是这些文件似乎没有是问题。如果有人对发生的事情有任何想法,并能指出正确的方向,我将不胜感激。
以下是我的其他一些文件供参考。
Procfile:
web: python app.py
requirements.txt:
gunicorn==19.9.0
requests==2.14.2
Flask==1.0.2
keras==2.2.2
numpy==1.15.2
pandas==0.23.4
Pillow==5.2.0
scipy==1.1.0
tensorflow==1.10.0
Werkzeug==0.14.1
h5py==2.8.0
还有我的app.py中的摘录:
if __name__ == '__main__':
port = int(os.environ.get('PORT', 5000))
app.run(host='0.0.0.0', port=port)