当前位置: 首页 > news >正文

TensorFlow 生成 .ckpt 和 .pb

原文:https://www.cnblogs.com/nowornever-L/p/6991295.html

1. TensorFlow  生成的  .ckpt 和  .pb 都有什么用?

 

The .ckpt is the model given by tensorflow which includes all the weights/parameters in the model. The .pb file stores the computational graph. To make tensorflow work we need both the graph and the parameters. There are two ways to get the graph: (1) use the python program that builds it in the first place (tensorflowNetworkFunctions.py). (2) Use a .pb file (which would have to be generated by tensorflowNetworkFunctions.py). .ckpt file is were all the intelligence is.

 

2. TensorFlow saving into/loading a graph from a file

       

       正好看到 StackOverflow 上有位仁兄问过相关的问题,整理的不错

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

        From what I've gathered so far, there are several different ways of dumping a TensorFlow graph
into a file and then loading it into another program, but I haven't been able to find clear examples/information on how they work. What I already know is this:

  1. Save the model's variables into a checkpoint file (.ckpt) using a tf.train.Saver() and restore them later (source)
  2. Save a model into a .pb file and load it back in using tf.import_graph_def() (source)
  3. Load in a model from a .pb file, retrain it, and dump it into a new .pb file using Bazel (source)
  4. Freeze the graph to save the graph and weights together (source)
  5. Use as_graph_def() to save the model, and for weights/variables, map them into constants (source)

However, I haven't been able to clear up several questions regarding these different methods:

  1. Regarding checkpoint files, do they only save the trained weights of a model? Could checkpoint files be loaded into a new program, and be used to run the model, or do they simply serve as ways to save the weights in a model at a certain time/stage?
  2. Regarding Regarding Bazel, can it only save into/load from .pb files for retraining? Is there a simple Bazel command just to dump a graph into a .pb?
  3. Regarding freezing, can a frozen graph be loaded in using The Android demo for TensorFlow loads in Google's Inception model from a .pb file. If I wanted to substitute my own .pb file, how would I go about doing that? Would I need to change any native code/methods?
  4. In general, what exactly is the difference between all these methods? Or more broadly, what is the difference between  In short, what I'm looking for is a method to save both a graph (as in, the various operations and such) and its weights/variables into a file, which can then be used to load the graph and weights into another program, for use (not necessarily continuing/retraining).

     

    Documentation about this topic isn't very straightforward, so any answers/information would be greatly appreciated.

1 Answer

active oldest votes

 

up vote
down voteaccepted

There are many ways to approach the problem of saving a model in TensorFlow, which can make it a bit confusing. The documentation on this topic is taking shape, but doesn't cover all of the details in your question. Taking each of your sub-questions in turn:

  1. The checkpoint files (produced e.g. by calling saver.save() on a tf.train.Saver object) contain only the weights, and any other variables defined in the same program. To use them in another program, you must re-create the associated graph structure (e.g. by running code to build it again, or calling saver.save() also produces a file containing a the tutorial for more details.

  2. tf.train.write_graph() only writes the graph structure; not the weights.

  3. Bazel is unrelated to reading or writing TensorFlow graphs. (Perhaps I misunderstand your question: feel free to clarify it in a comment.)

  4. A frozen graph can be loaded using 

    The main change would be to update the names of the tensor(s) that are fed into the model, and the names of the tensor(s) that are fetched from the model. In the TensorFlow Android demo, this would correspond to the outputName strings that are passed to 

    The GraphDef is the program structure, which typically does not change through the training process. The checkpoint is a snapshot of the state of a training process, which typically changes at every step of the training process. As a result, TensorFlow uses different storage formats for these types of data, and the low-level API provides different ways to save and load them. Higher-level libraries, such as the Keras, and skflow build on these mechanisms to provide more convenient ways to save and restore an entire model.

share
 
  
Does this mean that the C++ API documentation lies, when it says that you can load the graph saved withtf.train.write_graph() and then execute it? – mnicky yesterday 
  
The C++ API documentation does not lie, but it is missing a few details. The most important detail is that, in addition to the GraphDef saved by mrry yesterday

相关文章:

  • 分享一份非常强势的Android面试题
  • Linux中进行抓包
  • Cordova 笔记
  • VLAN及三层交换机实例
  • 易百教程人工智能python修正-人工智能监督学习(回归)
  • jenkins war下载地址
  • 企业开发的顶级语言调查;南大用“推荐算法”分宿舍;黑客每 60 秒可盗走超 100 万美元资产...
  • OpenCV图像哈希计算及汉明距离的计算
  • 【译Py】2018年8月,GitHub上的Python数据科学明星项目:自动化机器学习、自然语言处理、可视化、机器学习工作流...
  • ElasticSearch(九):springboot项目集成消息中间件activeMQ
  • BZOJ2157旅游——树链剖分+线段树
  • linux中快速清空文件内容的几种方法
  • JS中的继承
  • MyBatis拦截器原理探究
  • 关于电脑使用习惯的若干看法
  • Angular Elements 及其运作原理
  • Angular 响应式表单 基础例子
  • CentOS学习笔记 - 12. Nginx搭建Centos7.5远程repo
  • github指令
  • Java精华积累:初学者都应该搞懂的问题
  • js ES6 求数组的交集,并集,还有差集
  • Otto开发初探——微服务依赖管理新利器
  • overflow: hidden IE7无效
  • RedisSerializer之JdkSerializationRedisSerializer分析
  • session共享问题解决方案
  • Spring Boot MyBatis配置多种数据库
  • UEditor初始化失败(实例已存在,但视图未渲染出来,单页化)
  • Vue.js-Day01
  • Vue--数据传输
  • XForms - 更强大的Form
  • 编写符合Python风格的对象
  • 干货 | 以太坊Mist负责人教你建立无服务器应用
  • 简单基于spring的redis配置(单机和集群模式)
  • 为什么要用IPython/Jupyter?
  • 赢得Docker挑战最佳实践
  • 鱼骨图 - 如何绘制?
  • 浅谈sql中的in与not in,exists与not exists的区别
  • 完善智慧办公建设,小熊U租获京东数千万元A+轮融资 ...
  • ​LeetCode解法汇总518. 零钱兑换 II
  • ### Error querying database. Cause: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException
  • #DBA杂记1
  • #NOIP 2014# day.2 T2 寻找道路
  • #我与Java虚拟机的故事#连载11: JVM学习之路
  • #我与Java虚拟机的故事#连载16:打开Java世界大门的钥匙
  • (C#)if (this == null)?你在逗我,this 怎么可能为 null!用 IL 编译和反编译看穿一切
  • (cljs/run-at (JSVM. :browser) 搭建刚好可用的开发环境!)
  • (初研) Sentence-embedding fine-tune notebook
  • (入门自用)--C++--抽象类--多态原理--虚表--1020
  • (数据结构)顺序表的定义
  • (算法设计与分析)第一章算法概述-习题
  • (转)Android学习系列(31)--App自动化之使用Ant编译项目多渠道打包
  • (转)C#调用WebService 基础
  • (转)JVM内存分配 -Xms128m -Xmx512m -XX:PermSize=128m -XX:MaxPermSize=512m
  • (转)四层和七层负载均衡的区别
  • .a文件和.so文件