恢复Tensorflow模型的方式差异

时间:2019-02-06 05:30:56

标签: python python-3.x tensorflow model

我已经看到并尝试了两种方法,但是不知道它有什么区别。这是我使用的两种方法:
方法1:

saver = tf.train.import_meta_graph(tf.train.latest_checkpoint(model_path)+".meta")

sess  = tf.Session()
sess.run(tf.global_variables_initializer())   
sess.run(tf.local_variables_initializer()) 
if(tf.train.checkpoint_exists(tf.train.latest_checkpoint(model_path))):
    saver.restore(sess, tf.train.latest_checkpoint(model_path))
    print(tf.train.latest_checkpoint(model_path) + "Session Loaded for Testing")    

方法2:

saver = tf.train.Saver()
sess  =tf.Session()
sess.run(tf.global_variables_initializer())    
if(tf.train.checkpoint_exists(tf.train.latest_checkpoint(model_path))):
        saver.restore(sess, tf.train.latest_checkpoint(model_path))
        print(tf.train.latest_checkpoint(model_path) + "Session Loaded for Testing")    

我想知道的是:

  

以上两种方法有什么区别?
  哪种方法是加载模型的最佳方法?

请让我知道您对此有何建议。

1 个答案:

答案 0 :(得分:2)

我会尽量简洁,这是我在这件事上的2美分。我将对您代码的重要行进行评论,以指出我的想法。

# Importing the meta graph is same as building the same graph from scratch
# creating the same variables, creating the same placeholders and ect.
# Basically you are only importing the graph definition
saver = tf.train.import_meta_graph(tf.train.latest_checkpoint(model_path)+".meta")

sess  = tf.Session()
# Absolutely no need to initialize the variables here. They will be initialized
# when the you restore the learned variables.
sess.run(tf.global_variables_initializer())   
sess.run(tf.local_variables_initializer()) 
if(tf.train.checkpoint_exists(tf.train.latest_checkpoint(model_path))):
    saver.restore(sess, tf.train.latest_checkpoint(model_path))
    print(tf.train.latest_checkpoint(model_path) + "Session Loaded for Testing")

第二种方法:

# You can't create a saver object like this, you will get an error "No variables to save", which is true.
# You haven't created any variables. The workaround for doing this is:
# saver = tf.train.Saver(defer_build=True) and then after building the graph
# ....Graph building code goes here....
# saver.build()
saver = tf.train.Saver()
sess = tf.Session()
# Absolutely no need to initialize the variables here. They will be initialized
# when the you restore the learned variables. 
sess.run(tf.global_variables_initializer())    
if(tf.train.checkpoint_exists(tf.train.latest_checkpoint(model_path))):
    saver.restore(sess, tf.train.latest_checkpoint(model_path))
    print(tf.train.latest_checkpoint(model_path) + "Session Loaded for Testing")

因此,第一种方法没有什么问题,但是第二种方法是完全错误的。不要误会我的意思,但是我都不喜欢他们两个。但是,这只是个人喜好。另一方面,我想做的是:

# Have a class that creates the model and instantiate an object of that class
my_trained_model = MyModel()
# This is basically the same as what you are doing with
# saver = tf.train.import_meta_graph(tf.train.latest_checkpoint(model_path)+".meta")
# Then, once I have the graph build, I will create a saver object
saver = tf.train.Saver()
# Then I will create a session
with tf.Session() as sess:
    # Restore the trained variables here
    saver.restore(sess, model_checkpoint_path)
    # Now I can do whatever I want with the my_trained_model object

我希望这会对您有所帮助。