当前位置: 首页 > 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 torchview
    https://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.Graph
    model_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 file
    https://pytorch.org/docs/stable/onnx.html
    '''
    is_training = False

    if model.training:
        # view the network graph in onnx format
        # https://pytorch.org/docs/stable/onnx.html
        model.eval()
        is_training = True

    torch.onnx.export(
        model,        # model to export
        (input_sample,),      # inputs of the model,
        filepath,        # filename of the ONNX model
        input_names=["input"],  # Rename inputs for the ONNX model
        dynamo=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

生成 FCNN,AlexNet, LeNet 论文中,手绘风格的神经网络图片。可以手动设置参数。

使用

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 ↩︎

相关文章:

  • 漏洞预警 | Windows 文件资源管理器欺骗漏洞(CVE-2025-24071、CVE-2025-24054)
  • 《SRv6 网络编程:开启IP网络新时代》第2章、第3章:SRv6基本原理和基础协议
  • mybatis里in关键字拼接id问题
  • Java 时间处理:轻松掌握 java.time 包
  • 05-031-自考数据结构(20331)- 哈希表 - 例题分析
  • UE5学习记录part12
  • WebForms 事件
  • stm32第十天外部中断和NVIC讲解
  • 力扣刷题——排序链表
  • Lua语言脚本环境配置
  • 【uboot1】启动流程,环境变量,实例
  • Ubuntu上给AndroidStudio创建桌面图标
  • API 安全之认证鉴权
  • C++的指针数组、数组指针和指针数组指针
  • 怎么安装JSON服务器?JSON服务器最新安装教程
  • JavaScript中的模块化
  • GitHub Flow与GitLab Flow的详细
  • python求解常微分方程之Galerkin method:权函数
  • 【日期问题(判断星期几)】
  • 有哪些好用的项目管理工具推荐?并且支持AI定制和私有部署的?
  • 什么网站做家具外贸好呢/免费的个人网页
  • wap网站设计方案/真实的优化排名
  • 利用小说网站做本站优化/seo排名赚app靠谱吗
  • 寺庙招人做网站维护吗/搭建网站的步骤和顺序
  • 安徽网站建设费用/东莞seo优化
  • 视频网站分享复制通用代码怎么做/广州网站到首页排名