Python Tensorflow:.ckpt 文件和 .ckpt.meta 和 .ckpt.index 和 .pb 文件的关系是什么

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44516609/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 00:05:36  来源:igfitidea点击:

Tensorflow : What is the relationship between .ckpt file and .ckpt.meta and .ckpt.index , and .pb file

pythontensorflow

提问by Frank.Fan

I used saver=tf.train.Saver()to save the model that I trained, and I get three kinds of files named:

我曾经saver=tf.train.Saver()保存我训练过的模型,我得到了三种文件,命名为:

  • .ckpt.meta
  • .ckpt.index
  • .ckpt.data
  • .ckpt.meta
  • .ckpt.index
  • .ckpt.data

And a file called:

还有一个名为:

  • checkpoint
  • 检查站

What is the connection with the .ckptfile?

.ckpt文件有什么联系?

I saw someone saved model with only .ckpt file, I don't know how to make it. How can I save model as a .pb file?

我看到有人保存的模型只有 .ckpt 文件,我不知道如何制作。如何将模型保存为 .pb 文件?

回答by gdelab

  • the .ckpt file is the old version output of saver.save(sess), which is the equivalent of your .ckpt-data(see below)

  • the "checkpoint" file is only here to tell some TF functions which is the latest checkpoint file.

  • .ckpt-metacontains the metagraph, i.e. the structure of your computation graph, without the values of the variables (basically what you can see in tensorboard/graph).

  • .ckpt-datacontains the values for all the variables, without the structure. To restore a model in python, you'll usually use the meta and data files with (but you can also use the .pbfile):

    saver = tf.train.import_meta_graph(path_to_ckpt_meta)
    saver.restore(sess, path_to_ckpt_data)
    
  • I don't know exactly for .ckpt-index, I guess it's some kind of index needed internally to map the two previous files correctly. Anyway it's not really necessary usually, you can restore a model with only .ckpt-metaand .ckpt-data.

  • the .pbfile can save your whole graph (meta + data). To load and use (but not train) a graph in c++ you'll usually use it, created with freeze_graph, which creates the .pbfile from the meta and data. Be careful, (at least in previous TF versions and for some people) the py function provided by freeze_graphdid not work properly, so you'd have to use the script version. Tensorflow also provides a tf.train.Saver.to_proto()method, but I don't know what it does exactly.

  • .ckpt 文件是 的旧版本输出saver.save(sess),相当于您的.ckpt-data(见下文)

  • “checkpoint”文件只是在这里告诉一些TF函数哪个是最新的检查点文件。

  • .ckpt-meta包含元图,即计算图的结构,没有变量的值(基本上是你可以在张量板/图中看到的)。

  • .ckpt-data包含所有变量的值,没有结构。要在 python 中恢复模型,您通常会使用元和数据文件(但您也可以使用该.pb文件):

    saver = tf.train.import_meta_graph(path_to_ckpt_meta)
    saver.restore(sess, path_to_ckpt_data)
    
  • 我不确切知道 for .ckpt-index,我猜这是内部需要某种索引才能正确映射前两个文件。无论如何,通常并不是真的有必要,您可以仅使用.ckpt-meta和来恢复模型.ckpt-data

  • .pb文件可以保存您的整个图形(元 + 数据)。要在 C++ 中加载和使用(但不训练)图形,您通常会使用它,用 创build freeze_graph,它.pb从元和数据创建文件。请注意,(至少在以前的 TF 版本和某些人中)提供的 py 功能freeze_graph无法正常工作,因此您必须使用脚本版本。Tensorflow 也提供了一种tf.train.Saver.to_proto()方法,但我不知道它到底是做什么的。

There are a lot of questions here about how to save and restore a graph. See the answer herefor instance, but be careful that the two cited tutorials, though really helpful, are far from perfect, and a lot of people still seem to struggle to import a model in c++.

这里有很多关于如何保存和恢复图形的问题。例如,请参阅此处的答案,但请注意,这两个引用的教程虽然确实很有帮助,但远非完美,而且很多人似乎仍然难以在 C++ 中导入模型。

EDIT: it looks like you can also use the .ckpt files in c++ now,so I guess you don't necessarily need the .pb file any more.

编辑:看起来您现在也可以在 c++ 中使用 .ckpt 文件,所以我想您不一定需要 .pb 文件了。