Modify map demo, using abstract class. Move draw function in abstract class

Signed-off-by: Micaela Verucchi <micaelaverucchi@gmail.com>
This commit is contained in:
Micaela Verucchi
2020-03-23 19:32:37 +01:00
parent bbcc33c0cf
commit df37e11709
10 changed files with 85 additions and 150 deletions
+1 -1
View File
@@ -3,5 +3,5 @@ map_points : 101 #number of recall points (0 for all, 101 for COCO, 11 Pascal
map_levels : 10 #number of IoU step for the AP
map_step : 0.05 #step of IoU
IoU_thresh : 0.5 #starting IoU threshold
conf_thresh : 0.3 #threshold on the condifence of the bbox
conf_thresh : 0.0 #threshold on the condifence of the bbox
verbose : false #print on screen information
+31 -41
View File
@@ -38,6 +38,14 @@ int main(int argc, char *argv[])
bool write_res_on_file = true;
int n_images = 5000;
bool verbose;
int classes, map_points, map_levels;
float map_step, IoU_thresh, conf_thresh;
//read mAP parameters
readParams( config_filename, classes, map_points, map_levels, map_step,
IoU_thresh, conf_thresh, verbose);
if(argc > 1)
net = argv[1];
if(argc > 2)
@@ -62,26 +70,32 @@ int main(int argc, char *argv[])
times<<net<<";";
}
tk::dnn::Yolo3Detection yolo;
tk::dnn::CenternetDetection cnet;
tk::dnn::MobilenetDetection mbnet;
tk::dnn::MobilenetDetection mbnet;
tk::dnn::DetectionNN *detNN;
int n_classes = classes;
switch(ntype)
{
case 'y':
yolo.init(net);
detNN = &yolo;
break;
case 'c':
cnet.init(net);
detNN = &cnet;
break;
case 'm':
mbnet.init(net, 81);
detNN = &mbnet;
n_classes++;
break;
default:
FatalError("Network type not allowed (3rd parameter)\n");
}
detNN->init(net, n_classes);
std::ifstream all_labels(labels_path);
std::string l_filename;
std::vector<Frame> images;
@@ -115,36 +129,18 @@ int main(int argc, char *argv[])
dnn_input = frame.clone();
//inference
TIMER_START
detected_bbox.clear();
TIMER_START
switch(ntype)
{
case 'y':
yolo.update(dnn_input);
detected_bbox = yolo.detected;
break;
case 'c':
cnet.update(dnn_input);
detected_bbox = cnet.detected;
break;
case 'm':
mbnet.update(dnn_input);
detected_bbox = mbnet.detected;
for(auto& d:detected_bbox)
{
d.x = d.x;
d.y = d.y;
d.w = d.w - d.x; //in mobilenet b.w represents x1
d.h = d.h - d.y; //in mobilenet b.h repsresnts y1
d.cl = d.cl -1; //remove background class
}
break;
default:
FatalError("Network type not allowed!\n");
}
TIMER_STOP
if(write_res_on_file)
times<<t_ns<<";";
detNN->update(dnn_input);
frame = detNN->draw(frame);
detected_bbox = detNN->detected;
TIMER_STOP
if(write_res_on_file)
times<<t_ns<<";";
std::ofstream myfile;
if(write_dets)
@@ -200,13 +196,7 @@ int main(int argc, char *argv[])
std::cout<<"Done."<<std::endl;
bool verbose;
int classes, map_points, map_levels;
float map_step, IoU_thresh, conf_thresh;
//read mAP parameters
readParams( config_filename, classes, map_points, map_levels, map_step,
IoU_thresh, conf_thresh, verbose);
//compute mAP
double AP = computeMapNIoULevels(images,classes,IoU_thresh,conf_thresh, map_points, map_step, map_levels, verbose, write_res_on_file, net);
-3
View File
@@ -19,8 +19,6 @@ namespace tk { namespace dnn {
class CenternetDetection : public DetectionNN
{
private:
std::vector<std::string> classesNames;
tk::dnn::dataDim_t dim;
tk::dnn::dataDim_t dim2;
tk::dnn::dataDim_t dim_hm;
@@ -79,7 +77,6 @@ public:
void preprocess(cv::Mat &frame);
void update(cv::Mat &frame);
void postprocess(dnnType **rt_out, const int n_out);
cv::Mat draw(cv::Mat &frame);
};
+33 -3
View File
@@ -49,6 +49,7 @@ class DetectionNN {
std::vector<tk::dnn::box> detected; /*bounding boxes in output*/
std::vector<double> stats; /*keeps track of inference times (ms)*/
std::vector<std::string> classesNames;
DetectionNN() {};
~DetectionNN(){};
@@ -70,9 +71,9 @@ class DetectionNN {
virtual void preprocess(cv::Mat &frame) = 0;
/**
* This method performs the inference of the NN.
* This method performs the whole detection of the NN.
*
* @param frame to run inference on.
* @param frame to run detection on.
*/
virtual void update(cv::Mat &frame) = 0;
@@ -91,7 +92,36 @@ class DetectionNN {
* @param orginal frame to draw bounding box on.
* @return frame with boundig boxes.
*/
virtual cv::Mat draw(cv::Mat &frame) = 0;
cv::Mat draw(cv::Mat &frame)
{
tk::dnn::box b;
int x0, w, x1, y0, h, y1;
int objClass;
std::string det_class;
int baseline = 0;
float font_scale = 0.5;
int thickness = 2;
// draw dets
for(int i=0; i<detected.size(); i++) {
b = detected[i];
x0 = b.x;
x1 = b.x + b.w;
y0 = b.y;
y1 = b.y + b.h;
det_class = classesNames[b.cl];
// draw rectangle
cv::rectangle(frame, cv::Point(x0, y0), cv::Point(x1, y1), colors[b.cl], 2);
// draw label
cv::Size text_size = getTextSize(det_class, cv::FONT_HERSHEY_SIMPLEX, font_scale, thickness, &baseline);
cv::rectangle(frame, cv::Point(x0, y0), cv::Point((x0 + text_size.width - 2), (y0 - text_size.height - 2)), colors[b.cl], -1);
cv::putText(frame, det_class, cv::Point(x0, (y0 - (baseline / 2))), cv::FONT_HERSHEY_SIMPLEX, font_scale, cv::Scalar(255, 255, 255), thickness);
}
return frame;
}
};
}}
+1 -1
View File
@@ -475,7 +475,7 @@ public:
dnnType *predictions;
static const int MAX_DETECTIONS = 1024;
static const int MAX_DETECTIONS = 2048;
static Yolo::detection *allocateDetections(int nboxes, int classes);
static void mergeDetections(Yolo::detection *dets, int ndets, int classes);
};
+1 -2
View File
@@ -53,7 +53,7 @@ private:
int nPriors = 0;
float *locations_h, *confidences_h;
std::vector<std::string> classesNames;
void generate_ssd_priors(const SSDSpec *specs, const int n_specs, bool clamp = true);
void convert_locatios_to_boxes_and_center();
@@ -69,7 +69,6 @@ public:
void preprocess(cv::Mat &frame);
void update(cv::Mat &frame);
void postprocess(dnnType **rt_out, const int n_out);
cv::Mat draw(cv::Mat &frame);
};
-1
View File
@@ -26,7 +26,6 @@ public:
void preprocess(cv::Mat &frame);
void update(cv::Mat &frame);
void postprocess(dnnType **rt_out, const int n_out);
cv::Mat draw(cv::Mat &frame);
};
+4 -35
View File
@@ -131,7 +131,7 @@ void CenternetDetection::preprocess(cv::Mat &frame)
// auto step_t = std::chrono::steady_clock::now();
// auto end_t = std::chrono::steady_clock::now();
cv::Size sz = originalSize;
std::cout<<"image: "<<sz.width<<", "<<sz.height<<std::endl;
// std::cout<<"image: "<<sz.width<<", "<<sz.height<<std::endl;
cv::Size sz_old;
float scale = 1.0;
float new_height = sz.height * scale;
@@ -186,7 +186,7 @@ void CenternetDetection::preprocess(cv::Mat &frame)
checkCuda( cudaDeviceSynchronize() );
sz = imageF1_d.size();
std::cout<<"size: "<<sz.height<<" "<<sz.width<<" - "<<std::endl;
// std::cout<<"size: "<<sz.height<<" "<<sz.width<<" - "<<std::endl;
// end_t = std::chrono::steady_clock::now();
// std::cout << " TIME resize: " << std::chrono::duration_cast<std::chrono:: microseconds>(end_t - step_t).count() << " us" << std::endl;
// step_t = end_t;
@@ -226,7 +226,7 @@ void CenternetDetection::preprocess(cv::Mat &frame)
cv::Mat imageF;
resize(frame, imageF, cv::Size(new_width, new_height));
sz = imageF.size();
std::cout<<"size: "<<sz.height<<" "<<sz.width<<" - "<<std::endl;
// std::cout<<"size: "<<sz.height<<" "<<sz.width<<" - "<<std::endl;
// end_t = std::chrono::steady_clock::now();
// std::cout << " TIME resize: " << std::chrono::duration_cast<std::chrono:: microseconds>(end_t - step_t).count() << " us" << std::endl;
// step_t = end_t;
@@ -238,7 +238,7 @@ void CenternetDetection::preprocess(cv::Mat &frame)
// step_t = end_t;
sz = imageF.size();
std::cout<<"size: "<<sz.height<<" "<<sz.width<<" - "<<std::endl;
// std::cout<<"size: "<<sz.height<<" "<<sz.width<<" - "<<std::endl;
imageF.convertTo(imageF, CV_32FC3, 1/255.0);
// end_t = std::chrono::steady_clock::now();
// std::cout << " TIME convertto: " << std::chrono::duration_cast<std::chrono:: microseconds>(end_t - step_t).count() << " us" << std::endl;
@@ -425,37 +425,6 @@ void CenternetDetection::postprocess(dnnType **rt_out, const int n_out)
// step_t = end_t;
}
cv::Mat CenternetDetection::draw(cv::Mat &frame)
{
tk::dnn::box b;
int x0, w, x1, y0, h, y1;
int objClass;
std::string det_class;
int baseline = 0;
float font_scale = 0.5;
int thickness = 2;
int num_detected = detected.size();
for (int i = 0; i < num_detected; i++){
b = detected[i];
x0 = b.x;
w = b.w;
x1 = b.x + w;
y0 = b.y;
h = b.h;
y1 = b.y + h;
objClass = b.cl;
det_class = classesNames[objClass];
cv::rectangle(frame, cv::Point(x0, y0), cv::Point(x1, y1), colors[objClass], 2);
// draw label
cv::Size textSize = getTextSize(det_class, cv::FONT_HERSHEY_SIMPLEX, font_scale, thickness, &baseline);
cv::rectangle(frame, cv::Point(x0, y0), cv::Point((x0 + textSize.width - 2), (y0 - textSize.height - 2)), colors[b.cl], -1);
cv::putText(frame, det_class, cv::Point(x0, (y0 - (baseline / 2))), cv::FONT_HERSHEY_SIMPLEX, font_scale, cv::Scalar(255, 255, 255), thickness);
}
return frame;
}
}}
+12 -31
View File
@@ -131,8 +131,7 @@ float MobilenetDetection::iou(const tk::dnn::box &a, const tk::dnn::box &b)
bool MobilenetDetection::init(const std::string& tensor_path, const int n_classes)
{
std::cout<<"MobilenetDetection Init"<<std::endl;
std::cout<<(tensor_path).c_str()<<"\n";
netRT = new tk::dnn::NetworkRT(NULL, (tensor_path).c_str());
imageSize = netRT->input_dim.h;
classes = n_classes;
@@ -179,7 +178,7 @@ bool MobilenetDetection::init(const std::string& tensor_path, const int n_classe
if(classes == 21){
const char *classes_names_[] = {
"BACKGROUND", "aeroplane", "bicycle", "bird", "boat", "bottle", "bus",
"aeroplane", "bicycle", "bird", "boat", "bottle", "bus",
"car", "cat", "chair", "cow", "diningtable", "dog", "horse", "motorbike",
"person", "pottedplant", "sheep", "sofa", "train", "tvmonitor"};
classesNames = std::vector<std::string>(classes_names_, std::end(classes_names_));
@@ -187,7 +186,7 @@ bool MobilenetDetection::init(const std::string& tensor_path, const int n_classe
}
else if (classes == 81){
const char *classes_names_[] = {
"BACKGROUND", "person" , "bicycle" , "car" , "motorbike" , "aeroplane" , "bus" ,
"person" , "bicycle" , "car" , "motorbike" , "aeroplane" , "bus" ,
"train" , "truck" , "boat" , "traffic light" , "fire hydrant" , "stop sign" ,
"parking meter" , "bench" , "bird" , "cat" , "dog" , "horse" , "sheep" , "cow" ,
"elephant" , "bear" , "zebra" , "giraffe" , "backpack" , "umbrella" , "handbag" ,
@@ -210,7 +209,6 @@ bool MobilenetDetection::init(const std::string& tensor_path, const int n_classe
void MobilenetDetection::preprocess(cv::Mat &frame)
{
std::cout<<"preprocess"<<std::endl;
#ifdef OPENCV_CUDA
//move original image on GPU
cv::cuda::GpuMat orig_img, frame_nomean;
@@ -248,8 +246,10 @@ void MobilenetDetection::preprocess(cv::Mat &frame)
void MobilenetDetection::update(cv::Mat &frame)
{
TIMER_START
detected.clear();
if(!frame.data) {
std::cout<<"MOBILENET: NO IMAGE DATA\n";
return;
}
originalSize = frame.size();
//preprocess
@@ -271,6 +271,7 @@ void MobilenetDetection::update(cv::Mat &frame)
rt_out[0] = (dnnType *)netRT->buffersRT[3];
rt_out[1] = (dnnType *)netRT->buffersRT[4];
detected.clear();
//postprocess
postprocess(rt_out, 2);
@@ -313,12 +314,12 @@ void MobilenetDetection::postprocess(dnnType **rt_out, const int n_out)
remaining.clear();
tk::dnn::box b;
b.cl = boxes[0].cl;
b.cl = boxes[0].cl -1 ; //remove background class
b.prob = boxes[0].prob;
b.x = boxes[0].x * width;
b.x = boxes[0].x * width;
b.y = boxes[0].y * height;
b.w = boxes[0].w * width;
b.h = boxes[0].h * height;
b.w = boxes[0].w * width - b.x; //convert from x1 to width
b.h = boxes[0].h * height - b.y; //convert from y1 to height
detected.push_back(b);
for (size_t j = 1; j < boxes.size(); j++){
if (iou(boxes[0], boxes[j]) <= IoUThreshold){
@@ -331,25 +332,5 @@ void MobilenetDetection::postprocess(dnnType **rt_out, const int n_out)
}
cv::Mat MobilenetDetection::draw(cv::Mat &frame)
{
int baseline = 0;
float font_scale = 0.5;
int thickness = 2;
tk::dnn::box b;
for (size_t i = 0; i < detected.size(); i++){
b = detected[i];
std::string det_class = classesNames[b.cl];
cv::rectangle(frame, cv::Point(b.x, b.y), cv::Point(b.w, b.h), colors[b.cl], 2);
// draw label
cv::Size text_size = getTextSize(det_class, cv::FONT_HERSHEY_SIMPLEX, font_scale, thickness, &baseline);
cv::rectangle(frame, cv::Point(b.x, b.y), cv::Point((b.x + text_size.width - 2), (b.y - text_size.height - 2)), colors[b.cl], -1);
cv::putText(frame, det_class, cv::Point(b.x, (b.y - (baseline / 2))), cv::FONT_HERSHEY_SIMPLEX, font_scale, cv::Scalar(255, 255, 255), thickness);
}
return frame;
}
} // namespace dnn
} // namespace tk
+2 -32
View File
@@ -43,6 +43,8 @@ bool Yolo3Detection::init(const std::string& tensor_path, const int n_classes) {
float b = getColor(0, offset, classes);
colors[c] = cv::Scalar(int(255.0*b), int(255.0*g), int(255.0*r));
}
classesNames = getYoloLayer()->classesNames;
return true;
}
@@ -60,7 +62,6 @@ void Yolo3Detection::preprocess(cv::Mat &frame)
//write channels
for(int i=0; i<netRT->input_dim.c; i++) {
std::cout<<"copio il channel"<<i<<std::endl;
int idx = i*imagePreproc.rows*imagePreproc.cols;
int ch = netRT->input_dim.c-1 -i;
checkCuda( cudaMemcpy((void*)&input_d[idx], (void*)bgr[ch].data, imagePreproc.rows*imagePreproc.cols*sizeof(dnnType), cudaMemcpyDeviceToDevice));
@@ -122,8 +123,6 @@ void Yolo3Detection::postprocess(dnnType **rt_out, const int n_out)
float x_ratio = float(originalSize.width) / float(netRT->input_dim.w);
float y_ratio = float(originalSize.height) / float(netRT->input_dim.h);
std::cout<<"RATIO:"<<x_ratio<<" "<<y_ratio<<std::endl;
// compute dets
nDets = 0;
for(int i=0; i<n_out; i++) {
@@ -166,37 +165,8 @@ void Yolo3Detection::postprocess(dnnType **rt_out, const int n_out)
detected.push_back(res);
}
}
std::cout<<"N detections: "<<detected.size()<<std::endl;
}
cv::Mat Yolo3Detection::draw(cv::Mat &frame)
{
tk::dnn::box b;
int x0, w, x1, y0, h, y1;
int objClass;
std::string det_class;
int baseline = 0;
float font_scale = 0.5;
int thickness = 2;
// draw dets
for(int i=0; i<detected.size(); i++) {
b = detected[i];
x0 = b.x;
x1 = b.x + b.w;
y0 = b.y;
y1 = b.y + b.h;
det_class = getYoloLayer()->classesNames[b.cl];
// draw rectangle
cv::rectangle(frame, cv::Point(x0, y0), cv::Point(x1, y1), colors[b.cl], 2);
// draw label
cv::Size text_size = getTextSize(det_class, cv::FONT_HERSHEY_SIMPLEX, font_scale, thickness, &baseline);
cv::rectangle(frame, cv::Point(x0, y0), cv::Point((x0 + text_size.width - 2), (y0 - text_size.height - 2)), colors[b.cl], -1);
cv::putText(frame, det_class, cv::Point(x0, (y0 - (baseline / 2))), cv::FONT_HERSHEY_SIMPLEX, font_scale, cv::Scalar(255, 255, 255), thickness);
}
return frame;
}
tk::dnn::Yolo* Yolo3Detection::getYoloLayer(int n)
{