将TensorFlow的模型网络导出为单个文件的方法
作者:EncodeTS 发布时间:2022-11-11 07:30:13
有时候,我们需要将TensorFlow的模型导出为单个文件(同时包含模型架构定义与权重),方便在其他地方使用(如在c++中部署网络)。利用tf.train.write_graph()默认情况下只导出了网络的定义(没有权重),而利用tf.train.Saver().save()导出的文件graph_def与权重是分离的,因此需要采用别的方法。
我们知道,graph_def文件中没有包含网络中的Variable值(通常情况存储了权重),但是却包含了constant值,所以如果我们能把Variable转换为constant,即可达到使用一个文件同时存储网络架构与权重的目标。
我们可以采用以下方式冻结权重并保存网络:
import tensorflow as tf
from tensorflow.python.framework.graph_util import convert_variables_to_constants
# 构造网络
a = tf.Variable([[3],[4]], dtype=tf.float32, name='a')
b = tf.Variable(4, dtype=tf.float32, name='b')
# 一定要给输出tensor取一个名字!!
output = tf.add(a, b, name='out')
# 转换Variable为constant,并将网络写入到文件
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
# 这里需要填入输出tensor的名字
graph = convert_variables_to_constants(sess, sess.graph_def, ["out"])
tf.train.write_graph(graph, '.', 'graph.pb', as_text=False)
当恢复网络时,可以使用如下方式:
import tensorflow as tf
with tf.Session() as sess:
with open('./graph.pb', 'rb') as f:
graph_def = tf.GraphDef()
graph_def.ParseFromString(f.read())
output = tf.import_graph_def(graph_def, return_elements=['out:0'])
print(sess.run(output))
输出结果为:
[array([[ 7.],
[ 8.]], dtype=float32)]
可以看到之前的权重确实保存了下来!!
问题来了,我们的网络需要能有一个输入自定义数据的接口啊!不然这玩意有什么用。。别急,当然有办法。
import tensorflow as tf
from tensorflow.python.framework.graph_util import convert_variables_to_constants
a = tf.Variable([[3],[4]], dtype=tf.float32, name='a')
b = tf.Variable(4, dtype=tf.float32, name='b')
input_tensor = tf.placeholder(tf.float32, name='input')
output = tf.add((a+b), input_tensor, name='out')
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
graph = convert_variables_to_constants(sess, sess.graph_def, ["out"])
tf.train.write_graph(graph, '.', 'graph.pb', as_text=False)
用上述代码重新保存网络至graph.pb,这次我们有了一个输入placeholder,下面来看看怎么恢复网络并输入自定义数据。
import tensorflow as tf
with tf.Session() as sess:
with open('./graph.pb', 'rb') as f:
graph_def = tf.GraphDef()
graph_def.ParseFromString(f.read())
output = tf.import_graph_def(graph_def, input_map={'input:0':4.}, return_elements=['out:0'], name='a')
print(sess.run(output))
输出结果为:
[array([[ 11.],
[ 12.]], dtype=float32)]
可以看到结果没有问题,当然在input_map那里可以替换为新的自定义的placeholder,如下所示:
import tensorflow as tf
new_input = tf.placeholder(tf.float32, shape=())
with tf.Session() as sess:
with open('./graph.pb', 'rb') as f:
graph_def = tf.GraphDef()
graph_def.ParseFromString(f.read())
output = tf.import_graph_def(graph_def, input_map={'input:0':new_input}, return_elements=['out:0'], name='a')
print(sess.run(output, feed_dict={new_input:4}))
看看输出,同样没有问题。
[array([[ 11.],
[ 12.]], dtype=float32)]
另外需要说明的一点是,在利用tf.train.write_graph写网络架构的时候,如果令as_text=True了,则在导入网络的时候,需要做一点小修改。
import tensorflow as tf
from google.protobuf import text_format
with tf.Session() as sess:
# 不使用'rb'模式
with open('./graph.pb', 'r') as f:
graph_def = tf.GraphDef()
# 不使用graph_def.ParseFromString(f.read())
text_format.Merge(f.read(), graph_def)
output = tf.import_graph_def(graph_def, return_elements=['out:0'])
print(sess.run(output))
参考资料
Is there an example on how to generate protobuf files holding trained Tensorflow graphs
来源:https://blog.csdn.net/encodets/article/details/54428456
猜你喜欢
- 1.定义帕累托图:是一种特殊的直方图, 在项目管理知识体系中属于质量管理的工具。 它可以帮助观众了解哪些因素对结果影响最大。它基于帕累托原则
- 这篇文章主要介绍了python解析命令行参数的三种方法详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要
- 前几天遇到一个问题,需要把网页中的一部分内容挑出来,于是找到了urllib和HTMLParser两个库.urllib可以将网页爬下来,然后交
- 一、说在前面 需求:有一张长为960,宽为96的图片,需要将其分割成10张96*96的图
- 空接口定义空接口是特殊形式的接口类型,普通的接口都有方法,而空接口没有定义任何方法口,也因此,我们可以说所有类型都至少实现了空接口。type
- 前言这篇博客针对《PPython+Qt身体特征识别人数统计》编写代码,功能包括了相片,摄像头身体识别,数量统计。代码整洁,规则,易读。应用推
- 基础知识使用框架的优点稳定性和可扩展性强可以降低开发难度,提高了开发效率Flask诞生于2010年,是Armin ronacher用Pyth
- 表单内有两个提交按钮,要实现当点击不同的提交按钮时,分别进行两个不同的处理过程,在这里有实现表单多按钮提交action的处理方法分享给大家。
- python语言本身没有提供const,但实际开发中经常会遇到需要使用const的情形,由于语言本身没有这种支出,因此需要使用一些技巧来实现
- 前奏为了能操作数据库, 首先我们要有一个数据库, 所以要首先安装Mysql, 然后创建一个测试数据库python_test用以后面的测试使用
- 下面两个函数实现了对字符串中数字的判断。function isnaw(str) f
- DRF中的Request在Django REST Framework中内置的Request类扩展了Django中的Request类,实现了很
- SQL Server TEXT、NTEXT字段拆分的问题引用的内容:SET NOCOUNT ON CREATE 
- 这篇文章主要介绍了使用python远程操作linux过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需
- 本文研究的主要是Django中Forms的使用,具体如下。创建文件do.html{% extends 'base.html'
- 最近要做数据库同步,如果网上找了例子,成功,记录下来,下回再看。这个是网上找的一编文章。以下配置在本机上已经成功:实现功能:A为主服务器,B
- Python 中主要有8种数据类型:number(数字)、string(字符串)、list(列表)、tuple(元组)、dict(字典)、s
- SQL Server 2000使得以XML导出数据变得更加简单,但在SQL Server 2000中导入XML数据并对其进行处理则有些麻烦。
- 一、模型方法 本工程采用的模型方法为朴素贝叶斯分类算法,它的核心算法思想基于概率论。我们
- 1、什么是超链接图标规范超链接图标规范是我根据《Iconize Textlinks with CSS》修改调整完成的。主要是整理出常用到的一