当前位置:网站首页>H5 model trained by keras to tflite
H5 model trained by keras to tflite
2022-07-01 22:34:00 【I am a symmetric matrix】
1、 Use transcoding
Training use tf1.13.1 and keras2.0
transformation tflite The environment is tf1.13.1, Close test available
The code is as follows :
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、 Use tflite_convert Tools
I haven't tried yet : Use tflite_convert The command tool will keras h5 The file is converted to tflite Easy guide to documentation
This tutorial is divided into two steps , First the h5 transformation ( By code API transformation ) by pb Model , then pb Model transformation ( the tflite_convert Tool conversion ) by tflite
3、 Personal test use tflite_convert Tools
Model training uses tf1.13.1 and keras2.0
transformation tflite The environment is tf1.13.1
3.1 brief introduction
tflite_convert Is a command line tool , You can take a look at will pb Model transfer tflite The general process of :TF1.x and TF2.x Version trained PB Model transfer Tflite. Found that we are right keras The model is converted to tflite You need to know h5 Information about the model :
usage: tflite_convert --output_file OUTPUT_FILE # The output path
--keras_model_file KERAS_MODEL_FILE # keras Of h5 Model file path
--output_format TFLITE # Output format , Designated as TFLITE( Fix )
--input_arrays INPUT_ARRAYS # Enter the node name
--input_shapes INPUT_SHAPES # Enter shape
--output_arrays OUTPUT_ARRAYS # Output node name
Now let's talk about input_arrays 、input_shapes、output_arrays
How to get
3.2 Model information acquisition
Give the code directly :
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])
Program output :
# It can be seen that shape, stay tflite_convert Designated as 1,64,64,1
inputs info [<tf.Tensor 'input_1:0' shape=(?, 64, 64, 1) dtype=float32>]
# It can be seen that the input node name is input_1
inputs name ['input_1']
# You can see that the name of the output node is predictions/Softmax
output name ['predictions/Softmax']
In fact, you can directly print(net.inputs)
and print(net.outputs)
, Output is :0
It takes into account the multi input and multi output models , But in fact, this model is single input and single output , So that is 'input_1'
and 'predictions/Softmax'
3.3 transformation
Execute on the command line :
tflite_convert --output_file output.tflite # Specify the output path
--keras_model_file newdata_mini_XCEPTION.123-0.70.hdf5 # Specify the model to convert
--input_arrays input_1 # Specify the input node name
--input_shapes 1,64,64,1 # Input shape
--output_arrays predictions/Softmax # Output node name
--output_format TFLITE # TFLITE Pattern
If no error is reported , And it exists output.tflite
file , The conversion should be successful output.tflite
It should be stored in the directory where you execute the command line , Or specify the absolute path when executing the command , Easy to find output.tflite file .
边栏推荐
- Interview question: what is the difference between MySQL's Union all and union, and how many join methods MySQL has (Alibaba interview question) [easy to understand]
- 按照功能对Boost库进行分类
- Pytorch sharpening chapter | argmax and argmin functions
- Smart micro mm32 multi-channel adc-dma configuration
- 【juc学习之路第9天】屏障衍生工具
- PyTorch磨刀篇|argmax和argmin函数
- #yyds干货盘点# 解决名企真题:扭蛋机
- MySQL系列之事务日志Redo log学习笔记
- MySQL之MHA高可用配置及故障切换
- PHP reflective XSS, reflective XSS test and repair
猜你喜欢
Recent public ancestor offline practice (tarjan)
Basic operation of binary tree
ICML2022 | 基于元语义正则化的介入性对比学习
Relationship and difference between enterprise architecture and project management
[commercial terminal simulation solution] Shanghai daoning brings you Georgia introduction, trial and tutorial
黑马程序员-软件测试--06阶段2-linux和数据库-01-08第一章-linux操作系统阶段内容说明,linux命令基本格式以及常见形式的说明,操作系统的常见的分类,查看命令帮助信息方法,
Mysql——》索引存储模型推演
100年仅6款产品获批,疫苗竞争背后的“佐剂”江湖
Introduction and download of the latest version of airserver2022
详解JMM
随机推荐
20220701
Using closures to switch toggle by clicking a button
[deep learning] use deep learning to monitor your girlfriend's wechat chat?
Little p weekly Vol.11
【JetCache】JetCache的使用方法与步骤
从零开始学 MySQL —数据库和数据表操作
Clean up system cache and free memory under Linux
Mysql——》MyISAM存储引擎的索引
What is the difference between PMP and NPDP?
Use of vscode
EasyExcel 复杂数据导出
Smart micro mm32 multi-channel adc-dma configuration
详解Volatile关键字
首席信息官对高绩效IT团队定义的探讨和分析
String type conversion BigDecimal, date type
plantuml介绍与使用
keras训练的H5模型转tflite
GaussDB(DWS)主动预防排查
Clean up system cache and free memory under Linux
LIS (longest ascending subsequence) problem that can be understood [easy to understand]