examples/cpp/MNN/README.md
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.
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.| Dependency | Version | Description |
|---|---|---|
| MNN | >=2.0.0 | The core inference engine from Alibaba. |
| OpenCV | >=4.0.0 | Image I/O, drawing, and NMS. |
| C++ | >=17 | Modern C++ compiler. |
| CMake | >=3.12.0 | Build system. |
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.
yolo export model=yolo26n.pt imgsz=640 format=mnn # detect (also -seg / -pose / -obb / -cls / -sem)
[!NOTE] Prefer
format=mnnover the standaloneMNNConverttool.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).MNNConvertcan also fail to convert some YOLO26 segment graphs.
If you still want to convert an existing ONNX model:
yolo export model=yolo26n.pt format=onnx opset=12
/path/to/MNN/build/MNNConvert -f ONNX --modelFile yolo26n.onnx --MNNModel yolo26n.mnn --bizCode biz
First build the MNN library and converter from source:
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:
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.
# 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
| Argument | Default | Description |
|---|---|---|
--model | yolo26n.mnn | Path to the exported MNN model. |
--source | bus.jpg | Input image. |
--conf | 0.25 | Confidence threshold. |
--iou | 0.45 | NMS IoU threshold (grid models only). |
--threads | 4 | CPU threads. |
--out | result.jpg | Output image path. |
--show | off | Also open a display window. |
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.