使用Flask运行keras预测会出错 [英] Running keras predictions with Flask gives error

查看:110
本文介绍了使用Flask运行keras预测会出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a saved keras model that I'm trying to make predictions with on a server hosted using flask. The input dim of the model is 12 and the output dimension is 8. When I make a request to the server to make a prediction, I get an error.

server.py

server.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)

request.py

request.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错误

error for server.py

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

request.py的错误

error for request.py

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

推荐答案

在预测期间,您需要获取所构造的默认图形.您可以在以下代码段的帮助下完成该操作.

During prediction, you need to get the default graph that was constructed. You can do that with the help of the following snippet.

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

这篇关于使用Flask运行keras预测会出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆