diff --git a/include/tkDNN/CenternetDetection.h b/include/tkDNN/CenternetDetection.h index a2a656f..e4bb358 100644 --- a/include/tkDNN/CenternetDetection.h +++ b/include/tkDNN/CenternetDetection.h @@ -35,9 +35,7 @@ class CenternetDetection { int ndets = 0; // tk::dnn::Yolo::detection *dets = nullptr; - cv::Mat imageF; - cv::cuda::GpuMat imageF1_d, imageF2_d; - cv::cuda::GpuMat bgr[3]; + cv::Mat imageOrig; // std::vector< cv::cuda::GpuMat > bgr; // variable to test cnet on dog pictures @@ -71,8 +69,16 @@ class CenternetDetection { float *target_coords; - float *mean_d; - float *stddev_d; + + + #ifdef OPENCV_CUDA + float *mean_d; + float *stddev_d; + #else + cv::Vec mean; + cv::Vec stddev; + dnnType *input; + #endif float *d_ptrs; @@ -88,6 +94,8 @@ class CenternetDetection { // pointer used in the kernels float *src_out; int *ids_out; + + void preprocess(); public: dnnType *rt_out[4]; @@ -97,7 +105,7 @@ class CenternetDetection { int classes = 80; int num = 0; int n_masks = 0; - float thresh = 0.0; + float thresh = 0.3; cv::Scalar colors[256]; // this is filled with results diff --git a/include/tkDNN/MobilenetDetection.h b/include/tkDNN/MobilenetDetection.h index 09bdac8..f4bec95 100644 --- a/include/tkDNN/MobilenetDetection.h +++ b/include/tkDNN/MobilenetDetection.h @@ -83,7 +83,7 @@ private: void generate_ssd_priors(const SSDSpec *specs, const int n_specs, bool clamp = true); 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); + void preprocess(); std::vector postprocess(const int width, const int height); float get_color2(int c, int x, int max); diff --git a/include/tkDNN/utils.h b/include/tkDNN/utils.h index 5da6e81..1a6df95 100644 --- a/include/tkDNN/utils.h +++ b/include/tkDNN/utils.h @@ -14,6 +14,8 @@ #define dnnType float +#define OPENCV_CUDA + // Colored output #define COL_END "\033[0m" diff --git a/src/CenternetDetection.cpp b/src/CenternetDetection.cpp index 4560015..b1e8ff9 100644 --- a/src/CenternetDetection.cpp +++ b/src/CenternetDetection.cpp @@ -103,6 +103,8 @@ bool CenternetDetection::init(std::string tensor_path) { checkCuda( cudaMallocHost(&target_coords, 4 * K *sizeof(float)) ); +#ifdef OPENCV_CUDA + checkCuda( cudaMalloc(&mean_d, 3 * sizeof(float)) ); checkCuda( cudaMalloc(&stddev_d, 3 * sizeof(float)) ); float mean[3] = {0.408, 0.447, 0.47}; @@ -110,6 +112,11 @@ bool CenternetDetection::init(std::string tensor_path) { checkCuda(cudaMemcpy(mean_d, mean, 3*sizeof(float), cudaMemcpyHostToDevice)); checkCuda(cudaMemcpy(stddev_d, stddev, 3*sizeof(float), cudaMemcpyHostToDevice)); +#else + checkCuda(cudaMallocHost(&input, sizeof(dnnType)*netRT->input_dim.tot())); + mean << 0.408, 0.447, 0.47; + stddev << 0.289, 0.274, 0.278; +#endif checkCuda( cudaMalloc(&d_ptrs, dim.c * dim.h*dim.w * sizeof(float)) ); // mean << 0.408, 0.447, 0.47; @@ -130,7 +137,7 @@ bool CenternetDetection::init(std::string tensor_path) { } -cv::Mat CenternetDetection::draw(cv::Mat &imageORIG) { +cv::Mat CenternetDetection::draw(cv::Mat &imageOrig) { tk::dnn::box b; int x0, w, x1, y0, h, y1; @@ -158,33 +165,30 @@ cv::Mat CenternetDetection::draw(cv::Mat &imageORIG) { y1 = b.y + h; objClass = b.cl; det_class = coco_class_name[objClass]; - cv::rectangle(imageORIG, cv::Point(x0, y0), cv::Point(x1, y1), colors[objClass], 2); + cv::rectangle(imageOrig, cv::Point(x0, y0), cv::Point(x1, y1), colors[objClass], 2); // draw label cv::Size textSize = getTextSize(det_class, cv::FONT_HERSHEY_SIMPLEX, fontScale, thickness, &baseline); - cv::rectangle(imageORIG, cv::Point(x0, y0), cv::Point((x0 + textSize.width - 2), (y0 - textSize.height - 2)), colors[b.cl], -1); - cv::putText(imageORIG, det_class, cv::Point(x0, (y0 - (baseline / 2))), cv::FONT_HERSHEY_SIMPLEX, fontScale, cv::Scalar(255, 255, 255), thickness); + cv::rectangle(imageOrig, cv::Point(x0, y0), cv::Point((x0 + textSize.width - 2), (y0 - textSize.height - 2)), colors[b.cl], -1); + cv::putText(imageOrig, det_class, cv::Point(x0, (y0 - (baseline / 2))), cv::FONT_HERSHEY_SIMPLEX, fontScale, cv::Scalar(255, 255, 255), thickness); } - return imageORIG; + return imageOrig; // cv::namedWindow("cnet", cv::WINDOW_NORMAL); // cv::imshow("cnet", imageOrig); // cv::waitKey(10000); } -void CenternetDetection::update(cv::Mat &imageORIG) { - - if(!imageORIG.data) { - std::cout<<"CENTERNET: NO IMAGE DATA\n"; - return; - } - TIMER_START +void CenternetDetection::preprocess() +{ + auto start_t = std::chrono::steady_clock::now(); auto step_t = std::chrono::steady_clock::now(); auto end_t = std::chrono::steady_clock::now(); - // -----------------------------------pre-process ------------------------------------------ + + // -----------------------------------pre-process ------------------------------------------ // it will resize the images to `224 x 224` in GETTING_STARTED.md - cv::Size sz = imageORIG.size(); + cv::Size sz = imageOrig.size(); std::cout<<"image: "<(end_t - step_t).count() << " us" << std::endl; step_t = end_t; - // cv::cuda::warpAffine(imageF1_d, imageF2_d, trans, cv::Size(inp_width, inp_height), cv::INTER_LINEAR ); + cv::cuda::warpAffine(imageF1_d, imageF2_d, trans, cv::Size(inp_width, inp_height), cv::INTER_LINEAR ); checkCuda( cudaDeviceSynchronize() ); - end_t = std::chrono::steady_clock::now(); - std::cout << " TIME warpAffine: " << std::chrono::duration_cast(end_t - step_t).count() << " us" << std::endl; - step_t = end_t; + imageF2_d.convertTo(imageF1_d, CV_32FC3, 1/255.0); checkCuda( cudaDeviceSynchronize() ); @@ -256,7 +261,8 @@ void CenternetDetection::update(cv::Mat &imageORIG) { step_t = end_t; dim2 = dim; - // cv::cuda::split(imageF1_d,bgr);//split source + cv::cuda::GpuMat bgr[3]; + cv::cuda::split(imageF1_d,bgr);//split source end_t = std::chrono::steady_clock::now(); std::cout << " TIME split: " << std::chrono::duration_cast(end_t - step_t).count() << " us" << std::endl; step_t = end_t; @@ -275,6 +281,69 @@ void CenternetDetection::update(cv::Mat &imageORIG) { end_t = std::chrono::steady_clock::now(); std::cout << " TIME Memcpy to input_d: " << std::chrono::duration_cast(end_t - step_t).count() << " us" << std::endl; step_t = end_t; +#else + + cv::Mat imageF; + resize(imageOrig, 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; + + cv::Mat trans = cv::getAffineTransform( src, dst ); + cv::warpAffine(imageF, imageF, trans, cv::Size(inp_width, inp_height), cv::INTER_LINEAR ); + end_t = std::chrono::steady_clock::now(); + std::cout << " TIME warpAffine: " << std::chrono::duration_cast(end_t - step_t).count() << " us" << std::endl; + step_t = end_t; + + sz = imageF.size(); + std::cout<<"size: "<(end_t - step_t).count() << " us" << std::endl; + step_t = end_t; + + dim2 = dim; + + //split channels + cv::Mat bgr[3]; + cv::split(imageF,bgr);//split source + + for(int i=0; i<3; i++){ + bgr[i] = bgr[i] - mean[i]; + bgr[i] = bgr[i] / stddev[i]; + } + + //write channels + for(int i=0; i