将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

标签:TensorFlow,模型,文件
0
投稿

猜你喜欢

  • MySQL datetime类型与时间、日期格式字符串大小比较的方法

    2024-01-25 23:25:24
  • Pyqt+matplotlib 实现实时画图案例

    2022-01-06 12:52:23
  • Django如何与Ajax交互

    2023-05-09 12:53:11
  • python学习之whl文件解释与安装详解

    2021-11-01 18:09:14
  • python发送多人邮件没有展示收件人问题的解决方法

    2023-01-03 05:16:10
  • python过滤中英文标点符号的实例代码

    2022-11-09 13:43:09
  • Python中使用strip()方法删除字符串中空格的教程

    2022-08-30 16:16:09
  • Pytorch-LSTM输入输出参数方式

    2021-02-22 18:49:28
  • pycharm配置当鼠标悬停时快速提示方法参数

    2022-12-07 09:24:41
  • 10个Python面试常问的问题(小结)

    2023-04-11 19:36:15
  • ASP FCKeditor在线编辑器使用方法

    2023-01-12 23:15:04
  • python实现WebSocket服务端过程解析

    2022-09-14 10:45:19
  • Python+OpenCV实现信用卡数字识别的方法详解

    2022-01-15 14:25:41
  • 深入浅析Python2.x和3.x版本的主要区别

    2023-06-21 04:12:52
  • Python实现一元一次与一元二次方程求解

    2022-03-30 14:09:32
  • 使用Python将数组的元素导出到变量中(unpacking)

    2022-01-12 10:11:43
  • PHP设计模式之装饰器模式定义与用法详解

    2023-09-11 18:41:48
  • python实现连连看游戏

    2021-12-02 00:25:10
  • python制作定时发送信息脚本的实现思路

    2023-01-17 15:24:11
  • taro小程序添加骨架屏的实现代码

    2024-04-19 11:04:04
  • asp之家 网络编程 m.aspxhome.com