2025-07-04

ONNX -> TFLite, TensorFlow, Keras, TFJS, CoreML 変換

ONNX -> TFLite, TensorFlow, Keras, TFJS, CoreML 変換

自作ツール onnx2tf

実装している機能が多すぎるので、この記事表現していない変換オプションはREADMEを参照。TransformerやSTTやTTSやその他もろもろの一発変換に対応したつもり。onnx-tensorflow より推論効率が高いモデルを生成できる。また、onnx-tensorflowよりも対応しているオペレーションの種類が多い。

コントリビューター

コード量(code = 行)

cloc .

419 text files.

414 unique files.

174 files ignored.

----------------------------------------

Language files blank comment code

----------------------------------------

Python 300 4820 6637 37182

JSON 27 109 0 3107

Markdown 5 343 0 2061

YAML 7 42 79 586

Dockerfile 1 6 3 38

----------------------------------------

SUM: 340 5320 6719 42974

----------------------------------------

2. 使用方法

onnx==1.13.1

onnxruntime==1.13.1

onnx-simplifier==0.4.17

onnx_graphsurgeon

simple_onnx_processing_tools

tensorflow==2.13.0rc0

2-1. インストールサンプルモデルダウンロード

docker

docker run --rm -it \

  • v `pwd`:/workdir \
  • w /workdir \

ghcr.io/pinto0309/onnx2tf:latest

pip

pip install onnx2tf -U

モデル

wget https://github.com/PINTO0309/onnx2tf/releases/download/0.0.2/resnet18-v1-7.onnx

2-2. 基本の「き」

TFLite を出力。

onnx2tf -i resnet18-v1-7.onnx

2-3. 基本の「ほ」

TFLite変換 + 完全体の saved_model を出力。

onnx2tf -i resnet18-v1-7.onnx -osd

2-4. 基本の「ん」

TFLite変換 + 全OPの精度チェック。

onnx2tf -i resnet18-v1-7.onnx -cotof

2-5. Keras

.h5 はイロイロと問題があるので、現状最新のフォーマット keras_v3 フォーマットで出力。

onnx2tf -i resnet18-v1-7.onnx -okv3

.h5 を生成するとき

https://www.imdb.com/es/list/ls599679681/

https://www.imdb.com/es/list/ls599679681/copy/

onnx2tf -i resnet18-v1-7.onnx -oh5

2-6. TFLite の入出力名を魔改造

# Custom flatc binary for Ubuntu 20.04+

# https://github.com/PINTO0309/onnx2tf/issues/196

wget https://github.com/PINTO0309/onnx2tf/releases/download/1.7.3/flatc.tar.gz \

&& tar -zxvf flatc.tar.gz \

&& sudo chmod +x flatc \

&& sudo mv flatc /usr/bin/

# Custom flatc binary for Windows

# Set the environment variable paths appropriately on your own.

# https://github.com/PINTO0309/onnx2tf/issues/196

https://github.com/PINTO0309/onnx2tf/releases/download/1.7.3/flatc.exe

onnx2tf -i resnet18-v1-7.onnx -coion

https://www.imdb.com/es/list/ls599679368/

https://www.imdb.com/es/list/ls599679368/copy/

記事への反応(ブックマークコメント)

ログイン ユーザー登録
ようこそ ゲスト さん