forked from dusty-nv/jetson-inference
-
Notifications
You must be signed in to change notification settings - Fork 0
/
detectNet.cpp
296 lines (222 loc) · 8.34 KB
/
detectNet.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
/*
* http://github.com/dusty-nv/jetson-inference
*/
#include "detectNet.h"
#include "cudaMappedMemory.h"
#include "cudaOverlay.h"
#include "cudaResize.h"
#define OUTPUT_CVG 0
#define OUTPUT_BBOX 1
//#define DEBUG_CLUSTERING
detectNet* detectNet::Create( NetworkType networkType, float threshold )
{
if( networkType == PEDNET_MULTI )
return Create("multiped-500/deploy.prototxt", "multiped-500/snapshot_iter_178000.caffemodel", "multiped-500/mean.binaryproto", threshold );
else if( networkType == FACENET )
return Create("facenet-120/deploy.prototxt", "facenet-120/snapshot_iter_24000.caffemodel", NULL, threshold );
else /*if( networkTYpe == PEDNET )*/
return Create("ped-100/deploy.prototxt", "ped-100/snapshot_iter_70800.caffemodel", "ped-100/mean.binaryproto", threshold );
}
void detectNet::SetClassColor( uint32_t classIndex, float r, float g, float b, float a )
{
if( classIndex >= GetNumClasses() || !mClassColors[0] )
return;
const uint32_t i = classIndex * 4;
mClassColors[0][i+0] = r;
mClassColors[0][i+1] = g;
mClassColors[0][i+2] = b;
mClassColors[0][i+3] = a;
}
// constructor
detectNet::detectNet() : tensorNet()
{
mCoverageThreshold = 0.5f;
mClassColors[0] = NULL; // cpu ptr
mClassColors[1] = NULL; // gpu ptr
}
// destructor
detectNet::~detectNet()
{
}
// Create
detectNet* detectNet::Create( const char* prototxt, const char* model, const char* mean_binary, float threshold, const char* input_blob, const char* coverage_blob, const char* bbox_blob )
{
detectNet* net = new detectNet();
if( !net )
return NULL;
//net->EnableDebug();
std::vector<std::string> output_blobs;
output_blobs.push_back(coverage_blob);
output_blobs.push_back(bbox_blob);
if( !net->LoadNetwork(prototxt, model, mean_binary, input_blob, output_blobs) )
{
printf("detectNet -- failed to initialize.\n");
return NULL;
}
const uint32_t numClasses = net->GetNumClasses();
if( !cudaAllocMapped((void**)&net->mClassColors[0], (void**)&net->mClassColors[1], numClasses * sizeof(float4)) )
return NULL;
for( uint32_t n=0; n < numClasses; n++ )
{
if( n != 1 )
{
net->mClassColors[0][n*4+0] = 0.0f; // r
net->mClassColors[0][n*4+1] = 200.0f; // g
net->mClassColors[0][n*4+2] = 255.0f; // b
net->mClassColors[0][n*4+3] = 100.0f; // a
}
else
{
net->mClassColors[0][n*4+0] = 0.0f; // r
net->mClassColors[0][n*4+1] = 255.0f; // g
net->mClassColors[0][n*4+2] = 175.0f; // b
net->mClassColors[0][n*4+3] = 100.0f; // a
}
}
net->SetThreshold(threshold);
return net;
}
cudaError_t cudaPreImageNetMean( float4* input, size_t inputWidth, size_t inputHeight, float* output, size_t outputWidth, size_t outputHeight, const float3& mean_value );
struct float6 { float x; float y; float z; float w; float v; float u; };
static inline float6 make_float6( float x, float y, float z, float w, float v, float u ) { float6 f; f.x = x; f.y = y; f.z = z; f.w = w; f.v = v; f.u = u; return f; }
inline static bool rectOverlap(const float6& r1, const float6& r2)
{
return ! ( r2.x > r1.z
|| r2.z < r1.x
|| r2.y > r1.w
|| r2.w < r1.y
);
}
static void mergeRect( std::vector<float6>& rects, const float6& rect )
{
const uint32_t num_rects = rects.size();
bool intersects = false;
for( uint32_t r=0; r < num_rects; r++ )
{
if( rectOverlap(rects[r], rect) )
{
intersects = true;
#ifdef DEBUG_CLUSTERING
printf("found overlap\n");
#endif
if( rect.x < rects[r].x ) rects[r].x = rect.x;
if( rect.y < rects[r].y ) rects[r].y = rect.y;
if( rect.z > rects[r].z ) rects[r].z = rect.z;
if( rect.w > rects[r].w ) rects[r].w = rect.w;
break;
}
}
if( !intersects )
rects.push_back(rect);
}
// Detect
bool detectNet::Detect( float* rgba, uint32_t width, uint32_t height, float* boundingBoxes, int* numBoxes, float* confidence )
{
if( !rgba || width == 0 || height == 0 || !boundingBoxes || !numBoxes || *numBoxes < 1 )
{
printf("detectNet::Detect( 0x%p, %u, %u ) -> invalid parameters\n", rgba, width, height);
return false;
}
// downsample and convert to band-sequential BGR
if( CUDA_FAILED(cudaPreImageNetMean((float4*)rgba, width, height, mInputCUDA, mWidth, mHeight,
make_float3(104.0069879317889f, 116.66876761696767f, 122.6789143406786f))) )
{
printf("detectNet::Classify() -- cudaPreImageNetMean failed\n");
return false;
}
// process with GIE
void* inferenceBuffers[] = { mInputCUDA, mOutputs[OUTPUT_CVG].CUDA, mOutputs[OUTPUT_BBOX].CUDA };
if( !mContext->execute(1, inferenceBuffers) )
{
printf(LOG_GIE "detectNet::Classify() -- failed to execute tensorRT context\n");
*numBoxes = 0;
return false;
}
PROFILER_REPORT();
// cluster detection bboxes
float* net_cvg = mOutputs[OUTPUT_CVG].CPU;
float* net_rects = mOutputs[OUTPUT_BBOX].CPU;
const int ow = mOutputs[OUTPUT_BBOX].dims.w; // number of columns in bbox grid in X dimension
const int oh = mOutputs[OUTPUT_BBOX].dims.h; // number of rows in bbox grid in Y dimension
const int owh = ow * oh; // total number of bbox in grid
const int cls = GetNumClasses(); // number of object classes in coverage map
const float cell_width = /*width*/ mInputDims.w / ow;
const float cell_height = /*height*/ mInputDims.h / oh;
const float scale_x = float(width) / float(mInputDims.w);
const float scale_y = float(height) / float(mInputDims.h);
#ifdef DEBUG_CLUSTERING
printf("input width %i height %i\n", (int)mInputDims.w, (int)mInputDims.h);
printf("cells x %i y %i\n", ow, oh);
printf("cell width %f height %f\n", cell_width, cell_height);
printf("scale x %f y %f\n", scale_x, scale_y);
#endif
#if 1
std::vector< std::vector<float6> > rects;
rects.resize(cls);
// extract and cluster the raw bounding boxes that meet the coverage threshold
for( uint32_t z=0; z < cls; z++ )
{
rects[z].reserve(owh);
for( uint32_t y=0; y < oh; y++ )
{
for( uint32_t x=0; x < ow; x++)
{
const float coverage = net_cvg[z * owh + y * ow + x];
if( coverage > mCoverageThreshold )
{
const float mx = x * cell_width;
const float my = y * cell_height;
const float x1 = (net_rects[0 * owh + y * ow + x] + mx) * scale_x; // left
const float y1 = (net_rects[1 * owh + y * ow + x] + my) * scale_y; // top
const float x2 = (net_rects[2 * owh + y * ow + x] + mx) * scale_x; // right
const float y2 = (net_rects[3 * owh + y * ow + x] + my) * scale_y; // bottom
#ifdef DEBUG_CLUSTERING
printf("rect x=%u y=%u cvg=%f %f %f %f %f \n", x, y, coverage, x1, x2, y1, y2);
#endif
mergeRect( rects[z], make_float6(x1, y1, x2, y2, coverage, z) );
}
}
}
}
//printf("done clustering rects\n");
// condense the multiple class lists down to 1 list of detections
const uint32_t numMax = *numBoxes;
int n = 0;
for( uint32_t z = 0; z < cls; z++ )
{
const uint32_t numBox = rects[z].size();
for( uint32_t b = 0; b < numBox && n < numMax; b++ )
{
const float6 r = rects[z][b];
boundingBoxes[n * 4 + 0] = r.x;
boundingBoxes[n * 4 + 1] = r.y;
boundingBoxes[n * 4 + 2] = r.z;
boundingBoxes[n * 4 + 3] = r.w;
if( confidence != NULL )
{
confidence[n * 2 + 0] = r.v; // coverage
confidence[n * 2 + 1] = r.u; // class ID
}
n++;
}
}
*numBoxes = n;
#else
*numBoxes = 0;
#endif
return true;
}
// DrawBoxes
bool detectNet::DrawBoxes( float* input, float* output, uint32_t width, uint32_t height, const float* boundingBoxes, int numBoxes, int classIndex )
{
if( !input || !output || width == 0 || height == 0 || !boundingBoxes || numBoxes < 1 || classIndex < 0 || classIndex >= GetNumClasses() )
return false;
const float4 color = make_float4( mClassColors[0][classIndex*4+0],
mClassColors[0][classIndex*4+1],
mClassColors[0][classIndex*4+2],
mClassColors[0][classIndex*4+3] );
printf("draw boxes %i %i %f %f %f %f\n", numBoxes, classIndex, color.x, color.y, color.z, color.w);
if( CUDA_FAILED(cudaRectOutlineOverlay((float4*)input, (float4*)output, width, height, (float4*)boundingBoxes, numBoxes, color)) )
return false;
return true;
}