Project Icon

torch-scan

PyTorch模型分析和性能评估工具

torch-scan是一个专门用于PyTorch模型分析的开源工具。它提供详细的模型结构信息,包括参数数量、FLOPs、MACs和内存使用等指标。支持分析嵌套复杂架构,可估算卷积网络感受野。该工具帮助开发者深入了解和优化PyTorch模型,适用于模型分析和性能评估。

CI Status ruff ruff Test coverage percentage

PyPi Version Conda Version pyversions License

Documentation Status

The very useful summary method of tf.keras.Model but for PyTorch, with more useful information.

Quick Tour

Inspecting your PyTorch architecture

Similarly to the torchsummary implementation, torchscan brings useful module information into readable format. For nested complex architectures, you can use a maximum depth of display as follows:

from torchvision.models import densenet121
from torchscan import summary

model = densenet121().eval().cuda()
summary(model, (3, 224, 224), max_depth=2)

which would yield

__________________________________________________________________________________________
Layer                        Type                  Output Shape              Param #
==========================================================================================
densenet                     DenseNet              (-1, 1000)                0
├─features                   Sequential            (-1, 1024, 7, 7)          0
|    └─conv0                 Conv2d                (-1, 64, 112, 112)        9,408
|    └─norm0                 BatchNorm2d           (-1, 64, 112, 112)        257
|    └─relu0                 ReLU                  (-1, 64, 112, 112)        0
|    └─pool0                 MaxPool2d             (-1, 64, 56, 56)          0
|    └─denseblock1           _DenseBlock           (-1, 256, 56, 56)         338,316
|    └─transition1           _Transition           (-1, 128, 28, 28)         33,793
|    └─denseblock2           _DenseBlock           (-1, 512, 28, 28)         930,072
|    └─transition2           _Transition           (-1, 256, 14, 14)         133,121
|    └─denseblock3           _DenseBlock           (-1, 1024, 14, 14)        2,873,904
|    └─transition3           _Transition           (-1, 512, 7, 7)           528,385
|    └─denseblock4           _DenseBlock           (-1, 1024, 7, 7)          2,186,272
|    └─norm5                 BatchNorm2d           (-1, 1024, 7, 7)          4,097
├─classifier                 Linear                (-1, 1000)                1,025,000
==========================================================================================
Trainable params: 7,978,856
Non-trainable params: 0
Total params: 7,978,856
------------------------------------------------------------------------------------------
Model size (params + buffers): 30.76 Mb
Framework & CUDA overhead: 423.57 Mb
Total RAM usage: 454.32 Mb
------------------------------------------------------------------------------------------
Floating Point Operations on forward: 5.74 GFLOPs
Multiply-Accumulations on forward: 2.87 GMACs
Direct memory accesses on forward: 2.90 GDMAs
__________________________________________________________________________________________

Results are aggregated to the selected depth for improved readability.

For reference, here are explanations of a few acronyms:

  • FLOPs: floating-point operations (not to be confused with FLOPS which is FLOPs per second)
  • MACs: mutiply-accumulate operations (cf. wikipedia)
  • DMAs: direct memory accesses (many argue that it is more relevant than FLOPs or MACs to compare model inference speeds cf. wikipedia)

Additionally, for highway nets (models without multiple branches / skip connections), torchscan supports receptive field estimation.

from torchvision.models import vgg16
from torchscan import summary

model = vgg16().eval().cuda()
summary(model, (3, 224, 224), receptive_field=True, max_depth=0)

which will add the layer's receptive field (relatively to the last convolutional layer) to the summary.

Setup

Python 3.8 (or newer) and pip/conda are required to install Torchscan.

Stable release

You can install the last stable release of the package using pypi as follows:

pip install torchscan

or using conda:

conda install -c frgfm torchscan

Developer installation

Alternatively, if you wish to use the latest features of the project that haven't made their way to a release yet, you can install the package from source:

git clone https://github.com/frgfm/torch-scan.git
pip install -e torch-scan/.

Benchmark

Below are the results for classification models supported by torchvision for a single image with 3 color channels of size 224x224 (apart from inception_v3 which uses 299x299).

ModelParams (M)FLOPs (G)MACs (G)DMAs (G)RF
alexnet61.11.430.710.72195
googlenet6.623.011.511.53--
vgg11132.8615.237.617.64150
vgg11_bn132.8715.267.637.66150
vgg13133.0522.6311.3111.35156
vgg13_bn133.0522.6811.3311.37156
vgg16138.3630.9615.4715.52212
vgg16_bn138.3731.0115.515.55212
vgg19143.6739.2819.6319.69268
vgg19_bn143.6839.3419.6619.72268
resnet1811.693.641.821.84--
resnet3421.87.343.673.7--
resnet5025.568.214.114.15--
resnet10144.5515.667.837.9--
resnet15260.1923.111.5611.65--
inception_v327.1611.455.735.76--
squeezenet1_01.251.640.820.83--
squeezenet1_11.240.70.350.36--
wide_resnet50_268.8822.8411.4311.51--
wide_resnet101_2126.8945.5822.822.95--
densenet1217.985.742.872.9--
densenet16128.6815.597.797.86--
densenet16914.156.813.43.44--
densenet20120.018.74.344.39--
resnext50_32x4d25.038.514.264.3--
resnext101_32x8d88.7932.9316.4816.61--
mobilenet_v23.50.630.310.32--
shufflenet_v2_x0_51.370.090.040.05--
shufflenet_v2_x1_02.280.30.150.15--
shufflenet_v2_x1_53.50.60.30.31--
shufflenet_v2_x2_07.391.180.590.6--
mnasnet0_52.220.220.110.12--
mnasnet0_753.170.450.230.24--
mnasnet1_04.380.650.330.34--
mnasnet1_36.281.080.540.56--

The above results were produced using the scripts/benchmark.py script.

Note: receptive field computation is currently only valid for highway nets.

What else

Documentation

The full package documentation is available here for detailed specifications.

Example script

An example script is provided for you to benchmark torchvision models using the library:

python scripts/benchmark.py

Credits

This project is developed and maintained by the repo owner, but the implementation was inspired or helped by the following contributions:

Citation

If you wish to cite this project, feel free to use this BibTeX reference:

@misc{torchscan2020,
    title={Torchscan: meaningful module insights},
    author={François-Guillaume Fernandez},
    year={2020},
    month={March},
    publisher = {GitHub},
    howpublished = {\url{https://github.com/frgfm/torch-scan}}
}

Contributing

Any sort of contribution is greatly appreciated!

You can find a short guide in CONTRIBUTING to help grow this project!

License

Distributed under the Apache 2.0 License. See LICENSE for more

项目侧边栏1项目侧边栏2
推荐项目
Project Cover

豆包MarsCode

豆包 MarsCode 是一款革命性的编程助手,通过AI技术提供代码补全、单测生成、代码解释和智能问答等功能,支持100+编程语言,与主流编辑器无缝集成,显著提升开发效率和代码质量。

Project Cover

AI写歌

Suno AI是一个革命性的AI音乐创作平台,能在短短30秒内帮助用户创作出一首完整的歌曲。无论是寻找创作灵感还是需要快速制作音乐,Suno AI都是音乐爱好者和专业人士的理想选择。

Project Cover

白日梦AI

白日梦AI提供专注于AI视频生成的多样化功能,包括文生视频、动态画面和形象生成等,帮助用户快速上手,创造专业级内容。

Project Cover

有言AI

有言平台提供一站式AIGC视频创作解决方案,通过智能技术简化视频制作流程。无论是企业宣传还是个人分享,有言都能帮助用户快速、轻松地制作出专业级别的视频内容。

Project Cover

Kimi

Kimi AI助手提供多语言对话支持,能够阅读和理解用户上传的文件内容,解析网页信息,并结合搜索结果为用户提供详尽的答案。无论是日常咨询还是专业问题,Kimi都能以友好、专业的方式提供帮助。

Project Cover

讯飞绘镜

讯飞绘镜是一个支持从创意到完整视频创作的智能平台,用户可以快速生成视频素材并创作独特的音乐视频和故事。平台提供多样化的主题和精选作品,帮助用户探索创意灵感。

Project Cover

讯飞文书

讯飞文书依托讯飞星火大模型,为文书写作者提供从素材筹备到稿件撰写及审稿的全程支持。通过录音智记和以稿写稿等功能,满足事务性工作的高频需求,帮助撰稿人节省精力,提高效率,优化工作与生活。

Project Cover

阿里绘蛙

绘蛙是阿里巴巴集团推出的革命性AI电商营销平台。利用尖端人工智能技术,为商家提供一键生成商品图和营销文案的服务,显著提升内容创作效率和营销效果。适用于淘宝、天猫等电商平台,让商品第一时间被种草。

Project Cover

AIWritePaper论文写作

AIWritePaper论文写作是一站式AI论文写作辅助工具,简化了选题、文献检索至论文撰写的整个过程。通过简单设定,平台可快速生成高质量论文大纲和全文,配合图表、参考文献等一应俱全,同时提供开题报告和答辩PPT等增值服务,保障数据安全,有效提升写作效率和论文质量。

投诉举报邮箱: service@vectorlightyear.com
@2024 懂AI·鲁ICP备2024100362号-6·鲁公网安备37021002001498号