19
19
Requirements:
20
20
$ pip install -r requirements.txt coremltools onnx onnx-simplifier onnxruntime openvino-dev tensorflow-cpu # CPU
21
21
$ 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
22
23
23
24
Usage:
24
25
$ python utils/benchmarks.py --weights yolov5s.pt --img 640
41
42
import val
42
43
from utils import notebook_init
43
44
from utils .general import LOGGER , print_args
45
+ from utils .torch_utils import select_device
44
46
45
47
46
48
def run (weights = ROOT / 'yolov5s.pt' , # weights path
47
49
imgsz = 640 , # inference size (pixels)
48
50
batch_size = 1 , # batch size
49
51
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
50
54
):
51
55
y , t = [], time .time ()
52
56
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)
54
59
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
56
66
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 )
58
68
metrics = result [0 ] # metrics (mp, mr, map50, map, *losses(box, obj, cls))
59
69
speeds = result [2 ] # times (preprocess, inference, postprocess)
60
70
y .append ([name , metrics [3 ], speeds [1 ]]) # mAP, t_inference
@@ -78,6 +88,8 @@ def parse_opt():
78
88
parser .add_argument ('--imgsz' , '--img' , '--img-size' , type = int , default = 640 , help = 'inference size (pixels)' )
79
89
parser .add_argument ('--batch-size' , type = int , default = 1 , help = 'batch size' )
80
90
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' )
81
93
opt = parser .parse_args ()
82
94
print_args (FILE .stem , opt )
83
95
return opt
0 commit comments