PINTO0309/onnx2tf
A tool for converting ONNX files to LiteRT/TFLite/TensorFlow, 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.
What it solves
onnx2tf 是一個轉換工具,旨在將 AI 模型從 ONNX 格式搬移至其他多種框架,主要目標為 LiteRT(前身為 TensorFlow Lite)、TensorFlow 與 PyTorch。它消除了在這些生態系統之間切換時手動重寫模型的需求,支援廣泛的算子以確保高度相容性。
How it works
此工具提供兩條主要的執行路徑進行轉換:
flatbuffer_direct(Default): 一條高速路徑,繞過每個節點的 TensorFlow 轉換。它會先對 ONNX 圖做前處理,直接降階為內部 ModelIR,然後直接匯出為 FlatBuffer(TFLite)格式。此路徑的速度可比傳統路徑快超過 100 倍。tf_converter(Legacy): 一條相容性路徑,使用標準的 TensorFlow Lite Converter。
除了 TFLite,該工具還能產生原生 PyTorch nn.Module 套件、TorchScript 與 Exported Programs(.pt2),以及 TensorFlow SavedModel 與 Keras 檔案。它亦支援直接從 LiteRT 轉回 PyTorch。
Who it’s for
此工具適用於需要在邊緣裝置上部署模型(透過 LiteRT/TFLite)或在 PyTorch 與 TensorFlow/ONNX 生態系統之間遷移模型且不失功能精度的機器學習工程師與開發者。
Highlights
- Multi-Framework Support: 將 ONNX 轉換為 LiteRT、TensorFlow 與原生 PyTorch 程式碼。
- High Performance:
flatbuffer_direct後端相較於傳統方法提供顯著更快的轉換速度。 - Extensive Op Coverage: 支援大量 ONNX 算子,包括 Attention、GRU、LSTM 等複雜層。
- Accuracy Validation: 內建工具可產生精度報告,將輸入模型(例如 ONNX)與轉換後的輸出(例如 TFLite 或 PyTorch)進行比較。
- PyTorch Native Export: 能輸出標準的
torch.nn.Module,並附帶相容的state_dict.pth供即時推論使用。