运行烧瓶应用程序以解决Tensorflow模型问题

时间:2018-09-24 11:02:23

标签: python tensorflow nginx flask uwsgi

我遇到以下应用问题。

我在Nginx / uwsgi docker容器中有一个Flask应用程序,而我的app.py文件如下:

from flask import Flask
from keras.models import Model
from keras.models import model_from_yaml


# If load the model here here I get an error of type Tensor Tensor("dense_4/Softmax:0", shape=(?, 64), dtype=float32) is not an element of this graph.
proc = processor(0, 0)
global model

with open(model_path + model_name + '.yaml', 'r') as yaml_file:
    loaded_model_yaml = yaml_file.read()
    loaded_model = model_from_yaml(loaded_model_yaml)
    loaded_model.load_weights(model_path + model_name + '.h5')
model = loaded_model.compile(optimizer=model_optimizer, loss=model_loss_function, metrics=['acc'])


app = Flask(__name__)

@app.route("/extract")
def extract():

    model = proc.load_model()
    prediction = [np.argmax(l) for l in model.predict([data[0], data[1]])]
    return json.dumps(prediction)

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=80)

现在,如果在启动烧瓶应用程序之前加载模型,则会收到类型为Tensor Tensor("dense_4/Softmax:0", shape=(?, 64), dtype=float32) is not an element of this graph.的错误

是否有标准方法可以在烧瓶应用程序中一次一次加载经过训练的模型,并使该实例在您的应用程序上运行?

我尝试过:

  • 添加在app.run()之前调用的init()函数

  • 从其他文件(from source file import *)导入模型

  • 将模型作为全局变量加载。

任何想法建议将不胜感激。 谢谢。 PC。

编辑 仅供参考,因为问题在于张量流而不是建议的Flask,所以重复的标签不正确。 即使用的模型.__ make_predict_function() 然后我可以在路线内使用模型。

0 个答案:

没有答案
相关问题