当前位置: 首页 > news >正文

Python onnx 模型打印显示所有节点及查看相互关系

最近使用onnx时,想把所有的节点的信息和权重参数显示出来,找了一下没找到类似的源码,官方介绍的pythonAPI都是些什么加载,保存,转换之类的,没有详细介绍怎么使用onnx分析模型的,只好自己写一个。

其实很简单,我只列些最基本的,具体分析还得看个人的需要,

import onnx

model_in_file = 'yolov5s-sim.onnx'

if __name__ == "__main__":
    model = onnx.load(model_in_file)

    nodes = model.graph.node    
    nodnum = len(nodes) # 205

    for nid in range(nodnum):
        if (nodes[nid].output[0] == 'stride_32'):
            print('Found stride_32: index = ', nid)
        else:
            print(nodes[nid].output)

    inits = model.graph.initializer
    ininum = len(inits)  #124

    for iid in range(ininum):
        el = inits[iid]
        print('name:', el.name, ' dtype:', el.data_type, ' dim:', el.dims) 
        # el.raw_data for weights and biases

    print(model.graph.output) # display all the output nodes

print('Done')

比如,我这显示出来的模型中的节点是这样的,

[input: "data"
input: "model.0.conv.weight"
input: "model.0.conv.bias"
output: "122"
name: "Conv_0"
op_type: "Conv"
attribute {
  name: "dilations"
  ints: 1
  ints: 1
  type: INTS
}
attribute {
  name: "group"
  i: 1
  type: INT
}
attribute {
  name: "kernel_shape"
  ints: 6
  ints: 6
  type: INTS
}
attribute {
  name: "pads"
  ints: 2
  ints: 2
  ints: 2
  ints: 2
  type: INTS
}
attribute {
  name: "strides"
  ints: 2
  ints: 2
  type: INTS
}
, 
。。。。。。
, input: "325"
input: "model.24.m.2.weight"
input: "model.24.m.2.bias"
output: "376"
name: "Conv_234"
op_type: "Conv"
attribute {
  name: "dilations"
  ints: 1
  ints: 1
  type: INTS
}
attribute {
  name: "group"
  i: 1
  type: INT
}
attribute {
  name: "kernel_shape"
  ints: 1
  ints: 1
  type: INTS
}
attribute {
  name: "pads"
  ints: 0
  ints: 0
  ints: 0
  ints: 0
  type: INTS
}
attribute {
  name: "strides"
  ints: 1
  ints: 1
  type: INTS
}
, input: "376"
input: "398"
output: "399"
name: "Reshape_251"
op_type: "Reshape"
, input: "399"
output: "stride_32"
name: "Transpose_252"
op_type: "Transpose"
attribute {
  name: "perm"
  ints: 0
  ints: 1
  ints: 3
  ints: 4
  ints: 2
  type: INTS
}
]

可以看出,在onnx模型中,结点之间用逗号隔开,输出和输出都分别列出,比如我这里最后一个节点的信息是

>>> nodes[204]
input: "399"
output: "stride_32"
name: "Transpose_252"
op_type: "Transpose"
attribute {
  name: "perm"
  ints: 0
  ints: 1
  ints: 3
  ints: 4
  ints: 2
  type: INTS
}

>>> nodes[203]
input: "376"
input: "398"
output: "399"
name: "Reshape_251"
op_type: "Reshape"

其中node[204]input表示输入节点是399,也就是node[203]的输出;node[204]输出名称是stride_32,部署时就用这个名称来提取最终结果。像netron这样的工具,就是根据这些node之间的关系来绘制网络图的。

权重分析这里就不展开了。

相关文章:

  • LWIP 热插拔调试 -- 不同品牌路由器引起的问题
  • OpenCV的一个BUG: BMP读取后无法用imwrite成功保存PNG
  • 承接机器视觉项目到底应该选哪个解决方案?传统机器视觉 vs 人工智能
  • 安装OpenPCDet碰到的问题
  • Python3.8上安装vtk和mayavi踩坑: Building wheel for mayavi (setup.py) ...
  • ubuntu中如何显示或隐藏配置文件夹如.config
  • vscode没法选python解释器interpreter
  • 在windows10上安装运行ROS2
  • Ubuntu18.04 安装 ros2 foxy
  • VSCode常用操作
  • Ceres-Solver安装与简介
  • SLAM算法VINS-MONO安装运行介绍
  • Protobuf,gmock,gtest在windows10上的编译与安装
  • CMD闪退的问题及报错“点的大小应介于5和72之间”
  • 常用git 指令
  • @angular/forms 源码解析之双向绑定
  • 【剑指offer】让抽象问题具体化
  • ➹使用webpack配置多页面应用(MPA)
  • CSS居中完全指南——构建CSS居中决策树
  • JS正则表达式精简教程(JavaScript RegExp 对象)
  • JS专题之继承
  • MobX
  • php面试题 汇集2
  • Python语法速览与机器学习开发环境搭建
  • vue-cli3搭建项目
  • 等保2.0 | 几维安全发布等保检测、等保加固专版 加速企业等保合规
  • 分布式熔断降级平台aegis
  • 复杂数据处理
  • 算法---两个栈实现一个队列
  • 微信支付JSAPI,实测!终极方案
  • 哈罗单车融资几十亿元,蚂蚁金服与春华资本加持 ...
  • ​低代码平台的核心价值与优势
  • # Swust 12th acm 邀请赛# [ K ] 三角形判定 [题解]
  • $.type 怎么精确判断对象类型的 --(源码学习2)
  • (13)Latex:基于ΤΕΧ的自动排版系统——写论文必备
  • (4) openssl rsa/pkey(查看私钥、从私钥中提取公钥、查看公钥)
  • (C++)八皇后问题
  • (TipsTricks)用客户端模板精简JavaScript代码
  • (超简单)构建高可用网络应用:使用Nginx进行负载均衡与健康检查
  • (附源码)计算机毕业设计SSM在线影视购票系统
  • (九)信息融合方式简介
  • (转)setTimeout 和 setInterval 的区别
  • .htaccess 强制https 单独排除某个目录
  • .Mobi域名介绍
  • .NET CLR基本术语
  • .net framework profiles /.net framework 配置
  • .NET 除了用 Task 之外,如何自己写一个可以 await 的对象?
  • .NET下的多线程编程—1-线程机制概述
  • @angular/cli项目构建--http(2)
  • [Android]Android P(9) WIFI学习笔记 - 扫描 (1)
  • [APIO2012] 派遣 dispatching
  • [BUG]vscode插件live server无法自动打开浏览器
  • [C#]DataTable常用操作总结【转】
  • [C#]使用PaddleInference图片旋转四种角度检测
  • [C++]AVL树怎么转