Tensorflow Slim 恢复模型和预测

Tensorflow Slim restore model and predict

我目前正在尝试学习如何使用 TF-Slim,我正在学习本教程:https://github.com/mnuke/tf-slim-mnist

假设我已经在检查点中保存了一个经过训练的模型,我现在如何使用该模型并应用它?比如,在本教程中,我如何使用经过训练的 MNIST 模型并输入一组新的 MNIST 图像,然后打印预测结果?

看看官方的TF-Slimdocumentation and the walkthrough

您可以尝试以下工作流程:

#obtain the checkpoint file
checkpoint_file= tf.train.latest_checkpoint("./log")

#Construct a model as such:
with slim.arg_scope(mobilenet_arg_scope(weight_decay=weight_decay)):
            logits, end_points = mobilenet(images, num_classes = dataset.num_classes, is_training = True, width_multiplier=width_multiplier)

#Obtain the trainable variables and a saver
variables_to_restore = slim.get_variables_to_restore()
saver = tf.train.Saver(variables_to_restore)

#Proceed to create your training optimizer and predictions monitoring, summaries etc.
...

#Finally when you're about to train your model in a session, restore the checkpoint model to your graph first:

with tf.Session() as sess:
    saver.restore(sess, checkpoint_file)
    #...Continue your training

基本上,您必须获得要恢复的正确变量,并且这些变量的名称必须与您在检查点模型中找到的名称相匹配。之后,将要恢复的变量列表传递给一个Saver,然后在一个TF会话中,让Saver从会话中的一个检查点模型中恢复所有变量。