python简单解析打印onnx模型信息
当我们加载了一个
ONNX之后,我们获得的就是一个ModelProto,它包含了一些版本信息,生产者信息和一个GraphProto。在GraphProto里面又包含了四个repeated数组,它们分别是node(NodeProto类型),input(ValueInfoProto类型),output(ValueInfoProto类型)和initializer(TensorProto类型),其中node中存放了模型中所有的计算节点,input存放了模型的输入节点,output存放了模型中所有的输出节点,initializer存放了模型的所有权重参数。
这里用python,将onnx中包含的有用的信息打印出来,进行一个直观可视化。
整体流程:
- 解析graph input
- 解析graph output
- 解析graph initializer,模型的所有权重参数
- 解析graph node,打印op信息,输入输出,得到整个计算的graph
import onnx
import numpy as np
import logging
logging.basicConfig(level=logging.INFO)def onnx_datatype_to_npType(data_type):if data_type == 1:return np.float32else:raise TypeError("don't support data type")def parser_initializer(initializer):name = initializer.namelogging.info(f"initializer name: {name}")dims = initializer.dimsshape = [x for x in dims]logging.info(f"initializer with shape:{shape}")dtype = initializer.data_typelogging.info(f"initializer with type: {onnx_datatype_to_npType(dtype)} ")# print tenth bufferweights = np.frombuffer(initializer.raw_data, dtype=onnx_datatype_to_npType(dtype))logging.info(f"initializer first 10 wights:{weights[:10]}")def parser_tensor(tensor, use='normal'):name = tensor.namelogging.info(f"{use} tensor name: {name}")data_type = tensor.type.tensor_type.elem_typelogging.info(f"{use} tensor data type: {data_type}")dims = tensor.type.tensor_type.shape.dimshape = []for i, dim in enumerate(dims):shape.append(dim.dim_value)logging.info(f"{use} tensor with shape:{shape} ")def parser_node(node):def attri_value(attri):if attri.type == 1:return attri.ielif attri.type == 7:return list(attri.ints)name = node.namelogging.info(f"node name:{name}")opType = node.op_typelogging.info(f"node op type:{opType}")inputs = list(node.input)logging.info(f"node with {len(inputs)} inputs:{inputs}")outputs = list(node.output)logging.info(f"node with {len(outputs)} outputs:{outputs}")attributes = node.attributefor attri in attributes:name = attri.namevalue = attri_value(attri)logging.info(f"{name} with value:{value}")def parser_info(onnx_model):ir_version = onnx_model.ir_versionproducer_name = onnx_model.producer_nameproducer_version = onnx_model.producer_versionfor info in [ir_version, producer_name, producer_version]:logging.info("onnx model with info:{}".format(info))def parser_inputs(onnx_graph):inputs = onnx_graph.inputfor input in inputs:parser_tensor(input, 'input')def parser_outputs(onnx_graph):outputs = onnx_graph.outputfor output in outputs:parser_tensor(output, 'output')def parser_graph_initializers(onnx_graph):initializers = onnx_graph.initializerfor initializer in initializers:parser_initializer(initializer)def parser_graph_nodes(onnx_graph):nodes = onnx_graph.nodefor node in nodes:parser_node(node)t = 1def onnx_parser():model_path = 'D:/project/public/yolov5-5.0/yolov5s-sim.onnx'model = onnx.load(model_path)# 0.parser_info(model)graph = model.graph# 1.parser_inputs(graph)# 2. parser_outputs(graph)# 3.parser_graph_initializers(graph)# 4. parser_graph_nodes(graph)if __name__ == '__main__':onnx_parser()
INFO:root:onnx model with info:7
INFO:root:onnx model with info:pytorch
INFO:root:onnx model with info:1.10
INFO:root:input tensor name: images
INFO:root:input tensor data type: 1
INFO:root:input tensor with shape:[1, 3, 640, 640]
INFO:root:output tensor name: output
INFO:root:output tensor data type: 1
INFO:root:output tensor with shape:[1, 3, 80, 80, 85]
INFO:root:output tensor name: 405
INFO:root:output tensor data type: 1
INFO:root:output tensor with shape:[1, 3, 40, 40, 85]
INFO:root:output tensor name: 419
INFO:root:output tensor data type: 1
INFO:root:output tensor with shape:[1, 3, 20, 20, 85]
INFO:root:initializer name: model.0.conv.conv.weight
INFO:root:initializer with shape:[32, 12, 3, 3]
INFO:root:initializer with type: <class 'numpy.float32'>
INFO:root:initializer first 10 wights:[-0.2730072 -1.4410826 -1.187087 -0.31312177 -0.94754034 -0.7239634 0.4315643 2.0547783 1.5080036 0.04422583]
INFO:root:initializer name: model.0.conv.conv.bias
INFO:root:initializer with shape:[32]
INFO:root:initializer with type: <class 'numpy.float32'>
INFO:root:initializer first 10 wights:[ 1.4819448 -0.9827409 -0.7623507 -0.503065 -0.1677513 3.1156974 1.4849529 0.15412715 -0.4954783 2.8073668 ]
INFO:root:initializer name: model.1.conv.weight
INFO:root:initializer with shape:[64, 32, 3, 3]
INFO:root:initializer with type: <class 'numpy.float32'>
INFO:root:initializer first 10 wights:[-0.0130239 -0.00788062 0.0033419 -0.08140857 -0.1592819 -0.10095055 -0.01685373 -0.00378994 -0.01589627 0.01994706]
INFO:root:initializer name: model.1.conv.bias
INFO:root:initializer with shape:[64]
INFO:root:initializer with type: <class 'numpy.float32'>
INFO:root:initializer first 10 wights:[ 2.9810083 1.6583345 2.4536395 4.068509 -0.03429741 -0.2963567 1.7936802 0.32334638 2.3112206 0.9088864 ]
INFO:root:initializer name: model.2.cv1.conv.weight
INFO:root:initializer with shape:[32, 64, 1, 1]
INFO:root:initializer with type: <class 'numpy.float32'>
INFO:root:initializer first 10 wights:[ 0.00402472 -0.02254777 0.01670638 0.07000323 -0.01463853 -0.01208 -0.00047498 -0.01327164 0.02764146 0.03544556]
...
...
...
INFO:root:initializer name: model.23.cv2.conv.bias
INFO:root:initializer with shape:[256]
INFO:root:initializer with type: <class 'numpy.float32'>
INFO:root:initializer first 10 wights:[ 0.33130765 0.50842535 0.04622685 0.27884385 0.35381323 -0.05325952 0.18430158 0.16491716 -0.4574555 0.21860392]
INFO:root:initializer name: model.23.cv3.conv.weight
INFO:root:initializer with shape:[512, 512, 1, 1]
INFO:root:initializer with type: <class 'numpy.float32'>
INFO:root:initializer first 10 wights:[ 0.15170689 -0.05846716 -0.03708049 -0.00515915 0.04973555 -0.01793442 0.26971823 -0.08900855 -0.08623905 -0.01718434]
INFO:root:initializer name: model.23.cv3.conv.bias
INFO:root:initializer with shape:[512]
INFO:root:initializer with type: <class 'numpy.float32'>
INFO:root:initializer first 10 wights:[-0.07636432 -0.14711903 -0.1693097 1.2009852 0.00255883 1.4637253-0.29597348 2.088275 0.5806205 0.49393153]
INFO:root:initializer name: model.23.m.0.cv1.conv.weight
INFO:root:initializer with shape:[256, 256, 1, 1]
INFO:root:initializer with type: <class 'numpy.float32'>
INFO:root:initializer first 10 wights:[ 1.8370461e-01 -3.6310073e-02 2.6413003e-02 -3.4686580e-02-4.4441203e-04 3.3812389e-02 -3.7558913e-02 -9.6223257e-02-5.3294320e-02 -5.8845425e-01]
INFO:root:initializer name: model.23.m.0.cv1.conv.bias
INFO:root:initializer with shape:[256]
INFO:root:initializer with type: <class 'numpy.float32'>
INFO:root:initializer first 10 wights:[ 0.4050864 -0.03902826 0.31003702
INFO:root:initializer name: model.24.m.0.bias
INFO:root:initializer with shape:[255]
INFO:root:initializer with type: <class 'numpy.float32'>
INFO:root:initializer first 10 wights:[ 0.13708496 0.06561279 0.81152344 0.62353516 -4.1992188 -1.0546875 -5.2734375 -3.4414062 -5.5234375 -5.71875 ]
INFO:root:initializer name: model.24.m.1.weight
INFO:root:initializer with shape:[255, 256, 1, 1]
INFO:root:initializer with type: <class 'numpy.float32'>
INFO:root:initializer first 10 wights:[-0.00010455 -0.00139713 0.00134754 0.01174927 -0.00017703 -0.00750351-0.00029159 -0.00182247 -0.02702332 0.04980469]
INFO:root:initializer name: model.24.m.1.bias
INFO:root:initializer with shape:[255]
INFO:root:initializer with type: <class 'numpy.float32'>
INFO:root:initializer first 10 wights:[-0.05148315 -0.05493164 0.6333008 0.05197144 -2.625 -1.3398438 -5.7851562 -4.765625 -5.7929688 -6.2773438 ]
INFO:root:initializer name: model.24.m.2.weight
INFO:root:initializer with shape:[255, 512, 1, 1]
INFO:root:initializer with type: <class 'numpy.float32'>
INFO:root:initializer first 10 wights:[-2.5444031e-03 -1.1138916e-01 9.1195107e-06 1.0973215e-04-1.5907288e-03 -3.3130646e-03 2.6941299e-04 -9.5486641e-051.4615059e-04 -7.8857422e-02]
INFO:root:initializer name: model.24.m.2.bias
INFO:root:initializer with shape:[255]
INFO:root:initializer with type: <class 'numpy.float32'>
INFO:root:initializer first 10 wights:[-1.6265869e-02 -1.9702911e-03 -6.9091797e-02 1.9494629e-01-2.0878906e+00 -1.6210938e+00 -6.5625000e+00 -5.4531250e+00-6.3437500e+00 -6.7070312e+00]
INFO:root:initializer name: 420
INFO:root:initializer with shape:[4]
INFO:root:initializer with type: <class 'numpy.float32'>
INFO:root:initializer first 10 wights:[1. 1. 2. 2.]
相关文章:
python简单解析打印onnx模型信息
当我们加载了一个ONNX之后,我们获得的就是一个ModelProto,它包含了一些版本信息,生产者信息和一个GraphProto。在GraphProto里面又包含了四个repeated数组,它们分别是node(NodeProto类型),input(ValueInfoProto类型)&a…...
UE4 编写着色器以及各种宏的理解
参考链接:如何为 UE4 添加全局着色器(Global Shaders) - Unreal Enginehttps://docs.unrealengine.com/5.1/zh-CN/adding-global-shaders-to-unreal-engine/如何为 UE4 添加全局着色器(Global Shaders) - Unreal Engin…...
小笔记:Python 使用字符串调用函数
小笔记:Python中如何使用字符串调用函数/方法?jcLee95:https://blog.csdn.net/qq_28550263?spm1001.2101.3001.5343 本文地址:https://blog.csdn.net/qq_28550263/article/details/111874476 邮箱 :291148484163.co…...
红黑树的原理+实现
文章目录红黑树定义性质红黑树的插入动态效果演示代码测试红黑树红黑树 定义 红黑树是一个近似平衡的搜索树,关于近似平衡主要体现在最长路径小于最短路径的两倍(我认为这是红黑树核心原则),为了达到这个原则,红黑树所…...
用于非线性时间序列预测的稀疏局部线性和邻域嵌入(Matlab代码实现)
💥💥💞💞欢迎来到本博客❤️❤️💥💥 🏆博主优势:🌞🌞🌞博客内容尽量做到思维缜密,逻辑清晰,为了方便读者。 ⛳️座右铭&a…...
使用 Vue3 重构 Vue2 项目
目录前言:一、项目整体效果展示二、项目下载使用方法三、为什么要重构项目四、重构的流程五、步骤中的 bug 以及解决方式六、未解决的问题总结:前言: 2020年9月18日,vue3正式版发布了,前几天学习完成后,我决…...
Hive学习——单机版Hive的安装
目录 一、基本概念 (一)什么是Hive (二)优势和特点 (三)Hive元数据管理 二、Hive环境搭建 1.自动安装脚本 2./opt/soft/hive312/conf目录下创建hive配置文件hive-site.xml 3.拷贝一个jar包到hive下面的lib目录下 4.删除hive的guava,拷贝hadoop下的guava 5…...
uprobe 实战
观测数据源 目前按照我的理解,和trace相关的常用数据源–探针 大致分为四类。 内核 Trace point kprobe 用户程序 USDT uprobe 在用户程序中,USDT是所谓的静态Tracepoint。和内核代码中的Trace point类似。实现方式是在代码开发时,使用USDT…...
华为OD机试 - 求最大数字(Python)| 真题+思路+考点+代码+岗位
求最大数字 题目 给定一个由纯数字组成以字符串表示的数值,现要求字符串中的每个数字最多只能出现2次,超过的需要进行删除;删除某个重复的数字后,其它数字相对位置保持不变。 如34533,数字3重复超过2次,需要删除其中一个3,删除第一个3后获得最大数值4533 请返回经过删…...
雨水情测报与大坝安全监测系统
压电式雨量传感器产品概述传感器由上盖、外壳和下盖组成,壳体内部有压电片和电路板,可以固定在外径50mm立柱上和气象站横杆上。传感器采用冲击测量原理对单个雨滴重量进行测算,进而计算降雨量。雨滴在降落过程中受到雨滴重量和空气阻力的作用…...
抖音广告投放形式有哪些?新品牌进入抖音怎么建立口碑
坐拥5亿用户的抖音平台,已经成为各大品牌的兵家必争之地。想要在这块宣传的“高地”,做出声量,就必须了解抖音广告投放形式有哪些。这里整理的这份抖音广告投放指南,你一定不能错过。一、抖音为何如此牛想要弄清楚抖音广告的投放形…...
Beefxss使用教程图文教程(超详细)
「作者主页」:士别三日wyx 「作者简介」:CSDN top100、阿里云博客专家、华为云享专家、网络安全领域优质创作者 Beefxss一、首次使用二、修改账号密码三、自带练习页面四、简单使用五、工具界面介绍六、功能演示1、网页重定向2、社工弹窗3、功能颜色标识…...
【Python学习笔记】35.Python3 CGI编程(2)
前言 本章继续介绍Python的CGI编程。 通过CGI程序传递checkbox数据 checkbox用于提交一个或者多个选项数据,HTML代码如下: 实例 <!DOCTYPE html> <html> <head> <meta charset"utf-8"> <title>csdn教程(csd…...
博客等级说明
CSDN 博客等级是按照用户的博客积分数量进行的设定,为 Lv1 至 Lv10 共 10 个等级,不同的等级创作者可以享受到不同的权益待遇。例如,皮肤奖励、自定义域名、客服优先处理、自定义文章标签等特权。您需要提高博客积分进一步提升等级࿰…...
STL——容器适配器、deque
一、容器适配器 1.适配器 适配器是一种设计模式(设计模式是一套被反复使用的、多数人所知晓的、经过分类编目的、代码设计经验的总结),该种模式是将一个类的接口转换成客户希望的另外一个接口。 2.STL标准库中stack和queue的底层结构 stack…...
VBA数组和Excel工作表数据传递
本文介绍如何利用 VBA 的数组(Array) 来提高 Excel 单元格和外部数据传输的性能。如果数量比较大,通过 Array 来传输数据比直接操作单元格要快若干倍。 将 Range 的数据写入 VBA Array 将 Range 数据写入 VBA 的数组非常简单。下面的例子演示了用法&am…...
PyQt5保姆级入门教程——从安装到使用
目录 Part1:安装PyQt5 Part 2:PyCharm配置PyQt5 Part 3:PyQt5设计界面介绍 Part 4:PyQt5设计UI 今天看了多个大佬的教程,总算是把PyQt5开发弄好了,每个部分都要看几个人的十分不方便,我十分…...
1.6 epoll实战使用
文章目录1、连接池2、epoll两种工作模式2.1、LT模式2.2、ET模式3、后端开发面试题4、epoll验证1、连接池 将每一个套接字和一块内存进行绑定,连接池就是一个结构体数组,通过链表来维护一个空闲连接。 1、ngx_get_connection(int fd)从空闲链表取一个空闲…...
JDK定时、Spring定时、时间轮定时小结
Timer使用一个线程,一个小根堆。线程执行根上的任务,小根堆会根据执行时间戳重新调整,根上的任务是下一个执行的任务。 DelayedQueue维护一个优先级队列,本质也是一个数组方式的堆。任务生成时也有时间戳,只提供存储。…...
关于cFosSpeed如何配置
cFosSpeed配置一、检查Calibration Done情况二、优化Ping时间和线路校准三、测网速四、cFosSpeed控制台五、配置参数一、检查Calibration Done情况 安装完毕,激活成功后。 右键------>选项------>设置, 打开适配器信息,查看Calibra…...
龙虎榜——20250610
上证指数放量收阴线,个股多数下跌,盘中受消息影响大幅波动。 深证指数放量收阴线形成顶分型,指数短线有调整的需求,大概需要一两天。 2025年6月10日龙虎榜行业方向分析 1. 金融科技 代表标的:御银股份、雄帝科技 驱动…...
边缘计算医疗风险自查APP开发方案
核心目标:在便携设备(智能手表/家用检测仪)部署轻量化疾病预测模型,实现低延迟、隐私安全的实时健康风险评估。 一、技术架构设计 #mermaid-svg-iuNaeeLK2YoFKfao {font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}#mermaid-svg…...
DAY 47
三、通道注意力 3.1 通道注意力的定义 # 新增:通道注意力模块(SE模块) class ChannelAttention(nn.Module):"""通道注意力模块(Squeeze-and-Excitation)"""def __init__(self, in_channels, reduction_rat…...
macOS多出来了:Google云端硬盘、YouTube、表格、幻灯片、Gmail、Google文档等应用
文章目录 问题现象问题原因解决办法 问题现象 macOS启动台(Launchpad)多出来了:Google云端硬盘、YouTube、表格、幻灯片、Gmail、Google文档等应用。 问题原因 很明显,都是Google家的办公全家桶。这些应用并不是通过独立安装的…...
Nuxt.js 中的路由配置详解
Nuxt.js 通过其内置的路由系统简化了应用的路由配置,使得开发者可以轻松地管理页面导航和 URL 结构。路由配置主要涉及页面组件的组织、动态路由的设置以及路由元信息的配置。 自动路由生成 Nuxt.js 会根据 pages 目录下的文件结构自动生成路由配置。每个文件都会对…...
【Web 进阶篇】优雅的接口设计:统一响应、全局异常处理与参数校验
系列回顾: 在上一篇中,我们成功地为应用集成了数据库,并使用 Spring Data JPA 实现了基本的 CRUD API。我们的应用现在能“记忆”数据了!但是,如果你仔细审视那些 API,会发现它们还很“粗糙”:有…...
如何在最短时间内提升打ctf(web)的水平?
刚刚刷完2遍 bugku 的 web 题,前来答题。 每个人对刷题理解是不同,有的人是看了writeup就等于刷了,有的人是收藏了writeup就等于刷了,有的人是跟着writeup做了一遍就等于刷了,还有的人是独立思考做了一遍就等于刷了。…...
AI书签管理工具开发全记录(十九):嵌入资源处理
1.前言 📝 在上一篇文章中,我们完成了书签的导入导出功能。本篇文章我们研究如何处理嵌入资源,方便后续将资源打包到一个可执行文件中。 2.embed介绍 🎯 Go 1.16 引入了革命性的 embed 包,彻底改变了静态资源管理的…...
SAP学习笔记 - 开发26 - 前端Fiori开发 OData V2 和 V4 的差异 (Deepseek整理)
上一章用到了V2 的概念,其实 Fiori当中还有 V4,咱们这一章来总结一下 V2 和 V4。 SAP学习笔记 - 开发25 - 前端Fiori开发 Remote OData Service(使用远端Odata服务),代理中间件(ui5-middleware-simpleproxy)-CSDN博客…...
莫兰迪高级灰总结计划简约商务通用PPT模版
莫兰迪高级灰总结计划简约商务通用PPT模版,莫兰迪调色板清新简约工作汇报PPT模版,莫兰迪时尚风极简设计PPT模版,大学生毕业论文答辩PPT模版,莫兰迪配色总结计划简约商务通用PPT模版,莫兰迪商务汇报PPT模版,…...
