• pytorch使用tensorboardX面板自动生成模型结构图和各类可视化图像


    总结:

    在原本代码中额外添加如下几行即可实现查看模型结构:

    1. from tensorboardX import SummaryWriter # 用于进行可视化
    2. # 1. 来用tensorflow进行可视化
    3. with SummaryWriter("./log", comment="sample_model_visualization") as sw:
    4. sw.add_graph(modelviz, sampledata)

    操作步骤如下

    安装完torch之后,再安装tensorboardX

    pip install tensorboardX -i https://pypi.tuna.tsinghua.edu.cn/simple

    运行下面代码 

    1. import torch
    2. import torch.nn as nn
    3. import torch.nn.functional as F
    4. from tensorboardX import SummaryWriter # 用于进行可视化
    5. class modelViz(nn.Module):
    6. def __init__(self):
    7. super(modelViz, self).__init__()
    8. self.conv1 = nn.Conv2d(3, 16, 3, 1, padding=1)
    9. self.bn1 = nn.BatchNorm2d(16)
    10. self.conv2 = nn.Conv2d(16, 64, 3, 1, padding=1)
    11. self.bn2 = nn.BatchNorm2d(64)
    12. self.conv3 = nn.Conv2d(64, 10, 3, 1, padding=1)
    13. self.bn3 = nn.BatchNorm2d(10)
    14. def forward(self, x):
    15. x = self.bn1(self.conv1(x))
    16. x = F.relu(x)
    17. x = self.bn2(self.conv2(x))
    18. x = F.relu(x)
    19. x = self.bn3(self.conv3(x))
    20. x = F.relu(x)
    21. return x
    22. if __name__ == "__main__":
    23. # 首先来搭建一个模型
    24. modelviz = modelViz()
    25. # 创建输入
    26. sampledata = torch.rand(1, 3, 4, 4)
    27. # 看看输出结果对不对
    28. out = modelviz(sampledata)
    29. print(out) # 测试有输出,网络没有问题
    30. # 1. 来用tensorflow进行可视化
    31. with SummaryWriter("./log", comment="sample_model_visualization") as sw:
    32. sw.add_graph(modelviz, sampledata)
    33. # # 2. 保存成pt文件后进行可视化
    34. # torch.save(modelviz, "./log/modelviz.pt")

    运行代码后会在"./log"路径下生成一个tfevents文件,在终端中进入代码的主目录下执行命令

    tensorboard --logdir=./ 

     然后会输出

    1. (base) jie@dell:~/桌面/fno_task$ tensorboard --logdir=./
    2. TensorFlow installation not found - running with reduced feature set.
    3. NOTE: Using experimental fast data loading logic. To disable, pass
    4. "--load_fast=false" and report issues on GitHub. More details:
    5. https://github.com/tensorflow/tensorboard/issues/4784
    6. Serving TensorBoard on localhost; to expose to the network, use a proxy or pass --bind_all
    7. TensorBoard 2.16.2 at http://localhost:6006/ (Press CTRL+C to quit)

     http://localhost:6006/

    然后按照提示打开浏览器,输入上面这个网址就可以看到我们搭建的网络结构了,如下图所示,可以双击打开每一个节点查看其内容。也可以查看详细的结构以及每一层的输入输出shape。通过双击模型的组件实现展示网络细节和收起细节。

    结束!!!

    官网详细和介绍使用链接:https://www.tensorflow.org/tensorboard/graphs?hl=zh-cn

    tips:tensorboard是适用于tensorflow,而tensorboardX可以适用pytorch

    tips: 如果你在虚拟环境cd到log的上一级文件夹,那么按照上面的路径就得不到你想要的可视化结果,路径不正确,应该输入

    tensorboard --logdir=./log/
                            
    参考链接:https://blog.csdn.net/Vertira/article/details/127326470  

  • 相关阅读:
    python常见的面试题,看你都掌握了吗
    CSS - 弹性布局(flex)
    ASPICE标准快速掌握「5.1. ASPICE与ISO/IEC 33004」
    Gin框架源码解析
    利用frps搭建本地自签名https服务的透传
    【周周Python百日刷题计划】Day5~内置函数和运算符的使用
    R语言时间序列数据提取:使用xts包的last函数提取时间序列中最后面一个月的数据(last 1 month)
    程序人生 / 散文分享 / 生活感悟——【追光的日子】《爷爷的12本日历》,若你也共情,欢迎在评论区分享你的故事、观点、感悟和思考!
    [SpringBoot] 自定义spring-boot-starter
    86579-06-8,十七肽KAERADLIAYLKQATAK
  • 原文地址:https://blog.csdn.net/weixin_44162814/article/details/139317548