当前位置:网站首页>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文件。
边栏推荐
猜你喜欢

函数基本学习之一

List announced | outstanding intellectual property service team in China in 2021

最近公共祖先(LCA)在线做法

Flume interview questions

PMP证书真的有用吗?

【MySQL】explain的基本使用以及各列的作用

BlocProvider 为什么感觉和 Provider 很相似?

Manually implement function isinstanceof (child, parent)

企业架构与项目管理的关联和区别

Training on the device with MIT | 256Kb memory
随机推荐
A debugging to understand the slot mechanism of redis cluster
What is the difference between PMP and NPDP?
Separate the letters and numbers in the string so that the letters come first and the array comes last
旁路由设置的正确方式
MySQL清空表数据
Application of real estate management based on 3D GIS
ICML2022 | 基于元语义正则化的介入性对比学习
Count the number of each character in the character
Internet of things RFID, etc
Can you get a raise? Analysis on gold content of PMP certificate
mysql 学习笔记-优化之SQL优化
[monomer] recommended configuration of streaming information i-bpsv3 server
能升职加薪?PMP证书含金量浅析
基于K-means的用户画像聚类模型
浏览器tab页之间的通信
名单揭晓 | 2021年度中国杰出知识产权服务团队
MIT|256KB 内存下的设备上训练
【直播回顾】战码先锋首期8节直播完美落幕,下期敬请期待!
微信小程序,连续播放多段视频。合成一个视频的样子,自定义视频进度条
MQ学习笔记