keras h5文件,转换tensorflow pb文件,error:ReadVariableOp, github

1、h5模型文件转换成pb模型文件

https://blog.csdn.net/weixin_33795093/article/details/93888015

#*-coding:utf-8-*
 
"""
将keras的.h5的模型文件,转换成TensorFlow的pb文件
"""
# ==========================================================
 
from keras.models import load_model
import tensorflow as tf
import os
from keras import backend
 
 
def h5_to_pb(h5_model, output_dir, model_name, out_prefix="output_", log_tensorboard=True):
    """.h5模型文件转换成pb模型文件
    Argument:
        h5_model: str
            .h5模型文件
        output_dir: str
            pb模型文件保存路径
        model_name: str
            pb模型文件名称
        out_prefix: str
            根据训练,需要修改
        log_tensorboard: bool
            是否生成日志文件
    Return:
        pb模型文件
    """
    if os.path.exists(output_dir) == False:
        os.mkdir(output_dir)
    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))
    sess = backend.get_session()
 
    from tensorflow.python.framework import graph_util, graph_io
    # 写入pb模型文件
    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__':
    #  .h模型文件路径参数
    input_path = 'satellite/train_dir/models/'
    weight_file = 'satellite_iv3_ft.h5'
    weight_file_path = os.path.join(input_path, weight_file)
    output_graph_name = weight_file[:-3] + '.pb'
 
    #  pb模型文件输出输出路径
    output_dir = os.path.join(os.getcwd(), "satellite/train_dir/models/")
 
    #  加载模型
    h5_model = load_model(weight_file_path)
    h5_to_pb(h5_model, output_dir=output_dir, model_name=output_graph_name)
    print('Finished')

模型文件,keras,有两种, 一种,save保存,模型和参数一个h5文件
另一种保存save_weight, 结构和权重分开json,和h5,

from keras.models import model_from_json
from keras.models import load_model
 json_file = "models/model_batch4.json"
 with open(json_file, 'r') as f:
    model_json_file = f.read()
h5_model = model_from_json(model_json_file)
h5_model.load_weights(weight_file_path)
h5_model.save('model.h5')  #将两个模型加载,保存成一个model.h5


h5_model = load_model(weight_file_path)#加载一个含有结构和权重的h5文件

h5转换pb模型代码(解决:找不到输入到ReadVariableOp的变量)Cannot find the variable that is an input to the ReadVariableOp.

import keras.backend as K
K.set_learning_phase(0)
#解决方案是将学习阶段设置为测试模式。
    import keras.backend as K
    K.set_learning_phase(0)
    h5_model = load_model("model.h5")
    h5_to_pb(h5_model, output_dir=output_dir, model_name=output_graph_name)

2、github 工程项目可以直接转换

https://github.com/amir-abdi/keras_to_tensorflow

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值