forked from Guanbin-Huang/tensorRT_Pro_co-comments
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app_yolo.cpp
190 lines (156 loc) · 7.64 KB
/
app_yolo.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#include <builder/trt_builder.hpp>
#include <infer/trt_infer.hpp>
#include <common/ilogger.hpp>
#include "app_yolo/yolo.hpp"
using namespace std;
static const char* cocolabels[] = {
"person", "bicycle", "car", "motorcycle", "airplane",
"bus", "train", "truck", "boat", "traffic light", "fire hydrant",
"stop sign", "parking meter", "bench", "bird", "cat", "dog", "horse",
"sheep", "cow", "elephant", "bear", "zebra", "giraffe", "backpack",
"umbrella", "handbag", "tie", "suitcase", "frisbee", "skis",
"snowboard", "sports ball", "kite", "baseball bat", "baseball glove",
"skateboard", "surfboard", "tennis racket", "bottle", "wine glass",
"cup", "fork", "knife", "spoon", "bowl", "banana", "apple", "sandwich",
"orange", "broccoli", "carrot", "hot dog", "pizza", "donut", "cake",
"chair", "couch", "potted plant", "bed", "dining table", "toilet", "tv",
"laptop", "mouse", "remote", "keyboard", "cell phone", "microwave",
"oven", "toaster", "sink", "refrigerator", "book", "clock", "vase",
"scissors", "teddy bear", "hair drier", "toothbrush"
};
bool requires(const char* name);
static void append_to_file(const string& file, const string& data){
FILE* f = fopen(file.c_str(), "a+");
if(f == nullptr){
INFOE("Open %s failed.", file.c_str());
return;
}
fprintf(f, "%s\n", data.c_str());
fclose(f);
}
static void inference_and_performance(int deviceid, const string& engine_file, TRT::Mode mode, Yolo::Type type, const string& model_name){
auto engine = Yolo::create_infer(engine_file, type, deviceid, 0.25f, 0.45f);
if(engine == nullptr){
INFOE("Engine is nullptr");
return;
}
auto files = iLogger::find_files("inference", "*.jpg;*.jpeg;*.png;*.gif;*.tif");
vector<cv::Mat> images;
for(int i = 0; i < files.size(); ++i){
auto image = cv::imread(files[i]);
images.emplace_back(image);
}
// warmup
vector<shared_future<Yolo::BoxArray>> boxes_array;
for(int i = 0; i < 10; ++i)
boxes_array = engine->commits(images);
boxes_array.back().get();
boxes_array.clear();
/////////////////////////////////////////////////////////
const int ntest = 100;
auto begin_timer = iLogger::timestamp_now_float();
for(int i = 0; i < ntest; ++i)
boxes_array = engine->commits(images);
// wait all result
boxes_array.back().get();
float inference_average_time = (iLogger::timestamp_now_float() - begin_timer) / ntest / images.size();
auto type_name = Yolo::type_name(type);
auto mode_name = TRT::mode_string(mode);
INFO("%s[%s] average: %.2f ms / image, FPS: %.2f", engine_file.c_str(), type_name, inference_average_time, 1000 / inference_average_time);
append_to_file("perf.result.log", iLogger::format("%s,%s,%s,%f", model_name.c_str(), type_name, mode_name, inference_average_time));
string root = iLogger::format("%s_%s_%s_result", model_name.c_str(), type_name, mode_name);
iLogger::rmtree(root);
iLogger::mkdir(root);
for(int i = 0; i < boxes_array.size(); ++i){
auto& image = images[i];
auto boxes = boxes_array[i].get();
for(auto& obj : boxes){
uint8_t b, g, r;
tie(b, g, r) = iLogger::random_color(obj.class_label);
cv::rectangle(image, cv::Point(obj.left, obj.top), cv::Point(obj.right, obj.bottom), cv::Scalar(b, g, r), 5);
auto name = cocolabels[obj.class_label];
auto caption = iLogger::format("%s %.2f", name, obj.confidence);
int width = cv::getTextSize(caption, 0, 1, 2, nullptr).width + 10;
cv::rectangle(image, cv::Point(obj.left-3, obj.top-33), cv::Point(obj.left + width, obj.top), cv::Scalar(b, g, r), -1);
cv::putText(image, caption, cv::Point(obj.left, obj.top-5), 0, 1, cv::Scalar::all(0), 2, 16);
}
string file_name = iLogger::file_name(files[i], false);
string save_path = iLogger::format("%s/%s.jpg", root.c_str(), file_name.c_str());
INFO("Save to %s, %d object, average time %.2f ms", save_path.c_str(), boxes.size(), inference_average_time);
cv::imwrite(save_path, image);
}
engine.reset();
}
static void test(Yolo::Type type, TRT::Mode mode, const string& model){
int deviceid = 0;
auto mode_name = TRT::mode_string(mode);
TRT::set_device(deviceid);
auto int8process = [=](int current, int count, const vector<string>& files, shared_ptr<TRT::Tensor>& tensor){
INFO("Int8 %d / %d", current, count);
for(int i = 0; i < files.size(); ++i){
auto image = cv::imread(files[i]);
Yolo::image_to_tensor(image, tensor, type, i);
}
};
const char* name = model.c_str();
INFO("===================== test %s %s %s ==================================", Yolo::type_name(type), mode_name, name);
if(not requires(name))
return;
string onnx_file = iLogger::format("%s.onnx", name);
string model_file = iLogger::format("%s.%s.trtmodel", name, mode_name);
int test_batch_size = 16;
if(not iLogger::exists(model_file)){
TRT::compile(
mode, // FP32、FP16、INT8
test_batch_size, // max batch size
onnx_file, // source
model_file, // save to
{},
int8process,
"inference"
);
}
inference_and_performance(deviceid, model_file, mode, type, name);
}
int app_yolo(){
//iLogger::set_log_level(iLogger::LogLevel::Debug);
//test(Yolo::Type::X, TRT::Mode::FP32, "yolox_s");
//iLogger::set_log_level(iLogger::LogLevel::Info);
// test(Yolo::Type::X, TRT::Mode::FP32, "yolox_x");
// test(Yolo::Type::X, TRT::Mode::FP32, "yolox_l");
// test(Yolo::Type::X, TRT::Mode::FP32, "yolox_m");
// test(Yolo::Type::X, TRT::Mode::FP32, "yolox_s");
// test(Yolo::Type::X, TRT::Mode::FP16, "yolox_x");
// test(Yolo::Type::X, TRT::Mode::FP16, "yolox_l");
// test(Yolo::Type::X, TRT::Mode::FP16, "yolox_m");
// test(Yolo::Type::X, TRT::Mode::FP16, "yolox_s");
// test(Yolo::Type::X, TRT::Mode::INT8, "yolox_x");
// test(Yolo::Type::X, TRT::Mode::INT8, "yolox_l");
// test(Yolo::Type::X, TRT::Mode::INT8, "yolox_m");
// test(Yolo::Type::X, TRT::Mode::INT8, "yolox_s");
// test(Yolo::Type::V5, TRT::Mode::FP32, "yolov5x6");
// test(Yolo::Type::V5, TRT::Mode::FP32, "yolov5l6");
// test(Yolo::Type::V5, TRT::Mode::FP32, "yolov5m6");
// test(Yolo::Type::V5, TRT::Mode::FP32, "yolov5s6");
// test(Yolo::Type::V5, TRT::Mode::FP32, "yolov5x");
// test(Yolo::Type::V5, TRT::Mode::FP32, "yolov5l");
// test(Yolo::Type::V5, TRT::Mode::FP32, "yolov5m");
test(Yolo::Type::V5, TRT::Mode::FP32, "yolov5s");
// test(Yolo::Type::V5, TRT::Mode::FP16, "yolov5x6");
// test(Yolo::Type::V5, TRT::Mode::FP16, "yolov5l6");
// test(Yolo::Type::V5, TRT::Mode::FP16, "yolov5m6");
// test(Yolo::Type::V5, TRT::Mode::FP16, "yolov5s6");
// test(Yolo::Type::V5, TRT::Mode::FP16, "yolov5x");
// test(Yolo::Type::V5, TRT::Mode::FP16, "yolov5l");
// test(Yolo::Type::V5, TRT::Mode::FP16, "yolov5m");
//test(Yolo::Type::V5, TRT::Mode::FP32, "yolov5s");
// test(Yolo::Type::V5, TRT::Mode::INT8, "yolov5x6");
// test(Yolo::Type::V5, TRT::Mode::INT8, "yolov5l6");
// test(Yolo::Type::V5, TRT::Mode::INT8, "yolov5m6");
// test(Yolo::Type::V5, TRT::Mode::INT8, "yolov5s6");
// test(Yolo::Type::V5, TRT::Mode::INT8, "yolov5x");
// test(Yolo::Type::V5, TRT::Mode::INT8, "yolov5l");
// test(Yolo::Type::V5, TRT::Mode::INT8, "yolov5m");
// test(Yolo::Type::V5, TRT::Mode::INT8, "yolov5s");
return 0;
}