From af86310748e4d9b9e3493a5f0bbfd966f8a528ae Mon Sep 17 00:00:00 2001 From: Glenn Jocher Date: Thu, 20 Jun 2024 22:35:01 +0200 Subject: [PATCH 1/2] Refactor code for speed and clarity --- README.md | 4 ++-- benchmark/evaluate_famous_models.py | 3 +-- benchmark/evaluate_rnn_models.py | 1 - tests/test_conv2d.py | 1 - tests/test_matmul.py | 1 - tests/test_relu.py | 1 - thop/vision/basic_hooks.py | 2 +- 7 files changed, 4 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 1c2fefc..3cfa21e 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@
- +Ultralytics logo # 🚀 THOP: PyTorch-OpCounter @@ -154,7 +154,7 @@ For bugs or feature requests, please open an issue on [GitHub Issues](https://gi space Ultralytics TikTok space - Ultralytics Instagram + Ultralytics Instagram space Ultralytics Discord diff --git a/benchmark/evaluate_famous_models.py b/benchmark/evaluate_famous_models.py index f641bda..f36615c 100644 --- a/benchmark/evaluate_famous_models.py +++ b/benchmark/evaluate_famous_models.py @@ -1,7 +1,6 @@ import torch -from torchvision import models - from thop.profile import profile +from torchvision import models model_names = sorted( name diff --git a/benchmark/evaluate_rnn_models.py b/benchmark/evaluate_rnn_models.py index d1fad55..2c5060a 100644 --- a/benchmark/evaluate_rnn_models.py +++ b/benchmark/evaluate_rnn_models.py @@ -1,6 +1,5 @@ import torch import torch.nn as nn - from thop.profile import profile input_size = 160 diff --git a/tests/test_conv2d.py b/tests/test_conv2d.py index e058e25..6e3f974 100644 --- a/tests/test_conv2d.py +++ b/tests/test_conv2d.py @@ -1,6 +1,5 @@ import torch import torch.nn as nn - from thop import profile diff --git a/tests/test_matmul.py b/tests/test_matmul.py index 83cc361..e0688b2 100644 --- a/tests/test_matmul.py +++ b/tests/test_matmul.py @@ -1,6 +1,5 @@ import torch import torch.nn as nn - from thop import profile diff --git a/tests/test_relu.py b/tests/test_relu.py index 88739e6..ec916a6 100644 --- a/tests/test_relu.py +++ b/tests/test_relu.py @@ -1,6 +1,5 @@ import torch import torch.nn as nn - from thop import profile diff --git a/thop/vision/basic_hooks.py b/thop/vision/basic_hooks.py index 9ee8975..f4dfce3 100644 --- a/thop/vision/basic_hooks.py +++ b/thop/vision/basic_hooks.py @@ -21,7 +21,7 @@ def zero_ops(m, x, y): def count_convNd(m: _ConvNd, x, y: torch.Tensor): """Calculate and add the number of convolutional operations (FLOPs) to the model's total operations count.""" x = x[0] - + m.total_ops += calculate_conv2d_flops( input_size=list(x.shape), output_size=list(y.shape), From 9885efd3667dec1918b407f5edaf4dda2f4af2c0 Mon Sep 17 00:00:00 2001 From: UltralyticsAssistant Date: Thu, 20 Jun 2024 20:35:29 +0000 Subject: [PATCH 2/2] Auto-format by https://ultralytics.com/actions --- README.md | 3 ++- benchmark/evaluate_famous_models.py | 3 ++- benchmark/evaluate_rnn_models.py | 1 + tests/test_conv2d.py | 1 + tests/test_matmul.py | 1 + tests/test_relu.py | 1 + 6 files changed, 8 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 3cfa21e..7267aff 100644 --- a/README.md +++ b/README.md @@ -34,9 +34,10 @@ pip install --upgrade git+https://github.com/ultralytics/thop.git To profile a model, you can use the following example: ```python +import torch from torchvision.models import resnet50 + from thop import profile -import torch model = resnet50() input = torch.randn(1, 3, 224, 224) diff --git a/benchmark/evaluate_famous_models.py b/benchmark/evaluate_famous_models.py index f36615c..f641bda 100644 --- a/benchmark/evaluate_famous_models.py +++ b/benchmark/evaluate_famous_models.py @@ -1,7 +1,8 @@ import torch -from thop.profile import profile from torchvision import models +from thop.profile import profile + model_names = sorted( name for name in models.__dict__ diff --git a/benchmark/evaluate_rnn_models.py b/benchmark/evaluate_rnn_models.py index 2c5060a..d1fad55 100644 --- a/benchmark/evaluate_rnn_models.py +++ b/benchmark/evaluate_rnn_models.py @@ -1,5 +1,6 @@ import torch import torch.nn as nn + from thop.profile import profile input_size = 160 diff --git a/tests/test_conv2d.py b/tests/test_conv2d.py index 6e3f974..e058e25 100644 --- a/tests/test_conv2d.py +++ b/tests/test_conv2d.py @@ -1,5 +1,6 @@ import torch import torch.nn as nn + from thop import profile diff --git a/tests/test_matmul.py b/tests/test_matmul.py index e0688b2..83cc361 100644 --- a/tests/test_matmul.py +++ b/tests/test_matmul.py @@ -1,5 +1,6 @@ import torch import torch.nn as nn + from thop import profile diff --git a/tests/test_relu.py b/tests/test_relu.py index ec916a6..88739e6 100644 --- a/tests/test_relu.py +++ b/tests/test_relu.py @@ -1,5 +1,6 @@ import torch import torch.nn as nn + from thop import profile