-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathdemo_bevdet.cpp
177 lines (158 loc) · 6.28 KB
/
demo_bevdet.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
#include <cstdio>
#include <string>
#include <iostream>
#include <fstream>
#include <thread>
#include <chrono>
#include <cassert>
#include <yaml-cpp/yaml.h>
#include "bevdet.h"
#include "cpu_jpegdecoder.h"
using std::chrono::duration;
using std::chrono::high_resolution_clock;
void Getinfo(void) {
cudaDeviceProp prop;
int count = 0;
cudaGetDeviceCount(&count);
printf("\nGPU has cuda devices: %d\n", count);
for (int i = 0; i < count; ++i) {
cudaGetDeviceProperties(&prop, i);
printf("----device id: %d info----\n", i);
printf(" GPU : %s \n", prop.name);
printf(" Capbility: %d.%d\n", prop.major, prop.minor);
printf(" Global memory: %luMB\n", prop.totalGlobalMem >> 20);
printf(" Const memory: %luKB\n", prop.totalConstMem >> 10);
printf(" Shared memory in a block: %luKB\n", prop.sharedMemPerBlock >> 10);
printf(" warp size: %d\n", prop.warpSize);
printf(" threads in a block: %d\n", prop.maxThreadsPerBlock);
printf(" block dim: (%d,%d,%d)\n", prop.maxThreadsDim[0],
prop.maxThreadsDim[1], prop.maxThreadsDim[2]);
printf(" grid dim: (%d,%d,%d)\n", prop.maxGridSize[0], prop.maxGridSize[1],
prop.maxGridSize[2]);
}
printf("\n");
}
void Boxes2Txt(const std::vector<Box> &boxes, std::string file_name, bool with_vel=false) {
std::ofstream out_file;
out_file.open(file_name, std::ios::out);
if (out_file.is_open()) {
for (const auto &box : boxes) {
out_file << box.x << " ";
out_file << box.y << " ";
out_file << box.z << " ";
out_file << box.l << " ";
out_file << box.w << " ";
out_file << box.h << " ";
out_file << box.r << " ";
if(with_vel){
out_file << box.vx << " ";
out_file << box.vy << " ";
}
out_file << box.score << " ";
out_file << box.label << "\n";
}
}
out_file.close();
return;
};
void Egobox2Lidarbox(const std::vector<Box>& ego_boxes,
std::vector<Box> &lidar_boxes,
const Eigen::Quaternion<float> &lidar2ego_rot,
const Eigen::Translation3f &lidar2ego_trans){
for(size_t i = 0; i < ego_boxes.size(); i++){
Box b = ego_boxes[i];
Eigen::Vector3f center(b.x, b.y, b.z);
center -= lidar2ego_trans.translation();
center = lidar2ego_rot.inverse().matrix() * center;
b.r -= lidar2ego_rot.matrix().eulerAngles(0, 1, 2).z();
b.x = center.x();
b.y = center.y();
b.z = center.z();
lidar_boxes.push_back(b);
}
}
void TestNuscenes(YAML::Node &config){
size_t img_N = config["N"].as<size_t>();
int img_w = config["W"].as<int>();
int img_h = config["H"].as<int>();
std::string data_info_path = config["dataset_info"].as<std::string>();
std::string model_config = config["ModelConfig"].as<std::string>();
std::string imgstage_file = config["ImgStageEngine"].as<std::string>();
std::string bevstage_file = config["BEVStageEngine"].as<std::string>();
std::string output_dir = config["OutputDir"].as<std::string>();
std::vector<std::string> cams_name = config["cams"].as<std::vector<std::string>>();
DataLoader nuscenes(img_N, img_h, img_w, data_info_path, cams_name);
BEVDet bevdet(model_config, img_N, nuscenes.get_cams_intrin(),
nuscenes.get_cams2ego_rot(), nuscenes.get_cams2ego_trans(), imgstage_file,
bevstage_file);
std::vector<Box> ego_boxes;
double sum_time = 0;
int cnt = 0;
for(int i = 0; i < nuscenes.size(); i++){
ego_boxes.clear();
float time = 0.f;
bevdet.DoInfer(nuscenes.data(i), ego_boxes, time, i);
if(i != 0){
sum_time += time;
cnt++;
}
Boxes2Txt(ego_boxes, output_dir + "/bevdet_egoboxes_" + std::to_string(i) + ".txt", true);
}
printf("Infer mean cost time : %.5lf ms\n", sum_time / cnt);
}
void TestSample(YAML::Node &config){
size_t img_N = config["N"].as<size_t>();
int img_w = config["W"].as<int>();
int img_h = config["H"].as<int>();
std::string model_config = config["ModelConfig"].as<std::string>();
std::string imgstage_file = config["ImgStageEngine"].as<std::string>();
std::string bevstage_file = config["BEVStageEngine"].as<std::string>();
YAML::Node camconfig = YAML::LoadFile(config["CamConfig"].as<std::string>());
std::string output_lidarbox = config["OutputLidarBox"].as<std::string>();
YAML::Node sample = config["sample"];
std::vector<std::string> imgs_file;
std::vector<std::string> imgs_name;
for(auto file : sample){
imgs_file.push_back(file.second.as<std::string>());
imgs_name.push_back(file.first.as<std::string>());
}
camsData sampleData;
sampleData.param = camParams(camconfig, img_N, imgs_name);
BEVDet bevdet(model_config, img_N, sampleData.param.cams_intrin,
sampleData.param.cams2ego_rot, sampleData.param.cams2ego_trans,
imgstage_file, bevstage_file);
std::vector<std::vector<char>> imgs_data;
read_sample(imgs_file, imgs_data);
uchar* imgs_dev = nullptr;
CHECK_CUDA(cudaMalloc((void**)&imgs_dev, img_N * 3 * img_w * img_h * sizeof(uchar)));
decode_cpu(imgs_data, imgs_dev, img_w, img_h);
sampleData.imgs_dev = imgs_dev;
std::vector<Box> ego_boxes;
ego_boxes.clear();
float time = 0.f;
bevdet.DoInfer(sampleData, ego_boxes, time);
std::vector<Box> lidar_boxes;
Egobox2Lidarbox(ego_boxes, lidar_boxes, sampleData.param.lidar2ego_rot,
sampleData.param.lidar2ego_trans);
Boxes2Txt(lidar_boxes, output_lidarbox, false);
ego_boxes.clear();
bevdet.DoInfer(sampleData, ego_boxes, time); // only for inference time
}
int main(int argc, char **argv){
Getinfo();
if(argc < 2){
printf("Need a configure yaml! Exit!\n");
return 0;
}
std::string config_file(argv[1]);
YAML::Node config = YAML::LoadFile(config_file);
printf("Successful load config : %s!\n", config_file.c_str());
bool testNuscenes = config["TestNuscenes"].as<bool>();
if(testNuscenes){
TestNuscenes(config);
}
else{
TestSample(config);
}
return 0;
}