/YOLOv5-ONNX-cpp

YOLOv5 using ONNX Runtime in C++

Primary LanguageC++MIT LicenseMIT

GitHub release (latest by date) Visual Studio CMake Licence

YOLOv5-ONNX.cpp

YOLOv5 is a popular detector by Ultralytics. This project implements the YOLOv5 object detector in C++ utilizing the ONNX Runtime to speed up inference performance.

Features

  • Supports both image and video inference.
  • Faster CPU inference speeds.
  • Supports dynamic input sizes.

Getting Started

The following instructions demonstrates how to build this project on a Linux system. Windows is currently not supported by the DeepSparse library.

Prerequisites

  • CMake v3.8+ - found at https://cmake.org/

  • MSVC 2017++ (Windows Build) or GCC/G++ compiler (Linux Build)

  • Python 3.8+ - Python is used to install the deepsparse library which is required for the build. Download here.

  • OpenCV v4.0+ - Download here.

  • ONNX Runtime v1.5.1+ - Download here.

Building the project

  1. Set the OpenCV_DIR environment variable to point to your ../../opencv/build directory (if not set).

  2. Set the ONNX_INCLUDE_DIR to point the ONNX runtime header filesdirectory.

  3. Set the ONNX_LIB_DIR to point the ONNX runtime lib directory.

  4. Run the following build commands: a. [Windows] VS Developer Command Prompt:

    cd \d <yolov5-onnx-cpp-directory>
    cmake -S. -Bbuild -DCMAKE_BUILD_TYPE=Release
    cd build
    
    MSBuild yolov5-onnx-cpp.sln /property:Configuration=Release

    b. [Linux] Bash:

    cd <yolov5-onnx-cpp-directory>
    cmake -S. -Bbuild -DCMAKE_BUILD_TYPE=Release
    cd build
    
    make
  5. The compiled .exe will be inside the Release folder for Windows build, while the executable will be in root folder for Linux build.

Inference

  1. Clone the YOLOv5 repo and install the dependencies.

  2. Export the ONNX file:

    python export.py --weights=yolov5n.onnx --include onnx --dynamic
  3. To run the inference, execute the following command:

    yolov5-onnx-cpp --model <ONNX_MODEL_PATH> [-i <IMAGE_PATH> | -v <VIDEO_PATH>] [--labels <LABEL_PATH>] [--imgsz IMAGE_SIZE] [--iou-thresh IOU_THRESHOLD] [--score-thresh CONFIDENCE_THRESHOLD]

Authors

Acknowledgements

Thanks to @Hyuto for his work on ONNX implementation of YOLO-NAS in C++ which was utilized in this project.

License

This project is licensed under the MIT License - see the LICENSE file for details.