Tensorflow Slim 恢复模型和预测 [英] Tensorflow Slim restore model and predict
问题描述
我目前正在尝试学习如何使用 TF-Slim,我正在学习本教程:https://github.com/mnuke/tf-slim-mnist.
I'm currently trying to learn how to use TF-Slim and I'm following this tutorial: https://github.com/mnuke/tf-slim-mnist.
假设我已经在检查点中保存了一个经过训练的模型,我现在如何使用该模型并应用它?比如,在教程中,我如何使用我训练的 MNIST 模型并输入一组新的 MNIST 图像,并打印预测?
Assuming that I already have a trained model saved in a checkpoint, how do I now use that model and apply it? Like, in the tutorial how do I use my trained MNIST model and feed in a new set of MNIST images, and print the predictions?
推荐答案
您可以尝试以下工作流程:
You can try a workflow like:
#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 session中,让saver从session中的checkpoint模型中恢复所有变量.
Basically you have to get the right variables to be restored, and these variables must have names that match those found in your checkpoint model. Afterwards, pass the list of variables to be restored to a Saver, and then in a TF session, let the saver restore all the variables from a checkpoint model in the session.
这篇关于Tensorflow Slim 恢复模型和预测的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!