尝试提供tensorflow模型时Flask上下文错误

时间:2018-04-27 08:15:10

标签: python flask

我有一个简单的烧瓶应用程序来提供张量流模型。据我所知,我已经解决了所有其他错误。但是,当运行应用程序时,我得到了这个错误:

RuntimeError: Working outside of request context.

Traceback (most recent call last):
  File "app.py", line 61, in <module>
    if request.args:
  File "/home/m0oN/.local/lib/python3.5/site-packages/werkzeug/local.py", 
line 347, in __getattr__
    return getattr(self._get_current_object(), name)
  File "/home/m0oN/.local/lib/python3.5/site-packages/werkzeug/local.py", 
line 306, in _get_current_object
    return self.__local()
  File "/home/m0oN/.local/lib/python3.5/site-packages/flask/globals.py", 
line 37, in _lookup_req_object
    raise RuntimeError(_request_ctx_err_msg)
RuntimeError: Working outside of request context.

This typically means that you attempted to use functionality that needed
an active HTTP request.  Consult the documentation on testing for
information about how to avoid this problem.

我认为主要问题在于:

if __name__ == '__main__':
    file_name = 'steph.jpg'
    model_file = 'thotornot_graph.pb'
    lable_file = 'retrained_labels.txt'
    input_height = 299
    input_width = 299
    input_mean = 128
    input_std = 128
    input_layer = "Mul"
    output_layer = "final_result"

    if request.args:
        file_name = request.args

    graph = load_graph(model_file)
    t = read_tensor_from_image_file(file_name,
                                input_height=input_height,
                                input_width=input_width,
                                input_mean=input_mean,
                                input_std=input_std)

    input_name = 'import/' + input_layer
    output_name = 'import/' + output_layer
    input_operation = graph.get_operation_by_name(input_name);
    output_operation = graph.get_operation_by_name(output_name);

    with tf.Session(graph=graph) as sess:
        start = time.time()
        results = sess.run(output_operation.outputs[0],
                        {input_operation.outputs[0]: t})
        end = time.time()
    results = np.squeeze(results)

    top_k = results.argsort()[-5:][::-1]
    lables = load_lables(lable_file)

    print('\nEvaluation time (1-image): {:.3f}s\n'.format(end-start))

    for i in top_k:
        print(lables[i], results[i])

app.run(localhost, 8081)

我的代码已满的链接在这里https://paste.ee/p/ztYKg

有没有人有解决方案?

1 个答案:

答案 0 :(得分:0)

导致错误的行:

if request.args:
    file_name = request.args

似乎没有任何目的。您没有为请求提供服务,为什么要尝试检索请求args? request是一个Flask帮助器,它将提供有关Web请求的信息,内部端点代码调用。它不会在其他任何地方工作。