-
Notifications
You must be signed in to change notification settings - Fork 1
/
test_map.cpp
414 lines (367 loc) · 13.9 KB
/
test_map.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
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
#include <iostream>
#include <chrono>
#include <thread>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include "utils.h"
#include "objdetect_pub.h"
void trim(char *str)
{
char buffer[4096] = {0};
sprintf(buffer, "%s", str);
char *p = buffer;
while (*p == ' ' || *p == '\t') ++p;
char *end = p + strlen(p) - 1;
while (*end == ' ' || *end == '\t') {
*end = '\0';
--end;
}
sprintf(str, "%s", p);
}
void find_replace(char *str, char *orig, char *rep, char *output)
{
char buffer[4096] = {0};
char *p;
sprintf(buffer, "%s", str);
if(!(p = strstr(buffer, orig))){ // Is 'orig' even in 'str'?
sprintf(output, "%s", str);
return;
}
*p = '\0';
sprintf(output, "%s%s%s", buffer, rep, p+strlen(orig));
}
void find_replace_extension(char *str, char *orig, char *rep, char *output)
{
char *buffer = (char *)calloc(4096, sizeof(char));
sprintf(buffer, "%s", str);
char *p = strstr(buffer, orig);
int offset = (p - buffer);
int chars_from_end = strlen(buffer) - offset;
if (!p || chars_from_end != strlen(orig)) { // Is 'orig' even in 'str' AND is 'orig' found at the end of 'str'?
sprintf(output, "%s", str);
free(buffer);
return;
}
*p = '\0';
sprintf(output, "%s%s%s", buffer, rep, p + strlen(orig));
free(buffer);
}
void replace_image_to_label(char *input_path, char *output_path)
{
find_replace(input_path, "/images/train2014/", "/labels/train2014/", output_path); // COCO
find_replace(output_path, "/images/val2014/", "/labels/val2014/", output_path); // COCO
find_replace(output_path, "/JPEGImages/", "/labels/", output_path); // PascalVOC
find_replace(output_path, "\\images\\train2014\\", "\\labels\\train2014\\", output_path); // COCO
find_replace(output_path, "\\images\\val2014\\", "\\labels\\val2014\\", output_path); // COCO
find_replace(output_path, "\\JPEGImages\\", "\\labels\\", output_path); // PascalVOC
//find_replace(output_path, "/images/", "/labels/", output_path); // COCO
//find_replace(output_path, "/VOC2007/JPEGImages/", "/VOC2007/labels/", output_path); // PascalVOC
//find_replace(output_path, "/VOC2012/JPEGImages/", "/VOC2012/labels/", output_path); // PascalVOC
//find_replace(output_path, "/raw/", "/labels/", output_path);
trim(output_path);
// replace only ext of files
find_replace_extension(output_path, ".jpg", ".txt", output_path);
find_replace_extension(output_path, ".JPG", ".txt", output_path); // error
find_replace_extension(output_path, ".jpeg", ".txt", output_path);
find_replace_extension(output_path, ".JPEG", ".txt", output_path);
find_replace_extension(output_path, ".png", ".txt", output_path);
find_replace_extension(output_path, ".PNG", ".txt", output_path);
find_replace_extension(output_path, ".bmp", ".txt", output_path);
find_replace_extension(output_path, ".BMP", ".txt", output_path);
find_replace_extension(output_path, ".ppm", ".txt", output_path);
find_replace_extension(output_path, ".PPM", ".txt", output_path);
}
typedef struct{
int id;
float x,y,w,h;
float left, right, top, bottom;
} box_label;
box_label *read_boxes(char *filename, int *n)
{
FILE *file = fopen(filename, "r");
if(!file)
{
fprintf(stderr, "Couldn't open file: %s\n", filename);
exit(0);
}
float x, y, h, w;
int id;
int count = 0;
int size = 64;
box_label *boxes = (box_label *)calloc(size, sizeof(box_label));
while(fscanf(file, "%d %f %f %f %f", &id, &x, &y, &w, &h) == 5){
if(count == size) {
size = size * 2;
boxes = (box_label *)realloc(boxes, size*sizeof(box_label));
}
boxes[count].id = id;
boxes[count].x = x;
boxes[count].y = y;
boxes[count].h = h;
boxes[count].w = w;
boxes[count].left = x - w/2;
boxes[count].right = x + w/2;
boxes[count].top = y - h/2;
boxes[count].bottom = y + h/2;
++count;
}
fclose(file);
*n = count;
return boxes;
}
typedef struct{
float x, y, w, h;
} box;
typedef struct {
box b;
float p;
int class_id;
int image_index;
int truth_flag;
int unique_truth_index;
} box_prob;
float overlap(float x1, float w1, float x2, float w2)
{
float l1 = x1 - w1/2;
float l2 = x2 - w2/2;
float left = l1 > l2 ? l1 : l2;
float r1 = x1 + w1/2;
float r2 = x2 + w2/2;
float right = r1 < r2 ? r1 : r2;
return right - left;
}
float box_intersection(box a, box b)
{
float w = overlap(a.x, a.w, b.x, b.w);
float h = overlap(a.y, a.h, b.y, b.h);
if(w < 0 || h < 0) return 0;
float area = w*h;
return area;
}
float box_union(box a, box b)
{
float i = box_intersection(a, b);
float u = a.w*a.h + b.w*b.h - i;
return u;
}
float box_iou(box a, box b)
{
return box_intersection(a, b)/box_union(a, b);
}
int detections_comparator(const void *pa, const void *pb)
{
box_prob a = *(box_prob *)pa;
box_prob b = *(box_prob *)pb;
float diff = a.p - b.p;
if (diff < 0) return 1;
else if (diff > 0) return -1;
return 0;
}
int main
(
void
)
{
const float iou_thresh = 0.5f;
const float thresh_calc_avg_iou = 0.25f;
int netw = 300;
int neth = 300;
init_stack();
objdetect_init("/home/lincolnhard/Desktop/mobilenetssd-person.weights", netw, neth);
char imgpath[128];
char labelpath[128];
int num_pics = 0;
int classes = 1;
int *truth_classes_count = (int *)calloc(classes, sizeof(int));
FILE *fp = fopen("/media/lincolnhard/LHDISK1T/voc2/2007_test.txt", "r");
box_prob *detections = (box_prob *)calloc(1, sizeof(box_prob));
int detections_count = 0;
int unique_truth_count = 0;
float avg_iou = 0;
int tp_for_thresh = 0;
int fp_for_thresh = 0;
while(fgets(imgpath, 120, fp) != NULL)
{
size_t imgpathlen = strlen(imgpath) - 1;
if (*imgpath && imgpath[imgpathlen] == '\n')
{
imgpath[imgpathlen] = '\0';
}
std::cout << imgpath << std::endl;
cv::Mat im = cv::imread(imgpath);
cv::Mat netim;
cv::resize(im, netim, cv::Size(netw, neth));
int *out = objdetect_main(netim.data, im.cols, im.rows);
int nboxes = out[0];
replace_image_to_label(imgpath, labelpath);
int num_labels = 0;
box_label *truth = read_boxes(labelpath, &num_labels);
int j = 0;
for (j = 0; j < num_labels; ++j) {
truth_classes_count[truth[j].id]++;
}
const int checkpoint_detections_count = detections_count;
int i = 0;
for (i = 0; i < nboxes; ++i)
{
int class_id = 0;
for (class_id = 0; class_id < classes; ++class_id)
{
float prob = out[i*5+5];
if (prob > 0)
{
detections_count++;
detections = (box_prob *)realloc(detections, detections_count * sizeof(box_prob));
detections[detections_count - 1].b.w = (float)out[i*5+3]/(float)im.cols;
detections[detections_count - 1].b.h = (float)out[i*5+4]/(float)im.rows;
detections[detections_count - 1].b.x = (float)out[i*5+1]/(float)im.cols + detections[detections_count - 1].b.w / 2.0f;
detections[detections_count - 1].b.y = (float)out[i*5+2]/(float)im.rows + detections[detections_count - 1].b.h / 2.0f;
detections[detections_count - 1].p = prob;
detections[detections_count - 1].image_index = num_pics;
detections[detections_count - 1].class_id = class_id;
detections[detections_count - 1].truth_flag = 0;
detections[detections_count - 1].unique_truth_index = -1;
int truth_index = -1;
float max_iou = 0;
for (j = 0; j < num_labels; ++j)
{
box t = { truth[j].x, truth[j].y, truth[j].w, truth[j].h };
float current_iou = box_iou(detections[detections_count - 1].b, t);
if (current_iou > iou_thresh && class_id == truth[j].id)
{
if (current_iou > max_iou)
{
max_iou = current_iou;
truth_index = unique_truth_count + j;
}
}
}
// best IoU
if (truth_index > -1) {
detections[detections_count - 1].truth_flag = 1;
detections[detections_count - 1].unique_truth_index = truth_index;
}
// calc avg IoU, true-positives, false-positives for required Threshold
if (prob > thresh_calc_avg_iou)
{
int z, found = 0;
for (z = checkpoint_detections_count; z < detections_count-1; ++z)
{
if (detections[z].unique_truth_index == truth_index)
{
found = 1; break;
}
}
if(truth_index > -1 && found == 0)
{
avg_iou += max_iou;
++tp_for_thresh;
}
else
{
fp_for_thresh++;
}
}
}
}
}
unique_truth_count += num_labels;
++num_pics;
}
fclose(fp);
std::cout << num_pics << std::endl;
if((tp_for_thresh + fp_for_thresh) > 0)
{
avg_iou = avg_iou / (tp_for_thresh + fp_for_thresh);
}
// SORT(detections)
qsort(detections, detections_count, sizeof(box_prob), detections_comparator);
typedef struct {
double precision;
double recall;
int tp, fp, fn;
} pr_t;
int i = 0;
// for PR-curve
pr_t **pr = (pr_t **)calloc(classes, sizeof(pr_t*));
for (i = 0; i < classes; ++i) {
pr[i] = (pr_t *)calloc(detections_count, sizeof(pr_t));
}
printf("detections_count = %d, unique_truth_count = %d \n", detections_count, unique_truth_count);
int *truth_flags = (int *)calloc(unique_truth_count, sizeof(int));
int rank;
for (rank = 0; rank < detections_count; ++rank)
{
if (rank > 0) {
int class_id;
for (class_id = 0; class_id < classes; ++class_id) {
pr[class_id][rank].tp = pr[class_id][rank - 1].tp;
pr[class_id][rank].fp = pr[class_id][rank - 1].fp;
}
}
box_prob d = detections[rank];
// if (detected && isn't detected before)
if (d.truth_flag == 1) {
if (truth_flags[d.unique_truth_index] == 0)
{
truth_flags[d.unique_truth_index] = 1;
pr[d.class_id][rank].tp++; // true-positive
}
}
else {
pr[d.class_id][rank].fp++; // false-positive
}
for (i = 0; i < classes; ++i)
{
const int tp = pr[i][rank].tp;
const int fp = pr[i][rank].fp;
const int fn = truth_classes_count[i] - tp; // false-negative = objects - true-positive
pr[i][rank].fn = fn;
if ((tp + fp) > 0) pr[i][rank].precision = (double)tp / (double)(tp + fp);
else pr[i][rank].precision = 0;
if ((tp + fn) > 0) pr[i][rank].recall = (double)tp / (double)(tp + fn);
else pr[i][rank].recall = 0;
}
}
free(truth_flags);
double mean_average_precision = 0;
for (i = 0; i < classes; ++i) {
double avg_precision = 0;
int point;
for (point = 0; point < 11; ++point) {
double cur_recall = point * 0.1;
double cur_precision = 0;
for (rank = 0; rank < detections_count; ++rank)
{
if (pr[i][rank].recall >= cur_recall) { // > or >=
if (pr[i][rank].precision > cur_precision) {
cur_precision = pr[i][rank].precision;
}
}
}
//printf("class_id = %d, point = %d, cur_recall = %.4f, cur_precision = %.4f \n", i, point, cur_recall, cur_precision);
avg_precision += cur_precision;
}
avg_precision = avg_precision / 11;
printf("name = person, ap = %2.2f %%\n", avg_precision*100);
mean_average_precision += avg_precision;
}
const float cur_precision = (float)tp_for_thresh / ((float)tp_for_thresh + (float)fp_for_thresh);
const float cur_recall = (float)tp_for_thresh / ((float)tp_for_thresh + (float)(unique_truth_count - tp_for_thresh));
const float f1_score = 2.F * cur_precision * cur_recall / (cur_precision + cur_recall);
printf("for thresh = %1.2f, precision = %1.2f, recall = %1.2f, F1-score = %1.2f \n",
thresh_calc_avg_iou, cur_precision, cur_recall, f1_score);
printf("for thresh = %0.2f, TP = %d, FP = %d, FN = %d, average IoU = %2.2f %% \n",
thresh_calc_avg_iou, tp_for_thresh, fp_for_thresh, unique_truth_count - tp_for_thresh, avg_iou * 100);
mean_average_precision = mean_average_precision / classes;
printf("mean average precision (mAP) = %f, or %2.2f %% \n", mean_average_precision, mean_average_precision * 100);
for (i = 0; i < classes; ++i) {
free(pr[i]);
}
free(pr);
free(detections);
free(truth_classes_count);
free_stack();
return 0;
}