我正在读一篇Keras blog教学如何使用 Flask 创建简单的图像分类器 Restful API。我想知道如何在不使用 python 的其他 Web 框架中实现加载模型的相同方法。 在下面的代码中,模型在服务器启动之前加载到内存中,并运行直到服务器处于事件状态:
# if this is the main thread of execution first load the model and
# then start the server
if __name__ == "__main__":
print(("* Loading Keras model and Flask starting server..."
"please wait until server has fully started"))
load_model()
app.run()
我熟悉Pickle我知道如何在其他 Web 框架(例如 Node.js 的 python-shell)中运行 python 代码。 pickled 模型构建一次,每次需要时都可以加载。但我希望实现与教程建议的相同的目标,即仅加载一次,并多次使用它。创建一个单独的 python 服务器应用程序来为 Node.js 请求提供加载的模型是一个好主意吗?
最佳答案
您可以使用 load_model
在 Keras 中加载模型并传入路径:
from keras.models import load_model
model = load_model('model.hd5')
我创建了一个加载 Keras 模型的 Flask API,如果有帮助,您可以看一下: https://github.com/Ares513/DetectingTrollsApi/blob/master/api.py
关于python - 如何将 Keras 模型加载到内存中并在需要时使用它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52937256/