【发布时间】:2017-12-27 19:48:51
【问题描述】:
我正在构建一个小型 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 分类器执行此操作的示例。关于为什么会导致此错误的任何想法?
【问题讨论】:
-
对我来说似乎是一些线程问题。在 Tensorflow 和 Flask 上使用 Keras 时,谷歌搜索会出现几个类似的问题。一种解决方案似乎是明确地处理正确的图表:tensorflow.org/versions/r0.11/api_docs/python/framework/…