-
Notifications
You must be signed in to change notification settings - Fork 2
/
YOLO.cpp
318 lines (258 loc) · 9.59 KB
/
YOLO.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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
/*
Implementation of tiny-yolo algorithm (https://pjreddie.com/darknet/yolo/)
Created by Hammad Jutt, March 3 2018
References used:
- https://github.com/pjreddie/darknet
- https://github.com/joycex99/tiny-yolo-keras/
- https://github.com/tensorflow/tensorflow/blob/master/tensorflow/core/kernels/non_max_suppression_op.cc
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <vector>
#include <algorithm>
#include <iostream>
#include <fstream>
#include "YOLO.hpp"
const float anchors[] = {1.08, 1.19, 3.42, 4.41, 6.63, 11.38, 9.42, 5.11, 16.62, 10.52};
const std::string labels[] = { "aeroplane","bicycle","bird","boat","bottle","bus","car","cat","chair","cow","diningtable","dog","horse","motorbike","person","pottedplant","sheep","sofa","train","tvmonitor"};
/**
Logistic sigmoid, normalizes x to between 0 and 1
*/
double sigmoid(double x)
{
return 1 / (1 + exp(-x));
}
/**
Returns index of largest value in given vector
*/
int argMax(std::vector<float> in)
{
return std::distance(in.begin(), std::max_element(in.begin(), in.end()));
}
/**
Comparator function for sorting predictions
*/
bool comparePredictions(Prediction a, Prediction b)
{
return (a.score > b.score);
}
/**
Calculates intersection-over-union overlap of two bounding boxes.
Parameters:
- a: The first bounding box
- b: The second bounding box
Returns:
- value between 0 and 1 representing how much overlap there is
*/
double iou(Box a, Box b)
{
double areaA = a.width * a.height;
if (areaA <= 0)
return 0;
double areaB = b.width * b.height;
if (areaB <= 0)
return 0;
double minX = std::max(a.x, b.x);
double minY = std::max(a.y, b.y);
double maxX = std::min(a.x + a.width, b.x + b.width);
double maxY = std::min(a.y + a.height, b.y + b.height);
double intersectArea = std::max(maxY - minY, 0.0) * std::max(maxX - minX, 0.0);
return intersectArea / (areaA + areaB - intersectArea);
}
/**
Normalizes all values in the given vector
so that they all add up to 1.
Parameters:
- in: a vector of values to normalize
Returns:
- a vector containing the softmaxed values
*/
std::vector<float> softmax(std::vector<float> in)
{
if (in.size() <= 0)
return in;
std::vector<float> out(in.size());
float sum = 0.0;
// Find max value in the input vector
float max = *std::max_element(in.begin(), in.end());
// Shift all values so that max value is 0 and exponentiate, compute sum
for (std::vector<int>::size_type i = 0; i != in.size(); i++)
{
out[i] = exp(in[i] - max);
sum += out[i];
}
// Divide each element by the sum to normalize all values
for (std::vector<int>::size_type i = 0; i != out.size(); i++)
out[i] /= sum;
return out;
}
/**
Removes bounding boxes that have too much overlap
with other bounding boxes that have a higher score.
Parameters:
- predictions: an vector of bounding boxes and their scores
- threshold: value between 0 and 1 to determine whether a box overlaps too much
- limit: the maximum number of boxes to select
Returns:
- a vector containing the resulting bounding boxes
*/
std::vector<Prediction> filterRedundantBoxes(
std::vector<Prediction> predictions,
float threshold,
uint limit)
{
// Sort based on confidence score
std::vector<Prediction> sorted = predictions;
std::sort(sorted.begin(), sorted.end(), comparePredictions);
std::vector<Prediction> selected;
std::vector<bool> active(sorted.size(), true);
int numActive = active.size();
// Starting at highest scoring box, remove all other boxes
// that overlap more than the given threshold. Repeat until
// limit is reached or no other boxes remain;
for (std::vector<int>::size_type i = 0; i != sorted.size(); i++)
{
if (!active[i])
continue;
Prediction a = sorted[i];
selected.push_back(a);
if (selected.size() >= limit)
{
break;
}
for (std::vector<int>::size_type j = i + 1; j != sorted.size(); j++)
{
if (!active[j])
continue;
Prediction b = sorted[j];
if (iou(a.box, b.box) > threshold)
{
active[j] = false;
numActive -= 1;
if (numActive <= 0)
{
goto finish;
}
}
}
}
finish:
return selected;
}
/**
Removes bounding boxes that have too much overlap
with other bounding boxes that have a higher score.
Parameters:
- features: a 3D vector containing raw output data from the network
Returns:
*/
std::vector<Prediction> interpretNetworkOutput(float ***features) {
std::vector<Prediction> predictions;
for (int cy = 0; cy != GRID_HEIGHT; cy++) {
for (int cx = 0; cx != GRID_WIDTH; cx++) {
for (int b = 0; b != BOXES_PER_CELL; b++) {
// First box features: 0-24, second box features: (25-49), etc
int offset = b*(NUM_CLASSES + 5);
// extract bounding box data from feature array
float bx = features[cy][cx][offset + 0];
float by = features[cy][cx][offset + 1];
float bw = features[cy][cx][offset + 2];
float bh = features[cy][cx][offset + 3];
float bc = features[cy][cx][offset + 4];
// convert cell coords to coords in original image
float x = ((float)cx + sigmoid(bx)) * CELL_SIZE;
float y = ((float)cy + sigmoid(by)) * CELL_SIZE;
// box sizes relative to anchor, convert to width/height in original image
float w = expf(bw) * anchors[2*b + 0] * CELL_SIZE;
float h = expf(bh) * anchors[2*b + 1] * CELL_SIZE;
// convert confidence to percentage
float confidence = sigmoid(bc);
// printf("x: %f, y: %f, w: %f, h: %f, c: %f\n", x, y, w, h, confidence);
// extract classes from feature array and convert to percentages
std::vector<float> classes(NUM_CLASSES, 0.f);
for (std::vector<float>::size_type i = 0; i != classes.size(); i++) {
classes[i] = features[cy][cx][offset + 5 + i];
}
classes = softmax(classes);
// find best class
int bestClassIdx = argMax(classes);
float bestClassScore = classes[bestClassIdx];
// combine confidence of bounding box with confidence of class
float classConfidence = bestClassScore * confidence;
// Only keep results that meet threshold
if (classConfidence > CONFIDENCE_THRESHOLD) {
Box bounds = {};
bounds.x = x - w/2; bounds.width = w;
bounds.y = y - h/2; bounds.height = h;
Prediction pred = {};
pred.box = bounds;
pred.classIndex = bestClassIdx;
pred.score = classConfidence;
predictions.push_back(pred);
}
}
}
}
return filterRedundantBoxes(predictions, IOU_THRESHOLD, MAX_BOXES);
}
float *** readResults(){
std::ifstream result_file ("PipeCNN/project/result_dump.txt", std::ios::in);
if (!result_file.is_open()) {
printf("Unable to open results file\n");
exit(-1);
}
int xDim;
int yDim;
int zDim;
result_file >> xDim;
result_file >> yDim;
result_file >> zDim;
float *** array = new float**[xDim];
for(int x=0; x<xDim; x++){
array[x] = new float*[yDim];
for(int y=0; y<yDim; y++){
array[x][y] = new float[zDim];
for(int z=0; z<zDim; z++){
result_file >> array[x][y][z];
}
}
}
result_file.close();
return array;
}
/*
draws rectangle around images identified inside camera frame
*/
void draw_bounds(cv::Mat &cameraFrame, std::vector<Prediction> predictions) {
// draw rectangle on camera frame
for (std::vector<Prediction>::size_type i = 0; i < predictions.size(); i++) {
cv::Scalar color(i%3 == 0 ? 255 : 0, i%3 == 1 ? 255 : 0, i%3 == 2 ? 255 : 0);
float x = std::max(predictions[i].box.x, 0.0f);
float y = std::max(predictions[i].box.y, 0.0f);
float w = predictions[i].box.width;
float h = predictions[i].box.height;
cv::rectangle(cameraFrame, CvRect(x, y, w, h), color, 2);
cv::putText(cameraFrame, labels[predictions[i].classIndex], cv::Point(x, y), CV_FONT_HERSHEY_DUPLEX, 0.7, color);
}
}
// int main() {
// float ***features = readResults();
// std::vector<Prediction> predictions = interpretNetworkOutput(features);
// cv::Mat cameraFrame(416,416, CV_8UC3, cv::Scalar(0, 0, 0));
// draw_bounds(cameraFrame, predictions);
// cv::imshow("output", cameraFrame);
// cv::waitKey(0);
// for (std::vector<Prediction>::size_type i = 0; i <predictions.size(); i++) {
// float x = std::max(predictions[i].box.x, 0.0f);
// float y = std::max(predictions[i].box.y, 0.0f);
// float w = predictions[i].box.width;
// float h = predictions[i].box.height;
// int xmin = x;
// int xmax = x+w;
// int ymin = y;
// int ymax = y+h;
// printf("class: %s, score: %f, Box: { xmin:%d, ymin: %d, xmax:%d, ymax: %d }\n", labels[predictions[i].classIndex].c_str(), predictions[i].score, xmin, ymin, xmax, ymax);
// }
// }