forked from facebookresearch/multipathnet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nms.c
142 lines (118 loc) · 3.91 KB
/
nms.c
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
/*------------------------------------------------------------------------------
Copyright (c) 2016-present, Facebook, Inc. All rights reserved.
This source code is licensed under the BSD-style license found in the
LICENSE file in the root directory of this source tree. An additional grant
of patent rights can be found in the PATENTS file in the same directory.
------------------------------------------------------------------------------*/
#include <TH/TH.h>
#define MIN(a,b) (((a)<(b))?(a):(b))
#define MAX(a,b) (((a)>(b))?(a):(b))
float overlap(const float *a, const float *b)
{
float a_x1 = a[0];
float a_y1 = a[1];
float a_x2 = a[2];
float a_y2 = a[3];
float b_x1 = b[0];
float b_y1 = b[1];
float b_x2 = b[2];
float b_y2 = b[3];
float x1 = MAX(a_x1, b_x1);
float y1 = MAX(a_y1, b_y1);
float x2 = MIN(a_x2, b_x2);
float y2 = MIN(a_y2, b_y2);
float w = x2 - x1 + 1;
float h = y2 - y1 + 1;
float intersection = w * h;
float aarea = (a_x2 - a_x1 + 1) * (a_y2 - a_y1 + 1);
float barea = (b_x2 - b_x1 + 1) * (b_y2 - b_y1 + 1);
float iou = intersection / (aarea + barea - intersection);
return (w <= 0 || h <= 0) ? 0 : iou;
}
void boxoverlap(THFloatTensor *result, THFloatTensor *a, THFloatTensor *b)
{
int N = a->size[0];
THFloatTensor_resize1d(result, N);
float *a_data = THFloatTensor_data(a);
float *b_data = THFloatTensor_data(b);
float *r_data = THFloatTensor_data(result);
for(int i=0;i<N;++i)
{
r_data[i] = overlap(a_data, b_data);
a_data += 4;
}
}
void NMS(THFloatTensor *keep, THFloatTensor *scored_boxes, float threshold)
{
int N = scored_boxes->size[0];
float **boxes = calloc(N, sizeof(float*));
float *scored_boxes_data = THFloatTensor_data(scored_boxes);
for(int i=0; i<N; ++i)
boxes[i] = scored_boxes_data + i*5;
float **boxes_original = boxes;
int num = N;
int numNMS = 0;
while(num) {
// Greedily select the highest scoring bounding box
int best = -1;
float bestS = -10000000;
for(int i = 0; i < num; i++) {
if(boxes[i][4] > bestS) {
bestS = boxes[i][4];
best = i;
}
}
float *b = boxes[best];
float *tmp = boxes[0];
boxes[0] = boxes[best];
boxes[best] = tmp;
boxes++;
numNMS++;
// Remove all bounding boxes where the percent area of overlap is greater than overlap
int numGood = 0;
for(int i = 0; i < num-1; i++) {
float inter_over_union = overlap(b, boxes[i]);
if(inter_over_union <= threshold) {
tmp = boxes[numGood];
boxes[numGood++] = boxes[i];
boxes[i] = tmp;
}
}
num = numGood;
}
THFloatTensor_resize2d(keep, numNMS, 5);
float *keep_data = THFloatTensor_data(keep);
for(int i=0; i<numNMS; ++i)
memcpy(keep_data + i*5, boxes_original[i], sizeof(float)*5);
free(boxes_original);
}
void bbox_vote(THFloatTensor *res, THFloatTensor *nms_boxes, THFloatTensor *scored_boxes, float threshold)
{
THAssert(THFloatTensor_isContiguous(nms_boxes));
THAssert(THFloatTensor_isContiguous(scored_boxes));
THFloatTensor_resizeAs(res, nms_boxes);
THFloatTensor_zero(res);
int N_nms = nms_boxes->size[0];
int N_boxes = scored_boxes->size[0];
THAssert(nms_boxes->size[1] == 5);
THAssert(scored_boxes->size[1] == 5);
// THFloatTensor* overlaps = THFloatTensor_newWithSize1d(N_boxes);
float *nms_data = THFloatTensor_data(nms_boxes);
float *scored_data = THFloatTensor_data(scored_boxes);
float *res_data = THFloatTensor_data(res);
for(int i=0; i<N_nms; i++) {
for(int j=0; j<N_boxes; j++) {
float ov = overlap(scored_data + 5*j, nms_data + 5*i);
if(ov > threshold) {
for(int field = 0; field<4; field++) {
res_data[5*i+field] += scored_data[5*j+field] * scored_data[5*j+4];
}
res_data[5*i+4] += scored_data[5*j+4];
}
}
for(int field = 0; field<4; field++) {
res_data[5*i+field] /= res_data[5*i+4];
}
res_data[5*i+4] = nms_data[5*i+4];
}
}