glenn-jocher commited on
Commit
698a5d7
·
unverified ·
1 Parent(s): 446e6f5

Add `python benchmarks.py --test` for export-only (#7350)

Browse files
Files changed (1) hide show
  1. utils/benchmarks.py +41 -3
utils/benchmarks.py CHANGED
@@ -52,20 +52,26 @@ def run(
52
  data=ROOT / 'data/coco128.yaml', # dataset.yaml path
53
  device='', # cuda device, i.e. 0 or 0,1,2,3 or cpu
54
  half=False, # use FP16 half-precision inference
 
55
  ):
56
  y, t = [], time.time()
57
  formats = export.export_formats()
58
  device = select_device(device)
59
  for i, (name, f, suffix, gpu) in formats.iterrows(): # index, (name, file, suffix, gpu-capable)
60
  try:
61
- assert i < 9, 'Edge TPU and TF.js not supported'
 
62
  if device.type != 'cpu':
63
  assert gpu, f'{name} inference not supported on GPU'
 
 
64
  if f == '-':
65
  w = weights # PyTorch format
66
  else:
67
  w = export.run(weights=weights, imgsz=[imgsz], include=[f], device=device, half=half)[-1] # all others
68
  assert suffix in str(w), 'export failed'
 
 
69
  result = val.run(data, w, batch_size, imgsz, plots=False, device=device, task='benchmark', half=half)
70
  metrics = result[0] # metrics (mp, mr, map50, map, *losses(box, obj, cls))
71
  speeds = result[2] # times (preprocess, inference, postprocess)
@@ -78,8 +84,39 @@ def run(
78
  LOGGER.info('\n')
79
  parse_opt()
80
  notebook_init() # print system info
81
- py = pd.DataFrame(y, columns=['Format', 'mAP@0.5:0.95', 'Inference time (ms)'])
82
  LOGGER.info(f'\nBenchmarks complete ({time.time() - t:.2f}s)')
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
83
  LOGGER.info(str(py))
84
  return py
85
 
@@ -92,13 +129,14 @@ def parse_opt():
92
  parser.add_argument('--data', type=str, default=ROOT / 'data/coco128.yaml', help='dataset.yaml path')
93
  parser.add_argument('--device', default='', help='cuda device, i.e. 0 or 0,1,2,3 or cpu')
94
  parser.add_argument('--half', action='store_true', help='use FP16 half-precision inference')
 
95
  opt = parser.parse_args()
96
  print_args(vars(opt))
97
  return opt
98
 
99
 
100
  def main(opt):
101
- run(**vars(opt))
102
 
103
 
104
  if __name__ == "__main__":
 
52
  data=ROOT / 'data/coco128.yaml', # dataset.yaml path
53
  device='', # cuda device, i.e. 0 or 0,1,2,3 or cpu
54
  half=False, # use FP16 half-precision inference
55
+ test=False, # test exports only
56
  ):
57
  y, t = [], time.time()
58
  formats = export.export_formats()
59
  device = select_device(device)
60
  for i, (name, f, suffix, gpu) in formats.iterrows(): # index, (name, file, suffix, gpu-capable)
61
  try:
62
+ assert i != 9, 'Edge TPU not supported'
63
+ assert i != 10, 'TF.js not supported'
64
  if device.type != 'cpu':
65
  assert gpu, f'{name} inference not supported on GPU'
66
+
67
+ # Export
68
  if f == '-':
69
  w = weights # PyTorch format
70
  else:
71
  w = export.run(weights=weights, imgsz=[imgsz], include=[f], device=device, half=half)[-1] # all others
72
  assert suffix in str(w), 'export failed'
73
+
74
+ # Validate
75
  result = val.run(data, w, batch_size, imgsz, plots=False, device=device, task='benchmark', half=half)
76
  metrics = result[0] # metrics (mp, mr, map50, map, *losses(box, obj, cls))
77
  speeds = result[2] # times (preprocess, inference, postprocess)
 
84
  LOGGER.info('\n')
85
  parse_opt()
86
  notebook_init() # print system info
87
+ py = pd.DataFrame(y, columns=['Format', 'mAP@0.5:0.95', 'Inference time (ms)'] if map else ['Format', 'Export', ''])
88
  LOGGER.info(f'\nBenchmarks complete ({time.time() - t:.2f}s)')
89
+ LOGGER.info(str(py if map else py.iloc[:, :2]))
90
+ return py
91
+
92
+
93
+ def test(
94
+ weights=ROOT / 'yolov5s.pt', # weights path
95
+ imgsz=640, # inference size (pixels)
96
+ batch_size=1, # batch size
97
+ data=ROOT / 'data/coco128.yaml', # dataset.yaml path
98
+ device='', # cuda device, i.e. 0 or 0,1,2,3 or cpu
99
+ half=False, # use FP16 half-precision inference
100
+ test=False, # test exports only
101
+ ):
102
+ y, t = [], time.time()
103
+ formats = export.export_formats()
104
+ device = select_device(device)
105
+ for i, (name, f, suffix, gpu) in formats.iterrows(): # index, (name, file, suffix, gpu-capable)
106
+ try:
107
+ w = weights if f == '-' else \
108
+ export.run(weights=weights, imgsz=[imgsz], include=[f], device=device, half=half)[-1] # weights
109
+ assert suffix in str(w), 'export failed'
110
+ y.append([name, True])
111
+ except Exception:
112
+ y.append([name, False]) # mAP, t_inference
113
+
114
+ # Print results
115
+ LOGGER.info('\n')
116
+ parse_opt()
117
+ notebook_init() # print system info
118
+ py = pd.DataFrame(y, columns=['Format', 'Export'])
119
+ LOGGER.info(f'\nExports complete ({time.time() - t:.2f}s)')
120
  LOGGER.info(str(py))
121
  return py
122
 
 
129
  parser.add_argument('--data', type=str, default=ROOT / 'data/coco128.yaml', help='dataset.yaml path')
130
  parser.add_argument('--device', default='', help='cuda device, i.e. 0 or 0,1,2,3 or cpu')
131
  parser.add_argument('--half', action='store_true', help='use FP16 half-precision inference')
132
+ parser.add_argument('--test', action='store_true', help='test exports only')
133
  opt = parser.parse_args()
134
  print_args(vars(opt))
135
  return opt
136
 
137
 
138
  def main(opt):
139
+ test(**vars(opt)) if opt.test else run(**vars(opt))
140
 
141
 
142
  if __name__ == "__main__":