From 37753e0cb4da93077446d4846582b9ee23cd3efc Mon Sep 17 00:00:00 2001 From: AlexeyAB Date: Wed, 8 Jul 2020 00:08:45 +0300 Subject: [PATCH] Improved tracking (new params) --- include/darknet.h | 5 ++++- src/demo.c | 2 +- src/http_stream.cpp | 51 ++++++++++++++++++++++++++++++++------------- src/parser.c | 3 +++ 4 files changed, 44 insertions(+), 17 deletions(-) diff --git a/include/darknet.h b/include/darknet.h index fa213bde..892ad71b 100644 --- a/include/darknet.h +++ b/include/darknet.h @@ -305,6 +305,9 @@ struct layer { int embedding_size; float sim_thresh; int track_history_size; + int dets_for_track; + int dets_for_show; + float track_ciou_norm; int coords; int background; int rescore; @@ -1068,7 +1071,7 @@ void stop_timer_and_show(); void stop_timer_and_show_name(char *name); void show_total_time(); -void set_track_id(detection *new_dets, int new_dets_num, float thresh, float sim_thresh, int deque_size); +void set_track_id(detection *new_dets, int new_dets_num, float thresh, float sim_thresh, float track_ciou_norm, int deque_size, int dets_for_track, int dets_for_show); int fill_remaining_id(detection *new_dets, int new_dets_num, int new_track_id, float thresh); diff --git a/src/demo.c b/src/demo.c index 458dc6af..5fff2360 100644 --- a/src/demo.c +++ b/src/demo.c @@ -265,7 +265,7 @@ void demo(char *cfgfile, char *weightfile, float thresh, float hier_thresh, int else diounms_sort(local_dets, local_nboxes, l.classes, nms, l.nms_kind, l.beta_nms); } - if (l.embedding_size) set_track_id(local_dets, local_nboxes, demo_thresh, l.sim_thresh, l.track_history_size); + if (l.embedding_size) set_track_id(local_dets, local_nboxes, demo_thresh, l.sim_thresh, l.track_ciou_norm, l.track_history_size, l.dets_for_track, l.dets_for_show); //printf("\033[2J"); //printf("\033[1;1H"); diff --git a/src/http_stream.cpp b/src/http_stream.cpp index 2168dbb1..3fd3c94c 100644 --- a/src/http_stream.cpp +++ b/src/http_stream.cpp @@ -775,13 +775,15 @@ int check_prob(detection det, float thresh) return 0; } -int fill_remaining_id(detection *new_dets, int new_dets_num, int new_track_id, float thresh) +int fill_remaining_id(detection *new_dets, int new_dets_num, int new_track_id, float thresh, int detection_count) { for (int i = 0; i < new_dets_num; ++i) { if (new_dets[i].track_id == 0 && check_prob(new_dets[i], thresh)) { //printf(" old_tid = %d, new_tid = %d, sim = %f \n", new_dets[i].track_id, new_track_id, new_dets[i].sim); - new_dets[i].track_id = new_track_id; - new_track_id++; + if (new_dets[i].sort_class > detection_count) { + new_dets[i].track_id = new_track_id; + new_track_id++; + } } } return new_track_id; @@ -795,7 +797,8 @@ float *make_float_array(float* src, size_t size) } struct detection_t : detection { - detection_t(detection det) : detection(det) + int det_count; + detection_t(detection det) : detection(det), det_count(0) { if (embeddings) embeddings = make_float_array(det.embeddings, embedding_size); if (prob) prob = make_float_array(det.prob, classes); @@ -818,7 +821,7 @@ struct detection_t : detection { -void set_track_id(detection *new_dets, int new_dets_num, float thresh, float sim_thresh, int deque_size) +void set_track_id(detection *new_dets, int new_dets_num, float thresh, float sim_thresh, float track_ciou_norm, int deque_size, int dets_for_track, int dets_for_show) { static int new_track_id = 1; static std::deque> old_dets_dq; @@ -858,26 +861,44 @@ void set_track_id(detection *new_dets, int new_dets_num, float thresh, float sim const int new_id = sim_det[index].new_id; const int old_id = sim_det[index].old_id; const int track_id = old_dets[old_id].track_id; - if (check_prob(new_dets[new_id], thresh) && track_idx[track_id] && new_idx[new_id] && old_idx[old_id] && sim_thresh < sim_det[index].sim) { - new_dets[new_id].sim = sim_det[index].sim; - new_dets[new_id].track_id = track_id; - new_idx[new_id] = 0; - old_idx[old_id] = 0; - track_idx[track_id] = 0; + const int det_count = old_dets[old_id].sort_class; + if (check_prob(new_dets[new_id], thresh) && track_idx[track_id] && new_idx[new_id] && old_idx[old_id]) { + float sim = sim_det[index].sim; + float ciou = box_ciou(new_dets[new_id].bbox, old_dets[old_id].bbox); + sim = sim * (1 - track_ciou_norm) + ciou * track_ciou_norm; + if (sim_thresh < sim) { + new_dets[new_id].sim = sim; + new_dets[new_id].track_id = track_id; + new_dets[new_id].sort_class = det_count + 1; + new_idx[new_id] = 0; + old_idx[old_id] = 0; + if(track_id) track_idx[track_id] = 0; + } } } // set new track_id - new_track_id = fill_remaining_id(new_dets, new_dets_num, new_track_id, thresh); + new_track_id = fill_remaining_id(new_dets, new_dets_num, new_track_id, thresh, dets_for_track); // store new_detections to the queue of vectors std::vector new_det_vec; for (int i = 0; i < new_dets_num; ++i) { - if(check_prob(new_dets[i], thresh)) + if (check_prob(new_dets[i], thresh)) { new_det_vec.push_back(new_dets[i]); + } } - old_dets_dq.push_back(new_det_vec); // add new - if (old_dets_dq.size() > deque_size) old_dets_dq.pop_front(); // remove old + // add new + old_dets_dq.push_back(new_det_vec); + // remove old + if (old_dets_dq.size() > deque_size) old_dets_dq.pop_front(); + + // remove detection which were detected only on few frames + for (int i = 0; i < new_dets_num; ++i) { + if (new_dets[i].sort_class < dets_for_show) + for (int j = 0; j < new_dets[i].classes; ++j) { + new_dets[i].prob[j] = 0; + } + } } diff --git a/src/parser.c b/src/parser.c index f403fef8..4ac1912c 100644 --- a/src/parser.c +++ b/src/parser.c @@ -489,6 +489,9 @@ layer parse_yolo(list *options, size_params params) l.track_history_size = option_find_int_quiet(options, "track_history_size", 5); l.sim_thresh = option_find_int_quiet(options, "sim_thresh", 0.8); + l.dets_for_track = option_find_int_quiet(options, "dets_for_track", 1); + l.dets_for_show = option_find_int_quiet(options, "dets_for_show", 1); + l.track_ciou_norm = option_find_float_quiet(options, "track_ciou_norm", 0.01); int embedding_layer_id = option_find_int_quiet(options, "embedding_layer", 999999); if (embedding_layer_id < 0) embedding_layer_id = params.index + embedding_layer_id; if (embedding_layer_id != 999999) {