11import logging
22import warnings
3- from typing import Dict , Tuple
43
54import torch
65
1817CUDA_AVAILABLE = False
1918if torch .cuda .is_available ():
2019 try :
21- import torch_tensorrt
20+ import torch_tensorrt # noqa: F401
21+
2222 CUDA_AVAILABLE = True
2323 except ImportError :
2424 print ("torch-tensorrt not installed. Running in CPU mode only." )
2525
2626
27- def _run_onnx_inference (args , model_loader , img_batch ) -> Dict [str , Tuple [float , float ]]:
27+ def _run_onnx_inference (args , model_loader , img_batch ) -> dict [str , tuple [float , float ]]:
2828 onnx_inference = ONNXInference (model_loader , args .onnx_path , debug_mode = args .DEBUG )
2929 benchmark_result = onnx_inference .benchmark (img_batch )
3030 onnx_inference .predict (img_batch )
3131 return {"ONNX (CPU)" : benchmark_result }
3232
3333
34- def _run_openvino_inference (args , model_loader , img_batch ) -> Dict [str , Tuple [float , float ]]:
34+ def _run_openvino_inference (args , model_loader , img_batch ) -> dict [str , tuple [float , float ]]:
3535 ov_inference = OVInference (model_loader , args .ov_path , debug_mode = args .DEBUG )
3636 benchmark_result = ov_inference .benchmark (img_batch )
3737 ov_inference .predict (img_batch )
3838 return {"OpenVINO (CPU)" : benchmark_result }
3939
4040
41- def _run_pytorch_cpu_inference (args , model_loader , img_batch ) -> Dict [str , Tuple [float , float ]]:
41+ def _run_pytorch_cpu_inference (args , model_loader , img_batch ) -> dict [str , tuple [float , float ]]:
4242 pytorch_cpu_inference = PyTorchInference (model_loader , device = "cpu" , debug_mode = args .DEBUG )
4343 benchmark_result = pytorch_cpu_inference .benchmark (img_batch )
4444 pytorch_cpu_inference .predict (img_batch )
4545 return {"PyTorch (CPU)" : benchmark_result }
4646
4747
48- def _run_pytorch_cuda_inference (args , model_loader , device , img_batch ) -> Dict [str , Tuple [float , float ]]:
48+ def _run_pytorch_cuda_inference (
49+ args , model_loader , device , img_batch
50+ ) -> dict [str , tuple [float , float ]]:
4951 print ("Running CUDA inference..." )
5052 pytorch_cuda_inference = PyTorchInference (model_loader , device = device , debug_mode = args .DEBUG )
5153 benchmark_result = pytorch_cuda_inference .benchmark (img_batch )
5254 pytorch_cuda_inference .predict (img_batch )
5355 return {"PyTorch (CUDA)" : benchmark_result }
5456
5557
56- def _run_tensorrt_inference (args , model_loader , device , img_batch ) -> Dict [str , Tuple [float , float ]]:
58+ def _run_tensorrt_inference (
59+ args , model_loader , device , img_batch
60+ ) -> dict [str , tuple [float , float ]]:
5761 results = {}
5862 precisions = [torch .float16 , torch .float32 ]
59-
63+
6064 for precision in precisions :
6165 tensorrt_inference = TensorRTInference (
6266 model_loader , device = device , precision = precision , debug_mode = args .DEBUG
6367 )
6468 benchmark_result = tensorrt_inference .benchmark (img_batch )
6569 tensorrt_inference .predict (img_batch )
6670 results [f"TRT_{ precision } " ] = benchmark_result
67-
71+
6872 return results
6973
7074
@@ -76,7 +80,7 @@ def main():
7680
7781 benchmark_results = {}
7882 device = torch .device ("cuda" if torch .cuda .is_available () else "cpu" )
79-
83+
8084 model_loader = ModelLoader (device = device )
8185 img_processor = ImageProcessor (img_path = args .image_path , device = device )
8286 img_batch = img_processor .process_image ()
@@ -92,7 +96,9 @@ def main():
9296
9397 if torch .cuda .is_available ():
9498 if args .mode in ["cuda" , "all" ]:
95- benchmark_results .update (_run_pytorch_cuda_inference (args , model_loader , device , img_batch ))
99+ benchmark_results .update (
100+ _run_pytorch_cuda_inference (args , model_loader , device , img_batch )
101+ )
96102
97103 if args .mode in ["tensorrt" , "all" ]:
98104 benchmark_results .update (_run_tensorrt_inference (args , model_loader , device , img_batch ))
0 commit comments