Tensorflow Slim恢复模型并进行预测 [英] Tensorflow Slim restore model and predict

查看:319
本文介绍了Tensorflow Slim恢复模型并进行预测的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我当前正在尝试学习如何使用TF-Slim,并且正在关注本教程:

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会话中,让Saver从会话中的检查点模型还原所有变量.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆