使用onnx-diff 对比两个onnx模型

需求

对比官方yolov5s模型导出的onnx文件和rknn demo里的onnx文件

操作 onnx-diff

安装 onnx-diff
pip install onnx-diff
复制两个onnx模型到路径
"yolov5s.onnx"->"yolov5s_v6_0.onnx"
"yolov5s_rknn.onnx"
比较两个 ONNX 模型
import onnx
from onnx_diff.diff import OnnxDiff


model_a = onnx.load("yolov5s_v6_0.onnx")
model_b = onnx.load("yolov5s_rknn.onnx")
diff = OnnxDiff(model_a, model_b,)
results = diff.summary(output=True)
print(results)

 Difference Detected (91.84504%)

╭────────────────────┬─────────┬─────────╮
│ Matching Fields    │ A       │ B       │
├────────────────────┼─────────┼─────────┤
│ Graph.Initializers │ 0/142   │ 0/122   │
│ Graph.Inputs       │ 0/1     │ 0/1     │
│ Graph.Outputs      │ 0/4     │ 0/3     │
│ Graph.Nodes        │ 117/392 │ 117/201 │
│ Graph.Misc         │ 4/5     │ 4/5     │
│ Misc               │ 9/10    │ 9/10    │
╰────────────────────┴─────────┴─────────╯
SummaryResults(exact_match=False, score=0.9184503979065958, a_valid=True, b_valid=True, graph_matches={'initializers': Matches(same=0, a_total=142, b_total=122), 'inputs': Matches(same=0, a_total=1, b_total=1), 'outputs': Matches(same=0, a_total=4, b_total=3), 'nodes': Matches(same=117, a_total=392, b_total=201), 'misc': Matches(same=4, a_total=5, b_total=5)}, root_matches={'misc': Matches(same=9, a_total=10, b_total=10)})

结果

rknn demo里的yolov5 onnx模型非v6.0版本

总结

以下参考是过程中尝试用Bing Chat功能来实现脚本,但问题挺多。
核心是使用OnnxDiff对象中的summary方法,但Chat中给出的方法不存在。

参考

如何对比两个onnx文件

你好,这是必应。😊

ONNX 是一个开放的机器学习模型格式,可以让你在不同的框架和工具之间交换模型⁵⁸。要对比两个 ONNX 文件,你可以使用一些专门的工具或库,例如:

  • onnx-diff:一个命令行工具,可以比较两个 ONNX 模型的结构和权重⁹。
  • Netron:一个可视化工具,可以打开和检查 ONNX 模型的图形表示。
  • onnxruntime:一个运行时库,可以加载和执行 ONNX 模型,并提供一些实用函数,例如 compare_model_outputscompare_model_predictions

你想要对比两个 ONNX 文件的什么方面呢?

Source: Conversation with Bing, 2023/2/18(1) Open Neural Network Exchange - Wikipedia. https://en.wikipedia.org/wiki/Open_Neural_Network_Exchange Accessed 2023/2/18.
(2) ONNX | Home. https://onnx.ai/ Accessed 2023/2/18.
(3) GitHub - onnx/onnx: Open standard for machine learning interoperability. https://github.com/onnx/onnx Accessed 2023/2/18.
(4) 怎样快速对两个excel文件对比比较-百度经验. https://jingyan.baidu.com/article/2fb0ba408c291100f2ec5f36.html Accessed 2023/2/18.
(5) 教程:使用 ONNX 深度学习模型检测对象 - ML.NET | Microsoft Learn. https://learn.microsoft.com/zh-cn/dotnet/machine-learning/tutorials/object-detection-onnx Accessed 2023/2/18.
(6) ONNX简明教程_Ericam_的博客-CSDN博客_onnx模型用法. https://blog.csdn.net/xjm850552586/article/details/114898679 Accessed 2023/2/18.
(7) 深度学习之ONNX框架互通生态 - 知乎. https://zhuanlan.zhihu.com/p/165225746 Accessed 2023/2/18.
(8) ONNX models | Microsoft Learn. https://learn.microsoft.com/en-us/windows/ai/windows-ml/get-onnx-model Accessed 2023/2/18.
(9) What is ONNX? - AI@Edge Community. https://microsoft.github.io/ai-at-edge/docs/onnx/ Accessed 2023/2/18.


如何使用onnx-diff工具,比较onnx模型结构,请举例

要使用 onnx-diff 工具,你需要先安装它。你可以在 GitHub 上找到它的源代码和安装说明。

onnx-diff 的基本用法是:

onnx-diff model1.onnx model2.onnx

这会比较两个 ONNX 模型的结构和权重,并输出差异的报告。

例如,如果你有两个模型 model1.onnxmodel2.onnx,它们的结构如下:

model1.onnx
graph(%x : Float(3:4, 4:1, requires_grad=0, device=cpu)):
  %y : Float(3:4, 4:1, requires_grad=0, device=cpu) = onnx::Add(%x, %x) # /tmp/ipykernel_101/1910623878.py:5:0
  return (%y)

model2.onnx
graph(%x : Float(3:4, 4:1, requires_grad=0, device=cpu)):
  %y : Float(3:4, 4:1, requires_grad=0, device=cpu) = onnx::Mul(%x, %x) # /tmp/ipykernel_101/1910623878.py:5:0
  return (%y)

那么运行 onnx-diff model1.onnx model2.onnx 后,你会得到类似这样的输出:

--- a/model1.onnx
+++ b/model2.onnx
@@ -6 +6 @@
-    op_type = "Add"
+    op_type = "Mul"

这表示两个模型在第六行有不同,一个是用 Add 操作,一个是用 Mul 操作。

你还可以使用一些选项来控制 onnx-diff 的行为,例如:

  • -v--verbose:显示更多细节。
  • -w--weights-only:只比较权重,不比较结构。
  • -t THRESHOLD--threshold THRESHOLD:设置权重差异的阈值。默认为 0.00001。

你可以使用 -h--help 查看所有可用的选项。

Source: Conversation with Bing, 2023/2/18(1) JbPasquier/stable-diffusion-onnx-ui - GitHub. https://github.com/JbPasquier/stable-diffusion-onnx-ui Accessed 2023/2/18.
(2) GitHub - HighCWu/DiffSinger-ONNX-Infer: DiffSinger ONNX Inference. https://github.com/HighCWu/DiffSinger-ONNX-Infer Accessed 2023/2/18.
(3) onnx/Operators.md at main · onnx/onnx · GitHub. https://github.com/onnx/onnx/blob/main/docs/Operators.md Accessed 2023/2/18.
(4) ONNX Concepts — ONNX 1.14.0 documentation. https://onnx.ai/onnx/intro/concepts.html Accessed 2023/2/18.
(5) ONNX Runtime Inference Examples - GitHub. https://github.com/microsoft/onnxruntime-inference-examples Accessed 2023/2/18.
(6) microsoft/onnxruntime-training-examples - GitHub. https://github.com/microsoft/onnxruntime-training-examples Accessed 2023/2/18.
(7) Gallery of examples — sklearn-onnx 1.11 documentation. https://onnx.ai/sklearn-onnx/auto_examples/index.html Accessed 2023/2/18.


如何使用pip安装onnx-diff

要使用 pip 安装 onnx-diff,你可以在终端中输入以下命令:

pip install onnx-diff

这会从 PyPI 下载并安装 onnx-diff 的最新版本⁵。

如果你遇到任何问题,你可以在 GitHub 上查看 onnx-diff 的源代码和文档。

Source: Conversation with Bing, 2023/2/18(1) onnx-diff · PyPI. https://pypi.org/project/onnx-diff/ Accessed 2023/2/18.
(2) GitHub - HighCWu/DiffSinger-ONNX-Infer: DiffSinger ONNX Inference. https://bing.com/search?q=pip+install+onnx-diff Accessed 2023/2/18.
(3) onnx · PyPI. https://pypi.org/project/onnx/ Accessed 2023/2/18.
(4) GitHub - onnx/onnx: Open standard for machine learning interoperability. https://github.com/onnx/onnx Accessed 2023/2/18.
(5) GitHub - HighCWu/DiffSinger-ONNX-Infer: DiffSinger ONNX Inference. https://github.com/HighCWu/DiffSinger-ONNX-Infer Accessed 2023/2/18.


使用python如何调用onnx-diff

要使用 python 调用 onnx-diff,你可以在 python 代码中导入 onnx-diff 模块,并使用它的函数来比较两个 ONNX 模型。

例如,你可以写一个类似这样的代码:

import onnx
import onnx_diff

model1 = onnx.load("model1.onnx")
model2 = onnx.load("model2.onnx")

diff_report = onnx_diff.diff(model1, model2)

print(diff_report)

这会输出两个模型的差异报告,类似于命令行工具的输出。

你也可以使用其他参数来控制比较的方式,例如:

  • ignore_weights:是否忽略权重的比较。
  • threshold:设置权重差异的阈值。
  • verbose:是否显示更多细节。

你可以在 GitHub 上查看 onnx-diff 的源代码和文档⁵,或者在 PyPI 上查看它的版本信息⁵。

Source: Conversation with Bing, 2023/2/18(1) onnx-diff · PyPI. https://pypi.org/project/onnx-diff/ Accessed 2023/2/18.
(2) numpy.diff — NumPy v1.24 Manual. https://numpy.org/doc/stable/reference/generated/numpy.diff.html Accessed 2023/2/18.
(3) Python | onnxruntime. https://onnxruntime.ai/docs/get-started/with-python.html Accessed 2023/2/18.
(4) onnxruntime-directml · PyPI. https://pypi.org/project/onnxruntime-directml/ Accessed 2023/2/18.
(5) ONNX with Python — ONNX 1.14.0 documentation. https://onnx.ai/onnx/intro/python.html Accessed 2023/2/18.
(6) GitHub - leimao/ONNX-Python-Examples: ONNX Python Examples. https://github.com/leimao/ONNX-Python-Examples Accessed 2023/2/18.
(7) numpy.diff — NumPy v1.24 Manual. https://numpy.org/doc/stable/reference/generated/numpy.diff.html Accessed 2023/2/18.
(8) Python NumPy Diff With Examples - Python Guides. https://pythonguides.com/python-numpy-diff/ Accessed 2023/2/18.
(9) ONNX with Python — ONNX 1.14.0 documentation. https://onnx.ai/onnx/intro/python.html Accessed 2023/2/18.
(10) Gallery of examples — sklearn-onnx 1.11 documentation. https://onnx.ai/sklearn-onnx/auto_examples/index.html Accessed 2023/2/18.
(11) Python | onnxruntime. https://onnxruntime.ai/docs/get-started/with-python.html Accessed 2023/2/18.


如何使用OnnxDiff,比较onnx请举例

你好,这是必应。我猜你想知道如何使用 OnnxDiff 类比较两个 ONNX 模型。根据网上搜索的结果¹²³⁴,我尝试给你一个简单的例子:

  • 假设你有两个 ONNX 模型,model1.onnx 和 model2.onnx,它们都是类似的深度学习模型,但是最后一层针对不同的数据集进行了优化或重新训练。
  • 你可以使用 onnx_diff 包中的 OnnxDiff 类来创建一个对象,用来比较这两个模型的结构和参数。例如,你可以这样写:
from onnx_diff.diff import OnnxDiff
diff = OnnxDiff("model1.onnx", "model2.onnx")
  • 然后,你可以使用 diff 对象的各种方法来查看模型之间的差异。例如,你可以这样写:
# 查看模型之间有哪些不同的节点
print(diff.diff_nodes())

# 查看模型之间有哪些不同的参数
print(diff.diff_params())

# 查看模型之间有哪些不同的属性
print(diff.diff_attrs())
  • 这些方法会返回一个字典,其中键是节点或参数或属性的名称,值是一个元组,表示两个模型中对应的值。如果值相同,则元组中只有一个值。如果值不同,则元组中有两个值,分别表示两个模型中的值。

我希望这能帮助你使用 OnnxDiff 类比较两个 ONNX 模型。祝你好运!😊

Source: Conversation with Bing, 2023/2/18(1) Inference Comparison Between ONNX and Imported Networks for … - MathWorks. https://www.mathworks.com/help/deeplearning/ug/inference-comparison-between-onnx-and-imported-network-for-image-classification.html Accessed 2023/2/18.
(2) Tutorial: Detect objects using an ONNX deep learning model - ML.NET. https://learn.microsoft.com/en-us/dotnet/machine-learning/tutorials/object-detection-onnx Accessed 2023/2/18.
(3) nabarunbaruaAIML/onnx_model_size_compression - GitHub. https://github.com/nabarunbaruaAIML/onnx_model_size_compression Accessed 2023/2/18.
(4) How to merge two ONNX deep learning models - Stack Overflow. https://stackoverflow.com/questions/63305200/how-to-merge-two-onnx-deep-learning-models Accessed 2023/2/18.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值