Я пытаюсь обслуживать модель машинного обучения через API с использованием чертежей Flask, вот мой файл колбы __init__.py
from flask import Flask
def create_app(test_config=None):
app = Flask(__name__)
@app.route("/healthcheck")
def healthcheck() -> str:
return "OK"
# Registers the machine learning blueprint
from . import ml
app.register_blueprint(ml.bp)
return app
Файл ml.py
, который содержит схему для /ml
конечная точка
import numpy as np
from . import configuration as cfg
import tensorflow as tf
from flask import (
Blueprint, flash, request, url_for
)
bp = Blueprint("ml", __name__, url_prefix="/ml")
keras_model = None
graph = None
@bp.before_app_first_request
def load_model():
print("Loading keras model")
global keras_model
global graph
with open(cfg.config["model"]["path"], 'r') as model_file:
yaml_model = model_file.read()
keras_model = tf.keras.models.model_from_yaml(yaml_model)
graph = tf.get_default_graph()
keras_model.load_weights(cfg.config["model"]["weights"])
@bp.route('/predict', methods=['POST'])
def predict() -> str:
global graph
features = np.array([request.get_json()['features']])
print(features, len(features), features.shape)
with graph.as_default():
prediction = keras_model.predict(features)
print(prediction)
return "%.2f" % prediction
Я запускаю сервер с помощью сценария командной строки
#!/bin/bash
export FLASK_APP=src
export FLASK_ENV=development
flask run
И если я перехожу на localhost:5000/healthcheck
, я получаю ответ OK
, как и при запускеследующий локон
curl -X POST \
http://localhost:5000/ml/predict \
-H 'Cache-Control: no-cache' \
-H 'Content-Type: application/json' \
-d '{
"features" : [17.0, 0, 0, 12.0, 1, 0, 0]
}'
В первый раз я получаю ответ [[1.00]]
, если я запускаю его снова, я получаю следующую ошибку
tensorflow.python.framework.errors_impl.FailedPreconditionError:
Error while reading resource variable dense/kernel from
Container: localhost. This could mean that the variable was uninitialized.
Not found: Container localhost does not exist. (Could not find resource: localhost/dense/kernel)
[[{{node dense/MatMul/ReadVariableOp}}]]
Если я изменяю файл Blueprintсервер обнаружит изменения и обновит их, я могу снова вызвать API, и он вернет правильный результат для первого вызова, и я снова вернусь к ошибке.Почему это происходит?И почему только для звонков после первого?