部署到 Heroku 时,无法将已保存的“.h5”检查点文件加载到 python 应用程序中

Cannot load a saved '.h5' checkpoint file into a python app, when deploying into Heroku

我一直在尝试使用 Python 创建一个机器学习应用程序。在这种情况下,我需要导入存储为存储库中 checkpoint-1.111.h5 文件的预训练模型。但是在将代码部署到 Heroku 中时我无法导入该模型。存储库已成功部署,但在打开应用程序时显示

Internal Server Error

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.pycheckpoint-1.111.h5 都存在于存储库的同一位置。然而 checkpoint-1.111.h5 实际上是一个 git-lfs 文件。但这是一个问题吗?当我在 Google Colab 中使用相同的代码时,keras.models.load_model 完全可以加载 checkpoint-1.111.h5 文件。

the checkpoint-1.111.h5 is actually a git-lfs file

Heroku doesn't support Git LFS and unfortunately I don't know enough about TensorFlow or Keras to suggest an alternative solution. With its hard limit on slug size and ephermal filesystem Heroku 可能不适合此应用程序。