onnx2tf is a versatile Python tool designed for converting ONNX model files into various target formats, including LiteRT, TFLite, TensorFlow SavedModel, PyTorch native code (nn.Module), TorchScript (.pt), state_dict (.pt), Exported Program (.pt2), and Dynamo ONNX. It also supports direct conversion from LiteRT to PyTorch. The library maintains a rapid release cadence, with version 2.4.0 being the latest stable release.
pip install onnx2tfVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates the end-to-end process of defining a simple PyTorch model, exporting it to ONNX format, and then using `onnx2tf` to convert the ONNX model into a TensorFlow SavedModel. It highlights the primary `onnx2tf.convert()` function and the necessary input/output paths.
If you require the `tf_converter` backend for TFLite, explicitly set `tflite_backend='tf_converter'` in your `onnx2tf.convert()` call or use the `--tflite_backend tf_converter` CLI option.
Update your conversion scripts to use the `flatbuffer_direct` backend, which is now the default, or keep an eye on release notes for its eventual removal if `tf_converter` is critical for your workflow.
Ensure your development environment uses Python 3.12 or a later version. You can manage Python versions using `pyenv` or `conda`.
When performing integer quantization, ensure you provide calibration inputs explicitly rather than relying on the library to download them. Refer to the documentation for the `--calibration_data_path` or equivalent API parameters.
Use `simple-onnx-processing-tools` to preprocess the ONNX model and replace the unsupported operator with a combination of supported operations, or modify the original model's architecture to avoid the problematic operator.
Fix dynamic input dimensions to a static size during conversion using the `-b` (batch size) or `-ois` (overwrite input shape) options in the `onnx2tf` command. For example, `-b 1` for a fixed batch size of 1.
Ensure `ai_edge_litert` is installed in your Python environment. This typically happens automatically with `pip install onnx2tf`, but if not, install it explicitly: `pip install ai-edge-litert`. Also, verify TensorFlow and onnxruntime are installed.
Investigate the ONNX graph for problematic operations that alter tensor dimensions unexpectedly. Utilize `onnx2tf`'s options for channel transposition (`-kt` or `-kat`) or provide a parameter replacement JSON file (`-prf`) to manually specify correct transpositions or shape manipulations for the problematic layers.
Install it using `pip install onnx2tf`, then ensure your terminal is in the correct environment or its installation directory is in your PATH.