torch-scan

torch-scan

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

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

PyTorch模型分析神经网络性能评估深度学习Github开源项目
<p align="center"> <img src="https://github.com/frgfm/torch-scan/releases/download/v0.1.1/logo_text.png" width="30%"> </p> <p align="center"> <a href="https://github.com/frgfm/torch-scan/actions/workflows/builds.yml"> <img alt="CI Status" src="https://img.shields.io/github/actions/workflow/status/frgfm/torch-scan/builds.yml?branch=main&label=CI&logo=github&style=flat-square"> </a> <a href="https://github.com/astral-sh/ruff"> <img src="https://img.shields.io/badge/Linter-Ruff-FCC21B?style=flat-square&logo=ruff&logoColor=white" alt="ruff"> </a> <a href="https://github.com/astral-sh/ruff"> <img src="https://img.shields.io/badge/Formatter-Ruff-FCC21B?style=flat-square&logo=Python&logoColor=white" alt="ruff"> </a> <a href="https://www.codacy.com/gh/frgfm/torch-scan/dashboard?utm_source=github.com&amp;utm_medium=referral&amp;utm_content=frgfm/torch-scan&amp;utm_campaign=Badge_Grade"><img src="https://app.codacy.com/project/badge/Grade/9dc68e8bfce34d9dbc8b44a350e9adc7"/></a> <a href="https://codecov.io/gh/frgfm/torch-scan"> <img src="https://img.shields.io/codecov/c/github/frgfm/torch-scan.svg?logo=codecov&style=flat-square&label=Coverage" alt="Test coverage percentage"> </a> </p> <p align="center"> <a href="https://pypi.org/project/torchscan/"> <img src="https://img.shields.io/pypi/v/torchscan.svg?logo=PyPI&logoColor=fff&style=flat-square&label=PyPI" alt="PyPi Version"> </a> <a href="https://anaconda.org/frgfm/torchscan"> <img src="https://img.shields.io/conda/v/frgfm/torchscan.svg?logo=anaconda&label=Conda&logoColor=fff&style=flat-square" alt="Conda Version"> </a> <img src="https://img.shields.io/pypi/pyversions/torchscan.svg?logo=Python&label=Python&logoColor=fff&style=flat-square" alt="pyversions"> <a href="https://github.com/frgfm/torch-scan/blob/main/LICENSE"> <img src="https://img.shields.io/github/license/frgfm/torch-scan.svg?label=License&logoColor=fff&style=flat-square" alt="License"> </a> </p> <p align="center"> <a href="https://frgfm.github.io/torch-scan"> <img src="https://img.shields.io/github/actions/workflow/status/frgfm/torch-scan/docs.yml?branch=main&label=Documentation&logo=read-the-docs&logoColor=white&style=flat-square" alt="Documentation Status"> </a> </p>

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

编辑推荐精选

扣子-AI办公

扣子-AI办公

职场AI,就用扣子

AI办公助手,复杂任务高效处理。办公效率低?扣子空间AI助手支持播客生成、PPT制作、网页开发及报告写作,覆盖科研、商业、舆情等领域的专家Agent 7x24小时响应,生活工作无缝切换,提升50%效率!

堆友

堆友

多风格AI绘画神器

堆友平台由阿里巴巴设计团队创建,作为一款AI驱动的设计工具,专为设计师提供一站式增长服务。功能覆盖海量3D素材、AI绘画、实时渲染以及专业抠图,显著提升设计品质和效率。平台不仅提供工具,还是一个促进创意交流和个人发展的空间,界面友好,适合所有级别的设计师和创意工作者。

图像生成AI工具AI反应堆AI工具箱AI绘画GOAI艺术字堆友相机AI图像热门
码上飞

码上飞

零代码AI应用开发平台

零代码AI应用开发平台,用户只需一句话简单描述需求,AI能自动生成小程序、APP或H5网页应用,无需编写代码。

Vora

Vora

免费创建高清无水印Sora视频

Vora是一个免费创建高清无水印Sora视频的AI工具

Refly.AI

Refly.AI

最适合小白的AI自动化工作流平台

无需编码,轻松生成可复用、可变现的AI自动化工作流

酷表ChatExcel

酷表ChatExcel

大模型驱动的Excel数据处理工具

基于大模型交互的表格处理系统,允许用户通过对话方式完成数据整理和可视化分析。系统采用机器学习算法解析用户指令,自动执行排序、公式计算和数据透视等操作,支持多种文件格式导入导出。数据处理响应速度保持在0.8秒以内,支持超过100万行数据的即时分析。

AI工具酷表ChatExcelAI智能客服AI营销产品使用教程
TRAE编程

TRAE编程

AI辅助编程,代码自动修复

Trae是一种自适应的集成开发环境(IDE),通过自动化和多元协作改变开发流程。利用Trae,团队能够更快速、精确地编写和部署代码,从而提高编程效率和项目交付速度。Trae具备上下文感知和代码自动完成功能,是提升开发效率的理想工具。

AI工具TraeAI IDE协作生产力转型热门
AIWritePaper论文写作

AIWritePaper论文写作

AI论文写作指导平台

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

AI辅助写作AI工具AI论文工具论文写作智能生成大纲数据安全AI助手热门
博思AIPPT

博思AIPPT

AI一键生成PPT,就用博思AIPPT!

博思AIPPT,新一代的AI生成PPT平台,支持智能生成PPT、AI美化PPT、文本&链接生成PPT、导入Word/PDF/Markdown文档生成PPT等,内置海量精美PPT模板,涵盖商务、教育、科技等不同风格,同时针对每个页面提供多种版式,一键自适应切换,完美适配各种办公场景。

AI办公办公工具AI工具博思AIPPTAI生成PPT智能排版海量精品模板AI创作热门
潮际好麦

潮际好麦

AI赋能电商视觉革命,一站式智能商拍平台

潮际好麦深耕服装行业,是国内AI试衣效果最好的软件。使用先进AIGC能力为电商卖家批量提供优质的、低成本的商拍图。合作品牌有Shein、Lazada、安踏、百丽等65个国内外头部品牌,以及国内10万+淘宝、天猫、京东等主流平台的品牌商家,为卖家节省将近85%的出图成本,提升约3倍出图效率,让品牌能够快速上架。

下拉加载更多