使用" slim.learning.train"恢复用于微调的张量流模型。

时间:2016-10-19 10:08:52

标签: tensorflow

在tensorflow中,使用slim.learning.train(TF 0.11),我想从检查点恢复模型并继续训练。该模型有一个成功的培训课程,我想微调它。但是,当我这样做时,TF会因错误而崩溃 Init operations did not make model ready.

我用以下方式进行培训:

tf.contrib.slim.learning.train(
    train_op,
    train_dir,
    log_every_n_steps=FLAGS.log_every_n_steps,
    graph=g,
    global_step=model.global_step,
    number_of_steps=FLAGS.number_of_steps,
    init_fn=model.init_fn,
    saver=model.saver,
    session_config=session_config)

我尝试了3种选择:

#1

关注this doc

model.init_fn = None

#2

with g.as_default():
    model_path = tf.train.latest_checkpoint(train_dir)
    if model_path:
        def restore_fn(sess):
            tf.logging.info(
                "Restoring SA&T variables from checkpoint file %s",
                restore_fn.model_path)
            model.saver.restore(sess, restore_fn.model_path)
        restore_fn.model_path = model_path
        model.init_fn = restore_fn
    else:
        model.init_fn = None

#3

with g.as_default():
    model_path = tf.train.latest_checkpoint(train_dir)
    if model_path:
        variables_to_restore = tf.contrib.slim.get_variables_to_restore()
        model.init_fn = tensorflow.contrib.framework.assign_from_checkpoint_fn(
            model_path, variables_to_restore)
    else:
        model.init_fn = None

1 个答案:

答案 0 :(得分:1)

问题解决了。这是因为在模型构建之后直接定义了saver(tf.train.Saver)。

相反,在列车操作定义之后定义它,解决了这个问题。