2017-05-23 106 views
0

我目前正在尝试学习如何使用TF-Slim,并且我正在遵循本教程:https://github.com/mnuke/tf-slim-mnistTensorflow Slim恢复模型和预测

假设我已经在检查点中保存了训练有素的模型,那么现在如何使用该模型并应用它?就像在教程中,我该如何使用训练有素的MNIST模型,并提供一套新的MNIST图像,并打印预测结果?

回答

1

你可以尝试像一个工作流程:

#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会话中,让保护程序恢复会话中检查点模型的所有变量。