我部署了一个keras模型,并通过flask API将测试数据发送到该模型。我有两个文件:
首先:My Flask应用程序:
# Let's startup the Flask application app = Flask(__name__) # Model reload from jSON: print('Load model...') json_file = open('models/model_temp.json', 'r') loaded_model_json = json_file.read() json_file.close() keras_model_loaded = model_from_json(loaded_model_json) print('Model loaded...') # Weights reloaded from .h5 inside the model print('Load weights...') keras_model_loaded.load_weights("models/Model_temp.h5") print('Weights loaded...') # URL that we'll use to make predictions using get and post @app.route('/predict',methods=['GET','POST']) def predict(): data = request.get_json(force=True) predict_request = [data["month"],data["day"],data["hour"]] predict_request = np.array(predict_request) predict_request = predict_request.reshape(1,-1) y_hat = keras_model_loaded.predict(predict_request, batch_size=1, verbose=1) return jsonify({'prediction': str(y_hat)}) if __name__ == "__main__": # Choose the port port = int(os.environ.get('PORT', 9000)) # Run locally app.run(host='127.0.0.1', port=port)
第二:文件Im用于将json数据发送到api端点:
response = rq.get('api url has been removed') data=response.json() currentDT = datetime.datetime.now() Month = currentDT.month Day = currentDT.day Hour = currentDT.hour url= "http://127.0.0.1:9000/predict" post_data = json.dumps({'month': month, 'day': day, 'hour': hour,}) r = rq.post(url,post_data)
我从Flask收到有关Tensorflow的回复:
ValueError:Tensor Tensor(“ dense_6 / BiasAdd:0”,shape =(?, 1),dtype = float32)不是此图的元素。
我的keras模型是一个简单的6密层模型,并且训练没有错误。
Flask使用多个线程。你遇到的问题是因为tensorflow模型未在同一线程中加载和使用。一种解决方法是强制tensorflow使用gloabl默认图。
加载模型后添加
global graph graph = tf.get_default_graph()
而在你的预测
with graph.as_default(): y_hat = keras_model_loaded.predict(predict_request, batch_size=1, verbose=1)