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

将 PyTorch Model 用可视化方法浏览 torchview,onxx, netron, summary | 撰写论文 paper

将 PyTorch Model 用可视化方法浏览

    • torchview
      • 文档
      • 代码
      • 图像示例
      • 特点
    • onnx, netron
      • 导出 model 的 onnx 文件
      • 示例
      • 特点
    • torchinfo summary
      • 图片示例
      • 特点
    • nn-svg
      • 使用
      • 开源项目
      • 特点

使用 PyTorch 构建的 Model,想要查看网络的图形,那么有以下方法,最终可视化效果最好的是:torchview; 对于优秀论文中的神经网络图则主要是通过 PPT 等更自由的工具手绘的1

torchview

文档

https://mert-kurttutan.github.io/torchview/tutorial/example_introduction/

需要安装:

  • graphviz, 并保证 dot.exe 在 PATH 路径
  • torchview

代码

def export_model_graph(model, input_sample, directory, filename = "model_graph", format = "svg", scale=5.0):'''Export model as image with torchviewhttps://mert-kurttutan.github.io/torchview/reference/torchview/#torchview.torchview.draw_graph'''model_graph = draw_graph(model, input_size=input_sample.shape, expand_nested=True, directory=directory)model_graph.visual_graph.node_attr["fontname"] = "Helvetica"model_graph.resize_graph(scale=scale) # scale as per the view# https://graphviz.readthedocs.io/en/stable/api.html#graphviz.Graphmodel_graph.visual_graph.render(filename=filename, directory=directory, format=format)filepath = os.path.join(directory, "%s.%s" % (filename, format))if not os.path.exists(filepath):raise BaseException("Error on export torchview graph %s" % filepath)return filepath

图像示例

在这里插入图片描述

特点

清晰直观,信息丰富。

onnx, netron

https://onnx.ai/
https://github.com/lutzroeder/netron/tree/

导出 model 的 onnx 文件

def export_onnx_archive(model, filepath, input_sample):'''Export model as onnx format filehttps://pytorch.org/docs/stable/onnx.html'''is_training = Falseif model.training:# view the network graph in onnx format# https://pytorch.org/docs/stable/onnx.htmlmodel.eval()is_training = Truetorch.onnx.export(model,        # model to export(input_sample,),      # inputs of the model,filepath,        # filename of the ONNX modelinput_names=["input"],  # Rename inputs for the ONNX modeldynamo=True             # True or False to select the exporter to use)if not os.path.exists(filepath):raise BaseException("File %s not found" % filepath)if is_training:model.train()

示例

在这里插入图片描述

特点

识别更多模型的信息。

缺点:

  • 需要额外导出 onnx model
  • 并没有 torchview 直观

torchinfo summary

from torchinfo import summary
logger.info(summary(resnet_model_50, verbose=0))

图片示例

在这里插入图片描述

特点

是文本的形式,简洁依赖少。

缺点:

  • 表现力不如 torchview

nn-svg

使用

https://www.chatopera.com/files/nn-graph/index.html
在这里插入图片描述

开源项目

https://github.com/alexlenail/NN-SVG

特点

  • 生成和论文中类似的风格的图片

  1. https://datascience.stackexchange.com/questions/66343/drawing-neural-network-diagram-for-academic-papers ↩︎


http://www.mrgr.cn/news/96578.html

相关文章:

  • PDF解析黑科技:从OCR-Free到多模态大模型的进化之旅
  • DeepSeek 助力 Vue3 开发:打造丝滑的表格(Table)之添加行拖拽排序功能示例14,TableView16_14 拖拽自动保存示例
  • 《异常检测——从经典算法到深度学习》30. 在线服务系统中重复故障的可操作和可解释的故障定位
  • 基于PX4和Ardupilot固件下自定义MAVLink消息测试(QGroundControl和Mission Planner)
  • SQL注入之盲注技术详解
  • DataPlatter:利用最少成本数据提升机器人操控的泛化能力
  • 大模型时代的基础架构 读书笔记
  • Android设计模式之代理模式
  • 项目上传github——SSH连接配置文档
  • 【MySQL】从零开始:掌握MySQL数据库的核心概念(四)
  • 【MySQL】从零开始:掌握MySQL数据库的核心概念(五)
  • Transformer-BiLSTM、Transformer、CNN-BiLSTM、BiLSTM、CNN五模型多变量回归预测
  • 车载以太网网络测试-25【SOME/IP-报文格式-1】
  • Cocos Creator Shader入门实战(七):RGB不同算法效果的实现,及渲染技术、宏定义、属性参数的延伸配置
  • AIGC1——AIGC技术原理与模型演进:从GAN到多模态融合的突破
  • 01-Docker 安装
  • 五.ubuntu20.04 - ffmpeg推拉流以及Nginx、SRS本地部署
  • YOLOv11模型的常见处理
  • 我的机器学习学习之路
  • 【TensorRT】TensorRT从安装到推理——Python 环境下 MobileNetV4 三分类任务