Skip to content

Commit 170d43a

Browse files
authored
Merge pull request #49 from fateshelled/dev_cpp
Replace TensorRT deperecated function
2 parents 305bd60 + 2550ecd commit 170d43a

File tree

9 files changed

+124
-93
lines changed

9 files changed

+124
-93
lines changed

yolox_ros_cpp/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
## Requirements
44
- ROS2 Humble
5+
- ros-humble-generate-parameter-library
56
- OpenCV 4.x
67
- OpenVINO 2021.*
78
- TensorRT 8.x *
@@ -60,7 +61,7 @@ cd ~/ros2_ws
6061
- Download model using the following script.
6162
- https://github.com/PINTO0309/PINTO_model_zoo/blob/main/132_YOLOX/download_nano.sh
6263
- `curl -s https://raw.githubusercontent.com/PINTO0309/PINTO_model_zoo/main/132_YOLOX/download_nano.sh | bash`
63-
64+
6465
- ONNX model copy to weight dir
6566
- `cp resouces_new/saved_model_yolox_tiny_480x640/yolox_tiny_480x640.onnx ./src/YOLOX-ROS/weights/onnx/`
6667

yolox_ros_cpp/yolox_cpp/CMakeLists.txt

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,16 @@ endif()
4646

4747
if(YOLOX_USE_TENSORRT)
4848
find_package(CUDA REQUIRED)
49-
find_library(NVINFER NAMES nvinfer REQUIRED)
50-
find_library(NVINFERPLUGIN NAMES nvinfer_plugin REQUIRED)
51-
find_library(NVPARSERS NAMES nvparsers REQUIRED)
52-
find_library(NVONNXPARSER NAMES nvonnxparser REQUIRED)
49+
if (NOT CUDA_TOOLKIT_ROOT_DIR)
50+
set(CUDA_TOOLKIT_ROOT_DIR /usr/local/cuda)
51+
endif()
52+
find_path(TENSORRT_INCLUDE_DIR NvInfer.h
53+
HINTS ${TENSORRT_ROOT} ${CUDA_TOOLKIT_ROOT_DIR}
54+
PATH_SUFFIXES include)
5355

5456
set(ENABLE_TENSORRT ON)
5557
set(TARGET_SRC src/yolox_tensorrt.cpp)
56-
set(TARGET_LIBS nvinfer nvinfer_plugin nvparsers nvonnxparser)
58+
set(TARGET_LIBS nvinfer nvinfer_plugin)
5759
set(TARGET_DPENDENCIES CUDA)
5860
endif()
5961

yolox_ros_cpp/yolox_cpp/include/yolox_cpp/coco_names.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ namespace yolox_cpp{
9393
{0.857, 0.857, 0.857},
9494
{0.000, 0.447, 0.741},
9595
{0.314, 0.717, 0.741},
96-
{0.50, 0.5, 0}
96+
{0.500, 0.500, 0.000}
9797
};
9898
}
9999
#endif

yolox_ros_cpp/yolox_cpp/include/yolox_cpp/core.hpp

Lines changed: 35 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@ namespace yolox_cpp
88
/**
99
* @brief Define names based depends on Unicode path support
1010
*/
11-
#define tcout std::cout
1211
#define file_name_t std::string
13-
#define imread_t cv::imread
1412

1513
struct Object
1614
{
@@ -24,17 +22,21 @@ namespace yolox_cpp
2422
int grid0;
2523
int grid1;
2624
int stride;
25+
GridAndStride(const int grid0_, const int grid1_, const int stride_)
26+
: grid0(grid0_), grid1(grid1_), stride(stride_)
27+
{
28+
}
2729
};
2830

2931
class AbcYoloX
3032
{
3133
public:
3234
AbcYoloX() {}
33-
AbcYoloX(float nms_th = 0.45, float conf_th = 0.3,
34-
std::string model_version = "0.1.1rc0",
35-
int num_classes = 80, bool p6 = false)
35+
AbcYoloX(const float nms_th = 0.45, const float conf_th = 0.3,
36+
const std::string &model_version = "0.1.1rc0",
37+
const int num_classes = 80, const bool p6 = false)
3638
: nms_thresh_(nms_th), bbox_conf_thresh_(conf_th),
37-
model_version_(model_version), num_classes_(num_classes), p6_(p6)
39+
num_classes_(num_classes), p6_(p6), model_version_(model_version)
3840
{
3941
}
4042
virtual std::vector<Object> inference(const cv::Mat &frame) = 0;
@@ -55,10 +57,10 @@ namespace yolox_cpp
5557

5658
cv::Mat static_resize(const cv::Mat &img)
5759
{
58-
float r = std::min(input_w_ / (img.cols * 1.0), input_h_ / (img.rows * 1.0));
60+
const float r = std::min(input_w_ / (img.cols * 1.0), input_h_ / (img.rows * 1.0));
5961
// r = std::min(r, 1.0f);
60-
int unpad_w = r * img.cols;
61-
int unpad_h = r * img.rows;
62+
const int unpad_w = r * img.cols;
63+
const int unpad_h = r * img.rows;
6264
cv::Mat re(unpad_h, unpad_w, CV_8UC3);
6365
cv::resize(img, re, re.size());
6466
cv::Mat out(input_h_, input_w_, CV_8UC3, cv::Scalar(114, 114, 114));
@@ -69,9 +71,9 @@ namespace yolox_cpp
6971
// for NCHW
7072
void blobFromImage(const cv::Mat &img, float *blob_data)
7173
{
72-
size_t channels = 3;
73-
size_t img_h = img.rows;
74-
size_t img_w = img.cols;
74+
const size_t channels = 3;
75+
const size_t img_h = img.rows;
76+
const size_t img_w = img.cols;
7577
if (this->model_version_ == "0.1.0")
7678
{
7779
for (size_t c = 0; c < channels; ++c)
@@ -104,9 +106,9 @@ namespace yolox_cpp
104106
// for NHWC
105107
void blobFromImage_nhwc(const cv::Mat &img, float *blob_data)
106108
{
107-
size_t channels = 3;
108-
size_t img_h = img.rows;
109-
size_t img_w = img.cols;
109+
const size_t channels = 3;
110+
const size_t img_h = img.rows;
111+
const size_t img_w = img.cols;
110112
if (this->model_version_ == "0.1.0")
111113
{
112114
for (size_t i = 0; i < img_h * img_w; ++i)
@@ -134,19 +136,19 @@ namespace yolox_cpp
134136
{
135137
for (auto stride : strides)
136138
{
137-
int num_grid_w = target_w / stride;
138-
int num_grid_h = target_h / stride;
139+
const int num_grid_w = target_w / stride;
140+
const int num_grid_h = target_h / stride;
139141
for (int g1 = 0; g1 < num_grid_h; ++g1)
140142
{
141143
for (int g0 = 0; g0 < num_grid_w; ++g0)
142144
{
143-
grid_strides.push_back((GridAndStride){g0, g1, stride});
145+
grid_strides.emplace_back(g0, g1, stride);
144146
}
145147
}
146148
}
147149
}
148150

149-
void generate_yolox_proposals(const std::vector<GridAndStride> grid_strides, const float *feat_ptr, const float prob_threshold, std::vector<Object> &objects)
151+
void generate_yolox_proposals(const std::vector<GridAndStride> &grid_strides, const float *feat_ptr, const float prob_threshold, std::vector<Object> &objects)
150152
{
151153
const int num_anchors = grid_strides.size();
152154

@@ -158,13 +160,13 @@ namespace yolox_cpp
158160

159161
const int basic_pos = anchor_idx * (num_classes_ + 5);
160162

161-
float box_objectness = feat_ptr[basic_pos + 4];
163+
const float box_objectness = feat_ptr[basic_pos + 4];
162164
int class_id = 0;
163165
float max_class_score = 0.0;
164166
for (int class_idx = 0; class_idx < num_classes_; ++class_idx)
165167
{
166-
float box_cls_score = feat_ptr[basic_pos + 5 + class_idx];
167-
float box_prob = box_objectness * box_cls_score;
168+
const float box_cls_score = feat_ptr[basic_pos + 5 + class_idx];
169+
const float box_prob = box_objectness * box_cls_score;
168170
if (box_prob > max_class_score)
169171
{
170172
class_id = class_idx;
@@ -176,12 +178,12 @@ namespace yolox_cpp
176178
// yolox/models/yolo_head.py decode logic
177179
// outputs[..., :2] = (outputs[..., :2] + grids) * strides
178180
// outputs[..., 2:4] = torch.exp(outputs[..., 2:4]) * strides
179-
float x_center = (feat_ptr[basic_pos + 0] + grid0) * stride;
180-
float y_center = (feat_ptr[basic_pos + 1] + grid1) * stride;
181-
float w = exp(feat_ptr[basic_pos + 2]) * stride;
182-
float h = exp(feat_ptr[basic_pos + 3]) * stride;
183-
float x0 = x_center - w * 0.5f;
184-
float y0 = y_center - h * 0.5f;
181+
const float x_center = (feat_ptr[basic_pos + 0] + grid0) * stride;
182+
const float y_center = (feat_ptr[basic_pos + 1] + grid1) * stride;
183+
const float w = exp(feat_ptr[basic_pos + 2]) * stride;
184+
const float h = exp(feat_ptr[basic_pos + 3]) * stride;
185+
const float x0 = x_center - w * 0.5f;
186+
const float y0 = y_center - h * 0.5f;
185187

186188
Object obj;
187189
obj.rect.x = x0;
@@ -197,7 +199,7 @@ namespace yolox_cpp
197199

198200
float intersection_area(const Object &a, const Object &b)
199201
{
200-
cv::Rect_<float> inter = a.rect & b.rect;
202+
const cv::Rect_<float> inter = a.rect & b.rect;
201203
return inter.area();
202204
}
203205

@@ -260,8 +262,8 @@ namespace yolox_cpp
260262
const Object &b = faceobjects[picked[j]];
261263

262264
// intersection over union
263-
float inter_area = intersection_area(a, b);
264-
float union_area = areas[i] + areas[picked[j]] - inter_area;
265+
const float inter_area = intersection_area(a, b);
266+
const float union_area = areas[i] + areas[picked[j]] - inter_area;
265267
// float IoU = inter_area / union_area
266268
if (inter_area / union_area > nms_threshold)
267269
keep = 0;
@@ -272,7 +274,7 @@ namespace yolox_cpp
272274
}
273275
}
274276

275-
void decode_outputs(const float *prob, const std::vector<GridAndStride> grid_strides,
277+
void decode_outputs(const float *prob, const std::vector<GridAndStride> &grid_strides,
276278
std::vector<Object> &objects, const float bbox_conf_thresh,
277279
const float scale, const int img_w, const int img_h)
278280
{
@@ -312,4 +314,4 @@ namespace yolox_cpp
312314
}
313315
};
314316
}
315-
#endif
317+
#endif

yolox_ros_cpp/yolox_cpp/include/yolox_cpp/utils.hpp

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,39 +8,44 @@
88
#include "core.hpp"
99
#include "coco_names.hpp"
1010

11-
namespace yolox_cpp{
12-
namespace utils{
11+
namespace yolox_cpp
12+
{
13+
namespace utils
14+
{
1315

14-
static std::vector<std::string> read_class_labels_file(file_name_t file_name)
16+
static std::vector<std::string> read_class_labels_file(const file_name_t &file_name)
1517
{
1618
std::vector<std::string> class_names;
1719
std::ifstream ifs(file_name);
1820
std::string buff;
19-
if(ifs.fail()){
21+
if (ifs.fail())
22+
{
2023
return class_names;
2124
}
22-
while (getline(ifs, buff)) {
25+
while (getline(ifs, buff))
26+
{
2327
if (buff == "")
2428
continue;
2529
class_names.push_back(buff);
2630
}
2731
return class_names;
2832
}
2933

30-
static void draw_objects(cv::Mat bgr, const std::vector<Object>& objects, const std::vector<std::string>& class_names=COCO_CLASSES)
34+
static void draw_objects(cv::Mat bgr, const std::vector<Object> &objects, const std::vector<std::string> &class_names = COCO_CLASSES)
3135
{
3236

33-
for (size_t i = 0; i < objects.size(); i++)
37+
for (const Object &obj : objects)
3438
{
35-
const Object& obj = objects[i];
36-
37-
int color_index = obj.label % 80;
39+
const int color_index = obj.label % 80;
3840
cv::Scalar color = cv::Scalar(color_list[color_index][0], color_list[color_index][1], color_list[color_index][2]);
3941
float c_mean = cv::mean(color)[0];
4042
cv::Scalar txt_color;
41-
if (c_mean > 0.5){
43+
if (c_mean > 0.5)
44+
{
4245
txt_color = cv::Scalar(0, 0, 0);
43-
}else{
46+
}
47+
else
48+
{
4449
txt_color = cv::Scalar(255, 255, 255);
4550
}
4651

@@ -68,4 +73,4 @@ namespace yolox_cpp{
6873
}
6974
}
7075
}
71-
#endif
76+
#endif

yolox_ros_cpp/yolox_cpp/include/yolox_cpp/yolox_tensorrt.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ namespace yolox_cpp{
3636
YoloXTensorRT(file_name_t path_to_engine, int device=0,
3737
float nms_th=0.45, float conf_th=0.3, std::string model_version="0.1.1rc0",
3838
int num_classes=80, bool p6=false);
39+
~YoloXTensorRT();
3940
std::vector<Object> inference(const cv::Mat& frame) override;
4041

4142
private:
@@ -49,6 +50,7 @@ namespace yolox_cpp{
4950
int output_size_;
5051
const int inputIndex_ = 0;
5152
const int outputIndex_ = 1;
53+
void *inference_buffers_[2];
5254

5355
};
5456
} // namespace yolox_cpp

0 commit comments

Comments
 (0)