我已经通过以下方式将我的模型导出到ONNX:
# 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
现在,我试图将模型转换为Tensorflow Lite文件,这样我就可以在Android上进行推理了。不幸的是,PyTorch/Caffe2 2对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=FLOAT \
--input_arrays=0 \
--output_arrays=add_10 \
--input_shapes=1,3,28,28 \
--output_file=mnist.tflite`
但是,当我这样做时,会出现以下错误:
File "anaconda3/lib/python3.6/site-packages/tensorflow/lite/python/convert.py", line 172, in toco_convert_protos
"TOCO failed. See console for info.\n%s\n%s\n" % (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)
此外,即使在运行命令时,我也不知道该为input_arrays或output_arrays指定什么,因为模型最初是在PyTorch中构建的。
是否有人成功地将他们的ONNX模型转换成TFlite?
下面是我要转换的ONNX文件:https://drive.google.com/file/d/1sM4RpeBVqPNw1WeCROpKLdzbSJPWSK79/view?usp=sharing
额外信息
发布于 2019-10-27 01:50:02
我认为ONNX文件,即您给出的model.onnx
已损坏,我不知道问题是什么,但它没有对ONNX运行时进行任何推断。
现在,您可以直接在移动电话上运行PyTorch模型。查看PyTorch Mobile的文档这里 这个答案是TensorFlow版本1, For TensorFlow version 2或更高版本单击 链接
将模型从protobuf转换为TFlite的最佳方法是使用官方的TensorFlow lite转换器文档。
据TensorFlow 文档说,TocoConverter已经被否决了
这个类(tf.compat.v1.lite.TocoConverter)已被废弃。请改用lite.TFLiteConverter。
从PyTorch到ONNX模型的转换
将模型从torch.onnx.export() Pytorch转换为Onnx的最佳实践是添加以下参数,以指定模型的输入和输出层的名称
# 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
所以在您的例子中:现在使用onnx-tf将这个模型导出到TensorFlow protobuf中。
请注意,只有当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模型,请使用以下代码
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)
要为您的模型用例选择最适合优化的选项,请参阅关于TensorFlow lite优化的官方指南
注意:您可以尝试使用我的木星笔记本将ONNX转换为Tensorflow Lite上的Google对撞机链接。
发布于 2021-05-02 15:15:36
现在,您可以直接在移动电话上运行PyTorch模型。查看PyTorch Mobile的文档这里 这个答案是TensorFlow版本2或更高版本, For TensorFlow version 1单击 这里
将模型从protobuf转换为TFlite的最佳方法是使用官方的TensorFlow lite转换器文档。
据TensorFlow 文档说,TocoConverter已经被否决了
这个类(tf.compat.v1.lite.TocoConverter)已被废弃。请改用lite.TFLiteConverter。
从PyTorch到ONNX模型的转换
# 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
)
所以在您的例子中:现在使用onnx-tf将这个模型导出到TensorFlow protobuf中。
从ONNX转换到TensorFlow freezeGraph
要从下面的命令转换模型,请安装onnx-tf
git clone https://github.com/onnx/onnx-tensorflow.git && cd onnx-tensorflow
pip install -e .
现在,要将.onnx模型转换为TensorFlow冻结图,请在shell中运行以下命令
onnx-tf convert -i "mnist.onnx" -o "mnist.pb"
从TensorFlow FreezeGraph .pb转换为TF
现在,要将此模型从.pb文件转换为tflite模型,请使用以下代码
import tensorflow as tf
# make a converter object from the saved tensorflow file
converter = tf.lite.TFLiteConverter.from_saved_model('mnist.pb')
# 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)
要为您的模型用例选择最适合优化的选项,请参阅关于TensorFlow lite优化的官方指南
发布于 2022-09-22 16:19:36
在中Google Colab
!pip install onnx2keras
import onnx
from onnx2keras import onnx_to_keras
onnx_model = onnx.load('model.onnx')
k_model = onnx_to_keras(onnx_model,['input'],change_ordering=True)
import tensorflow as tf
converter = tf.lite.TFLiteConverter.from_keras_model(k_model)
tflite_model = converter.convert()
# Save the model.
with open('model.tflite', 'wb') as f:
f.write(tflite_model)
https://stackoverflow.com/questions/53182177
复制相似问题