1919Requirements:
2020 $ pip install -r requirements.txt coremltools onnx onnx-simplifier onnxruntime openvino-dev tensorflow-cpu # CPU
2121 $ pip install -r requirements.txt coremltools onnx onnx-simplifier onnxruntime-gpu openvino-dev tensorflow # GPU
22+ $ pip install -U nvidia-tensorrt --index-url https://pypi.ngc.nvidia.com # TensorRT
2223
2324Usage:
2425 $ python utils/benchmarks.py --weights yolov5s.pt --img 640
4142import val
4243from utils import notebook_init
4344from utils .general import LOGGER , print_args
45+ from utils .torch_utils import select_device
4446
4547
4648def run (weights = ROOT / 'yolov5s.pt' , # weights path
4749 imgsz = 640 , # inference size (pixels)
4850 batch_size = 1 , # batch size
4951 data = ROOT / 'data/coco128.yaml' , # dataset.yaml path
52+ device = '' , # cuda device, i.e. 0 or 0,1,2,3 or cpu
53+ half = False , # use FP16 half-precision inference
5054 ):
5155 y , t = [], time .time ()
5256 formats = export .export_formats ()
53- for i , (name , f , suffix ) in formats .iterrows (): # index, (name, file, suffix)
57+ device = select_device (device )
58+ for i , (name , f , suffix , gpu ) in formats .iterrows (): # index, (name, file, suffix, gpu-capable)
5459 try :
55- w = weights if f == '-' else export .run (weights = weights , imgsz = [imgsz ], include = [f ], device = 'cpu' )[- 1 ]
60+ if device .type != 'cpu' :
61+ assert gpu , f'{ name } inference not supported on GPU'
62+ if f == '-' :
63+ w = weights # PyTorch format
64+ else :
65+ w = export .run (weights = weights , imgsz = [imgsz ], include = [f ], device = device , half = half )[- 1 ] # all others
5666 assert suffix in str (w ), 'export failed'
57- result = val .run (data , w , batch_size , imgsz = imgsz , plots = False , device = 'cpu' , task = 'benchmark' )
67+ result = val .run (data , w , batch_size , imgsz , plots = False , device = device , task = 'benchmark' , half = half )
5868 metrics = result [0 ] # metrics (mp, mr, map50, map, *losses(box, obj, cls))
5969 speeds = result [2 ] # times (preprocess, inference, postprocess)
6070 y .append ([name , metrics [3 ], speeds [1 ]]) # mAP, t_inference
@@ -78,6 +88,8 @@ def parse_opt():
7888 parser .add_argument ('--imgsz' , '--img' , '--img-size' , type = int , default = 640 , help = 'inference size (pixels)' )
7989 parser .add_argument ('--batch-size' , type = int , default = 1 , help = 'batch size' )
8090 parser .add_argument ('--data' , type = str , default = ROOT / 'data/coco128.yaml' , help = 'dataset.yaml path' )
91+ parser .add_argument ('--device' , default = '' , help = 'cuda device, i.e. 0 or 0,1,2,3 or cpu' )
92+ parser .add_argument ('--half' , action = 'store_true' , help = 'use FP16 half-precision inference' )
8193 opt = parser .parse_args ()
8294 print_args (FILE .stem , opt )
8395 return opt
0 commit comments