benchmarks.py 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. # YOLOv5 🚀 by Ultralytics, AGPL-3.0 license
  2. """
  3. Run YOLOv5 benchmarks on all supported export formats.
  4. Format | `export.py --include` | Model
  5. --- | --- | ---
  6. PyTorch | - | yolov5s.pt
  7. TorchScript | `torchscript` | yolov5s.torchscript
  8. ONNX | `onnx` | yolov5s.onnx
  9. OpenVINO | `openvino` | yolov5s_openvino_model/
  10. TensorRT | `engine` | yolov5s.engine
  11. CoreML | `coreml` | yolov5s.mlmodel
  12. TensorFlow SavedModel | `saved_model` | yolov5s_saved_model/
  13. TensorFlow GraphDef | `pb` | yolov5s.pb
  14. TensorFlow Lite | `tflite` | yolov5s.tflite
  15. TensorFlow Edge TPU | `edgetpu` | yolov5s_edgetpu.tflite
  16. TensorFlow.js | `tfjs` | yolov5s_web_model/
  17. Requirements:
  18. $ pip install -r requirements.txt coremltools onnx onnx-simplifier onnxruntime openvino-dev tensorflow-cpu # CPU
  19. $ pip install -r requirements.txt coremltools onnx onnx-simplifier onnxruntime-gpu openvino-dev tensorflow # GPU
  20. $ pip install -U nvidia-tensorrt --index-url https://pypi.ngc.nvidia.com # TensorRT
  21. Usage:
  22. $ python benchmarks.py --weights yolov5s.pt --img 640
  23. """
  24. import argparse
  25. import platform
  26. import sys
  27. import time
  28. from pathlib import Path
  29. import pandas as pd
  30. FILE = Path(__file__).resolve()
  31. ROOT = FILE.parents[0] # YOLOv5 root directory
  32. if str(ROOT) not in sys.path:
  33. sys.path.append(str(ROOT)) # add ROOT to PATH
  34. # ROOT = ROOT.relative_to(Path.cwd()) # relative
  35. import export
  36. from models.experimental import attempt_load
  37. from models.yolo import SegmentationModel
  38. from segment.val import run as val_seg
  39. from utils import notebook_init
  40. from utils.general import LOGGER, check_yaml, file_size, print_args
  41. from utils.torch_utils import select_device
  42. from val import run as val_det
  43. def run(
  44. weights=ROOT / "yolov5s.pt", # weights path
  45. imgsz=640, # inference size (pixels)
  46. batch_size=1, # batch size
  47. data=ROOT / "data/coco128.yaml", # dataset.yaml path
  48. device="", # cuda device, i.e. 0 or 0,1,2,3 or cpu
  49. half=False, # use FP16 half-precision inference
  50. test=False, # test exports only
  51. pt_only=False, # test PyTorch only
  52. hard_fail=False, # throw error on benchmark failure
  53. ):
  54. y, t = [], time.time()
  55. device = select_device(device)
  56. model_type = type(attempt_load(weights, fuse=False)) # DetectionModel, SegmentationModel, etc.
  57. for i, (name, f, suffix, cpu, gpu) in export.export_formats().iterrows(): # index, (name, file, suffix, CPU, GPU)
  58. try:
  59. assert i not in (9, 10), "inference not supported" # Edge TPU and TF.js are unsupported
  60. assert i != 5 or platform.system() == "Darwin", "inference only supported on macOS>=10.13" # CoreML
  61. if "cpu" in device.type:
  62. assert cpu, "inference not supported on CPU"
  63. if "cuda" in device.type:
  64. assert gpu, "inference not supported on GPU"
  65. # Export
  66. if f == "-":
  67. w = weights # PyTorch format
  68. else:
  69. w = export.run(
  70. weights=weights, imgsz=[imgsz], include=[f], batch_size=batch_size, device=device, half=half
  71. )[-1] # all others
  72. assert suffix in str(w), "export failed"
  73. # Validate
  74. if model_type == SegmentationModel:
  75. result = val_seg(data, w, batch_size, imgsz, plots=False, device=device, task="speed", half=half)
  76. metric = result[0][7] # (box(p, r, map50, map), mask(p, r, map50, map), *loss(box, obj, cls))
  77. else: # DetectionModel:
  78. result = val_det(data, w, batch_size, imgsz, plots=False, device=device, task="speed", half=half)
  79. metric = result[0][3] # (p, r, map50, map, *loss(box, obj, cls))
  80. speed = result[2][1] # times (preprocess, inference, postprocess)
  81. y.append([name, round(file_size(w), 1), round(metric, 4), round(speed, 2)]) # MB, mAP, t_inference
  82. except Exception as e:
  83. if hard_fail:
  84. assert type(e) is AssertionError, f"Benchmark --hard-fail for {name}: {e}"
  85. LOGGER.warning(f"WARNING ⚠️ Benchmark failure for {name}: {e}")
  86. y.append([name, None, None, None]) # mAP, t_inference
  87. if pt_only and i == 0:
  88. break # break after PyTorch
  89. # Print results
  90. LOGGER.info("\n")
  91. parse_opt()
  92. notebook_init() # print system info
  93. c = ["Format", "Size (MB)", "mAP50-95", "Inference time (ms)"] if map else ["Format", "Export", "", ""]
  94. py = pd.DataFrame(y, columns=c)
  95. LOGGER.info(f"\nBenchmarks complete ({time.time() - t:.2f}s)")
  96. LOGGER.info(str(py if map else py.iloc[:, :2]))
  97. if hard_fail and isinstance(hard_fail, str):
  98. metrics = py["mAP50-95"].array # values to compare to floor
  99. floor = eval(hard_fail) # minimum metric floor to pass, i.e. = 0.29 mAP for YOLOv5n
  100. assert all(x > floor for x in metrics if pd.notna(x)), f"HARD FAIL: mAP50-95 < floor {floor}"
  101. return py
  102. def test(
  103. weights=ROOT / "yolov5s.pt", # weights path
  104. imgsz=640, # inference size (pixels)
  105. batch_size=1, # batch size
  106. data=ROOT / "data/coco128.yaml", # dataset.yaml path
  107. device="", # cuda device, i.e. 0 or 0,1,2,3 or cpu
  108. half=False, # use FP16 half-precision inference
  109. test=False, # test exports only
  110. pt_only=False, # test PyTorch only
  111. hard_fail=False, # throw error on benchmark failure
  112. ):
  113. y, t = [], time.time()
  114. device = select_device(device)
  115. for i, (name, f, suffix, gpu) in export.export_formats().iterrows(): # index, (name, file, suffix, gpu-capable)
  116. try:
  117. w = (
  118. weights
  119. if f == "-"
  120. else export.run(weights=weights, imgsz=[imgsz], include=[f], device=device, half=half)[-1]
  121. ) # weights
  122. assert suffix in str(w), "export failed"
  123. y.append([name, True])
  124. except Exception:
  125. y.append([name, False]) # mAP, t_inference
  126. # Print results
  127. LOGGER.info("\n")
  128. parse_opt()
  129. notebook_init() # print system info
  130. py = pd.DataFrame(y, columns=["Format", "Export"])
  131. LOGGER.info(f"\nExports complete ({time.time() - t:.2f}s)")
  132. LOGGER.info(str(py))
  133. return py
  134. def parse_opt():
  135. """Parses command-line arguments for YOLOv5 model inference configuration."""
  136. parser = argparse.ArgumentParser()
  137. parser.add_argument("--weights", type=str, default=ROOT / "yolov5s.pt", help="weights path")
  138. parser.add_argument("--imgsz", "--img", "--img-size", type=int, default=640, help="inference size (pixels)")
  139. parser.add_argument("--batch-size", type=int, default=1, help="batch size")
  140. parser.add_argument("--data", type=str, default=ROOT / "data/coco128.yaml", help="dataset.yaml path")
  141. parser.add_argument("--device", default="", help="cuda device, i.e. 0 or 0,1,2,3 or cpu")
  142. parser.add_argument("--half", action="store_true", help="use FP16 half-precision inference")
  143. parser.add_argument("--test", action="store_true", help="test exports only")
  144. parser.add_argument("--pt-only", action="store_true", help="test PyTorch only")
  145. parser.add_argument("--hard-fail", nargs="?", const=True, default=False, help="Exception on error or < min metric")
  146. opt = parser.parse_args()
  147. opt.data = check_yaml(opt.data) # check YAML
  148. print_args(vars(opt))
  149. return opt
  150. def main(opt):
  151. """Executes a test run if `opt.test` is True, otherwise starts training or inference with provided options."""
  152. test(**vars(opt)) if opt.test else run(**vars(opt))
  153. if __name__ == "__main__":
  154. opt = parse_opt()
  155. main(opt)