简体   繁体   English

加载SavedModel比加载tf.train.Saver检查点要慢很多

[英]Loading SavedModel is a lot slower than loading a tf.train.Saver checkpoint

I changed from tf.train.Saver to the SavedModel format which surprisingly means loading my model from disk is a lot slower (instead of a couple of seconds it takes minutes). 我从tf.train.Saver更改为SavedModel格式,这意味着从磁盘加载我的模型要慢得多(而不是几秒钟它需要几分钟)。 Why is this and what can I do to load the model faster? 为什么这样做以及如何更快地加载模型?

I used to do this: 我曾经这样做过:

# Save model
saver = tf.train.Saver()
save_path = saver.save(session, model_path)

# Load model
saver = tf.train.import_meta_graph(model_path + '.meta')
saver.restore(session, model_path)

But now I do this: 但现在我这样做:

# Save model
builder = tf.saved_model.builder.SavedModelBuilder(model_path)
builder.add_meta_graph_and_variables(session, [tf.saved_model.tag_constants.TRAINING])
builder.save()

# Load model
tf.saved_model.loader.load(session, [tf.saved_model.tag_constants.TRAINING], model_path)

I am by no ways an expert in Tensorflow, but if I had to take a guess as to why this is happening, I would say that: 我绝不是Tensorflow的专家,但如果我不得不猜测为什么会发生这种情况,我会说:

  • tf.train.Saver(), saves a complete meta-graph. tf.train.Saver(),保存完整的元图。 Therefore, all the information needed to perform any operations contained in your graph is already there. 因此,执行图表中包含的任何操作所需的所有信息都已存在。 All tensorflow needs to do to load the model, is insert the meta-graph into the default/current graph and you're good to go. 所有张量流都需要加载模型,将元图插入默认/当前图表,你就可以了。
  • The SavedModelBuilder() on the other hand, behind the scene creates a language agnostic representation of your operations and variables. 另一方面,SavedModelBuilder()在场景后面创建了一个与操作和变量无关的语言表示。 Which means that the loading method has to extract all the information, then recreate all the operation and variables from your previous graph, and insert them into the default/current graph. 这意味着加载方法必须提取所有信息,然后重新创建前一个图形中的所有操作和变量,并将它们插入到默认/当前图形中。

Depending on the size of your graph, recreating everything that it contained might take some time. 根据图表的大小,重新创建其中包含的所有内容可能需要一些时间。

Concerning the second question, as @JH said, if there are no reasons for you to use one strategy over the other, and time is of the essence, then just go with the fastest one. 关于第二个问题,正如@JH所说,如果没有理由让你使用一种策略而不是另一种策略,时间是至关重要的,那么就选择最快的。

what can I do to load the model faster? 我该怎么做才能更快地加载模型?

Switch back to tf.train.Saver , as your question shows no motivations for using SavedModelBuilder, and makes it clear that elapsed time matters to you. 切换回tf.train.Saver ,因为您的问题没有显示使用SavedModelBuilder的动机,并明确表示已用时间对您很重要。 Alternatively, an MCVE that reproduced the timing issue would allow others to collaborate with you on profiling, diagnosing, and fixing any perceived performance issue. 或者,再现时间问题的MCVE将允许其他人与您合作进行分析,诊断和修复任何感知性能问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM