我有一个已保存的 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/