onnx2tf:实践指南
实际看onnx2tf,先要确认它的用途:用于将 ONNX 文件转换为 LiteRT/TFLite/TensorFlow、PyTorch 本机代码 (nn.Module)、TorchScript (.pt)等相关能力。软件开发里,依赖、接口和异常处理往往比主路径更影响采用。我会在隔离分支完成一个可回滚的小任务,检查安装步骤、接口契约、测试结果和错误信息。它适合需要可检查开发流程而非单次演示的工程师;采用前仍要看维护状态和试跑结果。

昂恩克斯2tf
用于将 ONNX 文件转换为 LiteRT/TFLite/TensorFlow、PyTorch 本机代码 (nn.Module)、TorchScript (.pt)、state_dict (.pt)、导出程序 (.pt2) 和 Dynamo ONNX 的工具。它还支持从LiteRT到PyTorch的直接转换。
您应该使用 LiteRT Torch 而不是 onnx2tf。 https://github.com/google-ai-edge/litert-torch 和 https://github.com/google-ai-edge/ai-edge-quantizer
tf_converter 支持的层
- https://github.com/onnx/onnx/blob/main/docs/Operators.md
- :heavy_check_mark:: 支持:white_check_mark:: 部分支持需要帮助:欢迎 Pull Request
See 支持的列表 layers
| OP | 状态 |
|---|---|
| 腹肌 | :heavy_check_mark: |
| 阿科什 | :heavy_check_mark: |
| 阿科斯 | :heavy_check_mark: |
| 添加 | :heavy_check_mark: |
| AffineGrid | :heavy_check_mark: |
| 并且 | :heavy_check_mark: |
| ArgMax | :heavy_check_mark: |
| ArgMin | :heavy_check_mark: |
| 阿辛 | :heavy_check_mark: |
| 阿辛 | :heavy_check_mark: |
| 阿坦 | :heavy_check_mark: |
| 阿坦 | :heavy_check_mark: |
| 注意 | :heavy_check_mark: |
| AveragePool | :heavy_check_mark: |
| BatchNormalization | :heavy_check_mark: |
| 伯努利 | :heavy_check_mark: |
| BitShift | :heavy_check_mark: |
| BitwiseAnd | :heavy_check_mark: |
| BitwiseNot | :heavy_check_mark: |
| BitwiseOr | :heavy_check_mark: |
| BitwiseXor | :heavy_check_mark: |
| BlackmanWindow | :heavy_check_mark: |
| 演员阵容 | :heavy_check_mark: |
| 天花板 | :heavy_check_mark: |
| 策卢 | :heavy_check_mark: |
| CenterCropPad | :heavy_check_mark: |
| 剪辑 | :heavy_check_mark: |
| 列2Im | :白色复选标记: |
| 压缩 | :heavy_check_mark: |
| ConcatFromSequence | :heavy_check_mark: |
| 康卡特 | :heavy_check_mark: |
| ConstantOfShape | :heavy_check_mark: |
| 常数 | :heavy_check_mark: |
| 转化率 | :heavy_check_mark: |
| ConvInteger | :白色复选标记: |
| ConvTranspose | :heavy_check_mark: |
| 科什 | :heavy_check_mark: |
| 科斯 | :heavy_check_mark: |
| CumProd | :heavy_check_mark: |
| CumSum | :heavy_check_mark: |
| DeformConv | :白色复选标记: |
| DepthToSpace | :heavy_check_mark: |
| 德特 | :heavy_check_mark: |
| DequantizeLinear | :heavy_check_mark: |
| DFT | :白色复选标记: |
| 分区 | :heavy_check_mark: |
| 辍学 | :heavy_check_mark: |
| DynamicQuantizeLinear | :heavy_check_mark: |
| 埃因苏姆 | :heavy_check_mark: |
| 埃卢 | :heavy_check_mark: |
| 平等 | :heavy_check_mark: |
| 埃尔夫 | :heavy_check_mark: |
| 展开 | :heavy_check_mark: |
| 经验值 | :heavy_check_mark: |
| EyeLike | :heavy_check_mark: |
| 展平 | :heavy_check_mark: |
| 楼层 | :heavy_check_mark: |
| FusedConv | :heavy_check_mark: |
| GatherElements | :heavy_check_mark: |
| GatherND | :heavy_check_mark: |
| 聚集 | :heavy_check_mark: |
| 格鲁 | :heavy_check_mark: |
| 格姆 | :heavy_check_mark: |
| GlobalAveragePool | :heavy_check_mark: |
| GlobalLpPool | :heavy_check_mark: |
| GlobalMaxPool | :heavy_check_mark: |
| GreaterOrEqual | :heavy_check_mark: |
| 更大 | :heavy_check_mark: |
| GridSample | :白色复选标记: |
| GroupNormalization | :heavy_check_mark: |
| GRU | :heavy_check_mark: |
| HammingWindow | :白色复选标记: |
| HannWindow | :白色复选标记: |
| 硬最大值 | :heavy_check_mark: |
| HardSigmoid | :heavy_check_mark: |
| HardSwish | :heavy_check_mark: |
| 身份 | :heavy_check_mark: |
| 如果 | :heavy_check_mark: |
| ImageDecoder | :白色复选标记: |
| 输入 | :heavy_check_mark: |
| InstanceNormalization | :heavy_check_mark: |
| 逆 | :heavy_check_mark: |
| IsInf | :heavy_check_mark: |
| IsNaN | :heavy_check_mark: |
| LayerNormalization | :heavy_check_mark: |
| LeakyRelu | :heavy_check_mark: |
| LessOrEqual | :heavy_check_mark: |
| 少 | :heavy_check_mark: |
| 日志 | :heavy_check_mark: |
| LogSoftmax | :heavy_check_mark: |
| 循环 | :heavy_check_mark: |
| LpNormalization | :heavy_check_mark: |
| LpPool | :heavy_check_mark: |
| LRN | :heavy_check_mark: |
| LSTM | :heavy_check_mark: |
| MatMul | :heavy_check_mark: |
| MatMulInteger | :heavy_check_mark: |
| MaxPool | :heavy_check_mark: |
| 最大 | :heavy_check_mark: |
| MaxRoiPool | :heavy_check_mark: |
| MaxUnpool | :heavy_check_mark: |
| 平均值 | :heavy_check_mark: |
| MeanVarianceNormalization | :heavy_check_mark: |
| MelWeightMatrix | :heavy_check_mark: |
| 最小 | :heavy_check_mark: |
| 米什 | :heavy_check_mark: |
| 模组 | :heavy_check_mark: |
| 穆尔 | :heavy_check_mark: |
| 多项式 | :heavy_check_mark: |
| 负值 | :heavy_check_mark: |
| NegativeLogLikelihoodLoss | :heavy_check_mark: |
| NonMaxSuppression | :heavy_check_mark: |
| NonZero | :heavy_check_mark: |
| 可选 | :heavy_check_mark: |
| OptionalGetElement | :heavy_check_mark: |
| OptionalHasElement | :heavy_check_mark: |
| 不 | :heavy_check_mark: |
| OneHot | :heavy_check_mark: |
| 或者 | :heavy_check_mark: |
| 垫 | :heavy_check_mark: |
| 战俘 | :heavy_check_mark: |
| PRelu | :heavy_check_mark: |
| QLinearAdd | :heavy_check_mark: |
| QLinearAveragePool | :heavy_check_mark: |
| QLinearConcat | :heavy_check_mark: |
| QLinearConv | :heavy_check_mark: |
| QGemm | :heavy_check_mark: |
| QLinearGlobalAveragePool | :heavy_check_mark: |
| QLinearLeakyRelu | :heavy_check_mark: |
| QLinearMatMul | :heavy_check_mark: |
| QLinearMul | :heavy_check_mark: |
| QLinearSigmoid | :heavy_check_mark: |
| QLinearSoftmax | :heavy_check_mark: |
| QuantizeLinear | :heavy_check_mark: |
| RandomNormalLike | :heavy_check_mark: |
| RandomNormal | :heavy_check_mark: |
| RandomUniformLike | :heavy_check_mark: |
| RandomUniform | :heavy_check_mark: |
| 范围 | :heavy_check_mark: |
| 倒数 | :heavy_check_mark: |
| ReduceL1 | :heavy_check_mark: |
| ReduceL2 | :heavy_check_mark: |
| ReduceLogSum | :heavy_check_mark: |
| ReduceLogSumExp | :heavy_check_mark: |
| ReduceMax | :heavy_check_mark: |
| ReduceMean | :heavy_check_mark: |
| ReduceMin | :heavy_check_mark: |
| ReduceProd | :heavy_check_mark: |
| ReduceSum | :heavy_check_mark: |
| ReduceSumSquare | :heavy_check_mark: |
| RegexFullMatch | :heavy_check_mark: |
| 热鲁 | :heavy_check_mark: |
| 重塑 | :heavy_check_mark: |
| 调整大小 | :heavy_check_mark: |
| ReverseSequence | :heavy_check_mark: |
| RNN | :heavy_check_mark: |
| RoiAlign | :heavy_check_mark: |
| RotaryEmbedding | :heavy_check_mark: |
| 圆形 | :heavy_check_mark: |
| ScaleAndTranslate | :heavy_check_mark: |
| 分散 | :heavy_check_mark: |
| ScatterElements | :heavy_check_mark: |
| ScatterND | :heavy_check_mark: |
| 扫描 | :heavy_check_mark: |
| 塞鲁 | :heavy_check_mark: |
| SequenceAt | :heavy_check_mark: |
| SequenceConstruct | :heavy_check_mark: |
| SequenceEmpty | :heavy_check_mark: |
| SequenceErase | :heavy_check_mark: |
| SequenceInsert | :heavy_check_mark: |
| SequenceLength | :heavy_check_mark: |
| 形状 | :heavy_check_mark: |
| 收缩 | :heavy_check_mark: |
| 乙状结肠 | :heavy_check_mark: |
| 标志 | :heavy_check_mark: |
| 辛赫 | :heavy_check_mark: |
| 罪恶 | :heavy_check_mark: |
| 尺寸 | :heavy_check_mark: |
| 切片 | :heavy_check_mark: |
| 软最大 | :heavy_check_mark: |
| SoftmaxCrossEntropyLoss | :heavy_check_mark: |
| 软加 | :heavy_check_mark: |
| 软签名 | :heavy_check_mark: |
| SpaceToDepth | :heavy_check_mark: |
| 斯普利特 | :heavy_check_mark: |
| SplitToSequence | :heavy_check_mark: |
| 平方根 | :heavy_check_mark: |
| 挤压 | :heavy_check_mark: |
| STFT | :白色复选标记: |
| StringConcat | :heavy_check_mark: |
| StringNormalizer | :heavy_check_mark: |
| StringSplit | :heavy_check_mark: |
| 子 | :heavy_check_mark: |
| 总和 | :heavy_check_mark: |
| 谭 | :heavy_check_mark: |
| 谭 | :heavy_check_mark: |
| TensorScatter | :heavy_check_mark: |
| TfIdfVectorizer | :白色复选标记: |
| ThresholdedRelu | :heavy_check_mark: |
| 瓷砖 | :heavy_check_mark: |
| TopK | :heavy_check_mark: |
| 转置 | :heavy_check_mark: |
| 特里卢 | :heavy_check_mark: |
| 独特 | :heavy_check_mark: |
| 松开 | :heavy_check_mark: |
| 上采样 | :heavy_check_mark: |
| 哪里 | :heavy_check_mark: |
| 异或 | :heavy_check_mark: |
flatbuffer_direct 执行路径
flatbuffer_direct 现在是默认后端。对于支持的直接路径,它比 tf_converter 更快且成功率更高。现在最简单的转换命令默认只输出 LiteRT 模型,但如果添加 --flatbuffer_direct_output_saved_model,它也会输出 saved_model。与传统的 tf_converter 路径不同,此 SavedModel 是从 LiteRT-side ModelIR 构建的。
[!IMPORTANT]
flatbuffer_direct是当前默认后端。仅当您明确需要旧版 TensorFlow Lite Converter 兼容性路径时才使用--tflite_backend tf_converter。
使用默认的 flatbuffer_direct 后端,onnx2tf 对 ONNX 输入和 -it/--input_tflite_file_path 输入使用直接快速路径:
- ONNX 图预处理(
tflite_builder.preprocess)和直接降序(lower_onnx_to_ir) - 直接 FlatBuffer 导出(
*_float32.tflite、*_float16.tflite和可选的量化变体) - 可选直接reports/evaluation(
*_op_coverage_report.json,张量对应,ONNX/TFLite检查)
在此快速路径中,将跳过每个节点的 TensorFlow 转换(所有 ONNX 节点上的 op.make_node())。
这会删除长调试跟踪,例如:
INFO: <index> / <total>INFO: onnx_op_type: ...INFO: tf_op_type: ...
实测示例(同一型号,float32 TFLite 写入阶段):
tf_converter:~24.947sflatbuffer_direct:~0.239s- 在这种情况下,
flatbuffer_direct大约比tf_converter快 107 倍。
实际加速取决于模型结构、启用的选项和运行时环境。
直接导出还可以生成 TF-side 工件,而无需回退到 tf_converter:
--output_h5--output_keras_v3--output_tfv1_pb--flatbuffer_direct_output_pytorch
这些工件是由使用 float32 ModelIR 构建的内部 SavedModel 桥生成的。 如果直接导出失败,转换将停止并出现显式错误。
-inimc / -onimc 也停留在 flatbuffer_direct 的直接路径上。
对于 ONNX 输入和 -it 输入,这些选项裁剪 imported/lowered ModelIR
在指定的边界张量名称处而不是分割 ONNX 图。
-dgc、-ebu 和 -eru 也保留在 flatbuffer_direct 中的直接路径上。
对于 ONNX 输入,它们在降低期间或降低后 ModelIR 重写时应用。
对于 -it 输入,它们在 SavedModel 桥之前应用于导入的 ModelIR,
分割规划,或重写TFLite导出。
如果无法安全地应用请求的重写,则转换将停止并出现显式错误。
-me 也停留在 flatbuffer_direct 的直接路径上。
对于 ONNX MeanVarianceNormalization,直接降低使用原始内置操作
并将 mvn_epsilon 应用于内部 variance + epsilon 项,而无需
回落到 tf_converter。
--disable_model_save 也保持在直接路径上。在 flatbuffer_direct 中,这意味着转换仍然可以运行内部验证和临时暂存,但在请求的输出目录中不会留下最终工件。
无效的组合被明确拒绝:
--disable_model_save与--output_h5、--output_keras_v3或--output_tfv1_pb--enable_auto_split_model与--output_h5、--output_keras_v3或--output_tfv1_pb
SavedModel 从 flatbuffer_direct 直接导出 ModelIR 可用于
--flatbuffer_direct_output_saved_model.
PyTorch 封装直接导出可用
--flatbuffer_direct_output_pytorch.
这些选项具有以下限制:
- 两者都需要
--tflite_backend flatbuffer_direct --flatbuffer_direct_output_saved_model不能与--disable_model_save组合使用CUSTOM操作被拒绝并出现显式错误
| INT8 ONNX | INT8 TFLite(LiteRT) |
|---|---|
-
e.g。 LiteRT 仅输出
onnx2tf -i iat_llie_180x320.onnx -tb flatbuffer_direct -
e.g。 LiteRT 输出后,从 LiteRT 生成额外的
saved_modelonnx2tf -i iat_llie_180x320.onnx -tb flatbuffer_direct -fdosm -
e.g。直接从现有的 LiteRT (
.tflite) 文件生成saved_modelonnx2tf -it iat_llie_180x320_float32.tflite -tb flatbuffer_direct -
e.g。直接从现有的 LiteRT (
.tflite) 文件生成.h5,无需tf_converter后备onnx2tf -it iat_llie_180x320_float32.tflite -tb flatbuffer_direct -oh5 -
e.g。直接从现有的 LiteRT (
.tflite) 文件生成 PyTorch 包onnx2tf -it iat_llie_180x320_float32.tflite -o tmp_iat_llie_180x320_from_tflite -tb flatbuffer_direct -fdopt -
e.g。将输入 LiteRT 模型和生成的 PyTorch 包与相同的种子输入进行比较
onnx2tf -it iat_llie_180x320_float32.tflite -o tmp_iat_llie_180x320_from_tflite -tb flatbuffer_direct -fdopt -cotof这输出:
iat_llie_180x320_float32_pytorch/iat_llie_180x320_float32_pytorch_accuracy_report.json(TFLite↔PyTorch)iat_llie_180x320_float32_accuracy_comparison_report.json
【超实验】PyTorch导出示例( yolox_s.onnx )
flatbuffer_direct 可以发出本机 PyTorch 包以及可选的
一次运行即可生成 TorchScript、Dynamo ONNX 和 ExportedProgram 工件。
生成所有 PyTorch-side 工件以及 TFLite 和准确性报告:
onnx2tf
-i yolox_s.onnx
-o tmp_yolox_s
-tb flatbuffer_direct
-cotof
-fdopt
-fdots
-fdodo
-fdoep
输出目录包含:
yolox_s_float32.tfliteyolox_s_float16.tfliteyolox_s_accuracy_report.json(ONNX↔TFLite)yolox_s_pytorch_accuracy_report.json(ONNX↔PyTorch)yolox_s_accuracy_comparison_report.jsonyolox_s_pytorch/model.pyruntime.pystate_dict.pthmetadata.jsonyolox_s_pytorch/yolox_s_jit.ptyolox_s_pytorch/yolox_s_dynamo.onnxyolox_s_pytorch/yolox_s_ep.pt2
生成的PyTorch包是一个普通的torch.nn.Module包。你可以
加载它并直接运行急切推理:
import sys
import torch
sys.path.append("tmp_yolox_s")
from yolox_s_pytorch import load_model
model = load_model(device="cpu", eval_mode=True)
x = torch.zeros((1, 3, 640, 640), dtype=torch.float32)
with torch.no_grad():
output = model(x)
print("input :", tuple(x.shape))
print("output:", tuple(output.shape))
print(model.forward_named(x).keys())
当前 yolox_s 包的预期输出:
input : (1, 3, 640, 640)
output: (1, 8400, 85)
dict_keys(['output'])
您还可以使用标准显式加载捆绑的 state_dict.pth
PyTorch APIs:
import sys
from pathlib import Path
import torch
sys.path.append("tmp_yolox_s")
from yolox_s_pytorch.model import Model
package_dir = Path("tmp_yolox_s/yolox_s_pytorch")
model = Model(load_weights=False, eval_mode=True)
state_dict = torch.load(package_dir / "state_dict.pth", map_location="cpu")
model.load_state_dict(state_dict, strict=True)
生成的state_dict.pth以load_state_dict-兼容格式保存
对于本机 PyTorch 包。
对于本机包,原始 torch.onnx.export(..., dynamo=True) 和原始
torch.export.save(torch.export.export(...)) 旨在生产相同的
图结构为帮助程序生成的 *_dynamo.onnx 和 *_ep.pt2。
示例:原始 torch.onnx.export
from pathlib import Path
import importlib
import logging
import sys
import torch
package_dir = Path("tmp_yolox_s/yolox_s_pytorch").resolve()
sys.path.insert(0, str(package_dir.parent))
pkg = importlib.import_module(package_dir.name)
model = pkg.load_model(device="cpu", eval_mode=True)
model.eval()
example_inputs = (torch.randn(1, 3, 640, 640),)
logging.getLogger("torch.onnx._internal.exporter._registration").setLevel(logging.ERROR)
with torch.no_grad():
torch.onnx.export(
model,
example_inputs,
str(package_dir / "raw_dynamo.onnx"),
dynamo=True,
input_names=model.input_names,
output_names=model.output_names,
)
示例:原始 torch.export.save
from pathlib import Path
import importlib
import sys
import torch
package_dir = Path("tmp_yolox_s/yolox_s_pytorch").resolve()
sys.path.insert(0, str(package_dir.parent))
pkg = importlib.import_module(package_dir.name)
model = pkg.load_model(device="cpu", eval_mode=True)
model.eval()
example_inputs = (torch.randn(1, 3, 640, 640),)
with torch.no_grad():
exported_program = torch.export.export(model, example_inputs)
torch.export.save(
exported_program,
str(package_dir / "raw_exported_program.pt2"),
)
这两个示例都需要具体的示例输入形状。对于动态的公共投入,
使用与您提供给 -fdodo 或 -fdoep 相同的具体 shape/data
通过 --shape_hints、--test_data_nhwc_path 或 -cind。
[!CAUTION] 由
--flatbuffer_direct_output_pytorch生成的本机 PyTorch 包主要用于推理,而不是用于原样训练。详细原因:
- 导出器旨在保留转换后的 TFLite/ModelIR 图的推理行为,而不是重建原始训练时 PyTorch 模型语义。
- 生成的图可能包括面向推理的重写,例如布局规范化、常量折叠、为了运行时兼容性而插入的 reshapes/transposes 以及分解为原始操作。这些对于前向推理是正确的,但不能保证对于基于梯度的优化是理想的,甚至是稳定的。
- 一些生成的模型包括后处理或特定于任务的推理逻辑,例如
argmax、non_max_suppression、分数过滤、索引密集型张量选择或形状控制分支。这些通常是不可微分或较差的训练目标。- 本机导出可能会发出语义上等效的推理辅助路径,但在设计时并未考虑训练人体工程学,例如严格的 shape/layout 对齐、1D/2D/3D 卷积兼容性的桥垫片或图形本地运行时辅助程序。
- 回退支持的包(
tflite、saved_model、string_normalizer)是非 PyTorch 执行后端的包装器,因此应被视为仅推理。- 即使本机包也以
eval_mode=True作为正常使用路径发出。导出器目前不保证模块的训练安全重建,例如标准化层、循环状态处理或控制流密集型块,其形式与优化器和调度器预期的相同。state_dict.pth与本机包的load_state_dict- 兼容,但权重加载的兼容性并不意味着生成的模块是适合微调的忠实训练架构。实用指导:
- 使用生成的 PyTorch 包进行推理验证、打包和并排输出比较。
- 如果要训练或微调模型,请将生成的包仅视为参考实现,并在优化前将架构重建或简化为面向训练的 PyTorch 模型。
Click 至 expand
- 范围:上面“
tf_converter支持的层”表中列出的 ONNX 操作。 - 事实来源:
onnx2tf/tflite_builder/op_registry.py和--report_op_coverage输出。 - 当前摘要:
- 下表中列出了 ONNX 操作:
192 - 策略计数在
*_op_coverage_report.json(schema_policy_counts) 中生成。 - 使用
--report_op_coverage检查每次转换运行的最新数字。
- 下表中列出了 ONNX 操作:
注意事项:
flatbuffer_direct仅支持 ONNX 操作的子集作为 TFLite 内置函数。- 某些操作是有条件支持的(rank/attribute/constant-input 约束)。
- 对于特定于模型的结果,请使用
--report_op_coverage并检查*_op_coverage_report.json。
flatbuffer_direct
中支持
Builtin (ONNX -> TFLite) |ONNX OP|TFLite OP|关键约束(flatbuffer_direct)| |:-|:-|:-| |腹肌|ABS(或 NEG + MAXIMUM 为 INT64)|对于 INT64 输入,降低为 NEG + MAXIMUM,因为 TFLite ABS 内核不支持 INT64| |阿科斯|MUL + SUB + SQRT + ATAN2|Input/output dtype 必须是 FLOAT16 或 FLOAT32| |阿科什|SUB + ADD + SQRT + MUL + LOG|Input/output dtype 必须是 FLOAT16 或 FLOAT32| |添加|ADD|-| |AffineGrid|BATCH_MUL + TRANSPOSE + RESHAPE|size 输入必须是恒定的 1 级长度 4 或具有静态正值的 5; theta 必须是形状为 [N,2,3] 或 [N,3,4] 的三阶浮点张量;输出形状必须匹配 size; align_corners 在 {0,1}| |并且|LOGICAL_AND|-| |ArgMax|ARG_MAX(+ 用于 keepdims 的可选 RESHAPE)|axis 必须在范围内,keepdims 必须是 0 或 1、select_last_index=0,输出 dtype 必须是 INT32 或 INT64| |ArgMin|ARG_MIN(+ 用于 keepdims 的可选 RESHAPE)|axis 必须在范围内,keepdims 必须是 0 或 1、select_last_index=0,输出 dtype 必须是 INT32 或 INT64| |注意|RESHAPE + TRANSPOSE + BATCH_MUL + MUL + SOFTMAX + CAST|仅规范 3 输入形式 (query/key/value);仅单输出; q_num_heads == kv_num_heads > 0; is_causal=0; qk_matmul_output_mode=0; softcap=0;仅 3 阶浮点张量| |阿辛|MUL + SUB + SQRT + ATAN2|Input/output dtype 必须是 FLOAT16 或 FLOAT32| |阿辛|MUL + ADD + SQRT + LOG|Input/output dtype 必须是 FLOAT16 或 FLOAT32| |阿坦|ATAN2|Input/output dtype 必须是 FLOAT16 或 FLOAT32| |阿坦|ADD + SUB + DIV + LOG + MUL|Input/output dtype 必须是 FLOAT16 或 FLOAT32| |AveragePool|AVERAGE_POOL_2D(+可选PAD/PADV2+除数校正DIV)|仅 2D(等级=4),ceil_mode 位于 {0,1} 中,count_include_pad 位于 {0,1} 中。支持 {NOTSET,VALID,SAME_*,SAME_LOWER} 中的 auto_pad 和显式焊盘。对于具有非零有效焊盘的 count_include_pad=0,应用校正路径(掩模上的 AVERAGE_POOL_2D + DIV)| |BatchNormalization|MUL + ADD|所有参数输入(scale、bias、mean、var)必须为常量| |伯努利|SHAPE + RANDOM_UNIFORM + LESS(+ 可选 CAST)|输入数据类型必须为 FLOAT16/FLOAT32;输出数据类型必须是 BOOL 或数字| |BitShift|基于 RIGHT_SHIFT (RIGHT) 或 MUL- (LEFT)|LHS/RHS 必须是整数张量,direction 必须是 LEFT 或 RIGHT; LEFT 需要恒定的移位输入| |BitwiseAnd|LOGICAL_AND|仅 BOOL 张量| |BitwiseNot|LOGICAL_NOT / SUB + CAST|输入数据类型必须是 BOOL 或整数| |BitwiseOr|LOGICAL_OR|仅 BOOL 张量| |BitwiseXor|BITWISE_XOR|输入数据类型必须匹配且为 BOOL/integer| |BlackmanWindow|CAST + SQUEEZE + RANGE + MUL + DIV + COS + SUB + ADD + MAXIMUM|输入必须是类似标量的rank-1 length-1整数张量;输出数据类型必须是 FLOAT16/FLOAT32| |演员阵容|CAST|-| |CastLike|CAST|-| |天花板|CEIL|-| |策卢|MAXIMUM + MINIMUM + DIV + EXP + SUB + MUL + ADD|Input/output dtype 必须是 FLOAT16 或 FLOAT32| |CenterCropPad|SLICE + PAD(+ 可选 RESHAPE 直通)|目标形状输入必须是常量rank-1; axes 必须在范围内且长度必须与目标形状匹配;输出等级必须与输入等级匹配;内置路径中不支持字符串数据类型| |剪辑|RELU / RELU6 / MAXIMUM + MINIMUM|通过 MAXIMUM/MINIMUM 分解支持一般恒定剪辑范围。 ReLU 快速路径:min=0,max=+inf; ReLU6 快速路径:min=0,max=6| |列2Im|RESHAPE + TRANSPOSE + TRANSPOSE_CONV + SLICE + CAST|Input/output dtype 必须为 FLOAT16/FLOAT32; input/output 等级必须为 3/4; image_shape 和 block_shape 必须是常数 2 元素;所需静态正尺寸| |康卡特|CONCATENATION|-| |ConstantOfShape|CAST + FILL|形状输入必须是 1 阶整数张量; value 属性必须是标量(或省略零填充)| |转化率|CONV_2D / DEPTHWISE_CONV_2D / CONV_3D|2D:等级=4,恒定权重,仅分组转换regular/depthwise,零垫或auto_pad=SAME_*。 3D:等级=5,恒定等级5权重,group=1,strides/dilations长度=3,{NOTSET,VALID,SAME_UPPER}中的auto_pad(SAME_LOWER不支持);显式焊盘通过 VALID+pad/crop 路径处理| |ConvInteger|CAST + SUB + PAD + CONV_2D / DEPTHWISE_CONV_2D + TRANSPOSE|输入必须是整数张量,输出 dtype 必须是 INT32/INT64,权重必须是常量 4 级,分组转换必须是 regular/depthwise| |ConvTranspose|TRANSPOSE_CONV / CONV_3D_TRANSPOSE(+ 可选 ADD 偏置;1D 使用 EXPAND_DIMS/SQUEEZE 垫片)|支持1D/2D/3D(1D:输入rank = 3 +权重rank = 3 const,2D:输入rank = 4 +权重rank = 4 const,3D:输入rank = 5 +权重rank = 5 const),group=1,膨胀必须为全1,并且output_padding必须满足0 =1; axis 必须是标量常量(或 attr)并且在范围内; exclusive/reverse 必须是 0 或 1| |DeformConv|PAD + RESHAPE + TRANSPOSE + SHAPE + RANGE + SQUEEZE + GATHER + FLOOR + MAXIMUM/MINIMUM + CAST + MUL + ADD + SUB + BATCH_MUL|仅受约束的 2D 浮动路径:input/output/offset/mask 等级 = 4,input/offset/output(/mask) dtype FLOAT16/FLOAT32,权重和可选偏差必须恒定,kernel/channel/spatial 调暗必须为静态正值,内置降低仅限于 group=1 和offset_group=1 用于 LiteRT 运行时安全。分组模式仍然是自定义操作候选者| |DequantizeLinear|DEQUANTIZE|scale 必须为常数,zero_point(如果提供)必须为常数,每轴 axis 必须在范围内| |DepthToSpace|DEPTH_TO_SPACE (DCR) / RESHAPE + TRANSPOSE + RESHAPE (CRD)|仅排名 4,blocksize > 1、{DCR,CRD} 中的 mode| |德特|GATHER + RESHAPE + MUL + SUB + ADD|Input/output dtype 必须为 FLOAT16/FLOAT32;内置降低目前仅支持静态方形 2x2 / 3x3 矩阵| |分区|DIV 或 MUL(当除数为常数倒数时)|对于非浮动输出,降低为 CAST -> MUL(reciprocal) -> CAST 以保留输出 dtype,而不使用不支持的整数 DIV 路径| |辍学|RESHAPE(+可选SHAPE + FILL用于掩码输出)|flatbuffer_direct 中的推理时无操作;输入 ratio/training_mode 被忽略| |DynamicQuantizeLinear|NEG + REDUCE_MAX + MINIMUM + MAXIMUM + SUB + DIV + ADD + CAST|输入数据类型必须为 FLOAT16/FLOAT32,输出数据类型必须为 Y=UINT8、Y_Scale=FLOAT16/FLOAT32、Y_ZeroPoint=UINT8; scale/zero-point 输出必须是标量| |埃因苏姆|FULLY_CONNECTED|仅 Rank-2 matmul 式方程 (ij,jk->ik),rhs 输入必须是常量权重| |埃卢|ELU|-| |平等|EQUAL|-| |埃尔夫|ABS + SIGN + MUL + ADD + DIV + EXP + SUB|Input/output dtype 必须是 FLOAT16 或 FLOAT32| |经验值|EXP|-| |展开|RESHAPE + MUL(通过 const 广播)|输出形状必须静态已知、非负且与输入形状广播兼容(当前直接降低使用静态 RESHAPE + MUL)| |EyeLike|RESHAPE(来自const eye)|输出必须是 2 级且具有完全静态的正形状| |展平|RESHAPE|输入等级必须 >= 1| |楼层|FLOOR|-| |FusedConv|CONV_2D / DEPTHWISE_CONV_2D + 融合激活|支持具有有效标量参数的 Relu/Tanh/Sigmoid/LeakyRelu/Clip/HardSigmoid 激活;卷积约束遵循内置 Conv/FusedConv 验证器| |FusedMatMul|BATCH_MUL(+ alpha 可选 MUL)|输入等级 >= 2,仅 dtypes FLOAT16/FLOAT32,transA/transB 必须为 0 或 1,需要有限的 alpha| |聚集|GATHER|仅限 batch_dims=0| |GatherElements|CAST + RESHAPE + CONCATENATION + GATHER_ND|Data/indices 等级必须匹配,输出形状必须等于索引形状,需要静态正输出调光,axis 必须在范围内| |GatherND|CAST + GATHER_ND|仅限 batch_dims=0;索引必须是整数类型;最后暗淡的索引必须是静态正数且 =3| |GlobalLpPool|ABS + POW + SUM + RESHAPE (+ 可选 CAST)|输入等级必须为>=3; input/output dtype 必须为 FLOAT16/FLOAT32; p 必须是有限的且 > 0| |GlobalMaxPool|REDUCE_MAX|输入等级必须为 >=3| |GroupNormalization|RESHAPE + MEAN + SUB + MUL + ADD + SQRT + DIV (+可选 CAST)|Input/output dtype 必须为 FLOAT16/FLOAT32; scale/bias 必须为常数长度=C; num_groups > 0 且必须划分通道暗淡; channel/spatial 调光必须为静态正值| |GRU|TRANSPOSE + SLICE + SQUEEZE + BATCH_MUL + ADD + MUL + SUB + LOGISTIC + TANH + RESHAPE + CONCATENATION + EXPAND_DIMS|layout=0; direction 中的 {forward, reverse, bidirectional}; sequence_lens 不支持; W/R 必须恒定为 3 级; linear_before_reset 中的 {0,1};激活 [Sigmoid,Tanh]; clip=0| |硬最大值|TRANSPOSE + ARG_MAX + ONE_HOT|axis 必须在范围内;目标轴尺寸必须为静态正值| |HardSigmoid|MUL + ADD + MAXIMUM + MINIMUM|Input/output dtype 必须是 FLOAT16 或 FLOAT32| |HardSwish|HARD_SWISH|Input/output dtype 必须是 FLOAT16 或 FLOAT32| |HammingWindow|CAST + SQUEEZE + RANGE + MUL + DIV + COS + SUB + MAXIMUM|输入必须是类似标量的rank-1 length-1整数张量;输出数据类型必须是 FLOAT16/FLOAT32| |HannWindow|CAST + SQUEEZE + RANGE + MUL + DIV + COS + SUB + MAXIMUM|输入必须是类似标量的rank-1 length-1整数张量;输出数据类型必须是 FLOAT16/FLOAT32| |MelWeightMatrix|const 折叠内置张量物化|所有五个输入必须是常量标量;输出数据类型必须为 FLOAT16/FLOAT32;输出形状为[dft_length // 2 + 1, num_mel_bins];需要 0 =3; scale 和 bias 输入必须恒定| |IsInf|ABS + EQUAL / LESS + GREATER + LOGICAL_AND|输入数据类型必须为 FLOAT16/FLOAT32;输出数据类型必须为 BOOL; detect_negative / detect_positive 荣获| |IsNaN|NOT_EQUAL|输入数据类型必须为 FLOAT16/FLOAT32;输出数据类型必须是 BOOL| |MeanVarianceNormalization|MEAN + SUB + MUL + MEAN + ADD + SQRT + DIV|Input/output dtype 必须为 FLOAT16 或 FLOAT32; mvn_epsilon直接应用于内置下降;默认轴遵循 ONNX 通道优先语义,并且排名 =2;矩阵最后一个维度必须解析为平方 2x2 或 3x3| |LeakyRelu|LEAKY_RELU|Input/output dtype 必须是 FLOAT16 或 FLOAT32| |少|LESS|-| |LessOrEqual|LESS_EQUAL|-| |日志|LOG|Input/output dtype 必须是 FLOAT16 或 FLOAT32| |LogSoftmax|SOFTMAX + LOG(+ 对于非最后轴转置 in/out)|axis 必须在范围内(负轴标准化)| |循环|WHILE(+子图本地ADD/LESS/LOGICAL_AND/RESHAPE和降低的身体操作)|内置降低支持静态展开模式(恒定 trip_count/cond,仅循环携带输出)或仅具有循环携带输出的 WHILE 模式(无扫描输出)。 max_trip_count 输入数据类型必须是 INT32 或 INT64| |LpPool|ABS + POW + AVERAGE_POOL_2D + MUL + RESHAPE(+可选CAST)|仅限 4 级; input/output dtype 必须为 FLOAT16/FLOAT32; kernel_shape/strides/dilations 必须是 2D; dilations=[1,1]; p 必须是有限的且 > 0;非零焊盘需要 count_include_pad=1| |LpNormalization|L2_标准化|仅限 p=2、axis=last| |LRN|LOCAL_RESPONSE_NORMALIZATION(+转置in/out)|输入等级必须为 4,size 必须为正奇整数| |LayerNormalization|MEAN + SUB + MUL + ADD + SQRT + DIV(+可选CAST)|axis和stash_type荣获荣誉;支持可选的 ONNX 输出(mean、inv_std_dev)| |LSTM|UNIDIRECTIONAL_SEQUENCE_LSTM / BIDIRECTIONAL_SEQUENCE_LSTM + REVERSE_V2 + SPLIT + SQUEEZE + SLICE + RESHAPE/EXPAND_DIMS + CONCATENATION|direction 中的 {forward,reverse,bidirectional}、layout=0、input_forget=0; W/R 必须是常量等级 3,且 num_directions 与 direction 匹配;可选的 B 必须是恒定形状 [num_directions, 8*hidden_size]; initial_h/initial_c 是可选的(如果提供,形状必须是 [num_directions, batch, hidden];支持运行时张量输入);不支持sequence_lens和窥视孔输入P;不支持投影 (R.shape[2] != hidden_size)| |MatMul|BATCH_MUL(+ CAST/RESHAPE/SQUEEZE 帮助程序)|支持标准rank>=2 matmul、向量lhs/rhs形式、向量点和标量乘法模式| |MatMulInteger|CAST + SUB + BATCH_MUL|A/B 输入等级必须 >=2(允许等级=1 占位符),A/B dtypes 必须是整数张量类型 (INT8/UINT8/INT16/UINT16/INT32),输出 dtype 必须是 INT32/INT64;可选零点输入必须是 scalar/1D 且形状兼容| |最大|MAXIMUM(链接用于 >2 个输入)|至少 2 个输入| |MaxPool|MAX_POOL_2D|仅 2D(等级=4),{0,1} 中的 ceil_mode,零垫或 auto_pad=SAME_*| |MaxRoiPool|TRANSPOSE + SLICE + MAX_POOL_2D + CONCATENATION|当前内置路径仅支持等级 4 input/output,且常数为 rois; input/output dtype 必须为 FLOAT16/FLOAT32;所有形状必须是静态正值; pooled_shape 必须为长度 2 正数并匹配输出空间暗淡;输出通道必须与输入通道匹配;输出批次必须等于常量 rois 的数量| |MaxUnpool|CAST + RESHAPE + SCATTER_ND|Input/indices/output 必须为 4 级;输入和索引形状必须匹配; input/output dtype 必须匹配且索引必须为整数;输出形状必须为静态正且匹配 batch/channel; kernel_shape 和 strides 必须为长度 2 正值;仅零 pads;可选的 output_shape 输入必须是与图形输出形状匹配的恒定长度 4 张量| |平均值|ADD + DIV(+ 可选 CAST)|所有输入和输出必须为 FLOAT16/FLOAT32| |NegativeLogLikelihoodLoss|TRANSPOSE + CAST + EQUAL + SELECT_V2 + ONE_HOT + MUL + SUM + SUB (+可选GATHER/MEAN/DIV)|Input/output dtype 必须为 FLOAT16/FLOAT32;目标数据类型必须是整数;输入等级必须为 >=2,轴 1 处具有静态正类暗淡;可选权重必须是长度为 C 的 1 级浮点张量; reduction 中的 {none,sum,mean};支持 ignore_index| |最小|MINIMUM(链接用于 >2 个输入)|至少 2 个输入| |米什|EXP + ADD + LOG + TANH + MUL|Input/output dtype 必须是 FLOAT16 或 FLOAT32| |模组|FLOOR_MOD|仅限 fmod=0| |穆尔|MUL|-| |MultiHeadAttention|RESHAPE + TRANSPOSE + BATCH_MUL + MUL + SOFTMAX + CAST|num_heads > 0、unidirectional=0、query/key/value 必须是 3 级相同的 dtype (FLOAT16/FLOAT32),隐藏的暗淡必须是静态正数且可被 num_heads 整除| |负值|NEG|-| |NonMaxSuppression|NON_MAX_SUPPRESSION_V4/V5 + SLICE + GATHER + SUB + CAST + RESHAPE + CONCATENATION (+ 可选 ARG_MAX + REDUCE_MAX)|仅限 Rank-3 boxes/scores; center_point_box=0;目前为batch=1;最后变暗的盒子必须是 4;静态正值num_boxes; scores_shape[2] == boxes_shape[1];可选的 thresholds/max_output 必须是标量常量;输出数据类型必须是 INT32 或 INT64;当 --output_nms_with_argmax 被禁用时,类暗淡必须为静态正(通过类 NMS 支持类暗淡 >1)。 --switch_nms_version(-snms)选择V4或V5。| |NonZero|NOT_EQUAL + WHERE + TRANSPOSE + CAST|输入等级必须为>=1;输出等级必须为 2| |不|LOGICAL_NOT|-| |OneHot|CAST + ADD + FLOOR_MOD + ONE_HOT|depth 输入必须是常量标量且 >0; values 输入必须是常数 2 元素张量 [off_value,on_value];归一化 axis 必须在范围内| |OptionalHasElement|const 折叠(BOOL 标量)|内置降低仅支持可确定存在情况:非可选张量输入折叠为 true; Optional(empty/value)产生的输入被折叠到false/true;内置路径不支持运行时可选图形输入| |或者|LOGICAL_OR|-| |垫|PAD / PADV2 / MIRROR_PAD(+动态垫桥:CAST + RESHAPE + TRANSPOSE)|mode 中的 {constant,reflect}; reflect 降低为 MIRROR_PAD(REFLECT)。 pads 可以是长度为 2*rank 的常量或动态 1 阶张量(整数类型,内部转换为 INT32)。对于mode=constant,常数零降低为PAD;非零常量降低为 PADV2(仅限非量化张量)| |战俘|POW|输出数据类型必须是 FLOAT16 或 FLOAT32| |PRelu|PRELU|slope 必须是常量(标量或每通道)| |QGemm|FULLY_CONNECTED|输入rank=1或2,权重必须恒定rank=2,偏差必须恒定,量化参数必须恒定,transA=0,{0,1}中的transB| |QLinearAdd|ADD|所有量化参数(a/b/c scale、a/b/c zero_point)必须是常量| |QLinearAveragePool|DEQUANTIZE + TRANSPOSE + AVERAGE_POOL_2D + TRANSPOSE + QUANTIZE|仅输入等级 = 4,所有量化参数(x scale/zero_point、y scale/zero_point)必须为常数,kernel_shape/strides 必须为 2D,{0,1} 中的 dilations=[1,1]、ceil_mode (ceil_mode=1 具有更严格的 pad/auto_pad 约束)和 count_include_pad=0| |QLinearConcat|DEQUANTIZE + CONCATENATION + QUANTIZE|y scale/zero_point 和每个输入三元组 (x scale/zero_point) 必须是常量,输入等级必须匹配,axis 必须在范围内| |QLinearConv|CONV_2D / DEPTHWISE_CONV_2D|Input/output 等级=4,权重必须恒定 等级=4,所有量化参数恒定,仅组卷积 regular/depthwise(深度检测使用 group 和权重形状),可选偏差必须恒定| |QLinearGlobalAveragePool|AVERAGE_POOL_2D(首选)/ DEQUANTIZE + MEAN + QUANTIZE(后备)|所有量化参数(x scale/zero_point、y scale/zero_point)必须为常量,输入等级 >= 3,channels_last 必须为 0 或 1。量化 AVERAGE_POOL_2D 路径用于具有静态空间暗淡和每张量量化的等级 4| |QLinearLeakyRelu|DEQUANTIZE + PRELU + QUANTIZE|所有量化参数(x/y scale、x/y zero_point)必须是常量| |QLinearMatMul|FULLY_CONNECTED|输入rank=1或2,权重必须恒定rank=2,所有量化参数恒定| |QLinearMul|MUL|所有量化参数(a/b/c scale、a/b/c zero_point)必须是常量| |QLinearSigmoid|DEQUANTIZE + LOGISTIC + QUANTIZE|所有量化参数(x scale/zero_point、y scale/zero_point)必须是常量| |QLinearSoftmax|DEQUANTIZE + SOFTMAX + QUANTIZE|所有量化参数(x/y scale、x/y zero_point)必须恒定; axis 必须是最后一个维度| |QuantizeLinear|QUANTIZE|scale 必须为常数,zero_point(如果提供)必须为常数,每轴 axis 必须在范围内| |RandomNormal|RANDOM_STANDARD_NORMAL(+可选MUL + ADD + CAST)|shape 属性必须存在且非空;输出数据类型必须为 FLOAT16/FLOAT32;当提供时,seed 映射到 TFLite 随机选项| |RandomNormalLike|SHAPE + RANDOM_STANDARD_NORMAL(+ 可选 MUL + ADD + CAST)|从输入形状推断出的排名;输出 dtype 必须是受支持的数字类型 (FLOAT16/FLOAT32/INT*/UINT*)。当提供时,seed 映射到 TFLite 随机选项| |RandomUniform|RANDOM_UNIFORM(+可选MUL + ADD + CAST)|shape 属性必须存在且非空;输出数据类型必须为 FLOAT16/FLOAT32;当提供时,seed 映射到 TFLite 随机选项| |RandomUniformLike|SHAPE + RANDOM_UNIFORM(+ 可选 MUL + ADD + CAST)|输入等级仅用于具体化运行时形状;输出数据类型必须为 FLOAT16/FLOAT32;当提供时,seed 映射到 TFLite 随机选项| |范围|CAST + SQUEEZE + RANGE|每个 start/limit/delta 必须是类似标量的 1 阶长度 1 张量| |倒数|DIV|Input/output dtype 必须是 FLOAT16 或 FLOAT32| |ReduceL1|ABS + SUM|通过输入张量提供时,归约轴必须恒定| |ReduceL2|MUL + SUM + SQRT + CAST|通过输入张量提供时,归约轴必须恒定| |ReduceLogSum|SUM + LOG(+ 可选 CAST)|Input/output dtype 必须为 FLOAT16/FLOAT32;当通过输入张量提供时,归约轴必须是常量| |ReduceLogSumExp|EXP + SUM + LOG(+ 可选 CAST)|Input/output dtype 必须为 FLOAT16/FLOAT32;当通过输入张量提供时,归约轴必须是常量| |ReduceMax|REDUCE_MAX|通过输入张量提供时,归约轴必须恒定| |ReduceMean|MEAN|通过输入张量提供时,归约轴必须恒定| |ReduceMin|REDUCE_MIN|通过输入张量提供时,归约轴必须恒定| |ReduceProd|REDUCE_PROD|通过输入张量提供时,归约轴必须恒定| |ReduceSumSquare|MUL + SUM(+ 可选 CAST)|Input/output dtype 必须为 FLOAT16/FLOAT32;当通过输入张量提供时,归约轴必须是常量| |ReduceSum|SUM|通过输入张量提供时,归约轴必须恒定| |热鲁|RELU|-| |重塑|RESHAPE|形状输入必须恒定| |调整大小|RESIZE_NEAREST_NEIGHBOR / RESIZE_BILINEAR / (立方) RESHAPE + BATCH_MUL + RESHAPE + BATCH_MUL|仅限 4 级。 nearest/linear:内置调整大小路径(有限的属性组合),参数必须是常量scales/sizes或动态Rank-1整数sizes(INT32/INT64)。 cubic:严格的ONNX立方分解(无FlexResizeBicubic),支持{align_corners, asymmetric, half_pixel, pytorch_half_pixel}中的coordinate_transformation_mode并荣誉cubic_coeff_a/exclude_outside;需要静态输入 C/H/W 和静态输出 H/W。通过三次分解路径保留批量维度| |ReverseSequence|CAST + REVERSE_SEQUENCE|输入等级必须为>=2; seq_lengths 必须是 1 阶整数张量; batch_axis/time_axis 必须在范围内且不同| |RoiAlign|CAST + GATHER + PAD + RESHAPE + ADD/SUB/MUL/DIV + MAXIMUM/MINIMUM + FLOOR + TILE + AVERAGE_POOL_2D / MAX_POOL_2D + TRANSPOSE|Input/output 仅排名=4; rois 等级=2 ([...,4]), batch_indices 等级=1 整数;输入 C/H/W 必须为静态正值; mode 中的 {avg,max}; coordinate_transformation_mode 中的 {half_pixel,output_half_pixel}; output_height/output_width 必须为正| |RotaryEmbedding|TRANSPOSE + SLICE + RESHAPE + CAST + MUL + SUB + ADD + CONCATENATION|当前内置路径仅支持Rank-4 input/output、interleaved=0,不支持position_ids输入;所有张量数据类型必须为 FLOAT16/FLOAT32,且输出数据类型与输入匹配;形状必须是静态正值; cos/sin 必须是 2 级,形状为 [seq_len, rotary_embedding_dim/2]; rotary_embedding_dim 必须是偶数且 = updates.shape[0] 的 1 阶整数张量; output dtype 必须匹配 data dtype; mode=circular 需要静态正轴暗淡| |塞鲁|MAXIMUM + MINIMUM + EXP + SUB + MUL + ADD|Input/output dtype 必须是 FLOAT16 或 FLOAT32| |形状|SHAPE(+ SLICE 为 start/end)|输出数据类型必须为 INT32 或 INT64; start/end 切片遵循 ONNX 标准化| |收缩|ADD + SUB + LESS + GREATER + SELECT_V2(+可选CAST)|Input/output dtype 必须是 FLOAT16 或 FLOAT32| |乙状结肠|LOGISTIC|-| |标志|SIGN|-| |罪恶|SIN|-| |辛赫|SUB + EXP + MUL|Input/output dtype 必须是 FLOAT16 或 FLOAT32| |尺寸|SHAPE + REDUCE_PROD(+ 可选 CAST)|通过Shape -> ReduceProd计算张量元素计数;输出 dtype 遵循 ONNX 输出类型 (INT32/INT64)| |StringNormalizer|RESHAPE(无操作)/ EQUAL + LOGICAL_OR + LOGICAL_NOT + WHERE + GATHER(+ EXPAND_DIMS 对于等级 2)/常量折叠|Input/output dtype 必须是 STRING,locale 必须是 '' 或 en_US。运行时路径仅支持 case_change_action=NONE(或空)。对于非常量输入,仅当 is_case_sensitive=1 且输入等级为 1 或 2 时才支持停用词过滤(rank=2 遵循当前 onnx2tf 行为并处理第一行)。常量输入路径在转换时折叠,并支持 LOWER/UPPER、不区分大小写的匹配和空结果回退 ("") 语义。| |切片|SLICE / STRIDED_SLICE / REVERSE_V2|starts 必须是常量 input/attr。 ends 通常为常数 input/attr;动态 ends 仅支持 1 级轴 0 前缀切片(start=0、step=1)。 steps=0 不受支持。仅通过 REVERSE_V2 对于全轴反向模式(start=-1、非常负 end、step=-1)支持负 steps| |软最大|SOFTMAX(+转置 in/out 用于非最后一个轴)|axis 必须在范围内(负轴标准化)| |SoftmaxCrossEntropyLoss|TRANSPOSE + SOFTMAX + LOG + CAST + EQUAL + SELECT_V2 + ONE_HOT + MUL + SUM + SUB(+可选GATHER/MEAN/DIV)|Input/output dtype 必须为 FLOAT16/FLOAT32;标签数据类型必须是整数;输入等级必须为 >=2,轴 1 处具有静态正类暗淡;可选权重必须是长度为 C 的 1 级浮点张量; reduction 中的 {none,sum,mean};支持可选输出[1] log-prob 张量| |软加|EXP + ADD + LOG|Input/output dtype 必须是 FLOAT16 或 FLOAT32| |软签名|ABS + ADD + DIV|Input/output dtype 必须是 FLOAT16 或 FLOAT32| |STFT|SLICE + MUL + RESHAPE + BATCH_MUL + CONCATENATION(+可选CAST)|当前内置路径仅支持 onesided=1 的 2 级信号输入; frame_step、window 和 frame_length 输入必须恒定;形状必须为静态正值,且为 signal_length >= frame_length; window 长度必须等于 frame_length;输出形状必须为 [batch, num_frames, frame_length//2 + 1, 2]| |SpaceToDepth|SPACE_TO_DEPTH|blocksize > 1,等级=4(NCHW)| |斯普利特|SLICE|axis 必须在范围内;显式分割大小 (input/attr) 必须是常量,并且计数必须与输出匹配;如果没有明确的分割大小,轴尺寸必须已知并且可以被输出计数整除| |平方根|SQRT|-| |挤压|SQUEEZE|通过输入张量提供时,轴必须恒定| |子|SUB|-| |总和|ADD(链接用于 >2 个输入)|至少 2 个输入| |谭|SIN + COS + DIV|Input/output dtype 必须是 FLOAT16 或 FLOAT32| |谭|TANH|-| |ThresholdedRelu|GREATER + CAST + MUL|Input/output dtype 必须是 FLOAT16 或 FLOAT32| |瓷砖|CAST + TILE|multiples 必须是 1 阶整数张量;如果输入等级是静态的,则 len(multiples) 必须与输入等级匹配;常量 multiples 必须为非负数| |TopK|TOPK_V2(+可选TRANSPOSE + NEG + CAST + SQUEEZE)|输入等级必须为>=1;输入数据类型必须为 FLOAT16/FLOAT32; axis 必须在范围内; largest 必须是 0 或 1; sorted 必须是 1; k 必须是类似标量([] 或 [1])和整数数据类型;索引输出 dtype 必须是 INT32 或 INT64| |转置|TRANSPOSE|排列输入必须是常数| |特里卢|MUL / LOGICAL_AND|输入等级必须为>=2;矩阵变暗必须是静态正数;可选 k 输入必须恒定| |独特|CAST + FLOOR_MOD + UNIQUE + CONCATENATION|输入数据类型必须是整数;输出[0] dtype 必须是整数; sorted 必须是 0 或 1;当指定axis时,仅支持axis=0,并且输入必须是具有静态正第二维的Rank-2;内置路径仅支持输出[0](其他输出必须未使用)| |松开|RESHAPE|轴必须是恒定且唯一的。轴规范化遵循输出等级语义 (output_rank = input_rank + len(axes)),因此支持 opset8 样式模式,例如 input_rank=2, axes=[2,3]| |上采样|RESIZE_NEAREST_NEIGHBOR / RESIZE_BILINEAR / (立方) RESHAPE + BATCH_MUL + RESHAPE + BATCH_MUL|由同一构建者降低的 Resize 的旧别名。仅限 4 级;支持约束 nearest/linear 内置调整大小和约束 cubic 分解路径。参数输入遵循 Upsample 2 输入形式 (scales/sizes),具有与 Resize 相同的 constant/dynamic 整数约束| |哪里|CAST + SELECT|条件输入 dtype 必须是 BOOL 或数字| |异或|NOT_EQUAL|-| Custom-op flatbuffer_direct 中的候选者(选择加入)
| ONNX OP | 默认正策 | 启用时 |
|---|---|---|
| DeformConv | 内置_支持受约束的标准 2D 浮动模式(group=1、offset_group=1);否则显式错误(custom_op_candidate_disabled) |
当启用 --flatbuffer_direct_allow_custom_ops 并通过白名单时,分组或不支持的模式可以降低为 TFLite CUSTOM |
| DynamicQuantizeLinear | 受限 float-input/uint8-output 模式上的builtin_supported;否则显式错误(custom_op_candidate_disabled) |
当启用 --flatbuffer_direct_allow_custom_ops 并通过白名单时,不支持的模式可以降低为 TFLite CUSTOM |
| 埃因苏姆 | 受约束方程的builtin_supported;否则显式错误(custom_op_candidate_disabled) |
当启用 --flatbuffer_direct_allow_custom_ops 且白名单通过时,不支持的方程可以降低为 TFLite CUSTOM |
| GridSample | 受限 rank-4/5 双线性模式上的builtin_supported;否则显式错误(custom_op_candidate_disabled) |
当启用 --flatbuffer_direct_allow_custom_ops 并通过白名单时,不支持的 GridSample 模式可以降低为 TFLite CUSTOM |
| 如果 | builtin_supported 受约束模式(NMS-guard、axis0 Add-branch、SequenceConstruct Add-branch 和嵌套 ReduceMin/Add);否则显式错误(custom_op_candidate_disabled) |
不支持 当启用 --flatbuffer_direct_allow_custom_ops 并通过白名单时,如果模式可以降低到 TFLite CUSTOM |
| LogSoftmax | 受约束轴模式上的builtin_supported;否则显式错误(custom_op_candidate_disabled) |
当启用 --flatbuffer_direct_allow_custom_ops 并通过白名单时,不支持的模式可以降低为 TFLite CUSTOM |
| 循环 | 受约束模式上的builtin_supported(静态展开/WHILE循环携带形式);否则显式错误(custom_op_candidate_disabled) |
当启用 --flatbuffer_direct_allow_custom_ops 并通过白名单时,不支持的循环模式可以降低为 TFLite CUSTOM |
| LSTM | 受限 forward/reverse/bidirectional 模式上的builtin_supported;否则显式错误(custom_op_candidate_disabled) |
当启用 --flatbuffer_direct_allow_custom_ops 并通过白名单时,不支持的模式可以降低为 TFLite CUSTOM |
| NonMaxSuppression | 受约束的 3 级 boxes/scores 模式上的builtin_supported;否则显式错误(custom_op_candidate_disabled) |
当启用 --flatbuffer_direct_allow_custom_ops 并通过白名单时,不支持的模式可以降低为 TFLite CUSTOM |
| QLinearConv | 受限 regular/depthwise 模式上的builtin_supported;否则显式错误(custom_op_candidate_disabled) |
当启用 --flatbuffer_direct_allow_custom_ops 并通过白名单时,不支持的分组模式可以降低为 TFLite CUSTOM |
| RoiAlign | 受约束模式上的builtin_supported;否则显式错误(custom_op_candidate_disabled) |
当启用 --flatbuffer_direct_allow_custom_ops 并通过白名单时,不支持的 RoiAlign 模式可以降低为 TFLite CUSTOM |
| 扫描 | 显式错误(custom_op_candidate_disabled) |
当启用 --flatbuffer_direct_allow_custom_ops 且通过白名单时,降低至 TFLite CUSTOM |
| ScatterElements | 受约束模式上的builtin_supported;否则显式错误(custom_op_candidate_disabled) |
当启用 --flatbuffer_direct_allow_custom_ops 并通过白名单时,不支持的 ScatterElements 模式可以降低为 TFLite CUSTOM |
| SequenceAt | 显式错误(custom_op_candidate_disabled) |
当启用 --flatbuffer_direct_allow_custom_ops 且通过白名单时,降低至 TFLite CUSTOM |
| SequenceConstruct | 显式错误(custom_op_candidate_disabled) |
当启用 --flatbuffer_direct_allow_custom_ops 且通过白名单时,降低至 TFLite CUSTOM |
| SequenceErase | 显式错误(custom_op_candidate_disabled) |
当启用 --flatbuffer_direct_allow_custom_ops 且通过白名单时,降低至 TFLite CUSTOM |
| SequenceInsert | 显式错误(custom_op_candidate_disabled) |
当启用 --flatbuffer_direct_allow_custom_ops 且通过白名单时,降低至 TFLite CUSTOM |
| SequenceLength | 显式错误(custom_op_candidate_disabled) |
当启用 --flatbuffer_direct_allow_custom_ops 且通过白名单时,降低至 TFLite CUSTOM |
| TopK | 受限 float-input/scalar-k 模式上的builtin_supported;否则显式错误(custom_op_candidate_disabled) |
当启用 --flatbuffer_direct_allow_custom_ops 并通过白名单时,不支持的模式可以降低为 TFLite CUSTOM |
| 独特 | 受约束整数模式上的builtin_supported(仅输出[0]);否则显式错误(custom_op_candidate_disabled) |
当启用 --flatbuffer_direct_allow_custom_ops 并通过白名单时,不支持的模式可以降低为 TFLite CUSTOM |
注意事项:
- 当
Einsum匹配内置约束时,现在被视为builtin_supported;如果启用了自定义操作模式,则不受支持的Einsum模式仍可能回退到CUSTOM。 - 对于 regular/depthwise 形态,
QLinearConv被视为builtin_supported;当启用自定义操作模式时,不受支持的分组模式可能仍会回退到CUSTOM。 - 当内置约束通过时,
LogSoftmax现在被视为builtin_supported;如果启用自定义操作模式,不支持的模式仍可能回退到CUSTOM。 - 对于受约束的 forward/reverse/bidirectional 模式,
LSTM现在被视为builtin_supported;如果启用自定义操作模式,不支持的模式仍可能回退到CUSTOM。 - 当内置约束通过时,
NonMaxSuppression现在被视为builtin_supported;如果启用自定义操作模式,不支持的模式仍可能回退到CUSTOM。 - 对于受约束的 float-input/uint8-output 模式,
DynamicQuantizeLinear现在被视为builtin_supported;如果启用自定义操作模式,不支持的模式仍可能回退到CUSTOM。 - 对于约束模式,
If现在被视为builtin_supported(NMS-guard、axis0 Add-branch、SequenceConstruct Add-branch 和嵌套 ReduceMin/Add);如果启用自定义操作模式,不支持的模式仍可能回退到CUSTOM。 - 对于约束 static-unroll/WHILE 循环进位模式,
Loop现在被视为builtin_supported;如果启用自定义操作模式,不支持的模式仍可能回退到CUSTOM。 StringNormalizer现在在受限运行时模式下被视为builtin_supported(case_change_action=NONE、区域设置''/en_US、rank1/2)。如果启用了自定义操作模式,则不受支持的运行时模式(e.g、LOWER/UPPER或不区分大小写的停用词过滤)可能仍会回退到CUSTOM。StringNormalizer常量输入图现在在转换时折叠,并在 flatbuffer_direct 中支持字符串常量缓冲区序列化(包括停用词过滤和LOWER/UPPER大小写转换)。- 对于可确定的存在情况(非可选输入和
Optional生产者谱系),OptionalHasElement现在被视为builtin_supported,从而减少ONNX_OPTIONALHASELEMENT自定义操作后备。 - 当内置约束通过时,
OneHot、MatMulInteger、Pow、Reciprocal和Inverse现在被视为builtin_supported。 ReduceMin现在在内置约束下被视为builtin_supported。Min和TopK现在在内置约束下被视为builtin_supported,从而减少ONNX_MIN/ONNX_TOPK自定义操作回退。DepthToSpace和HardSwish现在在内置约束下被视为builtin_supported(HardSwish直接降低为 TFLiteHARD_SWISH)。Pad内置路径现在支持通过PAD之前的CAST + RESHAPE + TRANSPOSE桥进行动态pads输入(等级 1 长度2*rank)。Pad内置路径现在通过降低到 TFLiteMIRROR_PAD(REFLECT模式)来支持mode=reflect。Pad内置路径现在通过降低到 TFLitePADV2(非量化张量)来支持恒定的非零填充值。Unsqueeze内置路径现在使用输出等级语义 (output_rank = input_rank + len(axes)) 标准化轴,支持 opset8 样式的情况,例如用于等级 2 输入的axes=[2,3]并减少ONNX_UNSQUEEZE自定义操作回退。ConvTranspose内置路径已扩展:约束一维降低 (EXPAND_DIMS -> TRANSPOSE_CONV -> SQUEEZE)、宽松的output_padding处理 (0 <= output_padding < stride) 以及输出形状静态时通过裁剪后的显式非零填充处理。Conv现在包括 5 级CONV_3D内置降低(group=1,恒定权重),减少 3D 转换子图上的ONNX_CONV自定义操作后备。ConvTranspose现在包括 5 级CONV_3D_TRANSPOSE内置降低(group=1、dilations=[1,1,1]、约束output_padding),减少 3D 反卷积子图上的ONNX_CONVTRANSPOSE自定义操作回退。NonMaxSuppression内置路径现在支持类暗淡>1,无需通过使用每个类NON_MAX_SUPPRESSION_V4/V5(由--switch_nms_version选择)并连接[batch, class, index]三元组(匹配默认值)来强制--output_nms_with_argmax未设置-onwa时的onnx2tf/ops/NonMaxSuppression.py行为)。AveragePool内置路径现在支持显式焊盘和{0,1}中的count_include_pad;当count_include_pad=0具有非零有效焊盘时,应用除数校正以保持 ONNX 语义。- 领先的输入转置直通优化现在将
CAST视为直通,从而减少冗余的Transpose -> Cast -> (Sub/Mul/...) -> Transpose链。 - 此更新中新添加的内置操作包括:
Abs,Acos,Acosh,And,ArgMin,Asin,Asinh,Atan,Atanh,BitShift,BitwiseAnd,BitwiseNot,BitwiseOr,BitwiseXor,Ceil,Celu,Cos,Cosh,Elu,Equal,EyeLike,Floor,GatherND,Gelu,Greater,GRU,Hardmax,Less,LessOrEqual,Mish,NonZero,Not,Or,Range,ReduceL1,ReduceL2,RNN,Round,Selu,Sign,Sin,Sinh,Softplus,Softsign,Tan,Trilu,Where和Xor。 - 在后续提交中添加的其他内置操作包括:
Erf,GlobalAveragePool,GlobalMaxPool,QLinearLeakyRelu,QLinearSoftmax,ScatterND,Slice,Split和Tile。 - 此更新中添加的新内置操作包括:
Log、Max、RoiAlign和ScatterElements。 - 除了常量
scales/sizes之外,Resize内置路径现在还接受动态 1 级整数sizes输入。 Resize(cubic)现在在tf_converter和flatbuffer_direct中使用严格的 ONNX 立方语义(包括cubic_coeff_a和exclude_outside)。GreaterOrEqual、RandomNormalLike和约束GridSample(rank-4/5、bilinear、padding_mode中的{zeros,border}、align_corners中的{0,1}) 现在在内置约束下被视为builtin_supported。Slice内置路径现在支持其他约束模式:通过REVERSE_V2的 1 级动态端前缀切片和全轴反向 (step=-1)。Abs内置路径现在通过降低INT64输入到NEG + MAXIMUM来避免不受支持的ABS(INT64)。- 当 4 级输入和静态空间大小可用时,
tf_converter现在更喜欢非 Flex 立方降低 (RESHAPE + BATCH_MUL + RESHAPE + BATCH_MUL),从而减少FlexResizeBicubic的生成。 flatbuffer_direct三次降低现在保留中间张量和输出张量上的批次元数据(RESHAPE/BATCH_MUL链中没有批次维度下降)。- NHWC 围绕 Conv 系列输出的传播已加强:
CONV_2D/DEPTHWISE_CONV_2D/TRANSPOSE_CONV输出现在可以在布局已为 NHWC 时避免冗余的立即转置后插入。 - 通过删除转置和重新映射缩减轴,为
TRANSPOSE(0,3,1,2) -> MEAN(keepDims=True) -> TRANSPOSE(0,2,3,1)添加了直接 NHWC 直通优化。 - HardSigmoid-related 转置直通已得到加强(包括扩展的
MUL+ADD+RELU_0_TO_1形式),减少了 activation/residual 链中冗余的转置包装器。 - 为
TRANSPOSE(0,3,1,2) -> PRELU -> TRANSPOSE(0,2,3,1)样式链添加了 PReLU 转置直通优化(包括每通道斜率重新映射)。 - 扩展预连接转置链优化,以在
CONCATENATION之前处理更广泛的一元运算和单通道重塑适配器。 - 添加了 ShuffleNet-style 转置洗牌优化,以减少长
Transpose/Reshape/Transpose/Reshape/Gather链,同时保留下游布局合同。 - 添加了针对 Softmax-mask 残差块的 SiNet-tail NHWC 优化:删除
MUL/ADD/PRELU + SOFTMAX + REDUCE_MAX + RESHAPE + MUL + ADD周围冗余的 pre/post 转置适配器,并将 axis/shape 常量重新映射到 NHWC 以避免终端转置桥。 - 为单路径转换链添加仿射折叠:当Conv输出不是多扇出时,
CONV_2D -> MUL(const) -> ADD(const)被折叠到Conv weights/bias中。 - 添加了钳位规范化:
MAXIMUM(0.0) -> MINIMUM(1.0)被重写为RELU_0_TO_1,以减少操作数并提高下游 transpose/activation 融合机会。 - 添加一元钳位规范化:当第二个输入为单例零(
input2=0)时,MAXIMUM(x, 0.0)被重写为RELU(x)以减少操作数。 - 反复降低实用注意事项:
LSTM内置支持{forward, reverse, bidirectional}中的direction并支持可选的运行时initial_h/initial_c输入和Y_h/Y_c输出(在内置形状约束下)。GRU内置支持forward/reverse/bidirectional,但需要激活[Sigmoid,Tanh]、clip=0,并且不需要激活sequence_lens。RNN内置支持{forward, reverse, bidirectional}中的direction(layout=0,无sequence_lens)。
tf_converter 与 flatbuffer_direct (操作差异)
| 项目 | tf_converter(默认) |
flatbuffer_direct |
|---|---|---|
| 最终后端 | TensorFlow Lite转换器 | 直接FlatBuffer构建器(schema.fbs) |
| 主要转化路径 | 构建 TF 图表 (op.make_node) 然后转换 |
直接从 ONNX IR 降低,无需 TF 图形构建(快速路径) |
| 模型优化源码 | 现有TF-路径图大集rewrites/heuristics | 专用直接预处理管道+直接调度约束 |
| 失败行为 | 经常被TF-侧图降低吸收 | 不支持的模式上基于 reason_code- 的显式失败 |
| 自定义操作处理 | 如果可能,通常通过 TF- 侧面更换来避免 | 仅选择加入 (--flatbuffer_direct_allow_custom_ops) 并列入白名单 |
| 诊断 | 标准转换日志 | *_op_coverage_report.json (dispatch_mode, unsupported_reason_counts, custom_op_policy, preprocess_report) |
| 回退 | N/A | N/A(无后备) |
| SavedModel 直接输出 | 从 TF 转换路径生成 | 来自 float32 ModelIR 的可选 --flatbuffer_direct_output_saved_model(无后备,不支持 CUSTOM) |
flatbuffer_direct预处理吸收范围
flatbuffer_direct 在降低之前运行分阶段预处理规则。目前主要覆盖范围:
pattern_fusion_wave2Relu -> Clip(min=0,max=6)链标准化- GELU 链融合(
Div -> Erf -> Add -> Mul -> Mul) Reshape -> Transpose -> Reshape至SpaceToDepth
quant_chain_fusion_wave3DequantizeLinear -> BatchNormalization -> PRelu -> QuantizeLinear链重写- BatchNormalization参数折叠成
Mul + Add
pseudo_ops_wave1LeakyRelu、有限的Pow和MatMulInteger重写为内置友好形式
constant_fold_a5- shape/axes 和算术辅助链的有限常数折叠
- 包括
DequantizeLinear(axis/block-size 感知)和下游Reshape常量子图折叠
normalize_attrs_a5- 规范化
perm/axes/negative-axis 形式和 softmax 轴桥重写
- 规范化
注意事项:
- 这减少了 TF-path 替换覆盖范围,但并不完全匹配。
- 要检查应用的内容,请使用
--report_op_coverage并检查preprocess_report.applied_rules。
已知的限制和解决方法选项
症状(reason_code) |
含义 | 建议采取的行动 |
|---|---|---|
unsupported_onnx_op |
节点没有直接的 builtin/custom 路径 | 使用 --tflite_backend tf_converter 或 rewrite/export 模型来支持模式 |
requires_constant_input |
节点需要编译时常量输入(e.g.、axes/perm/shape) | 预折叠 ONNX 图 (onnxsim) 或重写模型以常数化输入 |
unsupported_attribute_value |
直接内置约束不接受 Attribute/rank/value | 调整 ONNX 导出选项或在转换前重写有问题的子图 |
custom_op_candidate_disabled |
Op 位于自定义候选集中,但自定义降低已禁用 | 当运行时支持自定义操作时启用 --flatbuffer_direct_allow_custom_ops |
custom_op_not_in_allowlist |
自定义降低已启用,但 op 未列入允许名单 | 显式添加 op 到 --flatbuffer_direct_custom_op_allowlist |
演示
视频速度调整为比实际速度慢约 50 倍。
环境
- Linux / Windows
- Python>=3.12
- numpy==2.2.6
- onnx==1.20.1
- onnx运行时==1.26.0
- opencv-python==4.13.0.92
- onnxsim==0.6.5
- onnxoptimizer==0.4.2
- onnxscript==0.6.2
- ai-edge-litert==2.1.2
- sne4onnx==2.0.1
- sng4onnx==2.0.1
- psutil==5.9.5
- 协议缓冲区==7.35.1
- h5py==3.14.0
- ml_dtypes==0.5.4
- 安装工具==81.0.0
- 平面缓冲区==25.12.19
- tqdm==4.67.1
- pytest==9.0.2
可选:
- tensorflow==2.21.0(仅 TensorFlow-backed 导出/tf_converter)
- tf-keras==2.21.0(仅限 TensorFlow-backed 导出/tf_converter)
- keras==3.15.0(仅 TensorFlow-backed 导出/tf_converter)
- torch==2.11.0(仅 PyTorch-backed 导出/验证)
使用示例
1. 安装
注意:
1.如果您使用的是 TensorFlow v2.13.0 或更早版本,请使用早于 onnx2tf v1.17.5 的版本。由于 TensorFlow 的 API 的更改,onnx2tf v1.17.6 或更高版本将无法正常工作。
2.最新的 onnx2tf 实现基于 Keras API 3,如果您安装 TensorFlow v2.15.0 或更早版本,则将无法正常工作。
3.从onnx2tf v2.0.0开始,由于onnxruntime问题,onnx2tf将不再支持Python 3.10之前的环境。因此,Docker Image 已升级至 Ubuntu 24.04。对 onnx-graphsurgeon 的依赖也已完全删除。 onnxruntime v1.24.1: https://github.com/microsoft/onnxruntime/releases/tag/v1.24.1
- HostPC
Click 至 expand
-
使用GHCR时,参见
Authenticating to the Container registryhttps://docs.github.com/en/packages/working-with-a-github-packages-registry/working-with-the-container-registry#authenticating-to-the-container-registry
# PAT authentication is required to pull from GHCR.
docker login ghcr.io
Username (xxxx): {Enter}
Password: {Personal Access Token}
Login Succeeded
# Start an interactive session on the terminal.
docker run --rm -it
-v `pwd`:/workdir
-w /workdir
ghcr.io/pinto0309/onnx2tf:2.6.8
or
# Authentication is not required for pulls from Docker Hub.
# Start an interactive session on the terminal.
docker run --rm -it
-v `pwd`:/workdir
-w /workdir
docker.io/pinto0309/onnx2tf:2.6.8
or
# Direct execution in Docker
# The model conversion is performed within Docker,
# but the model is output to the host PC's storage.
docker run --rm
--user $(id -u):$(id -g)
-v $(pwd):/work
docker.io/pinto0309/onnx2tf:2.6.8
onnx2tf -i /work/densenet-12.onnx -o /work/saved_model
or
curl -LsSf https://astral.sh/uv/install.sh | sh
uv python install 3.12.12
uv venv -p 3.12.12 .venv
source .venv/bin/activate
uv pip install -U onnx2tf
or
# Install TensorFlow-backed features too (tf_converter, SavedModel/H5/Keras exports).
uv pip install -U 'onnx2tf[tensorflow]'
or
# Install PyTorch-backed features too (native package / TorchScript / Dynamo ONNX / ExportedProgram).
uv pip install -U 'onnx2tf[torch]'
or
# Install all optional features at once.
uv pip install -U 'onnx2tf[tensorflow,torch]'
or
curl -LsSf https://astral.sh/uv/install.sh | sh
uv python install 3.12.12
uv venv -p 3.12.12 .venv
source .venv/bin/activate
uv sync
or
# Sync with TensorFlow-backed features enabled.
uv sync --extra tensorflow
or
# Sync with PyTorch-backed features enabled.
uv sync --extra torch
or
# Sync with all optional features enabled.
uv sync --all-extras
or
pip install -e .
or
pip install -e '.[tensorflow]'
or
pip install -e '.[torch]'
or
pip install -e '.[tensorflow,torch]'
or
docker buildx build
--platform linux/amd64
--build-arg BUILD_ARCH=linux/amd64
--progress=plain
-t onnx2tf:amd64
--load .
or
# It is possible to cross-compile an arm64 environment on an x64 environment.
docker buildx build
--platform linux/arm64
--build-arg BUILD_ARCH=linux/arm64
--progress=plain
-t onnx2tf:arm64
--load .
2. 运行测试
仅描述被认为特别频繁使用的模式。此外,还有其他几个选项,例如禁用 Flex OP 以及用于提高推理性能的其他选项。参见:CLI 参数
# Float32, Float16
# This is the fastest way to generate tflite.
# Add `-fdosm` if you also want SavedModel output on the default backend.
# Also, starting from v1.24.0, efficient TFLite can be generated
# without unrolling `GroupConvolution`. e.g. YOLOv9, YOLOvN
# Conversion to other frameworks. e.g. TensorFlow.js, CoreML, etc
# https://github.com/PINTO0309/onnx2tf#19-conversion-to-tensorflowjs
# https://github.com/PINTO0309/onnx2tf#20-conversion-to-coreml
wget https://github.com/PINTO0309/onnx2tf/releases/download/0.0.2/resnet18-v1-7.onnx
onnx2tf -i resnet18-v1-7.onnx -fdosm
ls -lh saved_model/
assets
fingerprint.pb
resnet18-v1-7_float16.tflite
resnet18-v1-7_float32.tflite
saved_model.pb
variables
TF_CPP_MIN_LOG_LEVEL=3
saved_model_cli show
--dir saved_model
--signature_def serving_default
--tag_set serve
The given SavedModel SignatureDef contains the following input(s):
inputs['data'] tensor_info:
dtype: DT_FLOAT
shape: (-1, 224, 224, 3)
name: serving_default_data:0
The given SavedModel SignatureDef contains the following output(s):
outputs['output_0'] tensor_info:
dtype: DT_FLOAT
shape: (-1, 1000)
name: PartitionedCall:0
Method name is: tensorflow/serving/predict
# In the interest of efficiency for my development and debugging of onnx2tf,
# the default configuration shows a large amount of debug level logs.
# However, for most users, a large number of debug logs are unnecessary.
# If you want to reduce the amount of information displayed in the conversion log,
# you can change the amount of information in the log by specifying the
# `--verbosity` or `-v` option as follows.
# Possible values are "debug", "info", "warn", and "error".
wget https://github.com/PINTO0309/onnx2tf/releases/download/0.0.2/resnet18-v1-7.onnx
onnx2tf -i resnet18-v1-7.onnx -v info
# Override undefined batch size or other dimensions with static values.
# If the model has undefined dimensions, rewriting them to a static size will significantly
# improve the success rate of the conversion.
# The `-b` option overwrites the zero-dimensional batch size with the number specified
# without input OP name.
# Note that if there are multiple input OPs, the zero dimension of all input OPs is
# forced to be rewritten.
# The `-sh/--shape-hints` option provides shape hints for input tensors with undefined
# dimensions, significantly improving the conversion success rate for models with dynamic
# input shapes. Specifying this option in combination with the `-b` option will further
# improve the success rate of model conversion. The `-sh` option does not change ONNX
# input OPs to static shapes.
# The `-ois/--overwrite_input_shape` option allows undefined dimensions in all dimensions,
# including the zero dimensionality, to be overwritten to a static shape, but requires
# the input OP name to be specified.
# e.g. -ois data1:1,3,224,224 data2:1,255 data3:1,224,6
wget https://github.com/PINTO0309/onnx2tf/releases/download/0.0.2/resnet18-v1-7.onnx
onnx2tf -i resnet18-v1-7.onnx -b 1
or
onnx2tf -i resnet18-v1-7.onnx -sh data:1,3,224,224 -b 1
or
onnx2tf -i resnet18-v1-7.onnx -ois data:1,3,224,224
# Suppress automatic transposition of input OPs from NCW, NCHW, NCDHW to NWC, NHWC, NDHWC.
# onnx2tf is a specification that automatically transposes the input OP to [N,H,W,C] format
# before converting the model. However, since onnx2tf cannot determine from the structure of
# the model whether the input data is image, audio data, or something else, it unconditionally
# transposes the channels. Therefore, it is the models of STT/TTS models where the input is
# not NHWC that tend to have particular problems with the automatic transposition of the
# input OP.
# If you do not want input OPs to be automatically transposed, you can disable automatic
# transposition of input OPs by specifying the `-kat` option.
wget https://github.com/PINTO0309/onnx2tf/releases/download/1.1.28/double_gru.onnx
# INPUT OPs: "spec": float32[1,3,257,1], "states_in": float32[2,1,32]
# The following command suppresses the automatic transposition of "states_in" and converts it.
onnx2tf -i double_gru.onnx -kat states_in
# Keras h5 format
# .h5, .json, .keras, .weights.h5, .weights.keras, .data-00000-of-00001, .index
wget https://github.com/PINTO0309/onnx2tf/releases/download/0.0.2/resnet18-v1-7.onnx
onnx2tf -i resnet18-v1-7.onnx -oh5
# Keras keras_v3 format (TensorFlow v2.12.0 or later only)
wget https://github.com/PINTO0309/onnx2tf/releases/download/0.0.2/resnet18-v1-7.onnx
onnx2tf -i resnet18-v1-7.onnx -okv3
# TensorFlow v1 (.pb) format
wget https://github.com/PINTO0309/onnx2tf/releases/download/0.0.2/resnet18-v1-7.onnx
onnx2tf -i resnet18-v1-7.onnx -otfv1pb
# Automatic JSON generation only
# Generates an optimal parameter replacement JSON file for model conversion.
# The JSON file is saved to {model_name}_auto.json when conversion errors occur
# or accuracy issues are detected and the feature is explicitly enabled.
onnx2tf -i model.onnx -agj
# Accuracy validation only (no JSON generation)
# Validates the accuracy between ONNX and TensorFlow outputs without generating
# any parameter replacement JSON file.
onnx2tf -i model.onnx -cotof
# Accuracy validation + automatic JSON generation
# First generates an optimal parameter replacement JSON file, then uses it
# to validate the model accuracy. This ensures the best possible conversion accuracy.
onnx2tf -i model.onnx -agj -cotof
# Accuracy validation with opt-in JSON generation on error
# Generates a parameter replacement JSON only when accuracy errors greater than 1e-2
# are detected during validation.
onnx2tf -i model.onnx -cotof -agje
# INT8 Quantization, Full INT8 Quantization
# INT8 Quantization with INT16 activation, Full INT8 Quantization with INT16 activation
# Dynamic Range Quantization
wget https://github.com/PINTO0309/onnx2tf/releases/download/1.1.1/emotion-ferplus-8.onnx
# INT8 Quantization (per-channel)
onnx2tf -i emotion-ferplus-8.onnx -oiqt
# INT8 Quantization (per-tensor)
onnx2tf -i emotion-ferplus-8.onnx -oiqt -qt per-tensor
# Split the model at the middle position for debugging
# Specify the input name of the OP
wget https://github.com/PINTO0309/onnx2tf/releases/download/1.25.0/cf_fus.onnx
onnx2tf -i cf_fus.onnx -inimc 448
# Split the model at the middle position for debugging
# Specify the output name of the OP
wget https://github.com/PINTO0309/onnx2tf/releases/download/1.25.0/cf_fus.onnx
onnx2tf -i cf_fus.onnx -onimc dep_sec
# Split the model at the middle position for debugging
# Specify the input/output name of the OP
wget https://github.com/PINTO0309/onnx2tf/releases/download/1.25.0/cf_fus.onnx
onnx2tf -i cf_fus.onnx -inimc 448 -onimc velocity
# Suppress generation of Flex OP and replace with Pseudo-Function
# [
# Asin, Acos, Atan, Abs, PReLU,
# LeakyReLU, Power, GatherND,
# Neg, HardSwish, Erf, GeLU, MatMulInteger,
# ]
# Below is a sample of replacing Erf with another set of operations.
wget https://s3.ap-northeast-2.wasabisys.com/temp-models/onnx2tf_readme/Erf_11.onnx
onnx2tf -i Erf_11.onnx -rtpo Erf
# High-dimensional Transpose decomposition
# If you do not like FlexTranspose being generated, try `-nodaftc`.
# Suppresses the generation of FlexTranspose by decomposing Transpose
# to the specified number of dimensions.
# In TensorFlow v2.12.0 and later, up to 6 dimensions are converted to normal Transpose;
# in v2.11.0 and earlier, up to 5 dimensions are converted to normal Transpose.
# Note that specifying `2` for the `-nodaftc` option causes all Transpose OPs to disappear
# from the model structure.
# Below is an example of decomposing a Transpose of 5 or more dimensions into a Transpose
# of 4 dimensions.
onnx2tf -i xxxx.onnx -nodaftc 4
# High-dimensional Slice(StridedSlice) decomposition
# If your special circumstances do not allow you to deploy a `StridedSlice` with more than
# 5 dimensions to a device, you can use the `-nodafsc` option to decompose the `StridedSlice`
# into a process with 4 or fewer dimensions.
# Below is an example of decomposing a `StridedSlice` of 5 or more dimensions into a
# `StridedSlice` of 4 dimensions.
onnx2tf -i xxxx.onnx -nodafsc 4
# Float16 inference doubling on devices with ARM64 ARMv8.2 or higher instruction set
# Double the inference speed with Float16 precision tflite models on devices with
# high-performance CPUs such as Snapdragon.
# (Pixel 3a, Pixel 5a, Pixel 7, Galaxy M12 and Galaxy S22, ...)
# XNNPACK float16 inference on certain ARM64 cores is 2x faster.
# Unfortunately, Float16 inference cannot be accelerated when using the RaspberryPi4's
# ARM64 CPU.
onnx2tf -i xxxx.onnx -eatfp16
# Parameter replacement (Resize,Transpose,Softmax)
rm replace.json
wget https://github.com/PINTO0309/onnx2tf/releases/download/1.1.27/human_segmentation_pphumanseg_2021oct.onnx
wget https://github.com/PINTO0309/onnx2tf/releases/download/1.1.27/replace.json
onnx2tf -i human_segmentation_pphumanseg_2021oct.onnx -prf replace.json
3. 准确性检查
Click 至 expand
对 ONNX 输出和 TensorFlow 输出进行错误检查。验证所有输出(一次一个操作)的误差是否低于特定阈值。自动确定OPs工具自动转换模型失败的前后时间。了解 Reshape 和 Traspose 的维度压缩、维度扩展和维度转置在哪里失败。确定问题区域后,您可以参考 参数替换 的教程来修改工具的行为。
经过多次升级后,对 JSON 参数校正的需求已变得不那么常见,但仍然存在一些需要 JSON 校正的边缘情况。如果 PC 在其 RAM 中有足够的可用空间,onnx2tf 将转换模型,同时对所有 OPs 仔细执行精度检查。因此,以模型转换成功为代价,转换速度慢了一些。如果精度检查所需的 RAM 数量预计超过整个 PC 的总可用 RAM 容量的 80%,则将在不进行精度检查的情况下执行转换操作。因此,如果发现转换后的模型精度明显下降,可以在RAM数量较多的PC上通过重新转换来自动修正精度。例如,我的PC有128GB的RAM,但是StableDiffusion v1.5模型结构太复杂,总共消耗了大约180GB的RAM和50GB的SWAP空间。
-ois 如果输入 OP 具有未定义的尺寸,则可以将其覆盖为静态尺寸。 -cotof 选项一一检查所有 OPs 的准确性。 -cotoa是确定精度误差的阈值的误差值。如果输入OP中存在未定义的尺寸,最好将它们固定到静态几何体上,以提高精度测量的准确性。
此外,您还可以使用 -cind 选项为 -cotof 指定自定义输入,而不是使用默认的虚拟输入。否则,所有输入值将设置为 1。您可以使用 --value_hints 覆盖虚拟输入值(仅限标量,支持 *:default)。有关 -cind 选项的更多信息,请参阅此处的 这里。如果您的输入是NHWC格式的图像数据,您还可以使用--test_data_nhwc_path提供固定的测试样本进行验证。对于 -fdots,解析动态走线形状的推荐方法是 --shape_hints。 --test_data_nhwc_path 也适用于符合条件的 4D RGB 输入,并且当需要每个输入自定义数据时,-cind 仍然可用。
-tdnp 和 -cind 之间的快速区别:
-tdnp(--test_data_nhwc_path):仅用于准确性检查的验证测试数据。预计为一个 NHWC RGB.npy([N,H,W,3])。没有mean/std。对于多输入模型,这个单个数组可以跨输入重复使用(不支持每个输入映射)。也被-fdots接受,用于合格的 4D RGB 输入。-cind(--custom_input_op_name_np_data_path):按输入名称按输入自定义数据映射。支持 multi-input/non-image 输入。还用于-fdots迹线输入和 INT8 校准 (-oiqt) 以及可选的mean/std。
-cotof 选项仅评估 Float32 精度。在默认的 flatbuffer_direct 路径上,基本报告为 ONNX↔TFLite。当显式使用 --tflite_backend tf_converter 时,它会根据 TensorFlow/TFLite 输出检查 ONNX。如果还启用了 --flatbuffer_direct_output_pytorch,onnx2tf 还会使用相同的输入样本发出 ONNX↔PyTorch 和组合比较报告。
onnx2tf -i mobilenetv2-12.onnx -ois input:1,3,224,224 -cotof -cotoa 1e-1
or
onnx2tf -i mobilenetv2-12.onnx -b 1 -cotof -cotoa 1e-1
or
onnx2tf -i mobilenetv2-12.onnx -cotof -cotoa 1e-1 -cind "input" "/your/path/x.npy"
or
onnx2tf -i mobilenetv2-12.onnx -cotof -cotoa 1e-1 -tdnp "/your/path/test_data_nhwc.npy"
or
onnx2tf -i mobilenetv2-12.onnx -cotof -cotoa 1e-1 --value_hints "input:0.5" "*:1.0"
4. 将 tflite input/output 名称和 input/output 顺序匹配到 ONNX
Click 至 expand
如果您想将 tflite 的 input/output OP 名称以及 input/output OPs 与 ONNX 的顺序进行匹配,则可以在使用-coion / --copy_onnx_input_output_names_to_tflite 选项输出 tflite 文件。请参阅:https://github.com/PINTO0309/onnx2tf/issues/228
onnx2tf自动比较ONNX的最终input/output形状和生成的TFLite,并在存在差异时尝试尽可能自动纠正input/output顺序。但是,如果使用 INT8 量化并且存在多个具有相同形状的输入和输出,则自动校正可能会失败。这是因为仅当执行 INT8 量化时,TFLiteConverter 才会自行打乱输入输出顺序。
import torch
import onnxruntime
import numpy as np
import onnx2tf
import tensorflow as tf
from ai_edge_litert.interpreter import Interpreter
class Model(torch.nn.Module):
def forward(self, x, y):
return {
"add": x + y,
"sub": x - y,
}
# Let's double check what PyTorch gives us
model = Model()
pytorch_output = model.forward(10, 2)
print("[PyTorch] Model Predictions:", pytorch_output)
# First, export the above model to ONNX
torch.onnx.export(
Model(),
{"x": 10, "y": 2},
"model.onnx",
opset_version=16,
input_names=["x", "y"],
output_names=["add", "sub"],
)
# And check its output
session = onnxruntime.InferenceSession("model.onnx")
onnx_output = session.run(["add", "sub"], {"x": np.array(10), "y": np.array(2)})
print("[ONNX] Model Outputs:", [o.name for o in session.get_outputs()])
print("[ONNX] Model Predictions:", onnx_output)
# Now, let's convert the ONNX model to TF
onnx2tf.convert(
input_onnx_file_path="model.onnx",
output_folder_path="model.tf",
copy_onnx_input_output_names_to_tflite=True,
non_verbose=True,
)
# Now, test the newer TFLite model
interpreter = Interpreter(model_path="model.tf/model_float32.tflite")
tf_lite_model = interpreter.get_signature_runner()
inputs = {
'x': np.asarray([10], dtype=np.int64),
'y': np.asarray([2], dtype=np.int64),
}
tf_lite_output = tf_lite_model(**inputs)
print("[TFLite] Model Predictions:", tf_lite_output)
[PyTorch] Model Predictions:
{
'add': 12,
'sub': 8
}
[ONNX] Model Outputs:
[
'add',
'sub'
]
[ONNX] Model Predictions:
[
array(12, dtype=int64),
array(8, dtype=int64)
]
[TFLite] Model Predictions:
{
'add': array([12]),
'sub': array([8])
}
5. tflite input/output OP 名称和 signature_defs 的重写
Click 至 expand
如果您不喜欢 tflite input/output 名称,例如 serving_default_*:0 或 StatefulPartitionedCall:0,您可以使用以下工具和过程重写它们。它可以从任何名称重写为任何名称,因此不必是 serving_default_*:0 或 StatefulPartitionedCall:0。
https://github.com/PINTO0309/tflite-input-output-rewriter
# Install tfliteiorewriter
pip install -U tfliteiorewriter
-
之前
tfliteiorewriter -i xxxx.tflite -r serving_default_input_1:0 aaa -r StatefulPartitionedCall:0 bbb -
之后
6. 在 tflite 中嵌入元数据
Click 至 expand
如果你想将标签图、量化参数、描述等嵌入到你的tflite文件中,你可以参考官方教程并自行尝试。目前,该工具不打算实现附加元数据的功能,因为我不想将字节数组写入 tflite 文件,这对于其操作来说不是必需的。
-
向 TensorFlow Lite 模型添加元数据
https://www.tensorflow.org/lite/models/convert/metadata
7. 如果INT8量化模型的精度明显下降
8. INT8 量化的校准数据创建
Click 至 expand
INT8 量化 (-cind) 的校准数据 (.npy) 生成如下。这是当用于训练的数据是图像数据时的示例。请参阅:https://github.com/PINTO0309/onnx2tf/issues/222
https://www.tensorflow.org/lite/performance/post_training_quantization
import cv2
import glob
import numpy as np
# Not used during data generation ################################
# You will need to do the calculations yourself using the test data
MEAN = np.asarray([[[[0.485, 0.456, 0.406]]]], dtype=np.float32) # [1,1,1,3]
STD = np.asarray([[[[0.229, 0.224, 0.225]]]], dtype=np.float32) # [1,1,1,3]
# Not used during data generation ################################
files = glob.glob("data/*.png")
img_datas = []
for idx, file in enumerate(files):
bgr_img = cv2.imread(file)
rgb_img = cv2.cvtColor(bgr_img, cv2.COLOR_BGR2RGB)
resized_img = cv2.resize(rgb_img, dsize=(200,112))
extend_batch_size_img = resized_img[np.newaxis, :]
normalized_img = extend_batch_size_img / 255.0 # 0.0 - 1.0
print(
f'{str(idx+1).zfill(2)}. extend_batch_size_img.shape: {extend_batch_size_img.shape}'
) # [1,112,200,3]
img_datas.append(extend_batch_size_img)
calib_datas = np.vstack(img_datas)
print(f'calib_datas.shape: {calib_datas.shape}') # [10,112,200,3]
np.save(file='data/calibdata.npy', arr=calib_datas)
loaded_data = np.load('data/calibdata.npy')
print(f'loaded_data.shape: {loaded_data.shape}') # [10,112,200,3]
"""
-cind INPUT_NAME NUMPY_FILE_PATH MEAN STD
int8_calib_datas = (loaded_data - MEAN) / STD # -1.0 - 1.0
e.g. How to specify calibration data in CLI or Script respectively.
1. CLI
-cind "pc_dep" "data/calibdata.npy" "[[[[0.485,0.456,0.406]]]]" "[[[[0.229,0.224,0.225]]]]"
-cind "feat" "data/calibdata2.npy" "[[[[0.123,...,0.321]]]]" "[[[[0.112,...,0.451]]]]"
2. Script
custom_input_op_name_np_data_path=[
["pc_dep", "data/calibdata.npy", [[[[0.485,0.456,0.406]]]], [[[[0.229,0.224,0.225]]]]],
["feat", "data/calibdata2.npy", [[[[0.123,...,0.321]]]], [[[[0.112,...,0.451]]]],
]
"""
9. INT8 需要非图像数据的多输入模型的量化
Click 至 expand
如果您不需要单独使用此工具执行 INT8 量化,则以下方法是最简单的。
在默认的 flatbuffer_direct 后端,将 -fdosm 与 -osd 组合,在 saved_model 文件夹中输出 saved_model.pb ,其具有量化所需的完整大小。即.pb中嵌入了一个名为serving_default的默认签名。如果您需要旧行为,请显式使用 --tflite_backend tf_converter。 -b 选项用于通过将其重写为静态整数来转换批量大小。
注意:按照此过程生成的 INT8 TFLite 将导致模型的准确性显着降低。本教程仅演示INT8量化过程;如果希望修正精度,请参考参数替换来修正操作中的换位错误。
# Ref: https://github.com/onnx/models/tree/main/text/machine_comprehension/bert-squad
wget https://s3.ap-northeast-2.wasabisys.com/temp-models/onnx2tf_248/bertsquad-12.onnx
onnx2tf -i bertsquad-12.onnx -b 1 -fdosm -osd -cotof
使用saved_model_cli命令检查saved_model签名。 INT8 使用签名的量化校准可以正确控制校准数据的输入顺序。因此,建议对具有多个输入的模型进行 INT8 量化时使用签名进行校准。
saved_model_cli show --dir saved_model/ --tag_set serve --signature_def serving_default
The given SavedModel SignatureDef contains the following input(s):
inputs['input_ids_0'] tensor_info:
dtype: DT_INT64
shape: (1, 256)
name: serving_default_input_ids_0:0
inputs['input_mask_0'] tensor_info:
dtype: DT_INT64
shape: (1, 256)
name: serving_default_input_mask_0:0
inputs['segment_ids_0'] tensor_info:
dtype: DT_INT64
shape: (1, 256)
name: serving_default_segment_ids_0:0
inputs['unique_ids_raw_output___9_0'] tensor_info:
dtype: DT_INT64
shape: (1)
name: serving_default_unique_ids_raw_output___9_0:0
通过指定 inputs 中显示的输入 OP 名称进行校准。必须用正确的校准测试数据更换 np.ones([xxx], dtype=np.int64) 部件。在实践中,会提取并使用几条用于训练的数据。
import tensorflow as tf
import numpy as np
def representative_dataset():
unique_ids = np.ones([10, 256], dtype=np.int64)
segment_ids = np.ones([10, 256], dtype=np.int64)
input_masks = np.ones([10, 256], dtype=np.int64)
input_ids = np.ones([10], dtype=np.int64)
for unique_id, segment_id, input_mask, input_id
in zip(unique_ids, segment_ids, input_masks, input_ids):
yield {
"unique_ids_raw_output___9_0": unique_id,
"segment_ids_0": segment_id,
"input_mask_0": input_mask,
"input_ids_0": input_id,
}
converter = tf.lite.TFLiteConverter.from_saved_model('saved_model')
converter.optimizations = [tf.lite.Optimize.DEFAULT]
converter.representative_dataset = representative_dataset
converter.target_spec.supported_ops = [tf.lite.OpsSet.TFLITE_BUILTINS_INT8]
converter.inference_input_type = tf.int8 # or tf.uint8
converter.inference_output_type = tf.int8 # or tf.uint8
tflite_quant_model = converter.convert()
with open('saved_model/int8_model.tflite', 'wb') as w:
w.write(tflite_quant_model)
https://www.tensorflow.org/lite/performance/post_training_quantization
请参阅:https://github.com/PINTO0309/onnx2tf/issues/248
10.修复NonMaxSuppression的输出(NMS)
Click 至 expand
PyTorch 的 NonMaxSuppression (torchvision.ops.nms) 和 ONNX 的 NonMaxSuppression 不完全兼容。 TorchVision的NMS效率很低。因此,在目标检测模型和其他模型中使用 NMS 转换 ONNX 不可避免地会非常冗余,并且将转换为 TensorFlow.js 和 TFLite 模型难以在设备中利用的结构。这是由于 NMS 输出的张量数量不定。在本章中,我将分享如何轻松调整使用 TorchVision 的冗余 NMS 生成的 ONNX,以生成优化的 NMS。
-
TorchVision 的 NMS 存在多个问题。首先,不支持batch size规格;其次,不能指定
max_output_boxes_per_class参数。请参阅我生成的 NMS 示例 ONNX 第一部分。max_output_boxes_per_class已更改为896,而不是-Infinity。 TorchVision NMS 的最大问题是,它生成 ONNX,其中max_output_boxes_per_class设置为-Infinity或9223372036854775807 (Maximum value of INT64),导致 NMS 输出的数量从零到无穷大。因此,通过将-Infinity或9223372036854775807 (Maximum value of INT64)重写为常数值,可以输出可以通过TFJS或TFLite轻松推断的NMS。Here you will find committed ONNX components optimized for various devices. https://github.com/PINTO0309/components_of_onnx/tree/main/components_of_onnx/ops
-
下面的例子中,YOLOv7生成的后处理中的NMS的
max_output_boxes_per_class由-Infinity或9223372036854775807 (Maximum value of INT64)更改为20,如下图所示。为了清楚起见,我重写了名称main01_max_output_boxes_per_class,但它最初显示为max_output_boxes_per_class。Simply execute the following command. The command rewrites the specified attribute value of the OP specified by ONNX.
pip install sam4onnx sam4onnx --op_name main01_nonmaxsuppression11 --input_onnx_file_path yolov7.onnx --output_onnx_file_path nms_yolov7_update.onnx --input_constants main01_max_output_boxes_per_class int64 [20]A tutorial on one of my ONNX modification tools,
sam4onnx, can be found here.https://github.com/PINTO0309/sam4onnx
Many detailed tutorials are provided below, so if you are interested, please play with them.
https://github.com/PINTO0309/PINTO_model_zoo/tree/main/307_YOLOv7/post_process_gen_tools
-
最后,只需使用onnx2tf将ONNX转换为TFLite或saved_model或TFJS即可。如果
max_output_boxes_per_class设置为-Infinity和9223372036854775807 (Maximum value of INT64)以外的值,onnx2tf 会执行内部操作,自动将 NMS 输出优化为固定形状。如果您不想针对固定形状进行优化,请指定--output_nms_with_dynamic_tensor或-onwdt。如果要将 NMS 中的班级分数从[B, C, N]缩小到[B, 1, N],请启用--output_nms_with_argmax或-onwa。onnx2tf -i nms_yolov7_update.onnx -fdosm -osd -cotofI would be happy if this is a reference for Android + Java or TFJS implementations. There are tons more tricky model optimization techniques described in my blog posts, so you'll have to find them yourself. I don't dare to list the URL here because it is annoying to see so many
issuesbeing posted. And unfortunately, all articles are in Japanese.
11. RNN(RNN、GRU、LSTM)推理加速
Click 至 expand
TensorFlow 的 RNN 有一个名为 unroll 的加速选项。网络将展开,否则将使用符号循环。展开可以加快 RNN 的速度,尽管它往往会占用更多内存。展开仅适用于短序列。 onnx2tf 允许您通过指定 --enable_rnn_unroll 或 -eru 选项将 RNNs 部署到内存密集型操作中。 --enable_rnn_unroll 选项适用于 RNN、GRU 和 LSTM。
- Keras https://keras.io/api/layers/recurrent_layers/lstm/
- TensorFlow https://www.tensorflow.org/api_docs/python/tf/keras/layers/LSTM
下面显示了使用 --enable_rnn_unroll 选项进行 BidirectionalLSTM 转换的示例。请忽略输入和输出张量的形状不匹配,因为样本是通过选取单独的模型来显示的。
-
ONNX
LSTM (Bidirectional) -
BidirectionalLSTM,其中--enable_rnn_unroll选项未指定循环层是从头开始实现的。
-
BidirectionalLSTM带有--enable_rnn_unroll选项
12.如果Float32模型的精度明显下降
Click 至 expand
转换模型的精度下降模式不仅在执行 INT8 量化时发生。一种特殊的边缘情况是当 TFLite 运行时端上的特定 OP 的实现出现问题时。下面,我将通过一个非常简单的 CNN 模型来重现该问题,并进一步解释其解决方法。这是促使我添加此解释的问题。 [Conv-TasNet] 转换 Conv-TasNet 模型时遇到问题 #447
下载示例模型进行验证。
curl
-L https://github.com/PINTO0309/onnx2tf/files/12367312/prelu_check.onnx.zip
-o prelu_check.onnx.zip
unzip prelu_check.onnx.zip
下载的模型中出现问题的部分是下图中的PRelu部分。
- ONNX
重现问题。以下命令将 ONNX 文件转换为 TFLite 文件。
onnx2tf -i prelu_check.onnx -cotof
转换成功,如下图所示,ONNX的推理测试结果与TensorFlow(Keras)中Float32模型的推理结果完美匹配。需要注意的是,ONNX 和 TensorFlow 转换模型之间的推理结果比较是 ONNX 模型与 TensorFlow (Keras) 模型的比较,而不是 ONNX 模型与 TFLite 模型的比较。
-
转换结果
-
特氟利特
现在,让我们尝试使用 TFLite 运行时而不是 TensorFlow 运行时进行推理。
test.pyimport time import numpy as np np.random.seed(0) from ai_edge_litert.interpreter import Interpreter # Load TFLite model interpreter = Interpreter(model_path="./saved_model/prelu_check_float32.tflite") interpreter.allocate_tensors() tensor_shape = (256, 20) input_data = {'waveform': np.random.randn(*tensor_shape).astype(np.float32)} # Load and preprocess input_details = interpreter.get_input_details() input_shape = input_details[0]['shape'] print(input_shape) # Run inference interpreter.set_tensor(input_details[0]['index'], input_data["waveform"]) separate_time = time.time() interpreter.invoke() print("Done! {:.3f} s".format(time.time() - separate_time)) output_details = interpreter.get_output_details() output_data = interpreter.get_tensor(output_details[0]['index']) output_data = [] for output_detail in output_details: output_data.append(interpreter.get_tensor(output_detail['index'])) print(output_data)
奇怪的是,PReLU 的输出值包含多个 nan。但是,通过使用 -onimc 选项将 ONNX 模型转换为模型的中间可以看出,nan 直到 PReLU 之前才会出现。由此可见,TFLite运行时中的PReLU OP存在推理结果发散的问题。
- TFLite 推理结果
以下是避免此问题的解决方法。在转换模型时,使用 -rtpo 选项将 PReLU 替换为类似的原语操作,然后执行模型转换。
onnx2tf -i prelu_check.onnx -cotof -rtpo PReLU
和之前一样,ONNX 和 TensorFlow (Keras) 的推理结果完美匹配。
- 转换结果
但是,-rtpo PReLU 将生成一个 .tflite 文件,其中 PRelu OP 替换为原始 OP 组合。
- 特氟利特
再次运行测试代码来检查推理结果。下图显示,仅用基元操作的组合替换PReLU OP进行推理时,不会出现nan。换句话说,重要的是要知道较大的算术错误不仅是由于模型结构损坏造成的,还可能是由内部实现(例如 TFLite 运行时)引起的。我已经实现了 -rtpo 选项来替换运算符,作为避免此类运行时问题的解决方法。
- TFLite 推理结果
13、 InstanceNormalization 计算误差极大的问题
Click 至 expand
即使转换成功,InstanceNormalization 也往往会出现很大的错误。这是 ONNX 规范。
- See.1: https://discuss.pytorch.org/t/understanding-instance-normalization-2d-with-running-mean-and-running-var/144139
- See.2: https://github.com/pytorch/pytorch/issues/72057
我用一个非常简单的示例模型验证了这一点。元素数量超过800万个,计算误差达到1e-2。
14. TFLite 中动态张量的推理
Click 至 expand
一段时间以来,TFLite 运行时支持动态张量推理。然而,这一重要功能的存在并未得到广泛认可。在本章中,我将展示如何将包含批量大小的动态几何图形的 ONNX 文件直接转换为包含动态几何图形的 TFLite 文件,然后在可变批量条件下进一步推断它。激发我添加本教程的问题就在这里。 [动态批次/动态形状]具有动态输入的 onnx 模型转换为具有静态输入 1 #441 或 的 tflite 无法使用具有动态输入形状的转换模型 #521
首先,下载示例 ONNX 文件。
wget https://s3.ap-northeast-2.wasabisys.com/temp-models/onnx2tf_441/osnet_x0_25_msmt17.onnx
该模型通过余弦相似度来计算特征的相似度。输入张量的batch size维度为batch,允许同时输入不同数量的图像。例如,这通常用于通过计算连续视频帧之间反映的人或物体的相似度来实现跟踪。然而,由于图像中的人和物体的数量不断增加和减少,因此要跟踪的物体总数随着每个视频帧而快速变化。因此,生成具有模型输入图像数量(批量大小)可变设置的模型是一个非常重要的用例。
将下载的 OSNet 转换为 tflite 和 saved_model 作为变量批次。如果不指定 -b 或 -ois 选项,onnx2tf 不会将批量大小更改为 N。唯一重要的一点是使用 -osd 和 -coion 选项转换模型。
onnx2tf -i osnet_x0_25_msmt17.onnx -fdosm -osd -coion
-
.tfliteWhen viewing tflite in Netron, the batch size appears to be fixed at
1. -
saved_modelHowever, checking the structure of
saved_model, the batch size is correctly set to-1.saved_model_cli show --dir saved_model/ --all MetaGraphDef with tag-set: 'serve' contains the following SignatureDefs: signature_def['__saved_model_init_op']: The given SavedModel SignatureDef contains the following input(s): The given SavedModel SignatureDef contains the following output(s): outputs['__saved_model_init_op'] tensor_info: dtype: DT_INVALID shape: unknown_rank name: NoOp Method name is: signature_def['serving_default']: The given SavedModel SignatureDef contains the following input(s): inputs['images'] tensor_info: dtype: DT_FLOAT shape: (-1, 256, 128, 3) name: serving_default_images:0 The given SavedModel SignatureDef contains the following output(s): outputs['output'] tensor_info: dtype: DT_FLOAT shape: (-1, 512) name: PartitionedCall:0 Method name is: tensorflow/serving/predict
为了证明tflite结构已经正确转换,我将tflite转换为JSON并查看结构。
(下面的flatc命令仅用于tflite2json2tflite容器中的手动检查,不是onnx2tf转换的要求。)
docker run --rm -it
-v `pwd`:/home/user/workdir
ghcr.io/pinto0309/tflite2json2tflite:latest
./flatc -t
--strict-json
--defaults-json
-o workdir
./schema.fbs -- workdir/saved_model/osnet_x0_25_msmt17_float32.tflite
ls -l workdir
-rw-rw-r-- 1 user user 921564 Aug 4 10:24 osnet_x0_25_msmt17.onnx
-rw-r--r-- 1 user user 10369524 Aug 4 10:30 osnet_x0_25_msmt17_float32.json
drwxrwxr-x 4 user user 4096 Aug 4 10:26 saved_model
-
osnet_x0_25_msmt17_float32.json"shape_signature"is correctly set to-1. However,"shape"is set to1. This could be a problem with TFLiteConverter, or it could be a problem with Netron's graphical display capabilities.
也就是说,虽然onnx2tf按照指定转换了TFLiteConverer,但是批量大小为-1,没有进行任何模型处理,只是Netron的显示是坏的。这是我很长时间以来都知道的一个问题。然而,推论本身并不会造成问题。
如果要以可变批次进行推断,则需要使用 signature 进行推断。在这种情况下,转换模型时必须指定 -coion 选项。请注意,我发现了 -coion 选项的量化问题,该问题可能会损坏 tflite 文件。 https://github.com/PINTO0309/onnx2tf/issues/429
https://github.com/PINTO0309/onnx2tf#4-match-tflite-inputoutput-names-and-inputoutput-order-to-onnx
您可以通过使用 signature 执行推理来使用 signature_runner 来处理动态输入张量。下面我展示了 batch_size=5 和 batch_size=3 张量都可以用同一模型推断。
test.py- 批量大小:5import numpy as np from ai_edge_litert.interpreter import Interpreter from pprint import pprint interpreter = Interpreter(model_path="saved_model/osnet_x0_25_msmt17_float32.tflite") tf_lite_model = interpreter.get_signature_runner() inputs = { 'images': np.ones([5,256,128,3], dtype=np.float32), } tf_lite_output = tf_lite_model(**inputs) print(f"[TFLite] Model Predictions shape: {tf_lite_output['output'].shape}") print(f"[TFLite] Model Predictions:") pprint(tf_lite_output)- 结果
[TFLite] Model Predictions shape: (5, 512) [TFLite] Model Predictions: {'output': array([[0.0000000e+00, 2.4730086e-04, 0.0000000e+00, ..., 1.0528549e+00, 3.7874988e-01, 0.0000000e+00], [0.0000000e+00, 2.4730086e-04, 0.0000000e+00, ..., 1.0528549e+00, 3.7874988e-01, 0.0000000e+00], [0.0000000e+00, 2.4730086e-04, 0.0000000e+00, ..., 1.0528549e+00, 3.7874988e-01, 0.0000000e+00], [0.0000000e+00, 2.4730086e-04, 0.0000000e+00, ..., 1.0528549e+00, 3.7874988e-01, 0.0000000e+00], [0.0000000e+00, 2.4730084e-04, 0.0000000e+00, ..., 1.0528525e+00, 3.7874976e-01, 0.0000000e+00]], dtype=float32)} test.py- 批量大小:3import numpy as np from ai_edge_litert.interpreter import Interpreter from pprint import pprint interpreter = Interpreter(model_path="saved_model/osnet_x0_25_msmt17_float32.tflite") tf_lite_model = interpreter.get_signature_runner() inputs = { 'images': np.ones([3,256,128,3], dtype=np.float32), } tf_lite_output = tf_lite_model(**inputs) print(f"[TFLite] Model Predictions shape: {tf_lite_output['output'].shape}") print(f"[TFLite] Model Predictions:") pprint(tf_lite_output)- 结果
[TFLite] Model Predictions shape: (3, 512) [TFLite] Model Predictions: {'output': array([[0.0000000e+00, 2.4730084e-04, 0.0000000e+00, ..., 1.0528525e+00, 3.7874976e-01, 0.0000000e+00], [0.0000000e+00, 2.4730084e-04, 0.0000000e+00, ..., 1.0528525e+00, 3.7874976e-01, 0.0000000e+00], [0.0000000e+00, 2.4730084e-04, 0.0000000e+00, ..., 1.0528525e+00, 3.7874976e-01, 0.0000000e+00]], dtype=float32)}
15.通过 Einsum 和 OneHot 优化对整个模型进行重大优化
Click 至 expand
Einsum 和 OneHot 未通过 onnx-optimizer 的标准行为进行最大优化。因此,使用我原来的方法对Einsum OP和OneHot OP进行预优化可以显着提高模型转换的成功率,并且输入的ONNX模型本身比单独优化onnxsim时可以得到显着的优化。请参阅:https://github.com/PINTO0309/onnx2tf/issues/569
-
我对引用的模型结构进行了一些独特的定制。
https://github.com/PINTO0309/LightGlue-ONNX
-
spo4onnx
https://github.com/PINTO0309/spo4onnx
例如
python export.py
--img_size 512 512
--lightglue_path weights/sjy_fused_static.onnx
--end2end
pip install -U spo4onnx onnx2tf
cd weights
spo4onnx -if sjy_fused_static.onnx -of sjy_fused_static_spo.onnx
onnx2tf -i sjy_fused_static_spo.onnx
16.向模型添加未连接到模型主体的常量输出
Click 至 expand
有时您希望始终输出未连接到模型主体的常量。请参阅:https://github.com/PINTO0309/onnx2tf/issues/627。例如,如下图所示的 ONNX 的情况。您可能希望将缩放参数和其他参数保留为模型内的固定值,并始终在输出中包含相同的值。
在这种情况下,必须绕过而不执行onnxsim中优化ONNX文件的过程。您可以通过指定 -nuo 或 --not_use_onnxsim 作为转换选项来绕过 onnxsim 的执行。运行 onnxsim 会在优化模型结构的过程中从模型定义中删除未连接到模型主体的常量。
wget https://github.com/PINTO0309/onnx2tf/files/15292126/toy_with_constant.onnx.zip
unzip toy_with_constant.onnx.zip
onnx2tf -i toy_with_constant.onnx -nuo -cotof
转换前的ONNX与转换后的TFLite文件的关系如下图所示。
| ONNX | TFLite |
|---|---|
使用生成的 TFLite 文件进行推理并确保它始终包含固定值输出。
from ai_edge_litert.interpreter import Interpreter
import numpy as np
from pprint import pprint
interpreter = Interpreter(model_path="saved_model/toy_with_constant_float32.tflite")
interpreter.allocate_tensors()
input_details = interpreter.get_input_details()
output_details = interpreter.get_output_details()
interpreter.set_tensor(
tensor_index=input_details[0]['index'],
value=np.ones(tuple(input_details[0]['shape']), dtype=np.float32)
)
interpreter.invoke()
variable_output = interpreter.get_tensor(output_details[0]['index'])
constant_output = interpreter.get_tensor(output_details[1]['index'])
print("=================")
print("Variable Output:")
pprint(variable_output)
print("=================")
print("Constant Output:")
pprint(constant_output)
=================
Variable Output:
array([[-0.02787317, -0.05505124, 0.05421712, 0.03526559, -0.14131774,
0.0019211 , 0.08399964, 0.00433664, -0.00984338, -0.03370604]],
dtype=float32)
=================
Constant Output:
array([1., 2., 3., 4., 5.], dtype=float32)
17.使用可变长度令牌和嵌入的模型的转换,例如LLM和声音模型
Click 至 expand
这是指未定义维度的模型,可以是所有维度,也可以是包括批量大小在内的多个维度,如下图所示。
-
样品模型
https://github.com/PINTO0309/onnx2tf/releases/download/1.24.0/bge-m3.onnx
-
结构
如果在没有任何选项的情况下转换此类模型,TensorFlow/Keras 将中止。这是内部 TensorFlow/Keras 实现问题,而不是 onnx2tf 问题。由于规范原因,TensorFlow/Keras 不允许 Reshape 的 shape 属性中存在两个以上未定义的维度,因此在 Reshape OP 的内部转换操作过程中会出现错误,如下所示。这一直是TensorFlow/Keras的固有问题,至今仍未得到解决。请参阅:RuntimeError:tensorflow/lite/kernels/range.cc:39(开始>限制&&增量<0) || (开始< limit && delta > 0)不是true.Node,编号3(RANGE)调用失败。节点号 393 (WHILE) 调用失败。当前错误:RuntimeError:tensorflow/lite/kernels/reshape.cc:55stretch_dim!= -1(0!= -1)节点号83(RESHAPE)无法准备。 #40504
-
OP 问题发生的地方
-
错误信息
错误:'tf.Reshape' 操作要求 'shape' 至多有一个动态维度,但在索引 0 和 3 处有多个动态维度
因此,对于此类模型,其中所有尺寸(包括批量大小)都是动态形状,通常可以通过使用 `-b 1` 或 `--batch_size 1` 选项将批量大小固定为 `1` 来进行转换。
onnx2tf -i model.onnx -b 1 -fdosm -osd
- 结果
转换后的tflite在Netron中显示时,动态形状的所有尺寸都显示为`1`,但这是Netron中的显示问题,形状实际上转换为`-1`或`None`。
单击此处查看如何使用动态形状张量进行推理。
https://github.com/PINTO0309/onnx2tf/tree/main?tab=readme-ov-file#14-inference-with-dynamic-tensors-in-tflite
### 18、仅转换ONNX模型的中间结构部分
**Click 至 expand**
通过指定 ONNX 输入或输出名称,只能转换模型的中间部分。当您想要查看转换后模型的哪个部分获得了哪些输出,或者调试模型转换操作本身时,这非常有用。
例如,采用如下图所示的具有多个输入和多个输出的模型来尝试部分转换。
- 通过仅指定输入名称来开始转换来进行转换
wget https://github.com/PINTO0309/onnx2tf/releases/download/1.25.0/cf_fus.onnx onnx2tf -i cf_fus.onnx -inimc 448 -coion
- 通过仅指定输出名称来结束转换
wget https://github.com/PINTO0309/onnx2tf/releases/download/1.25.0/cf_fus.onnx onnx2tf -i cf_fus.onnx -onimc dep_sec -coion
- 通过指定开始转换的输入名称和结束转换的输出名称来执行转换
wget https://github.com/PINTO0309/onnx2tf/releases/download/1.25.0/cf_fus.onnx onnx2tf -i cf_fus.onnx -inimc 448 -onimc velocity -coion
### 19. 转换为TensorFlow.js
**Click 至 expand**
转换为TensorFlow.js时,处理如下。
bash
pip install -U --no-deps
tensorflowjs
tensorflow_decision_forests
ydf
tensorflow_hub
onnx2tf -i mobilenetv2-12.onnx -ois input:1,3,224,224 -fdosm -osd -dgc
tensorflowjs_converter
--input_format tf_saved_model
--output_format tfjs_graph_model
saved_model
tfjs_model
请参阅:https://github.com/tensorflow/tfjs/tree/master/tfjs-converter
20. 转换为CoreML
Click 至 expand
转换为CoreML时,处理如下。 -k 选项用于转换,同时保持 ONNX 的 NCHW 格式的输入通道顺序。
pip install coremltools==8.2
onnx2tf -i mobilenetv2-12.onnx -k input -ois input:1,3,224,224 -fdosm -osd
import coremltools as ct
FOLDER_PATH = 'saved_model'
model = ct.convert(
model=FOLDER_PATH,
source='tensorflow',
)
model.save(f'{FOLDER_PATH}/model.mlpackage')
请参阅:https://github.com/apple/coremltools
CLI参数
Click 至 expand
onnx2tf -h
usage: onnx2tf
[-h]
(-i INPUT_ONNX_FILE_PATH | -it INPUT_TFLITE_FILE_PATH | -V)
[-o OUTPUT_FOLDER_PATH]
[-osd]
[-oh5]
[-okv3]
[-otfv1pb]
[-ow]
[-coion]
[-odrqt]
[-oiqt]
[-qt {per-channel,per-tensor}]
[-cind INPUT_NAME NUMPY_FILE_PATH MEAN STD]
[-iqd {int8,uint8,float32}]
[-oqd {int8,uint8,float32}]
[-npgts NATIVE_PYTORCH_GENERATION_TIMEOUT_SEC]
[-nuo]
[-nuonag]
[-b BATCH_SIZE]
[-ois OVERWRITE_INPUT_SHAPE [OVERWRITE_INPUT_SHAPE ...]]
[-sh SHAPE_HINTS [SHAPE_HINTS ...]]
[-nlt]
[-onwdt]
[-snms {v4,v5}]
[-k KEEP_NCW_OR_NCHW_OR_NCDHW_INPUT_NAMES [KEEP_NCW_OR_NCHW_OR_NCDHW_INPUT_NAMES ...]]
[-kt KEEP_NWC_OR_NHWC_OR_NDHWC_INPUT_NAMES [KEEP_NWC_OR_NHWC_OR_NDHWC_INPUT_NAMES ...]]
[-kat KEEP_SHAPE_ABSOLUTELY_INPUT_NAMES [KEEP_SHAPE_ABSOLUTELY_INPUT_NAMES ...]]
[-inimc INPUT_NAMES [INPUT_NAMES ...]]
[-onimc OUTPUT_NAMES [OUTPUT_NAMES ...]]
[-dgc]
[-eatfp16]
[-ebu]
[-eru]
[-dsft]
[-nodaftc]
[-dsfs]
[-dsm]
[-nodafsc]
[-ofgd]
[-rari64 | -rarf32 | -rafi64 | -raff32]
[-fasr FUSED_ARGMAX_SCALE_RATIO]
[-rtpo REPLACE_TO_PSEUDO_OPERATORS [REPLACE_TO_PSEUDO_OPERATORS ...]]
[-me MVN_EPSILON]
[-prf PARAM_REPLACEMENT_FILE]
[-cgdc]
[-coto | -cotof]
[-coton]
[-cotor CHECK_ONNX_TF_OUTPUTS_ELEMENTWISE_CLOSE_RTOL]
[-cotoa CHECK_ONNX_TF_OUTPUTS_ELEMENTWISE_CLOSE_ATOL]
[-tdnp TEST_DATA_NHWC_PATH]
[-agj]
[-dms]
[-uc]
[-n]
[-v]
optional arguments:
-h, --help
show this help message and exit
-i INPUT_ONNX_FILE_PATH,--input_onnx_文件路径 INPUT_ONNX_FILE_PATH
Input onnx file path.
-it INPUT_TFLITE_FILE_PATH, --input_tflite_file_path INPUT_TFLITE_FILE_PATH
Input tflite file path for direct import mode.
-V, --version
Show version and exit.
-o OUTPUT_FOLDER_PATH, --output_folder_path OUTPUT_FOLDER_PATH
Output folder path. Default: "saved_model"
-osd, --output_signaturedefs
Signature is added to the output for serving or for conversion
to other model formats. However, this can significantly reduce the speed
of model conversion and significant increase the size of the model.
-oh5, --output_h5
Output model in Keras (hdf5) format.
-okv3, --output_keras_v3
Output model in Keras (keras_v3) format.
-otfv1pb, --output_tfv1_pb
Output model in TF v1 (.pb) format.
-ow, --output_weights
Output weights in hdf5 format.
-coion, --copy_onnx_input_output_names_to_tflite
Copy the input/output OP name of ONNX to the input/output OP name of tflite.
Due to Tensorflow internal operating specifications,
the input/output order of ONNX does not necessarily match
the input/output order of tflite.
Be sure to check that the input/output OP names in the generated
tflite file have been converted as expected.
Also, this option generates a huge JSON file as a temporary file for processing.
Therefore, it is strongly discouraged to use it on large models of hundreds
of megabytes or more.
-odrqt, --output_dynamic_range_quantized_tflite
Output of dynamic range quantized tflite.
-oiqt, --output_integer_quantized_tflite
Output of integer quantized tflite.
-tb {tf_converter,flatbuffer_direct},
--tflite_backend {tf_converter,flatbuffer_direct}
TFLite generation backend.
"flatbuffer_direct"(default): Use direct FlatBuffer builder path (limited
OP/quantization support).
"tf_converter": Use TensorFlow Lite Converter as an explicit compatibility
path.
-fdosm, --flatbuffer_direct_output_saved_model
Output SavedModel directly from flatbuffer_direct ModelIR (float32).
Available only with --tflite_backend flatbuffer_direct.
Cannot be used with --disable_model_save.
Fails explicitly if CUSTOM ops are present.
With split output, partition SavedModels are emitted instead of a single root SavedModel.
-fdopt, --flatbuffer_direct_output_pytorch
Output a reloadable PyTorch package directly from flatbuffer_direct ModelIR.
Public spatial inputs/outputs use NCW/NCHW/NCDHW.
Unsupported/CUSTOM ops and residual channel-last layout bridges fail explicitly.
When used with -cotof, also outputs `<model_name>_pytorch_accuracy_report.json`
and `<model_name>_accuracy_comparison_report.json` in the output directory.
With `-it/--input_tflite_file_path`, these reports compare `TFLite↔PyTorch`
using the same seeded inputs.
-fdots, --flatbuffer_direct_output_torchscript
Save a traced TorchScript file (`<model_name>_jit.pt`) into the generated
flatbuffer_direct PyTorch package.
Requires `--tflite_backend flatbuffer_direct`.
Internally enables `--flatbuffer_direct_output_pytorch` automatically.
Only native PyTorch packages are supported. If the generated package falls
back to a non-native backend, conversion fails explicitly.
For dynamic public inputs, a concrete trace shape/input is required.
Recommended: `--shape_hints`
Also accepted: `--test_data_nhwc_path` for eligible 4D RGB inputs, or
`-cind` for per-input custom trace data.
-fdodo, --flatbuffer_direct_output_dynamo_onnx
Save a Dynamo ONNX file (`<model_name>_dynamo.onnx`) into the generated
flatbuffer_direct PyTorch package using `torch.onnx.export(..., dynamo=True)`.
Requires `--tflite_backend flatbuffer_direct`.
Internally enables `--flatbuffer_direct_output_pytorch` automatically.
Only native PyTorch packages are supported. If the generated package falls
back to a non-native backend, conversion fails explicitly.
For dynamic public inputs, a concrete example input is required.
Recommended: `--shape_hints`
Also accepted: `--test_data_nhwc_path` for eligible 4D RGB inputs, or
`-cind` for per-input custom example data.
-fdoep, --flatbuffer_direct_output_exported_program
Save a PyTorch ExportedProgram file (`<model_name>_ep.pt2`) into the
generated flatbuffer_direct PyTorch package using `torch.export.save`.
Requires `--tflite_backend flatbuffer_direct`.
Internally enables `--flatbuffer_direct_output_pytorch` automatically.
Only native PyTorch packages are supported. If the generated package falls
back to a non-native backend, conversion fails explicitly.
For dynamic public inputs, a concrete example input is required.
Recommended: `--shape_hints`
Also accepted: `--test_data_nhwc_path` for eligible 4D RGB inputs, or
`-cind` for per-input custom example data.
-qt {per-channel,per-tensor}, --quant_type {per-channel,per-tensor}
Selects whether "per-channel" or "per-tensor" quantization is used.
Default: "per-channel"
-qnm QUANT_NORM_MEAN, --quant_norm_mean QUANT_NORM_MEAN
Normalized average value during quantization.
Only valid when the "-cind" option is not used.
Default: "[[[[0.485, 0.456, 0.406]]]]"
-qns QUANT_NORM_STD, --quant_norm_std QUANT_NORM_STD
Normalized standard deviation during quantization.
Only valid when the "-cind" option is not used.
Default: "[[[[0.229, 0.224, 0.225]]]]"
-cind INPUT_NAME NUMPY_FILE_PATH MEAN STD,
--custom_input_op_name_np_data_path INPUT_NAME NUMPY_FILE_PATH MEAN STD
Input name of OP and path of data file (Numpy) for custom input for -cotof, -fdots, -fdodo, -fdoep, or -oiqt,
and mean (optional) and std (optional).
Unlike -tdnp, this option supports per-input mapping, non-image tensors, and INT8 calibration.
<Usage in -cotof>
When using -cotof, custom input defined by the user, instead of dummy data, is used.
In this case, mean and std are omitted from the input.
-cind {input_op_name} {numpy_file_path}
e.g. -cind onnx::Equal_0 test_cind/x_1.npy -cind onnx::Add_1 test_cind/x_2.npy -cotof
The input_op_name must be the same as in ONNX,
and it may not work if the input format is different between ONNX and TF.
<Usage in -fdots / -fdodo / -fdoep>
When using -fdots, -fdodo, or -fdoep, -cind can be used to provide a concrete example input for a dynamic public input.
For shape-only hints, prefer --shape_hints. For 4D RGB inputs, --test_data_nhwc_path is also supported.
In these modes, mean and std are omitted from the input.
-cind {input_op_name} {numpy_file_path} -fdots
<Usage in -oiqt>
INPUT Name of OP and path of calibration data file (Numpy) for quantization
and mean and std.
This option is required when using -oiqt.
The type of the input OP must be Float32.
Data for calibration must be pre-normalized to a range of 0 to 1.
-cind {input_op_name} {numpy_file_path} {mean} {std}
Numpy file paths must be specified the same number of times as the number of input OPs.
Normalize the value of the input OP based on the tensor specified in mean and std.
(input_value - mean) / std
Tensors in Numpy file format must be in dimension order after conversion to TF.
Note that this is intended for deployment on low-resource devices,
so the batch size is limited to 1 only.
e.g.
The example below shows a case where there are three input OPs.
Assume input0 is 128x128 RGB image data.
In addition, input0 should be a value that has been divided by 255
in the preprocessing and normalized to a range between 0 and 1.
input1 and input2 assume the input of something that is not an image.
Because input1 and input2 assume something that is not an image,
the divisor is not 255 when normalizing from 0 to 1.
"n" is the number of calibration data.
ONNX INPUT shapes:
input0: [n,3,128,128]
mean: [1,3,1,1] -> [[[[0.485]],[[0.456]],[[0.406]]]]
std: [1,3,1,1] -> [[[[0.229]],[[0.224]],[[0.225]]]]
input1: [n,64,64]
mean: [1,64] -> [0.1, ..., 0.64]
std: [1,64] -> [0.05, ..., 0.08]
input2: [n,5]
mean: [1] -> [0.3]
std: [1] -> [0.07]
TensorFlow INPUT shapes (Numpy file ndarray shapes):
input0: [n,128,128,3]
mean: [1,1,1,3] -> [[[[0.485, 0.456, 0.406]]]]
std: [1,1,1,3] -> [[[[0.229, 0.224, 0.225]]]]
input1: [n,64,64]
mean: [1,64] -> [0.1, ..., 0.64]
std: [1,64] -> [0.05, ..., 0.08]
input2: [n,5]
mean: [1] -> [0.3]
std: [1] -> [0.07]
-cind "input0" "../input0.npy" "[[[[0.485,0.456,0.406]]]]" "[[[[0.229,0.224,0.225]]]]"
-cind "input1" "./input1.npy" "[0.1,...,0.64]" "[0.05,...,0.08]"
-cind "input2" "input2.npy" "[0.3]" "[0.07]"
<Using -cotof and -oiqt at the same time>
To use -cotof and -oiqt simultaneously,
you need to enter the Input name of OP, path of data file, mean, and std all together.
And the data file must be in Float32 format,
and {input_op_name}, {numpy_file_path}, {mean}, and {std} must all be entered.
Otherwise, an error will occur during the -oiqt stage.
-iqd {int8,uint8,float32}, --input_quant_dtype {int8,uint8,float32}
Input dtypes when doing Full INT8 Quantization.
"int8"(default) or "uint8" or "float32"
-oqd {int8,uint8,float32}, --output_quant_dtype {int8,uint8,float32}
Output dtypes when doing Full INT8 Quantization.
"int8"(default) or "uint8" or "float32"
-npgts NATIVE_PYTORCH_GENERATION_TIMEOUT_SEC, --native_pytorch_generation_timeout_sec NATIVE_PYTORCH_GENERATION_TIMEOUT_SEC
Timeout in seconds for generated native PyTorch package creation.
When exceeded, onnx2tf treats the generation as a recursion explosion,
aborts native PyTorch generation for the current model, and continues
conversion without PyTorch artifacts for that model.
`0` disables this timeout.
-nuo, --not_use_onnxsim
No optimization by onnx-simplifier is performed.
If this option is used, the probability of a conversion error is very high.
Effective for both `tf_converter` and `flatbuffer_direct` when converting
ONNX input. With `-it/--input_tflite_file_path` and
`--tflite_backend flatbuffer_direct`, this remains unsupported because
there is no ONNX preprocess stage.
-nuonag, --not_use_opname_auto_generate
Automatic generation of each OP name in the old format ONNX file
and assignment of OP name are not performed.
-b BATCH_SIZE, --batch_size BATCH_SIZE
Fixes the dynamic batch size to the specified numeric batch size.
A value of 1 or more must be specified.
-ois OVERWRITE_INPUT_SHAPE [OVERWRITE_INPUT_SHAPE ...],
--overwrite_input_shape OVERWRITE_INPUT_SHAPE [OVERWRITE_INPUT_SHAPE ...]
Overwrite the input shape.
The format is
"i1:dim0,...,dimN" "i2:dim0,...,dimN" "i3:dim0,...,dimN"
When there is only one input, for example,
"data:1,3,224,224"
When there are multiple inputs, for example,
"data1:1,3,224,224" "data2:1,3,112" "data3:5"
A value of 1 or more must be specified.
Numerical values other than dynamic dimensions are ignored.
Ignores --batch_size if specified at the same time as --batch_size.
-sh SHAPE_HINTS [SHAPE_HINTS ...],
--shape_hints SHAPE_HINTS [SHAPE_HINTS ...]
Shape hints for input tensors containing dynamic dimensions.
Specify input shapes for test inference with -cotof or -coto.
Unlike `--overwrite_input_shape`, this operation does not overwrite
the ONNX input shape with a static shape.
The format is
"i1:dim0,...,dimN" "i2:dim0,...,dimN" "i3:dim0,...,dimN"
When there is only one input, for example,
"data:1,3,224,224"
When there are multiple inputs, for example,
"data1:1,3,224,224" "data2:1,3,112" "data3:5"
A value of 1 or more must be specified.
Numerical values other than dynamic dimensions are ignored.
Also used as the recommended example-input hint source for -fdots,
-fdodo, and -fdoep.
-vh VALUE_HINTS [VALUE_HINTS ...],
--value_hints VALUE_HINTS [VALUE_HINTS ...]
Value hints for dummy inference input tensors.
The format is
"input_name_1:value" "input_name_2:value" "*:default_value"
"*" applies to all inputs not explicitly specified.
Values are scalar only.
-nlt, --no_large_tensor
Suppresses constant bloat caused by Tile OP when optimizing models in onnxsim.
See: https://github.com/daquexian/onnx-simplifier/issues/178
-onwdt, --output_nms_with_dynamic_tensor
The number of bounding boxes in the NMS output results is
not fixed at the maximum number of max_output_boxes_per_class,
but rather at the smallest possible number of dynamic tensors.
If this option is disabled, NMS output is padded to the number
set in the max_output_boxes_per_class attribute.
e.g.
disable --output_nms_with_dynamic_tensor:
output_tensor_shape: [100, 7]
enable --output_nms_with_dynamic_tensor:
output_tensor_shape: [N, 7]
-onwa, --output_nms_with_argmax
Apply argmax to class scores dimension in NonMaxSuppression and shrink
scores tensor from [B, C, N] to [B, 1, N].
-snms {v4,v5}, --switch_nms_version {v4,v5}
Switch the NMS version to V4 or V5 to convert.
e.g.
NonMaxSuppressionV4(default): --switch_nms_version v4
NonMaxSuppressionV5: --switch_nms_version v5
-k KEEP_NCW_OR_NCHW_OR_NCDHW_INPUT_NAMES [KEEP_NCW_OR_NCHW_OR_NCDHW_INPUT_NAMES ...],
--keep_ncw_or_nchw_or_ncdhw_input_names KEEP_NCW_OR_NCHW_OR_NCDHW_INPUT_NAMES
[KEEP_NCW_OR_NCHW_OR_NCDHW_INPUT_NAMES ...]
Holds the NCW or NCHW or NCDHW of the input shape for the specified INPUT OP names.
If a nonexistent INPUT OP name is specified, it is ignored.
Valid only for 3D, 4D and 5D input tensors.
e.g. --keep_ncw_or_nchw_or_ncdhw_input_names "input0" "input1" "input2"
-kt KEEP_NWC_OR_NHWC_OR_NDHWC_INPUT_NAMES [KEEP_NWC_OR_NHWC_OR_NDHWC_INPUT_NAMES ...],
--keep_nwc_or_nhwc_or_ndhwc_input_names KEEP_NWC_OR_NHWC_OR_NDHWC_INPUT_NAMES
[KEEP_NWC_OR_NHWC_OR_NDHWC_INPUT_NAMES ...]
Holds the NWC or NHWC or NDHWC of the input shape for the specified INPUT OP names.
If a nonexistent INPUT OP name is specified, it is ignored.
If the input OP name is the same as the input OP name specified
in the keep_ncw_or_nchw_or_ncdhw_input_names option, it is ignored.
Valid only for 3D, 4D and 5D input tensors.
e.g. --keep_nwc_or_nhwc_or_ndhwc_input_names "input0" "input1" "input2"
-kat KEEP_SHAPE_ABSOLUTELY_INPUT_NAMES [KEEP_SHAPE_ABSOLUTELY_INPUT_NAMES ...],
--keep_shape_absolutely_input_names KEEP_SHAPE_ABSOLUTELY_INPUT_NAMES
[KEEP_SHAPE_ABSOLUTELY_INPUT_NAMES ...]
Name of the INPUT that unconditionally maintains its shape.
If a nonexistent INPUT OP name is specified, it is ignored.
e.g. --keep_shape_absolutely_input_names "input0" "input1" "input2"
-inimc INPUT_NAMES [INPUT_NAMES ...],
--input_names_to_interrupt_model_conversion INPUT_NAMES [INPUT_NAMES ...]
Input names of ONNX that interrupt model conversion.
Interrupts model transformation at the specified input name and inputs the
model partitioned into subgraphs.
With `--tflite_backend flatbuffer_direct`, this crops ModelIR and treats
the specified tensors as runtime inputs.
e.g. --input_names_to_interrupt_model_conversion "input0" "input1" "input2"
-onimc OUTPUT_NAMES [OUTPUT_NAMES ...],
--output_names_to_interrupt_model_conversion OUTPUT_NAMES [OUTPUT_NAMES ...]
Output names of ONNX that interrupt model conversion.
Interrupts model transformation at the specified output name and outputs the
model partitioned into subgraphs.
With `--tflite_backend flatbuffer_direct`, this crops ModelIR and treats
the specified tensors as runtime outputs.
e.g. --output_names_to_interrupt_model_conversion "output0" "output1" "output2"
-easm, --enable_auto_split_model
Force auto split regardless of the ONNX file size.
Uses --auto_split_max_size as the target partition size.
In `flatbuffer_direct`, this forces the shared ModelIR split planner to run and
emit split manifest outputs even if the size estimate would otherwise fit in one partition.
-asms AUTO_SPLIT_MAX_SIZE, --auto_split_max_size AUTO_SPLIT_MAX_SIZE
Target maximum size per partition when auto-split is triggered or forced.
Supported units: KB, MB, GB (e.g. 900MB, 1GB, 1536KB).
Bare numbers are treated as MB.
When specified, this value is also used as the target size for --enable_auto_split_model.
Default: 1GB
-esm {unsplit_tflite,onnx}, --eval_split_models {unsplit_tflite,onnx}
Evaluate split partitions sequentially using split manifest output.
Specify `unsplit_tflite` to compare against the unsplit/base TFLite model,
or `onnx` to compare against ONNX Runtime output.
Available only with `--tflite_backend flatbuffer_direct`
and requires `--enable_auto_split_model`.
Writes `*_split_accuracy_report.json`.
`*_accuracy_report.json` remains the unsplit base float32 TFLite vs ONNX report.
-dgc, --disable_group_convolution
Disable GroupConvolution and replace it with SeparableConvolution for
conversion outputs.
This option is applied in both tf_converter and flatbuffer_direct paths.
In `flatbuffer_direct`, ONNX input keeps using direct grouped-conv lowering
control, and `-it/--input_tflite_file_path` rewrites imported grouped
`CONV_2D` to `SPLIT` + per-group `CONV_2D` + `CONCATENATION`.
-eatfp16, --enable_accumulation_type_float16 ENABLE_ACCUMULATION_TYPE_FLOAT16
Hint for XNNPACK fp16 inference on float16 tflite model.
XNNPACK float16 inference on certain ARM64 cores is 2x faster.
Float16 inference doubling on devices with ARM64 ARMv8.2 or higher instruction set.
This option is applied in both tf_converter and flatbuffer_direct paths.
See: https://github.com/PINTO0309/onnx2tf/pull/553
-ebu, --enable_batchmatmul_unfold
BatchMatMul is separated batch by batch to generate a primitive MatMul.
In `flatbuffer_direct`, this rewrites ModelIR `BATCH_MUL` ops with
static batch prefixes into per-batch slices plus rank-lowered matmul ops.
This is available for both ONNX input and `-it/--input_tflite_file_path`.
-eru, --enable_rnn_unroll
Instead of increasing inference speed by expanding all symbolic loops of
the RNN (LSTM, GRU, RNN), RAM consumption will increase because all tensors
are expanded and embedded in the model.
In `flatbuffer_direct`, this rewrites supported sequence RNN/LSTM ModelIR
ops into step-unrolled primitive ops for both ONNX input and
`-it/--input_tflite_file_path`. `GRU` already uses step-style lowering in
the direct path.
https://keras.io/api/layers/recurrent_layers/
-dsft, --disable_suppression_flextranspose
Disables FlexTranspose generation suppression.
With `--tflite_backend flatbuffer_direct` on ONNX input, this emits a
single builtin `TRANSPOSE` without rank-compression. With
`-it/--input_tflite_file_path`, this option remains unsupported.
-nodaftc, --number_of_dimensions_after_flextranspose_compression
Number of Transpose OP dimensions generated after avoiding FlexTranspose generation.
Also suppress the creation of the Transpose itself by specifying 2.
Default: 6
-dsfs, --disable_suppression_flexstridedslice
Disables FlexStridedSlice generation suppression.
With `--tflite_backend flatbuffer_direct` on ONNX input, this emits a
single builtin `SLICE`/`STRIDED_SLICE` without rank-compression. With
`-it/--input_tflite_file_path`, this option remains unsupported.
-dsm, --disable_strict_mode
If specified, the conversion speed is greatly accelerated because the strict accuracy
correction process is skipped, but the frequency of transposition errors increases
and accuracy errors are more likely to occur. Strict mode is enabled by default.
As of 2023.05.07, this is a work in progress and is an experimental feature.
Therefore, only some OPs are converted in strict mode for accuracy correction.
-nodafsc, --number_of_dimensions_after_flexstridedslice_compression
Number of StridedSlice OP dimensions generated after avoiding FlexStridedSlice generation.
Default: 5
-ofgd, --optimization_for_gpu_delegate
Replace operations that do not support gpu delegate with those
that do as much as possible.
Effective in both `tf_converter` and `flatbuffer_direct` for ONNX input.
In `flatbuffer_direct`, this now applies direct lowering rewrites for
broadcast arithmetic, Gather negative-index normalization, and Gemm bias
handling. With `-it/--input_tflite_file_path`, this option remains
unsupported.
-rari64, --replace_argmax_to_reducemax_and_indices_is_int64
Replace ArgMax with a ReduceMax. The returned indices are int64.
Effective in both `tf_converter` and `flatbuffer_direct` for ONNX input.
With `-it/--input_tflite_file_path`, this option remains unsupported.
Only one of replace_argmax_to_reducemax_and_indices_is_int64
and replace_argmax_to_reducemax_and_indices_is_float32
and replace_argmax_to_fused_argmax_and_indices_is_int64
and replace_argmax_to_fused_argmax_and_indices_is_float32 can be specified.
-rarf32, --replace_argmax_to_reducemax_and_indices_is_float32
Replace ArgMax with a ReduceMax. The returned indices are float32.
Effective in both `tf_converter` and `flatbuffer_direct` for ONNX input.
With `-it/--input_tflite_file_path`, this option remains unsupported.
Only one of replace_argmax_to_reducemax_and_indices_is_int64
and replace_argmax_to_reducemax_and_indices_is_float32
and replace_argmax_to_fused_argmax_and_indices_is_int64
and replace_argmax_to_fused_argmax_and_indices_is_float32 can be specified.
-rafi64, --replace_argmax_to_fused_argmax_and_indices_is_int64
Replace ArgMax with a Fused_ArgMax. The returned indices are int64.
It improves inference speed at the cost of a small sacrifice in accuracy.
See. https://github.com/tensorflow/models/tree/master/official/projects/edgetpu/vision#argmax-fusion-to-improve-segmentation-model-latency
Currently, only 4D tensors are supported.
Effective in both `tf_converter` and `flatbuffer_direct` for ONNX input.
In `flatbuffer_direct`, this currently targets `Resize -> ArgMax` 4D
patterns. With `-it/--input_tflite_file_path`, this option remains
unsupported.
Only one of replace_argmax_to_reducemax_and_indices_is_int64
and replace_argmax_to_reducemax_and_indices_is_float32
and replace_argmax_to_fused_argmax_and_indices_is_int64
and replace_argmax_to_fused_argmax_and_indices_is_float32 can be specified.
-raff32, --replace_argmax_to_fused_argmax_and_indices_is_float32
Replace ArgMax with a Fused_ArgMax. The returned indices are float32.
It improves inference speed at the cost of a small sacrifice in accuracy.
See. https://github.com/tensorflow/models/tree/master/official/projects/edgetpu/vision#argmax-fusion-to-improve-segmentation-model-latency
Currently, only 4D tensors are supported.
Effective in both `tf_converter` and `flatbuffer_direct` for ONNX input.
In `flatbuffer_direct`, this currently targets `Resize -> ArgMax` 4D
patterns. With `-it/--input_tflite_file_path`, this option remains
unsupported.
Only one of replace_argmax_to_reducemax_and_indices_is_int64
and replace_argmax_to_reducemax_and_indices_is_float32
and replace_argmax_to_fused_argmax_and_indices_is_int64
and replace_argmax_to_fused_argmax_and_indices_is_float32 can be specified.
-fasr FUSED_ARGMAX_SCALE_RATIO, --fused_argmax_scale_ratio FUSED_ARGMAX_SCALE_RATIO
For Fused ArgMax.
Scale ratio when generating Fused ArgMax.
0.0 < fused_argmax_scale_ratio <= 1.0
Default: 0.5
-rtpo, --replace_to_pseudo_operators
Replace list of operators to pseudo operators.
Full name of the target operators should be given.
Currently supported operators :
Asin, Acos, Atan, Abs, PReLU, LeakyReLU, Power, GatherND, Neg, HardSwish, Erf, GeLU, MatMulInteger, Inverse
Note: Inverse is pseudo-lowered by default. Specifying Inverse keeps MatrixInverse/FlexMatrixInverse.
-me, --mvn_epsilon
For MeanVarianceNormalization.
The number to be added to the variance to avoid division by zero
when normalizing the value.
(input_tensor - mean) / tf.sqrt(variance + mvn_epsilon)
Effective in both `tf_converter` and `flatbuffer_direct` for ONNX input.
Default: 0.0000000001
-prf PARAM_REPLACEMENT_FILE, --param_replacement_file PARAM_REPLACEMENT_FILE
Parameter replacement file path. (.json)
-cgdc, --check_gpu_delegate_compatibility
Run TFLite ModelAnalyzer on the generated Float16 tflite model
to check if the model can be supported by GPU Delegate.
e.g.
"""
=== TFLite ModelAnalyzer ===
Your TFLite model has '1' subgraph(s). In the subgraph description below,
T# represents the Tensor numbers. For example, in Subgraph#0, the RESHAPE op takes
tensor #0 and tensor #6 as input and produces tensor #7 as output.
Subgraph#0 main(T#0) -> [T#17]
Op#0 RESHAPE(T#0, T#6[2, 8, 8, 3, 2, ...]) -> [T#7]
Op#1 SPLIT(T#5[0], T#7) -> [T#8, T#9]
Op#2 RESHAPE(T#8, T#1[8, 8, 3, 2, 2]) -> [T#10]
Op#3 TRANSPOSE(T#10, T#4[0, 3, 1, 4, 2]) -> [T#11]
Op#4 RESHAPE(T#11, T#2[1, 8, 2, 8, 2, ...]) -> [T#12]
Op#5 RESHAPE(T#9, T#1[8, 8, 3, 2, 2]) -> [T#13]
Op#6 TRANSPOSE(T#13, T#4[0, 3, 1, 4, 2]) -> [T#14]
Op#7 RESHAPE(T#14, T#2[1, 8, 2, 8, 2, ...]) -> [T#15]
Op#8 CONCATENATION(T#12, T#15) -> [T#16]
Op#9 RESHAPE(T#16, T#3[2, 16, 16, 3]) -> [T#17]
Tensors of Subgraph#0
T#0(inputs_0) shape:[2, 8, 8, 12], type:FLOAT32
T#1(model/tf.compat.v1.squeeze_2/Squeeze) shape:[5], type:INT32 RO 20 bytes, data:[8, 8, 3, 2, 2]
T#2(model/tf.expand_dims_1/ExpandDims) shape:[6], type:INT32 RO 24 bytes, data:[1, 8, 2, 8, 2, ...]
T#3(model/tf.reshape_1/Reshape/shape) shape:[4], type:INT32 RO 16 bytes, data:[2, 16, 16, 3]
T#4(model/tf.compat.v1.transpose/transpose/perm) shape:[5], type:INT32 RO 20 bytes, data:[0, 3, 1, 4, 2]
T#5(model/tf.concat/concat/axis) shape:[], type:INT32 RO 4 bytes, data:[0]
T#6(model/tf.reshape/Reshape/shape) shape:[6], type:INT32 RO 24 bytes, data:[2, 8, 8, 3, 2, ...]
T#7(model/tf.reshape/Reshape) shape:[2, 8, 8, 3, 2, 2], type:FLOAT32
T#8(model/tf.split/split) shape:[1, 8, 8, 3, 2, 2], type:FLOAT32
T#9(model/tf.split/split1) shape:[1, 8, 8, 3, 2, 2], type:FLOAT32
T#10(model/tf.compat.v1.squeeze_1/Squeeze) shape:[8, 8, 3, 2, 2], type:FLOAT32
T#11(model/tf.compat.v1.transpose/transpose) shape:[8, 2, 8, 2, 3], type:FLOAT32
T#12(model/tf.expand_dims/ExpandDims) shape:[1, 8, 2, 8, 2, 3], type:FLOAT32
T#13(model/tf.compat.v1.squeeze_2/Squeeze1) shape:[8, 8, 3, 2, 2], type:FLOAT32
T#14(model/tf.compat.v1.transpose_1/transpose) shape:[8, 2, 8, 2, 3], type:FLOAT32
T#15(model/tf.expand_dims_1/ExpandDims1) shape:[1, 8, 2, 8, 2, 3], type:FLOAT32
T#16(model/tf.concat/concat) shape:[2, 8, 2, 8, 2, 3], type:FLOAT32
T#17(Identity) shape:[2, 16, 16, 3], type:FLOAT32
Your model looks compatibile with GPU delegate with TFLite runtime version 2.10.0.
But it doesn't guarantee that your model works well with GPU delegate.
There could be some runtime incompatibililty happen.
---------------------------------------------------------------
Model size: 2988 bytes
Non-data buffer size: 2757 bytes (92.27 %)
Total data buffer size: 231 bytes (07.73 %)
(Zero value buffers): 4 bytes (00.13 %)
* Buffers of TFLite model are mostly used for constant tensors.
And zero value buffers are buffers filled with zeros.
Non-data buffers area are used to store operators, subgraphs and etc.
You can find more details from https://github.com/google-ai-edge/LiteRT/blob/v2.1.2/tflite/converter/schema/schema.fbs
"""
-coto, --check_onnx_tf_outputs_elementwise_close
Returns "Matches" if the output of onnx and the output of TF are
within acceptable proximity element by element.
Returns "Unmatched" if the output of onnx and the output of TF are
not within acceptable proximity element by element.
If the output of onnx is 1D, it returns "Skipped" and skips the comparison
between the output of onnx and that of TF. This is because when undefined
dimensions are present, a situation often arises where very large index
values are compared, causing OutOfMemory.
Only the output content of the models final output OP is checked.
-cotof, --check_onnx_tf_outputs_elementwise_close_full
Returns "Matches" if the output of onnx and the output of TF are
within acceptable proximity element by element.
Check the output of all OPs in sequence from the beginning,
including all but the final output OP of the model.
Returns "Unmatched" if the output of onnx and the output of TF are
not within acceptable proximity element by element.
If the output of onnx is 1D, it returns "Skipped" and skips the comparison
between the output of onnx and that of TF. This is because when undefined
dimensions are present, a situation often arises where very large index
values are compared, causing OutOfMemory.
It is very time consuming because it performs as many inferences as
there are operations.
With `--tflite_backend flatbuffer_direct`, this uses the TensorFlow-free
ONNX/TFLite comparison path. The base report is
`<model_name>_accuracy_report.json` (`ONNX↔TFLite`).
If `--flatbuffer_direct_output_pytorch` is also enabled, onnx2tf additionally
emits `<model_name>_pytorch_accuracy_report.json` (`ONNX↔PyTorch`) and
`<model_name>_accuracy_comparison_report.json` using the same input samples.
When `--input_tflite_file_path` is specified together with
`--flatbuffer_direct_output_pytorch`, onnx2tf emits
`<model_name>_pytorch_accuracy_report.json` (`TFLite↔PyTorch`) and
`<model_name>_accuracy_comparison_report.json`.
-coton, --check_onnx_tf_outputs_sample_data_normalization
norm: Validate using random data normalized to the range 0.0 to 1.0
denorm: Validate using random data in the range 0.0 to 255.0
If there is a normalization layer at the model's entry point, or
if the model was trained on denormalized data, "denorm" must be specified.
Default: "norm"
-cotor CHECK_ONNX_TF_OUTPUTS_ELEMENTWISE_CLOSE_RTOL,
--check_onnx_tf_outputs_elementwise_close_rtol CHECK_ONNX_TF_OUTPUTS_ELEMENTWISE_CLOSE_RTOL
The relative tolerance parameter.
Default: 0.0
-cotoa CHECK_ONNX_TF_OUTPUTS_ELEMENTWISE_CLOSE_ATOL,
--check_onnx_tf_outputs_elementwise_close_atol CHECK_ONNX_TF_OUTPUTS_ELEMENTWISE_CLOSE_ATOL
The absolute tolerance parameter.
Default: 1e-4
-tdnp TEST_DATA_NHWC_PATH, --test_data_nhwc_path TEST_DATA_NHWC_PATH
Path to a numpy file (.npy) containing custom test data in NHWC format.
This is used for test inference and validation when check_onnx_tf_outputs options are enabled.
The numpy array should have shape [batch_size, height, width, 3] with values
normalized to the range [0, 1].
This option is useful for offline environments or when you want to use
specific test data for validation.
It is also accepted by -fdots, -fdodo, and -fdoep for 4D RGB image inputs.
For models with multiple inputs, the same test array is reused for each eligible input
after per-input resize/layout conversion.
Unlike -cind, this option is not used for INT8 calibration and does not accept mean/std.
-agj, --auto_generate_json
Automatically generates a parameter replacement JSON file that achieves minimal error
when converting the model. This option explores various parameter combinations to find
the best settings that result in successful conversion and highest accuracy.
The search stops when the final output OP accuracy check shows "Matches".
When used together with -cotof, the generated JSON is used to re-evaluate accuracy.
WARNING: This option performs an exhaustive search to find the optimal conversion patterns,
which can take a very long time depending on the model complexity.
-agje, --auto_generate_json_on_error
Attempts to generate a parameter replacement JSON when conversion fails or when accuracy
validation finds errors greater than 1e-2. Useful for quickly capturing fixes during
-cotof runs. Disabled by default to avoid unexpected file generation.
-dms, --disable_model_save
Does not save the converted model. For CIs RAM savings.
-n, --non_verbose
Shorthand to specify a verbosity of "error".
-v, --verbosity
Change the level of information printed.
Values are "debug", "info", "warn", and "error".
Default: "debug" (for backwards compatability)
脚本内用法
Click 至 expand
>>> from onnx2tf import convert
>>> help(convert)
Help on function convert in module onnx2tf:
convert(
input_onnx_file_path: Union[str, NoneType] = '',
input_tflite_file_path: Union[str, NoneType] = '',
onnx_graph: Union[onnx.onnx_ml_pb2.ModelProto, NoneType] = None,
output_folder_path: Union[str, NoneType] = 'saved_model',
output_signaturedefs: Optional[bool] = False,
output_h5: Optional[bool] = False,
output_keras_v3: Optional[bool] = False,
output_tfv1_pb: Optional[bool] = False,
output_weights: Optional[bool] = False,
copy_onnx_input_output_names_to_tflite: Optional[bool] = False,
output_integer_quantized_tflite: Optional[bool] = False,
flatbuffer_direct_output_saved_model: Optional[bool] = False,
flatbuffer_direct_output_pytorch: Optional[bool] = False,
flatbuffer_direct_output_torchscript: Optional[bool] = False,
flatbuffer_direct_output_dynamo_onnx: Optional[bool] = False,
flatbuffer_direct_output_exported_program: Optional[bool] = False,
native_pytorch_generation_timeout_sec: Optional[int] = 0,
tflite_backend: Optional[str] = 'flatbuffer_direct',
quant_norm_mean: Optional[str] = '[[[[0.485, 0.456, 0.406]]]]',
quant_norm_std: Optional[str] = '[[[[0.229, 0.224, 0.225]]]]',
quant_type: Optional[str] = 'per-channel',
custom_input_op_name_np_data_path: Optional[List] = None,
input_quant_dtype: Optional[str] = 'int8',
output_quant_dtype: Optional[str] = 'int8',
not_use_onnxsim: Optional[bool] = False,
not_use_opname_auto_generate: Optional[bool] = False,
batch_size: Union[int, NoneType] = None,
overwrite_input_shape: Union[List[str], NoneType] = None,
shape_hints: Union[List[str], NoneType] = None,
value_hints: Union[List[str], NoneType] = None,
no_large_tensor: Optional[bool] = False,
output_nms_with_dynamic_tensor: Optional[bool] = False,
output_nms_with_argmax: Optional[bool] = False,
switch_nms_version: Optional[str] = 'v4',
keep_ncw_or_nchw_or_ncdhw_input_names:联合[列表[str],NoneType] =无,
keep_nwc_or_nhwc_or_ndhwc_input_names:联盟[列表[str],NoneType] =无,
keep_shape_absolutely_input_names: Optional[List[str]] = None,
input_names_to_interrupt_model_conversion: Union[List[str], NoneType] = None,
output_names_to_interrupt_model_conversion: Union[List[str], NoneType] = None,
enable_auto_split_model: Optional[bool] = False,
auto_split_max_size: Union[Any, NoneType] = None,
auto_split_max_size_mb: Union[int, NoneType] = None,
disable_group_convolution: Union[bool, NoneType] = False,
enable_batchmatmul_unfold: Optional[bool] = False,
enable_rnn_unroll: Optional[bool] = False,
disable_suppression_flextranspose: Optional[bool] = False,
number_of_dimensions_after_flextranspose_compression: Optional[int] = 6,
disable_suppression_flexstridedslice: Optional[bool] = False,
disable_strict_mode: Optional[bool] = False,
number_of_dimensions_after_flexstridedslice_compression: Optional[int] = 5,
optimization_for_gpu_delegate: Optional[bool] = False,
replace_argmax_to_reducemax_and_indices_is_int64: Union[bool, NoneType] = False,
replace_argmax_to_reducemax_and_indices_is_float32: Union[bool, NoneType] = False,
replace_argmax_to_fused_argmax_and_indices_is_int64: Union[bool, NoneType] = False,
replace_argmax_to_fused_argmax_and_indices_is_float32: Union[bool, NoneType] = False,
fused_argmax_scale_ratio: Union[float, NoneType] = 0.5,
replace_to_pseudo_operators: List[str] = None,
mvn_epsilon: Union[float, NoneType] = 0.0000000001,
param_replacement_file: Optional[str] = '',
auto_generate_json: Optional[bool] = False,
auto_generate_json_on_error: Optional[bool] = False,
check_gpu_delegate_compatibility: Optional[bool] = False,
check_onnx_tf_outputs_elementwise_close: Optional[bool] = False,
check_onnx_tf_outputs_elementwise_close_full: Optional[bool] = False,
check_onnx_tf_outputs_sample_data_normalization: Optional[str] = 'norm',
check_onnx_tf_outputs_elementwise_close_rtol: Optional[float] = 0.0,
check_onnx_tf_outputs_elementwise_close_atol: Optional[float] = 1e-4,
eval_split_models: Optional[str] = None,
test_data_nhwc_path: Union[str, NoneType] = None,
disable_model_save: Union[bool, NoneType] = False,
non_verbose: Union[bool, NoneType] = False,
verbosity: Optional[str] = 'debug'
) -> keras.engine.training.Model
Convert ONNX to TensorFlow models.
Parameters
----------
input_onnx_file_path: Optional[str]
Input onnx file path.
Either input_onnx_file_path or input_tflite_file_path or onnx_graph must be specified.
input_tflite_file_path: Optional[str]
Input tflite file path.
If specified, runs tflite-direct import mode.
In this mode, ONNX-dependent conversion options are rejected except for
direct ModelIR rewrites such as
`input_names_to_interrupt_model_conversion`,
`output_names_to_interrupt_model_conversion`,
`disable_group_convolution=True`,
`enable_batchmatmul_unfold=True`, and
`enable_rnn_unroll=True`.
By default it exports SavedModel from imported ModelIR, and
`input_names_to_interrupt_model_conversion` and
`output_names_to_interrupt_model_conversion` are resolved against
imported ModelIR tensor names,
`output_h5=True`, `output_keras_v3=True`, and `output_tfv1_pb=True`
are also supported through an internal SavedModel bridge without
`tf_converter` fallback.
`disable_group_convolution=True`, `enable_batchmatmul_unfold=True`, and
`enable_rnn_unroll=True` are applied to imported ModelIR before
SavedModel export or split planning, and fail explicitly if the requested
rewrite is not applicable.
`disable_model_save=True` is supported and leaves no final artifacts in
`output_folder_path`.
enable_auto_split_model=True can also emit split TFLite artifacts.
`enable_auto_split_model=True` cannot be combined with
`output_h5=True`, `output_keras_v3=True`, or `output_tfv1_pb=True`.
When used with flatbuffer_direct_output_saved_model=True and split,
partition SavedModels are emitted instead of a single root SavedModel.
onnx_graph: Optional[onnx.ModelProto]
onnx.ModelProto.
Either input_onnx_file_path or input_tflite_file_path or onnx_graph must be specified.
onnx_graph If specified, ignore input_onnx_file_path and process onnx_graph.
output_folder_path: Optional[str]
Output tensorflow model folder path.
Default: "saved_model"
output_signaturedefs: Optional[bool]
Signature is added to the output for serving or for conversion
to other model formats. However, this can significantly reduce the speed
of model conversion and significant increase the size of the model.
output_h5: Optional[bool]
Output model in Keras H5 format.
With `tflite_backend="flatbuffer_direct"`, this is generated from an
internal SavedModel bridge without falling back to `tf_converter`.
Cannot be combined with `disable_model_save=True` or
`enable_auto_split_model=True` in `flatbuffer_direct`.
output_keras_v3: Optional[bool]
Output model in Keras (keras_v3) format.
With `tflite_backend="flatbuffer_direct"`, this is generated from an
internal SavedModel bridge without falling back to `tf_converter`.
Cannot be combined with `disable_model_save=True` or
`enable_auto_split_model=True` in `flatbuffer_direct`.
output_tfv1_pb: Optional[bool]
Output model in TF v1 (.pb) format.
With `tflite_backend="flatbuffer_direct"`, this is generated from an
internal SavedModel bridge without falling back to `tf_converter`.
Cannot be combined with `disable_model_save=True` or
`enable_auto_split_model=True` in `flatbuffer_direct`.
output_weights: Optional[bool]
Output weights in hdf5 format.
copy_onnx_input_output_names_to_tflite: Optional[bool]
Copy the input/output OP name of ONNX to the input/output OP name of tflite.
Due to Tensorflow internal operating specifications,
the input/output order of ONNX does not necessarily match
the input/output order of tflite.
Be sure to check that the input/output OP names in the generated
tflite file have been converted as expected.
Also, this option generates a huge JSON file as a temporary file for processing.
Therefore, it is strongly discouraged to use it on large models of hundreds
of megabytes or more.
output_integer_quantized_tflite: Optional[bool]
Output of integer quantized tflite.
tflite_backend: Optional[str]
TFLite generation backend.
"flatbuffer_direct"(default): Experimental direct FlatBuffer builder path.
"tf_converter": Use TensorFlow Lite Converter as an explicit compatibility path.
Note: "flatbuffer_direct" supports a limited builtin OP set,
FP32/FP16 export, limited dynamic-range quantization,
limited integer quantization, and limited int16-activation variants.
When the direct fast path is active, TensorFlow per-node conversion is skipped.
In that case, `convert()` may return `None` (TFLite artifacts are still generated).
flatbuffer_direct_output_saved_model: Optional[bool]
Output SavedModel directly from flatbuffer_direct ModelIR (float32).
Requires `tflite_backend="flatbuffer_direct"`.
Cannot be combined with `disable_model_save=True`.
Fails explicitly if `CUSTOM` ops are present.
When used together with split output, partition SavedModels are emitted
instead of a single root SavedModel.
flatbuffer_direct_output_pytorch: Optional[bool]
Output a reloadable PyTorch package directly from flatbuffer_direct
ModelIR. Public spatial inputs/outputs use NCW/NCHW/NCDHW.
Unsupported/CUSTOM ops and residual channel-last layout bridges fail
explicitly.
flatbuffer_direct_output_torchscript: Optional[bool]
Save a traced TorchScript file (`<model_name>_jit.pt`) into the
generated flatbuffer_direct PyTorch package.
Requires `tflite_backend="flatbuffer_direct"`.
Internally enables `flatbuffer_direct_output_pytorch=True`.
Only native PyTorch packages are supported. If package generation falls
back to a non-native backend, conversion fails explicitly.
For dynamic public inputs, a concrete trace shape/input is required.
Recommended: `shape_hints`
Also accepted: `test_data_nhwc_path` for eligible 4D RGB inputs, or
`custom_input_op_name_np_data_path` for per-input custom trace data.
flatbuffer_direct_output_dynamo_onnx: Optional[bool]
Save a Dynamo ONNX file (`<model_name>_dynamo.onnx`) into the
generated flatbuffer_direct PyTorch package.
Requires `tflite_backend="flatbuffer_direct"`.
Internally enables `flatbuffer_direct_output_pytorch=True`.
Only native PyTorch packages are supported. If package generation falls
back to a non-native backend, conversion fails explicitly.
For dynamic public inputs, a concrete example input is required.
Recommended: `shape_hints`
Also accepted: `test_data_nhwc_path` for eligible 4D RGB inputs, or
`custom_input_op_name_np_data_path` for per-input custom example data.
flatbuffer_direct_output_exported_program: Optional[bool]
Save a PyTorch ExportedProgram file (`<model_name>_ep.pt2`) into the
generated flatbuffer_direct PyTorch package.
Requires `tflite_backend="flatbuffer_direct"`.
Internally enables `flatbuffer_direct_output_pytorch=True`.
Only native PyTorch packages are supported. If package generation falls
back to a non-native backend, conversion fails explicitly.
For dynamic public inputs, a concrete example input is required.
Recommended: `shape_hints`
Also accepted: `test_data_nhwc_path` for eligible 4D RGB inputs, or
`custom_input_op_name_np_data_path` for per-input custom example data.
native_pytorch_generation_timeout_sec: Optional[int]
Timeout in seconds for generated native PyTorch package creation.
When exceeded, onnx2tf treats the generation as a recursion explosion,
aborts native PyTorch generation for the current model, and continues
conversion without PyTorch artifacts for that model.
`0` disables this timeout.
quant_norm_mean: Optional[str]
Normalized average value during quantization.
Only valid when the "-cind" option is not used.
Default: "[[[[0.485, 0.456, 0.406]]]]"
quant_norm_std: Optional[str]
Normalized standard deviation during quantization.
Only valid when the "-cind" option is not used.
Default: "[[[[0.229, 0.224, 0.225]]]]"
quant_type: Optional[str]
Selects whether "per-channel" or "per-tensor" quantization is used.
Default: "per-channel"
custom_input_op_name_np_data_path: Optional[List]
--custom_input_op_name_np_data_path INPUT_NAME NUMPY_FILE_PATH MEAN STD
Input name of OP and path of data file (Numpy) for custom input for
-cotof, -fdots, -fdodo, -fdoep, or -oiqt,
and mean (optional) and std (optional).
<Usage in -cotof>
When using -cotof, custom input defined by the user, instead of dummy data, is used.
In this case, mean and std are omitted from the input.
-cind {input_op_name} {numpy_file_path}
e.g. -cind onnx::Equal_0 test_cind/x_1.npy -cind onnx::Add_1 test_cind/x_2.npy -cotof
The input_op_name must be the same as in ONNX,
and it may not work if the input format is different between ONNX and TF.
<Usage in -fdots / -fdodo / -fdoep>
When using these PyTorch artifact export modes,
`custom_input_op_name_np_data_path` can be used to provide a concrete
example input for a dynamic public input.
For shape-only hints, prefer `shape_hints`.
For 4D RGB inputs, `test_data_nhwc_path` is also supported.
In these modes, mean and std are omitted from the input.
<Usage in -oiqt>
INPUT Name of OP and path of calibration data file (Numpy) for quantization
and mean and std.
This option is required when using -oiqt.
The type of the input OP must be Float32.
Data for calibration must be pre-normalized to a range of 0 to 1.
-cind {input_op_name} {numpy_file_path} {mean} {std}
Numpy file paths must be specified the same number of times as the number of input OPs.
Normalize the value of the input OP based on the tensor specified in mean and std.
(input_value - mean) / std
Tensors in Numpy file format must be in dimension order after conversion to TF.
Note that this is intended for deployment on low-resource devices,
so the batch size is limited to 1 only.
e.g.
The example below shows a case where there are three input OPs.
Assume input0 is 128x128 RGB image data.
In addition, input0 should be a value that has been divided by 255
in the preprocessing and normalized to a range between 0 and 1.
input1 and input2 assume the input of something that is not an image.
Because input1 and input2 assume something that is not an image,
the divisor is not 255 when normalizing from 0 to 1.
"n" is the number of calibration data.
ONNX INPUT shapes:
input0: [n,3,128,128]
mean: [1,3,1,1] -> [[[[0.485]],[[0.456]],[[0.406]]]]
std : [1,3,1,1] -> [[[[0.229]],[[0.224]],[[0.225]]]]
input1: [n,64,64]
mean: [1,64] -> [[0.1, ..., 0.64]]
std : [1,64] -> [[0.05, ..., 0.08]]
input2: [n,5]
mean: [1] -> [0.3]
std : [1] -> [0.07]
TensorFlow INPUT shapes (Numpy file ndarray shapes):
input0: [n,128,128,3]
mean: [1,1,1,3] -> [[[[0.485, 0.456, 0.406]]]]
std : [1,1,1,3] -> [[[[0.229, 0.224, 0.225]]]]
input1: [n,64,64]
mean: [1,64] -> [[0.1, ..., 0.64]]
std : [1,64] -> [[0.05, ..., 0.08]]
input2: [n,5]
mean: [1] -> [0.3]
std : [1] -> [0.07]
cind=[
["input0","../input0.npy",[[[[0.485, 0.456, 0.406]]]],[[[[0.229, 0.224, 0.225]]]]],
["input1","./input1.npy",[0.1, ..., 0.64],[0.05, ..., 0.08]],
["input2","input2.npy",[0.3],[0.07]],
]
<Using -cotof and -oiqt at the same time>
To use -cotof and -oiqt simultaneously,
you need to enter the Input name of OP, path of data file, mean, and std all together.
And the data file must be in Float32 format,
and {input_op_name}, {numpy_file_path}, {mean}, and {std} must all be entered.
Otherwise, an error will occur during the -oiqt stage.
input_quant_dtype: Optional[str]
Input dtypes when doing Full INT8 Quantization.
"int8"(default) or "uint8" or "float32"
output_quant_dtype: Optional[str]
Output dtypes when doing Full INT8 Quantization.
"int8"(default) or "uint8" or "float32"
not_use_onnxsim: Optional[bool]
No optimization by onnx-simplifier is performed.
If this option is used, the probability of a conversion error is very high.
Effective for both `tf_converter` and `flatbuffer_direct` when converting
ONNX input. With `input_tflite_file_path` and
`tflite_backend="flatbuffer_direct"`, this remains unsupported because
there is no ONNX preprocess stage.
not_use_opname_auto_generate: Optional[bool]
Automatic generation of each OP name in the old format ONNX file
and assignment of OP name are not performed.
batch_size: Optional[int]
Fixes the dynamic batch size to the specified numeric batch size.
A value of 1 or more must be specified.
overwrite_input_shape: Optional[List[str]]
Overwrite the input shape.
The format is
['i1:dim0,dim1,...,dimN', 'i2:dim0,dim1,...,dimN', 'i3:dim0,dim1,...,dimN']
When there is only one input, for example,
['data:1,3,224,224']
When there are multiple inputs, for example,
['data1:1,3,224,224','data2:1,3,112','data3:5']
A value of 1 or more must be specified.
Numerical values other than dynamic dimensions are ignored.
Ignores batch_size if specified at the same time as batch_size.
shape_hints: Optional[List[str]]
Shape hints for input tensors containing dynamic dimensions.
Specify input shapes for test inference with -cotof or -coto.
Unlike `--overwrite_input_shape`, this operation does not overwrite
the ONNX input shape with a static shape.
The format is
['i1:dim0,...,dimN', 'i2:dim0,...,dimN', 'i3:dim0,...,dimN']
When there is only one input, for example,
['data:1,3,224,224']
When there are multiple inputs, for example,
['data1:1,3,224,224', 'data2:1,3,112', 'data3:5']
A value of 1 or more must be specified.
Numerical values other than dynamic dimensions are ignored.
Also used as the recommended example-input hint source for
`flatbuffer_direct_output_torchscript`,
`flatbuffer_direct_output_dynamo_onnx`, and
`flatbuffer_direct_output_exported_program`.
value_hints: Optional[List[str]]
Value hints for dummy inference input tensors.
The format is
['input_name_1:value', 'input_name_2:value', '*:default_value']
"*" applies to all inputs not explicitly specified.
Values are scalar only.
no_large_tensor: Optional[bool]
Suppresses constant bloat caused by Tile OP when optimizing models in onnxsim.
See: https://github.com/daquexian/onnx-simplifier/issues/178
output_nms_with_dynamic_tensor: Optional[bool]
The number of bounding boxes in the NMS output results is
not fixed at the maximum number of max_output_boxes_per_class,
but rather at the smallest possible number of dynamic tensors.
If this option is disabled, NMS output is padded to the number
set in the max_output_boxes_per_class attribute.
e.g.
disable --output_nms_with_dynamic_tensor:
output_tensor_shape: [100, 7]
enable --output_nms_with_dynamic_tensor:
output_tensor_shape: [N, 7]
output_nms_with_argmax: Optional[bool]
Apply argmax over scores class dimension in NonMaxSuppression to
shrink scores from [B, C, N] to [B, 1, N].
switch_nms_version {v4,v5}
Switch the NMS version to V4 or V5 to convert.
e.g.
NonMaxSuppressionV4(default): switch_nms_version="v4"
NonMaxSuppressionV5: switch_nms_version="v5"
keep_ncw_or_nchw_or_ncdhw_input_names: Optional[List[str]]
Holds the NCW or NCHW or NCDHW of the input shape for the specified INPUT OP names.
If a nonexistent INPUT OP name is specified, it is ignored.
Valid only for 3D, 4D and 5D input tensors.
e.g.
keep_ncw_or_nchw_or_ncdhw_input_names=['input0','input1','input2']
keep_nwc_or_nhwc_or_ndhwc_input_names: Optional[List[str]]
Holds the NWC or NHWC or NDHWC of the input shape for the specified INPUT OP names.
If a nonexistent INPUT OP name is specified, it is ignored.
If the input OP name is the same as the input OP name specified
in the keep_ncw_or_nchw_or_ncdhw_input_names option, it is ignored.
Valid only for 3D, 4D and 5D input tensors.
e.g.
keep_nwc_or_nhwc_or_ndhwc_input_names=['input0','input1','input2']
keep_shape_absolutely_input_names: Optional[List[str]]
Name of the INPUT that unconditionally maintains its shape.
If a nonexistent INPUT OP name is specified, it is ignored.
e.g.
keep_shape_absolutely_input_names=['input0','input1','input2']
input_names_to_interrupt_model_conversion: Optional[List[str]]
Input names of ONNX that interrupt model conversion.
Interrupts model transformation at the specified input name
and inputs the model partitioned into subgraphs.
With `tflite_backend="flatbuffer_direct"`, this crops ModelIR and
treats the specified tensors as runtime inputs.
e.g.
input_names_to_interrupt_model_conversion=['input0','input1','input2']
output_names_to_interrupt_model_conversion: Optional[List[str]]
Output names of ONNX that interrupt model conversion.
Interrupts model transformation at the specified output name
and outputs the model partitioned into subgraphs.
With `tflite_backend="flatbuffer_direct"`, this crops ModelIR and
treats the specified tensors as runtime outputs.
e.g.
output_names_to_interrupt_model_conversion=['output0','output1','output2']
enable_auto_split_model: Optional[bool]
Force auto split regardless of the ONNX file size.
Uses auto_split_max_size as the target partition size.
In `flatbuffer_direct`, this runs the ModelIR split planner and forces
split manifest generation.
A small model may still result in a single-partition manifest.
Short option: -easm
Default: False
auto_split_max_size: Optional[Any]
Target maximum size per partition.
Supports values such as "512KB", "900MB", and "1.5GB".
Bare numeric values are treated as MB.
Used when auto-split is triggered or forced.
When specified, also used as the split target when enable_auto_split_model=True.
eval_split_models: Optional[str]
Evaluate split partitions sequentially using split manifest output.
Specify "unsplit_tflite" to compare against the unsplit/base TFLite model,
or "onnx" to compare against ONNX Runtime output.
Available only with tflite_backend="flatbuffer_direct" and
requires enable_auto_split_model=True.
Short option: -esm
Writes `*_split_accuracy_report.json`.
`*_accuracy_report.json` remains the unsplit base float32 TFLite vs ONNX report.
Default: None
auto_split_max_size_mb: Optional[int]
[Deprecated] Legacy alias of auto_split_max_size in MB.
disable_group_convolution: Optional[bool]
Disable GroupConvolution and replace it with SeparableConvolution for
conversion outputs.
This option is applied in both tf_converter and flatbuffer_direct paths.
With `input_tflite_file_path` and `tflite_backend="flatbuffer_direct"`,
this rewrites imported grouped `CONV_2D` into split-per-group direct ops
and fails explicitly when the grouping cannot be inferred safely.
enable_accumulation_type_float16: Optional[bool]
Hint for XNNPack fp16 inference on float16 tflite model.
XNNPACK float16 inference on certain ARM64 cores is 2x faster.
Float16 inference doubling on devices with ARM64 ARMv8.2 or higher instruction set.
This option is applied in both tf_converter and flatbuffer_direct paths.
https://github.com/tensorflow/tensorflow/blob/master/tensorflow/lite/delegates/xnnpack/README.md#floating-point-ieee-fp16-operators
enable_batchmatmul_unfold: Optional[bool]
BatchMatMul is separated batch by batch to generate a primitive MatMul.
With `tflite_backend="flatbuffer_direct"`, this runs as a ModelIR rewrite
for both ONNX input and `input_tflite_file_path`.
Imported/direct ModelIR must have fully static batch prefixes.
enable_rnn_unroll: Optional[bool]
Instead of increasing inference speed by expanding all symbolic loops of
the RNN (LSTM, GRU, RNN), RAM consumption will increase because all tensors
are expanded and embedded in the model.
With `tflite_backend="flatbuffer_direct"`, this rewrites supported
sequence RNN/LSTM ModelIR ops into step-unrolled primitive ops for both
ONNX input and `input_tflite_file_path`.
https://keras.io/api/layers/recurrent_layers/
disable_suppression_flextranspose: Optional[bool]
Disables FlexTranspose generation suppression.
With `tflite_backend="flatbuffer_direct"` on ONNX input, this emits a
single builtin `TRANSPOSE` without rank-compression.
With `input_tflite_file_path`, this option remains unsupported.
number_of_dimensions_after_flextranspose_compression: Optional[int]
Number of Transpose OP dimensions generated after avoiding FlexTranspose generation.
Also suppress the creation of the Transpose itself by specifying 2.
Default: 6
disable_suppression_flexstridedslice: Optional[bool]
Disables FlexStridedSlice generation suppression.
With `tflite_backend="flatbuffer_direct"` on ONNX input, this emits a
single builtin `SLICE`/`STRIDED_SLICE` without rank-compression.
With `input_tflite_file_path`, this option remains unsupported.
disable_strict_mode: Optional[bool]
If specified, the conversion speed is greatly accelerated because the strict accuracy
correction process is skipped, but the frequency of transposition errors increases
and accuracy errors are more likely to occur. Strict mode is enabled by default.
As of 2023.05.07, this is a work in progress and is an experimental feature.
Therefore, only some OPs are converted in strict mode for accuracy correction.
number_of_dimensions_after_flexstridedslice_compression: Optional[int]
Number of StridedSlice OP dimensions generated after avoiding FlexStridedSlice generation.
Default: 5
optimization_for_gpu_delegate: Optional[bool]
Replace operations that do not support gpu delegate with those
that do as much as possible.
Effective in both `tf_converter` and `flatbuffer_direct` for ONNX input.
In `flatbuffer_direct`, this now applies direct lowering rewrites for
broadcast arithmetic, Gather negative-index normalization, and Gemm bias
handling. With `input_tflite_file_path`, this option remains unsupported.
replace_argmax_to_reducemax_and_indices_is_int64: Optional[bool]
Replace ArgMax with a ReduceMax. The returned indices are int64.
Effective in both `tf_converter` and `flatbuffer_direct` for ONNX input.
With `input_tflite_file_path`, this option remains unsupported.
Only one of replace_argmax_to_reducemax_and_indices_is_int64 and
replace_argmax_to_reducemax_and_indices_is_float32 and
replace_argmax_to_fused_argmax_and_indices_is_int64 and
replace_argmax_to_fused_argmax_and_indices_is_float32 can be specified.
Default: False
replace_argmax_to_reducemax_and_indices_is_float32: Optional[bool]
Replace ArgMax with a ReduceMax. The returned indices are float32.
Effective in both `tf_converter` and `flatbuffer_direct` for ONNX input.
With `input_tflite_file_path`, this option remains unsupported.
Only one of replace_argmax_to_reducemax_and_indices_is_int64 and
replace_argmax_to_reducemax_and_indices_is_float32 and
replace_argmax_to_fused_argmax_and_indices_is_int64 and
replace_argmax_to_fused_argmax_and_indices_is_float32 can be specified.
Default: False
replace_argmax_to_fused_argmax_and_indices_is_int64: Optional[bool]
Replace ArgMax with a ReduceMax. The returned indices are int64.
It improves inference speed at the cost of a small sacrifice in accuracy.
See. https://github.com/tensorflow/models/tree/master/official/projects/edgetpu/vision#argmax-fusion-to-improve-segmentation-model-latency
Currently, only 4D tensors are supported.
Effective in both `tf_converter` and `flatbuffer_direct` for ONNX input.
In `flatbuffer_direct`, this currently targets `Resize -> ArgMax` 4D
patterns. With `input_tflite_file_path`, this option remains unsupported.
Only one of replace_argmax_to_reducemax_and_indices_is_int64 and
replace_argmax_to_reducemax_and_indices_is_float32 and
replace_argmax_to_fused_argmax_and_indices_is_int64 and
replace_argmax_to_fused_argmax_and_indices_is_float32 can be specified.
Default: False
replace_argmax_to_fused_argmax_and_indices_is_float32: Optional[bool]
Replace ArgMax with a ReduceMax. The returned indices are float32.
It improves inference speed at the cost of a small sacrifice in accuracy.
See. https://github.com/tensorflow/models/tree/master/official/projects/edgetpu/vision#argmax-fusion-to-improve-segmentation-model-latency
Currently, only 4D tensors are supported.
Effective in both `tf_converter` and `flatbuffer_direct` for ONNX input.
In `flatbuffer_direct`, this currently targets `Resize -> ArgMax` 4D
patterns. With `input_tflite_file_path`, this option remains unsupported.
Only one of replace_argmax_to_reducemax_and_indices_is_int64 and
replace_argmax_to_reducemax_and_indices_is_float32 and
replace_argmax_to_fused_argmax_and_indices_is_int64 and
replace_argmax_to_fused_argmax_and_indices_is_float32 can be specified.
Default: False
fused_argmax_scale_ratio: Optional[float]
For Fused ArgMax.
Scale ratio when generating Fused ArgMax.
0.0 < fused_argmax_scale_ratio <= 1.0
Default: 0.5
replace_to_pseudo_operators: List[str]
Replace list of operators to pseudo operators.
Full name of the target operators should be given.
Currently supported operators :
Asin, Acos, Atan, Abs, PReLU, LeakyReLU, Power, GatherND, Neg, HardSwish, Erf, GeLU, MatMulInteger, Inverse
Note: Inverse is pseudo-lowered by default. Specifying Inverse keeps MatrixInverse/FlexMatrixInverse.
mvn_epsilon: Optional[float]
For MeanVarianceNormalization.
The number to be added to the variance to avoid division by zero
when normalizing the value.
(input_tensor - mean) / tf.sqrt(variance + mvn_epsilon)
Effective in both `tf_converter` and `flatbuffer_direct` for ONNX input.
Default: 0.0000000001
param_replacement_file: Optional[str]
Parameter replacement file path. (.json)
auto_generate_json: Optional[bool]
Automatically generates a parameter replacement JSON file that achieves minimal error
when converting the model. This option explores various parameter combinations to find
the best settings that result in successful conversion and highest accuracy.
The search stops when the final output OP accuracy check shows "Matches".
When used together with check_onnx_tf_outputs_elementwise_close_full,
the generated JSON is used to re-evaluate accuracy.
Default: False
auto_generate_json_on_error: Optional[bool]
When conversion fails or accuracy validation detects errors greater than 1e-2,
attempts to generate a parameter replacement JSON as a best-effort fix.
Default: False
check_gpu_delegate_compatibility: Optional[bool]
Run TFLite ModelAnalyzer on the generated Float16 tflite model
to check if the model can be supported by GPU Delegate.
e.g.
"""
=== TFLite ModelAnalyzer ===
Your TFLite model has '1' subgraph(s). In the subgraph description below,
T# represents the Tensor numbers. For example, in Subgraph#0, the RESHAPE op takes
tensor #0 and tensor #6 as input and produces tensor #7 as output.
Subgraph#0 main(T#0) -> [T#17]
Op#0 RESHAPE(T#0, T#6[2, 8, 8, 3, 2, ...]) -> [T#7]
Op#1 SPLIT(T#5[0], T#7) -> [T#8, T#9]
Op#2 RESHAPE(T#8, T#1[8, 8, 3, 2, 2]) -> [T#10]
Op#3 TRANSPOSE(T#10, T#4[0, 3, 1, 4, 2]) -> [T#11]
Op#4 RESHAPE(T#11, T#2[1, 8, 2, 8, 2, ...]) -> [T#12]
Op#5 RESHAPE(T#9, T#1[8, 8, 3, 2, 2]) -> [T#13]
Op#6 TRANSPOSE(T#13, T#4[0, 3, 1, 4, 2]) -> [T#14]
Op#7 RESHAPE(T#14, T#2[1, 8, 2, 8, 2, ...]) -> [T#15]
Op#8 CONCATENATION(T#12, T#15) -> [T#16]
Op#9 RESHAPE(T#16, T#3[2, 16, 16, 3]) -> [T#17]
Tensors of Subgraph#0
T#0(inputs_0) shape:[2, 8, 8, 12], type:FLOAT32
T#1(model/tf.compat.v1.squeeze_2/Squeeze) shape:[5], type:INT32 RO 20 bytes, data:[8, 8, 3, 2, 2]
T#2(model/tf.expand_dims_1/ExpandDims) shape:[6], type:INT32 RO 24 bytes, data:[1, 8, 2, 8, 2, ...]
T#3(model/tf.reshape_1/Reshape/shape) shape:[4], type:INT32 RO 16 bytes, data:[2, 16, 16, 3]
T#4(model/tf.compat.v1.transpose/transpose/perm) shape:[5], type:INT32 RO 20 bytes, data:[0, 3, 1, 4, 2]
T#5(model/tf.concat/concat/axis) shape:[], type:INT32 RO 4 bytes, data:[0]
T#6(model/tf.reshape/Reshape/shape) shape:[6], type:INT32 RO 24 bytes, data:[2, 8, 8, 3, 2, ...]
T#7(model/tf.reshape/Reshape) shape:[2, 8, 8, 3, 2, 2], type:FLOAT32
T#8(model/tf.split/split) shape:[1, 8, 8, 3, 2, 2], type:FLOAT32
T#9(model/tf.split/split1) shape:[1, 8, 8, 3, 2, 2], type:FLOAT32
T#10(model/tf.compat.v1.squeeze_1/Squeeze) shape:[8, 8, 3, 2, 2], type:FLOAT32
T#11(model/tf.compat.v1.transpose/transpose) shape:[8, 2, 8, 2, 3], type:FLOAT32
T#12(model/tf.expand_dims/ExpandDims) shape:[1, 8, 2, 8, 2, 3], type:FLOAT32
T#13(model/tf.compat.v1.squeeze_2/Squeeze1) shape:[8, 8, 3, 2, 2], type:FLOAT32
T#14(model/tf.compat.v1.transpose_1/transpose) shape:[8, 2, 8, 2, 3], type:FLOAT32
T#15(model/tf.expand_dims_1/ExpandDims1) shape:[1, 8, 2, 8, 2, 3], type:FLOAT32
T#16(model/tf.concat/concat) shape:[2, 8, 2, 8, 2, 3], type:FLOAT32
T#17(Identity) shape:[2, 16, 16, 3], type:FLOAT32
Your model looks compatibile with GPU delegate with TFLite runtime version 2.10.0.
But it doesn't guarantee that your model works well with GPU delegate.
There could be some runtime incompatibililty happen.
---------------------------------------------------------------
Model size: 2988 bytes
Non-data buffer size: 2757 bytes (92.27 %)
Total data buffer size: 231 bytes (07.73 %)
(Zero value buffers): 4 bytes (00.13 %)
* Buffers of TFLite model are mostly used for constant tensors.
And zero value buffers are buffers filled with zeros.
Non-data buffers area are used to store operators, subgraphs and etc.
You can find more details from https://github.com/google-ai-edge/LiteRT/blob/v2.1.2/tflite/converter/schema/schema.fbs
"""
check_onnx_tf_outputs_elementwise_close: Optional[bool]
Returns "Matches" if the output of onnx and the output of TF are
within acceptable proximity element by element.
Returns "Unmatched" if the output of onnx and the output of TF are
not within acceptable proximity element by element.
If the output of onnx is 1D, it returns "Skipped" and skips the comparison
between the output of onnx and that of TF. This is because when undefined
dimensions are present, a situation often arises where very large index
values are compared, causing OutOfMemory.
Only the output content of the models final output OP is checked.
check_onnx_tf_outputs_elementwise_close_full: Optional[bool]
Returns "Matches" if the output of onnx and the output of TF are
within acceptable proximity element by element.
Check the output of all OPs in sequence from the beginning,
including all but the final output OP of the model.
Returns "Unmatched" if the output of onnx and the output of TF are
not within acceptable proximity element by element.
If the output of onnx is 1D, it returns "Skipped" and skips the comparison
between the output of onnx and that of TF. This is because when undefined
dimensions are present, a situation often arises where very large index
values are compared, causing OutOfMemory.
It is very time consuming because it performs as many inferences as
there are operations.
check_onnx_tf_outputs_sample_data_normalization: Optional[str]
norm: Validate using random data normalized to the range 0.0 to 1.0
denorm: Validate using random data in the range 0.0 to 255.0
If there is a normalization layer at the models entry point, or
if the model was trained on denormalized data, "denorm" must be specified.
Default: "norm"
check_onnx_tf_outputs_elementwise_close_rtol: Optional[float]
The relative tolerance parameter.
Default: 0.0
check_onnx_tf_outputs_elementwise_close_atol: Optional[float]
The absolute tolerance parameter.
Default: 1e-4
test_data_nhwc_path: Optional[str]
Path to a numpy file (.npy) containing custom test data in NHWC format.
This is used for test inference and validation when check_onnx_tf_outputs options are enabled.
The numpy array should have shape [batch_size, height, width, 3] with values
normalized to the range [0, 1].
This option is useful for offline environments or when you want to use
specific test data for validation.
It is also accepted by -fdots, -fdodo, and -fdoep for 4D RGB image inputs.
disable_model_save: Optional[bool]
Does not save the converted model. For CIs RAM savings.
With `tflite_backend="flatbuffer_direct"`, conversion may still use
temporary staging and validation internally, but no final artifacts are
left in `output_folder_path`.
Default: False
non_verbose: Optional[bool]
Shorthand to specify a verbosity of "error".
Default: False
verbosity: Optional[str]
Change the level of information printed.
Values are "debug", "info", "warn", and "error".
Default: "debug" (for backwards compatability)
Returns
----------
model: tf_keras.Model
Model
参数替换
该工具用于将NCW转换为NWC,NCHW转换为NHWC,NCDHW转换为NDHWC,NCDDHW转换为NDDHWC、NCDDDDDDHW 至 NDDDDDDHWC。因此,正如关键概念中所述,转换将不可避免地在模型中的某个点发生故障。您需要查看整个转换日志以查看哪些 OP 转置失败并自行更正它们。我不敢解释得很少,因为我知道无论我在README里写得多么详细,你们根本就不会看。 attribute 或 INPUT constant 或 INPUT Initializer 可以替换为指定值。
Click 至 expand
从v1.3.0开始,除了一些特殊的OPs之外,几乎所有的OPs都支持pre_process_transpose和post_process_transpose的前后转置。
- “发生转换错误。”
- “输出结果错误。”
不要提交仅包含无法重现的信息量的问题。
-
转换选项
--param_replacement_file param_replacement.json or -prf param_replacement.json -
param_replacement.json
See 替换样品 JSON
{
"format_version": 1,
"operations": [
{
"op_name": "StatefulPartitionedCall/Tile_4",
"param_target": "inputs", # attributes or inputs
"param_name": "const_fold_opt__677",
"values": [1,1,17] # Disable parameter transposition or overwrite parameters
},
{
"op_name": "StatefulPartitionedCall/Cast_3",
"param_target": "attributes", # attributes or inputs
"param_name": "to",
"values": 1 # Disable parameter transposition or overwrite "to" parameters
},
{
"op_name": "Resize__697",
"param_target": "inputs",
"param_name": "Concat__696:0",
"values": [26,26] # Replacement of unk__x (Resize OP, sizes height/width parameter)
},
{
"op_name": "Transpose__927",
"param_target": "attributes",
"param_name": "perm",
"values": [0,1,2,3] # Disable parameter transposition or overwrite "perm" parameters
},
{
"op_name": "StatefulPartitionedCall/functional_1/max_unpooling2d_2/Reshape_1",
"param_target": "inputs",
"param_name": "const_fold_opt__911",
"values": [4,131072] # Overwrite "shape" parameters
},
{
"op_name": "Reshape_25",
"param_target": "outputs",
"param_name": "onnx::InstanceNormalization_270",
"post_process_transpose_perm": [0,2,1] # Extrapolate 3D Transpose after Reshape
},
{
"op_name": "Reshape_30",
"param_target": "outputs",
"param_name": "onnx::Mul_275",
"post_process_transpose_perm": [0,2,3,1] # Extrapolate 4D Transpose after Reshape
},
{
"op_name": "flatten_1127",
"param_target": "inputs",
"param_name": "dropout0",
"pre_process_transpose_perm": [0,3,1,2]
},
{
"op_name": "/Slice",
"param_target": "op",
"begin": [0,0,1,0],
"end": [0,0,0,0],
"end_mask": 15
},
{
"op_name": "/Slice_1",
"param_target": "op",
"begin": [0,0,0,0],
"end": [0,0,39,0],
"end_mask": 11
},
{
"op_name": "/backbone/backbone.1/Unsqueeze_1",
"param_target": "op",
"new_shape": [1,15,15,1]
}
]
}
- 支持更换 OPs
See 替换清单 specifications
| 不。 | OP型 | 备注 | |||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 1 | 添加 | 1. "param_target": "inputs" | |||||||||||||||||||||||||||||
pre_process_transpose_perm:在 Add 操作之前将转置应用于张量,并将 perm 指定为 pre-processing. |
|||||||||||||||||||||||||||||||
| 2。 "param_target": "outputs" | |||||||||||||||||||||||||||||||
post_process_transpose_perm:在 Add 操作之后将转置应用于张量,并将 perm 指定为后处理。 |
|||||||||||||||||||||||||||||||
| 2 | 演员阵容 |
|
|||||||||||||||||||||||||||||
| 3 | 康卡特 | 1. "param_target": "属性" | |||||||||||||||||||||||||||||
axis:axis |
|||||||||||||||||||||||||||||||
| 2 的值。 "param_target": "outputs" | |||||||||||||||||||||||||||||||
post_process_transpose_perm:在 Concat 操作之后将转置应用于张量,并将 perm 指定为后处理。 |
|||||||||||||||||||||||||||||||
| 4 | ConvTranspose | ConvTranspose 单独实现特殊替换,忽略所有自动转换,通过指定所有 parameters. |
|||||||||||||||||||||||||||||
| https://www.tensorflow.org/api_docs/python/tf/nn/conv1d_transpose | |||||||||||||||||||||||||||||||
| https://www.tensorflow.org/api_docs/python/tf/nn/conv2d_transpose | |||||||||||||||||||||||||||||||
| https://www.tensorflow.org/api_docs/python/tf/nn/conv3d_transpose | |||||||||||||||||||||||||||||||
1 直接生成 tf.nn.conv1d_transpose 或 tf.nn.conv2d_transpose 或 tf.nn.conv3d_transpose。 "param_target": "op" |
|||||||||||||||||||||||||||||||
output_shape:output_shape |
|||||||||||||||||||||||||||||||
strides 的值:strides |
|||||||||||||||||||||||||||||||
padding 的值:padding |
|||||||||||||||||||||||||||||||
dilations 的值:dilations 的值 |
|||||||||||||||||||||||||||||||
| 5 | 分区 | 1. "param_target": "inputs" | |||||||||||||||||||||||||||||
values:input |
|||||||||||||||||||||||||||||||
pre_process_transpose_perm 的值:转置在 Div 操作之前应用于张量,perm 指定为 pre-processing. |
|||||||||||||||||||||||||||||||
| 2。 "param_target": "outputs" | |||||||||||||||||||||||||||||||
post_process_transpose_perm:在 Div 操作之后将转置应用于张量,并将 perm 指定为后处理。 |
|||||||||||||||||||||||||||||||
| 6 | 展开 | 1. "param_target": "inputs" | |||||||||||||||||||||||||||||
values:shape |
|||||||||||||||||||||||||||||||
pre_process_transpose_perm 的值:在 Expand 操作之前将转置应用于张量,并将 perm 指定为 pre-processing. |
|||||||||||||||||||||||||||||||
| 2。 "param_target": "outputs" | |||||||||||||||||||||||||||||||
post_process_transpose_perm:在 Expand 操作之后将转置应用于张量,并将 perm 指定为后处理。 |
|||||||||||||||||||||||||||||||
| 7 | 展平 | 1. "param_target": "属性" | |||||||||||||||||||||||||||||
axis:axis |
|||||||||||||||||||||||||||||||
| 2 的值。 "param_target": "inputs" | |||||||||||||||||||||||||||||||
pre_process_transpose_perm:在 Flatten 操作之前将转置应用于张量,并将 perm 指定为 pre-processing. |
|||||||||||||||||||||||||||||||
| 3。 "param_target": "outputs" | |||||||||||||||||||||||||||||||
post_process_transpose_perm:转置在 Flatten 操作之后应用于张量,并将 perm 指定为后处理。 |
|||||||||||||||||||||||||||||||
| 8 | 格姆 | ||||||||||||||||||||||||||||||
| 9 | 聚集 | 1. "param_target": "属性" | |||||||||||||||||||||||||||||
axis:axis |
|||||||||||||||||||||||||||||||
| 2 的值。 "param_target": "inputs" | |||||||||||||||||||||||||||||||
values:indices |
|||||||||||||||||||||||||||||||
pre_process_transpose_perm 的值:在 Gather 操作之前将转置应用于张量,并将 perm 指定为 pre-processing. |
|||||||||||||||||||||||||||||||
| 3。 "param_target": "outputs" | |||||||||||||||||||||||||||||||
post_process_transpose_perm:在 Gather 操作之后将转置应用于张量,并将 perm 指定为后处理。 |
|||||||||||||||||||||||||||||||
| 10 | MatMul | 1. "param_target": "inputs" | |||||||||||||||||||||||||||||
pre_process_transpose_perm:在 MatMul 操作之前将转置应用于张量,并将 perm 指定为 pre-processing. |
|||||||||||||||||||||||||||||||
| 2。 "param_target": "outputs" | |||||||||||||||||||||||||||||||
post_process_transpose_perm:在 MatMul 操作之后将转置应用于张量,并将 perm 指定为后处理。 |
|||||||||||||||||||||||||||||||
| 11 | 穆尔 | 1. "param_target": "inputs" | |||||||||||||||||||||||||||||
values:input |
|||||||||||||||||||||||||||||||
pre_process_transpose_perm 的值:在 Mul 操作之前将转置应用于张量,并将 perm 指定为 pre-processing. |
|||||||||||||||||||||||||||||||
| 2。 "param_target": "outputs" | |||||||||||||||||||||||||||||||
post_process_transpose_perm:转置在 Mul 操作之后应用于张量,并将 perm 指定为后处理。 |
|||||||||||||||||||||||||||||||
| 12 | NonMaxSuppression | ||||||||||||||||||||||||||||||
| 13 | ReduceL1 | ||||||||||||||||||||||||||||||
| ReduceL2 | |||||||||||||||||||||||||||||||
| ReduceLogSum | |||||||||||||||||||||||||||||||
| ReduceLogSumExp | |||||||||||||||||||||||||||||||
| ReduceMax | |||||||||||||||||||||||||||||||
| ReduceMean | |||||||||||||||||||||||||||||||
| ReduceMin | |||||||||||||||||||||||||||||||
| ReduceProd | |||||||||||||||||||||||||||||||
| ReduceSum | |||||||||||||||||||||||||||||||
| ReduceSumSquare | 1. "param_target": "属性" | ||||||||||||||||||||||||||||||
axes:axes |
|||||||||||||||||||||||||||||||
keepdims 的值:keepdims |
|||||||||||||||||||||||||||||||
| 2 的值。 "param_target": "inputs" | |||||||||||||||||||||||||||||||
pre_process_transpose_perm:在 ReduceXX 操作之前将转置应用于张量,并将 perm 指定为 pre-processing. |
|||||||||||||||||||||||||||||||
| 3。 "param_target": "outputs" | |||||||||||||||||||||||||||||||
post_process_transpose_perm:在 ReduceXX 操作之后将转置应用于张量,并将 perm 指定为后处理。 |
|||||||||||||||||||||||||||||||
| 14 | 松开 | 1. "param_target": "inputs" | |||||||||||||||||||||||||||||
pre_process_transpose_perm:在 Unsqueeze 操作之前将转置应用于张量,并将 perm 指定为 pre-processing. |
|||||||||||||||||||||||||||||||
| 2。 "param_target": "outputs" | |||||||||||||||||||||||||||||||
post_process_transpose_perm:在 Unsqueeze 操作之后将转置应用于张量,并将 perm 指定为 post-processing. |
|||||||||||||||||||||||||||||||
| 3。 "param_target": "op" | |||||||||||||||||||||||||||||||
new_shape: 直接指定 Unsqueeze 后的形状 processing. |
|||||||||||||||||||||||||||||||
| { | |||||||||||||||||||||||||||||||
| "op_name": "/backbone/backbone.1/Unsqueeze_1", | |||||||||||||||||||||||||||||||
| "param_target": “操作”, | |||||||||||||||||||||||||||||||
| “new_shape”:[1,15,15,1] | |||||||||||||||||||||||||||||||
| } | |||||||||||||||||||||||||||||||
| 15 | 重塑 | 1. "param_target": "inputs" | |||||||||||||||||||||||||||||
values:shape |
|||||||||||||||||||||||||||||||
pre_process_transpose_perm 的值:在 Reshape 操作之前将转置应用于张量,并将 perm 指定为 pre-processing. |
|||||||||||||||||||||||||||||||
| 2。 "param_target": "outputs" | |||||||||||||||||||||||||||||||
post_process_transpose_perm:在 Reshape 操作之后将转置应用于张量,并将 perm 指定为后处理。 |
|||||||||||||||||||||||||||||||
| 16 | 调整大小 | 1. "param_target": "属性" | |||||||||||||||||||||||||||||
coordinate_transformation_mode:coordinate_transformation_mode |
|||||||||||||||||||||||||||||||
extrapolation_value 的值:extrapolation_value |
|||||||||||||||||||||||||||||||
mode 的值:mode |
|||||||||||||||||||||||||||||||
cubic_coeff_a 的值:cubic_coeff_a |
|||||||||||||||||||||||||||||||
exclude_outside 的值:exclude_outside |
|||||||||||||||||||||||||||||||
| 2 的值。 "param_target": "输入" | |||||||||||||||||||||||||||||||
values:roi 或 scales 或 sizes 的值。 scales=[scale_h,scale_w],sizes=[h,w] |
|||||||||||||||||||||||||||||||
pre_process_transpose_perm:在 Resize 操作之前将转置应用于张量,并将 perm 指定为 pre-processing. |
|||||||||||||||||||||||||||||||
| 3。 "param_target": "outputs" | |||||||||||||||||||||||||||||||
post_process_transpose_perm:在 Resize 操作之后将转置应用于张量,并将 perm 指定为后处理。 |
|||||||||||||||||||||||||||||||
| 17 | Slice | Slice implements special replacements separately ignore all automatic conversions and generate tf.strided_slice directly by specifying all parameters of tf.strided_slice directly. |
|||||||||||||||||||||||||||||
| https://www.tensorflow.org/api_docs/python/tf/strided_slice | |||||||||||||||||||||||||||||||
| See json_samples/replace_slice.json for a sample description. |
- "param_target": "op"
begin: Value ofbeginend: Value ofendstrides: Value ofstridesbegin_mask: Value ofbegin_maskend_mask: Value ofend_maskellipsis_mask: Value ofellipsis_masknew_axis_mask: Value ofnew_axis_maskshrink_axis_mask: Value ofshrink_axis_mask{ "op_name": "/Slice", "param_target": "op", "begin": [0,0,1,0], "end": [0,0,0,0], "end_mask": 15 }| |18|软最大|1. "param_target": "属性"axis:axis的值。在Softmax. 2 之前和之后推断对应于指定轴的转置。 "param_target": "输入"values:tensor的值| |19|斯普利特|1. "param_target": "输入"values:split2 的值。 "param_target": "属性"axis:axis.num_outputs的值:num_outputs的值。| |20|子|1. "param_target": "inputs"values:inputpre_process_transpose_perm的值:在 Sub 操作之前将转置应用于张量,并将 perm 指定为 pre-processing. 2。 "param_target": "outputs"post_process_transpose_perm:转置在 Sub 操作之后应用于张量,并将 perm 指定为后处理。| |21|瓷砖|1. "param_target": "inputs"values:inputpre_process_transpose_perm的值:在 Tile 操作之前将转置应用于张量,并将 perm 指定为 pre-processing. 2。 "param_target": "outputs"post_process_transpose_perm:转置在 Tile 操作之后应用于张量,并将 perm 指定为后处理。| |22|转置|1. "param_target": "属性"perm:perm2 的值。 "param_target": "输入"values:tensor的值|
生成模型
- 带后处理的 YOLOv7-tiny (NMS) ONNX 至 TFLite Float32 https://github.com/PINTO0309/onnx2tf/releases/download/0.0.33/yolov7_tiny_head_0.768_post_480x640.onnx
See model
的结构
|昂恩克斯2tf|onnx-tensorflow (超级冗余+损坏)| |:-:|:-:| |||
- YOLACT-Edge MobileNetV2 带后处理 (MultiClass-NMS) ONNX 至 TFLite Float32 https://github.com/PINTO0309/onnx2tf/releases/download/1.0.11/yolact_edge_mobilenetv2_550x550.onnx
See model
的结构
- MoveNet MultiPose ONNX 至 TFLite Float32(
Cast和TrueDiv标准 OP 支持) https://github.com/PINTO0309/onnx2tf/releases/download/1.0.24/movenet_multipose_lightning_192x256_p6.onnx
See model
的结构
相关工具
- tflite2json2tflite
- tensorflowjs_converter
- coremltools
- simple-onnx-processing-tools
- onnxsim
- onnx
- TinyNeuralNetwork
- onnx2torch
- 手电筒
- 人工智能边缘量化器
- LiteRT.js
- pnnx
-
09.11
pytorch-gradual-warmup-lr:实践指南
-
09.11
EASY-HWID-SPOOFER:实践指南
-
09.11
computer-museum-dnbwg:实践指南
-
09.11
tmux-better-mouse-mode:实践指南
-
09.11
onnx2tf:实践指南
-
09.11
keras-hub:实践指南
-
-
- onnx2tf:实践指南
- 09.11
-
- Blackout:实践指南
- 09.11
-
- wechat-app-mall:实践指南
- 09.11
-
- keras-hub:实践指南
- 09.11
-
-
下载
- |
-
-
下载
- 《行尸走肉第一章》免安装中文汉化硬盘版下载
- 单机|436 MB
- 一款以动作冒险为主题的游戏
-
-
下载
- 《街头霸王X铁拳》免安装中文汉化硬盘版下载
- 单机|111MB
- 一款非常好玩的格斗游戏
-
-
下载
- |
-
-
下载
- 《暗黑破坏神3》免安装繁体中文正式版下载
- 单机|7630 MB
- 一款以角色扮演为主题的游戏
-
-
下载
- 《马克思佩恩3》免安装硬盘版下载
- 单机|27033 MB
- 一款以第三人称射击为主题的游戏