diff --git a/demo/config.yaml b/demo/config.yaml index e86bf2b..e6f91a7 100644 --- a/demo/config.yaml +++ b/demo/config.yaml @@ -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 diff --git a/demo/demo/map.cpp b/demo/demo/map.cpp index fcfd5a5..31ea821 100644 --- a/demo/demo/map.cpp +++ b/demo/demo/map.cpp @@ -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<init(net, n_classes); + std::ifstream all_labels(labels_path); std::string l_filename; std::vector 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<update(dnn_input); + frame = detNN->draw(frame); + detected_bbox = detNN->detected; + + TIMER_STOP + + if(write_res_on_file) + times< 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); }; diff --git a/include/tkDNN/DetectionNN.h b/include/tkDNN/DetectionNN.h index ddc2d43..50659e6 100644 --- a/include/tkDNN/DetectionNN.h +++ b/include/tkDNN/DetectionNN.h @@ -49,6 +49,7 @@ class DetectionNN { std::vector detected; /*bounding boxes in output*/ std::vector stats; /*keeps track of inference times (ms)*/ + std::vector 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 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); }; diff --git a/include/tkDNN/Yolo3Detection.h b/include/tkDNN/Yolo3Detection.h index 148a53f..547b0e0 100644 --- a/include/tkDNN/Yolo3Detection.h +++ b/include/tkDNN/Yolo3Detection.h @@ -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); }; diff --git a/src/CenternetDetection.cpp b/src/CenternetDetection.cpp index 29cdec6..acf438e 100644 --- a/src/CenternetDetection.cpp +++ b/src/CenternetDetection.cpp @@ -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: "<(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: "<(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: "<(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; -} - }} diff --git a/src/MobilenetDetection.cpp b/src/MobilenetDetection.cpp index adee317..8d4f363 100644 --- a/src/MobilenetDetection.cpp +++ b/src/MobilenetDetection.cpp @@ -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"<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(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"<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 \ No newline at end of file diff --git a/src/Yolo3Detection.cpp b/src/Yolo3Detection.cpp index f94dd94..987924d 100644 --- a/src/Yolo3Detection.cpp +++ b/src/Yolo3Detection.cpp @@ -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; iinput_dim.c; i++) { - std::cout<<"copio il channel"<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:"<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) {