AI实战:keras的h5模型转tensorflow的pb模型

前言

深度学习模型开发首选Keras,模型搭建简单快速。模型部署应用首选Tensorflow。

Keras训练的模型一般保存为h5格式,Tensorflow模型部署使用pb格式最为简单。


h5转pb格式

  • 方法一
    Keras模型保存时使用 save() 函数保存,转换代码如下:
from keras.models import load_model
import tensorflow as tf
import os, sys
from keras import backend as K
from tensorflow.python.framework import graph_util, graph_io

def h5_to_pb(h5_weight_path, output_dir, out_prefix = "output_", log_tensorboard = True):

    if not os.path.exists(output_dir):
        os.mkdir(output_dir)
        
    h5_model = load_model(h5_weight_path)
    
    out_nodes = []
    for i in range(len(h5_model.outputs)):
        out_nodes.append(out_prefix + str(i + 1))
        tf.identity(h5_model.output[i],out_prefix + str(i + 1))
    
    model_name = os.path.splitext(os.path.split(h5_weight_path)[-1])[0] + '.pb'    
    
    sess = K.get_session()
    init_graph = sess.graph.as_graph_def()
    main_graph = graph_util.convert_variables_to_constants(sess, init_graph, out_nodes)
    graph_io.write_graph(main_graph, output_dir, name = model_name, as_text = False)
    if log_tensorboard:
        from tensorflow.python.tools import import_pb_to_tensorboard
        import_pb_to_tensorboard.import_to_tensorboard(os.path.join(output_dir, model_name), output_dir)
        
        
if __name__ == '__main__':
    if len(sys.argv) == 3:
        # usage: python3 h5_to_pb.py h5_weight_path output_dir
        h5_to_pb(h5_weight_path=sys.argv[1], output_dir=sys.argv[2])
  • 方法二
    Keras模型保存时使用 save_weigths() 函数,转换代码如下:
from keras.models import load_model
import tensorflow as tf
import os, sys
from keras import backend as K
from tensorflow.python.framework import graph_util, graph_io

def h5_to_pb(h5_weight_path, output_dir, out_prefix = "output_", log_tensorboard = True):

    if not os.path.exists(output_dir):
        os.mkdir(output_dir)
        
    h5_model = build_model()
    h5_model.load_weights(h5_weight_path)
    
    out_nodes = []
    for i in range(len(h5_model.outputs)):
        out_nodes.append(out_prefix + str(i + 1))
        tf.identity(h5_model.output[i],out_prefix + str(i + 1))
    
    model_name = os.path.splitext(os.path.split(h5_weight_path)[-1])[0] + '.pb'    
    
    sess = K.get_session()
    init_graph = sess.graph.as_graph_def()
    main_graph = graph_util.convert_variables_to_constants(sess, init_graph, out_nodes)
    graph_io.write_graph(main_graph, output_dir, name = model_name, as_text = False)
    if log_tensorboard:
        from tensorflow.python.tools import import_pb_to_tensorboard
        import_pb_to_tensorboard.import_to_tensorboard(os.path.join(output_dir, model_name), output_dir)
        
        
def build_model():
    
    inputs = Input(shape=(784, ), name='input_img')
    x = Dense(64, activation='relu')(inputs)
    x = Dense(64, activation='relu')(x)
    y = Dense(10, activation='softmax')(x)
    h5_model = Model(inputs=inputs, outputs=y)
    
    return h5_model
    
        
if __name__ == '__main__':
    if len(sys.argv) == 3:
        # usage: python3 h5_to_pb.py h5_weight_path output_dir
        h5_to_pb(h5_weight_path=sys.argv[1], output_dir=sys.argv[2])
注意:
1、函数build_model()根据自己的实际模型做修改即可
2、“ inputs = Input(shape=(784, ), name='input_img') ” 中name='input_img'最好是自己命名,在pb使用时会用到

参考

https://blog.csdn.net/leviopku/article/details/86612293

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

szZack

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值