当前位置: 首页 > news >正文

如何将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

到此大功告成!

http://www.lryc.cn/news/338037.html

相关文章:

  • 最新Zibll子比主题V7.1版本源码 全新推出开心版
  • 响应式布局(其次)
  • arhtas idea plugin 使用手册
  • 数组算法——查询位置
  • 【解决leecode打不开的问题】使用chrome浏览器和其他浏览器均打不开leecode
  • 尝试在手机上运行google 最新开源的gpt模型 gemma
  • 56、巴利亚多利德大学、马德里卡洛斯三世研究所:EEG-Inception-多时间尺度与空间卷积巧妙交叉堆叠,终达SOTA!
  • ORA-00600: internal error code, arguments: [krbcbp_9]
  • uni-app实现分页--(2)分页加载,首页下拉触底加载更多
  • 前端工程化理解 (2024 面试题)
  • 10 Php学习:循环
  • FreeSWITCH 1.10.10 简单图形化界面17 - ubuntu22.04或者debian12 安装FreeSWITCH
  • ZStack Cloud 5.0.0正式发布——Vhost主存储、隔离PVLAN网络、云平台报警优化、灰度升级增强四大亮点简析
  • 商标没有去注册有哪些不好的影响!
  • 【小程序】常用方法、知识点汇总1
  • AugmentedReality之路-平面检测(5)
  • MQ:延迟队列
  • Element ui 动态展示表格列,动态格式化表格列的值
  • xxl-job调度任务原理解析
  • 实验2 路由器基本配置
  • docker部署安装整理
  • 为什么你明明拥有5年开发经验,但是依然写不出来一份简历?
  • 【ZZULIOJ】1062: 最大公约数(Java)
  • 北斗导航 | ARAIM算法的原理和性能测试
  • elasticsearch7安全配置--最低安全等级,用户名密码
  • 项目架构MVC,DDD学习
  • SQLite的PRAGMA 声明
  • 使用ArrayList.removeAll(List list)导致的机器重启
  • 如何在项目中使用uni-ui组件库
  • redis的过期策略和内存淘汰机制(redis篇)