About the problems encountered in python and Flask (updated from time to time)

1. Tensor Tensor("dense_1/Softmax:0", shape=(?, 10), dtype=float32) is not an element of this graph

solve:

Reason: because Tensorflow's operating mechanism happens to conflict with the Web. When Tensorflow makes predictions on the backend, it imports the "graph" into the memory, and then calculates the graph to return the result. Normally, after the execution is completed, the program is Kill But because there is a Web service here, the calculation of the "graph" is not killed. In the second execution, the "graph" is imported into the calculation again. Because two identical "graphs" appear at the same time, the program It is not clear which element is which element, so this problem almost arises.

Solution one:

Add code: Pack the prediction program, use system commands to directly call this program every time you query, and get the returned result. The disadvantage is that the model has to be reloaded for each prediction, which consumes too much time.

Solution two:

At the time of initialization, load the model file and generate graph.

import tensorflow as tf
graph = tf.get_default_graph()
...
...
...
global graph
with graph.as_default():
    # 执行预测函数
    # 如:predictions = model.predict(img, batch_size = 1, verbose = 0)

Later, it was found that the program solved by solution 2 can run without error in the tensorflowCPU version, and it will appear when running in the tensorflowGPU version:

2. tensorflow.python.framework.errors_impl.FailedPreconditionError: Error while reading resource variable conv_dw_7/depthwise_kernel from Container: localhost. This could mean that the variable was uninitialized. Not found: Container localhost does not exist. (Could not find resource: localhost/conv_dw_7/depthwise_kernel)
     [[{ {node conv_dw_7/depthwise/ReadVariableOp}}]]

Solution:

global sess
global graph
sess = tf.Session()
graph = tf.get_default_graph()
# 在model加载前添加set_session
set_session(sess)

#加载模型

with graph.as_default():
    set_session(sess)
    # 预测

Note: The code sequence must not be changed, otherwise an error will still be reported. A day of blood and tears!

 

 

Guess you like

Origin blog.csdn.net/wwwangzai/article/details/102683059