使用TensorFlow从检查点文件恢复CIFAR10训练

3

在使用Tensorflow时,我试图使用一个检查点文件来恢复CIFAR10的训练。参考一些其他的文章,我尝试过tf.train.Saver().restore,但没有成功。有人可以告诉我如何继续吗?

Tensorflow CIFAR10代码片段:

def train():
  # methods to build graph from the cifar10_train.py
  global_step = tf.Variable(0, trainable=False)
  images, labels = cifar10.distorted_inputs()
  logits = cifar10.inference(images)
  loss = cifar10.loss(logits, labels)
  train_op = cifar10.train(loss, global_step)
  saver = tf.train.Saver(tf.all_variables())
  summary_op = tf.merge_all_summaries()

  init = tf.initialize_all_variables() 
  sess = tf.Session(config=tf.ConfigProto(log_device_placement=FLAGS.log_device_placement))
  sess.run(init)


  print("FLAGS.checkpoint_dir is %s" % FLAGS.checkpoint_dir)

  if FLAGS.checkpoint_dir is None:
    # Start the queue runners.
    tf.train.start_queue_runners(sess=sess)
    summary_writer = tf.train.SummaryWriter(FLAGS.train_dir, sess.graph)
  else:
    # restoring from the checkpoint file
    ckpt = tf.train.get_checkpoint_state(FLAGS.checkpoint_dir)
    tf.train.Saver().restore(sess, ckpt.model_checkpoint_path)

  # cur_step prints out well with the checkpointed variable value
  cur_step = sess.run(global_step);
  print("current step is %s" % cur_step)

  for step in xrange(cur_step, FLAGS.max_steps):
    start_time = time.time()
    # **It stucks at this call **
    _, loss_value = sess.run([train_op, loss])
    # below same as original
1个回答

2
问题似乎在于这行代码:
tf.train.start_queue_runners(sess=sess)

仅当FLAGS.checkpoint_dir为None时才执行此操作。如果您正在从检查点还原,则仍需要启动队列运行程序。

请注意,我建议您在创建tf.train.Saver后再启动队列运行程序(由于发布版本中存在竞争条件),因此更好的结构应该是:

if FLAGS.checkpoint_dir is not None:
  # restoring from the checkpoint file
  ckpt = tf.train.get_checkpoint_state(FLAGS.checkpoint_dir)
  tf.train.Saver().restore(sess, ckpt.model_checkpoint_path)

# Start the queue runners.
tf.train.start_queue_runners(sess=sess)

# ...

for step in xrange(cur_step, FLAGS.max_steps):
  start_time = time.time()
  _, loss_value = sess.run([train_op, loss])
  # ...

谢谢你的回答!它解决了问题。我以为queue_runner负责通过扭曲创建输入图像,但这不是必要的步骤,因为我从检查点文件中恢复。 - emerson

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接