当前位置:网站首页>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文件。
边栏推荐
- Microsoft, Columbia University | Godel: large scale pre training of goal oriented dialogue
- require与import的区别和使用
- BlocProvider 为什么感觉和 Provider 很相似?
- [STM32] stm32cubemx tutorial II - basic use (new projects light up LED lights)
- Icml2022 | interventional contrastive learning based on meta semantic regularization
- MIT|256KB 内存下的设备上训练
- Yan Rong looks at how to formulate a multi cloud strategy in the era of hybrid cloud
- Significance and measures of security encryption of industrial control equipment
- MQ学习笔记
- TOPS,处理器运算能力单位、每秒钟可进行一万亿次
猜你喜欢
基于YOLOv5的口罩佩戴检测方法
基于K-means的用户画像聚类模型
Pytest collection (2) - pytest operation mode
[noip2013] building block competition [noip2018] road laying greed / difference
[intelligent QBD risk assessment tool] Shanghai daoning brings you leanqbd introduction, trial and tutorial
locust 系列入门
mysql 学习笔记-优化之SQL优化
Pytest Collection (2) - mode de fonctionnement pytest
Aidl basic use
九章云极DataCanvas公司蝉联中国机器学习平台市场TOP 3
随机推荐
require与import的区别和使用
Aidl basic use
Can I choose to open an account for stock trading on flush? Is it safe?
Classify boost libraries by function
月入1W+的自媒体达人都会用到的运营工具
【商业终端仿真解决方案】上海道宁为您带来Georgia介绍、试用、教程
PMP证书真的有用吗?
MySQL series transaction log redo log learning notes
Communication between browser tab pages
Significance and measures of security encryption of industrial control equipment
按照功能对Boost库进行分类
ngnix基础知识
收到一封CTO来信,邀约面试机器学习工程师
业务可视化-让你的流程图'Run'起来
Can you get a raise? Analysis on gold content of PMP certificate
K-means based user portrait clustering model
地图其他篇总目录
Smart micro mm32 multi-channel adc-dma configuration
[intelligent QBD risk assessment tool] Shanghai daoning brings you leanqbd introduction, trial and tutorial
PHP reflective XSS, reflective XSS test and repair