当前位置:网站首页>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 .
边栏推荐
- 详解JMM
- 指标陷阱:IT领导者易犯的七个KPI错误
- 13th Blue Bridge Cup group B national tournament
- Icml2022 | interventional contrastive learning based on meta semantic regularization
- LC501. 二叉搜索树中的众数
- 详解Kubernetes网络模型
- Separate the letters and numbers in the string so that the letters come first and the array comes last
- The difference between NiO and traditional IO
- Aidl basic use
- Training on the device with MIT | 256Kb memory
猜你喜欢
随机推荐
Unity 使用Sqlite
黑马程序员-软件测试--06阶段2-linux和数据库-01-08第一章-linux操作系统阶段内容说明,linux命令基本格式以及常见形式的说明,操作系统的常见的分类,查看命令帮助信息方法,
Design and practice of new generation cloud native database
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
Gaussdb (DWS) active prevention and troubleshooting
Mask wearing detection method based on yolov5
IDA动态调试apk
MySQL series transaction log redo log learning notes
【单体】流辰信息I-BPSv3服务器推荐配置
flink sql 命令行 连接 yarn
焱融看 | 混合云时代下,如何制定多云策略
Smart micro mm32 multi-channel adc-dma configuration
BlocProvider 为什么感觉和 Provider 很相似?
Sonic cloud real machine learning summary 6 - 1.4.1 server and agent deployment
Using closures to switch toggle by clicking a button
Indicator trap: seven KPI mistakes that it leaders are prone to make
CNN convolution neural network principle explanation + image recognition application (with source code) [easy to understand]
二叉树的基本操作
Pytorch sharpening chapter | argmax and argmin functions
MySQL的视图练习题