-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.cpp
77 lines (73 loc) · 2.6 KB
/
main.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
#include <sys/time.h>
#include <chrono>
#include <fstream>
#include <vector>
#include "inferlib.h"
const std::vector<std::pair<std::string, int>> read_label(
const std::string& base_dir = "./") {
std::vector<std::pair<std::string, int>> rst;
std::ifstream f_label(base_dir + "label.txt");
while (f_label) {
std::string temp_str;
int label;
f_label >> temp_str;
f_label >> label;
if (temp_str.size() > 0) {
rst.push_back(std::make_pair(base_dir + temp_str, label));
}
}
return rst;
}
int main(void) {
DAWNBench_Inference inference("resnet26d.engine");
std::vector<std::pair<std::string, int>> dataset = read_label("./");
int dataset_size = dataset.size();
double totaltime = 0;
int top5_count = 0;
int image_count = 0;
char(*preprocess_data)[3 * 224 * 224] = new char[dataset_size][3 * 224 * 224];
int* label = new int[dataset_size];
// preprocess_data
for (int i = 0; i < dataset_size; i++) {
auto& data = dataset[i];
cv::Mat img_bgr = cv::imread(data.first);
inference.preprocessing_data(preprocess_data[i], img_bgr);
label[i] = data.second;
if (i % 1000 == 0) {
std::cout << "loading: " << i << std::endl;
}
}
// inference
for (int image_idx = 0; image_idx < dataset_size; image_idx++) {
inference.do_preload(preprocess_data[image_idx]);
// do inference
auto tStart = std::chrono::high_resolution_clock::now();
std::vector<int> rst =
inference.do_inference(preprocess_data[image_idx]);
auto tEnd = std::chrono::high_resolution_clock::now();
double m_dectime =
std::chrono::duration<float, std::milli>(tEnd - tStart).count();
totaltime += m_dectime;
// check top 5
image_count += 1;
for (int i = 0; i < 5; i++) {
if (rst[i] == label[image_idx]) {
top5_count += 1;
break;
}
}
if (image_count % 100 == 0) {
std::cout << "run at " << image_count
<< ": Prec@5: " << (double)top5_count / image_count
<< std::endl;
std::cout << "inference time at " << image_count << " : "
<< totaltime / image_count << " ms" << std::endl;
}
}
std::cout << "final inference time : " << totaltime / dataset.size()
<< " ms" << std::endl;
std::cout << "final Prec@5: " << (double)top5_count / image_count
<< std::endl;
delete[] preprocess_data;
delete[] label;
}