Skip to content

Commit 035f2cc

Browse files
committed
1
1
1 parent 718f2df commit 035f2cc

File tree

6 files changed

+408
-11
lines changed

6 files changed

+408
-11
lines changed

tensorrtx_my/yolov5/CMakeLists.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,15 +53,15 @@ ELSEIF(CMAKE_SYSTEM_NAME MATCHES "Windows")
5353
set(CUDA_NVCC_PLAGS ${CUDA_NVCC_PLAGS};-std=c++11; -g; -G;-gencode; arch=compute_75;code=sm_75)
5454
find_package(CUDA REQUIRED)
5555
include_directories(${CUDA_INCLUDE_DIRS})
56-
enable_language(CUDA) # add this line, then no need to setup cuda path in vs
56+
#enable_language(CUDA) # add this line, then no need to setup cuda path in vs
5757

5858
# tensorrt
59-
set(TRT_DIR "C://tools//TensorRT-7.0.0.11//") #3
59+
set(TRT_DIR "D:/SDK/TensorRT-8.2.0.6//") #3
6060
include_directories(${TRT_DIR}\\include)
6161
link_directories(${TRT_DIR}\\lib)
6262

6363
# setup opencv
64-
set(OpenCV_DIR "C:/tools/opencv/build/") #2
64+
set(OpenCV_DIR "D:/sheepy/tools/opencv455/build/") #2
6565
find_package(OpenCV REQUIRED)
6666
include_directories(${OpenCV_INCLUDE_DIRS})
6767
# Threads

tensorrtx_my/yolov5_7.0/CMakeLists.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,11 @@ ELSEIF(CMAKE_SYSTEM_NAME MATCHES "Windows")
5454
set(CUDA_NVCC_PLAGS ${CUDA_NVCC_PLAGS};-std=c++11; -g; -G;-gencode; arch=compute_75;code=sm_75)
5555
find_package(CUDA REQUIRED)
5656
include_directories(${CUDA_INCLUDE_DIRS})
57-
link_directories("C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v11.3/lib/x64/")
57+
link_directories("C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v11.1/lib/x64/")
5858
enable_language(CUDA) # add this line, then no need to setup cuda path in vs
5959

6060
# tensorrt
61-
set(TRT_DIR "D://tools//TensorRT-8.0.1.6-cu11.3//") #3
61+
set(TRT_DIR "D:/tools/TensorRT-8.2.0.6//") #3
6262
include_directories(${TRT_DIR}\\include)
6363
link_directories(${TRT_DIR}\\lib)
6464

@@ -104,7 +104,7 @@ ELSEIF(CMAKE_SYSTEM_NAME MATCHES "Windows")
104104
ENDIF()
105105

106106
#生成目标文件
107-
add_executable(${PROJECT_NAME} ${PROJECT_SOURCE_DIR}/yolov5_det.cpp ${SRC_FILES} ${SRC_CU_FILES} ${INC_FILES})
107+
add_executable(${PROJECT_NAME} ${PROJECT_SOURCE_DIR}/yolov5_seg.cpp ${SRC_FILES} ${SRC_CU_FILES} ${INC_FILES})
108108

109109
#target_link_libraries(${PROJECT_NAME} ${LIB_NAME})
110110
target_link_libraries(${PROJECT_NAME} nvinfer cudart nvinfer_plugin ${CUDA_LIBRARIES})

tensorrtx_my/yolov5_7.0/include/config.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ constexpr static int kClsNumClass = 1000;
2323
constexpr static int kBatchSize = 1;
2424

2525
// Yolo's input width and height must by divisible by 32
26-
constexpr static int kInputH = 320;
27-
constexpr static int kInputW = 320;
26+
constexpr static int kInputH = 1024;
27+
constexpr static int kInputW = 1024;
2828

2929
// Classfication model's input shape
3030
constexpr static int kClsInputH = 224;
@@ -46,7 +46,7 @@ constexpr static float kIgnoreThresh = 0.1f;
4646

4747
// NMS overlapping thresh and final detection confidence thresh
4848
const static float kNmsThresh = 0.45f;
49-
const static float kConfThresh = 0.5f;
49+
const static float kConfThresh = 0.6f;
5050

5151
const static int kGpuId = 0;
5252

tensorrtx_my/yolov5_7.0/seg_best.wts

Lines changed: 370 additions & 0 deletions
Large diffs are not rendered by default.

tensorrtx_my/yolov5_7.0/src/postprocess.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,15 @@ cv::Rect get_rect(cv::Mat& img, float bbox[4]) {
2424
t = t / r_h;
2525
b = b / r_h;
2626
}
27+
if (l < 0) {
28+
l = 0;
29+
}
30+
if (b >= 2048) {
31+
b = 2048;
32+
}
33+
if (r >= 2448) {
34+
r = 2448;
35+
}
2736
return cv::Rect(round(l), round(t), round(r - l), round(b - t));
2837
}
2938

@@ -93,6 +102,9 @@ void draw_bbox(std::vector<cv::Mat>& img_batch, std::vector<std::vector<Detectio
93102

94103
static cv::Rect get_downscale_rect(float bbox[4], float scale) {
95104
float left = bbox[0] - bbox[2] / 2;
105+
if (left < 0) {
106+
left = 0;
107+
}
96108
float top = bbox[1] - bbox[3] / 2;
97109
float right = bbox[0] + bbox[2] / 2;
98110
float bottom = bbox[1] + bbox[3] / 2;
@@ -110,11 +122,17 @@ std::vector<cv::Mat> process_mask(const float* proto, int proto_size, std::vecto
110122
auto r = get_downscale_rect(dets[i].bbox, 4);
111123
for (int x = r.x; x < r.x + r.width; x++) {
112124
for (int y = r.y; y < r.y + r.height; y++) {
125+
if ((x<=0)||(y<=0)) {
126+
continue;
127+
}
113128
float e = 0.0f;
114129
for (int j = 0; j < 32; j++) {
115130
e += dets[i].mask[j] * proto[j * proto_size / 32 + y * mask_mat.cols + x];
116131
}
117132
e = 1.0f / (1.0f + expf(-e));
133+
if ((x>= kInputW / 4)||(y>= kInputH / 4)) {
134+
continue;
135+
}
118136
mask_mat.at<float>(y, x) = e;
119137
}
120138
}
@@ -158,6 +176,9 @@ void draw_mask_bbox(cv::Mat& img, std::vector<Detection>& dets, std::vector<cv::
158176
cv::Rect r = get_rect(img, dets[i].bbox);
159177
for (int x = r.x; x < r.x + r.width; x++) {
160178
for (int y = r.y; y < r.y + r.height; y++) {
179+
if ((x <= 0) || (y <= 0)) {
180+
continue;
181+
}
161182
float val = img_mask.at<float>(y, x);
162183
if (val <= 0.5) continue;
163184
img.at<cv::Vec3b>(y, x)[0] = img.at<cv::Vec3b>(y, x)[0] / 2 + bgr[0] / 2;

tensorrtx_my/yolov5_7.0/yolov5_seg.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,11 +194,12 @@ int main(int argc, char** argv) {
194194

195195
// batch predict
196196
for (size_t i = 0; i < file_names.size(); i += kBatchSize) {
197+
std::cout << "i:" << i << std::endl;
197198
// Get a batch of images
198199
std::vector<cv::Mat> img_batch;
199200
std::vector<std::string> img_name_batch;
200201
for (size_t j = i; j < i + kBatchSize && j < file_names.size(); j++) {
201-
cv::Mat img = cv::imread(img_dir + "/" + file_names[j]);
202+
cv::Mat img = cv::imread(img_dir + "/" + file_names[j]);
202203
img_batch.push_back(img);
203204
img_name_batch.push_back(file_names[j]);
204205
}
@@ -220,10 +221,15 @@ int main(int argc, char** argv) {
220221
for (size_t b = 0; b < img_name_batch.size(); b++) {
221222
auto& res = res_batch[b];
222223
cv::Mat img = img_batch[b];
224+
cv::Mat img_org;
225+
img_batch[b].copyTo(img_org);
223226

224227
auto masks = process_mask(&cpu_output_buffer2[b * kOutputSize2], kOutputSize2, res);
225228
draw_mask_bbox(img, res, masks, labels_map);
226-
cv::imwrite("_" + img_name_batch[b], img);
229+
if (res.size() > 0) {
230+
cv::imwrite("result/" + img_name_batch[b], img);
231+
cv::imwrite("NG/" + img_name_batch[b], img_org);
232+
}
227233
}
228234
}
229235

0 commit comments

Comments
 (0)