Skip to content

[Example] Yolo12 Detection sample with OpenVINO/XNNPACK backend #10156

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Jul 1, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
197 changes: 197 additions & 0 deletions .ci/scripts/test_yolo12.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,197 @@
#!/bin/bash
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this need to be running in CI?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is copy-paste from the other similar .sh files like this https://github.com/pytorch/executorch/blob/main/.ci/scripts/test_openvino.sh

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No my question is this script is not actually running in ci. If so why is it inside .ci folder

# Copyright (c) Meta Platforms, Inc. and affiliates.
# All rights reserved.
#
# This source code is licensed under the BSD-style license found in the
# LICENSE file in the root directory of this source tree.

set -ex
# shellcheck source=/dev/null
source "$(dirname "${BASH_SOURCE[0]}")/utils.sh"

while [[ $# -gt 0 ]]; do
case "$1" in
-model)
MODEL_NAME="$2" # stories110M
shift 2
;;
-mode)
MODE="$2" # portable or xnnpack+custom or xnnpack+custom+qe
shift 2
;;
-pt2e_quantize)
PT2E_QUANTIZE="$2"
shift 2
;;
-upload)
UPLOAD_DIR="$2"
shift 2
;;
-video_path)
VIDEO_PATH="$2" # portable or xnnpack+custom or xnnpack+custom+qe
shift 2
;;
*)
echo "Unknown option: $1"
usage
;;
esac
done

# Default mode to xnnpack+custom if not set
MODE=${MODE:-"openvino"}

# Default UPLOAD_DIR to empty string if not set
UPLOAD_DIR="${UPLOAD_DIR:-}"

# Default PT2E_QUANTIZE to empty string if not set
PT2E_QUANTIZE="${PT2E_QUANTIZE:-}"

# Default CMake Build Type to release mode
CMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE:-Release}

if [[ $# -lt 5 ]]; then # Assuming 4 mandatory args
echo "Expecting atleast 5 positional arguments"
echo "Usage: [...]"
fi
if [[ -z "${MODEL_NAME:-}" ]]; then
echo "Missing model name, exiting..."
exit 1
fi


if [[ -z "${MODE:-}" ]]; then
echo "Missing mode, choose openvino or xnnpack, exiting..."
exit 1
fi

if [[ -z "${PYTHON_EXECUTABLE:-}" ]]; then
PYTHON_EXECUTABLE=python3
fi

TARGET_LIBS=""

if [[ "${MODE}" =~ .*openvino.* ]]; then
OPENVINO=ON
TARGET_LIBS="$TARGET_LIBS openvino_backend "

git clone https://github.com/openvinotoolkit/openvino.git
cd openvino && git b16b776ac119dafda51f69a80f1e6b7376d02c3b
git submodule update --init --recursive
sudo ./install_build_dependencies.sh
mkdir build && cd build
cmake .. -DCMAKE_BUILD_TYPE=Release -DENABLE_PYTHON=ON
make -j$(nproc)

cd ..
cmake --install build --prefix dist

source dist/setupvars.sh
cd ../backends/openvino
pip install -r requirements.txt
cd ../../
else
OPENVINO=OFF
fi

if [[ "${MODE}" =~ .*xnnpack.* ]]; then
XNNPACK=ON
TARGET_LIBS="$TARGET_LIBS xnnpack_backend "
else
XNNPACK=OFF
fi

which "${PYTHON_EXECUTABLE}"


DIR="examples/models/yolo12"
$PYTHON_EXECUTABLE -m pip install -r ${DIR}/requirements.txt

cmake_install_executorch_libraries() {
rm -rf cmake-out
build_dir=cmake-out
mkdir $build_dir


retry cmake -DCMAKE_INSTALL_PREFIX="${build_dir}" \
-DCMAKE_BUILD_TYPE="${CMAKE_BUILD_TYPE}" \
-DEXECUTORCH_BUILD_OPENVINO="$OPENVINO" \
-DEXECUTORCH_BUILD_XNNPACK="$XNNPACK" \
-DEXECUTORCH_BUILD_EXTENSION_DATA_LOADER=ON \
-DEXECUTORCH_BUILD_EXTENSION_MODULE=ON \
-DEXECUTORCH_BUILD_EXTENSION_RUNNER_UTIL=ON \
-DEXECUTORCH_BUILD_EXTENSION_TENSOR=ON \
-B"${build_dir}"

# Build the project
cmake --build ${build_dir} --target install --config ${CMAKE_BUILD_TYPE} -j$(nproc)

export CMAKE_ARGS="
-DEXECUTORCH_BUILD_OPENVINO="$OPENVINO" \
-DEXECUTORCH_BUILD_XNNPACK="$XNNPACK" \
-DEXECUTORCH_BUILD_EXTENSION_DATA_LOADER=ON \
-DEXECUTORCH_BUILD_EXTENSION_MODULE=ON \
-DEXECUTORCH_BUILD_EXTENSION_RUNNER_UTIL=ON \
-DEXECUTORCH_ENABLE_LOGGING=ON \
-DEXECUTORCH_BUILD_EXTENSION_TENSOR=ON \
-DEXECUTORCH_BUILD_PYBIND=ON"

echo $TARGET_LIBS
export CMAKE_BUILD_ARGS="--target $TARGET_LIBS"
pip install . --no-build-isolation
}

cmake_build_demo() {
echo "Building yolo12 runner"
retry cmake \
-DCMAKE_BUILD_TYPE="$CMAKE_BUILD_TYPE" \
-DUSE_OPENVINO_BACKEND="$OPENVINO" \
-DUSE_XNNPACK_BACKEND="$XNNPACK" \
-Bcmake-out/${DIR} \
${DIR}
cmake --build cmake-out/${DIR} -j9 --config "$CMAKE_BUILD_TYPE"

}

cleanup_files() {
rm $EXPORTED_MODEL_NAME
}

prepare_artifacts_upload() {
if [ -n "${UPLOAD_DIR}" ]; then
echo "Preparing for uploading generated artifacs"
zip -j model.zip "${EXPORTED_MODEL_NAME}"
mkdir -p "${UPLOAD_DIR}"
mv model.zip "${UPLOAD_DIR}"
mv result.txt "${UPLOAD_DIR}"

fi
}


# Export model.
EXPORTED_MODEL_NAME="${MODEL_NAME}_fp32_${MODE}.pte"
echo "Exporting ${EXPORTED_MODEL_NAME}"
EXPORT_ARGS="--model_name=${MODEL_NAME} --backend=${MODE}"

# Add dynamically linked library location
cmake_install_executorch_libraries

$PYTHON_EXECUTABLE -m examples.models.yolo12.export_and_validate ${EXPORT_ARGS}


RUNTIME_ARGS="--model_path=${EXPORTED_MODEL_NAME} --input_path=${VIDEO_PATH}"
# Check build tool.
cmake_build_demo
# Run yolo12 runner
NOW=$(date +"%H:%M:%S")
echo "Starting to run yolo12 runner at ${NOW}"
# shellcheck source=/dev/null
cmake-out/examples/models/yolo12/Yolo12DetectionDemo ${RUNTIME_ARGS} > result.txt
NOW=$(date +"%H:%M:%S")
echo "Finished at ${NOW}"

RESULT=$(cat result.txt)

prepare_artifacts_upload
cleanup_files
2 changes: 1 addition & 1 deletion backends/openvino/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ Before you begin, ensure you have openvino installed and configured on your syst

```bash
git clone https://github.com/openvinotoolkit/openvino.git
cd openvino && git checkout releases/2025/1
cd openvino && git checkout b16b776ac119dafda51f69a80f1e6b7376d02c3b
git submodule update --init --recursive
sudo ./install_build_dependencies.sh
mkdir build && cd build
Expand Down
85 changes: 85 additions & 0 deletions examples/models/yolo12/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
cmake_minimum_required(VERSION 3.5)

project(Yolo12DetectionDemo VERSION 0.1)

option(USE_OPENVINO_BACKEND "Build the tutorial with the OPENVINO backend" ON)
option(USE_XNNPACK_BACKEND "Build the tutorial with the XNNPACK backend" OFF)

set(CMAKE_INCLUDE_CURRENT_DIR ON)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

# OpenCV
find_package(OpenCV REQUIRED)
include_directories(${OpenCV_INCLUDE_DIRS})
# !OpenCV

if(NOT PYTHON_EXECUTABLE)
set(PYTHON_EXECUTABLE python3)
endif()

set(EXECUTORCH_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/../../..)
set(TORCH_ROOT ${EXECUTORCH_ROOT}/third-party/pytorch)

include(${EXECUTORCH_ROOT}/tools/cmake/Utils.cmake)

# Let files say "include <executorch/path/to/header.h>".
set(_common_include_directories ${EXECUTORCH_ROOT}/..)

# find `executorch` libraries Same as for gflags
find_package(executorch CONFIG REQUIRED PATHS ${EXECUTORCH_ROOT}/cmake-out)
target_link_options_shared_lib(executorch)

add_subdirectory(${EXECUTORCH_ROOT}/third-party/gflags gflags)
set(link_libraries gflags)
list(APPEND link_libraries portable_ops_lib portable_kernels)
target_link_options_shared_lib(portable_ops_lib)


if(USE_XNNPACK_BACKEND)
set(xnnpack_backend_libs xnnpack_backend XNNPACK microkernels-prod)
list(APPEND link_libraries ${xnnpack_backend_libs})
target_link_options_shared_lib(xnnpack_backend)
endif()

if(USE_OPENVINO_BACKEND)
add_subdirectory(${EXECUTORCH_ROOT}/backends/openvino openvino_backend)

target_include_directories(
openvino_backend
INTERFACE ${CMAKE_CURRENT_BINARY_DIR}/../../include
${CMAKE_CURRENT_BINARY_DIR}/../../include/executorch/runtime/core/portable_type/c10
${CMAKE_CURRENT_BINARY_DIR}/../../lib
)
list(APPEND link_libraries openvino_backend)
target_link_options_shared_lib(openvino_backend)
endif()

list(APPEND link_libraries extension_threadpool pthreadpool)
list(APPEND _common_include_directories
${XNNPACK_ROOT}/third-party/pthreadpool/include
)

set(PROJECT_SOURCES
main.cpp
inference.h
${EXECUTORCH_ROOT}/extension/data_loader/file_data_loader.cpp
${EXECUTORCH_ROOT}/extension/evalue_util/print_evalue.cpp
${EXECUTORCH_ROOT}/extension/runner_util/inputs.cpp
${EXECUTORCH_ROOT}/extension/runner_util/inputs_portable.cpp
)

add_executable(Yolo12DetectionDemo ${PROJECT_SOURCES})
target_link_libraries(Yolo12DetectionDemo PUBLIC
${link_libraries}
${OpenCV_LIBS}
executorch_core
extension_module
extension_tensor
)

find_package(Threads REQUIRED)
target_link_libraries(Yolo12DetectionDemo PRIVATE Threads::Threads)
target_include_directories(Yolo12DetectionDemo PUBLIC ${_common_include_directories})
Loading
Loading