Add writing results on file for map demo
Signed-off-by: Micaela Verucchi <micaelaverucchi@gmail.com>
This commit is contained in:
@@ -115,6 +115,9 @@ target_link_libraries(test_dla34_cnet tkDNN)
|
||||
add_executable(test_rtinference tests/test_rtinference/rtinference.cpp)
|
||||
target_link_libraries(test_rtinference tkDNN)
|
||||
|
||||
add_executable(map_demo demo/demo/map.cpp)
|
||||
target_link_libraries(map_demo tkDNN)
|
||||
|
||||
add_executable(demo demo/demo/demo.cpp)
|
||||
target_link_libraries(demo tkDNN)
|
||||
|
||||
|
||||
+20
-2
@@ -34,7 +34,15 @@ int main(int argc, char *argv[])
|
||||
char * labels_path = "../demo/COCO_val2017/all_labels.txt";
|
||||
bool show = false;
|
||||
bool write_dets = false;
|
||||
bool write_res_on_file = true;
|
||||
int n_images = 5000;
|
||||
|
||||
std::ofstream times;
|
||||
if(write_res_on_file)
|
||||
{
|
||||
times.open ("times.csv", std::ios_base::app);
|
||||
times<<net<<";";
|
||||
}
|
||||
|
||||
if(argc > 1)
|
||||
net = argv[1];
|
||||
@@ -91,6 +99,7 @@ int main(int argc, char *argv[])
|
||||
|
||||
//inference
|
||||
detected_bbox.clear();
|
||||
TIMER_START
|
||||
switch(ntype)
|
||||
{
|
||||
case 'y':
|
||||
@@ -104,6 +113,9 @@ int main(int argc, char *argv[])
|
||||
default:
|
||||
FatalError("Network type not allowed!\n");
|
||||
}
|
||||
TIMER_STOP
|
||||
if(write_res_on_file)
|
||||
times<<t_ns<<";";
|
||||
|
||||
std::ofstream myfile;
|
||||
if(write_dets)
|
||||
@@ -168,11 +180,17 @@ int main(int argc, char *argv[])
|
||||
IoU_thresh, conf_thresh, verbose);
|
||||
|
||||
//compute mAP
|
||||
double AP = computeMapNIoULevels(images,classes,IoU_thresh,conf_thresh, map_points, map_step, map_levels, verbose);
|
||||
double AP = computeMapNIoULevels(images,classes,IoU_thresh,conf_thresh, map_points, map_step, map_levels, verbose, write_res_on_file, net);
|
||||
std::cout<<"mAP "<<IoU_thresh<<":"<<IoU_thresh+map_step*(map_levels-1)<<" = "<<AP<<std::endl;
|
||||
|
||||
//compute average precision, recall and f1score
|
||||
computeTPFPFN(images,classes,IoU_thresh,conf_thresh);
|
||||
computeTPFPFN(images,classes,IoU_thresh,conf_thresh, verbose, write_res_on_file, net);
|
||||
|
||||
if(write_res_on_file)
|
||||
{
|
||||
times<<"\n";
|
||||
times.close();
|
||||
}
|
||||
|
||||
|
||||
return 0;
|
||||
|
||||
@@ -52,8 +52,8 @@ void readParams(char* config_filename, int& classes, int& map_points,
|
||||
float& conf_thresh, bool& verbose);
|
||||
|
||||
double computeMap(std::vector<Frame> &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<Frame> &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);
|
||||
double computeMapNIoULevels(std::vector<Frame> &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, const bool write_on_file = false, std::string net = "");
|
||||
|
||||
void computeTPFPFN(std::vector<Frame> &images,const int classes,const float IoU_thresh=0.5, const float conf_thresh=0.3, bool verbose=false);
|
||||
void computeTPFPFN(std::vector<Frame> &images,const int classes,const float IoU_thresh=0.5, const float conf_thresh=0.3, bool verbose=false, const bool write_on_file=false, std::string net="");
|
||||
|
||||
#endif /*EVALUATION_H*/
|
||||
#endif /*EVALUATION_H*/
|
||||
|
||||
@@ -37,7 +37,7 @@ class Yolo3Detection {
|
||||
int classes = 0;
|
||||
int num = 0;
|
||||
int n_masks = 0;
|
||||
float thresh = 0.3;
|
||||
float thresh = 0.05;
|
||||
cv::Scalar colors[256];
|
||||
|
||||
// this is filled with results
|
||||
@@ -70,4 +70,4 @@ class Yolo3Detection {
|
||||
|
||||
}}
|
||||
|
||||
#endif /* YOLODETECTION_H*/
|
||||
#endif /* YOLODETECTION_H*/
|
||||
|
||||
+43
-6
@@ -1,4 +1,5 @@
|
||||
#include "evaluation.h"
|
||||
#include <fstream>
|
||||
|
||||
|
||||
void BoundingBox::clear()
|
||||
@@ -283,27 +284,51 @@ double computeMap(std::vector<Frame> &images,const int classes,const float IoU_t
|
||||
return mean_average_precision;
|
||||
}
|
||||
|
||||
double computeMapNIoULevels(std::vector<Frame> &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 computeMapNIoULevels(std::vector<Frame> &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, const bool write_on_file, std::string net)
|
||||
{
|
||||
double AP = 0;
|
||||
std::ofstream out_file;
|
||||
if(write_on_file)
|
||||
{
|
||||
out_file.open("map.csv", std::ios_base::app);
|
||||
out_file<<net<<";";
|
||||
}
|
||||
|
||||
double AP = 0, cur_AP = 0;
|
||||
float IoU_thresh = i_IoU_thresh;
|
||||
for(int i=0; i<map_levels; ++i)
|
||||
{
|
||||
for(auto& img:images)
|
||||
for(auto & d:img.det)
|
||||
d.clear();
|
||||
AP += computeMap(images,classes,IoU_thresh,conf_thresh,map_points, verbose);
|
||||
cur_AP = computeMap(images,classes,IoU_thresh,conf_thresh,map_points, verbose);
|
||||
if(write_on_file)
|
||||
out_file<<cur_AP<<";";
|
||||
AP += cur_AP;
|
||||
IoU_thresh +=map_step;
|
||||
}
|
||||
AP/=map_levels;
|
||||
|
||||
if(write_on_file)
|
||||
{
|
||||
out_file<<AP<<"\n";
|
||||
out_file.close();
|
||||
}
|
||||
return AP;
|
||||
}
|
||||
|
||||
void computeTPFPFN(std::vector<Frame> &images,const int classes,const float IoU_thresh, const float conf_thresh, bool verbose)
|
||||
void computeTPFPFN(std::vector<Frame> &images,const int classes,const float IoU_thresh, const float conf_thresh, bool verbose, const bool write_on_file, std::string net)
|
||||
{
|
||||
|
||||
std::ofstream out_file;
|
||||
if(write_on_file)
|
||||
{
|
||||
out_file.open("pr.csv", std::ios_base::app);
|
||||
out_file<<net<<";";
|
||||
}
|
||||
|
||||
std::vector<int> truth_classes_count(classes,0);
|
||||
std::vector<int> dets_classes_count(classes,0);
|
||||
std::vector<PR> pr( classes);
|
||||
std::vector<PR> pr(classes);
|
||||
|
||||
for(auto &img:images)
|
||||
{
|
||||
@@ -355,6 +380,8 @@ void computeTPFPFN(std::vector<Frame> &images,const int classes,const float IoU_
|
||||
|
||||
double avg_precision = 0, avg_recall = 0, f1_score = 0;
|
||||
|
||||
|
||||
int TP = 0, FP = 0, FN = 0;
|
||||
for(size_t i=0; i<classes; i++)
|
||||
{
|
||||
pr[i].precision = (pr[i].tp + pr[i].fp) > 0 ? (double)pr[i].tp / (double)(pr[i].tp +pr[i].fp) : 0;
|
||||
@@ -364,13 +391,23 @@ void computeTPFPFN(std::vector<Frame> &images,const int classes,const float IoU_
|
||||
// std::cout<<i<<"\t"<<pr[i].tp<<"\t"<<pr[i].fp<<"\t"<<pr[i].fn<<"\t"<<pr[i].precision<<"\t"<<pr[i].recall<<std::endl;
|
||||
avg_precision += pr[i].precision;
|
||||
avg_recall += pr[i].recall;
|
||||
|
||||
TP += pr[i].tp;
|
||||
FP += pr[i].fp;
|
||||
FN += pr[i].fn;
|
||||
}
|
||||
avg_precision /= classes;
|
||||
avg_recall /= classes;
|
||||
|
||||
f1_score = avg_precision + avg_recall > 0 ? 2 * ( avg_precision * avg_recall ) / ( avg_precision + avg_recall ) : 0;
|
||||
|
||||
if(write_on_file)
|
||||
{
|
||||
out_file<<TP<<";"<<FP<<";"<<FN<<";"<<avg_precision<<";"<<avg_recall<<";"<<f1_score<<"\n";
|
||||
out_file.close();
|
||||
}
|
||||
|
||||
std::cout<<"avg precision: "<<avg_precision<<"\tavg recall: "<<avg_recall<<"\tavg f1 score:"<<f1_score<<std::endl;
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user