🤪
ONNX -> TFLite, TensorFlow, Keras, TFJS, CoreML 変換
およそ3光年ぶりの公開記事。
1. 自作ツール 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
を生成するとき。
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
2-7. INT8量子化 (per-channel)
onnx2tf -i resnet18-v1-7.onnx -oiqt -qt per-channel
2-8. INT8量子化 (per-tensor)
onnx2tf -i resnet18-v1-7.onnx -oiqt -qt per-tensor
2-9. TFJS変換
onnx2tf -i resnet18-v1-7.onnx -osd
pip install tensorflowjs
tensorflowjs_converter \
--input_format tf_saved_model \
--output_format tfjs_graph_model \
saved_model \
tfjs_model
2-10. CoreML変換
onnx2tf -i resnet18-v1-7.onnx -osd -k data
pip install coremltools
import coremltools as ct
FOLDER_PATH = 'saved_model'
model = ct.convert(
model=FOLDER_PATH,
source='tensorflow',
)
model.save(f'{FOLDER_PATH}/model.mlmodel')
3. 対応レイヤー
OP | Status |
---|---|
Abs | ✅ |
Acosh | ✅ |
Acos | ✅ |
Add | ✅ |
And | ✅ |
ArgMax | ✅ |
ArgMin | ✅ |
Asinh | ✅ |
Asin | ✅ |
Atanh | ✅ |
Atan | ✅ |
AveragePool | ✅ |
BatchNormalization | ✅ |
Bernoulli | ✅ |
BitShift | ✅ |
BitwiseAnd | Help wanted |
BitwiseNot | Help wanted |
BitwiseOr | Help wanted |
BitwiseXor | Help wanted |
Cast | ✅ |
Ceil | ✅ |
Celu | ✅ |
CenterCropPad | Help wanted |
Clip | ✅ |
Col2Im | Help wanted |
Compress | ✅ |
ConcatFromSequence | ✅ |
Concat | ✅ |
ConstantOfShape | ✅ |
Constant | ✅ |
Conv | ✅ |
ConvTranspose | ✅ |
Cosh | ✅ |
Cos | ✅ |
CumSum | ✅ |
DepthToSpace | ✅ |
Det | ✅ |
DequantizeLinear | ✅ |
DFT | Help wanted |
Div | ✅ |
Dropout | ✅ |
DynamicQuantizeLinear | ✅ |
Einsum | ✅ |
Elu | ✅ |
Equal | ✅ |
Erf | ✅ |
Expand | ✅ |
Exp | ✅ |
EyeLike | ✅ |
Flatten | ✅ |
Floor | ✅ |
FusedConv | ✅ |
GatherElements | ✅ |
GatherND | ✅ |
Gather | ✅ |
Gemm | ✅ |
GlobalAveragePool | ✅ |
GlobalLpPool | ✅ |
GlobalMaxPool | ✅ |
GreaterOrEqual | ✅ |
Greater | ✅ |
GridSample | ✅ |
GroupNormalization | Help wanted |
GRU | ✅ |
Hardmax | ✅ |
HardSigmoid | ✅ |
HardSwish | ✅ |
Identity | ✅ |
If | ✅ |
Input | ✅ |
InstanceNormalization | ✅ |
Inverse | ✅ |
IsInf | ✅ |
IsNaN | ✅ |
LayerNormalization | ✅ |
LeakyRelu | ✅ |
LessOrEqual | ✅ |
Less | ✅ |
Log | ✅ |
LogSoftmax | ✅ |
Loop | Help wanted |
LpNormalization | ✅ |
LRN | ✅ |
LSTM | ✅ |
MatMul | ✅ |
MatMulInteger | ✅ |
MaxPool | ✅ |
Max | ✅ |
MaxRoiPool | Help wanted |
MaxUnpool | ✅ |
Mean | ✅ |
MeanVarianceNormalization | ✅ |
MelWeightMatrix | Help wanted |
Min | ✅ |
Mish | ✅ |
Mod | ✅ |
Mul | ✅ |
Multinomial | ✅ |
Neg | ✅ |
NonMaxSuppression | ✅ |
NonZero | ✅ |
Optional | Help wanted |
OptionalGetElement | Help wanted |
OptionalHasElement | Help wanted |
Not | ✅ |
OneHot | ✅ |
Or | ✅ |
Pad | ✅ |
Pow | ✅ |
PRelu | ✅ |
QLinearAdd | ✅ |
QLinearConcat | ✅ |
QLinearConv | ✅ |
QLinearLeakyRelu | ✅ |
QLinearMatMul | ✅ |
QLinearMul | ✅ |
QLinearSigmoid | ✅ |
QLinearSoftmax | ✅ |
QuantizeLinear | ✅ |
RandomNormalLike | ✅ |
RandomNormal | ✅ |
RandomUniformLike | ✅ |
RandomUniform | ✅ |
Range | ✅ |
Reciprocal | ✅ |
ReduceL1 | ✅ |
ReduceL2 | ✅ |
ReduceLogSum | ✅ |
ReduceLogSumExp | ✅ |
ReduceMax | ✅ |
ReduceMean | ✅ |
ReduceMin | ✅ |
ReduceProd | ✅ |
ReduceSum | ✅ |
ReduceSumSquare | ✅ |
Relu | ✅ |
Reshape | ✅ |
Resize | ✅ |
ReverseSequence | ✅ |
RNN | ✅ |
RoiAlign | ✅ |
Round | ✅ |
ScaleAndTranslate | ✅ |
Scatter | ✅ |
ScatterElements | ✅ |
ScatterND | ✅ |
Scan | Help wanted |
Selu | ✅ |
SequenceAt | ✅ |
SequenceConstruct | ✅ |
SequenceEmpty | ✅ |
SequenceErase | ✅ |
SequenceInsert | ✅ |
SequenceLength | ✅ |
Shape | ✅ |
Shrink | ✅ |
Sigmoid | ✅ |
Sign | ✅ |
Sinh | ✅ |
Sin | ✅ |
Size | ✅ |
Slice | ✅ |
Softmax | ✅ |
Softplus | ✅ |
Softsign | ✅ |
SpaceToDepth | ✅ |
Split | ✅ |
SplitToSequence | ✅ |
Sqrt | ✅ |
Squeeze | ✅ |
STFT | Help wanted |
StringNormalizer | Help wanted |
Sub | ✅ |
Sum | ✅ |
Tanh | ✅ |
Tan | ✅ |
TfIdfVectorizer | Help wanted |
ThresholdedRelu | ✅ |
Tile | ✅ |
TopK | ✅ |
Transpose | ✅ |
Trilu | ✅ |
Unique | ✅ |
Unsqueeze | ✅ |
Upsample | ✅ |
Where | ✅ |
Xor | ✅ |
Discussion