Skip to content

Commit b88b906

Browse files
committed
cosmetics
1 parent fd12d6a commit b88b906

File tree

2 files changed

+26
-53
lines changed

2 files changed

+26
-53
lines changed

benchmarks/benchmark_transforms.py

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,19 +28,22 @@
2828

2929
try:
3030
import cv2
31+
3132
HAS_OPENCV = True
3233
except ImportError:
3334
HAS_OPENCV = False
3435

3536
try:
3637
import albumentations as A
38+
3739
HAS_ALBUMENTATIONS = True
3840
except ImportError:
3941
HAS_ALBUMENTATIONS = False
4042

4143
try:
4244
import kornia as K
4345
import kornia.augmentation as KA
46+
4447
HAS_KORNIA = True
4548
except ImportError:
4649
HAS_KORNIA = False
@@ -52,8 +55,6 @@
5255
NORM_STD = [0.229, 0.224, 0.225]
5356

5457

55-
56-
5758
def torchvision_pipeline(images: torch.Tensor, target_size: int) -> torch.Tensor:
5859
images = F.resize(
5960
images, size=(target_size, target_size), interpolation=F.InterpolationMode.BILINEAR, antialias=True
@@ -110,13 +111,15 @@ def kornia_pipeline(image: torch.Tensor, target_size: int) -> torch.Tensor:
110111

111112
def run_benchmark(args) -> Dict[str, Any]:
112113
backend = args.backend.lower()
113-
114+
114115
device = args.device.lower()
115116
# Check device compatibility
116-
if device == 'cuda' and backend not in ['tv', 'tv-compiled']:
117-
raise RuntimeError(f"CUDA device not supported for {backend} backend. Only 'tv' and 'tv-compiled' support CUDA.")
118-
119-
if device == 'cuda' and not torch.cuda.is_available():
117+
if device == "cuda" and backend not in ["tv", "tv-compiled"]:
118+
raise RuntimeError(
119+
f"CUDA device not supported for {backend} backend. Only 'tv' and 'tv-compiled' support CUDA."
120+
)
121+
122+
if device == "cuda" and not torch.cuda.is_available():
120123
raise RuntimeError("CUDA not available. Install cuda-enabled torch and torchvision, or use 'cpu' device.")
121124

122125
if backend == "opencv" and not HAS_OPENCV:
@@ -161,9 +164,9 @@ def generate_test_images():
161164
memory_format = torch.channels_last if args.contiguity == "CL" else torch.contiguous_format
162165
if memory_format == torch.channels_last:
163166
images = images.to(memory_format=torch.channels_last)
164-
167+
165168
# Move to device for torchvision backends
166-
if backend in ['tv', 'tv-compiled']:
169+
if backend in ["tv", "tv-compiled"]:
167170
images = images.to(device)
168171

169172
if args.batch_size == 1:
@@ -203,8 +206,6 @@ def generate_test_images():
203206
return {"backend": args.backend, "stats": stats}
204207

205208

206-
207-
208209
def main():
209210
parser = argparse.ArgumentParser(description="Benchmark torchvision transforms")
210211
parser.add_argument("--num-exp", type=int, default=100, help="Number of experiments we average over")
@@ -233,7 +234,7 @@ def main():
233234
"--backend", type=str.lower, choices=all_backends + ["all"], default="all", help="Backend to benchmark"
234235
)
235236
parser.add_argument("-v", "--verbose", action="store_true", help="Enable verbose output")
236-
parser.add_argument("--device", type=str, default="cpu", help="Device to use: cpu, cuda, or gpu (default: cpu)")
237+
parser.add_argument("--device", type=str, default="cpu", help="Device to use: cpu or cuda (default: cpu)")
237238

238239
args = parser.parse_args()
239240

benchmarks/utils.py

Lines changed: 13 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,21 @@
1010

1111
try:
1212
import cv2
13+
1314
HAS_OPENCV = True
1415
except ImportError:
1516
HAS_OPENCV = False
1617

1718
try:
1819
import albumentations as A
20+
1921
HAS_ALBUMENTATIONS = True
2022
except ImportError:
2123
HAS_ALBUMENTATIONS = False
2224

2325
try:
2426
import kornia as K
27+
2528
HAS_KORNIA = True
2629
except ImportError:
2730
HAS_KORNIA = False
@@ -108,17 +111,9 @@ def print_comparison_table(results: List[Dict[str, Any]]) -> None:
108111
def print_benchmark_info(args):
109112
"""Print benchmark configuration and library versions."""
110113
device = args.device.lower()
111-
if device in ['gpu', 'cuda']:
112-
device = 'cuda'
113-
else:
114-
device = 'cpu'
115-
116-
memory_format = 'channels_last' if args.contiguity == 'CL' else 'channels_first'
117-
118-
print("=" * 80)
119-
print("BENCHMARK CONFIGURATION")
120-
print("=" * 80)
121-
114+
115+
memory_format = "channels_last" if args.contiguity == "CL" else "channels_first"
116+
122117
# Collect configuration info
123118
config = [
124119
["Device", device],
@@ -128,41 +123,18 @@ def print_benchmark_info(args):
128123
["Experiments", f"{args.num_exp} (+ {args.warmup} warmup)"],
129124
["Input → output size", f"{args.min_size}-{args.max_size}{args.target_size}×{args.target_size}"],
130125
]
131-
126+
132127
print(tabulate(config, headers=["Parameter", "Value"], tablefmt="simple"))
133128
print()
134-
135-
print("=" * 80)
136-
print("LIBRARY VERSIONS")
137-
print("=" * 80)
138-
129+
139130
# Collect library versions
140131
versions = [
141132
["PyTorch", torch.__version__],
142133
["TorchVision", torchvision.__version__],
134+
["OpenCV", cv2.__version__ if HAS_OPENCV else "Not available"],
135+
["PIL/Pillow", getattr(Image, '__version__', "Version unavailable")],
136+
["Albumentations", A.__version__ if HAS_ALBUMENTATIONS else "Not available"],
137+
["Kornia", K.__version__ if HAS_KORNIA else "Not available"],
143138
]
144-
145-
if HAS_OPENCV:
146-
versions.append(["OpenCV", cv2.__version__])
147-
else:
148-
versions.append(["OpenCV", "Not available"])
149-
150-
# PIL version
151-
try:
152-
versions.append(["PIL/Pillow", Image.__version__])
153-
except AttributeError:
154-
versions.append(["PIL/Pillow", "Version unavailable"])
155-
156-
if HAS_ALBUMENTATIONS:
157-
versions.append(["Albumentations", A.__version__])
158-
else:
159-
versions.append(["Albumentations", "Not available"])
160-
161-
if HAS_KORNIA:
162-
versions.append(["Kornia", K.__version__])
163-
else:
164-
versions.append(["Kornia", "Not available"])
165-
139+
166140
print(tabulate(versions, headers=["Library", "Version"], tablefmt="simple"))
167-
print("=" * 80)
168-
print()

0 commit comments

Comments
 (0)