我一直在尝试使用 Python 创建一个机器学习应用程序。在这种情况下,我需要导入存储为存储库中的文件的预训练模型checkpoint-1.111.h5
。但是在将代码部署到 Heroku 时,我无法导入该模型。存储库已成功部署,但在打开应用程序时显示
内部服务器错误
的代码app.py
如下:
import re
from flask import Flask, jsonify, render_template, request
from keras.models import load_model
# Run this cell to mount your Google Drive.
#from google.colab import drive
from keras.preprocessing.text import Tokenizer
from keras.preprocessing.sequence import pad_sequences
app = Flask(__name__)
@app.route('/')
def index():
best_model = load_model('checkpoint-1.111.h5')
data_int_t = pad_sequences([[1, 72, 19, 38], [], [], [], []],padding='pre', maxlen=(30-5))
data_test = pad_sequences(data_int_t, padding='post', maxlen=(30))
y_prob = best_model.predict(data_test)
return str(x + y)
if __name__ == '__main__':
app.run()
打开应用程序时,我总是得到结果内部服务器错误。日志如下:
2019-07-02T17:38:32.982084+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/keras/utils/io_utils.py", line 186, in __init__
2019-07-02T17:38:32.982086+00:00 app[web.1]: self.data = h5py.File(path, mode=mode)
2019-07-02T17:38:32.982088+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/h5py/_hl/files.py", line 394, in __init__
2019-07-02T17:38:32.982090+00:00 app[web.1]: swmr=swmr)
2019-07-02T17:38:32.982092+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/h5py/_hl/files.py", line 170, in make_fid
2019-07-02T17:38:32.982094+00:00 app[web.1]: fid = h5f.open(name, flags, fapl=fapl)
2019-07-02T17:38:32.982097+00:00 app[web.1]: File "h5py/_objects.pyx", line 54, in h5py._objects.with_phil.wrapper
2019-07-02T17:38:32.982099+00:00 app[web.1]: File "h5py/_objects.pyx", line 55, in h5py._objects.with_phil.wrapper
2019-07-02T17:38:32.982101+00:00 app[web.1]: File "h5py/h5f.pyx", line 85, in h5py.h5f.open
2019-07-02T17:38:32.982110+00:00 app[web.1]: OSError: Unable to open file (file signature not found)
这两个文件app.py
都checkpoint-1.111.h5
存在于存储库的同一位置。但是checkpoint-1.111.h5
实际上是一个git-lfs
文件。但这是个问题吗?当我在 Google Colab 中使用相同的代码时,keras.models.load_model
完全可以加载文件。checkpoint-1.111.h5