TensorFlow固化模型的实现操作

作者:Jcme丶Ls 时间:2022-09-12 22:28:42 

前言

TensorFlow目前在移动端是无法training的,只能跑已经训练好的模型,但一般的保存方式只有单一保存参数或者graph的,如何将参数、graph同时保存呢?

生成模型

主要有两种方法生成模型,一种是通过freeze_graph把tf.train.write_graph()生成的pb文件与tf.train.saver()生成的chkp文件固化之后重新生成一个pb文件,这一种现在不太建议使用。另一种是把变量转成常量之后写入PB文件中。我们简单的介绍下freeze_graph方法。

freeze_graph

这种方法我们需要先使用tf.train.write_graph()以及tf.train.saver()生成pb文件和ckpt文件,代码如下:


with tf.Session() as sess:
saver = tf.train.Saver()
saver.save(session, "model.ckpt")
tf.train.write_graph(session.graph_def, '', 'graph.pb')

然后使用TensorFlow源码中的freeze_graph工具进行固化操作:

首先需要build freeze_graph 工具( 需要 bazel ):

bazel build tensorflow/python/tools:freeze_graph

然后使用这个工具进行固化(/path/to/表示文件路径):

bazel-bin/tensorflow/python/tools/freeze_graph --input_graph=/path/to/graph.pb --input_checkpoint=/path/to/model.ckpt --output_node_names=output/predict --output_graph=/path/to/frozen.pb
convert_variables_to_constants

其实在TensorFlow中传统的保存模型方式是保存常量以及graph的,而我们的权重主要是变量,如果我们把训练好的权重变成常量之后再保存成PB文件,这样确实可以保存权重,就是方法有点繁琐,需要一个一个调用eval方法获取值之后赋值,再构建一个graph,把W和b赋值给新的graph。

牛逼的Google为了方便大家使用,编写了一个方法供我们快速的转换并保存。

首先我们需要引入这个方法

from tensorflow.python.framework.graph_util import convert_variables_to_constants

在想要保存的地方加入如下代码,把变量转换成常量

output_graph_def = convert_variables_to_constants(sess, sess.graph_def, output_node_names=['output/predict'])

这里参数第一个是当前的session,第二个为graph,第三个是输出节点名(如我的输出层代码是这样的:)


with tf.name_scope('output'):
w_out = tf.Variable(w_alpha * tf.random_normal([1024, MAX_CAPTCHA * CHAR_SET_LEN]))
tf.summary.histogram('output/weight', w_out)
b_out = tf.Variable(b_alpha * tf.random_normal([MAX_CAPTCHA * CHAR_SET_LEN]))
tf.summary.histogram('output/biases', b_out)
out = tf.add(tf.matmul(dense2, w_out), b_out)
out = tf.nn.softmax(out)
predict = tf.argmax(tf.reshape(out, [-1, 11, 36]), 2, name='predict')

由于我们采用了name_scope所以我们在predict之前需要加上output/

生成文件

with tf.gfile.FastGFile('model/CTNModel.pb', mode='wb') as f:
f.write(output_graph_def.SerializeToString())

第一个参数是文件路径,第二个是指文件操作的模式,这里指的是以二进制的方式写入文件。

运行代码,系统会生成一个PB文件,接下来我们要测试下这个模型是否能够正常的读取、运行。

测试模型

在Python环境下,我们首先需要加载这个模型,代码如下:


with open('./model/rounded_graph.pb', 'rb') as f:
graph_def = tf.GraphDef()
graph_def.ParseFromString(f.read())
output = tf.import_graph_def(graph_def,
    input_map={'inputs/X:0': newInput_X},
    return_elements=['output/predict:0'])

由于我们原本的网络输入值是一个placeholder,这里为了方便输入我们也先定义一个新的placeholder:

newInput_X = tf.placeholder(tf.float32, [None, IMAGE_HEIGHT * IMAGE_WIDTH], name="X")

在input_map的参数填入新的placeholder。

在调用我们的网络的时候直接用这个新的placeholder接收数据,如:

text_list = sesss.run(output, feed_dict={newInput_X: [captcha_image]})

然后就是运行我们的网络,看是否可以运行吧。

来源:https://www.jianshu.com/p/091415b114e2

标签:TensorFlow,固化,模型
0
投稿

猜你喜欢

  • Python PSO算法处理TSP问题详解

    2022-12-02 02:39:24
  • python实现简易猜数小游戏

    2022-08-08 09:51:55
  • Django REST 异常处理详解

    2023-06-19 02:40:34
  • Python标准库pathlib操作目录和文件

    2021-08-18 02:24:57
  • python如何往列表头部和尾部添加元素

    2021-12-17 07:05:17
  • 怎么样才能设计出漂亮的网页?

    2008-10-07 16:57:00
  • Python科学画图代码分享

    2023-08-19 07:06:25
  • MYSQL的存储过程和函数简单写法

    2024-01-21 20:16:34
  • python实现自动抢课脚本的示例代码

    2022-03-12 08:16:19
  • 解决jupyter (python3) 读取文件遇到的问题

    2021-02-05 10:00:25
  • Python中的迭代器与生成器使用及说明

    2022-01-01 08:14:27
  • Python设计模式之简单工厂模式实例详解

    2022-11-03 04:43:08
  • Python中设置变量作为默认值时容易遇到的错误

    2023-09-14 09:32:00
  • mysql与sqlserver的所有区别

    2009-02-27 16:18:00
  • 前端来看看 maxthon bugs

    2008-09-23 18:35:00
  • Python figure参数及subplot子图绘制代码

    2023-09-14 17:13:00
  • SpringBoot项目application.yml文件数据库配置密码加密的方法

    2024-01-18 02:12:40
  • Python数据清洗&预处理入门教程

    2021-08-15 20:14:24
  • 使用python创建Excel工作簿及工作表过程图解

    2021-10-05 03:57:34
  • MySQL数据库备份和还原的常用命令

    2012-01-05 18:50:06
  • asp之家 网络编程 m.aspxhome.com