【问题标题】:Tensorflow Slim restore model and predictTensorflow Slim 恢复模型并预测
【发布时间】:2023-06-03 02:36:01
【问题描述】:

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

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

【问题讨论】:

    标签: model tensorflow prediction mnist tf-slim


    【解决方案1】:

    看看官方的 TF-Slim documentationwalkthrough

    【讨论】:

    • 链接已失效。这就是为什么最好的 SO 答案不只是没有解释的链接。
    • 更新了链接,很难解释所有细节,文档更好。
    【解决方案2】:

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

    #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 模型中恢复所有变量。

    【讨论】: