当前位置:网站首页>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 .
边栏推荐
- awoo‘s Favorite Problem(优先队列)
- Sonic云真机学习总结6 - 1.4.1服务端、agent端部署
- Recent public ancestor offline practice (tarjan)
- The correct way to set the bypass route
- 企业架构与项目管理的关联和区别
- Use of vscode
- 完全注解的ssm框架搭建
- Mysql——》MyISAM存储引擎的索引
- 互联网的智算架构设计
- [commercial terminal simulation solution] Shanghai daoning brings you Georgia introduction, trial and tutorial
猜你喜欢
随机推荐
mysql 学习笔记-优化之SQL优化
Mysql——》Innodb存储引擎的索引
"The silk road is in its youth and looks at Fujian" is in the hot collection of works in the Fujian foreign youth short video competition
News classification based on LSTM model
20220701
Count the number of each character in the character
[noip2013] building block competition [noip2018] road laying greed / difference
spark analyze命令使用及其作用 map join broadcast join 广播join
信标委云原生专题组组长,任重道远!
详解Kubernetes网络模型
Case of camera opening by tour
Spark interview questions
Icml2022 | interventional contrastive learning based on meta semantic regularization
Airserver mobile phone third-party screen projection computer software
为什么数字化转型战略必须包括持续测试?
从零开始学 MySQL —数据库和数据表操作
灵动微 MM32 多路ADC-DMA配置
记录一次spark on yarn 任务报错 Operation category READ is not supported in state standby
CIO's discussion and Analysis on the definition of high-performance it team
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









