Style fix, useless params removed

Signed-off-by: Micaela Verucchi <micaelaverucchi@gmail.com>
This commit is contained in:
Micaela Verucchi
2020-03-19 18:13:35 +01:00
parent cae0b84687
commit 5c6d6ae0af
2 changed files with 61 additions and 116 deletions
+5 -10
View File
@@ -7,10 +7,8 @@
#include <opencv2/highgui/highgui.hpp> #include <opencv2/highgui/highgui.hpp>
#include <opencv2/videoio.hpp> #include <opencv2/videoio.hpp>
#include <opencv2/imgproc/imgproc.hpp> #include <opencv2/imgproc/imgproc.hpp>
#include "opencv2/opencv.hpp" #include "opencv2/opencv.hpp"
#include "tkdnn.h" #include "tkdnn.h"
#define N_COORDS 4 #define N_COORDS 4
@@ -58,17 +56,16 @@ private:
tk::dnn::NetworkRT *netRT = nullptr; tk::dnn::NetworkRT *netRT = nullptr;
int classes; int classes;
float iouThreshold = 0.45; float IoUThreshold = 0.45;
float centerVariance = 0.1; float centerVariance = 0.1;
float sizeVariance = 0.2; float sizeVariance = 0.2;
float confThresh = 0.4; float confThreshold = 0.4;
int imageSize; int imageSize;
float *priors = nullptr; float *priors = nullptr;
int n_priors = 0; int nPriors = 0;
cv::Mat origImg; cv::Mat origImg;
float *input, *input_d; float *input, *input_d;
float *locations_h, *confidences_h; float *locations_h, *confidences_h;
@@ -78,18 +75,16 @@ private:
dnnType *conf; dnnType *conf;
dnnType *loc; dnnType *loc;
float __colors[6][3] = {{1, 0, 1}, {0, 0, 1}, {0, 1, 1}, {0, 1, 0}, {1, 1, 0}, {1, 0, 0}}; 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; int baseline = 0;
float fontScale = 0.5; float fontScale = 0.5;
int thickness = 2; int thickness = 2;
void generate_ssd_priors(const SSDSpec *specs, const int n_specs, bool clamp = true); 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); float iou(const tk::dnn::box &a, const tk::dnn::box &b);
void preprocess(const bool gpu = true); void preprocess(const bool gpu = true);
std::vector<tk::dnn::box> 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<tk::dnn::box> postprocess(const int width, const int height);
float get_color2(int c, int x, int max); float get_color2(int c, int x, int max);
cv::Scalar colors[256]; cv::Scalar colors[256];
+56 -106
View File
@@ -1,40 +1,31 @@
#include "MobilenetDetection.h" #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); return (a.prob > b.prob);
} }
namespace tk namespace tk{
{ namespace dnn{
namespace dnn
{
void MobilenetDetection::generate_ssd_priors(const SSDSpec *specs, const int n_specs, bool clamp) 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++) 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: "<<n_priors<<std::endl; priors = (float *)malloc(N_COORDS * nPriors * sizeof(float));
// std::cout<<"n priors: "<<n_specs<<std::endl;
priors = (float *)malloc(N_COORDS * n_priors * sizeof(float));
int i_prio = 0; int i_prio = 0;
float scale, x_center, y_center, h, w, size, ratio; float scale, x_center, y_center, h, w, size, ratio;
int min, max; int min, max;
for (int i = 0; i < n_specs; i++) for (int i = 0; i < n_specs; i++){
{
scale = (float)imageSize / (float)specs[i].shrinkage; scale = (float)imageSize / (float)specs[i].shrinkage;
min = specs[i].boxHeight > specs[i].boxWidth ? specs[i].boxWidth : specs[i].boxHeight; min = specs[i].boxHeight > specs[i].boxWidth ? specs[i].boxWidth : specs[i].boxHeight;
max = specs[i].boxHeight < 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 j = 0; j < specs[i].featureSize; j++){
{ for (int k = 0; k < specs[i].featureSize; k++){
for (int k = 0; k < specs[i].featureSize; k++)
{
//small sized square box //small sized square box
size = min; size = min;
x_center = (k + 0.5f) / scale; x_center = (k + 0.5f) / scale;
@@ -89,39 +80,30 @@ void MobilenetDetection::generate_ssd_priors(const SSDSpec *specs, const int n_s
} }
} }
if (clamp) if (clamp){
{ for (int i = 0; i < nPriors * N_COORDS; i++){
for (int i = 0; i < n_priors * N_COORDS; i++)
{
priors[i] = priors[i] > 1.0f ? 1.0f : priors[i]; priors[i] = priors[i] > 1.0f ? 1.0f : priors[i];
priors[i] = priors[i] < 0.0f ? 0.0f : priors[i]; priors[i] = priors[i] < 0.0f ? 0.0f : priors[i];
// std::cout<<priors[i]<<" ";
// if((i+1)%4 == 0)
// std::cout<< i/4 <<" " <<std::endl;
} }
} }
} }
void MobilenetDetection::convert_locatios_to_boxes_and_center(float *priors, const int n_priors, float *locations, const float centerVariance, const float sizeVariance) void MobilenetDetection::convert_locatios_to_boxes_and_center()
{ {
float cur_x, cur_y; float cur_x, cur_y;
for (int i = 0; i < n_priors; i++) for (int i = 0; i < nPriors; i++){
{ locations_h[i * N_COORDS + 0] = locations_h[i * N_COORDS + 0] * centerVariance * priors[i * N_COORDS + 2] + priors[i * N_COORDS + 0];
locations[i * N_COORDS + 0] = locations[i * N_COORDS + 0] * centerVariance * priors[i * N_COORDS + 2] + priors[i * N_COORDS + 0]; locations_h[i * N_COORDS + 1] = locations_h[i * N_COORDS + 1] * centerVariance * priors[i * N_COORDS + 3] + priors[i * N_COORDS + 1];
locations[i * N_COORDS + 1] = locations[i * N_COORDS + 1] * centerVariance * priors[i * N_COORDS + 3] + priors[i * N_COORDS + 1]; locations_h[i * N_COORDS + 2] = exp(locations_h[i * N_COORDS + 2] * sizeVariance) * priors[i * N_COORDS + 2];
locations[i * N_COORDS + 2] = exp(locations[i * N_COORDS + 2] * sizeVariance) * priors[i * N_COORDS + 2]; locations_h[i * N_COORDS + 3] = exp(locations_h[i * N_COORDS + 3] * sizeVariance) * priors[i * N_COORDS + 3];
locations[i * N_COORDS + 3] = exp(locations[i * N_COORDS + 3] * sizeVariance) * priors[i * N_COORDS + 3];
cur_x = locations[i * N_COORDS + 0]; cur_x = locations_h[i * N_COORDS + 0];
cur_y = locations[i * N_COORDS + 1]; cur_y = locations_h[i * N_COORDS + 1];
locations[i * N_COORDS + 0] = cur_x - locations[i * N_COORDS + 2] / 2; locations_h[i * N_COORDS + 0] = cur_x - locations_h[i * N_COORDS + 2] / 2;
locations[i * N_COORDS + 1] = cur_y - locations[i * N_COORDS + 3] / 2; locations_h[i * N_COORDS + 1] = cur_y - locations_h[i * N_COORDS + 3] / 2;
locations[i * N_COORDS + 2] = cur_x + locations[i * N_COORDS + 2] / 2; locations_h[i * N_COORDS + 2] = cur_x + locations_h[i * N_COORDS + 2] / 2;
locations[i * N_COORDS + 3] = cur_y + locations[i * N_COORDS + 3] / 2; locations_h[i * N_COORDS + 3] = cur_y + locations_h[i * N_COORDS + 3] / 2;
// std::cout<<locations[i*N_COORDS + 0]<<" "<<locations[i*N_COORDS + 1]<<" "<<locations[i*N_COORDS + 2]<<" "<<locations[i*N_COORDS + 3]<<" "<<std::endl;
} }
} }
@@ -135,8 +117,6 @@ float MobilenetDetection::iou(const tk::dnn::box &a, const tk::dnn::box &b)
float ao_w = min_w - max_x > 0 ? min_w - max_x : 0; float ao_w = min_w - max_x > 0 ? min_w - max_x : 0;
float ao_h = min_h - max_y > 0 ? min_h - max_y : 0; float ao_h = min_h - max_y > 0 ? min_h - max_y : 0;
// std::cout<<" ao w: "<<ao_w<<" ao h: "<<ao_h<<std::endl;
float area_overlap = ao_w * ao_h; float area_overlap = ao_w * ao_h;
float area_0_w = a.w - a.x > 0 ? a.w - a.x : 0; float area_0_w = a.w - a.x > 0 ? a.w - a.x : 0;
float area_0_h = a.h - a.y > 0 ? a.h - a.y : 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_0 = area_0_h * area_0_w;
float area_1 = area_1_h * area_1_w; float area_1 = area_1_h * area_1_w;
// std::cout<<" area_overlap : "<<area_overlap<<" area_0: "<<area_0<<" area_1: "<<area_1<<std::endl;
float iou = area_overlap / (area_0 + area_1 - area_overlap + 1e-5); float iou = area_overlap / (area_0 + area_1 - area_overlap + 1e-5);
return iou; return iou;
} }
std::vector<tk::dnn::box> 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<tk::dnn::box> MobilenetDetection::postprocess(const int width, const int height)
{ {
float *conf_per_class; float *conf_per_class;
std::vector<tk::dnn::box> detections; std::vector<tk::dnn::box> detections;
for (int i = 1; i < n_classes; i++) for (int i = 1; i < classes; i++){
{ conf_per_class = &confidences_h[i * nPriors];
conf_per_class = &confidences[i * n_values];
std::vector<tk::dnn::box> boxes; std::vector<tk::dnn::box> 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; tk::dnn::box b;
b.cl = i; b.cl = i;
b.prob = conf_per_class[j]; b.prob = conf_per_class[j];
b.x = locations[j * N_COORDS + 0]; b.x = locations_h[j * N_COORDS + 0];
b.y = locations[j * N_COORDS + 1]; b.y = locations_h[j * N_COORDS + 1];
b.w = locations[j * N_COORDS + 2]; b.w = locations_h[j * N_COORDS + 2];
b.h = locations[j * N_COORDS + 3]; b.h = locations_h[j * N_COORDS + 3];
boxes.push_back(b); boxes.push_back(b);
} }
} }
std::sort(boxes.begin(), boxes.end(), boxProbCmp); std::sort(boxes.begin(), boxes.end(), boxProbCmp);
// for(auto b:boxes)
// b.print();
std::vector<tk::dnn::box> remaining; std::vector<tk::dnn::box> remaining;
while (boxes.size() > 0) while (boxes.size() > 0){
{
remaining.clear(); remaining.clear();
tk::dnn::box b; tk::dnn::box b;
@@ -193,20 +166,14 @@ std::vector<tk::dnn::box> MobilenetDetection::postprocess(float *locations, floa
b.w = boxes[0].w * width; b.w = boxes[0].w * width;
b.h = boxes[0].h * height; b.h = boxes[0].h * height;
detections.push_back(b); detections.push_back(b);
for (size_t j = 1; j < boxes.size(); j++) for (size_t j = 1; j < boxes.size(); j++){
{ if (iou(boxes[0], boxes[j]) <= IoUThreshold){
if (iou(boxes[0], boxes[j]) <= iou_thresh)
{
remaining.push_back(boxes[j]); remaining.push_back(boxes[j]);
} }
} }
boxes = remaining; boxes = remaining;
} }
} }
// std::cout<<"picked"<<std::endl;
// for(auto b:detections)
// b.print();
return detections; return detections;
} }
@@ -217,7 +184,6 @@ float MobilenetDetection::get_color2(int c, int x, int max)
int j = ceil(ratio); int j = ceil(ratio);
ratio -= i; ratio -= i;
float r = (1 - ratio) * __colors[i % 6][c % 3] + ratio * __colors[j % 6][c % 3]; float r = (1 - ratio) * __colors[i % 6][c % 3] + ratio * __colors[j % 6][c % 3];
//printf("%f\n", r);
return r; return r;
} }
@@ -229,9 +195,7 @@ void MobilenetDetection::init(std::string tensor_path, int input_size, int n_cla
const int n_SSDSpec = 6; const int n_SSDSpec = 6;
SSDSpec specs[6]; SSDSpec specs[6];
if(input_size == 300){
if(input_size == 300)
{
specs[0].setAll(19, 16, 60, 105, 2, 3); specs[0].setAll(19, 16, 60, 105, 2, 3);
specs[1].setAll(10, 32, 105, 150, 2, 3); specs[1].setAll(10, 32, 105, 150, 2, 3);
specs[2].setAll(5, 64, 150, 195, 2, 3); specs[2].setAll(5, 64, 150, 195, 2, 3);
@@ -239,8 +203,7 @@ void MobilenetDetection::init(std::string tensor_path, int input_size, int n_cla
specs[4].setAll(2, 150, 240, 285, 2, 3); specs[4].setAll(2, 150, 240, 285, 2, 3);
specs[5].setAll(1, 300, 285, 330, 2, 3); specs[5].setAll(1, 300, 285, 330, 2, 3);
} }
else if(input_size == 512) else if(input_size == 512){
{
specs[0].setAll(32, 16, 60, 105, 2, 3); specs[0].setAll(32, 16, 60, 105, 2, 3);
specs[1].setAll(16, 32, 105, 150, 2, 3); specs[1].setAll(16, 32, 105, 150, 2, 3);
specs[2].setAll(8, 64, 150, 195, 2, 3); specs[2].setAll(8, 64, 150, 195, 2, 3);
@@ -248,11 +211,9 @@ void MobilenetDetection::init(std::string tensor_path, int input_size, int n_cla
specs[4].setAll(2, 150, 240, 285, 2, 3); specs[4].setAll(2, 150, 240, 285, 2, 3);
specs[5].setAll(1, 300, 285, 330, 2, 3); specs[5].setAll(1, 300, 285, 330, 2, 3);
} }
else else{
{
FatalError("Input size for mobilenet not supported"); FatalError("Input size for mobilenet not supported");
} }
generate_ssd_priors(specs, n_SSDSpec); generate_ssd_priors(specs, n_SSDSpec);
@@ -261,13 +222,12 @@ void MobilenetDetection::init(std::string tensor_path, int input_size, int n_cla
checkCuda(cudaMallocHost(&input, sizeof(dnnType) * netRT->input_dim.tot())); checkCuda(cudaMallocHost(&input, sizeof(dnnType) * netRT->input_dim.tot()));
checkCuda(cudaMalloc(&input_d, sizeof(dnnType) * netRT->input_dim.tot())); checkCuda(cudaMalloc(&input_d, sizeof(dnnType) * netRT->input_dim.tot()));
locations_h = (float *)malloc(N_COORDS * n_priors * sizeof(float)); locations_h = (float *)malloc(N_COORDS * nPriors * sizeof(float));
confidences_h = (float *)malloc(n_priors * classes * sizeof(float)); confidences_h = (float *)malloc(nPriors * classes * sizeof(float));
dim = tk::dnn::dataDim_t(1, 3, imageSize, imageSize, 1); 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; int offset = c * 123457 % classes;
float r = get_color2(2, offset, classes); float r = get_color2(2, offset, classes);
float g = get_color2(1, 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)); 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_[] = { const char *classes_names_[] = {
"BACKGROUND", "aeroplane", "bicycle", "bird", "boat", "bottle", "bus", "BACKGROUND", "aeroplane", "bicycle", "bird", "boat", "bottle", "bus",
"car", "cat", "chair", "cow", "diningtable", "dog", "horse", "motorbike", "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<std::string>(classes_names_, std::end(classes_names_)); classesNames = std::vector<std::string>(classes_names_, std::end(classes_names_));
} }
else if (classes == 81) else if (classes == 81){
{
const char *classes_names_[] = { const char *classes_names_[] = {
"BACKGROUND", "person" , "bicycle" , "car" , "motorbike" , "aeroplane" , "bus" , "BACKGROUND", "person" , "bicycle" , "car" , "motorbike" , "aeroplane" , "bus" ,
"train" , "truck" , "boat" , "traffic light" , "fire hydrant" , "stop sign" , "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<std::string>(classes_names_, std::end(classes_names_)); classesNames = std::vector<std::string>(classes_names_, std::end(classes_names_));
} }
else else{
{
FatalError("Number of classes not supported for mobilenet"); FatalError("Number of classes not supported for mobilenet");
} }
} }
cv::Mat MobilenetDetection::draw() cv::Mat MobilenetDetection::draw()
{ {
tk::dnn::box b; 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]; b = detected[i];
std::string det_class = classesNames[b.cl]; 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); 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; return origImg;
} }
void MobilenetDetection::preprocess(const bool gpu) void MobilenetDetection::preprocess(const bool gpu)
{ {
std::cout<<"preprocess"<<std::endl; std::cout<<"preprocess"<<std::endl;
if(gpu){ if(gpu){
cv::cuda::GpuMat im_Orig; //move original image on GPU
cv::cuda::GpuMat frame_resize, frame_nomean, frame_scaled; cv::cuda::GpuMat im_Orig, frame_resize, frame_nomean, frame_scaled;
im_Orig = cv::cuda::GpuMat(origImg); im_Orig = cv::cuda::GpuMat(origImg);
cv::cuda::resize (im_Orig, frame_resize, cv::Size(netRT->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_resize.convertTo(frame_nomean, CV_32FC3, 1, -127);
frame_nomean.convertTo(frame_scaled, CV_32FC3, 1 / 128.0, 0); 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::GpuMat bgr[3];
cv::cuda::split(frame_scaled, bgr); 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)); checkCuda(cudaMemcpyAsync(input_d, input, netRT->input_dim.tot() * sizeof(dnnType), cudaMemcpyHostToDevice, netRT->stream));
} }
} }
void MobilenetDetection::update(cv::Mat &img) void MobilenetDetection::update(cv::Mat &img)
@@ -393,16 +343,16 @@ void MobilenetDetection::update(cv::Mat &img)
dim2.print(); dim2.print();
} }
//get confidences and locations //get confidences and locations_h
conf = (dnnType *)netRT->buffersRT[3]; conf = (dnnType *)netRT->buffersRT[3];
loc = (dnnType *)netRT->buffersRT[4]; loc = (dnnType *)netRT->buffersRT[4];
checkCuda(cudaMemcpy(confidences_h, conf, n_priors * classes * sizeof(float), cudaMemcpyDeviceToHost)); checkCuda(cudaMemcpy(confidences_h, conf, nPriors * classes * sizeof(float), cudaMemcpyDeviceToHost));
checkCuda(cudaMemcpy(locations_h, loc, N_COORDS * n_priors * sizeof(float), cudaMemcpyDeviceToHost)); checkCuda(cudaMemcpy(locations_h, loc, N_COORDS * nPriors * sizeof(float), cudaMemcpyDeviceToHost));
//postprocess //postprocess
convert_locatios_to_boxes_and_center(priors, n_priors, locations_h, centerVariance, sizeVariance); convert_locatios_to_boxes_and_center();
detected = postprocess(locations_h, confidences_h, n_priors, confThresh, classes, iouThreshold, sz.width, sz.height); detected = postprocess(sz.width, sz.height);
TIMER_STOP TIMER_STOP
stats.push_back(t_ns); stats.push_back(t_ns);