将巨大的Keras模型加载到Flask App

时间:2017-05-06 15:54:56

标签: python flask keras

我正在构建一个小型Flask应用程序,它在幕后使用卷积神经网络来预测用户上传的图像。如果我像这样加载它就可以了:

@app.route("/uploader", methods=["GET","POST"])
def get_image():
    if request.method == 'POST':
        f = request.files['file']
        sfname = 'static/'+str(secure_filename(f.filename))
        f.save(sfname)
        clf = catdog.classifier()
        return render_template('result.html', pred = clf.predict(sfname), imgpath = sfname)

但是,这需要在用户添加图像后加载分类器(clf)。这需要一段时间,因为它需要从pickle文件设置200+层神经网络的所有权重。

我想要做的是在应用程序生成时加载所有权重。为此,我尝试了这一点(删除了HTML模板/导入/应用启动的无关代码):

# put model into memory on spawn
clf = catdog.classifier()
# Initialize the app
app = flask.Flask(__name__)

@app.route("/uploader", methods=["GET","POST"])
def get_image():
    if request.method == 'POST':
        f = request.files['file']
        sfname = 'static/'+str(secure_filename(f.filename))
        f.save(sfname)
        return render_template('result.html', pred = clf.predict(sfname), imgpath = sfname)

当我这样做时,我得到了这个追溯(跳过顶部所有烧瓶特定的痕迹):

 File "/Users/zachariahmiller/Documents/Metis/test_area/flask_catdog/flask_backend.py", line 26, in get_image
    return render_template('result.html', pred = clf.predict(sfname), imgpath = sfname)
  File "/Users/zachariahmiller/Documents/Metis/test_area/flask_catdog/catdog.py", line 56, in predict
    prediction = self.model.predict(img_to_predict, batch_size=1, verbose=1)
  File "/Users/zachariahmiller/anaconda/lib/python2.7/site-packages/keras/engine/training.py", line 1569, in predict
    self._make_predict_function()
  File "/Users/zachariahmiller/anaconda/lib/python2.7/site-packages/keras/engine/training.py", line 1037, in _make_predict_function
    **kwargs)
  File "/Users/zachariahmiller/anaconda/lib/python2.7/site-packages/keras/backend/tensorflow_backend.py", line 2095, in function
    return Function(inputs, outputs, updates=updates)
  File "/Users/zachariahmiller/anaconda/lib/python2.7/site-packages/keras/backend/tensorflow_backend.py", line 2049, in __init__
    with tf.control_dependencies(self.outputs):
  File "/Users/zachariahmiller/anaconda/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 3583, in control_dependencies
    return get_default_graph().control_dependencies(control_inputs)
  File "/Users/zachariahmiller/anaconda/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 3314, in control_dependencies
    c = self.as_graph_element(c)
  File "/Users/zachariahmiller/anaconda/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 2405, in as_graph_element
    return self._as_graph_element_locked(obj, allow_tensor, allow_operation)
  File "/Users/zachariahmiller/anaconda/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 2484, in _as_graph_element_locked
    raise ValueError("Tensor %s is not an element of this graph." % obj)
ValueError: Tensor Tensor("dense_2/Softmax:0", shape=(?, 2), dtype=float32) is not an element of this graph.

我不确定为什么在特定调用之外加载分类器作为应用程序的全局对象会导致失败。它应该在记忆中,我已经看过其他人用SKLearn分类器做这个例子。关于为什么会导致此错误的任何想法?

2 个答案:

答案 0 :(得分:2)

尝试在烧瓶中设置debug=False

在许多张量流量保存/加载尝试失败后为我工作。

(感谢shafy @github) https://github.com/fchollet/keras/issues/2397#issuecomment-338659190

对我来说,它看起来像这样,在我的烧瓶应用程序的底部:

if __name__ == '__main__':
    app.run(debug=False)

答案 1 :(得分:1)

您好我遇到了同样的问题。

我以thread = True运行我的python服务器。删除它,让我的工作

app.run(host='0.0.0.0', port=5000, threaded=True)

---->

app.run(host='0.0.0.0', port=5000)

调试似乎不会影响我的任何事情