forked from NVIDIA-AI-IOT/CUDA-PointPillars
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
204 lines (167 loc) · 6.12 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
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
202
203
204
/*
* SPDX-FileCopyrightText: Copyright (c) 2021 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <iostream>
#include <sstream>
#include <fstream>
#include "cuda_runtime.h"
#include "./params.h"
#include "./pointpillar.h"
#define checkCudaErrors(status) \
{ \
if (status != 0) \
{ \
std::cout << "Cuda failure: " << cudaGetErrorString(status) \
<< " at line " << __LINE__ \
<< " in file " << __FILE__ \
<< " error status: " << status \
<< std::endl; \
abort(); \
} \
}
std::string Data_File = "../data/";
std::string Save_Dir = "../eval/kitti/object/pred_velo/";
std::string Model_File = "../model/pointpillar.onnx";
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(" SM 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");
}
int loadData(const char *file, void **data, unsigned int *length)
{
std::fstream dataFile(file, std::ifstream::in);
if (!dataFile.is_open())
{
std::cout << "Can't open files: "<< file<<std::endl;
return -1;
}
//get length of file:
unsigned int len = 0;
dataFile.seekg (0, dataFile.end);
len = dataFile.tellg();
dataFile.seekg (0, dataFile.beg);
//allocate memory:
char *buffer = new char[len];
if(buffer==NULL) {
std::cout << "Can't malloc buffer."<<std::endl;
dataFile.close();
exit(-1);
}
//read data as a block:
dataFile.read(buffer, len);
dataFile.close();
*data = (void*)buffer;
*length = len;
return 0;
}
void SaveBoxPred(std::vector<Bndbox> boxes, std::string file_name)
{
std::ofstream ofs;
ofs.open(file_name, std::ios::out);
if (ofs.is_open()) {
for (const auto box : boxes) {
ofs << box.x << " ";
ofs << box.y << " ";
ofs << box.z << " ";
ofs << box.w << " ";
ofs << box.l << " ";
ofs << box.h << " ";
ofs << box.rt << " ";
ofs << box.id << " ";
ofs << box.score << " ";
ofs << "\n";
}
}
else {
std::cerr << "Output file cannot be opened!" << std::endl;
}
ofs.close();
std::cout << "Saved prediction in: " << file_name << std::endl;
return;
};
int main(int argc, const char **argv)
{
Getinfo();
cudaEvent_t start, stop;
float elapsedTime = 0.0f;
cudaStream_t stream = NULL;
checkCudaErrors(cudaEventCreate(&start));
checkCudaErrors(cudaEventCreate(&stop));
checkCudaErrors(cudaStreamCreate(&stream));
Params params_;
std::vector<Bndbox> nms_pred;
nms_pred.reserve(100);
PointPillar pointpillar(Model_File, stream);
for (int i = 0; i < 10; i++)
{
std::string dataFile = Data_File;
std::stringstream ss;
ss<< i;
int n_zero = 6;
std::string _str = ss.str();
std::string index_str = std::string(n_zero - _str.length(), '0') + _str;
dataFile += index_str;
dataFile +=".bin";
std::cout << "<<<<<<<<<<<" <<std::endl;
std::cout << "load file: "<< dataFile <<std::endl;
//load points cloud
unsigned int length = 0;
void *data = NULL;
std::shared_ptr<char> buffer((char *)data, std::default_delete<char[]>());
loadData(dataFile.data(), &data, &length);
buffer.reset((char *)data);
float* points = (float*)buffer.get();
size_t points_size = length/sizeof(float)/4;
std::cout << "find points num: "<< points_size <<std::endl;
float *points_data = nullptr;
unsigned int points_data_size = points_size * 4 * sizeof(float);
checkCudaErrors(cudaMallocManaged((void **)&points_data, points_data_size));
checkCudaErrors(cudaMemcpy(points_data, points, points_data_size, cudaMemcpyDefault));
checkCudaErrors(cudaDeviceSynchronize());
cudaEventRecord(start, stream);
pointpillar.doinfer(points_data, points_size, nms_pred);
cudaEventRecord(stop, stream);
cudaEventSynchronize(stop);
cudaEventElapsedTime(&elapsedTime, start, stop);
std::cout<<"TIME: pointpillar: "<< elapsedTime <<" ms." <<std::endl;
checkCudaErrors(cudaFree(points_data));
std::cout<<"Bndbox objs: "<< nms_pred.size()<<std::endl;
std::string save_file_name = Save_Dir + index_str + ".txt";
SaveBoxPred(nms_pred, save_file_name);
nms_pred.clear();
std::cout << ">>>>>>>>>>>" <<std::endl;
}
checkCudaErrors(cudaEventDestroy(start));
checkCudaErrors(cudaEventDestroy(stop));
checkCudaErrors(cudaStreamDestroy(stream));
return 0;
}