Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. # YOLOv5 🚀 by Ultralytics, GPL-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 utils/benchmarks.py --weights yolov5s.pt --img 640
  23. """
  24. import argparse
  25. import sys
  26. import time
  27. from pathlib import Path
  28. import pandas as pd
  29. FILE = Path(__file__).resolve()
  30. ROOT = FILE.parents[1] # YOLOv5 root directory
  31. if str(ROOT) not in sys.path:
  32. sys.path.append(str(ROOT)) # add ROOT to PATH
  33. # ROOT = ROOT.relative_to(Path.cwd()) # relative
  34. import export
  35. import val
  36. from utils import notebook_init
  37. from utils.general import LOGGER, print_args
  38. from utils.torch_utils import select_device
  39. def run(weights=ROOT / 'yolov5s.pt', # weights path
  40. imgsz=640, # inference size (pixels)
  41. batch_size=1, # batch size
  42. data=ROOT / 'data/coco128.yaml', # dataset.yaml path
  43. device='', # cuda device, i.e. 0 or 0,1,2,3 or cpu
  44. half=False, # use FP16 half-precision inference
  45. ):
  46. y, t = [], time.time()
  47. formats = export.export_formats()
  48. device = select_device(device)
  49. for i, (name, f, suffix, gpu) in formats.iterrows(): # index, (name, file, suffix, gpu-capable)
  50. try:
  51. if device.type != 'cpu':
  52. assert gpu, f'{name} inference not supported on GPU'
  53. if f == '-':
  54. w = weights # PyTorch format
  55. else:
  56. w = export.run(weights=weights, imgsz=[imgsz], include=[f], device=device, half=half)[-1] # all others
  57. assert suffix in str(w), 'export failed'
  58. result = val.run(data, w, batch_size, imgsz, plots=False, device=device, task='benchmark', half=half)
  59. metrics = result[0] # metrics (mp, mr, map50, map, *losses(box, obj, cls))
  60. speeds = result[2] # times (preprocess, inference, postprocess)
  61. y.append([name, round(metrics[3], 4), round(speeds[1], 2)]) # mAP, t_inference
  62. except Exception as e:
  63. LOGGER.warning(f'WARNING: Benchmark failure for {name}: {e}')
  64. y.append([name, None, None]) # mAP, t_inference
  65. # Print results
  66. LOGGER.info('\n')
  67. parse_opt()
  68. notebook_init() # print system info
  69. py = pd.DataFrame(y, columns=['Format', 'mAP@0.5:0.95', 'Inference time (ms)'])
  70. LOGGER.info(f'\nBenchmarks complete ({time.time() - t:.2f}s)')
  71. LOGGER.info(str(py))
  72. return py
  73. def parse_opt():
  74. parser = argparse.ArgumentParser()
  75. parser.add_argument('--weights', type=str, default=ROOT / 'yolov5s.pt', help='weights path')
  76. parser.add_argument('--imgsz', '--img', '--img-size', type=int, default=640, help='inference size (pixels)')
  77. parser.add_argument('--batch-size', type=int, default=1, help='batch size')
  78. parser.add_argument('--data', type=str, default=ROOT / 'data/coco128.yaml', help='dataset.yaml path')
  79. parser.add_argument('--device', default='', help='cuda device, i.e. 0 or 0,1,2,3 or cpu')
  80. parser.add_argument('--half', action='store_true', help='use FP16 half-precision inference')
  81. opt = parser.parse_args()
  82. print_args(FILE.stem, opt)
  83. return opt
  84. def main(opt):
  85. run(**vars(opt))
  86. if __name__ == "__main__":
  87. opt = parse_opt()
  88. main(opt)