介绍pytorch-summary安装与使用

https://github.com/sksq96/pytorch-summary

pytorch-summary的GitHub链接如上,可以通过下面两种方式安装,由于本人网络问题,建议通过第二种安装方式。即先去GitHub将源码下载解压后,cd到文件中,python setup.py Install (注意安装前先选择你要安装的conda环境),下面贴上原网站的使用教程。

pip install torchsummary
git clone https://github.com/sksq96/pytorch-summary

Use the new and updated torchinfo.

Keras style model.summary() in PyTorch

Keras has a neat API to view the visualization of the model which is very helpful while debugging your network. Here is a barebone code to try and mimic the same in PyTorch. The aim is to provide information complementary to, what is not provided by print(your_model) in PyTorch.

Usage

  • pip install torchsummary or
  • git clone https://github.com/sksq96/pytorch-summary
from torchsummary import summary
summary(your_model, input_size=(channels, H, W))
  • Note that the input_size is required to make a forward pass through the network.

Examples

CNN for MNIST

import torch
import torch.nn as nn
import torch.nn.functional as F
from torchsummary import summaryclass Net(nn.Module):def __init__(self):super(Net, self).__init__()self.conv1 = nn.Conv2d(1, 10, kernel_size=5)self.conv2 = nn.Conv2d(10, 20, kernel_size=5)self.conv2_drop = nn.Dropout2d()self.fc1 = nn.Linear(320, 50)self.fc2 = nn.Linear(50, 10)def forward(self, x):x = F.relu(F.max_pool2d(self.conv1(x), 2))x = F.relu(F.max_pool2d(self.conv2_drop(self.conv2(x)), 2))x = x.view(-1, 320)x = F.relu(self.fc1(x))x = F.dropout(x, training=self.training)x = self.fc2(x)return F.log_softmax(x, dim=1)device = torch.device("cuda" if torch.cuda.is_available() else "cpu") # PyTorch v0.4.0
model = Net().to(device)summary(model, (1, 28, 28))
----------------------------------------------------------------Layer (type)               Output Shape         Param #
================================================================Conv2d-1           [-1, 10, 24, 24]             260Conv2d-2             [-1, 20, 8, 8]           5,020Dropout2d-3             [-1, 20, 8, 8]               0Linear-4                   [-1, 50]          16,050Linear-5                   [-1, 10]             510
================================================================
Total params: 21,840
Trainable params: 21,840
Non-trainable params: 0
----------------------------------------------------------------
Input size (MB): 0.00
Forward/backward pass size (MB): 0.06
Params size (MB): 0.08
Estimated Total Size (MB): 0.15
----------------------------------------------------------------

VGG16

import torch
from torchvision import models
from torchsummary import summarydevice = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
vgg = models.vgg16().to(device)summary(vgg, (3, 224, 224))
----------------------------------------------------------------Layer (type)               Output Shape         Param #
================================================================Conv2d-1         [-1, 64, 224, 224]           1,792ReLU-2         [-1, 64, 224, 224]               0Conv2d-3         [-1, 64, 224, 224]          36,928ReLU-4         [-1, 64, 224, 224]               0MaxPool2d-5         [-1, 64, 112, 112]               0Conv2d-6        [-1, 128, 112, 112]          73,856ReLU-7        [-1, 128, 112, 112]               0Conv2d-8        [-1, 128, 112, 112]         147,584ReLU-9        [-1, 128, 112, 112]               0MaxPool2d-10          [-1, 128, 56, 56]               0Conv2d-11          [-1, 256, 56, 56]         295,168ReLU-12          [-1, 256, 56, 56]               0Conv2d-13          [-1, 256, 56, 56]         590,080ReLU-14          [-1, 256, 56, 56]               0Conv2d-15          [-1, 256, 56, 56]         590,080ReLU-16          [-1, 256, 56, 56]               0MaxPool2d-17          [-1, 256, 28, 28]               0Conv2d-18          [-1, 512, 28, 28]       1,180,160ReLU-19          [-1, 512, 28, 28]               0Conv2d-20          [-1, 512, 28, 28]       2,359,808ReLU-21          [-1, 512, 28, 28]               0Conv2d-22          [-1, 512, 28, 28]       2,359,808ReLU-23          [-1, 512, 28, 28]               0MaxPool2d-24          [-1, 512, 14, 14]               0Conv2d-25          [-1, 512, 14, 14]       2,359,808ReLU-26          [-1, 512, 14, 14]               0Conv2d-27          [-1, 512, 14, 14]       2,359,808ReLU-28          [-1, 512, 14, 14]               0Conv2d-29          [-1, 512, 14, 14]       2,359,808ReLU-30          [-1, 512, 14, 14]               0MaxPool2d-31            [-1, 512, 7, 7]               0Linear-32                 [-1, 4096]     102,764,544ReLU-33                 [-1, 4096]               0Dropout-34                 [-1, 4096]               0Linear-35                 [-1, 4096]      16,781,312ReLU-36                 [-1, 4096]               0Dropout-37                 [-1, 4096]               0Linear-38                 [-1, 1000]       4,097,000
================================================================
Total params: 138,357,544
Trainable params: 138,357,544
Non-trainable params: 0
----------------------------------------------------------------
Input size (MB): 0.57
Forward/backward pass size (MB): 218.59
Params size (MB): 527.79
Estimated Total Size (MB): 746.96
----------------------------------------------------------------

Multiple Inputs

import torch
import torch.nn as nn
from torchsummary import summaryclass SimpleConv(nn.Module):def __init__(self):super(SimpleConv, self).__init__()self.features = nn.Sequential(nn.Conv2d(1, 1, kernel_size=3, stride=1, padding=1),nn.ReLU(),)def forward(self, x, y):x1 = self.features(x)x2 = self.features(y)return x1, x2device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model = SimpleConv().to(device)summary(model, [(1, 16, 16), (1, 28, 28)])
----------------------------------------------------------------Layer (type)               Output Shape         Param #
================================================================Conv2d-1            [-1, 1, 16, 16]              10ReLU-2            [-1, 1, 16, 16]               0Conv2d-3            [-1, 1, 28, 28]              10ReLU-4            [-1, 1, 28, 28]               0
================================================================
Total params: 20
Trainable params: 20
Non-trainable params: 0
----------------------------------------------------------------
Input size (MB): 0.77
Forward/backward pass size (MB): 0.02
Params size (MB): 0.00
Estimated Total Size (MB): 0.78
----------------------------------------------------------------

References

  • The idea for this package sparked from this PyTorch issue.
  • Thanks to @ncullen93 and @HTLife.
  • For Model Size Estimation @jacobkimmel (details here)

License

pytorch-summary is MIT-licensed.

在Pytorch中显示网络每层的输出特征图大小、参数、显存占用、参数量的工具---pytorch-summary相关推荐

  1. ubuntu中显示本机的gpu_Ubuntu下实时查看Nvidia显卡显存占用情况和GPU温度

    一.查看Nvidia显卡显存占用情况 查看Nvidia显卡显存占用情况 nvidia-smi 效果如下: 显示的表格中: Fan: 风扇转速(0%–100%),N/A表示没有风扇 Temp: GPU温 ...

  2. pytorch中使用TensorBoard进行可视化Loss及特征图

    pytorch中使用TensorBoard进行可视化Loss及特征图 安装导入TensorBoard 安装TensorBoard pip install tensorboard 导入TensorBoa ...

  3. 后向重计算在OneFlow中的实现:以时间换空间,大幅降低显存占用

    撰文 | 赵露阳 2016年,陈天奇团队提出了亚线性内存优化相关的"gradient/activation checkpointing(后向重计算)"等技术[1],旨在降低深度学习 ...

  4. 大幅减少GPU显存占用:可逆残差网络(The Reversible Residual Network)

    点击我爱计算机视觉标星,更快获取CVML新技术 本文经授权转载自AINLP. 作者:光彩照人 学校:北京邮电大学 研究方向:自然语言处理,精准营销,风险控制 前序: Google AI最新出品的论文R ...

  5. pytorch 关于显存增长原因以及显存占用优化

    ps:师傅希望想tensorflow一样设置 GPU占用(tf.GPUOptions),但事实情况是好像pytorch根本就没有相关函数来设置(如8G显存我只用2G,来跑是否可行) 1.网上很多攻略关 ...

  6. pytorch 优化GPU显存占用,避免out of memory

    pytorch 优化GPU显存占用,避免out of memory 分享一个最实用的招: 用完把tensor删掉,pytorch不会自动清理显存! 代码举例,最后多删除一个,gpu显存占用就会下降,训 ...

  7. 卷积神经网络中特征图大小计算公式总结

    W:输入特征图的宽,H:输入特征图的高 K:kernel size卷积核宽和高,P:padding(特征图需要填充的0的个数),S:stride步长 width_out:卷积后输出特征图的宽,heig ...

  8. 关于模型训练中显存占用过大的或直接报显存爆炸的解决方法

    模型训练显存爆炸解决方法 在模型训练中,应该理解梯度.反向传播.图层.显存这些概念,在模型训练过程中,一般会分为训练+验证+测试 ,在这些过程中,一般在训练过程中会比较占用显存,因为涉及到反向传播,需 ...

  9. tensor torch 构造_详解Pytorch中的网络构造

    背景 在PyTroch框架中,如果要自定义一个Net(网络,或者model,在本文中,model和Net拥有同样的意思),通常需要继承自nn.Module然后实现自己的layer.比如,在下面的示例中 ...

  10. allegro 设计中显示网络飞线或关闭网络飞线的方法

    1.allegro pcb设计中显示全部网络线,或都关闭全部网络 2-allegro pcb设计中显示某一部分部网,或者显示器件网络:在display >show rats> net显示网 ...

最新文章

  1. 开启报名丨AutoML-Zero:从零开始搜索机器学习算法
  2. 全中了!接手同事项目时最崩溃的事 | 每日趣闻
  3. linux系统主要常见目录结构
  4. DVWA1.9平台XSS小结
  5. 【NOIP2016】蚯蚓 --队列模拟
  6. 高效的判断素数---筛选法
  7. Hibernate HQL基础 投影查询
  8. 的控制台主题_【12.11最新版】芯片机/大气层主题软件NXThemesInstaller
  9. c语言sizeof(test),解析C语言中的sizeof
  10. 作为程序猿必须了解的生产者与消费者
  11. poi 获取删除线_Houdini 删除相机看不到的点背面的点或面
  12. PCWorld评出的2010年世界杀毒软件排名
  13. 关于python中pymysql数据编码问题
  14. js 获得较浅的颜色_了解较少的颜色功能
  15. gulp在工作中的应用
  16. 涨价不再“嘘声”一片,“爱优腾”集体“想开了”?
  17. 手机摄影 -参数ISO
  18. Win10 WLAN消失网络连接不上解决方法
  19. 荣耀猎人游戏本散热怎么样?测试一下就知道
  20. 【爬虫学习笔记day66】7.8. scrapy-redis实战-- IT桔子分布式项目2

热门文章

  1. Excel VB脚本,下拉框多选
  2. echart饼状图上显示百分比
  3. redis集群搭建管理入门
  4. python识别验证码并自动登录_Python完全识别验证码自动登录实例详解
  5. 新产品开发过程基本原则
  6. 计算机无法正常更新,电脑时间不能自动更新怎么回事?电脑时间校准同步方法介绍...
  7. pdfplumber和pdfminer.six提取PDF中文本行内容及对应坐标
  8. PHP 大小写转换函数 lcfirst ucfirst ucwords strtolower strtoupper
  9. 用计算机弹刚好一点,《计算机组成原理》作业解答(14级)
  10. 浏览器刷新和页面手动为什么不一样?