2017-05-06 14 views
1

我正在構建一個小燒瓶應用程序,它使用幕後的卷積神經網絡來對用戶上傳的圖像進行預測。它的工作原理,如果我加載它是這樣的:將巨大的Keras模型加載到燒瓶應用程序中

@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分類器來做這件事的例子。任何想法爲什麼這會導致此錯誤?

+0

看起來有些線程問題給我。谷歌在Tensorflow和Flask上與Keras合作時出現了幾個類似的問題。一種解決方案似乎在正確的圖表中明確的工作:https://www.tensorflow.org/versions/r0.11/api_docs/python/framework/utility_functions#get_default_graph –

回答

0

您好,我有同樣的問題。

我正在運行我的python服務器爲threaded = True。刪除此,讓我的工作

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

---->

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

調試似乎並沒有影響到任何東西,我

相關問題