{"id":4663,"library":"onnx2tf","title":"ONNX to TensorFlow/TFLite Converter","description":"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.","status":"active","version":"2.4.0","language":"en","source_language":"en","source_url":"https://github.com/PINTO0309/onnx2tf","tags":["onnx","tensorflow","pytorch","tflite","model conversion","machine learning"],"install":[{"cmd":"pip install onnx2tf","lang":"bash","label":"Default Install"}],"dependencies":[{"reason":"Required for ONNX model processing, core functionality.","package":"onnx","optional":false},{"reason":"Target format for conversion, essential for TensorFlow/TFLite outputs.","package":"tensorflow","optional":false},{"reason":"Required for PyTorch related conversions and for creating ONNX models from PyTorch.","package":"torch","optional":false},{"reason":"Used for direct TFLite backend conversions.","package":"flatbuffers","optional":false},{"reason":"Fundamental library for array operations in ML workflows.","package":"numpy","optional":false},{"reason":"Dependency for ONNX to TensorFlow conversion components.","package":"onnx_tf","optional":false},{"reason":"Used for serializing structured data, essential for model formats.","package":"protobuf","optional":false}],"imports":[{"symbol":"convert","correct":"import onnx2tf\n\n# ... then onnx2tf.convert(...)"}],"quickstart":{"code":"import onnx2tf\nimport torch\nimport torch.nn as nn\nimport os\n\n# 1. Define a simple PyTorch model\nclass SimpleModel(nn.Module):\n    def __init__(self):\n        super(SimpleModel, self).__init__()\n        self.conv = nn.Conv2d(3, 16, 3, 1, 1)\n\n    def forward(self, x):\n        return self.conv(x)\n\n# 2. Instantiate and export to ONNX\nmodel = SimpleModel()\ndummy_input = torch.randn(1, 3, 224, 224)\nonnx_file_path = \"simple_model.onnx\"\ntorch.onnx.export(\n    model,\n    dummy_input,\n    onnx_file_path,\n    opset_version=17,\n    input_names=[\"input\"],\n    output_names=[\"output\"],\n    dynamic_axes={\"input\": {0: \"batch_size\"}, \"output\": {0: \"batch_size\"}}\n)\nprint(f\"PyTorch model exported to {onnx_file_path}\")\n\n# 3. Convert ONNX to TensorFlow SavedModel\noutput_folder = \"./converted_tf_model\"\nos.makedirs(output_folder, exist_ok=True)\n\nonnx2tf.convert(\n    input_onnx_file_path=onnx_file_path,\n    output_folder_path=output_folder,\n    # For TFLite conversion, you might add:\n    # tflite_output_file_path=\"./converted_tf_model/model.tflite\"\n)\nprint(f\"ONNX model converted to TensorFlow SavedModel at {output_folder}\")\n\n# Clean up generated ONNX file\nos.remove(onnx_file_path)\n\n# To use a specific backend for TFLite (e.g., the deprecated tf_converter):\n# onnx2tf.convert(\n#     input_onnx_file_path=onnx_file_path,\n#     output_folder_path=\"./converted_tf_model_tfconv\",\n#     tflite_output_file_path=\"./converted_tf_model_tfconv/model.tflite\",\n#     tflite_backend='tf_converter'\n# )","lang":"python","description":"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."},"warnings":[{"fix":"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.","message":"Starting with v2.4.0, the default TFLite backend for `onnx2tf.convert()` and the CLI has switched from `tf_converter` to `flatbuffer_direct`. Code relying on the implicit `tf_converter` behavior for TFLite conversion will now use `flatbuffer_direct`.","severity":"breaking","affected_versions":">=2.4.0"},{"fix":"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.","message":"The `tf_converter` TFLite backend is deprecated starting from v2.4.0. While still available as an explicit option, users are encouraged to migrate to `flatbuffer_direct` as it may be removed in future versions.","severity":"deprecated","affected_versions":">=2.4.0"},{"fix":"Ensure your development environment uses Python 3.12 or a later version. You can manage Python versions using `pyenv` or `conda`.","message":"onnx2tf requires Python 3.12 or newer. Using older Python versions will lead to installation failures or runtime errors.","severity":"gotcha","affected_versions":">=2.3.18"},{"fix":"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.","message":"Implicit network downloads for validation sample data have been removed since v2.3.17. For integer quantization (`-oiqt`), explicit calibration input mapping is now required.","severity":"gotcha","affected_versions":">=2.3.17"}],"env_vars":null,"last_verified":"2026-04-12T00:00:00.000Z","next_check":"2026-07-11T00:00:00.000Z"}