• keras训练的H5模型转tflite


    1、使用代码转换

    训练使用tf1.13.1和keras2.0
    转换tflite环境是tf1.13.1,亲测可用

    代码如下:

    import tensorflow.lite as lite
    import argparse
    
    def parse_args():
    
        parser = argparse.ArgumentParser(description='Keras to TensorFlow Lite converter')
    
        parser.add_argument('--input_keras',
                            required=True,
                            type=str,
                            help='The input Keras file model (.h5)')
    
        parser.add_argument('--output_tflite',
                            required=True,
                            type=str,
                            help='The output TensorFlow Lite file model (.tflite)')
    
        parser.add_argument('--post_quantize',
                            required=False,
                            type=bool,
                            help='Use post-quantization')
    
        args = parser.parse_args()
        return args
    
    def convert(args):
    
        input_file = args.input_keras
        output_file = args.output_tflite
    
        # Converts the Keras model to TensorFlow Lite
        converter = lite.TocoConverter.from_keras_model_file(input_file)
        converter.post_training_quantize = True
        tflite_model = converter.convert()
        open(output_file, "wb").write(tflite_model)
    
    
    def run():
        args = parse_args()
        convert(args)
    
    if __name__ == "__main__":
        run()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43

    2、使用tflite_convert工具

    还没有试过:使用tflite_convert命令工具将keras h5文件转换为tflite文件简易指南
    这个教程分两步走,先将h5转换(经代码API转换)为pb模型,再将pb模型转换(经tflite_convert工具转换)为tflite

    3、亲测使用tflite_convert工具

    模型训练使用tf1.13.1和keras2.0
    转换tflite环境是tf1.13.1

    3.1 简介

    tflite_convert是一个命令行工具,可以先看下将pb模型转tflite的大概流程:TF1.x和TF2.x版本训练的PB模型转Tflite。发现我们在对keras模型转换为tflite时需要知道h5模型的信息:

    usage: tflite_convert --output_file OUTPUT_FILE	# 输出路径
                          --keras_model_file KERAS_MODEL_FILE	# keras的h5模型文件路径
                          --output_format TFLITE	# 输出格式,指定为TFLITE(固定)
                          --input_arrays INPUT_ARRAYS	# 输入节点名
                          --input_shapes INPUT_SHAPES	# 输入形状
                          --output_arrays OUTPUT_ARRAYS	# 输出节点名
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    下面我们讲input_arrays 、input_shapes、output_arrays的获取方法

    3.2 模型信息获取

    直接给出代码:

    from keras.models import load_model
    net = load_model(r"model.hdf5", compile=False)
    
    print("inputs info",net.inputs)	
    print("inputs name",[node.op.name for node in net.inputs])
    print("output name",[node.op.name for node in net.outputs])
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    程序输出:

    # 可以看出shape,在tflite_convert中指定为1,64,64,1
    inputs info [<tf.Tensor 'input_1:0' shape=(?, 64, 64, 1) dtype=float32>]
    
    # 可以看出输入节点名为input_1
    inputs name ['input_1']
    
    # 可以看出输出节点名为predictions/Softmax
    output name ['predictions/Softmax']
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    实际上你可以直接print(net.inputs)print(net.outputs),输出为
    在这里插入图片描述
    :0是考虑到了多输入和多输出模型,但实际上本模型是单输入和单输出,所以也就是'input_1''predictions/Softmax'

    3.3 转换

    在命令行中执行:

    tflite_convert --output_file output.tflite 	# 指定输出路径
    			   --keras_model_file newdata_mini_XCEPTION.123-0.70.hdf5 # 指定要转换的模型
    			   --input_arrays input_1 	# 指定输入节点名
    			   --input_shapes 1,64,64,1 	# 输入shape
    			   --output_arrays predictions/Softmax 	# 输出节点名
    			   --output_format TFLITE	# TFLITE模式
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    如果没有报错,并且存在 output.tflite文件,应当就是转换成功了
    output.tflite应当存在你执行命令行目录下,或者在执行命令指定绝对路径,方便寻找output.tflite文件。

  • 相关阅读:
    20221130 RabbitMQ
    K8S之prometheus-operator监控
    企业数字化转型到底是什么?
    软件工程与计算总结(一)软件工程基础
    Java日志系列——概述,JUL
    STC 32位8051单片机开发实例教程 三 程序编译设置与下载
    力扣-228. 汇总区间
    通过提示工程将化学知识整合到大型语言模型中
    11月第1周榜单丨飞瓜数据B站UP主排行榜(哔哩哔哩)发布!
    Zalando Postgres Operator 快速上手
  • 原文地址:https://blog.csdn.net/qq_40243750/article/details/125542807