From d601e980f62ae816d2a34cee7c8ef9c66c4360ef Mon Sep 17 00:00:00 2001 From: xavier Date: Mon, 10 Feb 2020 18:19:08 +0100 Subject: [PATCH] Add avg precision, recall, f1score computation, other minors Signed-off-by: xavier --- demo/demo/map.cpp | 5 ++- include/evaluation.h | 6 ++- src/evaluation.cpp | 99 +++++++++++++++++++++++++++++++++++++++----- 3 files changed, 96 insertions(+), 14 deletions(-) diff --git a/demo/demo/map.cpp b/demo/demo/map.cpp index f7e9e3e..bbbd77a 100644 --- a/demo/demo/map.cpp +++ b/demo/demo/map.cpp @@ -164,11 +164,14 @@ int main(int argc, char *argv[]) int map_levels = 10; float map_step = 0.05; float IoU_thresh = 0.5; + float conf_thresh = 0.3; bool verbose = false; - double AP = computeMapNIoULevels(images,classes,IoU_thresh, map_points, map_step, map_levels, verbose); + double AP = computeMapNIoULevels(images,classes,IoU_thresh,conf_thresh, map_points, map_step, map_levels, verbose); std::cout<<"mAP "< &images,const int classes,const float IoU_thresh, const int map_points, const bool verbose=false); -double computeMapNIoULevels(std::vector &images,const int classes,const float i_IoU_thresh=0.5, const int map_points=101, const float map_step=0.05, const int map_levels=10, const bool verbose=false); +double computeMap(std::vector &images,const int classes,const float IoU_thresh, const float conf_thresh=0.3, const int map_points=101, const bool verbose=false); +double computeMapNIoULevels(std::vector &images,const int classes,const float i_IoU_thresh=0.5, const float conf_thresh=0.3, const int map_points=101, const float map_step=0.05, const int map_levels=10, const bool verbose=false); + +void computeTPFPFN(std::vector &images,const int classes,const float IoU_thresh=0.5, const float conf_thresh=0.3, bool verbose=false); #endif /*EVALUATION_H*/ \ No newline at end of file diff --git a/src/evaluation.cpp b/src/evaluation.cpp index 2b60449..65f7708 100644 --- a/src/evaluation.cpp +++ b/src/evaluation.cpp @@ -77,11 +77,8 @@ float boxIoU(const BoundingBox &a, const BoundingBox &b) } /* Credits to https://github.com/AlexeyAB/darknet/blob/master/src/detector.c*/ -double computeMap(std::vector &images,const int classes,const float IoU_thresh, const int map_points, const bool verbose) +double computeMap(std::vector &images,const int classes,const float IoU_thresh, const float conf_thresh, const int map_points, const bool verbose) { - - std::cout<<"Computing mAP"< &images,const int classes,const float IoU_t groundtruths_count += i.gt.size(); } - std::cout<<"gt_count: "< all_dets; std::vector all_gts; @@ -117,7 +117,7 @@ double computeMap(std::vector &images,const int classes,const float IoU_t { for(size_t i=0; i 0) + if(img.det[i].prob > conf_thresh) { float maxIoU = 0; int truth_index = -1; @@ -257,17 +257,18 @@ double computeMap(std::vector &images,const int classes,const float IoU_t avg_precision = avg_precision / map_points; } - std::cout<<"Class: "< &images,const int classes,const float i_IoU_thresh, const int map_points, const float map_step, const int map_levels, const bool verbose) +double computeMapNIoULevels(std::vector &images,const int classes,const float i_IoU_thresh, const float conf_thresh, const int map_points, const float map_step, const int map_levels, const bool verbose) { double AP = 0; float IoU_thresh = i_IoU_thresh; @@ -276,9 +277,85 @@ double computeMapNIoULevels(std::vector &images,const int classes,const f for(auto& img:images) for(auto & d:img.det) d.clear(); - AP += computeMap(images,classes,IoU_thresh,map_points, verbose); + AP += computeMap(images,classes,IoU_thresh,conf_thresh,map_points, verbose); IoU_thresh +=map_step; } AP/=map_levels; return AP; +} + +void computeTPFPFN(std::vector &images,const int classes,const float IoU_thresh, const float conf_thresh, bool verbose) +{ + std::vector truth_classes_count(classes,0); + std::vector dets_classes_count(classes,0); + std::vector pr( classes); + + for(auto &img:images) + { + for(auto& tc: truth_classes_count) + tc = 0; + for(auto& dc: dets_classes_count) + dc = 0; + + std::vector det_assigned(img.det.size(), false); + for(size_t j=0; j conf_thresh) + { + float currentIoU = boxIoU(img.det[i], img.gt[j]); + if(currentIoU > maxIoU && img.det[i].cl == img.gt[j].cl && !det_assigned[i]) + { + maxIoU = currentIoU; + det_index = i; + } + } + } + if(det_index > -1 && maxIoU > IoU_thresh && !det_assigned[det_index]) + { + img.det[det_index].unique_truth_index = j; + img.det[det_index].truth_flag = 1; + img.det[det_index].max_IoU = maxIoU; + det_assigned[det_index] = true; + dets_classes_count[img.det[det_index].cl]++; + } + } + + for(size_t i=0; i 0 ? (double)pr[i].tp / (double)(pr[i].tp +pr[i].fp) : 0; + pr[i].recall = (pr[i].tp + pr[i].fn) > 0 ? (double)pr[i].tp / (double)(pr[i].tp +pr[i].fn) : 0; + if(verbose) + std::cout<<"Class "< 0 ? 2 * ( avg_precision * avg_recall ) / ( avg_precision + avg_recall ) : 0; + + std::cout<<"avg precision: "<