Skip to main content

A tool to layer-wise count the MACs and parameters of PyTorch model.

Project description

PyTorch-layerwise-OpCounter

A tool for profile the MACs, parameters, input_shape, output_shape et.al of each layer in Pytorch model. Forked from Lyken17/pytorch-OpCounter which is not supporting layer-wise profile and I will follow it.

How to install

pip install torchlop

OR

pip install --upgrade git+https://github.com/hahnyuan/pytorch-layerwise-OpCounter.git

How to use

  • Basic usage

    from torchvision.models import resnet50
    from torchlop import profile
    model = resnet50()
    input = torch.randn(1, 3, 224, 224)
    macs, params, layer_infos = profile(model, inputs=(input, ))
    
  • The layer_infos is a dict that contains the infos for each layer of the pytorch model.

    • The key is the name of the layer
    • 'type': the class name of the layer
    • 'in_size': input size
    • 'out_size': output size
    • 'ops': operations (MACs)
    • 'params': parameters
  • Define the rule for 3rd party module.

    class YourModule(nn.Module):
        # your definition
    def count_your_model(model, x, y):
        # your rule here
    
    input = torch.randn(1, 3, 224, 224)
    macs, params, ayer_infos = profile(model, inputs=(input, ), 
                            custom_ops={YourModule: count_your_model})
    

Results of Recent Models

The implementation are adapted from torchvision. Following results can be obtained using benchmark/evaluate_famours_models.py.

Model Params(M) MACs(G)
alexnet 61.10 0.77
vgg11 132.86 7.74
vgg11_bn 132.87 7.77
vgg13 133.05 11.44
vgg13_bn 133.05 11.49
vgg16 138.36 15.61
vgg16_bn 138.37 15.66
vgg19 143.67 19.77
vgg19_bn 143.68 19.83
resnet18 11.69 1.82
resnet34 21.80 3.68
resnet50 25.56 4.14
resnet101 44.55 7.87
resnet152 60.19 11.61
wide_resnet101_2 126.89 22.84
wide_resnet50_2 68.88 11.46
Model Params(M) MACs(G)
resnext50_32x4d 25.03 4.29
resnext101_32x8d 88.79 16.54
densenet121 7.98 2.90
densenet161 28.68 7.85
densenet169 14.15 3.44
densenet201 20.01 4.39
squeezenet1_0 1.25 0.82
squeezenet1_1 1.24 0.35
mnasnet0_5 2.22 0.14
mnasnet0_75 3.17 0.24
mnasnet1_0 4.38 0.34
mnasnet1_3 6.28 0.53
mobilenet_v2 3.50 0.33
shufflenet_v2_x0_5 1.37 0.05
shufflenet_v2_x1_0 2.28 0.15
shufflenet_v2_x1_5 3.50 0.31
shufflenet_v2_x2_0 7.39 0.60
inception_v3 27.16 5.75

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

torchlop-0.1.tar.gz (6.0 kB view details)

Uploaded Source

Built Distribution

torchlop-0.1-py3-none-any.whl (7.6 kB view details)

Uploaded Python 3

File details

Details for the file torchlop-0.1.tar.gz.

File metadata

  • Download URL: torchlop-0.1.tar.gz
  • Upload date:
  • Size: 6.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.30.0 CPython/3.6.7

File hashes

Hashes for torchlop-0.1.tar.gz
Algorithm Hash digest
SHA256 7549d2396ee86439bc5f8e96dbb319dca0a9397402530e5b9f89bfe2501e2fb9
MD5 4d191d4feec1ef959b5c7eebfb1b591f
BLAKE2b-256 868eb7ee8f578709f20ebda83f5356ba73337f560b8988bd757fc8837e787a1b

See more details on using hashes here.

File details

Details for the file torchlop-0.1-py3-none-any.whl.

File metadata

  • Download URL: torchlop-0.1-py3-none-any.whl
  • Upload date:
  • Size: 7.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.30.0 CPython/3.6.7

File hashes

Hashes for torchlop-0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 8525ebd6e4fa5a2a1b4c6a9517f1ac8bfe0d2f81d18e6ef5ffd80493a0e83ff6
MD5 eab6af6e23c4c13521311b418adeb49e
BLAKE2b-256 faad3c7425fce87aca4fb3efe2b5e400673150a6a44424bed7a6c97aa1f04ddc

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page