当前位置:网站首页>keras训练的H5模型转tflite
keras训练的H5模型转tflite
2022-07-01 21:43:00 【我是一个对称矩阵】
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()
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 # 输出节点名
下面我们讲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])
程序输出:
# 可以看出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']
实际上你可以直接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模式
如果没有报错,并且存在 output.tflite文件,应当就是转换成功了 output.tflite应当存在你执行命令行目录下,或者在执行命令指定绝对路径,方便寻找output.tflite文件。
边栏推荐
- js如何获取集合对象中某元素列表
- AirServer2022最新版功能介绍及下载
- Classify boost libraries by function
- 比较版本号[双指针截取自己想要的字串]
- [deep learning] use deep learning to monitor your girlfriend's wechat chat?
- 工控设备安全加密的意义和措施
- 业务可视化-让你的流程图'Run'起来
- Qtreeview+qabstractitemmodel custom model: the third of a series of tutorials [easy to understand]
- 选择在同花顺上炒股开户可以吗?安全吗?
- ngnix基础知识
猜你喜欢

编程英语生词笔记本

What is the difference between PMP and NPDP?

Pytest Collection (2) - mode de fonctionnement pytest

指标陷阱:IT领导者易犯的七个KPI错误

locust 系列入门

工控设备安全加密的意义和措施

基于LSTM模型实现新闻分类

Pytest collection (2) - pytest operation mode

Flume面试题

Yan Rong looks at how to formulate a multi cloud strategy in the era of hybrid cloud
随机推荐
Use of vscode
Introduction and download of the latest version of airserver2022
游览器打开摄像头案例
Copy ‘XXXX‘ to effectively final temp variable
基于三维GIS的不动产管理应用
Spark interview questions
从MLPerf谈起:如何引领AI加速器的下一波浪潮
Training on the device with MIT | 256Kb memory
Matlab traverses images, string arrays and other basic operations
CNN卷积神经网络原理讲解+图片识别应用(附源码)[通俗易懂]
测试撤销1
同花顺股票开户选哪个券商好手机开户是安全么?
JS how to get a list of elements in a collection object
基于K-means的用户画像聚类模型
最近公共祖先离线做法(tarjan)
I received a letter from CTO inviting me to interview machine learning engineer
Tops, the unit of computing power of the processor, can be carried out 1 trillion times per second
Go — 相关依赖对应的exe
Four methods of JS array splicing [easy to understand]
Several ways of writing main function in C