Back to Ultralytics

Ultralytics YOLO MNN Inference in C++

examples/cpp/MNN/README.md

8.4.714.9 KB
Original Source

Ultralytics YOLO MNN Inference in C++

A C++ application that runs every Ultralytics YOLO task and model generation with the Alibaba MNN inference engine and OpenCV. Point it at any .mnn model; the task, class names, and input size are read from the model bizCode metadata, and the right post-processing is selected automatically.

✨ Features

  • All tasks: detect, segment, pose, OBB, classify, and YOLO26 semantic segmentation.
  • All generations: YOLOv8, YOLO11, and YOLO26. Grid and end-to-end outputs are detected automatically.
  • Zero configuration: the task, class names, and imgsz come from the MNN bizCode metadata. When a model has none (for example after a plain MNNConvert), the task is inferred from the output shapes and names fall back to COCO.

📋 Dependencies

DependencyVersionDescription
MNN>=2.0.0The core inference engine from Alibaba.
OpenCV>=4.0.0Image I/O, drawing, and NMS.
C++>=17Modern C++ compiler.
CMake>=3.12.0Build system.

📦 Exporting a Model

Export directly to MNN with the Ultralytics export mode. This is the recommended path: it keeps the model metadata in bizCode, so the task and class names are read automatically.

bash
yolo export model=yolo26n.pt imgsz=640 format=mnn # detect (also -seg / -pose / -obb / -cls / -sem)

[!NOTE] Prefer format=mnn over the standalone MNNConvert tool. MNNConvert --bizCode <code> overwrites the model metadata, so the task is then inferred from the output shapes and class names fall back to COCO (wrong for OBB, classify, and semantic models). MNNConvert can also fail to convert some YOLO26 segment graphs.

If you still want to convert an existing ONNX model:

bash
yolo export model=yolo26n.pt format=onnx opset=12
/path/to/MNN/build/MNNConvert -f ONNX --modelFile yolo26n.onnx --MNNModel yolo26n.mnn --bizCode biz

🛠️ Build

First build the MNN library and converter from source:

bash
git clone https://github.com/alibaba/MNN.git && cd MNN
mkdir build && cd build
cmake -DMNN_BUILD_CONVERTER=ON -DCMAKE_BUILD_TYPE=Release ..
make -j$(nproc)

Then build the example, pointing it at the MNN headers and library:

bash
cd ultralytics/examples/cpp/MNN
mkdir build && cd build
cmake .. -DMNN_INCLUDE_DIR=/path/to/MNN/include -DMNN_LIB_DIR=/path/to/MNN/build
cmake --build . --config Release

The shared helpers in ../common are header-only and added to the include path automatically.

🚀 Usage

bash
# If MNN is built as a shared library, add it to the loader path:
export LD_LIBRARY_PATH=/path/to/MNN/build:$LD_LIBRARY_PATH

# Defaults: --model yolo26n.mnn --source bus.jpg --conf 0.25 --iou 0.45 --out result.jpg
./yolo_mnn --model yolo26n.mnn --source bus.jpg
./yolo_mnn --model yolo26n-pose.mnn --source bus.jpg --out pose.jpg --show
ArgumentDefaultDescription
--modelyolo26n.mnnPath to the exported MNN model.
--sourcebus.jpgInput image.
--conf0.25Confidence threshold.
--iou0.45NMS IoU threshold (grid models only).
--threads4CPU threads.
--outresult.jpgOutput image path.
--showoffAlso open a display window.

🤝 Contributing

Contributions are welcome! If you find any issues or have suggestions for improvements, please feel free to open an issue or submit a pull request on the main Ultralytics repository.