这个库可以查看用pytorch搭建的模型,各层输出尺寸。
1.安装
相信大家使用到现在这个地步,导入库已经很熟练了。里面的坑我就不多说。
pip install torchsummary
2.导入
from torchsummary import summary
summary(your_model, input_size=(channels, H, W))
上述的your_model是搭建的模型,input_size是你输入的数据。比如输入一张图片大小为28*28的RGB(3个通道)的图片。那么input_size=(3,28,28),模型是根据你输入的大小来输出中间过程的模型尺寸。而且这里不能输入多张比如input_size(8, 3, 28, 28),你想输入八张看效果,不可以的,会报错。
3.使用实例介绍
这里简单导入一个VGG的模型,介绍一下使用情况:
import torch
from torchsummary import summary
from torchvision.models import vgg11
model = vgg11(pretrained=False) #使用现成的VGG11的网络结构
if torch.cuda.is_available():
model.cuda()
summary(model, (3, 224, 224))
输出结果:
----------------------------------------------------------------
Layer (type) Output Shape Param #
================================================================
Conv2d-1 [-1, 64, 224, 224] 1792
ReLU-2 [-1, 64, 224, 224] 0
MaxPool2d-3 [-1, 64, 112, 112] 0
Conv2d-4 [-1, 128, 112, 112] 73856
ReLU-5 [-1, 128, 112, 112] 0
MaxPool2d-6 [-1, 128, 56, 56] 0
Conv2d-7 [-1, 256, 56, 56] 295168
ReLU-8 [-1, 256, 56, 56] 0
Conv2d-9 [-1, 256, 56, 56] 590080
ReLU-10 [-1, 256, 56, 56] 0
MaxPool2d-11 [-1, 256, 28, 28] 0
Conv2d-12 [-1, 512, 28, 28] 1180160
ReLU-13 [-1, 512, 28, 28] 0
Conv2d-14 [-1, 512, 28, 28] 2359808
ReLU-15 [-1, 512, 28, 28] 0
MaxPool2d-16 [-1, 512, 14, 14] 0
Conv2d-17 [-1, 512, 14, 14] 2359808
ReLU-18 [-1, 512, 14, 14] 0
Conv2d-19 [-1, 512, 14, 14] 2359808
ReLU-20 [-1, 512, 14, 14] 0
MaxPool2d-21 [-1, 512, 7, 7] 0
Linear-22 [-1, 4096] 102764544
ReLU-23 [-1, 4096] 0
Dropout-24 [-1, 4096] 0
Linear-25 [-1, 4096] 16781312
ReLU-26 [-1, 4096] 0
Dropout-27 [-1, 4096] 0
Linear-28 [-1, 1000] 4097000
================================================================
Total params: 132863336
Trainable params: 132863336
Non-trainable params: 0
----------------------------------------------------------------