tf2onnx converts TensorFlow (tf-1.x or tf-2.x), keras, tensorflow.js and tflite models to ONNX via command line or python api.
Note: tensorflow.js support was just added. While we tested it with many tfjs models from tfhub, it should be considered experimental.
TensorFlow has many more ops than ONNX and occasionally mapping a model to ONNX creates issues.
You find a list of supported Tensorflow ops and their mapping to ONNX here.
The common issues we run into we try to document here Troubleshooting Guide.
Build Type | OS | Python | Tensorflow | ONNX opset | Status |
---|---|---|---|---|---|
Unit Test - Basic | Linux, MacOS*, Windows* | 3.6-3.9 | 1.12-1.15, 2.1-2.6 | 9-15 | |
Unit Test - Full | Linux, MacOS, Windows | 3.6-3.9 | 1.12-1.15, 2.1-2.6 | 9-15 |
tf2onnx will use the ONNX version installed on your system and installs the latest ONNX version if none is found.
We support and test ONNX opset-9 to opset-15. opset-6 to opset-8 should work but we don't test them.
By default we use opset-9
for the resulting ONNX graph since most runtimes will support opset-9.
If you want the graph to be generated with a specific opset, use --opset
in the command line, for example --opset 13
.
We support tf-1.x graphs
and tf-2
. To keep our test matrix manageable we test tf2onnx running on top of tf-1.12 or better
.
When running under tf-2.x tf2onnx will use the tensorflow V2 controlflow.
You can install tf2onnx on top of tf-1.x or tf-2.x.
We support Python 3.6-3.9
.
Note that on windows for Python > 3.7 the protobuf package doesn't use the cpp implementation and is very slow - we recommend to use Python 3.7 for that reason.
If you don't have TensorFlow installed already, install the desired TensorFlow build, for example:
pip install tensorflow
If you want to run tests, install a runtime that can run ONNX models. For example:
ONNX Runtime (available for Linux, Windows, and Mac):
pip install onnxruntime
pip install -U tf2onnx
pip install git+https://github.com/onnx/tensorflow-onnx
git clone https://github.com/onnx/tensorflow-onnx
Once dependencies are installed, from the tensorflow-onnx folder call:
python setup.py install
or
python setup.py develop
tensorflow-onnx requires onnx-1.5 or better and will install/upgrade onnx if needed.
To create a wheel for distribution:
python setup.py bdist_wheel
To get started with tensorflow-onnx
, run the t2onnx.convert
command, providing:
- the path to your TensorFlow model (where the model is in
saved model
format) - a name for the ONNX output file:
python -m tf2onnx.convert --saved-model tensorflow-model-path --output model.onnx
The above command uses a default of 9
for the ONNX opset. If you need a newer opset, or want to limit your model to use an older opset then you can provide the --opset
argument to the command. If you are unsure about which opset to use, refer to the ONNX operator documentation.
python -m tf2onnx.convert --saved-model tensorflow-model-path --opset 13 --output model.onnx
If your TensorFlow model is in a format other than saved model
, then you need to provide the inputs and outputs of the model graph.
For checkpoint
format:
python -m tf2onnx.convert --checkpoint tensorflow-model-meta-file-path --output model.onnx --inputs input0:0,input1:0 --outputs output0:0
For graphdef
format:
python -m tf2onnx.convert --graphdef tensorflow-model-graphdef-file --output model.onnx --inputs input0:0,input1:0 --outputs output0:0
If your model is in checkpoint
or graphdef
format and you do not know the input and output nodes of the model, you can use the summarize_graph TensorFlow utility. The summarize_graph
tool does need to be downloaded and built from source. If you have the option of going to your model provider and obtaining the model in saved model
format, then we recommend doing so.
You find an end-to-end tutorial for ssd-mobilenet here
We recently added support for tflite. You convert tflite
models via command line, for example:
python -m tf2onnx.convert --opset 13 --tflite tflite--file --output model.onnx
python -m tf2onnx.conv
8000
ert
--saved-model SOURCE_SAVED_MODEL_PATH |
--checkpoint SOURCE_CHECKPOINT_METAFILE_PATH |
--tflite TFLITE_MODEL_PATH |
--tfjs TFJS_MODEL_PATH |
--input | --graphdef SOURCE_GRAPHDEF_PB
--output TARGET_ONNX_MODEL
[--inputs GRAPH_INPUTS]
[--outputs GRAPH_OUTPUS]
[--inputs-as-nchw inputs_provided_as_nchw]
[--opset OPSET]
[--dequantize]
[--tag TAG]
[--signature_def SIGNATURE_DEF]
[--concrete_function CONCRETE_FUNCTION]
[--target TARGET]
[--custom-ops list-of-custom-ops]
[--fold_const]
[--large_model]
[--continue_on_error]
[--verbose]
[--output_frozen_graph]
TensorFlow model as saved_model. We expect the path to the saved_model directory.
TensorFlow model as checkpoint. We expect the path to the .meta file.
TensorFlow model as graphdef file.
Convert a tensorflow.js model by providing a path to the .tfjs file. Inputs/outputs do not need to be specified.
Convert a tflite model by providing a path to the .tflite file. Inputs/outputs do not need to be specified.
The target onnx file path.
TensorFlow model's input/output names, which can be found with summarize graph tool. Those names typically end with :0
, for example --inputs input0:0,input1:0
. Inputs and outputs are not needed for models in saved-model format. Some models specify placeholders with unknown ranks and dims which can not be mapped to onnx. In those cases one can add the shape after the input name inside []
, for example --inputs X:0[1,28,28,3]
. Use -1 to indicate unknown dimensions.
By default we preserve the image format of inputs (nchw
or nhwc
) as given in the TensorFlow model. If your hosts (for example windows) native format nchw and the model is written for nhwc, --inputs-as-nchw
tensorflow-onnx will transpose the input. Doing so is convenient for the application and the converter in many cases can optimize the transpose away. For example --inputs input0:0,input1:0 --inputs-as-nchw input0:0
assumes that images are passed into input0:0
as nchw while the TensorFlow model given uses nhwc.
ONNX requires default values for graph inputs to be constant, while Tensorflow's PlaceholderWithDefault op accepts computed defaults. To convert such models, pass a comma-separated list of node names to the ignore_default and/or use_default flags. PlaceholderWithDefault nodes with matching names will be replaced with Placeholder or Identity ops, respectively.
By default we use the opset 9 to generate the graph. By specifying --opset
the user can override the default to generate a graph with the desired opset. For example --opset 13
would create a onnx graph that uses only ops available in opset 13. Because older opsets have in most cases fewer ops, some models might not convert on a older opset.
(This is experimental, only supported for tflite)
Produces a float32 model from a quantized tflite model. Detects ReLU and ReLU6 ops from quantization bounds.
Only valid with parameter --saved_model
. Specifies the tag in the saved_model to be used. Typical value is 'serve'.
Only valid with parameter --saved_model
. Specifies which signature to use within the specified --tag value. Typical value is 'serving_default'.
(This is experimental, valid only for TF2.x models)
Only valid with parameter --saved_model
. If a model contains a list of concrete functions, under the function name __call__
(as can be viewed using the command saved_model_cli show --all
), this parameter is a 0-based integer specifying which function in that list should be converted. This parameter takes priority over --signature_def
, which will be ignored.
(Can be used only for TF2.x models)
Only valid with parameter --saved_model
. When set, creates a zip file containing the ONNX protobuf model and large tensor values stored externally. This allows for converting models that exceed the 2 GB protobuf limit.
Saves the frozen and optimize tensorflow graph to file.
If a model contains ops not recognized by onnx runtime, you can tag these ops with a custom op domain so that the
runtime can still open the model. The format is a comma-separated map of tf op names to domains in the format
OpName:domain. If only an op name is provided (no colon), the default domain of ai.onnx.converters.tensorflow
will be used.
Some models require special handling to run on some runtimes. In particular, the model may use unsupported data types. Workarounds are activated with --target TARGET
. Currently supported values are listed on this wiki. If your model will be run on Windows ML, you should specify the appropriate target value.
Deprecated.
To find the inputs and outputs for the TensorFlow graph the model developer will know or you can consult TensorFlow's summarize_graph tool, for example:
summarize_graph --in_graph=tests/models/fc-layers/frozen.pb
There are 2 types of tests.
python setup.py test
python tests/run_pretrained_models.py
usage: run_pretrained_models.py [-h] [--cache CACHE] [--tests TESTS] [--backend BACKEND] [--verbose] [--debug] [--config yaml-config]
optional arguments:
-h, --help show this help message and exit
--cache CACHE pre-trained models cache dir
--tests TESTS tests to run
--backend BACKEND backend to use
--config yaml config file
--verbose verbose output, option is additive
--opset OPSET target opset to use
--perf csv-file capture performance numbers for tensorflow and onnx runtime
--debug dump generated graph with shape info
run_pretrained_models.py
will run the TensorFlow model, captures the TensorFlow output and runs the same test against the specified ONNX backend after converting the model.
If the option --perf csv-file
is specified, we'll capture the timeing for inferece of tensorflow and onnx runtime and write the result into the given csv file.
You call it for example with:
python tests/run_pretrained_models.py --backend onnxruntime --config tests/run_pretrained_models.yaml --perf perf.csv
We provide an utility to save pre-trained model along with its config.
Put save_pretrained_model(sess, outputs, feed_inputs, save_dir, model_name)
in your last testing epoch and the pre-trained model and config will be saved under save_dir/to_onnx
.
Please refer to the example in tools/save_pretrained_model.py for more information.
Note the minimum required Tensorflow version is r1.6.
With tf2onnx-1.8.4 we updated our API. Our old API still works - you find the documentation here.
< C6DB div class="markdown-heading" dir="auto">