将tensorflow模型打包成PB文件及PB文件读取方式
作者:zchenack 时间:2022-07-02 22:47:33
1. tensorflow模型文件打包成PB文件
import tensorflow as tf
from tensorflow.python.tools import freeze_graph
with tf.Graph().as_default():
with tf.device("/cpu:0"):
config = tf.ConfigProto(allow_soft_placement=True)
with tf.Session(config=config).as_default() as sess:
model = Your_Model_Name()
model.build_graph()
sess.run(tf.initialize_all_variables())
saver = tf.train.Saver()
ckpt_path = "/your/model/path"
saver.restore(sess, ckpt_path)
graphdef = tf.get_default_graph().as_graph_def()
tf.train.write_graph(sess.graph_def,"/your/save/path/","save_name.pb",as_text=False)
frozen_graph = tf.graph_util.convert_variables_to_constants(sess,graphdef,['output/node/name'])
frozen_graph_trim = tf.graph_util.remove_training_nodes(frozen_graph)
freeze_graph.freeze_graph('/your/save/path/save_name.pb','',True, ckpt_path,'output/node/name','save/restore_all','save/Const:0','frozen_name.pb',True,"")
2. PB文件读取使用
output_graph_def = tf.GraphDef()
with open("your_name.pb","rb") as f:
output_graph_def.ParseFromString(f.read())
_ = tf.import_graph_def(output_graph_def, name="")
node_in = sess.graph.get_tensor_by_name("input_node_name")
model_out = sess.graph.get_tensor_by_name("out_node_name")
feed_dict = {node_in:in_data}
pred = sess.run(model_out, feed_dict)
来源:https://blog.csdn.net/hustchenze/article/details/83660960
标签:tensorflow,打包,PB文件
0
投稿
猜你喜欢
MySQL拼接字符串函数GROUP_CONCAT详解
2024-01-27 18:21:56
微信企业号开发之微信考勤百度地图定位
2024-05-08 10:11:47
go程序测试CPU占用率统计ps vs top两种不同方式对比
2024-05-05 09:27:04
python画折线图的程序
2021-07-04 18:08:20
WEB页面工具语言XML产生背景
2008-05-29 10:52:00
梅尔倒谱系数(MFCC)实现
2022-08-08 18:28:08
详解webpack require.ensure与require AMD的区别
2024-05-03 15:08:18
PHP最常用的正则表达式
2024-05-03 15:34:38
SQL Server中identity(自增)的用法详解
2024-01-24 22:33:50
如何用Python进行时间序列分解和预测
2022-06-20 14:39:42
详解Django-channels 实现WebSocket实例
2021-02-12 03:53:48
HTML5 JS压缩图片并获取图片BASE64编码上传
2024-04-17 10:25:44
MySQL查询优化的5个实用技巧
2024-01-19 03:30:36
javascript移动设备Web开发中对touch事件的封装实例
2024-04-26 17:13:17
教你使用Python连接oracle
2023-09-11 01:38:54
SQL SERVER数据库开发之asp存储过程应用
2008-05-19 12:55:00
MSSQL数据类型及长度限制详细说明
2024-01-25 06:01:09
如何在nodejs中体验http/2详解
2024-05-05 09:21:48
python实现词法分析器
2022-09-12 15:03:18
django-rest-framework 自定义swagger过程详解
2023-01-01 22:05:34