Skip to content

Commit 32d5eb5

Browse files
authored
Merge pull request #31 from fateshelled/dev_cpp
support tflite C++
2 parents 55cc10c + 0a7e86a commit 32d5eb5

21 files changed

+648
-83
lines changed

yolox_ros_cpp/README.md

Lines changed: 101 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,16 @@
88
- OpenVINO 2021.*
99
- TensorRT 8.x *
1010
- ONNXRuntime *
11+
- Tensorflow Lite *
1112

12-
※ Either one of OpenVINO or TensorRT or ONNXRuntime is required.
13+
※ Either one of OpenVINO or TensorRT or ONNXRuntime or Tensorflow Lite is required.
1314

1415
※ ONNXRuntime support CPU or CUDA execute provider.
1516

17+
※ Tensorflow Lite support XNNPACK Delegate only.
18+
19+
※ Tensorflow Lite support float model and does not support integer model.
20+
1621
※ Model convert script is not supported OpenVINO 2022.*
1722

1823
※ YOLOX is not required.
@@ -144,6 +149,15 @@ source /opt/ros/foxy/setup.bash
144149
./src/YOLOX-ROS/weights/onnx/download.bash yolox_nano
145150
```
146151

152+
#### Tensorflow Lite
153+
```bash
154+
cd ~/ros2_ws
155+
156+
# Download tflite Person Detection model
157+
# https://github.com/Kazuhito00/Person-Detection-using-RaspberryPi-CPU/
158+
./src/YOLOX-ROS/weights/tflite/download_model.bash
159+
```
160+
147161
#### PINTO_model_zoo
148162
- Support PINTO_model_zoo model
149163
- Download model using the following script.
@@ -152,9 +166,12 @@ source /opt/ros/foxy/setup.bash
152166

153167
- ONNX model copy to weight dir
154168
- `cp resouces_new/saved_model_yolox_nano_480x640/yolox_nano_480x640.onnx ./src/YOLOX-ROS/weights/onnx/`
169+
155170
- Convert to TensorRT engine
156171
- `./src/YOLOX-ROS/weights/tensorrt/convert.bash yolox_nano_480x640`
157172

173+
- tflite model copy to weight dir
174+
- `cp resouces_new/saved_model_yolox_nano_480x640/model_float32.tflite ./src/YOLOX-ROS/weights/tflite/`
158175

159176
### build packages
160177
```bash
@@ -167,6 +184,46 @@ colcon build --symlink-install
167184
source ./install/setup.bash
168185
```
169186

187+
188+
#### build yolox_ros_cpp with tflite
189+
190+
##### build tflite
191+
https://www.tensorflow.org/lite/guide/build_cmake
192+
193+
Below is an example build script.
194+
Please change `${workspace}` as appropriate for your environment.
195+
```bash
196+
cd ${workspace}
197+
git clone https://github.com/tensorflow/tensorflow.git tensorflow_src
198+
mkdir tflite_build
199+
cd tflite_build
200+
201+
cmake ../tensorflow_src/tensorflow/lite \
202+
-DBUILD_SHARED_LIBS=ON \
203+
-DTFLITE_ENABLE_INSTALL=OFF \
204+
-DTFLITE_ENABLE_XNNPACK=ON \
205+
-DTFLITE_ENABLE_RUY=OFF \
206+
-DCMAKE_BUILD_TYPE=Release
207+
208+
make -j"$(nproc)"
209+
```
210+
211+
##### build ros package with tflite
212+
213+
This is build script when tflite built as above.
214+
215+
```bash
216+
# build with tflite
217+
colcon build --symlink-install \
218+
--packages-up-to yolox_ros_cpp \
219+
--cmake-args \
220+
-DYOLOX_USE_TFLITE=ON \
221+
-DTFLITE_LIB_PATH=${workspace}/tflite_build/libtensorflow-lite.so \
222+
-DTFLITE_INCLUDE_DIR=${workspace}/tensorflow_src \
223+
-DABSEIL_CPP_ICLUDE_DIR=${workspace}/tflite_build/abseil-cpp \
224+
-DFLATBUFFERS_INCLUDE_DIR=${workspace}/tflite_build/flatbuffers/include
225+
```
226+
170227
### Run
171228

172229
#### OpenVINO
@@ -217,9 +274,26 @@ ros2 launch yolox_ros_cpp yolox_tensorrt_jetson.launch.py
217274
ros2 launch yolox_ros_cpp yolox_onnxruntime.launch.py
218275
```
219276

277+
#### Tensorflow Lite
278+
```bash
279+
# add libtensorflow-lite.so directory path to `LD_LIBRARY_PATH`
280+
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:${workspace}/tflite_build
281+
282+
# run Person Detection Model
283+
ros2 launch yolox_ros_cpp yolox_tflite.launch.py
284+
285+
# run PINTO_model_zoo model
286+
ros2 launch yolox_ros_cpp yolox_tflite.launch.py \
287+
model_path:=install/yolox_ros_cpp/share/yolox_ros_cpp/weights/tflite/model_float32.tflite \
288+
model_version:=0.1.0 \
289+
num_classes:=80 \
290+
is_nchw:=false
291+
```
292+
220293
### Parameter
221294
#### OpenVINO example
222295
- `model_path`: ./install/yolox_ros_cpp/share/yolox_ros_cpp/weights/openvino/yolox_nano.xml
296+
- `p6`: false
223297
- `class_labels_path`: ""
224298
- if not set, use coco_names.
225299
- See [here](https://github.com/fateshelled/YOLOX-ROS/blob/dev_cpp/yolox_ros_cpp/yolox_ros_cpp/labels/coco_names.txt) for label format.
@@ -229,27 +303,29 @@ ros2 launch yolox_ros_cpp yolox_onnxruntime.launch.py
229303
- `conf`: 0.3
230304
- `nms`: 0.45
231305
- `imshow_isshow`: true
232-
- `src_image_topic_name`: image_raw
233-
- `publish_image_topic_name`: yolox/image_raw
234-
- `publish_boundingbox_topic_name`: yolox/bounding_boxes
306+
- `src_image_topic_name`: /image_raw
307+
- `publish_image_topic_name`: /yolox/image_raw
308+
- `publish_boundingbox_topic_name`: /yolox/bounding_boxes
235309

236310

237311
#### TensorRT example.
238312
- `model_path`: ./install/yolox_ros_cpp/share/yolox_ros_cpp/weights/tensorrt/yolox_nano.trt
313+
- `p6`: false
239314
- `class_labels_path`: ""
240315
- `num_classes`: 80
241316
- `model_version`: 0.1.1rc0
242317
- `tensorrt/device`: 0
243318
- `conf`: 0.3
244319
- `nms`: 0.45
245320
- `imshow_isshow`: true
246-
- `src_image_topic_name`: image_raw
247-
- `publish_image_topic_name`: yolox/image_raw
248-
- `publish_boundingbox_topic_name`: yolox/bounding_boxes
321+
- `src_image_topic_name`: /image_raw
322+
- `publish_image_topic_name`: /yolox/image_raw
323+
- `publish_boundingbox_topic_name`: /yolox/bounding_boxes
249324

250325

251326
#### ONNXRuntime example.
252327
- `model_path`: ./install/yolox_ros_cpp/share/yolox_ros_cpp/weights/onnx/yolox_nano.onnx
328+
- `p6`: false
253329
- `class_labels_path`: ""
254330
- `num_classes`: 80
255331
- `model_version`: 0.1.1rc0
@@ -263,9 +339,24 @@ ros2 launch yolox_ros_cpp yolox_onnxruntime.launch.py
263339
- `conf`: 0.3
264340
- `nms`: 0.45
265341
- `imshow_isshow`: true
266-
- `src_image_topic_name`: image_raw
267-
- `publish_image_topic_name`: yolox/image_raw
268-
- `publish_boundingbox_topic_name`: yolox/bounding_boxes
342+
- `src_image_topic_name`: /image_raw
343+
- `publish_image_topic_name`: /yolox/image_raw
344+
- `publish_boundingbox_topic_name`: /yolox/bounding_boxes
345+
346+
#### Tensorflow Lite example.
347+
- `model_path`: ./install/yolox_ros_cpp/share/yolox_ros_cpp/weights/tflite/model.tflite
348+
- `p6`: false
349+
- `is_nchw`: true
350+
- `class_labels_path`: ""
351+
- `num_classes`: 1
352+
- `model_version`: 0.1.1rc0
353+
- `tflite/num_threads`: 1
354+
- `conf`: 0.3
355+
- `nms`: 0.45
356+
- `imshow_isshow`: true
357+
- `src_image_topic_name`: /image_raw
358+
- `publish_image_topic_name`: /yolox/image_raw
359+
- `publish_boundingbox_topic_name`: /yolox/bounding_boxes
269360

270361

271362
### Reference

yolox_ros_cpp/yolox_cpp/CMakeLists.txt

Lines changed: 41 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,16 @@ set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
2121
option(YOLOX_USE_OPENVINO "Use OpenVINO" ON)
2222
option(YOLOX_USE_TENSORRT "Use TensorRT" ON)
2323
option(YOLOX_USE_ONNXRUNTIME "Use ONNXRuntime" ON)
24+
option(YOLOX_USE_TFLITE "Use tflite" OFF)
25+
set(TFLITE_LIB_PATH "" CACHE PATH "Path to libtensorflow-lite.so")
26+
set(TFLITE_INCLUDE_DIR "" CACHE PATH "Header directory of tflite")
27+
set(ABSEIL_CPP_ICLUDE_DIR "" CACHE PATH "Header directory of abseil-cpp")
28+
set(FLATBUFFERS_INCLUDE_DIR "" CACHE PATH "Header directory of flatbuffers")
2429

2530
set(ENABLE_OPENVINO OFF)
2631
set(ENABLE_TENSORRT OFF)
2732
set(ENABLE_ONNXRUNTIME OFF)
33+
set(ENABLE_TFLITE OFF)
2834

2935
# find dependencies
3036
find_package(ament_cmake REQUIRED)
@@ -46,43 +52,60 @@ if(YOLOX_USE_TENSORRT)
4652
find_library(NVONNXPARSER NAMES nvonnxparser)
4753
find_library(NVONNXPARSERRUNTIME NAMES nvonnxparser_runtime)
4854
if(NOT CUDA_FOUND)
49-
message(WARNING " CUDA ${CUDA_FOUND}")
55+
message(WARNING " CUDA not found")
5056
endif()
5157
if(NOT NVINFER)
52-
message(WARNING " NVINFER ${NVINFER}")
58+
message(WARNING " NVINFER not found")
5359
endif()
5460
if(NOT NVINFERPLUGIN)
55-
message(WARNING " NVINFERPLUGIN ${NVINFERPLUGIN}")
61+
message(WARNING " NVINFERPLUGIN not found")
5662
endif()
5763
if(NOT NVPARSERS)
58-
message(WARNING " NVPARSERS ${NVPARSERS}")
64+
message(WARNING " NVPARSERS not found")
5965
endif()
6066
if(NOT NVONNXPARSER)
61-
message(WARNING " NVONNXPARSER ${NVONNXPARSER}")
67+
message(WARNING " NVONNXPARSER not found")
6268
endif()
6369
# message(WARNING " NVONNXPARSERRUNTIME ${NVONNXPARSERRUNTIME}") #not use
6470
if( CUDA_FOUND AND NVINFER AND NVINFERPLUGIN AND NVPARSERS AND NVONNXPARSER )
71+
message(STATUS " CUDA ${CUDA_FOUND}")
72+
message(STATUS " NVINFER ${NVINFER}")
73+
message(STATUS " NVINFERPLUGIN ${NVINFERPLUGIN}")
74+
message(STATUS " NVPARSERS ${NVPARSERS}")
75+
message(STATUS " NVONNXPARSER ${NVONNXPARSER}")
6576
set(ENABLE_TENSORRT ON)
6677
set(SRC ${SRC} src/yolox_tensorrt.cpp)
6778
endif()
6879
endif()
6980
if(YOLOX_USE_ONNXRUNTIME)
7081
find_library(ONNXRUNTIME NAMES onnxruntime)
7182
if(NOT ONNXRUNTIME)
72-
message(WARNING " ONNXRUNTIME ${ONNXRUNTIME}")
73-
endif()
74-
if(ONNXRUNTIME)
83+
message(WARNING " ONNXRUNTIME not found")
84+
else()
85+
message(STATUS " ONNXRUNTIME ${ONNXRUNTIME}")
7586
set(ENABLE_ONNXRUNTIME ON)
7687
set(SRC ${SRC} src/yolox_onnxruntime.cpp)
7788
endif()
7889
endif()
90+
if(YOLOX_USE_TFLITE)
91+
if(NOT ${TFLITE_LIB_PATH})
92+
set(ENABLE_TFLITE ON)
93+
set(SRC ${SRC} src/yolox_tflite.cpp)
94+
set(INCLUDES ${INCLUDES} ${TFLITE_INCLUDE_DIR})
95+
set(INCLUDES ${INCLUDES} ${ABSEIL_CPP_ICLUDE_DIR})
96+
set(INCLUDES ${INCLUDES} ${FLATBUFFERS_INCLUDE_DIR})
97+
else()
98+
message(WARNING "TFLITE_LIB_PATH is not set")
99+
endif()
100+
endif()
79101

80102
message(STATUS " ENABLE_OPENVINO: ${ENABLE_OPENVINO}")
81103
message(STATUS " ENABLE_TENSORRT: ${ENABLE_TENSORRT}")
82104
message(STATUS " ENABLE_ONNXRUNTIME: ${ENABLE_ONNXRUNTIME}")
105+
message(STATUS " ENABLE_TFLITE: ${ENABLE_TFLITE}")
83106

84-
if(NOT ENABLE_OPENVINO AND NOT ENABLE_TENSORRT AND NOT ENABLE_ONNXRUNTIME)
85-
message(WARNING "skip building yolox_cpp, no OpenVINO, TensorRT and ONNXRuntime found")
107+
if(NOT ENABLE_OPENVINO AND NOT ENABLE_TENSORRT AND NOT ENABLE_ONNXRUNTIME AND NOT ENABLE_TFLITE)
108+
message(WARNING "skip building yolox_cpp, no OpenVINO, TensorRT, ONNXRuntime and tflite found")
86109
return()
87110
endif()
88111

@@ -91,6 +114,8 @@ configure_file(
91114
"${PROJECT_SOURCE_DIR}/include/yolox_cpp/config.h"
92115
)
93116

117+
set(INCLUDES ${INCLUDES} $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>)
118+
set(INCLUDES ${INCLUDES} $<INSTALL_INTERFACE:include>)
94119

95120
add_library(yolox_cpp SHARED
96121
${SRC}
@@ -102,8 +127,7 @@ target_compile_definitions(yolox_cpp
102127
target_compile_options(yolox_cpp PUBLIC -Wall)
103128

104129
target_include_directories(yolox_cpp PUBLIC
105-
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
106-
$<INSTALL_INTERFACE:include>
130+
${INCLUDES}
107131
)
108132

109133
ament_target_dependencies(yolox_cpp
@@ -132,6 +156,11 @@ if(ENABLE_ONNXRUNTIME)
132156
onnxruntime
133157
)
134158
endif()
159+
if(ENABLE_TFLITE)
160+
target_link_libraries(yolox_cpp
161+
${TFLITE_LIB_PATH}
162+
)
163+
endif()
135164

136165

137166

yolox_ros_cpp/yolox_cpp/include/yolox_cpp/config.h.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@
44
#cmakedefine ENABLE_OPENVINO
55
#cmakedefine ENABLE_TENSORRT
66
#cmakedefine ENABLE_ONNXRUNTIME
7+
#cmakedefine ENABLE_TFLITE
78

89
#endif // _YOLOX_CPP_CONFIG_H_

yolox_ros_cpp/yolox_cpp/include/yolox_cpp/core.hpp

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ namespace yolox_cpp{
3030
AbcYoloX(){}
3131
AbcYoloX(float nms_th=0.45, float conf_th=0.3,
3232
std::string model_version="0.1.1rc0",
33-
int num_classes=80)
33+
int num_classes=80, bool p6=false)
3434
:nms_thresh_(nms_th), bbox_conf_thresh_(conf_th),
35-
model_version_(model_version), num_classes_(num_classes)
35+
model_version_(model_version), num_classes_(num_classes), p6_(p6)
3636
{
3737
}
3838
virtual std::vector<Object> inference(const cv::Mat& frame) = 0;
@@ -42,10 +42,12 @@ namespace yolox_cpp{
4242
float nms_thresh_;
4343
float bbox_conf_thresh_;
4444
int num_classes_;
45+
bool p6_;
4546
std::string model_version_;
4647
const std::vector<float> mean_ = {0.485, 0.456, 0.406};
4748
const std::vector<float> std_ = {0.229, 0.224, 0.225};
4849
const std::vector<int> strides_ = {8, 16, 32};
50+
const std::vector<int> strides_p6_ = {8, 16, 32, 64};
4951
std::vector<GridAndStride> grid_strides_;
5052

5153
cv::Mat static_resize(const cv::Mat& img)
@@ -61,11 +63,12 @@ namespace yolox_cpp{
6163
return out;
6264
}
6365

66+
// for NCHW
6467
void blobFromImage(const cv::Mat& img, float *blob_data)
6568
{
66-
int channels = 3;
67-
int img_h = img.rows;
68-
int img_w = img.cols;
69+
size_t channels = 3;
70+
size_t img_h = img.rows;
71+
size_t img_w = img.cols;
6972
if(this->model_version_=="0.1.0"){
7073
for (size_t c = 0; c < channels; ++c)
7174
{
@@ -92,6 +95,32 @@ namespace yolox_cpp{
9295
}
9396
}
9497

98+
// for NHWC
99+
void blobFromImage_nhwc(const cv::Mat& img, float *blob_data)
100+
{
101+
size_t channels = 3;
102+
size_t img_h = img.rows;
103+
size_t img_w = img.cols;
104+
if(this->model_version_=="0.1.0"){
105+
for (size_t i = 0; i < img_h * img_w; ++i)
106+
{
107+
for (size_t c = 0; c < channels; ++c)
108+
{
109+
blob_data[i * channels + c] =
110+
((float)img.data[i * channels + c] / 255.0 - this->mean_[c]) / this->std_[c];
111+
}
112+
}
113+
}else{
114+
for (size_t i = 0; i < img_h * img_w; ++i)
115+
{
116+
for (size_t c = 0; c < channels; ++c)
117+
{
118+
blob_data[i * channels + c] = (float)img.data[i * channels + c]; // 0.1.1rc0 or later
119+
}
120+
}
121+
}
122+
}
123+
95124
void generate_grids_and_stride(const int target_w, const int target_h, const std::vector<int>& strides, std::vector<GridAndStride>& grid_strides)
96125
{
97126
for (auto stride : strides)

yolox_ros_cpp/yolox_cpp/include/yolox_cpp/yolox.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,9 @@
1515
#include "yolox_onnxruntime.hpp"
1616
#endif
1717

18+
#ifdef ENABLE_TFLITE
19+
#include "yolox_tflite.hpp"
20+
#endif
21+
1822

1923
#endif

0 commit comments

Comments
 (0)