|
28 | 28 |
|
29 | 29 | try: |
30 | 30 | import cv2 |
| 31 | + |
31 | 32 | HAS_OPENCV = True |
32 | 33 | except ImportError: |
33 | 34 | HAS_OPENCV = False |
34 | 35 |
|
35 | 36 | try: |
36 | 37 | import albumentations as A |
| 38 | + |
37 | 39 | HAS_ALBUMENTATIONS = True |
38 | 40 | except ImportError: |
39 | 41 | HAS_ALBUMENTATIONS = False |
40 | 42 |
|
41 | 43 | try: |
42 | 44 | import kornia as K |
43 | 45 | import kornia.augmentation as KA |
| 46 | + |
44 | 47 | HAS_KORNIA = True |
45 | 48 | except ImportError: |
46 | 49 | HAS_KORNIA = False |
|
52 | 55 | NORM_STD = [0.229, 0.224, 0.225] |
53 | 56 |
|
54 | 57 |
|
55 | | - |
56 | | - |
57 | 58 | def torchvision_pipeline(images: torch.Tensor, target_size: int) -> torch.Tensor: |
58 | 59 | images = F.resize( |
59 | 60 | 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: |
110 | 111 |
|
111 | 112 | def run_benchmark(args) -> Dict[str, Any]: |
112 | 113 | backend = args.backend.lower() |
113 | | - |
| 114 | + |
114 | 115 | device = args.device.lower() |
115 | 116 | # 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(): |
120 | 123 | raise RuntimeError("CUDA not available. Install cuda-enabled torch and torchvision, or use 'cpu' device.") |
121 | 124 |
|
122 | 125 | if backend == "opencv" and not HAS_OPENCV: |
@@ -161,9 +164,9 @@ def generate_test_images(): |
161 | 164 | memory_format = torch.channels_last if args.contiguity == "CL" else torch.contiguous_format |
162 | 165 | if memory_format == torch.channels_last: |
163 | 166 | images = images.to(memory_format=torch.channels_last) |
164 | | - |
| 167 | + |
165 | 168 | # Move to device for torchvision backends |
166 | | - if backend in ['tv', 'tv-compiled']: |
| 169 | + if backend in ["tv", "tv-compiled"]: |
167 | 170 | images = images.to(device) |
168 | 171 |
|
169 | 172 | if args.batch_size == 1: |
@@ -203,8 +206,6 @@ def generate_test_images(): |
203 | 206 | return {"backend": args.backend, "stats": stats} |
204 | 207 |
|
205 | 208 |
|
206 | | - |
207 | | - |
208 | 209 | def main(): |
209 | 210 | parser = argparse.ArgumentParser(description="Benchmark torchvision transforms") |
210 | 211 | parser.add_argument("--num-exp", type=int, default=100, help="Number of experiments we average over") |
@@ -233,7 +234,7 @@ def main(): |
233 | 234 | "--backend", type=str.lower, choices=all_backends + ["all"], default="all", help="Backend to benchmark" |
234 | 235 | ) |
235 | 236 | 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)") |
237 | 238 |
|
238 | 239 | args = parser.parse_args() |
239 | 240 |
|
|
0 commit comments