当前位置:网站首页>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 .
边栏推荐
- Introduction and download of the latest version of airserver2022
- 地图其他篇总目录
- 按照功能对Boost库进行分类
- Matlab traverses images, string arrays and other basic operations
- The leader of the cloud native theme group of beacon Committee has a long way to go!
- One of the basic learning of function
- LC501. 二叉搜索树中的众数
- 【直播回顾】战码先锋首期8节直播完美落幕,下期敬请期待!
- Mysql——》MyISAM存储引擎的索引
- List announced | outstanding intellectual property service team in China in 2021
猜你喜欢

CIO's discussion and Analysis on the definition of high-performance it team

高攀不起的希尔排序,直接插入排序

EasyExcel 复杂数据导出

Little p weekly Vol.11

AirServer2022最新版功能介绍及下载

Redis配置与优化

keras训练的H5模型转tflite
![[deep learning] use deep learning to monitor your girlfriend's wechat chat?](/img/03/ecf50eacc91c0633b0d9689cdad2c2.png)
[deep learning] use deep learning to monitor your girlfriend's wechat chat?

I received a letter from CTO inviting me to interview machine learning engineer

Recent public ancestor offline practice (tarjan)
随机推荐
MQ learning notes
MySQL series transaction log redo log learning notes
Classify boost libraries by function
Aidl basic use
Simple interactive operation of electron learning (III)
按照功能对Boost库进行分类
【目标跟踪】|单目标跟踪指标
高攀不起的希尔排序,直接插入排序
awoo‘s Favorite Problem(优先队列)
flink sql-client 使用 对照并熟悉官方文档
Test cancellation 1
Matlab traverses images, string arrays and other basic operations
91.(cesium篇)cesium火箭发射模拟
为什么数字化转型战略必须包括持续测试?
3DE 资源没东西或不对
Sonic云真机学习总结6 - 1.4.1服务端、agent端部署
详解LockSupport的使用
MySQL learning notes - SQL optimization of optimization
Can you get a raise? Analysis on gold content of PMP certificate
#yyds干货盘点# 解决名企真题:扭蛋机