仓库已不再积极维护并将废弃,新项目建议迁移到 tf-onnx;安装脚本不包含 TensorFlow,需自行安装且要求 TensorFlow 2.8.0,1.x 不再支持;通过 pip 安装 ONNX 时需确保系统有 protoc 可用
onnx-tensorflow
onnx活跃维护把 ONNX 模型转成 TensorFlow 格式,方便在 TensorFlow 生态里直接加载和运行其他框架训练好的模型。
Tensorflow Backend for ONNX
pip install onnx-tf
onnx-tf convert -i /path/to/input.onnx -o /path/to/outputREADME 未说明具体显存或 GPU 型号要求;TensorFlow 版本由用户自行选择 CPU 或 GPU 版本,测试提示需要较多硬件资源
把 ONNX 模型转成 TensorFlow 模型并执行推理,适合需要复用 TF 生态的工程师
把 ONNX 模型转成 TensorFlow 格式,方便在 TensorFlow 生态里直接加载和运行其他框架训练好的模型。
核心能力
- 用 onnx-tf convert 命令行把 ONNX 模型转为 TensorFlow 表示
- 提供 prepare(onnx_model).run(input) 的 Python 推理接口
- 转换后交由 TensorFlow 执行,复用 TF 运行时
- 支持 ONNX Model Zoo 模型转换验证
- 提供算子覆盖状态文档供评估兼容性
容易踩的坑
- v1.10.0最新正式版2022-03-17 · 2022-03-17 15:41 UTC
Major changes and updates since v1.9.0 release:
Support ONNX opset 15
All Opset 15 operators are supported or partially supproted, please refer to support_status_v1_10_0.md for details.TensorFlow update
Supported TensorFlow version is updated to 2.8.0.Bug fixes
Several bug fixes are included. - v1.9.02021-08-24 · 2021-08-24 01:08 UTC
查看更新说明
Change Log
Major changes and updates since v1.8.0 release:
Support ONNX opset 14
- All Opset 14 operators are supported or partially supproted, please refer to support_status_v1_9_0.md for details.
TensorFlow update
- Supported TensorFlow version is updated to 2.6.0.
Python update
- Supproted Python version are updated to 3.8 and 3.9.
Support Loading ONNX Model with External Data
- Support Loading ONNX Model with External Data. Refer to Command Line Interface Documentation for details.
Bug fixes
- Several bug fixes are included.
- v1.8.02021-04-13 · 2021-04-13 21:29 UTC
查看更新说明
Change Log
Major changes and updates since v1.7.0 release:
Support ONNX opset 13
- All Opset 13 operators are supported except training ops, please refer to support_status_v1_8_0.md for details.
Model zoo verification
- We now run model zoo conversion and inference verification in CI.
- The verification results are published to wiki, https://github.com/onnx/onnx-tensorflow/wiki/ModelZoo-Status-(branch=master)
Inference graph based training and examples
- We added a new training option so that an ONNX model can be trained in Tensorflow simply based on the inference graph.
- A couple of examples are provided in https://github.com/onnx/onnx-tensorflow/blob/master/example/train_onnx_model.py
Update CI
- Moved CI from travis to Github actions
- Added a new automated test for all ONNX model zoo models
Special notes
- Tensorflow 1.x is no longer supported starting ONNX-TF 1.8.0.
- v1.7.02020-11-24 · 2020-11-24 18:06 UTC
查看更新说明
Change Log
Major changes and updates since v1.6.0 release:
Export model in SavedModel format
- API: “onnx_tf.backend_rep.TensorflowRep.export_graph” and CLI: “convert” will create a TensorFlow SavedModel for user to deploy it in TensorFlow.
Auto data type cast to support data types that are not natively supported by TensorFlow
- User can set auto_cast=True in API: “onnx_tf.backend.prepare” or CLI: “convert” to enable this auto_cast feature.
Convert model to run in a GPU or CPU environment base on user input
- User can set device=’CPU’(default) or device=’CUDA’ in API: “onnx_tf.backend.prepare” or CLI: “convert” to set the model inferencing environment.
Support Opset 12 operators
- All Opset 12 operators are supported except training ops, please refer to support_status_v1_7_0.md for details.
Create graph using tf.function(recommended in tf-2.x) instead of tf.Graph(deprecated in tf-2.x)
- Used tf.Module as the base class of the converted model
- Used tf.function to generate the graph automatically
Define a template to compare inference result with other backend
- Added a model stepping test for MNIST model to compare inference result with ONNX runtime.
Update CI
- Migrated travis CI from travis.org to travis.com.
- Updated CI to skip unsupported operators and allow failure against ONNX latest master branch.
- v1.6.02020-07-23 · 2020-07-23 22:44 UTC
查看更新说明
Change Log
Major changes and updates since v1.5.0 release:
Support Tensorflow 2.x and 1.15
- For production, please use onnx-tf PyPi package for Tensorflow 2.x conversion and use tag v1.6.0-tf-1.15 to build a package from source for Tensorflow 1.15 conversion.
- For development, please use the master branch for Tensorflow 2.x conversion, and use tf-1.x branch for Tensorflow 1.15 conversion.
- tf-1.x branch is for users who cannot upgrade to Tensorflow 2.x yet. This branch will only support up to ONNX OpSet 12 operators. If any user needs to use operators in OpSet 13 or above, please upgrade Tensorflow to 2.x and use the master branch of this repo. By January 1st, 2021 this branch will switch to maintenance mode only, no new development will be added into this branch from then on.
Support dynamic shape inputs
- Added test_dynamic_shape.py to verify the handlers can process dynamic shape inputs.
Support OpSet 11 operators
- Please refer to support_status_v1_6_0.md for all supported OpSet 11 operators.
Python 2.7 support is deprecated
用户点评
登录用户的真实使用记录,可按版本和硬件筛选还没有人点评。第一个说出真实体验的人最有价值。
TensorFlow Backend for ONNX
Note this repo is not actively maintained and will be deprecated. If you are interested in becoming the owner, please contact the ONNX Steering Committee (https://github.com/onnx/steering-committee).
Open Neural Network Exchange (ONNX) is an open standard format for representing machine learning models. ONNX is supported by a community of partners who have implemented it in many frameworks and tools.
TensorFlow Backend for ONNX makes it possible to use ONNX models as input for TensorFlow. The ONNX model is first converted to a TensorFlow model and then delegated for execution on TensorFlow to produce the output.
This is one of the two TensorFlow converter projects which serve different purposes in the ONNX community:
- onnx-tensorflow converts ONNX models to Tensorflow
- tf2onnx converts Tensorflow models to ONNX
Converting Models from ONNX to TensorFlow
Use CLI
Command Line Interface Documentation
From ONNX to TensorFlow: onnx-tf convert -i /path/to/input.onnx -o /path/to/output
Convert Programmatically
Migrating from onnx-tf to tf-onnx
We have joined force with Microsoft to co-develop ONNX TensorFlow frontend. For current onnx-tf frontend users, please migrate to use tf-onnx (https://github.com/onnx/tensorflow-onnx) where our code had been merged into.
ONNX Model Inference with TensorFlow Backend
import onnx
from onnx_tf.backend import prepare
onnx_model = onnx.load("input_path") # load onnx model
output = prepare(onnx_model).run(input) # run the loaded model
More Tutorials
Running an ONNX model using TensorFlow
Production Installation
ONNX-TF requires ONNX (Open Neural Network Exchange) as an external dependency, for any issues related to ONNX installation, we refer our users to ONNX project repository for documentation and help. Notably, please ensure that protoc is available if you plan to install ONNX via pip.
The specific ONNX release version that we support in the master branch of ONNX-TF can be found here. This information about ONNX version requirement is automatically encoded in setup.py, therefore users needn't worry about ONNX version requirement when installing ONNX-TF.
To install the latest version of ONNX-TF via pip, run pip install onnx-tf.
Because users often have their own preferences for which variant of TensorFlow to install (i.e., a GPU version instead of a CPU version), we do not explicitly require tensorflow in the installation script. It is therefore users' responsibility to ensure that the proper variant of TensorFlow is available to ONNX-TF. Moreover, we require TensorFlow version == 2.8.0.
Development
Coverage Status
ONNX-TensorFlow Op Coverage Status
API
Installation
- Install ONNX master branch from source.
- Install TensorFlow >= 2.8.0, tensorflow-probability and tensorflow-addons. (Note TensorFlow 1.x is no longer supported)
- Run
git clone https://github.com/onnx/onnx-tensorflow.git && cd onnx-tensorflow. - Run
pip install -e ..
Folder Structure
- onnx_tf: main source code file.
- test: test files.
Code Standard
- Format code
pip install yapf
yapf -rip --style="{based_on_style: google, indent_width: 2}" $FilePath$
- Install pylint
pip install pylint
wget -O /tmp/pylintrc https://raw.githubusercontent.com/tensorflow/tensorflow/master/tensorflow/tools/ci_build/pylintrc
- Check format
pylint --rcfile=/tmp/pylintrc myfile.py
Documentation Standard
Google Style Python Docstrings
Testing
Unit Tests
To perfom unit tests:
pip install pytest tabulate
python -m unittest discover test
Note: Only the ONNX backend tests found in test_onnx_backend.py require the pytest and tabulate packages.
Testing requires significant hardware resources, but nonetheless, we highly recommend that users run through the complete test suite before deploying onnx-tf. The complete test suite typically takes between 15 and 45 minutes to complete, depending on hardware configurations.
Model Zoo Tests
The tests in test_modelzoo.py verify whether the ONNX Model Zoo models can be successfully validated against the ONNX specification and converted to a TensorFlow representation. Model inferencing on the converted model is not tested currently.
Prerequisites
The model zoo uses Git LFS (Large File Storage) to store ONNX model files. Make sure that Git LFS is installed on your operating system.
Running
By default, the tests assume that the model zoo repository has been cloned into this project directory. The model zoo directory is scanned for ONNX models. For each model found: download the model, convert the model to TensorFlow, generate a test status, and delete the model. By default, the generated test report is created in the system temporary directory. Run python test/test_modelzoo.py -h for help on command line options.
git clone https://github.com/onnx/models
python test/test_modelzoo.py
Testing all models can take at least an hour to complete, depending on hardware configuration and model download times. If you expect to test some models frequently, we recommend using Git LFS to download those models before running the tests so the large files are cached locally.
Reports
When making code contributions, the model zoo tests are run when a commit is merged. Generated test reports are published on the onnx-tensorflow wiki.