保存和加载pb模型
2018-01-31 22:20
375 查看
将模型保存为pb
加载pb模型
import tensorflow as tf from tensorflow.python.framework import graph_util logdir='output/' with tf.variable_scope('conv'): w=tf.get_variable('w',[2,2],tf.float32,initializer=tf.random_normal_initializer) b=tf.get_variable('b',[2],tf.float32,initializer=tf.random_normal_initializer) sess=tf.InteractiveSession() tf.global_variables_initializer().run() # 初始化所有变量 constant_graph_w = graph_util.convert_variables_to_constants(sess, sess.graph_def, ["conv/w"]) constant_graph_b = graph_util.convert_variables_to_constants(sess , sess.graph_def , ['conv/b']) with tf.gfile.FastGFile(logdir+'expert_graph.pb', mode='wb') as f: f.write(constant_graph_w.SerializeToString()) f.write(constant_graph_b.SerializeToString()) sess.close()
加载pb模型
import tensorflow as tf from tensorflow.python.framework import graph_util logdir = 'output/' output_graph_path = logdir+'expert_graph.pb' with tf.Session() as sess: tf.global_variables_initializer().run() output_graph_def = tf.GraphDef() with open(output_graph_path, "rb") as f: output_graph_def.ParseFromString(f.read()) _ = tf.import_graph_def(output_graph_def, name="") w = sess.graph.get_tensor_by_name("conv/w:0") print('w:' , w.eval()) b = sess.graph.get_tensor_by_name("conv/b:0") print('b:' , b.eval())
相关文章推荐
- 将tflearn的模型保存为pb,给TensorFlow使用
- Tensorflow 保存和加载模型
- TensorFlow 深度学习框架(7)-- 变量管理及训练模型的保存与加载
- Tensorflow加载预训练模型和保存模型
- Tensorflow模型的保存和加载
- tensorflow模型参数保存和加载问题
- TensorFlow 深度学习框架(7)-- 变量管理及训练模型的保存与加载
- TensorFlow 深度学习框架(7)-- 变量管理及训练模型的保存与加载
- 【pytorch】模型的搭建保存加载
- TensorFlow 模型保存与加载
- TensorFlow1.5训练模型的保存与加载
- Tensorflow学习笔记-模型保存与加载
- tensorflow之保存模型与加载模型
- 如何用Tensorflow训练模型成pb文件和和如何加载已经训练好的模型文件
- 【tensorflow】保存模型、再次加载模型等操作
- TensorFlow保存和加载训练模型
- TensorFlow保存和加载训练模型
- pytorch学习笔记(五):保存和加载模型
- Tensorflow加载预训练模型和保存模型的实例
- TensorFlow 深度学习框架(7)-- 变量管理及训练模型的保存与加载