Added ./darknet detector valid for KITTI and BDD (were previously MSCOCO and ImageNet)

pull/7577/head
AlexeyAB 6 years ago
parent 27da7f85c7
commit aaba21bdd3
  1. 139
      src/detector.c
  2. 11
      src/utils.c
  3. 1
      src/utils.h

@ -430,6 +430,104 @@ void print_imagenet_detections(FILE *fp, int id, detection *dets, int total, int
} }
} }
static void print_kitti_detections(FILE **fps, char *id, detection *dets, int total, int classes, int w, int h, char *outfile, char *prefix)
{
char *kitti_ids[] = { "car", "pedestrian", "cyclist" };
FILE *fpd = 0;
char buffd[1024];
snprintf(buffd, 1024, "%s/%s/data/%s.txt", prefix, outfile, id);
fpd = fopen(buffd, "w");
int i, j;
for (i = 0; i < total; ++i)
{
float xmin = dets[i].bbox.x - dets[i].bbox.w / 2.;
float xmax = dets[i].bbox.x + dets[i].bbox.w / 2.;
float ymin = dets[i].bbox.y - dets[i].bbox.h / 2.;
float ymax = dets[i].bbox.y + dets[i].bbox.h / 2.;
if (xmin < 0) xmin = 0;
if (ymin < 0) ymin = 0;
if (xmax > w) xmax = w;
if (ymax > h) ymax = h;
for (j = 0; j < classes; ++j)
{
//if (dets[i].prob[j]) fprintf(fpd, "%s 0 0 0 %f %f %f %f -1 -1 -1 -1 0 0 0 %f\n", kitti_ids[j], xmin, ymin, xmax, ymax, dets[i].prob[j]);
if (dets[i].prob[j]) fprintf(fpd, "%s -1 -1 -10 %f %f %f %f -1 -1 -1 -1000 -1000 -1000 -10 %f\n", kitti_ids[j], xmin, ymin, xmax, ymax, dets[i].prob[j]);
}
}
fclose(fpd);
}
static void eliminate_bdd(char *buf, char *a)
{
int n = 0;
int i, k;
for (i = 0; buf[i] != '\0'; i++)
{
if (buf[i] == a[n])
{
k = i;
while (buf[i] == a[n])
{
if (a[++n] == '\0')
{
for (k; buf[k + n] != '\0'; k++)
{
buf[k] = buf[k + n];
}
buf[k] = '\0';
break;
}
i++;
}
n = 0; i--;
}
}
}
static void get_bdd_image_id(char *filename)
{
char *p = strrchr(filename, '/');
eliminate_bdd(p, ".jpg");
eliminate_bdd(p, "/");
strcpy(filename, p);
}
static void print_bdd_detections(FILE *fp, char *image_path, detection *dets, int num_boxes, int classes, int w, int h)
{
char *bdd_ids[] = { "bike" , "bus" , "car" , "motor" ,"person", "rider", "traffic light", "traffic sign", "train", "truck" };
get_bdd_image_id(image_path);
int i, j;
for (i = 0; i < num_boxes; ++i)
{
float xmin = dets[i].bbox.x - dets[i].bbox.w / 2.;
float xmax = dets[i].bbox.x + dets[i].bbox.w / 2.;
float ymin = dets[i].bbox.y - dets[i].bbox.h / 2.;
float ymax = dets[i].bbox.y + dets[i].bbox.h / 2.;
if (xmin < 0) xmin = 0;
if (ymin < 0) ymin = 0;
if (xmax > w) xmax = w;
if (ymax > h) ymax = h;
float bx1 = xmin;
float by1 = ymin;
float bx2 = xmax;
float by2 = ymax;
for (j = 0; j < classes; ++j)
{
if (dets[i].prob[j])
{
fprintf(fp, "\t{\n\t\t\"name\":\"%s\",\n\t\t\"category\":\"%s\",\n\t\t\"bbox\":[%f, %f, %f, %f],\n\t\t\"score\":%f\n\t},\n", image_path, bdd_ids[j], bx1, by1, bx2, by2, dets[i].prob[j]);
}
}
}
}
void validate_detector(char *datacfg, char *cfgfile, char *weightfile, char *outfile) void validate_detector(char *datacfg, char *cfgfile, char *weightfile, char *outfile)
{ {
int j; int j;
@ -462,6 +560,9 @@ void validate_detector(char *datacfg, char *cfgfile, char *weightfile, char *out
FILE **fps = 0; FILE **fps = 0;
int coco = 0; int coco = 0;
int imagenet = 0; int imagenet = 0;
int bdd = 0;
int kitti = 0;
if (0 == strcmp(type, "coco")) { if (0 == strcmp(type, "coco")) {
if (!outfile) outfile = "coco_results"; if (!outfile) outfile = "coco_results";
snprintf(buff, 1024, "%s/%s.json", prefix, outfile); snprintf(buff, 1024, "%s/%s.json", prefix, outfile);
@ -469,6 +570,23 @@ void validate_detector(char *datacfg, char *cfgfile, char *weightfile, char *out
fprintf(fp, "[\n"); fprintf(fp, "[\n");
coco = 1; coco = 1;
} }
else if (0 == strcmp(type, "bdd")) {
if (!outfile) outfile = "bdd_results";
snprintf(buff, 1024, "%s/%s.json", prefix, outfile);
fp = fopen(buff, "w");
fprintf(fp, "[\n");
bdd = 1;
}
else if (0 == strcmp(type, "kitti")) {
char buff2[1024];
if (!outfile) outfile = "kitti_results";
printf("%s\n", outfile);
snprintf(buff, 1024, "%s/%s", prefix, outfile);
int mkd = make_directory(buff, 0777);
snprintf(buff2, 1024, "%s/%s/data", prefix, outfile);
int mkd2 = make_directory(buff2, 0777);
kitti = 1;
}
else if (0 == strcmp(type, "imagenet")) { else if (0 == strcmp(type, "imagenet")) {
if (!outfile) outfile = "imagenet-detection"; if (!outfile) outfile = "imagenet-detection";
snprintf(buff, 1024, "%s/%s.txt", prefix, outfile); snprintf(buff, 1024, "%s/%s.txt", prefix, outfile);
@ -478,7 +596,7 @@ void validate_detector(char *datacfg, char *cfgfile, char *weightfile, char *out
} }
else { else {
if (!outfile) outfile = "comp4_det_test_"; if (!outfile) outfile = "comp4_det_test_";
fps = (FILE**)calloc(classes, sizeof(FILE*)); fps = (FILE**) calloc(classes, sizeof(FILE *));
for (j = 0; j < classes; ++j) { for (j = 0; j < classes; ++j) {
snprintf(buff, 1024, "%s/%s%s.txt", prefix, outfile, names[j]); snprintf(buff, 1024, "%s/%s%s.txt", prefix, outfile, names[j]);
fps[j] = fopen(buff, "w"); fps[j] = fopen(buff, "w");
@ -542,15 +660,23 @@ void validate_detector(char *datacfg, char *cfgfile, char *weightfile, char *out
if (l.nms_kind == DEFAULT_NMS) do_nms_sort(dets, nboxes, l.classes, nms); if (l.nms_kind == DEFAULT_NMS) do_nms_sort(dets, nboxes, l.classes, nms);
else diounms_sort(dets, nboxes, l.classes, nms, l.nms_kind, l.beta_nms); else diounms_sort(dets, nboxes, l.classes, nms, l.nms_kind, l.beta_nms);
} }
if (coco) { if (coco) {
print_cocos(fp, path, dets, nboxes, classes, w, h); print_cocos(fp, path, dets, nboxes, classes, w, h);
} }
else if (imagenet) { else if (imagenet) {
print_imagenet_detections(fp, i + t - nthreads + 1, dets, nboxes, classes, w, h); print_imagenet_detections(fp, i + t - nthreads + 1, dets, nboxes, classes, w, h);
} }
else if (bdd) {
print_bdd_detections(fp, path, dets, nboxes, classes, w, h);
}
else if (kitti) {
print_kitti_detections(fps, id, dets, nboxes, classes, w, h, outfile, prefix);
}
else { else {
print_detector_detections(fps, id, dets, nboxes, classes, w, h); print_detector_detections(fps, id, dets, nboxes, classes, w, h);
} }
free_detections(dets, nboxes); free_detections(dets, nboxes);
free(id); free(id);
free_image(val[t]); free_image(val[t]);
@ -569,6 +695,17 @@ void validate_detector(char *datacfg, char *cfgfile, char *weightfile, char *out
#endif #endif
fprintf(fp, "\n]\n"); fprintf(fp, "\n]\n");
} }
if (bdd) {
#ifdef WIN32
fseek(fp, -3, SEEK_CUR);
#else
fseek(fp, -2, SEEK_CUR);
#endif
fprintf(fp, "\n]\n");
fclose(fp);
}
if (fp) fclose(fp); if (fp) fclose(fp);
if (val) free(val); if (val) free(val);

@ -14,8 +14,10 @@
#include "gettimeofday.h" #include "gettimeofday.h"
#else #else
#include <sys/time.h> #include <sys/time.h>
#include <sys/stat.h>
#endif #endif
#ifndef USE_CMAKE_LIBS #ifndef USE_CMAKE_LIBS
#pragma warning(disable: 4996) #pragma warning(disable: 4996)
#endif #endif
@ -935,4 +937,13 @@ boxabs box_to_boxabs(const box* b, const int img_w, const int img_h, const int b
} }
return ba; return ba;
}
int make_directory(char *path, int mode)
{
#ifdef WIN32
return _mkdir(path);
#else
return mkdir(path, mode);
#endif
} }

@ -81,6 +81,7 @@ int int_index(int *a, int val, int n);
int *random_index_order(int min, int max); int *random_index_order(int min, int max);
int max_int_index(int *a, int n); int max_int_index(int *a, int n);
boxabs box_to_boxabs(const box* b, const int img_w, const int img_h, const int bounds_check); boxabs box_to_boxabs(const box* b, const int img_w, const int img_h, const int bounds_check);
int make_directory(char *path, int mode);
#ifdef __cplusplus #ifdef __cplusplus
} }

Loading…
Cancel
Save