你如何将 .onnx 转换为 tflite? [英] How do you convert a .onnx to tflite?

查看:267
本文介绍了你如何将 .onnx 转换为 tflite?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已通过以下方式将模型导出到 ONNX:

#导出模型torch_out = torch.onnx._export(learn.model, # 正在运行的模型x, # 模型输入(或多个输入的元组)EXPORT_PATH + "mnist.onnx", # 模型的保存位置(可以是文件或类文件对象)export_params=True) # 将训练好的参数权重存储在模型文件中

现在我正在尝试将模型转换为 Tensorflow Lite 文件,以便我可以在 Android 上进行推理.不幸的是,PyTorch/Caffe2 对 Android 的支持相当缺乏或过于复杂,但 Tensorflow 看起来要简单得多.

关于 ONNX 到 Tflite 的文档对此非常简单.

我尝试通过以下方式导出到 Tensorflow GraphDef proto:

tf_rep.export_graph(EXPORT_PATH + 'mnist-test/mnist-tf-export.pb')

然后运行toco:

toco --graph_def_file=mnist-tf-export.pb --input_format=TENSORFLOW_GRAPHDEF --output_format=TFLITE --inference_type=FLOAT --input_type=浮动--input_arrays=0 --output_arrays=add_10 --input_shapes=1,3,28,28 --output_file=mnist.tflite`

当我这样做时,虽然我收到以下错误:

文件anaconda3/lib/python3.6/site-packages/tensorflow/lite/python/convert.py",第172行,在toco_convert_protos"TOCO 失败.有关信息,请参阅控制台.
%s
%s
" % (stdout, stderr))tensorflow.lite.python.convert.ConverterError: TOCO 失败.有关信息,请参阅控制台.2018-11-06 16:28:33.864889: I tensorflow/lite/toco/import_tensorflow.cc:1268] 转换不支持的操作:PyFunc2018-11-06 16:28:33.874130:F tensorflow/lite/toco/import_tensorflow.cc:114] 检查失败:attr.value_case() == AttrValue::kType(1 对 6)

此外,即使我运行命令时,我也不知道要为 input_arrays 或 output_arrays 指定什么,因为模型最初是在 PyTorch 中构建的.

有没有人成功地将他们的 ONNX 模型转换为 TFlite?

这是我要转换的 ONNX 文件:https:///drive.google.com/file/d/1sM4RpeBVqPNw1WeCROpKLdzbSJPWSK79/view?usp=sharing

额外信息

  • Python 3.6.6 :: Anaconda 自定义(64 位)
  • onnx.版本 = '1.3.0'
  • tf.version = '1.13.0-dev20181106'
  • torch.version = '1.0.0.dev20181029'

解决方案

我认为您提供的 ONNX 文件,即 model.onnx 已损坏我不知道是什么问题,但它未对 ONNX 运行时进行任何推断.

<块引用>

现在您可以直接在手机上运行 PyTorch 模型.查看 PyTorch Mobile 的文档此处

<块引用>

此答案适用于 TensorFlow 版本 1,
对于 TensorFlow 版本 2 或更高版本,请点击链接强>

将模型从 protobuf freezeGraph 转换为 TFlite 的最佳方法是使用官方的 TensorFlow lite 转换器 文档

根据 TensorFlow Docs,TocoConverter 有已弃用

<块引用>

该类 (tf.compat.v1.lite.TocoConverter) 已被弃用.请改用 lite.TFLiteConverter.

从 PyTorch 转换为 ONNX 模型

将模型从 Pytorch 转换为 Onnx 的最佳实践是您应该添加以下参数来指定模型的输入和输出层的名称torch.onnx.export() 函数

# 将模型从 PyTorch 导出到 ONNXtorch_out = torch.onnx._export(模型,# 正在运行的模型x, # 模型输入(或多个输入的元组)EXPORT_PATH + "mnist.onnx", # 模型的保存位置(可以是文件或类文件对象)export_params=True, # 将训练好的参数权重存储在模型文件中input_names=['main_input'], #指定onnx模型中输入层的名称output_names=['main_output']) # 指定onnx模型中输入层的名称

所以在你的情况下:现在使用 onnx-tf

将此模型导出到 TensorFlow protobuf FreezeGraph<块引用>

请注意,此方法仅在 tensorflow_version <2

从 ONNX 转换为 TensorFlow freezGraph

要转换模型,请从以下命令安装 onnx-tf 1.5.0 版

pip install onnx-tf==1.5.0

现在将 .onnx 模型转换为 TensorFlow 冻结图,在 shell 中运行以下命令

onnx-tf convert -i "mnist.onnx";-o "mnist.pb";

从 TensorFlow FreezeGraph .pb 转换为 TF

现在将此模型从 .pb 文件转换为 tflite 模型,使用此代码

将tensorflow导入为tf# 从保存的 tensorflow 文件中创建一个转换器对象转换器 = tf.lite.TFLiteConverter.from_frozen_graph('mnist.pb', #TensorFlow freezegraph .pb 模型文件input_arrays=['main_input'], # 之前在torch.onnx.export 函数中定义的输入数组的名称.output_arrays=['main_output'] # 之前在torch.onnx.export 函数中定义的输出数组的名称.)# 告诉转换器使用哪种类型的优化技术Converter.optimizations = [tf.lite.Optimize.DEFAULT]# 要查看优化的最佳选项,请阅读 tflite 关于优化的文档# 转到此链接 https://www.tensorflow.org/lite/guide/get_started#4_optimize_your_model_optional# 转换模型tf_lite_model = converter.convert()# 保存转换后的模型打开('mnist.tflite','wb').写(tf_lite_model)

要为您的模型用例选择最适合优化的选项,请参阅有关 TensorFlow lite 优化的官方指南

https://www.tensorflow.org/lite/guide/get_started#4_optimize_your_model_optional

<块引用>

注意:您可以在 Google Colaboratory 链接

I've exported my model to ONNX via:

# Export the model
torch_out = torch.onnx._export(learn.model,             # model being run
                           x,                       # model input (or a tuple for multiple inputs)
                          EXPORT_PATH + "mnist.onnx", # where to save the model (can be a file or file-like object)
                           export_params=True)      # store the trained parameter weights inside the model file

And now I am trying to convert the model to a Tensorflow Lite file so that I can do inference on Android. Unfortunately, PyTorch/Caffe2 support is fairly lacking or too complex for Android but Tensorflow appears much simpler.

The documentation for ONNX to Tflite is pretty light on this.

I've tried exporting to a Tensorflow GraphDef proto via:

tf_rep.export_graph(EXPORT_PATH + 'mnist-test/mnist-tf-export.pb')

And then running toco:

toco 
--graph_def_file=mnist-tf-export.pb 
--input_format=TENSORFLOW_GRAPHDEF 
--output_format=TFLITE 
--inference_type=FLOAT 
--input_type=FLOAT 
--input_arrays=0 
--output_arrays=add_10 
--input_shapes=1,3,28,28 
--output_file=mnist.tflite`

When I do though I get the following error:

File "anaconda3/lib/python3.6/site-packages/tensorflow/lite/python/convert.py", line 172, in toco_convert_protos
    "TOCO failed. See console for info.
%s
%s
" % (stdout, stderr))
tensorflow.lite.python.convert.ConverterError: TOCO failed. See console for info.
2018-11-06 16:28:33.864889: I tensorflow/lite/toco/import_tensorflow.cc:1268] Converting unsupported operation: PyFunc
2018-11-06 16:28:33.874130: F tensorflow/lite/toco/import_tensorflow.cc:114] Check failed: attr.value_case() == AttrValue::kType (1 vs. 6)

Further, even when I run the command I don't know what to specify for the input_arrays or output_arrays since the model was originally built in PyTorch.

Has anyone successfully converted their ONNX model to TFlite?

Here's the ONNX file I'm trying to convert: https://drive.google.com/file/d/1sM4RpeBVqPNw1WeCROpKLdzbSJPWSK79/view?usp=sharing

Extra info

  • Python 3.6.6 :: Anaconda custom (64-bit)
  • onnx.version = '1.3.0'
  • tf.version = '1.13.0-dev20181106'
  • torch.version = '1.0.0.dev20181029'

解决方案

I think the ONNX file i.e. model.onnx that you have given is corrupted I don't know what is the issue but it is not doing any inference on ONNX runtime.

Now you can run PyTorch Models directly on mobile phones. check out PyTorch Mobile's documentation here

This answer is for TensorFlow version 1,
For TensorFlow version 2 or higher click link

The best way to convert the model from protobuf freezeGraph to TFlite is to use the official TensorFlow lite converter documentation

According to TensorFlow Docs, TocoConverter has been deprecated

This class (tf.compat.v1.lite.TocoConverter) has been deprecated. Please use lite.TFLiteConverter instead.

Convert from PyTorch to ONNX model

The best practice to convert the model from Pytorch to Onnx is that you should add the following parameters to specify the names of the input and output layer of your model in torch.onnx.export() function


# Export the model from PyTorch to ONNX
torch_out = torch.onnx._export(model,             # model being run
                                x,          # model input (or a tuple for multiple inputs)
                                EXPORT_PATH + "mnist.onnx",      # where to save the model (can be a file or file-like object)
                                export_params=True,       # store the trained parameter weights inside the model file
                                input_names=['main_input'],     # specify the name of input layer in onnx model
                                output_names=['main_output'])     # specify the name of input layer in onnx model

So in your case: Now export this model to TensorFlow protobuf FreezeGraph using onnx-tf

Please note that this method is only working when tensorflow_version < 2

Convert from ONNX to TensorFlow freezGraph

To convert the model please install onnx-tf version 1.5.0 from the below command

pip install  onnx-tf==1.5.0

Now to convert .onnx model to TensorFlow freeze graph run this below command in shell

onnx-tf convert -i "mnist.onnx" -o  "mnist.pb"

Convert from TensorFlow FreezeGraph .pb to TF

Now to convert this model from .pb file to tflite model use this code

import tensorflow as tf
# make a converter object from the saved tensorflow file
converter = tf.lite.TFLiteConverter.from_frozen_graph('mnist.pb', #TensorFlow freezegraph .pb model file
                                                      input_arrays=['main_input'], # name of input arrays as defined in torch.onnx.export function before.
                                                      output_arrays=['main_output']  # name of output arrays defined in torch.onnx.export function before.
                                                      )
# tell converter which type of optimization techniques to use
converter.optimizations = [tf.lite.Optimize.DEFAULT]
# to view the best option for optimization read documentation of tflite about optimization
# go to this link https://www.tensorflow.org/lite/guide/get_started#4_optimize_your_model_optional

# convert the model 
tf_lite_model = converter.convert()
# save the converted model 
open('mnist.tflite', 'wb').write(tf_lite_model)

To choose which option is best for optimization for your model use case see this official guide about TensorFlow lite optimization

https://www.tensorflow.org/lite/guide/get_started#4_optimize_your_model_optional

Note: You can try my Jupyter Notebook Convert ONNX model to Tensorflow Lite on Google Colaboratory link

这篇关于你如何将 .onnx 转换为 tflite?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆