pytorch框架下卷积网络的flops计数器
ptflops的Python项目详细描述
pytorch框架中卷积网络的flops计数器
此脚本用于计算乘法加法运算的理论量 在卷积神经网络中。它还可以计算参数的数量和 给定网络的每层打印计算成本。
支持的层:
- conv1d/2d/3d(包括分组)
- convTranspasse2d(包括分组)
- 批次标准1d/2d/3d
- 激活(relu、prelu、elu、relu6、leakyrelu)
- 线性
- 向上采样
- 池(avgpool1d/2d/3d、maxpool1d/2d/3d和自适应池)
要求:Pythorch>;=0.4.1,TorchVision>;=0.2.1
感谢@warmspringwinds提供了脚本的初始版本。
使用技巧
- 此脚本不考虑
torch.nn.functional.*
操作。例如,如果一个人有一个语义分割模型,并且使用torch.nn.functional.interpolate
来提升特性,那么这些操作将不会贡献总的失败次数。为了避免这种情况,可以使用torch.nn.Upsample
,而不是torch.nn.functional.interpolate
。 ptflops
在一个随机张量上启动一个给定的模型,并在推理过程中估计计算量。复杂的模型可以有几个输入,其中一些可以是可选的。要构造非平凡的输入,可以使用get_model_complexity_info
的input_constructor
参数。input_constructor
是一个函数,它将输入空间分辨率作为元组,并返回带有模型的命名输入参数的dict。接下来,这个dict将作为关键字参数传递给模型。
安装最新版本
pip install --upgrade git+https://github.com/sovrasov/flops-counter.pytorch.git
示例
importtorchvision.modelsasmodelsimporttorchfromptflopsimportget_model_complexity_infowithtorch.cuda.device(0):net=models.densenet161()flops,params=get_model_complexity_info(net,(3,224,224),as_strings=True,print_per_layer_stat=True)print('Flops: '+flops)print('Params: '+params)
基准
torchvision
Model | Input Resolution | Params(M) | MACs(G) | Top-1 error | Top-5 error |
---|---|---|---|---|---|
alexnet | 224x224 | 61.1 | 0.72 | 43.45 | 20.91 |
vgg11 | 224x224 | 132.86 | 7.63 | 30.98 | 11.37 |
vgg13 | 224x224 | 133.05 | 11.34 | 30.07 | 10.75 |
vgg16 | 224x224 | 138.36 | 15.5 | 28.41 | 9.62 |
vgg19 | 224x224 | 143.67 | 19.67 | 27.62 | 9.12 |
vgg11_bn | 224x224 | 132.87 | 7.64 | 29.62 | 10.19 |
vgg13_bn | 224x224 | 133.05 | 11.36 | 28.45 | 9.63 |
vgg16_bn | 224x224 | 138.37 | 15.53 | 26.63 | 8.50 |
vgg19_bn | 224x224 | 143.68 | 19.7 | 25.76 | 8.15 |
resnet18 | 224x224 | 11.69 | 1.82 | 30.24 | 10.92 |
resnet34 | 224x224 | 21.8 | 3.68 | 26.70 | 8.58 |
resnet50 | 224x224 | 25.56 | 4.12 | 23.85 | 7.13 |
resnet101 | 224x224 | 44.55 | 7.85 | 22.63 | 6.44 |
resnet152 | 224x224 | 60.19 | 11.58 | 21.69 | 5.94 |
squeezenet1_0 | 224x224 | 1.25 | 0.83 | 41.90 | 19.58 |
squeezenet1_1 | 224x224 | 1.24 | 0.36 | 41.81 | 19.38 |
densenet121 | 224x224 | 7.98 | 2.88 | 25.35 | 7.83 |
densenet169 | 224x224 | 14.15 | 3.42 | 24.00 | 7.00 |
densenet201 | 224x224 | 20.01 | 4.37 | 22.80 | 6.43 |
densenet161 | 224x224 | 28.68 | 7.82 | 22.35 | 6.20 |
inception_v3 | 224x224 | 27.16 | 2.85 | 22.55 | 6.44 |
- Top-1错误-ImageNet单裁剪Top-1错误(224x224)
- Top-5错误-ImageNet单次裁剪Top-5错误(224x224)
Cadene/pretrained-models.pytorch
Model | Input Resolution | Params(M) | MACs(G) | Acc@1 | Acc@5 |
---|---|---|---|---|---|
alexnet | 224x224 | 61.1 | 0.72 | 56.432 | 79.194 |
bninception | 224x224 | 11.3 | 2.05 | 73.524 | 91.562 |
cafferesnet101 | 224x224 | 44.55 | 7.62 | 76.2 | 92.766 |
densenet121 | 224x224 | 7.98 | 2.88 | 74.646 | 92.136 |
densenet161 | 224x224 | 28.68 | 7.82 | 77.56 | 93.798 |
densenet169 | 224x224 | 14.15 | 3.42 | 76.026 | 92.992 |
densenet201 | 224x224 | 20.01 | 4.37 | 77.152 | 93.548 |
dpn107 | 224x224 | 86.92 | 18.42 | 79.746 | 94.684 |
dpn131 | 224x224 | 79.25 | 16.13 | 79.432 | 94.574 |
dpn68 | 224x224 | 12.61 | 2.36 | 75.868 | 92.774 |
dpn68b | 224x224 | 12.61 | 2.36 | 77.034 | 93.59 |
dpn92 | 224x224 | 37.67 | 6.56 | 79.4 | 94.62 |
dpn98 | 224x224 | 61.57 | 11.76 | 79.224 | 94.488 |
fbresnet152 | 224x224 | 60.27 | 11.6 | 77.386 | 93.594 |
inceptionresnetv2 | 299x299 | 55.84 | 13.22 | 80.17 | 95.234 |
inceptionv3 | 299x299 | 27.16 | 5.73 | 77.294 | 93.454 |
inceptionv4 | 299x299 | 42.68 | 12.31 | 80.062 | 94.926 |
nasnetalarge | 331x331 | 88.75 | 24.04 | 82.566 | 96.086 |
nasnetamobile | 224x224 | 5.29 | 0.59 | 74.08 | 91.74 |
pnasnet5large | 331x331 | 86.06 | 25.21 | 82.736 | 95.992 |
polynet | 331x331 | 95.37 | 34.9 | 81.002 | 95.624 |
resnet101 | 224x224 | 44.55 | 7.85 | 77.438 | 93.672 |
resnet152 | 224x224 | 60.19 | 11.58 | 78.428 | 94.11 |
resnet18 | 224x224 | 11.69 | 1.82 | 70.142 | 89.274 |
resnet34 | 224x224 | 21.8 | 3.68 | 73.554 | 91.456 |
resnet50 | 224x224 | 25.56 | 4.12 | 76.002 | 92.98 |
resnext101_32x4d | 224x224 | 44.18 | 8.03 | 78.188 | 93.886 |
resnext101_64x4d | 224x224 | 83.46 | 15.55 | 78.956 | 94.252 |
se_resnet101 | 224x224 | 49.33 | 7.63 | 78.396 | 94.258 |
se_resnet152 | 224x224 | 66.82 | 11.37 | 78.658 | 94.374 |
se_resnet50 | 224x224 | 28.09 | 3.9 | 77.636 | 93.752 |
se_resnext101_32x4d | 224x224 | 48.96 | 8.05 | 80.236 | 95.028 |
se_resnext50_32x4d | 224x224 | 27.56 | 4.28 | 79.076 | 94.434 |
senet154 | 224x224 | 115.09 | 20.82 | 81.304 | 95.498 |
squeezenet1_0 | 224x224 | 1.25 | 0.83 | 58.108 | 80.428 |
squeezenet1_1 | 224x224 | 1.24 | 0.36 | 58.25 | 80.8 |
vgg11 | 224x224 | 132.86 | 7.63 | 68.97 | 88.746 |
vgg11_bn | 224x224 | 132.87 | 7.64 | 70.452 | 89.818 |
vgg13 | 224x224 | 133.05 | 11.34 | 69.662 | 89.264 |
vgg13_bn | 224x224 | 133.05 | 11.36 | 71.508 | 90.494 |
vgg16 | 224x224 | 138.36 | 15.5 | 71.636 | 90.354 |
vgg16_bn | 224x224 | 138.37 | 15.53 | 73.518 | 91.608 |
vgg19 | 224x224 | 143.67 | 19.67 | 72.08 | 90.822 |
vgg19_bn | 224x224 | 143.68 | 19.7 | 74.266 | 92.066 |
xception | 299x299 | 22.86 | 8.42 | 78.888 | 94.292 |
- acc@1-imagenet在训练过程中使用的相同大小的验证图像上的单次裁剪最高精度。
- acc@5-imagenet在训练过程中使用的相同大小的验证图像的单次裁剪精度达到前5名。