Skip to content

Commit 1decf7a

Browse files
[Yolo12] Revert of the revert of the Yolo12 Sample (#12163)
### Summary Follow up #12136 Revert of the revert #10156 The big gif is removed from the PR CC: @kimishpatel
1 parent 8497ea7 commit 1decf7a

File tree

8 files changed

+1113
-1
lines changed

8 files changed

+1113
-1
lines changed

.ci/scripts/test_yolo12.sh

Lines changed: 197 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,197 @@
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/openvinotoolkit/openvino.git
79+
cd openvino && git b16b776ac119dafda51f69a80f1e6b7376d02c3b
80+
git submodule update --init --recursive
81+
sudo ./install_build_dependencies.sh
82+
mkdir build && cd build
83+
cmake .. -DCMAKE_BUILD_TYPE=Release -DENABLE_PYTHON=ON
84+
make -j$(nproc)
85+
86+
cd ..
87+
cmake --install build --prefix dist
88+
89+
source dist/setupvars.sh
90+
cd ../backends/openvino
91+
pip install -r requirements.txt
92+
cd ../../
93+
else
94+
OPENVINO=OFF
95+
fi
96+
97+
if [[ "${MODE}" =~ .*xnnpack.* ]]; then
98+
XNNPACK=ON
99+
TARGET_LIBS="$TARGET_LIBS xnnpack_backend "
100+
else
101+
XNNPACK=OFF
102+
fi
103+
104+
which "${PYTHON_EXECUTABLE}"
105+
106+
107+
DIR="examples/models/yolo12"
108+
$PYTHON_EXECUTABLE -m pip install -r ${DIR}/requirements.txt
109+
110+
cmake_install_executorch_libraries() {
111+
rm -rf cmake-out
112+
build_dir=cmake-out
113+
mkdir $build_dir
114+
115+
116+
retry cmake -DCMAKE_INSTALL_PREFIX="${build_dir}" \
117+
-DCMAKE_BUILD_TYPE="${CMAKE_BUILD_TYPE}" \
118+
-DEXECUTORCH_BUILD_OPENVINO="$OPENVINO" \
119+
-DEXECUTORCH_BUILD_XNNPACK="$XNNPACK" \
120+
-DEXECUTORCH_BUILD_EXTENSION_DATA_LOADER=ON \
121+
-DEXECUTORCH_BUILD_EXTENSION_MODULE=ON \
122+
-DEXECUTORCH_BUILD_EXTENSION_RUNNER_UTIL=ON \
123+
-DEXECUTORCH_BUILD_EXTENSION_TENSOR=ON \
124+
-B"${build_dir}"
125+
126+
# Build the project
127+
cmake --build ${build_dir} --target install --config ${CMAKE_BUILD_TYPE} -j$(nproc)
128+
129+
export CMAKE_ARGS="
130+
-DEXECUTORCH_BUILD_OPENVINO="$OPENVINO" \
131+
-DEXECUTORCH_BUILD_XNNPACK="$XNNPACK" \
132+
-DEXECUTORCH_BUILD_EXTENSION_DATA_LOADER=ON \
133+
-DEXECUTORCH_BUILD_EXTENSION_MODULE=ON \
134+
-DEXECUTORCH_BUILD_EXTENSION_RUNNER_UTIL=ON \
135+
-DEXECUTORCH_ENABLE_LOGGING=ON \
136+
-DEXECUTORCH_BUILD_EXTENSION_TENSOR=ON \
137+
-DEXECUTORCH_BUILD_PYBIND=ON"
138+
139+
echo $TARGET_LIBS
140+
export CMAKE_BUILD_ARGS="--target $TARGET_LIBS"
141+
pip install . --no-build-isolation
142+
}
143+
144+
cmake_build_demo() {
145+
echo "Building yolo12 runner"
146+
retry cmake \
147+
-DCMAKE_BUILD_TYPE="$CMAKE_BUILD_TYPE" \
148+
-DUSE_OPENVINO_BACKEND="$OPENVINO" \
149+
-DUSE_XNNPACK_BACKEND="$XNNPACK" \
150+
-Bcmake-out/${DIR} \
151+
${DIR}
152+
cmake --build cmake-out/${DIR} -j9 --config "$CMAKE_BUILD_TYPE"
153+
154+
}
155+
156+
cleanup_files() {
157+
rm $EXPORTED_MODEL_NAME
158+
}
159+
160+
prepare_artifacts_upload() {
161+
if [ -n "${UPLOAD_DIR}" ]; then
162+
echo "Preparing for uploading generated artifacs"
163+
zip -j model.zip "${EXPORTED_MODEL_NAME}"
164+
mkdir -p "${UPLOAD_DIR}"
165+
mv model.zip "${UPLOAD_DIR}"
166+
mv result.txt "${UPLOAD_DIR}"
167+
168+
fi
169+
}
170+
171+
172+
# Export model.
173+
EXPORTED_MODEL_NAME="${MODEL_NAME}_fp32_${MODE}.pte"
174+
echo "Exporting ${EXPORTED_MODEL_NAME}"
175+
EXPORT_ARGS="--model_name=${MODEL_NAME} --backend=${MODE}"
176+
177+
# Add dynamically linked library location
178+
cmake_install_executorch_libraries
179+
180+
$PYTHON_EXECUTABLE -m examples.models.yolo12.export_and_validate ${EXPORT_ARGS}
181+
182+
183+
RUNTIME_ARGS="--model_path=${EXPORTED_MODEL_NAME} --input_path=${VIDEO_PATH}"
184+
# Check build tool.
185+
cmake_build_demo
186+
# Run yolo12 runner
187+
NOW=$(date +"%H:%M:%S")
188+
echo "Starting to run yolo12 runner at ${NOW}"
189+
# shellcheck source=/dev/null
190+
cmake-out/examples/models/yolo12/Yolo12DetectionDemo ${RUNTIME_ARGS} > result.txt
191+
NOW=$(date +"%H:%M:%S")
192+
echo "Finished at ${NOW}"
193+
194+
RESULT=$(cat result.txt)
195+
196+
prepare_artifacts_upload
197+
cleanup_files

backends/openvino/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ Before you begin, ensure you have openvino installed and configured on your syst
4646

4747
```bash
4848
git clone https://github.com/openvinotoolkit/openvino.git
49-
cd openvino && git checkout releases/2025/1
49+
cd openvino && git checkout b16b776ac119dafda51f69a80f1e6b7376d02c3b
5050
git submodule update --init --recursive
5151
sudo ./install_build_dependencies.sh
5252
mkdir build && cd build

examples/models/yolo12/CMakeLists.txt

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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+
add_subdirectory(${EXECUTORCH_ROOT}/third-party/gflags gflags)
36+
set(link_libraries gflags)
37+
list(APPEND link_libraries portable_ops_lib portable_kernels)
38+
target_link_options_shared_lib(portable_ops_lib)
39+
40+
41+
if(USE_XNNPACK_BACKEND)
42+
set(xnnpack_backend_libs xnnpack_backend XNNPACK microkernels-prod)
43+
list(APPEND link_libraries ${xnnpack_backend_libs})
44+
target_link_options_shared_lib(xnnpack_backend)
45+
endif()
46+
47+
if(USE_OPENVINO_BACKEND)
48+
add_subdirectory(${EXECUTORCH_ROOT}/backends/openvino openvino_backend)
49+
50+
target_include_directories(
51+
openvino_backend
52+
INTERFACE ${CMAKE_CURRENT_BINARY_DIR}/../../include
53+
${CMAKE_CURRENT_BINARY_DIR}/../../include/executorch/runtime/core/portable_type/c10
54+
${CMAKE_CURRENT_BINARY_DIR}/../../lib
55+
)
56+
list(APPEND link_libraries openvino_backend)
57+
target_link_options_shared_lib(openvino_backend)
58+
endif()
59+
60+
list(APPEND link_libraries extension_threadpool pthreadpool)
61+
list(APPEND _common_include_directories
62+
${XNNPACK_ROOT}/third-party/pthreadpool/include
63+
)
64+
65+
set(PROJECT_SOURCES
66+
main.cpp
67+
inference.h
68+
${EXECUTORCH_ROOT}/extension/data_loader/file_data_loader.cpp
69+
${EXECUTORCH_ROOT}/extension/evalue_util/print_evalue.cpp
70+
${EXECUTORCH_ROOT}/extension/runner_util/inputs.cpp
71+
${EXECUTORCH_ROOT}/extension/runner_util/inputs_portable.cpp
72+
)
73+
74+
add_executable(Yolo12DetectionDemo ${PROJECT_SOURCES})
75+
target_link_libraries(Yolo12DetectionDemo PUBLIC
76+
${link_libraries}
77+
${OpenCV_LIBS}
78+
executorch_core
79+
extension_module
80+
extension_tensor
81+
)
82+
83+
find_package(Threads REQUIRED)
84+
target_link_libraries(Yolo12DetectionDemo PRIVATE Threads::Threads)
85+
target_include_directories(Yolo12DetectionDemo PUBLIC ${_common_include_directories})

0 commit comments

Comments
 (0)