我的问题是关于上下文和TensorFlow默认会话和图表 .
The problem: 在以下情形中,Tensorflow无法提供占位符:函数 Test
定义图形 . 函数 Test_Once
定义了一个会话 . 当函数 Test
调用 Test_Once
- >进给失败 . 当我更改代码时,函数 Test
声明图形会话 - >一切正常 .
这是代码:
def test_once(g, saver, summary_writer, logits, images, summary_op):
"""Run a session once for a givven test image.
Args:
saver: Saver.
summary_writer: Summary writer.
logits:
summary_op: Summary op.
"""
with tf.Session(graph=g) as sess:
ckpt = tf.train.get_checkpoint_state(FLAGS.checkpoint_dir)
if ckpt and ckpt.model_checkpoint_path:
# Restores from checkpoint
saver.restore(sess, ckpt.model_checkpoint_path)
# extract global_step from it.
global_step = ckpt.model_checkpoint_path.split('/')[-1].split('-')[-1]
else:
print('No checkpoint file found')
return
images.astype(np.float32)
predictions = sess.run(logits, feed_dict={'InputPlaceHolder/TestInput:0':images})
summary = tf.Summary()
summary.ParseFromString(sess.run(summary_op))
summary_writer.add_summary(summary, global_step)
return (predictions)
def test():
"""Test LCPR with a test image"""
with tf.Graph().as_default() as g:
# Get image for testing
images, labels = lcpr.test_input()
# Build a Graph that computes the logits predictions from the
# inference model.
with tf.name_scope('InputPlaceHolder'):
test_image_placeholder = tf.placeholder(tf.float32, (None,None,None,3), 'TestInput')
# Display the training images in the visualizer.
# The 'max_outputs' default is 3. Not stated. (Max number of batch elements to generate images for.)
#tf.summary.image('input_images', test_image_placeholder)
with tf.name_scope('Inference'):
logits = lcpr.inference(test_image_placeholder)
# Restore the moving average version of the learned variables for eval.
variable_averages = tf.train.ExponentialMovingAverage(
lcpr.MOVING_AVERAGE_DECAY)
variables_to_restore = variable_averages.variables_to_restore()
saver = tf.train.Saver(variables_to_restore)
# Build the summary operation based on the TF collection of Summaries.
writer = tf.summary.FileWriter("/tmp/lcpr/test")
writer.add_graph(g)
summary_op = tf.summary.merge_all()
summary_writer = tf.summary.FileWriter(FLAGS.test_dir, g)
#Sadly, this will not work:
predictions = test_once(g, saver, summary_writer, logits, images, summary_op)
'''Alternative working option :
with tf.Session() as sess:
ckpt = tf.train.get_checkpoint_state(FLAGS.checkpoint_dir)
if ckpt and ckpt.model_checkpoint_path:
# Restores from checkpoint
saver.restore(sess, ckpt.model_checkpoint_path)
# Assuming model_checkpoint_path looks something like:
# /my-favorite-path/cifar10_train/model.ckpt-0,
# extract global_step from it.
global_step = ckpt.model_checkpoint_path.split('/')[-1].split('-')[-1]
else:
print('No checkpoint file found')
return
x = sess.run(logits, feed_dict={'InputPlaceHolder/TestInput:0':images})
print(x)
'''
上面的代码显示一个错误,占位符没有被输入:
InvalidArgumentError(请参见上面的回溯):您必须使用dtype float为占位符张量'InputPlaceHolder/TestInput'提供值
它's not that TensorFlow does not recognize the placeholder. If I change the name from ' InputPlaceHolder / TestInput:0 ' to ' InputPlaceHolder / TestInput:1 ' I receive a message calming that ' InputPlaceHolder / TestInput'存在但只有1个输出 . 这是有道理的,我想会话在我的默认图表上运行 .
Things only work for me if I stay within the same def: 如果我通过直接从第一个函数中运行注释部分(以tf.Session()作为sess :)开始更改代码全部起作用 .
我想知道我错过了什么?我的猜测是与上下文相关的,可能没有将会话分配给图表?谢谢你的帮忙 !
1 回答
解决了 . 愚蠢的错误test_once两次调用sess.run . 在第二次,确实没有占位符被喂... .... summary.ParseFromString(sess.run(summary_op))