当前位置:网站首页>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 .
边栏推荐
- 为什么数字化转型战略必须包括持续测试?
- Business visualization - make your flowchart'run'up
- Icml2022 | interventional contrastive learning based on meta semantic regularization
- MySQL的存储过程
- Is PMP certificate really useful?
- Application of real estate management based on 3D GIS
- Communication between browser tab pages
- 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
- Basic knowledge of ngnix
- Recent public ancestor (LCA) online practices
猜你喜欢
随机推荐
Separate the letters and numbers in the string so that the letters come first and the array comes last
Spark interview questions
LC501. 二叉搜索树中的众数
Ida dynamic debugging apk
Mask wearing detection method based on yolov5
The correct way to set the bypass route
K-means based user portrait clustering model
互联网的智算架构设计
三翼鸟两周年:羽翼渐丰,腾飞指日可待
配置筛选机
Fundamentals - IO intensive computing and CPU intensive computing
PHP reflective XSS, reflective XSS test and repair
Why does blocprovider feel similar to provider?
List announced | outstanding intellectual property service team in China in 2021
【juc学习之路第9天】屏障衍生工具
Unity uses SQLite
业务可视化-让你的流程图'Run'起来
Getting started with the lockust series
按照功能对Boost库进行分类
园区全光技术选型-中篇









