**//导出pb模型**
import tensorflow as tf
from tensorflow.python.framework import graph_util
v1=tf.Variable(tf.constant(1.0,shape=[1]),name='v1');
v2=tf.Variable(tf.constant(2.0,shape=[1]),name='v2');
result=v1+v2;
init_op=tf.initialize_all_variables();
with tf.Session() as sess:
sess.run(init_op);
graph_def=tf.get_default_graph().as_graph_def();
output_graph_def=graph_util.convert_variables_to_constants(sess,graph_def,['add']);
with tf.gfile.GFile('model/model.pb','wb') as f:
f.write(output_graph_def.SerializeToString());
**//导入pb模型**
import tensorflow as tf
from tensorflow.python.framework import graph_util
from tensorflow.python.platform import gfile
with tf.Session() as sess:
model_filename="model/model.pb";
with gfile.FastGFile(model_filename,'rb') as f:
graph_def=tf.GraphDef();
graph_def.ParseFromString(f.read());
result=tf.import_graph_def(graph_def,return_elements=['add:0']);
print(sess.run(result));
导出pb模型和导入pb模型
最新推荐文章于 2020-09-22 15:55:46 发布