Skip to content

Commit 71f9551

Browse files
Yolo12 OpenVINO/XNNPACK sample
1 parent af0a246 commit 71f9551

File tree

9 files changed

+1112
-2
lines changed

9 files changed

+1112
-2
lines changed

.ci/scripts/test_yolo12.sh

Lines changed: 198 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,198 @@
1+
#!/bin/bash
2+
# Copyright (c) Meta Platforms, Inc. and affiliates.
3+
# All rights reserved.
4+
#
5+
# This source code is licensed under the BSD-style license found in the
6+
# LICENSE file in the root directory of this source tree.
7+
8+
set -ex
9+
# shellcheck source=/dev/null
10+
source "$(dirname "${BASH_SOURCE[0]}")/utils.sh"
11+
12+
while [[ $# -gt 0 ]]; do
13+
case "$1" in
14+
-model)
15+
MODEL_NAME="$2" # stories110M
16+
shift 2
17+
;;
18+
-mode)
19+
MODE="$2" # portable or xnnpack+custom or xnnpack+custom+qe
20+
shift 2
21+
;;
22+
-pt2e_quantize)
23+
PT2E_QUANTIZE="$2"
24+
shift 2
25+
;;
26+
-upload)
27+
UPLOAD_DIR="$2"
28+
shift 2
29+
;;
30+
-video_path)
31+
VIDEO_PATH="$2" # portable or xnnpack+custom or xnnpack+custom+qe
32+
shift 2
33+
;;
34+
*)
35+
echo "Unknown option: $1"
36+
usage
37+
;;
38+
esac
39+
done
40+
41+
# Default mode to xnnpack+custom if not set
42+
MODE=${MODE:-"openvino"}
43+
44+
# Default UPLOAD_DIR to empty string if not set
45+
UPLOAD_DIR="${UPLOAD_DIR:-}"
46+
47+
# Default PT2E_QUANTIZE to empty string if not set
48+
PT2E_QUANTIZE="${PT2E_QUANTIZE:-}"
49+
50+
# Default CMake Build Type to release mode
51+
CMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE:-Release}
52+
53+
if [[ $# -lt 5 ]]; then # Assuming 4 mandatory args
54+
echo "Expecting atleast 5 positional arguments"
55+
echo "Usage: [...]"
56+
fi
57+
if [[ -z "${MODEL_NAME:-}" ]]; then
58+
echo "Missing model name, exiting..."
59+
exit 1
60+
fi
61+
62+
63+
if [[ -z "${MODE:-}" ]]; then
64+
echo "Missing mode, choose openvino or xnnpack, exiting..."
65+
exit 1
66+
fi
67+
68+
if [[ -z "${PYTHON_EXECUTABLE:-}" ]]; then
69+
PYTHON_EXECUTABLE=python3
70+
fi
71+
72+
TARGET_LIBS=""
73+
74+
if [[ "${MODE}" =~ .*openvino.* ]]; then
75+
OPENVINO=ON
76+
TARGET_LIBS="$TARGET_LIBS openvino_backend "
77+
78+
git clone https://github.com/daniil-lyakhov/openvino.git
79+
80+
cd openvino && git checkout dl/executorch/yolo12
81+
git submodule update --init --recursive
82+
sudo ./install_build_dependencies.sh
83+
mkdir build && cd build
84+
cmake .. -DCMAKE_BUILD_TYPE=Release -DENABLE_PYTHON=ON
85+
make -j$(nproc)
86+
87+
cd ..
88+
cmake --install build --prefix dist
89+
90+
source dist/setupvars.sh
91+
cd ../backends/openvino
92+
pip install -r requirements.txt
93+
cd ../../
94+
else
95+
OPENVINO=OFF
96+
fi
97+
98+
if [[ "${MODE}" =~ .*xnnpack.* ]]; then
99+
XNNPACK=ON
100+
TARGET_LIBS="$TARGET_LIBS xnnpack_backend "
101+
else
102+
XNNPACK=OFF
103+
fi
104+
105+
which "${PYTHON_EXECUTABLE}"
106+
107+
108+
DIR="examples/models/yolo12"
109+
$PYTHON_EXECUTABLE -m pip install -r ${DIR}/requirements.txt
110+
111+
cmake_install_executorch_libraries() {
112+
rm -rf cmake-out
113+
build_dir=cmake-out
114+
mkdir $build_dir
115+
116+
117+
retry cmake -DCMAKE_INSTALL_PREFIX="${build_dir}" \
118+
-DCMAKE_BUILD_TYPE="${CMAKE_BUILD_TYPE}" \
119+
-DEXECUTORCH_BUILD_OPENVINO="$OPENVINO" \
120+
-DEXECUTORCH_BUILD_XNNPACK="$XNNPACK" \
121+
-DEXECUTORCH_BUILD_EXTENSION_DATA_LOADER=ON \
122+
-DEXECUTORCH_BUILD_EXTENSION_MODULE=ON \
123+
-DEXECUTORCH_BUILD_EXTENSION_RUNNER_UTIL=ON \
124+
-DEXECUTORCH_BUILD_EXTENSION_TENSOR=ON \
125+
-B"${build_dir}"
126+
127+
# Build the project
128+
cmake --build ${build_dir} --target install --config ${CMAKE_BUILD_TYPE} -j$(nproc)
129+
130+
export CMAKE_ARGS="
131+
-DEXECUTORCH_BUILD_OPENVINO="$OPENVINO" \
132+
-DEXECUTORCH_BUILD_XNNPACK="$XNNPACK" \
133+
-DEXECUTORCH_BUILD_EXTENSION_DATA_LOADER=ON \
134+
-DEXECUTORCH_BUILD_EXTENSION_MODULE=ON \
135+
-DEXECUTORCH_BUILD_EXTENSION_RUNNER_UTIL=ON \
136+
-DEXECUTORCH_ENABLE_LOGGING=ON \
137+
-DEXECUTORCH_BUILD_EXTENSION_TENSOR=ON \
138+
-DEXECUTORCH_BUILD_PYBIND=ON"
139+
140+
echo $TARGET_LIBS
141+
export CMAKE_BUILD_ARGS="--target $TARGET_LIBS"
142+
pip install . --no-build-isolation
143+
}
144+
145+
cmake_build_demo() {
146+
echo "Building yolo12 runner"
147+
retry cmake \
148+
-DCMAKE_BUILD_TYPE="$CMAKE_BUILD_TYPE" \
149+
-DUSE_OPENVINO_BACKEND="$OPENVINO" \
150+
-DUSE_XNNPACK_BACKEND="$XNNPACK" \
151+
-Bcmake-out/${DIR} \
152+
${DIR}
153+
cmake --build cmake-out/${DIR} -j9 --config "$CMAKE_BUILD_TYPE"
154+
155+
}
156+
157+
cleanup_files() {
158+
rm $EXPORTED_MODEL_NAME
159+
}
160+
161+
prepare_artifacts_upload() {
162+
if [ -n "${UPLOAD_DIR}" ]; then
163+
echo "Preparing for uploading generated artifacs"
164+
zip -j model.zip "${EXPORTED_MODEL_NAME}"
165+
mkdir -p "${UPLOAD_DIR}"
166+
mv model.zip "${UPLOAD_DIR}"
167+
mv result.txt "${UPLOAD_DIR}"
168+
169+
fi
170+
}
171+
172+
173+
# Export model.
174+
EXPORTED_MODEL_NAME="${MODEL_NAME}_fp32_${MODE}.pte"
175+
echo "Exporting ${EXPORTED_MODEL_NAME}"
176+
EXPORT_ARGS="--model_name=${MODEL_NAME} --backend=${MODE}"
177+
178+
# Add dynamically linked library location
179+
cmake_install_executorch_libraries
180+
181+
$PYTHON_EXECUTABLE -m examples.models.yolo12.export_and_validate ${EXPORT_ARGS}
182+
183+
184+
RUNTIME_ARGS="--model_path=${EXPORTED_MODEL_NAME} --input_path=${VIDEO_PATH}"
185+
# Check build tool.
186+
cmake_build_demo
187+
# Run yolo12 runner
188+
NOW=$(date +"%H:%M:%S")
189+
echo "Starting to run yolo12 runner at ${NOW}"
190+
# shellcheck source=/dev/null
191+
cmake-out/examples/models/yolo12/Yolo12DetectionDemo ${RUNTIME_ARGS} > result.txt
192+
NOW=$(date +"%H:%M:%S")
193+
echo "Finished at ${NOW}"
194+
195+
RESULT=$(cat result.txt)
196+
197+
prepare_artifacts_upload
198+
cleanup_files

backends/openvino/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ Before you begin, ensure you have openvino installed and configured on your syst
4545
### Build OpenVINO from Source
4646

4747
```bash
48-
git clone https://github.com/openvinotoolkit/openvino.git
49-
cd openvino && git checkout releases/2025/1
48+
git clone https://github.com/daniil-lyakhov/openvino.git
49+
cd openvino && git checkout dl/executorch/yolo12
5050
git submodule update --init --recursive
5151
sudo ./install_build_dependencies.sh
5252
mkdir build && cd build
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
cmake_minimum_required(VERSION 3.5)
2+
3+
project(Yolo12DetectionDemo VERSION 0.1)
4+
5+
option(USE_OPENVINO_BACKEND "Build the tutorial with the OPENVINO backend" ON)
6+
option(USE_XNNPACK_BACKEND "Build the tutorial with the XNNPACK backend" OFF)
7+
8+
set(CMAKE_INCLUDE_CURRENT_DIR ON)
9+
10+
set(CMAKE_CXX_STANDARD 17)
11+
set(CMAKE_CXX_STANDARD_REQUIRED ON)
12+
set(CMAKE_CXX_EXTENSIONS OFF)
13+
14+
# OpenCV
15+
find_package(OpenCV REQUIRED)
16+
include_directories(${OpenCV_INCLUDE_DIRS})
17+
# !OpenCV
18+
19+
if(NOT PYTHON_EXECUTABLE)
20+
set(PYTHON_EXECUTABLE python3)
21+
endif()
22+
23+
set(EXECUTORCH_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/../../..)
24+
set(TORCH_ROOT ${EXECUTORCH_ROOT}/third-party/pytorch)
25+
26+
include(${EXECUTORCH_ROOT}/tools/cmake/Utils.cmake)
27+
28+
# Let files say "include <executorch/path/to/header.h>".
29+
set(_common_include_directories ${EXECUTORCH_ROOT}/..)
30+
31+
# find `executorch` libraries Same as for gflags
32+
find_package(executorch CONFIG REQUIRED PATHS ${EXECUTORCH_ROOT}/cmake-out)
33+
target_link_options_shared_lib(executorch)
34+
35+
set(link_libraries gflags)
36+
list(APPEND link_libraries portable_ops_lib portable_kernels)
37+
target_link_options_shared_lib(portable_ops_lib)
38+
39+
40+
if(USE_XNNPACK_BACKEND)
41+
set(xnnpack_backend_libs xnnpack_backend XNNPACK microkernels-prod)
42+
list(APPEND link_libraries ${xnnpack_backend_libs})
43+
target_link_options_shared_lib(xnnpack_backend)
44+
endif()
45+
46+
if(USE_OPENVINO_BACKEND)
47+
add_subdirectory(${EXECUTORCH_ROOT}/backends/openvino openvino_backend)
48+
49+
target_include_directories(
50+
openvino_backend
51+
INTERFACE ${CMAKE_CURRENT_BINARY_DIR}/../../include
52+
${CMAKE_CURRENT_BINARY_DIR}/../../include/executorch/runtime/core/portable_type/c10
53+
${CMAKE_CURRENT_BINARY_DIR}/../../lib
54+
)
55+
list(APPEND link_libraries openvino_backend)
56+
target_link_options_shared_lib(openvino_backend)
57+
endif()
58+
59+
list(APPEND link_libraries extension_threadpool pthreadpool)
60+
list(APPEND _common_include_directories
61+
${XNNPACK_ROOT}/third-party/pthreadpool/include
62+
)
63+
64+
set(PROJECT_SOURCES
65+
main.cpp
66+
inference.h
67+
${EXECUTORCH_ROOT}/extension/data_loader/file_data_loader.cpp
68+
${EXECUTORCH_ROOT}/extension/evalue_util/print_evalue.cpp
69+
${EXECUTORCH_ROOT}/extension/runner_util/inputs.cpp
70+
${EXECUTORCH_ROOT}/extension/runner_util/inputs_portable.cpp
71+
)
72+
73+
add_executable(Yolo12DetectionDemo ${PROJECT_SOURCES})
74+
target_link_libraries(Yolo12DetectionDemo PUBLIC
75+
${link_libraries}
76+
${OpenCV_LIBS}
77+
executorch_core
78+
extension_module
79+
extension_tensor
80+
)
81+
82+
find_package(Threads REQUIRED)
83+
target_link_libraries(Yolo12DetectionDemo PRIVATE Threads::Threads)
84+
target_include_directories(Yolo12DetectionDemo PUBLIC ${_common_include_directories})

examples/models/yolo12/README.md

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
# YOLO12 Detection C++ Inference with ExecuTorch
2+
3+
<p align="center">
4+
<br>
5+
<img src="./yolo12s_demo.gif">
6+
<br>
7+
</p>
8+
9+
This example demonstrates how to perform inference of [Ultralytics YOLO12 family](https://docs.ultralytics.com/models/yolo12/) detection models in C++ leveraging the Executorch backends:
10+
- [OpenVINO](../../../backends/openvino/README.md)
11+
- [XNNPACK](../../../backends/xnnpack/README.md)
12+
13+
14+
# Instructions
15+
16+
### Step 1: Install ExecuTorch
17+
18+
To install ExecuTorch, follow this [guide](https://pytorch.org/executorch/stable/getting-started-setup.html).
19+
20+
### Step 2: Install the backend of your choice
21+
22+
- [OpenVINO backend installation guide](../../../backends/openvino/README.md#build-instructions)
23+
- [XNNPACK backend installation guilde](https://pytorch.org/executorch/stable/tutorial-xnnpack-delegate-lowering.html#running-the-xnnpack-model-with-cmake)
24+
25+
### Step 3: Install the demo requirements
26+
27+
28+
Python demo requirements:
29+
```bash
30+
python -m pip install -r examples/models/yolo12/requirements.txt
31+
```
32+
33+
Demo infenrece dependency - OpenCV library:
34+
https://opencv.org/get-started/
35+
36+
37+
### Step 4: Export the Yolo12 model to the ExecuTorch
38+
39+
40+
OpenVINO:
41+
```bash
42+
python export_and_validate.py --model_name yolo12s --input_dims=[1920,1080] --backend openvino --device CPU
43+
```
44+
45+
XNNPACK:
46+
```bash
47+
python export_and_validate.py --model_name yolo12s --input_dims=[1920,1080] --backend xnnpack
48+
```
49+
50+
> **_NOTE:_** Quantization is comming soon!
51+
52+
Exported model could be validated using the `--validate` key:
53+
54+
```bash
55+
python export_and_validate.py --model_name yolo12s --backend ... --validate dataset_name.yaml
56+
```
57+
58+
A list of available datasets and instructions on how to use a custom dataset can be found [here](https://docs.ultralytics.com/datasets/detect/).
59+
Validation only supports the default `--input_dims`; please do not specify this parameter when using the `--validate` flag.
60+
61+
62+
To get a full parameters description please use the following command:
63+
```bash
64+
python export_and_validate.py --help
65+
```
66+
67+
### Step 5: Build the demo project
68+
69+
OpenVINO:
70+
71+
```bash
72+
cd examples/models/yolo12
73+
mkdir build && cd build
74+
cmake -DCMAKE_BUILD_TYPE=Release -DUSE_OPENVINO_BACKEND=ON ..
75+
make -j$(nproc)
76+
```
77+
78+
XNNPACK:
79+
80+
```bash
81+
cd examples/models/yolo12
82+
mkdir build && cd build
83+
cmake -DCMAKE_BUILD_TYPE=Release -DUSE_XNNPACK_BACKEND=ON ..
84+
make -j$(nproc)
85+
```
86+
87+
### Step 6: Run the demo
88+
89+
```bash
90+
./build/Yolo12DetectionDemo -model_path /path/to/exported/model -input_path /path/to/video/file -output_path /path/to/output/annotated/video
91+
```
92+
93+
To get a full parameters description please use the following command:
94+
```
95+
./build/Yolo12DetectionDemo --help
96+
```
97+
98+
99+
# Credits:
100+
101+
Ultralytics examples: https://github.com/ultralytics/ultralytics/tree/main/examples
102+
103+
Sample video: https://www.pexels.com/@shanu-1040189/

0 commit comments

Comments
 (0)