-
Notifications
You must be signed in to change notification settings - Fork 4
/
nms.cu
423 lines (311 loc) · 9.6 KB
/
nms.cu
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
/*
* NMS Benchmarking Framework
*
* "Work-Efficient Parallel Non-Maximum Suppression Kernels"
* Copyright (c) 2019 David Oro et al.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <cuda.h>
#include <math.h>
#include <stdio.h>
#include <unistd.h>
#include "config.h"
#include "nms.h"
#define MAX_DETECTIONS 4096
#define N_PARTITIONS 32
/* GPU array for storing the coordinates, dimensions and score of each detected object */
float4* points;
/* CPU array for storing the coordinates, dimensions and score of each detected object */
float4* cpu_points;
/* GPU array for storing the detection bitmap */
uint8* pointsbitmap;
/* CPU array for storing the detection bitmap */
uint8* cpu_pointsbitmap;
/* Number of detection windows */
int ndetections;
/* GPU array for storing the non-maximum supression bitmap */
uint8* nmsbitmap;
/* Kernel streams and events */
cudaEvent_t begin_map_event;
cudaEvent_t end_map_event;
cudaEvent_t begin_reduce_event;
cudaEvent_t end_reduce_event;
/* Reads detections from a comma separated input text file
encoded as follows:
x0,y0,width0,score0\n
x1,y1,width1,score1\n
x2,y2,width2,score2\n
...
xn,yn,widthn,scoren\n
*/
int read_detections(const char* filename)
{
FILE* fp;
int x, y, w, cnt;
float score;
ndetections = 0;
fp = fopen(filename, "r");
if (!fp)
{
printf("Error: Unable to open file %s.\n", filename);
return -1;
}
/* Memory allocation in the host memory address space */
cpu_points = (float4*) malloc(sizeof(float4) * MAX_DETECTIONS);
if(!cpu_points)
{
printf("Error: Unable to allocate CPU memory.\n");
return -1;
}
memset(cpu_points, 0, sizeof(float4) * MAX_DETECTIONS);
while(!feof(fp))
{
cnt = fscanf(fp, "%d,%d,%d,%f\n", &x, &y, &w, &score);
if (cnt !=4)
{
printf("Error: Invalid file format in line %d when reading %s\n", ndetections, filename);
return -1;
}
cpu_points[ndetections].x = (float) x; // x coordinate
cpu_points[ndetections].y = (float) y; // y coordinate
cpu_points[ndetections].z = (float) w; // window dimensions
cpu_points[ndetections].w = score; // score
ndetections++;
}
printf("Detections read from input file (%s): %d\n", filename, ndetections);
fclose(fp);
return 0;
}
int dump_merged_detections(const char* filename)
{
FILE* fp;
cudaError_t err;
int x, y, w, i, totaldets;
float score;
totaldets = 0;
err = cudaMemcpy(cpu_pointsbitmap, pointsbitmap, sizeof(uint8) * MAX_DETECTIONS, cudaMemcpyDeviceToHost);
if(err != cudaSuccess)
{
printf("Error: %s\n", cudaGetErrorString(err));
return -1;
}
fp = fopen(filename, "w");
if (!fp)
{
printf("Error: Unable to open file %s for writing.\n", filename);
return -1;
}
for(i = 0; i < ndetections; i++)
{
if(cpu_pointsbitmap[i])
{
x = (int) cpu_points[i].x; // x coordinate
y = (int) cpu_points[i].y; // y coordinate
w = (int) cpu_points[i].z; // window dimensions
score = cpu_points[i].w; // score
fprintf(fp, "%d,%d,%d,%f\n", x, y, w, score);
totaldets++;
}
}
printf("Detections after NMS: %d\n", totaldets);
fclose(fp);
return 0;
}
/* Gets the optimal X or Y dimension for a given CUDA block */
int get_optimal_dim(int val)
{
int div, neg, cntneg, cntpos;
/* We start figuring out if 'val' is divisible by 16
(e.g. optimal 16x16 CUDA block of maximum GPU occupancy */
neg = 1;
div = 16;
cntneg = div;
cntpos = div;
/* In order to guarantee the ending of this loop if 'val' is
a prime number, we limit the loop to 5 iterations */
for(int i=0; i<5; i++)
{
if(val % div == 0)
return div;
if(neg)
{
cntneg--;
div = cntneg;
neg = 0;
}
else
{
cntpos++;
div = cntpos;
neg = 1;
}
}
return 16;
}
/* Gets an upper limit for 'val' multiple of the 'mul' integer */
int get_upper_limit(int val, int mul)
{
int cnt = mul;
/* The upper limit must be lower than
the maximum allowed number of detections */
while(cnt < val)
cnt += mul;
if(cnt > MAX_DETECTIONS)
cnt = MAX_DETECTIONS;
return cnt;
}
int allocate_gpu_memory()
{
cudaError_t err;
cudaEventCreate(&begin_map_event);
cudaEventCreate(&end_map_event);
cudaEventCreate(&begin_reduce_event);
cudaEventCreate(&end_reduce_event);
/* Memory allocation for the data structure of detected objects */
err = cudaMalloc((void**) &points, sizeof(float4) * MAX_DETECTIONS);
if(err != cudaSuccess)
{
printf("Error: %s\n", cudaGetErrorString(err));
return -1;
}
err = cudaMemset(points, 0, sizeof(float4) * MAX_DETECTIONS);
if(err != cudaSuccess)
{
printf("Error: %s\n", cudaGetErrorString(err));
return -1;
}
/* Memory allocation for the non-maximum supression bitmaps */
err = cudaMalloc((void**) &nmsbitmap, sizeof(uint8) * MAX_DETECTIONS * MAX_DETECTIONS);
if(err != cudaSuccess)
{
printf("Error: %s\n", cudaGetErrorString(err));
return -1;
}
err = cudaMalloc((void**) &pointsbitmap, sizeof(uint8) * MAX_DETECTIONS);
if(err != cudaSuccess)
{
printf("Error: %s\n", cudaGetErrorString(err));
return -1;
}
err = cudaMemset(nmsbitmap, 1, sizeof(uint8) * MAX_DETECTIONS * MAX_DETECTIONS);
if(err != cudaSuccess)
{
printf("Error: %s\n", cudaGetErrorString(err));
return -1;
}
err = cudaMemset(pointsbitmap, 0, sizeof(uint8) * MAX_DETECTIONS);
if(err != cudaSuccess)
{
printf("Error: %s\n", cudaGetErrorString(err));
return -1;
}
cpu_pointsbitmap = (uint8*) malloc(sizeof(uint8) * MAX_DETECTIONS);
memset(cpu_pointsbitmap, 0, sizeof(uint8) * MAX_DETECTIONS);
return 0;
}
void free_memory()
{
cudaEventDestroy(begin_map_event);
cudaEventDestroy(end_map_event);
cudaEventDestroy(begin_reduce_event);
cudaEventDestroy(end_reduce_event);
cudaFree(points);
cudaFree(nmsbitmap);
cudaFree(pointsbitmap);
free(cpu_points);
free(cpu_pointsbitmap);
}
int transfer_detections_to_gpu()
{
cudaError_t err;
err = cudaMemcpy(points, cpu_points, sizeof(float4) * MAX_DETECTIONS, cudaMemcpyHostToDevice);
if(err != cudaSuccess)
{
printf("Error: %s\n", cudaGetErrorString(err));
return -1;
}
return 0;
}
/* NMS Map kernel */
__global__ void generate_nms_bitmap(float4* rects, uint8* nmsbitmap, float othreshold)
{
const int i = blockIdx.x * blockDim.x + threadIdx.x;
const int j = blockIdx.y * blockDim.y + threadIdx.y;
if(rects[i].w < rects[j].w)
{
float area = (rects[j].z + 1.0f) * (rects[j].z + 1.0f);
float w = max(0.0f, min(rects[i].x + rects[i].z, rects[j].x + rects[j].z) - max(rects[i].x, rects[j].x) + 1.0f);
float h = max(0.0f, min(rects[i].y + rects[i].z, rects[j].y + rects[j].z) - max(rects[i].y, rects[j].y) + 1.0f);
nmsbitmap[i * MAX_DETECTIONS + j] = (((w * h) / area) < othreshold) && (rects[j].z != 0);
}
}
/* NMS Reduce kernel */
__device__ __inline__ void compute_nms_point_mask(uint8* pointsbitmap, int cond, int idx, int ndetections)
{
*pointsbitmap = __syncthreads_and(cond);
}
__global__ void reduce_nms_bitmap(uint8* nmsbitmap, uint8* pointsbitmap, int ndetections)
{
int idx = blockIdx.x * MAX_DETECTIONS + threadIdx.x;
compute_nms_point_mask(&pointsbitmap[blockIdx.x], nmsbitmap[idx], idx, ndetections);
for(int i=0; i<(N_PARTITIONS-1); i++)
{
idx += MAX_DETECTIONS / N_PARTITIONS;
compute_nms_point_mask(&pointsbitmap[blockIdx.x], pointsbitmap[blockIdx.x] && nmsbitmap[idx], idx, ndetections);
}
}
int non_maximum_suppression()
{
dim3 pkthreads(1, 1, 1);
dim3 pkgrid(1, 1, 1);
int limit;
float nms_elapsed_time;
cudaError_t err;
limit = get_upper_limit(ndetections, 16);
pkthreads.x = get_optimal_dim(limit);
pkthreads.y = get_optimal_dim(limit);
pkgrid.x = limit / pkthreads.x;
pkgrid.y = limit / pkthreads.y;
cudaEventRecord(begin_map_event, 0);
/* We build up the non-maximum supression bitmap matrix by removing overlapping windows */
generate_nms_bitmap<<<pkgrid, pkthreads>>>(points, nmsbitmap, 0.3f);
err = cudaGetLastError();
if(err != cudaSuccess)
{
printf("CUDA Error: %s\n", cudaGetErrorString(err));
return -1;
}
cudaEventRecord(end_map_event, 0);
cudaEventSynchronize(end_map_event);
cudaEventElapsedTime(&nms_elapsed_time, begin_map_event, end_map_event);
printf("NMS-MAP elapsed time: %.3f ms\n", nms_elapsed_time);
pkthreads.x = MAX_DETECTIONS / N_PARTITIONS;
pkthreads.y = 1;
pkgrid.x = ndetections;
pkgrid.y = 1;
cudaEventRecord(begin_reduce_event, 0);
/* Then we perform a reduction for generating a point bitmap vector */
reduce_nms_bitmap<<<pkgrid, pkthreads>>>(nmsbitmap, pointsbitmap, ndetections);
err = cudaGetLastError();
if(err != cudaSuccess)
{
printf("CUDA Error: %s\n", cudaGetErrorString(err));
return -1;
}
cudaEventRecord(end_reduce_event, 0);
cudaEventSynchronize(end_reduce_event);
cudaEventElapsedTime(&nms_elapsed_time, begin_reduce_event, end_reduce_event);
printf("NMS-REDUCE elapsed time: %.3f ms\n", nms_elapsed_time);
return 0;
}