• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

将TensorFlow的模型网络导出为单个文件的方法

python 搞代码 4年前 (2022-01-09) 26次浏览 已收录 0个评论

本篇文章主要介绍了将TensorFlow的网络导出为单个文件的方法,现在分享给大家,也给大家做个参考。一起过来看看吧

有时候,我们需要将TensorFlow的模型导出为单个文件(同时包含模型架构定义与权重),方便在其他地方使用(如在c++中部署网络)。利用tf.train.write_graph()默认情况下只导出了网络的定义(没有权重),而利用tf.train.Saver().save()导出的文件graph_def与权重是分离的,因此需要采用别的方法。

我们知道,graph_def文件中没有包含网络中的Variable值(通常情况存储了权重),但是却包含了constant值,所以如果我们能把Variable转换为constant,即可达到使用一个文件同时存储网络架构与权重的目标。

我们可以采用以下方式冻结权重并保存网络:

import tensorflow as tffrom 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 tfwith 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 tffrom tensorflow.python.framework.graph_util import convert_variables_to_constantsa = 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 tfwith 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 tfnew_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 tffrom google.protobuf import text_formatwith tf.Session() as sess:  # 不使用'rb'模式  with open('./graph.pb', 'r') as f:    graph_def = tf.GraphDef()    # 不使用graph_def.ParseFromString(f.read())    t<em>本文来源gao.dai.ma.com搞@代*码(网$</em>ext_format.Merge(f.read(), graph_def)    output = tf.import_graph_def(graph_def, return_elements=['out:0'])     print(sess.run(output))

相关推荐:

TensorFlow安装以及对jupyter notebook配置详解

以上就是将TensorFlow的模型网络导出为单个文件的方法的详细内容,更多请关注搞代码gaodaima其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:将TensorFlow的模型网络导出为单个文件的方法

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址