本文主要是介绍如何将Paddle(Lite)模型转换为TensorFlow(Lite)模型,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
模型间的相互转换在深度学习应用中很常见,paddlelite和TensorFlowLite是移动端常用的推理框架,有时候需要将模型在两者之间做转换,本文将对转换方法做说明。
环境准备
建议使用TensorFlow2.14,PaddlePaddle 2.6
docker pull tensorflow/tensorflow:2.14.0
Step1:From Paddle to ONNX
直接参考https://github.com/PaddlePaddle/Paddle2ONNX/blob/develop/docs/zh/compile.md 源码编译Paddle2ONNX
然后执行
paddle2onnx --model_dir . --model_filename your.pdmodel --params_filename your.pdiparams --save_file model.onnx
会看到输出
[Paddle2ONNX] Start to parse PaddlePaddle model...
[Paddle2ONNX] Model file path: ./pdmodel.pdmodel
[Paddle2ONNX] Parameters file path: ./pdmodel.pdiparams
[Paddle2ONNX] Start to parsing Paddle model...
[Paddle2ONNX] [bilinear_interp_v2: bilinear_interp_v2_1.tmp_0] Requires the minimal opset version of 11.
[Paddle2ONNX] [pixel_shuffle: pixel_shuffle_1.tmp_0] Requires the minimal opset version of 11.
[Paddle2ONNX] [pixel_shuffle: pixel_shuffle_2.tmp_0] Requires the minimal opset version of 11.
[Paddle2ONNX] Due to the operator: bilinear_interp_v2, requires opset_version >= 11.
[Paddle2ONNX] Opset version will change to 11 from 9
[Paddle2ONNX] Use opset_version = 11 for ONNX export.
[Paddle2ONNX] PaddlePaddle model is exported as ONNX format now.
2024-04-09 11:55:50 [INFO] ===============Make PaddlePaddle Better!================
2024-04-09 11:55:50 [INFO] A little survey: https://iwenjuan.baidu.com/?code=r8hu2s
关于pdparams和pdiparams两种参数文件的区别,参考https://www.paddlepaddle.org.cn/documentation/docs/zh/faq/save_cn.html中的描述
Step2:From ONNX to TensorFlow
使用https://github.com/onnx/onnx-tensorflow
pip install tensorflow-addons
pip install tensorflow-probability==0.22.1
pip install onnx-tf
接下来
onnx-tf convert -i model.onnx -o model.pb
会看到输出
2024-04-09 07:03:32,346 - onnx-tf - INFO - Start converting onnx pb to tf saved model
2024-04-09 07:03:41,015 - onnx-tf - INFO - Converting completes successfully.
INFO:onnx-tf:Converting completes successfully.
在model.pb目录下可以看到saved_model.pb
Step3:From TensorFlow to tflite
参考https://www.tensorflow.org/lite/convert?hl=zh-cn 编写python脚本
import tensorflow as tf
# Convert the model
converter = tf.lite.TFLiteConverter.from_saved_model(saved_model_dir) # path to the SavedModel directory
tflite_model = converter.convert()# Save the model.
with open('model.tflite', 'wb') as f:f.write(tflite_model)
运行python脚本,会看到输出
2024-04-09 07:16:45.514656: W tensorflow/compiler/mlir/lite/python/tf_tfl_flatbuffer_helpers.cc:378] Ignored output_format.
2024-04-09 07:16:45.514767: W tensorflow/compiler/mlir/lite/python/tf_tfl_flatbuffer_helpers.cc:381] Ignored drop_control_dependency.
2024-04-09 07:16:45.515630: I tensorflow/cc/saved_model/reader.cc:83] Reading SavedModel from: .
2024-04-09 07:16:45.517291: I tensorflow/cc/saved_model/reader.cc:51] Reading meta graph with tags { serve }
2024-04-09 07:16:45.517352: I tensorflow/cc/saved_model/reader.cc:146] Reading SavedModel debug info (if present) from: .
2024-04-09 07:16:45.523781: I tensorflow/compiler/mlir/mlir_graph_optimization_pass.cc:382] MLIR V1 optimization pass is not enabled
2024-04-09 07:16:45.524480: I tensorflow/cc/saved_model/loader.cc:233] Restoring SavedModel bundle.
2024-04-09 07:16:45.543346: I tensorflow/cc/saved_model/loader.cc:217] Running initialization op on SavedModel bundle at path: .
2024-04-09 07:16:45.559402: I tensorflow/cc/saved_model/loader.cc:316] SavedModel load for tags { serve }; Status: success: OK. Took 43775 microseconds.
2024-04-09 07:16:45.584171: I tensorflow/compiler/mlir/tensorflow/utils/dump_mlir_util.cc:269] disabling MLIR crash reproducer, set env var `MLIR_CRASH_REPRODUCER_DIRECTORY` to enable.
2024-04-09 07:16:45.635201: I tensorflow/compiler/mlir/lite/flatbuffer_export.cc:2245] Estimated count of arithmetic op
到此大功告成!
这篇关于如何将Paddle(Lite)模型转换为TensorFlow(Lite)模型的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!