keras模型导出到tensorflow,以及pb模型导出

1 keras模型导出成tf模型

https://github.com/amir-abdi/keras_to_tensorflow/blob/master/keras_to_tensorflow.ipynb

constant_graph = graph_util.convert_variables_to_constants(sess, sess.graph.as_graph_def(), pred_node_names)
pred_node_names 为keras模型输出的名字,可用model.output.name拿到
graph_io.write_graph(constant_graph, output_fld, output_graph_name, as_text=False)
函数定义:
write_graph(
graph_or_graph_def,
logdir,
name,
as_text=True
)
函数returns:the path of the output proto file
The graph is written as a binary proto unless as_text is True

注意:
调整keras的learning_phase为0,表示为测试模式
from keras import backend as K
K.set_learning_phase(0)  # all new operations will be in test mode from now on# serialize the model and get its weights, for quick re-buildingconfig = previous_model.get_config()weights = previous_model.get_weights()# re-build a model where the learning phase is now hard-coded to 0
from keras.models import model_from_config
new_model = model_from_config(config)
new_model.set_weights(weights)
由此发现write_graph中model是由输出决定的,即不同的输出对应不同的model
graph_def = g_2.as_graph_def()
tf.train.write_graph(graph_def, export_dir, 'expert-graph.pb', as_text=False)
猜测:此种只保留了图的结构
 
2 pb模型导出
with open(output_graph_path, "rb") as f:
        output_graph_def = tf.GraphDef()
        output_graph_def.ParseFromString(f.read())
        _ = tf.import_graph_def(output_graph_def, name="")

with tf.Session() as sess:
        tf.initialize_all_variables().run()
        input_x = sess.graph.get_tensor_by_name("input:0")
#和模型的输入名字有关,input是模型的输入tensor
        print input_x
        output = sess.graph.get_tensor_by_name("output:0")
        print output
#和模型的输出名字有关,output是模型的输出tensor
        y_conv_2 = sess.run(output,{input_x:mnist.test.images})
        print "y_conv_2", y_conv_2
       

 
 


要将训练好的 TensorFlow 模型保存为 .pb 文件,您可以按照以下步骤进行操作: 1. 定义模型结构:在保存模型之前,您需要定义模型的结构,包括输入和输出节点的名称、形状和数据类型。您可以使用 TensorFlow 的高级 API(如 Keras)或自定义模型来定义模型结构。 2. 加载模型权重:将训练好的模型权重加载到定义的模型结构中。这可以通过加载已保存的模型权重文件(如 .h5、.ckpt 等)或通过重新训练模型来实现。 3. 创建 SavedModel:使用 TensorFlow 的 `tf.saved_model.save` 函数将模型保存为 SavedModel 格式。SavedModel 是 TensorFlow 的一种标准模型保存格式,可以包含模型的计算图和变量值。 ```python import tensorflow as tf # 定义和加载模型权重 model = ... # 定义模型结构 model.load_weights('model_weights.h5') # 加载模型权重 # 保存为 SavedModel 格式 tf.saved_model.save(model, 'saved_model') ``` 这将会在指定路径下创建一个名为 `saved_model` 的文件夹,其中包含了模型的计算图和变量值。 4. 导出为 .pb 文件:从 SavedModel 中导出所需的 .pb 文件。可以使用 TensorFlow 的 `tf.compat.v1.graph_util.convert_variables_to_constants` 函数将 SavedModel 的计算图和变量值转换为常量,并保存为 .pb 文件。 ```python from tensorflow.python.framework import graph_util # 加载 SavedModel saved_model_dir = 'saved_model' saved_model = tf.saved_model.load(saved_model_dir) # 将 SavedModel 转换为 .pb 文件 output_pb_file = 'model.pb' graph_def = graph_util.convert_variables_to_constants( saved_model.sess, saved_model.sess.graph_def, ['output_node_name']) with tf.io.gfile.GFile(output_pb_file, 'wb') as f: f.write(graph_def.SerializeToString()) ``` 将上述代码中的 `'output_node_name'` 替换为模型输出节点的名称。 现在,您应该已经成功将训练好的 TensorFlow 模型保存为 .pb 文件。请注意,这只是一个基本示例,具体的实现细节可能因您的模型结构和需求而有所不同。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值