Oven logo

Oven

Published

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

pip install ultralytics-thop

Package Downloads

Weekly DownloadsMonthly Downloads

Authors

Ligeng Zhu

Requires Python

>=3.8

Dependencies


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.

ModelParams(M)MACs(G)
alexnet61.100.77
vgg11132.867.74
vgg11_bn132.877.77
vgg13133.0511.44
vgg13_bn133.0511.49
vgg16138.3615.61
vgg16_bn138.3715.66
vgg19143.6719.77
vgg19_bn143.6819.83
resnet1811.691.82
resnet3421.803.68
resnet5025.564.14
resnet10144.557.87
resnet15260.1911.61
wide_resnet101_2126.8922.84
wide_resnet50_268.8811.46
ModelParams(M)MACs(G)
resnext50_32x4d25.034.29
resnext101_32x8d88.7916.54
densenet1217.982.90
densenet16128.687.85
densenet16914.153.44
densenet20120.014.39
squeezenet1_01.250.82
squeezenet1_11.240.35
mnasnet0_52.220.14
mnasnet0_753.170.24
mnasnet1_04.380.34
mnasnet1_36.280.53
mobilenet_v23.500.33
shufflenet_v2_x0_51.370.05
shufflenet_v2_x1_02.280.15
shufflenet_v2_x1_53.500.31
shufflenet_v2_x2_07.390.60
inception_v327.165.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