Skip to content

Commit 22da61d

Browse files
committed
Working switch between cuda and non cuda; code not working
1 parent a1e06ed commit 22da61d

File tree

5 files changed

+147
-93
lines changed

5 files changed

+147
-93
lines changed

CMakeLists.txt

Lines changed: 46 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,37 +20,48 @@ find_package(catkin REQUIRED
2020
)
2121

2222

23+
set(${PROJECT_NAME}_CUDA_ENABLED ${onnxruntime_ros_CUDA_ENABLED})
24+
if(onnxruntime_ros_CUDA_ENABLED)
25+
find_package(CUDAToolkit REQUIRED)
26+
endif()
27+
28+
configure_file(include/${PROJECT_NAME}/config.hpp.in ${CATKIN_DEVEL_PREFIX}/${CATKIN_GLOBAL_INCLUDE_DESTINATION}/${PROJECT_NAME}/config.hpp)
29+
# add_custom_target(generate_config_hpp
30+
# DEPENDS ${CATKIN_DEVEL_PREFIX}/${CATKIN_GLOBAL_INCLUDE_DESTINATION}/${PROJECT_NAME}/config.hpp
31+
# )
32+
2333
# ------------------------------------------------------------------------------------------------
2434
# CATKIN EXPORT
2535
# ------------------------------------------------------------------------------------------------
2636

2737
catkin_package(
28-
INCLUDE_DIRS include
38+
INCLUDE_DIRS include ${CATKIN_DEVEL_PREFIX}/${CATKIN_GLOBAL_INCLUDE_DESTINATION}
2939
LIBRARIES ${PROJECT_NAME}
3040
CATKIN_DEPENDS
31-
DEPENDS OpenCV
41+
DEPENDS CUDAToolkit OpenCV
3242
)
3343

44+
3445
# ------------------------------------------------------------------------------------------------
3546
# BUILD
3647
# ------------------------------------------------------------------------------------------------
3748

3849
include_directories(
3950
include
51+
${CATKIN_DEVEL_PREFIX}/${CATKIN_GLOBAL_INCLUDE_DESTINATION}
4052
SYSTEM
53+
${CUDAToolkit_INCLUDE_DIRS}
4154
${OpenCV_INCLUDE_DIRS}
4255
${catkin_INCLUDE_DIRS}
4356
)
4457

4558
# -------------- ONNXRuntime ------------------#
4659

47-
# -------------- Cuda ------------------#
48-
add_definitions(-DUSE_CUDA=1)
49-
5060
add_library(${PROJECT_NAME}
5161
src/yolo_inference.cpp
5262
)
53-
target_link_libraries(${PROJECT_NAME} ${OpenCV_LIBRARIES} ${catkin_LIBRARIES})
63+
# add_dependencies(${PROJECT_NAME} generate_config_hpp)
64+
target_link_libraries(${PROJECT_NAME} CUDA::toolkit ${OpenCV_LIBRARIES} ${catkin_LIBRARIES})
5465

5566
add_executable(test_${PROJECT_NAME}
5667
src/main.cpp
@@ -65,6 +76,35 @@ if (WIN32)
6576
$<TARGET_FILE_DIR:${PROJECT_NAME}>)
6677
endif ()
6778

79+
80+
# ------------------------------------------------------------------------------------------------
81+
# INSTALL
82+
# ------------------------------------------------------------------------------------------------
83+
84+
install(FILES
85+
${CATKIN_DEVEL_PREFIX}/${CATKIN_PACKAGE_INCLUDE_DESTINATION}/config.hpp
86+
DESTINATION ${CATKIN_PACKAGE_INCLUDE_DESTINATION}
87+
)
88+
89+
install(
90+
DIRECTORY include/${PROJECT_NAME}/
91+
DESTINATION ${CATKIN_PACKAGE_INCLUDE_DESTINATION}
92+
)
93+
94+
install(
95+
TARGETS
96+
${PROJECT_NAME}
97+
ARCHIVE DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION}
98+
LIBRARY DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION}
99+
RUNTIME DESTINATION ${CATKIN_GLOBAL_BIN_DESTINATION}
100+
)
101+
102+
install(
103+
TARGETS
104+
${PROJECT_NAME}
105+
DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION}
106+
)
107+
68108
# Download https://raw.githubusercontent.com/ultralytics/ultralytics/main/ultralytics/cfg/datasets/coco.yaml
69109
# and put it in the same folder of the executable file
70110
configure_file(data/coco.yaml ${CMAKE_CURRENT_BINARY_DIR}/coco.yaml COPYONLY)
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#ifndef YOLO_ONNX_ROS_CONFIG_HPP_
2+
#define YOLO_ONNX_ROS_CONFIG_HPP_
3+
4+
//Set which version of the Tree Interface to use
5+
#define YOLO_ONNX_ROS_CUDA_ENABLED @onnx_yolo_ros_CUDA_ENABLED@
6+
7+
#endif //#define YOLO_ONNX_ROS_CONFIG_HPP_

include/yolo_onnx_ros/yolo_inference.h

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414
#include <opencv2/opencv.hpp>
1515
#include <onnxruntime_cxx_api.h>
1616

17-
#ifdef USE_CUDA
17+
#include <yolo_onnx_ros/config.hpp>
18+
19+
#ifdef YOLO_ONNX_ROS_CUDA_ENABLED
1820
#include <cuda_fp16.h>
1921
#endif
2022

@@ -75,21 +77,21 @@ class YOLO_V8
7577

7678
// Note: The logic is on the .cpp file since its a private method.
7779
template<typename N>
78-
char* TensorProcess(clock_t& starttime_1, const cv::Mat& iImg, N& blob, std::vector<int64_t>& inputNodeDims,
80+
char* TensorProcess(clock_t& starttime_1, N& blob, std::vector<int64_t>& inputNodeDims,
7981
std::vector<DL_RESULT>& oResult);
8082

8183
char* PreProcess(const cv::Mat& iImg, std::vector<int> iImgSize, cv::Mat& oImg);
8284

83-
Ort::Env env;
84-
std::unique_ptr<Ort::Session> session;
85-
bool cudaEnable;
86-
Ort::RunOptions options;
87-
std::vector<const char*> inputNodeNames;
88-
std::vector<const char*> outputNodeNames;
89-
90-
MODEL_TYPE modelType;
91-
std::vector<int> imgSize;
92-
float rectConfidenceThreshold;
93-
float iouThreshold;
94-
float resizeScales;//letterbox scale
85+
Ort::Env env_;
86+
std::unique_ptr<Ort::Session> session_;
87+
bool cudaEnable_;
88+
Ort::RunOptions options_;
89+
std::vector<const char*> inputNodeNames_;
90+
std::vector<const char*> outputNodeNames_;
91+
92+
MODEL_TYPE modelType_;
93+
std::vector<int> imgSize_;
94+
float rectConfidenceThreshold_;
95+
float iouThreshold_;
96+
float resizeScales_; //letterbox scale
9597
};

src/main.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include <iostream>
22
#include <iomanip>
33
#include "yolo_onnx_ros/yolo_inference.h"
4+
#include <yolo_onnx_ros/config.hpp>
45
#include <filesystem>
56
#include <fstream>
67
#include <random>
@@ -76,7 +77,7 @@ void Classifier(std::unique_ptr<YOLO_V8>& p)
7677
const char* ret = p->RunSession(img, res);
7778

7879
float positionY = 50;
79-
for (int i = 0; i < res.size(); i++)
80+
for (uint i = 0; i < res.size(); i++)
8081
{
8182
int r = dis(gen);
8283
int g = dis(gen);
@@ -157,7 +158,7 @@ void DetectTest()
157158
params.iouThreshold = 0.5;
158159
params.modelPath = "yolo11m.onnx";
159160
params.imgSize = { 640, 640 };
160-
#ifdef USE_CUDA
161+
#ifdef YOLO_ONNX_ROS_CUDA_ENABLED
161162
params.cudaEnable = true;
162163

163164
// GPU FP32 inference

0 commit comments

Comments
 (0)