将ONNX模型转换为TensorFlow Lite

乔尔:

我有一些用于ONNX Model Zoo的模型我想在TensorFlow Lite(Android)应用程序中使用此处的模型,但是在弄清楚如何转换模型时遇到了问题。

从我的阅读中,我需要遵循的过程是将ONNX模型转换为TensorFlow模型,然后将该TensorFlow模型转换为TensorFlow Lite模型。

import onnx
from onnx_tf.backend import prepare
import tensorflow as tf

onnx_model = onnx.load('./some-model.onnx') 
tf_rep = prepare(onnx_model)
tf_rep.export_graph("some-model.pb") 

执行完上述操作后,我有一个文件some-model.pb,我相信其中包含一个TensorFlow冻结图。从这里我不确定要去哪里。当我搜索时,我发现很多针对TensorFlow 1.x的答案(只有在发现示例后我才意识到执行该答案)。我正在尝试使用TensorFlow 2.x.

如果有关系,这里是我要开始使用的特定模型

根据ReadMe.md,输入形状为(1x3x416x416),输出形状为(1x125x13x13)。

乔尔:

我知道了 我可以使用下面的代码完成转换。

import tensorflow as tf
converter = tf.compat.v1.lite.TFLiteConverter.from_frozen_graph('model.pb', #TensorFlow freezegraph 
                                                  input_arrays=['input.1'], # name of input
                                                  output_arrays=['218']  # name of output
                                                  )
converter.target_spec.supported_ops = [tf.lite.OpsSet.TFLITE_BUILTINS,
                                   tf.lite.OpsSet.SELECT_TF_OPS]      
# tell converter which type of optimization techniques to use
converter.optimizations = [tf.lite.Optimize.DEFAULT]
tf_lite_model = converter.convert()
open('model.tflite', 'wb').write(tf_lite_model)

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

为量化的Tensorflow Lite模型创建位图ByteBuffer

如何知道Tensorflow Lite模型的输入/输出功能信息?

Tensorflow Lite模型输出错误

TF-Lite:将pb模型转换为tflite后,PReLU无法正常工作

如何将TensorFlow Lite模型量化为16位

将模型转换为Tensorflow-lite时出错

在Hexagon DSP上运行Tensorflow Lite演示模型

Tensorflow在Windows中使用python将.pb文件转换为.lite文件

将Tensorflow模型转换为Lite版本时出现问题

我可以在Tensorflow Lite模型上获取指标吗?

如何将Onnx模型(.onnx)转换为Tensorflow(.pb)模型

Android分类应用程序因Tensorflow Lite模型崩溃

Tensorflow js VS Tensorflow Lite

问题将字符串转换为日期vega lite

如何验证从Keras转换的Tensorflow-Lite模型

如何从Tensorflow.js(.json)模型转换为Tensorflow(SavedModel)或Tensorflow Lite(.tflite)模型?

将已保存的Tensorflow模型转换为Tensorflow Lite的正确方法是什么

将Tensorflow转换为Tensorflow-lite

在 TensorFlow Lite 中运行 Keras 模型时的不同预测

使用 Keras Functional API 为 Tensorflow LITE 构建模型

如何创建可轻松转换为 TensorFlow Lite 的模型?

带有 NativeScript 的 Tensorflow lite

使用 TF-lite 将 MobileFacenet 转换为量化感知模型时,create_training_graph() 失败

将tensorflow lite模型直接导入tensorflow/keras

错误:Tensorflow 预处理层未转换为 Tensorflow lite

Tensorflow Lite 模型输出比 Tensorflow 模型更大的值

在 python 中加载 Tensorflow Lite 模型

如何将python中的KNN Scikit-learn模型转换为tensorflow lite模型?

如何基于模型 Tensorflow lite 进行预测?