forked from hopef/tensorRT_Pro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app_yolo_fast.cpp
201 lines (163 loc) · 8.45 KB
/
app_yolo_fast.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
191
192
193
194
195
196
197
198
199
200
201
#include <builder/trt_builder.hpp>
#include <infer/trt_infer.hpp>
#include <common/ilogger.hpp>
#include "app_yolo_fast/yolo_fast.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);
YoloFast::DecodeMeta type_of_meta(YoloFast::Type type){
switch(type){
case YoloFast::Type::V5_P5: return YoloFast::DecodeMeta::v5_p5_default_meta();
case YoloFast::Type::V5_P6: return YoloFast::DecodeMeta::v5_p6_default_meta();
case YoloFast::Type::X: return YoloFast::DecodeMeta::x_default_meta();
default: return YoloFast::DecodeMeta::v5_p5_default_meta();
}
}
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, YoloFast::Type type, const string& model_name){
auto meta = type_of_meta(type);
auto engine = YoloFast::create_infer(engine_file, type, deviceid, 0.4f, 0.5f, meta);
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<YoloFast::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 = YoloFast::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);
}
}
static void test(YoloFast::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]);
YoloFast::image_to_tensor(image, tensor, type, i);
}
};
const char* name = model.c_str();
INFO("===================== test %s %s %s ==================================", YoloFast::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_fast(){
test(YoloFast::Type::X, TRT::Mode::FP32, "yolox_s_fast");
//iLogger::set_log_level(iLogger::LogLevel::Info);
// test(YoloFast::Type::X, TRT::Mode::FP32, "yolox_x_fast");
// test(YoloFast::Type::X, TRT::Mode::FP32, "yolox_l_fast");
// test(YoloFast::Type::X, TRT::Mode::FP32, "yolox_m_fast");
// test(YoloFast::Type::X, TRT::Mode::FP32, "yolox_s_fast");
// test(YoloFast::Type::X, TRT::Mode::FP16, "yolox_x_fast");
// test(YoloFast::Type::X, TRT::Mode::FP16, "yolox_l_fast");
// test(YoloFast::Type::X, TRT::Mode::FP16, "yolox_m_fast");
// test(YoloFast::Type::X, TRT::Mode::FP16, "yolox_s_fast");
// test(YoloFast::Type::X, TRT::Mode::INT8, "yolox_x_fast");
// test(YoloFast::Type::X, TRT::Mode::INT8, "yolox_l_fast");
// test(YoloFast::Type::X, TRT::Mode::INT8, "yolox_m_fast");
// test(YoloFast::Type::X, TRT::Mode::INT8, "yolox_s_fast");
// test(YoloFast::Type::V5_P6, TRT::Mode::FP32, "yolov5x6_fast");
// test(YoloFast::Type::V5_P6, TRT::Mode::FP32, "yolov5l6_fast");
// test(YoloFast::Type::V5_P6, TRT::Mode::FP32, "yolov5m6_fast");
// test(YoloFast::Type::V5_P6, TRT::Mode::FP32, "yolov5s6_fast");
// test(YoloFast::Type::V5_P5, TRT::Mode::FP32, "yolov5x_fast");
// test(YoloFast::Type::V5_P5, TRT::Mode::FP32, "yolov5l_fast");
// test(YoloFast::Type::V5_P5, TRT::Mode::FP32, "yolov5m_fast");
// test(YoloFast::Type::V5_P5, TRT::Mode::FP32, "yolov5s_fast");
// test(YoloFast::Type::V5_P6, TRT::Mode::FP16, "yolov5x6_fast");
// test(YoloFast::Type::V5_P6, TRT::Mode::FP16, "yolov5l6_fast");
// test(YoloFast::Type::V5_P6, TRT::Mode::FP16, "yolov5m6_fast");
// test(YoloFast::Type::V5_P6, TRT::Mode::FP16, "yolov5s6_fast");
// test(YoloFast::Type::V5_P5, TRT::Mode::FP16, "yolov5x_fast");
// test(YoloFast::Type::V5_P5, TRT::Mode::FP16, "yolov5l_fast");
// test(YoloFast::Type::V5_P5, TRT::Mode::FP16, "yolov5m_fast");
// test(YoloFast::Type::V5_P5, TRT::Mode::FP16, "yolov5s_fast");
// test(YoloFast::Type::V5_P6, TRT::Mode::INT8, "yolov5x6_fast");
// test(YoloFast::Type::V5_P6, TRT::Mode::INT8, "yolov5l6_fast");
// test(YoloFast::Type::V5_P6, TRT::Mode::INT8, "yolov5m6_fast");
// test(YoloFast::Type::V5_P6, TRT::Mode::INT8, "yolov5s6_fast");
// test(YoloFast::Type::V5_P5, TRT::Mode::INT8, "yolov5x_fast");
// test(YoloFast::Type::V5_P5, TRT::Mode::INT8, "yolov5l_fast");
// test(YoloFast::Type::V5_P5, TRT::Mode::INT8, "yolov5m_fast");
// test(YoloFast::Type::V5_P5, TRT::Mode::INT8, "yolov5s_fast");
return 0;
}