ultralytics-thop 2.0.13__tar.gz → 2.0.15__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (26) hide show
  1. ultralytics_thop-2.0.15/PKG-INFO +258 -0
  2. ultralytics_thop-2.0.15/README.md +219 -0
  3. {ultralytics_thop-2.0.13 → ultralytics_thop-2.0.15}/pyproject.toml +6 -5
  4. {ultralytics_thop-2.0.13 → ultralytics_thop-2.0.15}/tests/test_conv2d.py +5 -3
  5. {ultralytics_thop-2.0.13 → ultralytics_thop-2.0.15}/tests/test_matmul.py +2 -0
  6. {ultralytics_thop-2.0.13 → ultralytics_thop-2.0.15}/tests/test_relu.py +2 -0
  7. {ultralytics_thop-2.0.13 → ultralytics_thop-2.0.15}/tests/test_utils.py +2 -0
  8. {ultralytics_thop-2.0.13 → ultralytics_thop-2.0.15}/thop/__init__.py +3 -1
  9. {ultralytics_thop-2.0.13 → ultralytics_thop-2.0.15}/thop/fx_profile.py +3 -2
  10. {ultralytics_thop-2.0.13 → ultralytics_thop-2.0.15}/thop/profile.py +2 -0
  11. {ultralytics_thop-2.0.13 → ultralytics_thop-2.0.15}/thop/rnn_hooks.py +2 -0
  12. {ultralytics_thop-2.0.13 → ultralytics_thop-2.0.15}/thop/utils.py +2 -0
  13. ultralytics_thop-2.0.15/thop/vision/__init__.py +1 -0
  14. {ultralytics_thop-2.0.13 → ultralytics_thop-2.0.15}/thop/vision/basic_hooks.py +4 -1
  15. {ultralytics_thop-2.0.13 → ultralytics_thop-2.0.15}/thop/vision/calc_func.py +2 -0
  16. ultralytics_thop-2.0.15/ultralytics_thop.egg-info/PKG-INFO +258 -0
  17. ultralytics_thop-2.0.13/PKG-INFO +0 -199
  18. ultralytics_thop-2.0.13/README.md +0 -161
  19. ultralytics_thop-2.0.13/thop/vision/__init__.py +0 -0
  20. ultralytics_thop-2.0.13/ultralytics_thop.egg-info/PKG-INFO +0 -199
  21. {ultralytics_thop-2.0.13 → ultralytics_thop-2.0.15}/LICENSE +0 -0
  22. {ultralytics_thop-2.0.13 → ultralytics_thop-2.0.15}/setup.cfg +0 -0
  23. {ultralytics_thop-2.0.13 → ultralytics_thop-2.0.15}/ultralytics_thop.egg-info/SOURCES.txt +0 -0
  24. {ultralytics_thop-2.0.13 → ultralytics_thop-2.0.15}/ultralytics_thop.egg-info/dependency_links.txt +0 -0
  25. {ultralytics_thop-2.0.13 → ultralytics_thop-2.0.15}/ultralytics_thop.egg-info/requires.txt +0 -0
  26. {ultralytics_thop-2.0.13 → ultralytics_thop-2.0.15}/ultralytics_thop.egg-info/top_level.txt +0 -0
@@ -0,0 +1,258 @@
1
+ Metadata-Version: 2.4
2
+ Name: ultralytics-thop
3
+ Version: 2.0.15
4
+ Summary: Ultralytics THOP package for fast computation of PyTorch model FLOPs and parameters.
5
+ Author-email: Ligeng Zhu <ligeng.zhu+github@gmail.com>
6
+ Maintainer-email: Ultralytics <hello@ultralytics.com>
7
+ License: AGPL-3.0
8
+ Project-URL: Homepage, https://ultralytics.com
9
+ Project-URL: Source, https://github.com/ultralytics/thop
10
+ Project-URL: Documentation, https://docs.ultralytics.com
11
+ Project-URL: Bug Reports, https://github.com/ultralytics/thop/issues
12
+ Project-URL: Changelog, https://github.com/ultralytics/thop/releases
13
+ Keywords: FLOPs,PyTorch,Model Analysis
14
+ Classifier: Development Status :: 4 - Beta
15
+ Classifier: Intended Audience :: Developers
16
+ Classifier: Intended Audience :: Education
17
+ Classifier: Intended Audience :: Science/Research
18
+ Classifier: License :: OSI Approved :: GNU Affero General Public License v3 or later (AGPLv3+)
19
+ Classifier: Programming Language :: Python :: 3
20
+ Classifier: Programming Language :: Python :: 3.8
21
+ Classifier: Programming Language :: Python :: 3.9
22
+ Classifier: Programming Language :: Python :: 3.10
23
+ Classifier: Programming Language :: Python :: 3.11
24
+ Classifier: Programming Language :: Python :: 3.12
25
+ Classifier: Programming Language :: Python :: 3.13
26
+ Classifier: Topic :: Software Development
27
+ Classifier: Topic :: Scientific/Engineering
28
+ Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
29
+ Classifier: Topic :: Scientific/Engineering :: Image Recognition
30
+ Classifier: Operating System :: POSIX :: Linux
31
+ Classifier: Operating System :: MacOS
32
+ Classifier: Operating System :: Microsoft :: Windows
33
+ Requires-Python: >=3.8
34
+ Description-Content-Type: text/markdown
35
+ License-File: LICENSE
36
+ Requires-Dist: numpy
37
+ Requires-Dist: torch
38
+ Dynamic: license-file
39
+
40
+ <a href="https://www.ultralytics.com/"><img src="https://raw.githubusercontent.com/ultralytics/assets/main/logo/Ultralytics_Logotype_Original.svg" width="320" alt="Ultralytics logo"></a>
41
+
42
+ # 🚀 THOP: PyTorch-OpCounter
43
+
44
+ Welcome to the [THOP](https://github.com/ultralytics/thop) repository, your comprehensive solution for profiling [PyTorch](https://pytorch.org/) models by computing the number of Multiply-Accumulate Operations (MACs) and parameters. Developed by Ultralytics, this tool is essential for [deep learning](https://www.ultralytics.com/glossary/deep-learning-dl) practitioners aiming to evaluate model efficiency and performance, crucial aspects discussed in our [model training tips guide](https://docs.ultralytics.com/guides/model-training-tips/).
45
+
46
+ [![Ultralytics Actions](https://github.com/ultralytics/thop/actions/workflows/format.yml/badge.svg)](https://github.com/ultralytics/thop/actions/workflows/format.yml)
47
+ [![Ultralytics Discord](https://img.shields.io/discord/1089800235347353640?logo=discord&logoColor=white&label=Discord&color=blue)](https://discord.com/invite/ultralytics)
48
+ [![Ultralytics Forums](https://img.shields.io/discourse/users?server=https%3A%2F%2Fcommunity.ultralytics.com&logo=discourse&label=Forums&color=blue)](https://community.ultralytics.com/)
49
+ [![Ultralytics Reddit](https://img.shields.io/reddit/subreddit-subscribers/ultralytics?style=flat&logo=reddit&logoColor=white&label=Reddit&color=blue)](https://reddit.com/r/ultralytics)
50
+
51
+ ## 📄 Description
52
+
53
+ THOP offers an intuitive API designed to profile PyTorch models by calculating the total number of MACs and parameters. This functionality is vital for assessing the computational efficiency and memory footprint of deep learning models, helping developers optimize performance for deployment, especially on [edge devices](https://www.ultralytics.com/glossary/edge-ai). Understanding these metrics is key to selecting the right model architecture, a topic explored in our [model comparison pages](https://docs.ultralytics.com/compare/).
54
+
55
+ ## 📦 Installation
56
+
57
+ Get started with THOP quickly by installing it via pip:
58
+
59
+ [![PyPI - Version](https://img.shields.io/pypi/v/ultralytics-thop?logo=pypi&logoColor=white)](https://pypi.org/project/ultralytics-thop/) [![Downloads](https://static.pepy.tech/badge/ultralytics-thop)](https://www.pepy.tech/projects/ultralytics-thop) [![PyPI - Python Version](https://img.shields.io/pypi/pyversions/ultralytics-thop?logo=python&logoColor=gold)](https://pypi.org/project/ultralytics-thop/)
60
+
61
+ ```bash
62
+ pip install ultralytics-thop
63
+ ```
64
+
65
+ Alternatively, for the latest features and updates, install directly from the GitHub repository:
66
+
67
+ ```bash
68
+ pip install --upgrade git+https://github.com/ultralytics/thop.git
69
+ ```
70
+
71
+ This ensures you have the most recent version, incorporating the latest improvements and bug fixes.
72
+
73
+ ## 🛠️ How to Use
74
+
75
+ ### Basic Usage
76
+
77
+ Profiling a standard PyTorch model like [ResNet50](https://pytorch.org/vision/main/models/generated/torchvision.models.resnet50.html) is straightforward. Import the necessary libraries, load your model and a sample input tensor, then use the `profile` function:
78
+
79
+ ```python
80
+ import torch
81
+ from torchvision.models import resnet50 # Example model
82
+
83
+ from thop import profile # Import the profile function from THOP
84
+
85
+ # Load a pre-trained model (e.g., ResNet50)
86
+ model = resnet50()
87
+
88
+ # Create a dummy input tensor matching the model's expected input shape
89
+ dummy_input = torch.randn(1, 3, 224, 224)
90
+
91
+ # Profile the model
92
+ macs, params = profile(model, inputs=(dummy_input,))
93
+
94
+ print(f"MACs: {macs}, Parameters: {params}")
95
+ # Expected output: MACs: 4139975680.0, Parameters: 25557032.0
96
+ ```
97
+
98
+ ### Define Custom Rules for Third-Party Modules
99
+
100
+ If your model includes custom or third-party modules not natively supported by THOP, you can define custom profiling rules using the `custom_ops` argument. This allows for accurate profiling even with complex or non-standard architectures, which is useful when working with models like those found in the [Ultralytics models section](https://docs.ultralytics.com/models/).
101
+
102
+ ```python
103
+ import torch
104
+ import torch.nn as nn
105
+
106
+ from thop import profile
107
+
108
+
109
+ # Define your custom module
110
+ class YourCustomModule(nn.Module):
111
+ def __init__(self):
112
+ super().__init__()
113
+ # Define layers, e.g., a convolution
114
+ self.conv = nn.Conv2d(3, 64, kernel_size=3, stride=1, padding=1)
115
+
116
+ def forward(self, x):
117
+ return self.conv(x)
118
+
119
+
120
+ # Define a custom counting function for your module
121
+ # This function should calculate and return the MACs for the module's operations
122
+ def count_your_custom_module(module, x, y):
123
+ # Example: Calculate MACs for the conv layer
124
+ # Note: This is a simplified example. Real calculations depend on the module's specifics.
125
+ # MACs = output_height * output_width * kernel_height * kernel_width * in_channels * out_channels
126
+ # For simplicity, we'll just assign a placeholder value or use a helper if available
127
+ # In a real scenario, you'd implement the precise MAC calculation here.
128
+ # For nn.Conv2d, THOP usually handles it, but this demonstrates the concept.
129
+ macs = 0 # Placeholder: Implement actual MAC calculation based on module logic
130
+ # You might need access to module properties like kernel_size, stride, padding, channels etc.
131
+ # Example for a Conv2d layer (simplified):
132
+ if isinstance(module, nn.Conv2d):
133
+ _, _, H, W = y.shape # Output shape
134
+ k_h, k_w = module.kernel_size
135
+ in_c = module.in_channels
136
+ out_c = module.out_channels
137
+ groups = module.groups
138
+ macs = (k_h * k_w * in_c * out_c * H * W) / groups
139
+ module.total_ops += torch.DoubleTensor([macs]) # Accumulate MACs
140
+
141
+
142
+ # Instantiate a model containing your custom module
143
+ model = YourCustomModule() # Or a larger model incorporating this module
144
+
145
+ # Create a dummy input
146
+ dummy_input = torch.randn(1, 3, 224, 224)
147
+
148
+ # Profile the model, providing the custom operation mapping
149
+ macs, params = profile(model, inputs=(dummy_input,), custom_ops={YourCustomModule: count_your_custom_module})
150
+
151
+ print(f"Custom MACs: {macs}, Parameters: {params}")
152
+ # Expected output: Custom MACs: 87457792.0, Parameters: 1792.0
153
+ ```
154
+
155
+ ### Improve Output Readability
156
+
157
+ For clearer and more interpretable results, use the `thop.clever_format` function. This formats the raw MACs and parameter counts into human-readable strings (e.g., GigaMACs, MegaParams). This formatting helps in quickly understanding the scale of computational resources required, similar to the metrics provided in our [Ultralytics YOLOv8 documentation](https://docs.ultralytics.com/models/yolov8/).
158
+
159
+ ```python
160
+ import torch
161
+ from torchvision.models import resnet50
162
+
163
+ from thop import clever_format, profile
164
+
165
+ model = resnet50()
166
+ dummy_input = torch.randn(1, 3, 224, 224)
167
+ macs, params = profile(model, inputs=(dummy_input,))
168
+
169
+ # Format the numbers into a readable format (e.g., 4.14 GMac, 25.56 MParams)
170
+ macs_readable, params_readable = clever_format([macs, params], "%.3f")
171
+
172
+ print(f"Formatted MACs: {macs_readable}, Formatted Parameters: {params_readable}")
173
+ # Expected output: Formatted MACs: 4.140G, Formatted Parameters: 25.557M
174
+ ```
175
+
176
+ ## 📊 Results of Recent Models
177
+
178
+ The table below showcases the parameters and MACs for several popular [computer vision](https://www.ultralytics.com/glossary/computer-vision-cv) models, profiled using THOP. These benchmarks provide a comparative overview of model complexity and computational cost. You can reproduce these results by running the script located at `benchmark/evaluate_famous_models.py` in this repository. Comparing these metrics is essential for tasks like selecting models for [object detection](https://www.ultralytics.com/glossary/object-detection) or [image classification](https://www.ultralytics.com/glossary/image-classification). For more comparisons, see our [model comparison section](https://docs.ultralytics.com/compare/).
179
+
180
+ <table align="center">
181
+ <tr>
182
+ <td>
183
+
184
+ | Model | Params(M) | MACs(G) |
185
+ | ---------------- | --------- | ------- |
186
+ | alexnet | 61.10 | 0.77 |
187
+ | vgg11 | 132.86 | 7.74 |
188
+ | vgg11_bn | 132.87 | 7.77 |
189
+ | vgg13 | 133.05 | 11.44 |
190
+ | vgg13_bn | 133.05 | 11.49 |
191
+ | vgg16 | 138.36 | 15.61 |
192
+ | vgg16_bn | 138.37 | 15.66 |
193
+ | vgg19 | 143.67 | 19.77 |
194
+ | vgg19_bn | 143.68 | 19.83 |
195
+ | resnet18 | 11.69 | 1.82 |
196
+ | resnet34 | 21.80 | 3.68 |
197
+ | resnet50 | 25.56 | 4.14 |
198
+ | resnet101 | 44.55 | 7.87 |
199
+ | resnet152 | 60.19 | 11.61 |
200
+ | wide_resnet101_2 | 126.89 | 22.84 |
201
+ | wide_resnet50_2 | 68.88 | 11.46 |
202
+
203
+ </td>
204
+ <td>
205
+
206
+ | Model | Params(M) | MACs(G) |
207
+ | ------------------ | --------- | ------- |
208
+ | resnext50_32x4d | 25.03 | 4.29 |
209
+ | resnext101_32x8d | 88.79 | 16.54 |
210
+ | densenet121 | 7.98 | 2.90 |
211
+ | densenet161 | 28.68 | 7.85 |
212
+ | densenet169 | 14.15 | 3.44 |
213
+ | densenet201 | 20.01 | 4.39 |
214
+ | squeezenet1_0 | 1.25 | 0.82 |
215
+ | squeezenet1_1 | 1.24 | 0.35 |
216
+ | mnasnet0_5 | 2.22 | 0.14 |
217
+ | mnasnet0_75 | 3.17 | 0.24 |
218
+ | mnasnet1_0 | 4.38 | 0.34 |
219
+ | mnasnet1_3 | 6.28 | 0.53 |
220
+ | mobilenet_v2 | 3.50 | 0.33 |
221
+ | shufflenet_v2_x0_5 | 1.37 | 0.05 |
222
+ | shufflenet_v2_x1_0 | 2.28 | 0.15 |
223
+ | shufflenet_v2_x1_5 | 3.50 | 0.31 |
224
+ | shufflenet_v2_x2_0 | 7.39 | 0.60 |
225
+ | inception_v3 | 27.16 | 5.75 |
226
+
227
+ </td>
228
+ </tr>
229
+ </table>
230
+
231
+ ## 🙌 Contribute
232
+
233
+ We actively welcome and encourage community contributions to make THOP even better! Whether it's adding support for new [PyTorch layers](https://pytorch.org/docs/stable/nn.html), improving existing calculations, enhancing documentation, or fixing bugs, your input is valuable. Please see our [Contributing Guide](https://docs.ultralytics.com/help/contributing/) for detailed instructions on how to participate. Together, we can ensure THOP remains a state-of-the-art tool for the [machine learning](https://www.ultralytics.com/glossary/machine-learning-ml) community. Don't hesitate to share your feedback and suggestions!
234
+
235
+ ## 📜 License
236
+
237
+ THOP is distributed under the [AGPL-3.0 License](https://www.gnu.org/licenses/agpl-3.0.en.html). This license promotes open collaboration and sharing of improvements. For complete details, please refer to the [LICENSE](https://github.com/ultralytics/thop/blob/main/LICENSE) file included in the repository. Understanding the license is important before integrating THOP into your projects, especially for commercial applications which may require an [Enterprise License](https://www.ultralytics.com/license).
238
+
239
+ ## 📧 Contact
240
+
241
+ Encountered a bug or have a feature request? Please submit an issue through our [GitHub Issues](https://github.com/ultralytics/thop/issues) page. For general discussions, questions, and community support, join the vibrant Ultralytics community on our [Discord server](https://discord.com/invite/ultralytics). We look forward to hearing from you and collaborating!
242
+
243
+ <br>
244
+ <div align="center">
245
+ <a href="https://github.com/ultralytics"><img src="https://github.com/ultralytics/assets/raw/main/social/logo-social-github.png" width="3%" alt="Ultralytics GitHub"></a>
246
+ <img src="https://github.com/ultralytics/assets/raw/main/social/logo-transparent.png" width="3%" alt="space">
247
+ <a href="https://www.linkedin.com/company/ultralytics/"><img src="https://github.com/ultralytics/assets/raw/main/social/logo-social-linkedin.png" width="3%" alt="Ultralytics LinkedIn"></a>
248
+ <img src="https://github.com/ultralytics/assets/raw/main/social/logo-transparent.png" width="3%" alt="space">
249
+ <a href="https://twitter.com/ultralytics"><img src="https://github.com/ultralytics/assets/raw/main/social/logo-social-twitter.png" width="3%" alt="Ultralytics Twitter"></a>
250
+ <img src="https://github.com/ultralytics/assets/raw/main/social/logo-transparent.png" width="3%" alt="space">
251
+ <a href="https://youtube.com/ultralytics?sub_confirmation=1"><img src="https://github.com/ultralytics/assets/raw/main/social/logo-social-youtube.png" width="3%" alt="Ultralytics YouTube"></a>
252
+ <img src="https://github.com/ultralytics/assets/raw/main/social/logo-transparent.png" width="3%" alt="space">
253
+ <a href="https://www.tiktok.com/@ultralytics"><img src="https://github.com/ultralytics/assets/raw/main/social/logo-social-tiktok.png" width="3%" alt="Ultralytics TikTok"></a>
254
+ <img src="https://github.com/ultralytics/assets/raw/main/social/logo-transparent.png" width="3%" alt="space">
255
+ <a href="https://ultralytics.com/bilibili"><img src="https://github.com/ultralytics/assets/raw/main/social/logo-social-bilibili.png" width="3%" alt="Ultralytics BiliBili"></a>
256
+ <img src="https://github.com/ultralytics/assets/raw/main/social/logo-transparent.png" width="3%" alt="space">
257
+ <a href="https://discord.com/invite/ultralytics"><img src="https://github.com/ultralytics/assets/raw/main/social/logo-social-discord.png" width="3%" alt="Ultralytics Discord"></a>
258
+ </div>
@@ -0,0 +1,219 @@
1
+ <a href="https://www.ultralytics.com/"><img src="https://raw.githubusercontent.com/ultralytics/assets/main/logo/Ultralytics_Logotype_Original.svg" width="320" alt="Ultralytics logo"></a>
2
+
3
+ # 🚀 THOP: PyTorch-OpCounter
4
+
5
+ Welcome to the [THOP](https://github.com/ultralytics/thop) repository, your comprehensive solution for profiling [PyTorch](https://pytorch.org/) models by computing the number of Multiply-Accumulate Operations (MACs) and parameters. Developed by Ultralytics, this tool is essential for [deep learning](https://www.ultralytics.com/glossary/deep-learning-dl) practitioners aiming to evaluate model efficiency and performance, crucial aspects discussed in our [model training tips guide](https://docs.ultralytics.com/guides/model-training-tips/).
6
+
7
+ [![Ultralytics Actions](https://github.com/ultralytics/thop/actions/workflows/format.yml/badge.svg)](https://github.com/ultralytics/thop/actions/workflows/format.yml)
8
+ [![Ultralytics Discord](https://img.shields.io/discord/1089800235347353640?logo=discord&logoColor=white&label=Discord&color=blue)](https://discord.com/invite/ultralytics)
9
+ [![Ultralytics Forums](https://img.shields.io/discourse/users?server=https%3A%2F%2Fcommunity.ultralytics.com&logo=discourse&label=Forums&color=blue)](https://community.ultralytics.com/)
10
+ [![Ultralytics Reddit](https://img.shields.io/reddit/subreddit-subscribers/ultralytics?style=flat&logo=reddit&logoColor=white&label=Reddit&color=blue)](https://reddit.com/r/ultralytics)
11
+
12
+ ## 📄 Description
13
+
14
+ THOP offers an intuitive API designed to profile PyTorch models by calculating the total number of MACs and parameters. This functionality is vital for assessing the computational efficiency and memory footprint of deep learning models, helping developers optimize performance for deployment, especially on [edge devices](https://www.ultralytics.com/glossary/edge-ai). Understanding these metrics is key to selecting the right model architecture, a topic explored in our [model comparison pages](https://docs.ultralytics.com/compare/).
15
+
16
+ ## 📦 Installation
17
+
18
+ Get started with THOP quickly by installing it via pip:
19
+
20
+ [![PyPI - Version](https://img.shields.io/pypi/v/ultralytics-thop?logo=pypi&logoColor=white)](https://pypi.org/project/ultralytics-thop/) [![Downloads](https://static.pepy.tech/badge/ultralytics-thop)](https://www.pepy.tech/projects/ultralytics-thop) [![PyPI - Python Version](https://img.shields.io/pypi/pyversions/ultralytics-thop?logo=python&logoColor=gold)](https://pypi.org/project/ultralytics-thop/)
21
+
22
+ ```bash
23
+ pip install ultralytics-thop
24
+ ```
25
+
26
+ Alternatively, for the latest features and updates, install directly from the GitHub repository:
27
+
28
+ ```bash
29
+ pip install --upgrade git+https://github.com/ultralytics/thop.git
30
+ ```
31
+
32
+ This ensures you have the most recent version, incorporating the latest improvements and bug fixes.
33
+
34
+ ## 🛠️ How to Use
35
+
36
+ ### Basic Usage
37
+
38
+ Profiling a standard PyTorch model like [ResNet50](https://pytorch.org/vision/main/models/generated/torchvision.models.resnet50.html) is straightforward. Import the necessary libraries, load your model and a sample input tensor, then use the `profile` function:
39
+
40
+ ```python
41
+ import torch
42
+ from torchvision.models import resnet50 # Example model
43
+
44
+ from thop import profile # Import the profile function from THOP
45
+
46
+ # Load a pre-trained model (e.g., ResNet50)
47
+ model = resnet50()
48
+
49
+ # Create a dummy input tensor matching the model's expected input shape
50
+ dummy_input = torch.randn(1, 3, 224, 224)
51
+
52
+ # Profile the model
53
+ macs, params = profile(model, inputs=(dummy_input,))
54
+
55
+ print(f"MACs: {macs}, Parameters: {params}")
56
+ # Expected output: MACs: 4139975680.0, Parameters: 25557032.0
57
+ ```
58
+
59
+ ### Define Custom Rules for Third-Party Modules
60
+
61
+ If your model includes custom or third-party modules not natively supported by THOP, you can define custom profiling rules using the `custom_ops` argument. This allows for accurate profiling even with complex or non-standard architectures, which is useful when working with models like those found in the [Ultralytics models section](https://docs.ultralytics.com/models/).
62
+
63
+ ```python
64
+ import torch
65
+ import torch.nn as nn
66
+
67
+ from thop import profile
68
+
69
+
70
+ # Define your custom module
71
+ class YourCustomModule(nn.Module):
72
+ def __init__(self):
73
+ super().__init__()
74
+ # Define layers, e.g., a convolution
75
+ self.conv = nn.Conv2d(3, 64, kernel_size=3, stride=1, padding=1)
76
+
77
+ def forward(self, x):
78
+ return self.conv(x)
79
+
80
+
81
+ # Define a custom counting function for your module
82
+ # This function should calculate and return the MACs for the module's operations
83
+ def count_your_custom_module(module, x, y):
84
+ # Example: Calculate MACs for the conv layer
85
+ # Note: This is a simplified example. Real calculations depend on the module's specifics.
86
+ # MACs = output_height * output_width * kernel_height * kernel_width * in_channels * out_channels
87
+ # For simplicity, we'll just assign a placeholder value or use a helper if available
88
+ # In a real scenario, you'd implement the precise MAC calculation here.
89
+ # For nn.Conv2d, THOP usually handles it, but this demonstrates the concept.
90
+ macs = 0 # Placeholder: Implement actual MAC calculation based on module logic
91
+ # You might need access to module properties like kernel_size, stride, padding, channels etc.
92
+ # Example for a Conv2d layer (simplified):
93
+ if isinstance(module, nn.Conv2d):
94
+ _, _, H, W = y.shape # Output shape
95
+ k_h, k_w = module.kernel_size
96
+ in_c = module.in_channels
97
+ out_c = module.out_channels
98
+ groups = module.groups
99
+ macs = (k_h * k_w * in_c * out_c * H * W) / groups
100
+ module.total_ops += torch.DoubleTensor([macs]) # Accumulate MACs
101
+
102
+
103
+ # Instantiate a model containing your custom module
104
+ model = YourCustomModule() # Or a larger model incorporating this module
105
+
106
+ # Create a dummy input
107
+ dummy_input = torch.randn(1, 3, 224, 224)
108
+
109
+ # Profile the model, providing the custom operation mapping
110
+ macs, params = profile(model, inputs=(dummy_input,), custom_ops={YourCustomModule: count_your_custom_module})
111
+
112
+ print(f"Custom MACs: {macs}, Parameters: {params}")
113
+ # Expected output: Custom MACs: 87457792.0, Parameters: 1792.0
114
+ ```
115
+
116
+ ### Improve Output Readability
117
+
118
+ For clearer and more interpretable results, use the `thop.clever_format` function. This formats the raw MACs and parameter counts into human-readable strings (e.g., GigaMACs, MegaParams). This formatting helps in quickly understanding the scale of computational resources required, similar to the metrics provided in our [Ultralytics YOLOv8 documentation](https://docs.ultralytics.com/models/yolov8/).
119
+
120
+ ```python
121
+ import torch
122
+ from torchvision.models import resnet50
123
+
124
+ from thop import clever_format, profile
125
+
126
+ model = resnet50()
127
+ dummy_input = torch.randn(1, 3, 224, 224)
128
+ macs, params = profile(model, inputs=(dummy_input,))
129
+
130
+ # Format the numbers into a readable format (e.g., 4.14 GMac, 25.56 MParams)
131
+ macs_readable, params_readable = clever_format([macs, params], "%.3f")
132
+
133
+ print(f"Formatted MACs: {macs_readable}, Formatted Parameters: {params_readable}")
134
+ # Expected output: Formatted MACs: 4.140G, Formatted Parameters: 25.557M
135
+ ```
136
+
137
+ ## 📊 Results of Recent Models
138
+
139
+ The table below showcases the parameters and MACs for several popular [computer vision](https://www.ultralytics.com/glossary/computer-vision-cv) models, profiled using THOP. These benchmarks provide a comparative overview of model complexity and computational cost. You can reproduce these results by running the script located at `benchmark/evaluate_famous_models.py` in this repository. Comparing these metrics is essential for tasks like selecting models for [object detection](https://www.ultralytics.com/glossary/object-detection) or [image classification](https://www.ultralytics.com/glossary/image-classification). For more comparisons, see our [model comparison section](https://docs.ultralytics.com/compare/).
140
+
141
+ <table align="center">
142
+ <tr>
143
+ <td>
144
+
145
+ | Model | Params(M) | MACs(G) |
146
+ | ---------------- | --------- | ------- |
147
+ | alexnet | 61.10 | 0.77 |
148
+ | vgg11 | 132.86 | 7.74 |
149
+ | vgg11_bn | 132.87 | 7.77 |
150
+ | vgg13 | 133.05 | 11.44 |
151
+ | vgg13_bn | 133.05 | 11.49 |
152
+ | vgg16 | 138.36 | 15.61 |
153
+ | vgg16_bn | 138.37 | 15.66 |
154
+ | vgg19 | 143.67 | 19.77 |
155
+ | vgg19_bn | 143.68 | 19.83 |
156
+ | resnet18 | 11.69 | 1.82 |
157
+ | resnet34 | 21.80 | 3.68 |
158
+ | resnet50 | 25.56 | 4.14 |
159
+ | resnet101 | 44.55 | 7.87 |
160
+ | resnet152 | 60.19 | 11.61 |
161
+ | wide_resnet101_2 | 126.89 | 22.84 |
162
+ | wide_resnet50_2 | 68.88 | 11.46 |
163
+
164
+ </td>
165
+ <td>
166
+
167
+ | Model | Params(M) | MACs(G) |
168
+ | ------------------ | --------- | ------- |
169
+ | resnext50_32x4d | 25.03 | 4.29 |
170
+ | resnext101_32x8d | 88.79 | 16.54 |
171
+ | densenet121 | 7.98 | 2.90 |
172
+ | densenet161 | 28.68 | 7.85 |
173
+ | densenet169 | 14.15 | 3.44 |
174
+ | densenet201 | 20.01 | 4.39 |
175
+ | squeezenet1_0 | 1.25 | 0.82 |
176
+ | squeezenet1_1 | 1.24 | 0.35 |
177
+ | mnasnet0_5 | 2.22 | 0.14 |
178
+ | mnasnet0_75 | 3.17 | 0.24 |
179
+ | mnasnet1_0 | 4.38 | 0.34 |
180
+ | mnasnet1_3 | 6.28 | 0.53 |
181
+ | mobilenet_v2 | 3.50 | 0.33 |
182
+ | shufflenet_v2_x0_5 | 1.37 | 0.05 |
183
+ | shufflenet_v2_x1_0 | 2.28 | 0.15 |
184
+ | shufflenet_v2_x1_5 | 3.50 | 0.31 |
185
+ | shufflenet_v2_x2_0 | 7.39 | 0.60 |
186
+ | inception_v3 | 27.16 | 5.75 |
187
+
188
+ </td>
189
+ </tr>
190
+ </table>
191
+
192
+ ## 🙌 Contribute
193
+
194
+ We actively welcome and encourage community contributions to make THOP even better! Whether it's adding support for new [PyTorch layers](https://pytorch.org/docs/stable/nn.html), improving existing calculations, enhancing documentation, or fixing bugs, your input is valuable. Please see our [Contributing Guide](https://docs.ultralytics.com/help/contributing/) for detailed instructions on how to participate. Together, we can ensure THOP remains a state-of-the-art tool for the [machine learning](https://www.ultralytics.com/glossary/machine-learning-ml) community. Don't hesitate to share your feedback and suggestions!
195
+
196
+ ## 📜 License
197
+
198
+ THOP is distributed under the [AGPL-3.0 License](https://www.gnu.org/licenses/agpl-3.0.en.html). This license promotes open collaboration and sharing of improvements. For complete details, please refer to the [LICENSE](https://github.com/ultralytics/thop/blob/main/LICENSE) file included in the repository. Understanding the license is important before integrating THOP into your projects, especially for commercial applications which may require an [Enterprise License](https://www.ultralytics.com/license).
199
+
200
+ ## 📧 Contact
201
+
202
+ Encountered a bug or have a feature request? Please submit an issue through our [GitHub Issues](https://github.com/ultralytics/thop/issues) page. For general discussions, questions, and community support, join the vibrant Ultralytics community on our [Discord server](https://discord.com/invite/ultralytics). We look forward to hearing from you and collaborating!
203
+
204
+ <br>
205
+ <div align="center">
206
+ <a href="https://github.com/ultralytics"><img src="https://github.com/ultralytics/assets/raw/main/social/logo-social-github.png" width="3%" alt="Ultralytics GitHub"></a>
207
+ <img src="https://github.com/ultralytics/assets/raw/main/social/logo-transparent.png" width="3%" alt="space">
208
+ <a href="https://www.linkedin.com/company/ultralytics/"><img src="https://github.com/ultralytics/assets/raw/main/social/logo-social-linkedin.png" width="3%" alt="Ultralytics LinkedIn"></a>
209
+ <img src="https://github.com/ultralytics/assets/raw/main/social/logo-transparent.png" width="3%" alt="space">
210
+ <a href="https://twitter.com/ultralytics"><img src="https://github.com/ultralytics/assets/raw/main/social/logo-social-twitter.png" width="3%" alt="Ultralytics Twitter"></a>
211
+ <img src="https://github.com/ultralytics/assets/raw/main/social/logo-transparent.png" width="3%" alt="space">
212
+ <a href="https://youtube.com/ultralytics?sub_confirmation=1"><img src="https://github.com/ultralytics/assets/raw/main/social/logo-social-youtube.png" width="3%" alt="Ultralytics YouTube"></a>
213
+ <img src="https://github.com/ultralytics/assets/raw/main/social/logo-transparent.png" width="3%" alt="space">
214
+ <a href="https://www.tiktok.com/@ultralytics"><img src="https://github.com/ultralytics/assets/raw/main/social/logo-social-tiktok.png" width="3%" alt="Ultralytics TikTok"></a>
215
+ <img src="https://github.com/ultralytics/assets/raw/main/social/logo-transparent.png" width="3%" alt="space">
216
+ <a href="https://ultralytics.com/bilibili"><img src="https://github.com/ultralytics/assets/raw/main/social/logo-social-bilibili.png" width="3%" alt="Ultralytics BiliBili"></a>
217
+ <img src="https://github.com/ultralytics/assets/raw/main/social/logo-transparent.png" width="3%" alt="space">
218
+ <a href="https://discord.com/invite/ultralytics"><img src="https://github.com/ultralytics/assets/raw/main/social/logo-social-discord.png" width="3%" alt="Ultralytics Discord"></a>
219
+ </div>
@@ -1,21 +1,22 @@
1
- # Ultralytics YOLO 🚀, AGPL-3.0 license
1
+ # Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license
2
+
2
3
  # Publish pip package to PyPI https://pypi.org/project/ultralytics/ and Docs to https://docs.ultralytics.com
3
- #
4
+
4
5
  # Overview:
5
6
  # This `pyproject.toml` file manages the build, packaging, and distribution of the `thop` library.
6
7
  # It defines essential project metadata, dependencies, and settings used to develop and deploy the library.
7
- #
8
+
8
9
  # Key Sections:
9
10
  # - `[build-system]`: Specifies the build requirements and backend (e.g., setuptools, wheel).
10
11
  # - `[project]`: Includes details like name, version, description, authors, dependencies, and more.
11
12
  # - `[tool.setuptools.dynamic]`: Configures dynamic versioning for the project.
12
13
  # - `[tool.setuptools]`: Configures settings specific to the `setuptools` build backend.
13
- #
14
+
14
15
  # Installation:
15
16
  # The `thop` library can be installed using the command: `pip install ultralytics-thop`
16
17
  # For development purposes, you can install the package in editable mode with: `pip install -e .`
17
18
  # This approach allows for real-time code modifications without the need for re-installation.
18
- #
19
+
19
20
  # Documentation:
20
21
  # For comprehensive documentation and usage instructions, visit: https://github.com/ultralytics/thop
21
22
 
@@ -1,3 +1,5 @@
1
+ # Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license
2
+
1
3
  import torch
2
4
  import torch.nn as nn
3
5
 
@@ -54,6 +56,6 @@ class TestUtils:
54
56
 
55
57
  flops, params = profile(net, inputs=(data,))
56
58
  print(flops, params)
57
- assert (
58
- flops == n * out_c * oh * ow // g * in_c * kh * kw
59
- ), f"{flops} v.s. {n * out_c * oh * ow // g * in_c * kh * kw}"
59
+ assert flops == n * out_c * oh * ow // g * in_c * kh * kw, (
60
+ f"{flops} v.s. {n * out_c * oh * ow // g * in_c * kh * kw}"
61
+ )
@@ -1,3 +1,5 @@
1
+ # Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license
2
+
1
3
  import torch
2
4
  import torch.nn as nn
3
5
 
@@ -1,3 +1,5 @@
1
+ # Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license
2
+
1
3
  import torch
2
4
  import torch.nn as nn
3
5
 
@@ -1,3 +1,5 @@
1
+ # Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license
2
+
1
3
  from thop import utils
2
4
 
3
5
 
@@ -1,4 +1,6 @@
1
- __version__ = "2.0.13"
1
+ # Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license
2
+
3
+ __version__ = "2.0.15"
2
4
 
3
5
 
4
6
  import torch
@@ -1,3 +1,5 @@
1
+ # Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license
2
+
1
3
  import logging
2
4
  from distutils.version import LooseVersion
3
5
 
@@ -7,8 +9,7 @@ import torch.nn as nn
7
9
 
8
10
  if LooseVersion(torch.__version__) < LooseVersion("1.8.0"):
9
11
  logging.warning(
10
- f"torch.fx requires version higher than 1.8.0. "
11
- f"But You are using an old version PyTorch {torch.__version__}. "
12
+ f"torch.fx requires version higher than 1.8.0. But You are using an old version PyTorch {torch.__version__}. "
12
13
  )
13
14
 
14
15
 
@@ -1,3 +1,5 @@
1
+ # Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license
2
+
1
3
  from thop.rnn_hooks import *
2
4
  from thop.vision.basic_hooks import *
3
5
 
@@ -1,3 +1,5 @@
1
+ # Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license
2
+
1
3
  import torch
2
4
  import torch.nn as nn
3
5
  from torch.nn.utils.rnn import PackedSequence
@@ -1,3 +1,5 @@
1
+ # Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license
2
+
1
3
  from collections.abc import Iterable
2
4
 
3
5
  COLOR_RED = "91m"
@@ -0,0 +1 @@
1
+ # Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license
@@ -1,3 +1,5 @@
1
+ # Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license
2
+
1
3
  import logging
2
4
 
3
5
  import torch.nn as nn
@@ -125,7 +127,8 @@ def count_upsample(m, x, y):
125
127
  "linear",
126
128
  "bilinear",
127
129
  "bicubic",
128
- ): # "trilinear"
130
+ "trilinear",
131
+ ):
129
132
  logging.warning(f"mode {m.mode} is not implemented yet, take it a zero op")
130
133
  m.total_ops += 0
131
134
  else:
@@ -1,3 +1,5 @@
1
+ # Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license
2
+
1
3
  import warnings
2
4
 
3
5
  import numpy as np