THOP: PyTorch-OpCounter
How to install
pip install thop
(now continously intergrated on Github actions)
OR
pip install --upgrade git+https://github.com/Lyken17/pytorch-OpCounter.git
How to use
Basic usage
from torchvision.models import resnet50from thop import profile
model = resnet50()input = torch.randn(1, 3, 224, 224)
flops, params = profile(model, inputs=(input, ))
Define the rule for 3rd party module.
class YourModule(nn.Module): # your definitiondef count_your_model(model, x, y): # your rule hereinput = torch.randn(1, 3, 224, 224)
flops, params = profile(model, inputs=(input, ),
custom_ops={YourModule: count_your_model})
Improve the output readability
Call thop.clever_format
to give a better format of the output.
from thop import clever_format
flops, params = clever_format([flops, params], "%.3f")
Results of Recent Models
The implementation are adapted from torchvision
. Following results can be obtained using benchmark/evaluate_famours_models.py.
ModelParams(M)MACs(G)alexnet61.100.77vgg11132.867.74vgg11_bn132.877.77vgg13133.0511.44vgg13_bn133.0511.49vgg16138.3615.61vgg16_bn138.3715.66vgg19143.6719.77vgg19_bn143.6819.83resnet1811.691.82resnet3421.803.68resnet5025.564.14resnet10144.557.87resnet15260.1911.61wide_resnet101_2126.8922.84wide_resnet50_268.8811.46 | ModelParams(M)MACs(G)resnext50_32x4d25.034.29resnext101_32x8d88.7916.54densenet1217.982.90densenet16128.687.85densenet16914.153.44densenet20120.014.39squeezenet1_01.250.82squeezenet1_11.240.35mnasnet0_52.220.14mnasnet0_753.170.24mnasnet1_04.380.34mnasnet1_36.280.53mobilenet_v23.500.33shufflenet_v2_x0_51.370.05shufflenet_v2_x1_02.280.15shufflenet_v2_x1_53.500.31shufflenet_v2_x2_07.390.60inception_v327.165.75 |