我正在尝试对先前受过训练的.pb文件进行推理,但是无法运行会话。我收到一个“ FailedPreconditionError”,并且谷歌搜索表示这是因为我需要运行全局变量初始化程序,但我已经这样做了,但仍然出现错误。我正在运行tf 1.09(制作protobuf文件的原因),因为否则会在加载文件时出错。
import tensorflow as tf
import numpy as np
model_filepath = "keras.pb"
graph = tf.Graph()
with tf.gfile.GFile(model_filepath, 'rb') as f:
graph_def = tf.GraphDef()
graph_def.ParseFromString(f.read())
with tf.Graph().as_default() as graph:
tf.import_graph_def(graph_def)
init = tf.global_variables_initializer()
y = graph.get_tensor_by_name("import/y/Softmax:0")
x = graph.get_tensor_by_name("import/x:0")
bbox = graph.get_tensor_by_name("import/bbox:0")
fd = { x : np.zeros((128, 128, 128, 1)),
bbox : np.zeros((128, 5))} # dummy data
sess = tf.Session(graph = graph)
sess.run(init, fd) # tried with and without feed dict, doesn't raise error
sess.run(y, feed_dict = fd) # throws error
我收到以下错误:
Traceback (most recent call last):
File "C:\Users\user\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\client\session.py", line 1322, in _do_call
return fn(*args)
File "C:\Users\user\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\client\session.py", line 1307, in _run_fn
options, feed_dict, fetch_list, target_list, run_metadata)
File "C:\Users\user\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\client\session.py", line 1409, in _call_tf_sessionrun
run_metadata)
tensorflow.python.framework.errors_impl.FailedPreconditionError: Attempting to use uninitialized value import/batch_normalization_13/moving_mean
[[Node: import/batch_normalization_13/moving_mean/read = Identity[T=DT_FLOAT, _class=["loc:@import/batch_normalization_13/cond/FusedBatchNorm/Switch_3"], _device="/job:localhost/replica:0/task:0/device:GPU:0"](import/batch_normalization_13/moving_mean)]]
[[Node: import/first_block/stochastic_downsampling2d_1/cond/cond/random_uniform_1/_33 = _Recv[client_terminated=false, recv_device="/job:localhost/replica:0/task:0/device:CPU:0", send_device="/job:localhost/replica:0/task:0/device:GPU:0", send_device_incarnation=1, tensor_name="edge_420_i..._uniform_1", tensor_type=DT_INT32, _device="/job:localhost/replica:0/task:0/device:CPU:0"]()]]
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\user\Desktop\project\reader.py", line 26, in <module>
sess.run(y, feed_dict = fd)
File "C:\Users\user\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\client\session.py", line 900, in run
run_metadata_ptr)
File "C:\Users\user\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\client\session.py", line 1135, in _run
feed_dict_tensor, options, run_metadata)
File "C:\Users\user\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\client\session.py", line 1316, in _do_run
run_metadata)
File "C:\Users\user\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\client\session.py", line 1335, in _do_call
raise type(e)(node_def, op, message)
tensorflow.python.framework.errors_impl.FailedPreconditionError: Attempting to use uninitialized value import/batch_normalization_13/moving_mean
[[Node: import/batch_normalization_13/moving_mean/read = Identity[T=DT_FLOAT, _class=["loc:@import/batch_normalization_13/cond/FusedBatchNorm/Switch_3"], _device="/job:localhost/replica:0/task:0/device:GPU:0"](import/batch_normalization_13/moving_mean)]]
[[Node: import/first_block/stochastic_downsampling2d_1/cond/cond/random_uniform_1/_33 = _Recv[client_terminated=false, recv_device="/job:localhost/replica:0/task:0/device:CPU:0", send_device="/job:localhost/replica:0/task:0/device:GPU:0", send_device_incarnation=1, tensor_name="edge_420_i..._uniform_1", tensor_type=DT_INT32, _device="/job:localhost/replica:0/task:0/device:CPU:0"]()]]
Caused by op 'import/batch_normalization_13/moving_mean/read', defined at:
File "<string>", line 1, in <module>
File "C:\Users\user\AppData\Local\Programs\Python\Python36\lib\idlelib\run.py", line 144, in main
ret = method(*args, **kwargs)
File "C:\Users\user\AppData\Local\Programs\Python\Python36\lib\idlelib\run.py", line 474, in runcode
exec(code, self.locals)
File "C:\Users\user\Desktop\project\reader.py", line 13, in <module>
tf.import_graph_def(graph_def)
File "C:\Users\user\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\util\deprecation.py", line 432, in new_func
return func(*args, **kwargs)
File "C:\Users\user\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\framework\importer.py", line 442, in import_graph_def
_ProcessNewOps(graph)
File "C:\Users\user\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\framework\importer.py", line 234, in _ProcessNewOps
for new_op in graph._add_new_tf_operations(compute_devices=False): # pylint: disable=protected-access
File "C:\Users\user\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\framework\ops.py", line 3563, in _add_new_tf_operations
for c_op in c_api_util.new_tf_operations(self)
File "C:\Users\user\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\framework\ops.py", line 3563, in <listcomp>
for c_op in c_api_util.new_tf_operations(self)
File "C:\Users\user\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\framework\ops.py", line 3450, in _create_op_from_tf_operation
ret = Operation(c_op, self)
File "C:\Users\user\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\framework\ops.py", line 1740, in __init__
self._traceback = self._graph._extract_stack() # pylint: disable=protected-access
FailedPreconditionError (see above for traceback): Attempting to use uninitialized value import/batch_normalization_13/moving_mean
[[Node: import/batch_normalization_13/moving_mean/read = Identity[T=DT_FLOAT, _class=["loc:@import/batch_normalization_13/cond/FusedBatchNorm/Switch_3"], _device="/job:localhost/replica:0/task:0/device:GPU:0"](import/batch_normalization_13/moving_mean)]]
[[Node: import/first_block/stochastic_downsampling2d_1/cond/cond/random_uniform_1/_33 = _Recv[client_terminated=false, recv_device="/job:localhost/replica:0/task:0/device:CPU:0", send_device="/job:localhost/replica:0/task:0/device:GPU:0", send_device_incarnation=1, tensor_name="edge_420_i..._uniform_1", tensor_type=DT_INT32, _device="/job:localhost/replica:0/task:0/device:CPU:0"]()]]
编辑: 我认为问题可能在于没有要初始化的全局变量。在我立即调用tf.global_variables_initializer()之前,tf.global_variables()是一个空列表(tf.local_variables()也是,fwiw)