python - 使用 Flask 运行 keras 预测会出错

标签 python flask keras

我有一个已保存的 keras 模型,我正试图在使用 Flask 托管的服务器上对其进行预测。模型的输入维度为 12,输出维度为 8。 当我向服务器发出预测请求时,出现错误。

服务器.py

model_path = 'dom-loc.h5'
model = load_model(model_path)



@app.route('/api', methods=['POST', 'GET'])

def predict():
    data = request.get_json(force=True)
    location = model.predict_classes(np.array(data['dompath']))
    output = location[0]
    print('OUTPUT', output)
    return jsonify(output)


if __name__ == '__main__':
    app.run(port=5000, debug=True)

请求.py

url = 'http://localhost:5000/api'


r = requests.post(url, json={'dompath':[[2, 3, 5, 1, 3, 3, 1, 5, 6, 8, 4, 8]]})
print(r.json())

server.py 错误

ValueError: Tensor Tensor("dense_4/Sigmoid:0", shape=(?, 8), dtype=float32) is not an element of this graph.

request.py 错误

json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

最佳答案

在进行预测时,需要获取默认构建的图。您可以在以下代码片段的帮助下完成此操作。

graph = tf.get_default_graph()
with graph.as_default():
    #predict here

关于python - 使用 Flask 运行 keras 预测会出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56376943/

相关文章:

python - Flask-Ask:如何修复 'flask.debughelpers.FormDataRoutingRedirect' 错误?

python - 类型错误 : Input 'y' of 'Mul' Op has type float32 that does not match type float64 of argument 'x'

python - 如何处理 CNN 训练 Keras 的数千张图像

flask - 如果我使用应用程序工厂模式,如何在gunicorn中运行flask应用程序?

python - 如何训练项目序列分割模型

python - 使用 python meteor 查看数据的实时变化

python - 字典的键在添加时顺序不对?

python - Lucid 包使用 Tensorflow 包时出现问题

python - 在 python 中使用格式错误的 JSON 抓取页面

python - 如何使命令行参数对 Flask 路由可见?