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

Go — 相关依赖对应的exe
![[deep learning] use deep learning to monitor your girlfriend's wechat chat?](/img/03/ecf50eacc91c0633b0d9689cdad2c2.png)
[deep learning] use deep learning to monitor your girlfriend's wechat chat?

Mask wearing detection method based on yolov5

MIT|256KB 内存下的设备上训练

Is PMP certificate really useful?
![比较版本号[双指针截取自己想要的字串]](/img/19/4f858ffdc1281d6b8b18a996467f10.png)
比较版本号[双指针截取自己想要的字串]

PMP与NPDP之间的区别是什么?

Why does blocprovider feel similar to provider?

Getting started with the lockust series

业务可视化-让你的流程图'Run'起来
随机推荐
上半年暂停考试要补考?包含监理工程师、建筑师等十项考试
NIO与传统IO的区别
Go - exe corresponding to related dependency
burpsuite简单抓包教程[通俗易懂]
AirServer手机第三方投屏电脑软件
GaussDB(DWS)主动预防排查
Several ways of writing main function in C
String type conversion BigDecimal, date type
焱融看 | 混合云时代下,如何制定多云策略
PCB线路板塞孔工艺的那些事儿~
Four methods of JS array splicing [easy to understand]
Matlab traverses images, string arrays and other basic operations
收到一封CTO来信,邀约面试机器学习工程师
Do you want to make up for the suspended examination in the first half of the year? Including ten examinations for supervision engineers, architects, etc
PCB plug hole technology~
【MySQL】explain的基本使用以及各列的作用
手动实现function isInstanceOf(child,Parent)
MIT|256KB 内存下的设备上训练
locust 系列入门
打出三位数的所有水仙花数「建议收藏」