【问题标题】:Tensorflow session and graph contextTensorFlow 会话和图形上下文
【发布时间】:2017-11-02 11:03:37
【问题描述】:

我的问题是关于上下文和 TensorFlow 默认会话和图表。

问题: 在以下情况下,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(参见上文的回溯):您必须为占位符张量“InputPlaceHolder/TestInput”提供一个 dtype 浮点数

并不是说 TensorFlow 不能识别占位符。如果我将名称从“InputPlaceHolder/TestInput:0”更改为“InputPlaceHolder/TestInput:1”,我会收到一条消息,表明“InputPlaceHolder/TestInput”存在但只有1 个输出。这是有道理的,我猜会话在我的默认图表上运行。

只有当我保持相同的定义时,事情才对我有用: 如果我通过直接从第一个函数中运行注释部分(以 tf.Session() 作为 sess:) 来更改代码,则一切正常。

我想知道我错过了什么? 我的猜测是与上下文相关,可能没有将会话分配给图表?

【问题讨论】:

  • 尝试with tf.Session(graph=g) as sess: 并确保将正确的图表发送到会话
  • 感谢您的回复!我实际上尝试过它并没有解决问题。还尝试获取 tf 默认图并使用它,但没有运气。从外部函数运行会话时,我丢失了一些东西。
  • 似乎不太可能,需要从test函数返回g,然后将返回值发送给test_once函数。请尝试并回复
  • 谢谢。通过将图表 g 传递给 test_once 以及错误消息更新了上面的代码。

标签: tensorflow


【解决方案1】:

解决了。愚蠢的错误 test_once 调用 sess.run 两次。第二次,确实没有输入占位符......:summary.ParseFromString(sess.run(summary_op))

【讨论】:

    猜你喜欢
    • 2019-01-07
    • 1970-01-01
    • 2020-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-23
    • 2020-02-10
    • 1970-01-01
    相关资源
    最近更新 更多