Are you sure you want to delete this access key?
Welcome to the Ultralytics YOLOv8 MNN Inference example in C++! This guide will help you get started with leveraging the powerful Ultralytics YOLOv8 models using the Alibaba MNN inference engine in your C++ projects. Whether you're looking to enhance performance on CPU hardware or add flexibility to your applications, this example provides a solid foundation. Learn more about optimizing models and deployment strategies on the Ultralytics blog.
To ensure smooth execution, please make sure you have the following dependencies installed:
Dependency | Version | Description |
---|---|---|
MNN | >=2.0.0 | The core inference engine from Alibaba. |
C++ | >=14 | A modern C++ compiler supporting C++14 features. |
CMake | >=3.12.0 | Cross-platform build system generator required for building MNN and the example. |
OpenCV | Optional | Used for image loading and preprocessing within the example (built with MNN). |
Follow these steps to build the project:
Clone the Ultralytics repository:
git clone https://github.com/ultralytics/ultralytics.git
cd ultralytics/examples/YOLOv8-MNN-CPP
Clone the Alibaba MNN repository:
git clone https://github.com/alibaba/MNN.git
cd MNN
Build the MNN library:
# Create build directory
mkdir build && cd build
# Configure CMake (enable OpenCV integration, disable shared libs, enable image codecs)
cmake -DMNN_BUILD_OPENCV=ON -DBUILD_SHARED_LIBS=OFF -DMNN_IMGCODECS=ON ..
# Build the library (use -j flag for parallel compilation)
make -j$(nproc) # Use nproc for Linux, sysctl -n hw.ncpu for macOS
Note: If you encounter issues during the build process, consult the official MNN documentation for detailed build instructions and troubleshooting tips.
Copy the required MNN libraries and headers to the example project directory:
# Navigate back to the example directory
cd ../..
# Create directories for libraries and headers if they don't exist
mkdir -p libs include
# Copy static libraries
cp MNN/build/libMNN.a libs/ # Main MNN library
cp MNN/build/express/libMNN_Express.a libs/ # MNN Express API library
cp MNN/build/tools/cv/libMNNOpenCV.a libs/ # MNN OpenCV wrapper library
# Copy header files
cp -r MNN/include .
cp -r MNN/tools/cv/include . # MNN OpenCV wrapper headers
Note:
.a
for static) and paths might vary based on your operating system (e.g., use .lib
on Windows) and build configuration. Adjust the commands accordingly..a
files). If you built shared libraries (.so
, .dylib
, .dll
), ensure they are correctly placed or accessible in your system's library path.Create a build directory for the example project and compile using CMake:
mkdir build && cd build
cmake ..
make
To use your Ultralytics YOLOv8 model with this C++ example, you first need to export it to the MNN format. This can be done easily using the yolo export
command provided by the Ultralytics Python package.
Refer to the Ultralytics Export documentation for detailed instructions and options.
# Export a YOLOv8n model to MNN format with input size 640x640
yolo export model=yolov8n.pt imgsz=640 format=mnn
Alternatively, you can use the MNNConvert
tool provided by MNN:
# Assuming MNNConvert is built and in your PATH or MNN build directory
# Convert an ONNX model (first export YOLOv8 to ONNX)
yolo export model=yolov8n.pt format=onnx
./MNN/build/MNNConvert -f ONNX --modelFile yolov8n.onnx --MNNModel yolov8n.mnn --bizCode biz
For more details on model conversion using MNN tools, see the MNN Convert documentation.
You can verify the exported MNN model using the Ultralytics Python package for a quick check.
Download an example image:
wget https://ultralytics.com/images/bus.jpg
Run prediction using the MNN model:
yolo predict model=yolov8n.mnn source=bus.jpg
Expected Python Output:
ultralytics/examples/YOLOv8-MNN-CPP/assets/bus.jpg: 640x640 4 persons, 1 bus, 84.6ms
Speed: 9.7ms preprocess, 128.7ms inference, 12.4ms postprocess per image at shape (1, 3, 640, 640)
Results saved to runs/detect/predict
(Note: Speed and specific detections might vary based on hardware and model version)
This example uses the higher-level Express API for simpler inference code.
./build/main yolov8n.mnn bus.jpg
Expected C++ Express API Output:
The device supports: i8sdot:0, fp16:0, i8mm: 0, sve2: 0, sme2: 0
Detection: box = {48.63, 399.30, 243.65, 902.90}, class = person, score = 0.86
Detection: box = {22.14, 228.36, 796.07, 749.74}, class = bus, score = 0.86
Detection: box = {669.92, 375.82, 809.86, 874.41}, class = person, score = 0.86
Detection: box = {216.01, 405.24, 346.36, 858.19}, class = person, score = 0.82
Detection: box = {-0.11, 549.41, 62.05, 874.88}, class = person, score = 0.33
Result image write to `mnn_yolov8_cpp.jpg`.
Speed: 35.6ms preprocess, 386.0ms inference, 68.3ms postprocess
(Note: Speed and specific detections might vary based on hardware and MNN configuration)
This example uses the lower-level Interpreter API, offering more control over the inference process.
./build/main_interpreter yolov8n.mnn bus.jpg
Expected C++ Interpreter API Output:
The device supports: i8sdot:0, fp16:0, i8mm: 0, sve2: 0, sme2: 0
Detection: box = {48.63, 399.30, 243.65, 902.90}, class = person, score = 0.86
Detection: box = {22.14, 228.36, 796.07, 749.74}, class = bus, score = 0.86
Detection: box = {669.92, 375.82, 809.86, 874.41}, class = person, score = 0.86
Detection: box = {216.01, 405.24, 346.36, 858.19}, class = person, score = 0.82
Result image written to `mnn_yolov8_cpp.jpg`.
Speed: 26.0ms preprocess, 190.9ms inference, 58.9ms postprocess
(Note: Speed and specific detections might vary based on hardware and MNN configuration)
We hope this example helps you integrate Ultralytics YOLOv8 with MNN into your C++ projects effortlessly! Contributions to improve this example or add new features are highly welcome. Please see the Ultralytics contribution guidelines for more information on how to get involved.
For further guides, tutorials, and documentation on Ultralytics YOLO models and tools, visit the main Ultralytics documentation. Happy coding! 🚀
Press p or to see the previous file or, n or to see the next file
Are you sure you want to delete this access key?
Are you sure you want to delete this access key?
Are you sure you want to delete this access key?
Are you sure you want to delete this access key?