Skip to main content

Ultralytics THOP package for fast computation of PyTorch model FLOPs and parameters.

Project description


Ultralytics logo

🚀 THOP: PyTorch-OpCounter

Welcome to the THOP repository, your comprehensive solution for profiling PyTorch models by computing the number of Multiply-Accumulate Operations (MACs) and parameters. This tool is essential for deep learning practitioners to evaluate model efficiency and performance.

GitHub Actions Discord Ultralytics Forums Ultralytics Reddit

📄 Description

THOP offers an intuitive API to profile PyTorch models by calculating the number of MACs and parameters. This functionality is crucial for assessing the computational efficiency and memory footprint of deep learning models.

📦 Installation

You can install THOP via pip:

PyPI - Version Downloads PyPI - Python Version

pip install ultralytics-thop

Alternatively, install the latest version directly from GitHub:

pip install --upgrade git+https://github.com/ultralytics/thop.git

🛠 How to Use

Basic Usage

To profile a model, you can use the following example:

import torch
from torchvision.models import resnet50

from thop import profile

model = resnet50()
input = torch.randn(1, 3, 224, 224)
macs, params = profile(model, inputs=(input,))

Define Custom Rules for Third-Party Modules

You can define custom rules for unsupported modules:

import torch.nn as nn


class YourModule(nn.Module):
    # your definition
    pass


def count_your_model(model, x, y):
    # your rule here
    pass


input = torch.randn(1, 3, 224, 224)
macs, params = profile(model, inputs=(input,), custom_ops={YourModule: count_your_model})

Improve Output Readability

Use thop.clever_format for a more readable output:

from thop import clever_format

macs, params = clever_format([macs, params], "%.3f")

📊 Results of Recent Models

The following table presents the parameters and MACs for popular models. These results can be reproduced using the script benchmark/evaluate_famous_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

💡 Contribute

We welcome community contributions to enhance THOP. Please check our Contributing Guide for more details. Your feedback and suggestions are highly appreciated!

📄 License

THOP is licensed under the AGPL-3.0 License. For more information, see the LICENSE file.

📮 Contact

For bugs or feature requests, please open an issue on GitHub Issues. Join our community on Discord for discussions and support.


Ultralytics GitHub space Ultralytics LinkedIn space Ultralytics Twitter space Ultralytics YouTube space Ultralytics TikTok space Ultralytics BiliBili space Ultralytics Discord

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

ultralytics_thop-2.0.14.tar.gz (28.8 kB view details)

Uploaded Source

Built Distribution

ultralytics_thop-2.0.14-py3-none-any.whl (26.5 kB view details)

Uploaded Python 3

File details

Details for the file ultralytics_thop-2.0.14.tar.gz.

File metadata

  • Download URL: ultralytics_thop-2.0.14.tar.gz
  • Upload date:
  • Size: 28.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.0.1 CPython/3.12.8

File hashes

Hashes for ultralytics_thop-2.0.14.tar.gz
Algorithm Hash digest
SHA256 38ebfdbd3cd8dafdc3d26ec3a7d4f604fbeed5e69a74e61a48060b39736c945c
MD5 aad453c47b130ad23bda240f09b51545
BLAKE2b-256 c2d8e43a8bfcb03ff036119d098a7ea27be9f0adb715543ed6bd83b16cda83dc

See more details on using hashes here.

Provenance

The following attestation bundles were made for ultralytics_thop-2.0.14.tar.gz:

Publisher: publish.yml on ultralytics/thop

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file ultralytics_thop-2.0.14-py3-none-any.whl.

File metadata

File hashes

Hashes for ultralytics_thop-2.0.14-py3-none-any.whl
Algorithm Hash digest
SHA256 720b421e2459179fee21ec8f730d242a20774cd4b0a00a58d02351a39ec3881c
MD5 a1f8d6e8a5b9df4cb1eb1316d3bdda39
BLAKE2b-256 a610251f036b4c5d77249f9a119cc89dafe8745dc1ad1f1a5f06b6a3988ca454

See more details on using hashes here.

Provenance

The following attestation bundles were made for ultralytics_thop-2.0.14-py3-none-any.whl:

Publisher: publish.yml on ultralytics/thop

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

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