Skip to content

Commit fe6e64f

Browse files
author
lsf
committed
lsf pc update
1 parent 69fcf9f commit fe6e64f

File tree

15 files changed

+644
-34
lines changed

15 files changed

+644
-34
lines changed

apps/app_ptq.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
using namespace std;
2020

21-
class Logger : public nvinfer1::ILogger{
21+
class LLogger : public nvinfer1::ILogger{
2222
public:
2323
void log(Severity severity, const char* msg) noexcept override {
2424
if (severity == Severity::kINTERNAL_ERROR) {
@@ -38,7 +38,7 @@ class Logger : public nvinfer1::ILogger{
3838
}
3939
}
4040
};
41-
static Logger gLogger;
41+
static LLogger gLogger;
4242

4343
template<typename _T>
4444
static shared_ptr<_T> make_nvshared(_T* ptr){

apps/app_rtdetr.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ void performance(const string& engine_file, int gpuid){
7171
}
7272

7373
int batch = 8;
74-
std::vector<cv::Mat> images{cv::imread("imgs/car.jpg"), cv::imread("imgs/gril.jpg"),
74+
std::vector<cv::Mat> images{cv::imread("imgs/bus.jpg"), cv::imread("imgs/girl.jpg"),
7575
cv::imread("imgs/group.jpg"), cv::imread("imgs/yq.jpg")};
7676
for (int i = images.size(); i < batch; ++i)
7777
images.push_back(images[i % 4]);
@@ -122,7 +122,7 @@ void batch_inference(const string& engine_file, int gpuid){
122122
// 等待全部推理结束
123123
boxes_array.back().get();
124124

125-
string root_res = "infer_res";
125+
string root_res = "infer_res/rtdetr";
126126
for(int i = 0; i < boxes_array.size(); ++i){
127127
cv::Mat image = images[i];
128128
auto boxes = boxes_array[i].get();

apps/app_seg.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ void inference_seg(const string& engine_file, int gpuid){
4141
auto image = cv::imread("imgs/frame_3.jpg");
4242
auto res = predictor->seg(image);
4343

44-
cv::Mat color_img(res.size(), CV_8UC3, cv::Scalar(0, 0, 0));
44+
cv::Mat color_img(image.size(), CV_8UC3, cv::Scalar(0, 0, 0));
4545
// 遍历每个像素点,根据类别索引应用颜色映射
4646
for (int i = 0; i < res.rows; ++i) {
4747
for (int j = 0; j < res.cols; ++j) {
@@ -51,11 +51,9 @@ void inference_seg(const string& engine_file, int gpuid){
5151
PPSeg::color_map[pixel_value][0]);
5252
}
5353
}
54-
cv::Mat out_color_img;
55-
cv::resize(color_img, out_color_img, image.size());
56-
out_color_img.convertTo(out_color_img, CV_8UC3);
54+
cv::Mat out_color_img(image.size(), CV_8UC3, cv::Scalar(0, 0, 0));
5755
float alpha = 0.7;
58-
out_color_img = (1 - alpha) * image + alpha * out_color_img;
56+
out_color_img = (1 - alpha) * image + alpha * color_img;
5957
cv::imwrite("infer_res/seg_frame_3.jpg", out_color_img);
6058
}
6159

apps/app_yolov10.cpp

Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
2+
#include <fstream>
3+
#include <opencv2/opencv.hpp>
4+
#include "yolov10/yolov10.hpp"
5+
6+
using namespace std;
7+
8+
inline vector<string> cocolabels = {
9+
"person", "bicycle", "car", "motorcycle", "airplane",
10+
"bus", "train", "truck", "boat", "traffic light", "fire hydrant",
11+
"stop sign", "parking meter", "bench", "bird", "cat", "dog", "horse",
12+
"sheep", "cow", "elephant", "bear", "zebra", "giraffe", "backpack",
13+
"umbrella", "handbag", "tie", "suitcase", "frisbee", "skis",
14+
"snowboard", "sports ball", "kite", "baseball bat", "baseball glove",
15+
"skateboard", "surfboard", "tennis racket", "bottle", "wine glass",
16+
"cup", "fork", "knife", "spoon", "bowl", "banana", "apple", "sandwich",
17+
"orange", "broccoli", "carrot", "hot dog", "pizza", "donut", "cake",
18+
"chair", "couch", "potted plant", "bed", "dining table", "toilet", "tv",
19+
"laptop", "mouse", "remote", "keyboard", "cell phone", "microwave",
20+
"oven", "toaster", "sink", "refrigerator", "book", "clock", "vase",
21+
"scissors", "teddy bear", "hair drier", "toothbrush"
22+
};
23+
24+
inline std::tuple<uint8_t, uint8_t, uint8_t> hsv2bgr(float h, float s, float v){
25+
const int h_i = static_cast<int>(h * 6);
26+
const float f = h * 6 - h_i;
27+
const float p = v * (1 - s);
28+
const float q = v * (1 - f*s);
29+
const float t = v * (1 - (1 - f) * s);
30+
float r, g, b;
31+
switch (h_i) {
32+
case 0:r = v; g = t; b = p;break;
33+
case 1:r = q; g = v; b = p;break;
34+
case 2:r = p; g = v; b = t;break;
35+
case 3:r = p; g = q; b = v;break;
36+
case 4:r = t; g = p; b = v;break;
37+
case 5:r = v; g = p; b = q;break;
38+
default:r = 1; g = 1; b = 1;break;}
39+
return make_tuple(static_cast<uint8_t>(b * 255), static_cast<uint8_t>(g * 255), static_cast<uint8_t>(r * 255));
40+
}
41+
42+
inline std::tuple<uint8_t, uint8_t, uint8_t> random_color(int id){
43+
float h_plane = ((((unsigned int)id << 2) ^ 0x937151) % 100) / 100.0f;;
44+
float s_plane = ((((unsigned int)id << 3) ^ 0x315793) % 100) / 100.0f;
45+
return hsv2bgr(h_plane, s_plane, 1);
46+
}
47+
48+
inline string get_file_name(const string& path, bool include_suffix){
49+
if (path.empty()) return "";
50+
int p = path.rfind('/');
51+
int e = path.rfind('\\');
52+
p = std::max(p, e);
53+
p += 1;
54+
//include suffix
55+
if (include_suffix)
56+
return path.substr(p);
57+
int u = path.rfind('.');
58+
if (u == -1)
59+
return path.substr(p);
60+
61+
if (u <= p) u = path.size();
62+
return path.substr(p, u - p);
63+
}
64+
65+
void performance_v10(const string& engine_file, int gpuid){
66+
auto infer = YOLOV10::create_infer(engine_file, gpuid, 0.5);
67+
if(infer == nullptr){
68+
printf("infer is nullptr.\n");
69+
return;
70+
}
71+
72+
int batch = 1;
73+
std::vector<cv::Mat> images{cv::imread("imgs/bus.jpg"), cv::imread("imgs/girl.jpg"),
74+
cv::imread("imgs/group.jpg"), cv::imread("imgs/yq.jpg")};
75+
for (int i = images.size(); i < batch; ++i)
76+
images.push_back(images[i % 4]);
77+
78+
// warmup
79+
vector<shared_future<YOLOV10::BoxArray>> boxes_array;
80+
for(int i = 0; i < 10; ++i)
81+
boxes_array = infer->commits(images);
82+
boxes_array.back().get();
83+
boxes_array.clear();
84+
85+
// 测试 100 轮
86+
const int ntest = 100;
87+
auto start = std::chrono::steady_clock::now();
88+
for(int i = 0; i < ntest; ++i)
89+
boxes_array = infer->commits(images);
90+
// 等待全部推理结束
91+
boxes_array.back().get();
92+
93+
std::chrono::duration<double> during = std::chrono::steady_clock::now() - start;
94+
double all_time = 1000.0 * during.count();
95+
float avg_time = all_time / ntest / images.size();
96+
printf("Average time: %.2f ms, FPS: %.2f\n", engine_file.c_str(), avg_time, 1000 / avg_time);
97+
}
98+
99+
void batch_inference_v10(const string& engine_file, int gpuid){
100+
auto infer = YOLOV10::create_infer(engine_file, gpuid, 0.5);
101+
if(infer == nullptr){
102+
printf("infer is nullptr.\n");
103+
return;
104+
}
105+
106+
vector<cv::String> files_;
107+
files_.reserve(100);
108+
cv::glob("imgs/*.jpg", files_, true);
109+
vector<string> files(files_.begin(), files_.end());
110+
111+
vector<cv::Mat> images;
112+
for(const auto& file : files){
113+
auto image = cv::imread(file);
114+
images.emplace_back(image);
115+
}
116+
117+
vector<shared_future<YOLOV10::BoxArray>> boxes_array;
118+
boxes_array = infer->commits(images);
119+
120+
// 等待全部推理结束
121+
boxes_array.back().get();
122+
123+
string root_res = "infer_res/yolov10";
124+
for(int i = 0; i < boxes_array.size(); ++i){
125+
cv::Mat image = images[i];
126+
auto boxes = boxes_array[i].get();
127+
for(auto & ibox : boxes){
128+
cv::Scalar color;
129+
std::tie(color[0], color[1], color[2]) = random_color(ibox.label);
130+
cv::rectangle(image, cv::Point(ibox.left, ibox.top), cv::Point(ibox.right, ibox.bottom), color, 2);
131+
132+
auto name = cocolabels[ibox.label];
133+
auto caption = cv::format("%s %.2f", name.c_str(), ibox.confidence);
134+
int text_width = cv::getTextSize(caption, 0, 1, 2, nullptr).width + 10;
135+
cv::rectangle(image, cv::Point(ibox.left-2, ibox.top-32), cv::Point(ibox.left + text_width, ibox.top), color, -1);
136+
cv::putText(image, caption, cv::Point(ibox.left, ibox.top-5), 0, 1, cv::Scalar::all(0), 2, 16);
137+
}
138+
string file_name = get_file_name(files[i], false);
139+
string save_path = cv::format("%s/%s.jpg", root_res.c_str(), file_name.c_str());
140+
cv::imwrite(save_path, image);
141+
printf("Save to %s, %d object\n", save_path.c_str(), boxes.size());
142+
}
143+
}
144+
145+
146+
void single_inference_v10(const string& engine_file, int gpuid){
147+
auto infer = YOLOV10::create_infer(engine_file, gpuid, 0.6);
148+
if(infer == nullptr){
149+
printf("infer is nullptr.\n");
150+
return;
151+
}
152+
153+
auto image = cv::imread("imgs/bus.jpg");
154+
auto boxes = infer->commit(image).get();
155+
156+
for(auto& ibox : boxes){
157+
cv::Scalar color;
158+
std::tie(color[0], color[1], color[2]) = random_color(ibox.label);
159+
cv::rectangle(image, cv::Point(ibox.left, ibox.top), cv::Point(ibox.right, ibox.bottom), color, 2);
160+
161+
auto name = cocolabels[ibox.label];
162+
auto caption = cv::format("%s %.2f", name.c_str(), ibox.confidence);
163+
int text_width = cv::getTextSize(caption, 0, 1, 2, nullptr).width + 10;
164+
cv::rectangle(image, cv::Point(ibox.left-2, ibox.top-32), cv::Point(ibox.left + text_width, ibox.top), color, -1);
165+
cv::putText(image, caption, cv::Point(ibox.left, ibox.top-5), 0, 1, cv::Scalar::all(0), 2, 16);
166+
}
167+
cv::imwrite("infer_res/result.jpg", image);
168+
}
169+
170+
171+

apps/ppseg/ppseg.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
//
2-
// Created by lsf on 24-5-20.
3-
//
41

52
#include "ppseg.hpp"
63

@@ -91,6 +88,7 @@ namespace PPSeg{
9188
parray, input_width_ * sizeof(int32_t));
9289
cv::Mat out;
9390
out_img.convertTo(out, CV_8UC1);
91+
cv::resize(out, out, image.size(), 0, 0, cv::INTER_NEAREST);
9492

9593
return out;
9694
}

apps/ppseg/ppseg.hpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
//
2-
// Created by lsf on 24-5-20.
3-
//
41

52
#ifndef PPSEG_HPP
63
#define PPSEG_HPP

apps/rtdetr/README.md

Lines changed: 0 additions & 3 deletions
This file was deleted.

apps/yolo/yolo.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ namespace Yolo{
156156
// output_array_device 是输出 output 经过 decode 的结果
157157
TRT::Tensor output_array_device{};
158158

159-
// load success:设置好了输入,宽高,batch,allocator等,返回true
159+
/// load success:设置好了输入,宽高,batch,allocator等,返回true
160160
result.set_value(true);
161161

162162
// 先调整 shape 再 分配 input Tensor GPU 内存

0 commit comments

Comments
 (0)