From 5c6d6ae0af4ec97e1f981389d85634ca4aec9454 Mon Sep 17 00:00:00 2001 From: Micaela Verucchi Date: Thu, 19 Mar 2020 18:13:35 +0100 Subject: [PATCH] Style fix, useless params removed Signed-off-by: Micaela Verucchi --- include/tkDNN/MobilenetDetection.h | 15 +-- src/MobilenetDetection.cpp | 162 ++++++++++------------------- 2 files changed, 61 insertions(+), 116 deletions(-) diff --git a/include/tkDNN/MobilenetDetection.h b/include/tkDNN/MobilenetDetection.h index 405f781..09bdac8 100644 --- a/include/tkDNN/MobilenetDetection.h +++ b/include/tkDNN/MobilenetDetection.h @@ -7,10 +7,8 @@ #include #include #include - #include "opencv2/opencv.hpp" - #include "tkdnn.h" #define N_COORDS 4 @@ -58,17 +56,16 @@ private: tk::dnn::NetworkRT *netRT = nullptr; int classes; - float iouThreshold = 0.45; + float IoUThreshold = 0.45; float centerVariance = 0.1; float sizeVariance = 0.2; - float confThresh = 0.4; + float confThreshold = 0.4; int imageSize; float *priors = nullptr; - int n_priors = 0; + int nPriors = 0; cv::Mat origImg; - float *input, *input_d; float *locations_h, *confidences_h; @@ -78,18 +75,16 @@ private: dnnType *conf; dnnType *loc; - - float __colors[6][3] = {{1, 0, 1}, {0, 0, 1}, {0, 1, 1}, {0, 1, 0}, {1, 1, 0}, {1, 0, 0}}; int baseline = 0; float fontScale = 0.5; int thickness = 2; void generate_ssd_priors(const SSDSpec *specs, const int n_specs, bool clamp = true); - void convert_locatios_to_boxes_and_center(float *priors, const int n_priors, float *locations, const float center_variance, const float size_variance); + void convert_locatios_to_boxes_and_center(); float iou(const tk::dnn::box &a, const tk::dnn::box &b); void preprocess(const bool gpu = true); - std::vector postprocess(float *locations, float *confidences, const int n_values, const float threshold, const int n_classes, const float iou_thresh, const int width, const int height); + std::vector postprocess(const int width, const int height); float get_color2(int c, int x, int max); cv::Scalar colors[256]; diff --git a/src/MobilenetDetection.cpp b/src/MobilenetDetection.cpp index 8b8ed0a..87a7bc2 100644 --- a/src/MobilenetDetection.cpp +++ b/src/MobilenetDetection.cpp @@ -1,40 +1,31 @@ #include "MobilenetDetection.h" -bool boxProbCmp(const tk::dnn::box &a, const tk::dnn::box &b) -{ +bool boxProbCmp(const tk::dnn::box &a, const tk::dnn::box &b){ return (a.prob > b.prob); } -namespace tk -{ -namespace dnn -{ +namespace tk{ +namespace dnn{ void MobilenetDetection::generate_ssd_priors(const SSDSpec *specs, const int n_specs, bool clamp) { - n_priors = 0; + nPriors = 0; for (int i = 0; i < n_specs; i++) { - n_priors += specs[i].featureSize * specs[i].featureSize * 6; + nPriors += specs[i].featureSize * specs[i].featureSize * 6; } - // std::cout<<"n priors: "< specs[i].boxWidth ? specs[i].boxWidth : specs[i].boxHeight; max = specs[i].boxHeight < specs[i].boxWidth ? specs[i].boxWidth : specs[i].boxHeight; - for (int j = 0; j < specs[i].featureSize; j++) - { - for (int k = 0; k < specs[i].featureSize; k++) - { + for (int j = 0; j < specs[i].featureSize; j++){ + for (int k = 0; k < specs[i].featureSize; k++){ //small sized square box size = min; x_center = (k + 0.5f) / scale; @@ -89,39 +80,30 @@ void MobilenetDetection::generate_ssd_priors(const SSDSpec *specs, const int n_s } } - if (clamp) - { - for (int i = 0; i < n_priors * N_COORDS; i++) - { + if (clamp){ + for (int i = 0; i < nPriors * N_COORDS; i++){ priors[i] = priors[i] > 1.0f ? 1.0f : priors[i]; priors[i] = priors[i] < 0.0f ? 0.0f : priors[i]; - - // std::cout< 0 ? min_w - max_x : 0; float ao_h = min_h - max_y > 0 ? min_h - max_y : 0; - // std::cout<<" ao w: "< 0 ? a.w - a.x : 0; float area_0_h = a.h - a.y > 0 ? a.h - a.y : 0; @@ -147,42 +127,35 @@ float MobilenetDetection::iou(const tk::dnn::box &a, const tk::dnn::box &b) float area_0 = area_0_h * area_0_w; float area_1 = area_1_h * area_1_w; - // std::cout<<" area_overlap : "< MobilenetDetection::postprocess(float *locations, float *confidences, const int n_values, const float threshold, const int n_classes, const float iou_thresh, const int width, const int height) +std::vector MobilenetDetection::postprocess(const int width, const int height) { float *conf_per_class; std::vector detections; - for (int i = 1; i < n_classes; i++) - { - conf_per_class = &confidences[i * n_values]; + for (int i = 1; i < classes; i++){ + conf_per_class = &confidences_h[i * nPriors]; std::vector boxes; - for (int j = 0; j < n_values; j++) - { + for (int j = 0; j < nPriors; j++){ - if (conf_per_class[j] > threshold) - { + if (conf_per_class[j] > confThreshold){ tk::dnn::box b; b.cl = i; b.prob = conf_per_class[j]; - b.x = locations[j * N_COORDS + 0]; - b.y = locations[j * N_COORDS + 1]; - b.w = locations[j * N_COORDS + 2]; - b.h = locations[j * N_COORDS + 3]; + b.x = locations_h[j * N_COORDS + 0]; + b.y = locations_h[j * N_COORDS + 1]; + b.w = locations_h[j * N_COORDS + 2]; + b.h = locations_h[j * N_COORDS + 3]; boxes.push_back(b); } } std::sort(boxes.begin(), boxes.end(), boxProbCmp); - // for(auto b:boxes) - // b.print(); + std::vector remaining; - while (boxes.size() > 0) - { + while (boxes.size() > 0){ remaining.clear(); tk::dnn::box b; @@ -193,20 +166,14 @@ std::vector MobilenetDetection::postprocess(float *locations, floa b.w = boxes[0].w * width; b.h = boxes[0].h * height; detections.push_back(b); - for (size_t j = 1; j < boxes.size(); j++) - { - if (iou(boxes[0], boxes[j]) <= iou_thresh) - { + for (size_t j = 1; j < boxes.size(); j++){ + if (iou(boxes[0], boxes[j]) <= IoUThreshold){ remaining.push_back(boxes[j]); } } boxes = remaining; } } - // std::cout<<"picked"<input_dim.tot())); checkCuda(cudaMalloc(&input_d, sizeof(dnnType) * netRT->input_dim.tot())); - locations_h = (float *)malloc(N_COORDS * n_priors * sizeof(float)); - confidences_h = (float *)malloc(n_priors * classes * sizeof(float)); + locations_h = (float *)malloc(N_COORDS * nPriors * sizeof(float)); + confidences_h = (float *)malloc(nPriors * classes * sizeof(float)); dim = tk::dnn::dataDim_t(1, 3, imageSize, imageSize, 1); - for (int c = 0; c < classes; c++) - { + for (int c = 0; c < classes; c++){ int offset = c * 123457 % classes; float r = get_color2(2, offset, classes); float g = get_color2(1, offset, classes); @@ -275,8 +235,7 @@ void MobilenetDetection::init(std::string tensor_path, int input_size, int n_cla colors[c] = cv::Scalar(int(255.0 * b), int(255.0 * g), int(255.0 * r)); } - if(classes == 21) - { + if(classes == 21){ const char *classes_names_[] = { "BACKGROUND", "aeroplane", "bicycle", "bird", "boat", "bottle", "bus", "car", "cat", "chair", "cow", "diningtable", "dog", "horse", "motorbike", @@ -284,8 +243,7 @@ void MobilenetDetection::init(std::string tensor_path, int input_size, int n_cla classesNames = std::vector(classes_names_, std::end(classes_names_)); } - else if (classes == 81) - { + else if (classes == 81){ const char *classes_names_[] = { "BACKGROUND", "person" , "bicycle" , "car" , "motorbike" , "aeroplane" , "bus" , "train" , "truck" , "boat" , "traffic light" , "fire hydrant" , "stop sign" , @@ -302,19 +260,15 @@ void MobilenetDetection::init(std::string tensor_path, int input_size, int n_cla classesNames = std::vector(classes_names_, std::end(classes_names_)); } - else - { + else{ FatalError("Number of classes not supported for mobilenet"); } - - } cv::Mat MobilenetDetection::draw() { tk::dnn::box b; - for (size_t i = 0; i < detected.size(); i++) - { + for (size_t i = 0; i < detected.size(); i++){ b = detected[i]; std::string det_class = classesNames[b.cl]; cv::rectangle(origImg, cv::Point(b.x, b.y), cv::Point(b.w, b.h), colors[b.cl], 2); @@ -326,22 +280,20 @@ cv::Mat MobilenetDetection::draw() return origImg; } - void MobilenetDetection::preprocess(const bool gpu) { std::cout<<"preprocess"<input_dim.w, netRT->input_dim.h)); - // resize(origImg, frame_resize, cv::Size(netRT->input_dim.w, netRT->input_dim.h)); + //resize image, remove mean, divide by std + cv::cuda::resize (im_Orig, frame_resize, cv::Size(netRT->input_dim.w, netRT->input_dim.h)); frame_resize.convertTo(frame_nomean, CV_32FC3, 1, -127); frame_nomean.convertTo(frame_scaled, CV_32FC3, 1 / 128.0, 0); - //copy image into tensor and copy it into GPU + //copy image into tensors cv::cuda::GpuMat bgr[3]; cv::cuda::split(frame_scaled, bgr); @@ -366,8 +318,6 @@ void MobilenetDetection::preprocess(const bool gpu) } checkCuda(cudaMemcpyAsync(input_d, input, netRT->input_dim.tot() * sizeof(dnnType), cudaMemcpyHostToDevice, netRT->stream)); } - - } void MobilenetDetection::update(cv::Mat &img) @@ -393,16 +343,16 @@ void MobilenetDetection::update(cv::Mat &img) dim2.print(); } - //get confidences and locations + //get confidences and locations_h conf = (dnnType *)netRT->buffersRT[3]; loc = (dnnType *)netRT->buffersRT[4]; - checkCuda(cudaMemcpy(confidences_h, conf, n_priors * classes * sizeof(float), cudaMemcpyDeviceToHost)); - checkCuda(cudaMemcpy(locations_h, loc, N_COORDS * n_priors * sizeof(float), cudaMemcpyDeviceToHost)); + checkCuda(cudaMemcpy(confidences_h, conf, nPriors * classes * sizeof(float), cudaMemcpyDeviceToHost)); + checkCuda(cudaMemcpy(locations_h, loc, N_COORDS * nPriors * sizeof(float), cudaMemcpyDeviceToHost)); //postprocess - convert_locatios_to_boxes_and_center(priors, n_priors, locations_h, centerVariance, sizeVariance); - detected = postprocess(locations_h, confidences_h, n_priors, confThresh, classes, iouThreshold, sz.width, sz.height); + convert_locatios_to_boxes_and_center(); + detected = postprocess(sz.width, sz.height); TIMER_STOP stats.push_back(t_ns);