Enabled cuda pre-process via opencv_contrib

This commit is contained in:
Harijs Grinbergs
2021-11-04 20:39:37 +02:00
parent 18303c06c5
commit e449d344c0
3 changed files with 49 additions and 49 deletions
+7 -7
View File
@@ -18,7 +18,7 @@ if(DEBUG)
endif() endif()
if(TKDNN_PATH) if(TKDNN_PATH)
message("SET TKDNN_PATH:"${TKDNN_PATH}) message("SET TKDNN_PATH:${TKDNN_PATH}")
add_definitions(-DTKDNN_PATH="${TKDNN_PATH}") add_definitions(-DTKDNN_PATH="${TKDNN_PATH}")
else() else()
add_definitions(-DTKDNN_PATH="${CMAKE_CURRENT_SOURCE_DIR}") add_definitions(-DTKDNN_PATH="${CMAKE_CURRENT_SOURCE_DIR}")
@@ -51,11 +51,11 @@ find_package(Eigen3 REQUIRED)
message("Eigen DIR: " ${EIGEN3_INCLUDE_DIR}) message("Eigen DIR: " ${EIGEN3_INCLUDE_DIR})
include_directories(${EIGEN3_INCLUDE_DIR}) include_directories(${EIGEN3_INCLUDE_DIR})
find_package(OpenCV REQUIRED) find_package(OpenCV 4.5 REQUIRED)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DOPENCV") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DOPENCV")
# if(OpenCV_CUDA_VERSION) if(OpenCV_CUDA_VERSION)
# add_compile_definitions(OPENCV_CUDACONTRIB) add_compile_definitions(OPENCV_CUDACONTRIB)
# endif() endif()
# gives problems in cross-compiling, probably malformed cmake config # gives problems in cross-compiling, probably malformed cmake config
find_package(yaml-cpp REQUIRED) find_package(yaml-cpp REQUIRED)
@@ -71,7 +71,7 @@ include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include ${CUDA_INCLUDE_DIRS} ${O
add_library(tkDNN SHARED ${tkdnn_SRC}) add_library(tkDNN SHARED ${tkdnn_SRC})
target_link_libraries(tkDNN ${tkdnn_LIBS}) target_link_libraries(tkDNN ${tkdnn_LIBS})
#static #static
#add_library(tkDNN_static STATIC ${tkdnn_SRC}) #add_library(tkDNN_static STATIC ${tkdnn_SRC})
#target_link_libraries(tkDNN_static ${tkdnn_LIBS}) #target_link_libraries(tkDNN_static ${tkdnn_LIBS})
@@ -163,7 +163,7 @@ target_link_libraries(seg_demo tkDNN)
# Install # Install
#------------------------------------------------------------------------------- #-------------------------------------------------------------------------------
#if (CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT) #if (CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)
# set (CMAKE_INSTALL_PREFIX "${CMAKE_BINARY_DIR}/install" # set (CMAKE_INSTALL_PREFIX "${CMAKE_BINARY_DIR}/install"
# CACHE PATH "default install path" FORCE) # CACHE PATH "default install path" FORCE)
#endif() #endif()
message("install dir:" ${CMAKE_INSTALL_PREFIX}) message("install dir:" ${CMAKE_INSTALL_PREFIX})
+22 -22
View File
@@ -3,10 +3,10 @@
#include <iostream> #include <iostream>
#include <signal.h> #include <signal.h>
#include <stdlib.h> #include <stdlib.h>
#ifdef __linux__ #ifdef __linux__
#include <unistd.h> #include <unistd.h>
#endif #endif
#include <mutex> #include <mutex>
#include "utils.h" #include "utils.h"
@@ -17,7 +17,7 @@
#include "tkdnn.h" #include "tkdnn.h"
//#define OPENCV_CUDACONTRIB //if OPENCV has been compiled with CUDA and contrib. #define OPENCV_CUDACONTRIB //if OPENCV has been compiled with CUDA and contrib.
#ifdef OPENCV_CUDACONTRIB #ifdef OPENCV_CUDACONTRIB
#include <opencv2/cudawarping.hpp> #include <opencv2/cudawarping.hpp>
@@ -37,7 +37,7 @@ class DetectionNN {
cv::Scalar colors[256]; cv::Scalar colors[256];
int nBatches = 1; int nBatches = 2;
#ifdef OPENCV_CUDACONTRIB #ifdef OPENCV_CUDACONTRIB
cv::cuda::GpuMat bgr[3]; cv::cuda::GpuMat bgr[3];
@@ -57,11 +57,11 @@ class DetectionNN {
virtual void preprocess(cv::Mat &frame, const int bi=0) = 0; virtual void preprocess(cv::Mat &frame, const int bi=0) = 0;
/** /**
* This method postprocess the output of the NN to obtain the correct * This method postprocess the output of the NN to obtain the correct
* boundig boxes. * boundig boxes.
* *
* @param bi batch index * @param bi batch index
* @param mAP set to true only if all the probabilities for a bounding * @param mAP set to true only if all the probabilities for a bounding
* box are needed, as in some cases for the mAP calculation * box are needed, as in some cases for the mAP calculation
*/ */
virtual void postprocess(const int bi=0,const bool mAP=false) = 0; virtual void postprocess(const int bi=0,const bool mAP=false) = 0;
@@ -79,25 +79,25 @@ class DetectionNN {
~DetectionNN(){}; ~DetectionNN(){};
/** /**
* Method used to initialize the class, allocate memory and compute * Method used to initialize the class, allocate memory and compute
* needed data. * needed data.
* *
* @param tensor_path path to the rt file of the NN. * @param tensor_path path to the rt file of the NN.
* @param n_classes number of classes for the given dataset. * @param n_classes number of classes for the given dataset.
* @param n_batches maximum number of batches to use in inference * @param n_batches maximum number of batches to use in inference
* @return true if everything is correct, false otherwise. * @return true if everything is correct, false otherwise.
*/ */
virtual bool init(const std::string& tensor_path, const int n_classes=80, const int n_batches=1, const float conf_thresh=0.3) = 0; virtual bool init(const std::string& tensor_path, const int n_classes=80, const int n_batches=1, const float conf_thresh=0.3) = 0;
/** /**
* This method performs the whole detection of the NN. * This method performs the whole detection of the NN.
* *
* @param frames frames to run detection on. * @param frames frames to run detection on.
* @param cur_batches number of batches to use in inference * @param cur_batches number of batches to use in inference
* @param save_times if set to true, preprocess, inference and postprocess times * @param save_times if set to true, preprocess, inference and postprocess times
* are saved on a csv file, otherwise not. * are saved on a csv file, otherwise not.
* @param times pointer to the output stream where to write times * @param times pointer to the output stream where to write times
* @param mAP set to true only if all the probabilities for a bounding * @param mAP set to true only if all the probabilities for a bounding
* box are needed, as in some cases for the mAP calculation * box are needed, as in some cases for the mAP calculation
*/ */
void update(std::vector<cv::Mat>& frames, const int cur_batches=1, bool save_times=false, std::ofstream *times=nullptr, const bool mAP=false){ void update(std::vector<cv::Mat>& frames, const int cur_batches=1, bool save_times=false, std::ofstream *times=nullptr, const bool mAP=false){
@@ -107,14 +107,14 @@ class DetectionNN {
FatalError("A batch size greater than nBatches cannot be used"); FatalError("A batch size greater than nBatches cannot be used");
originalSize.clear(); originalSize.clear();
if(TKDNN_VERBOSE) printCenteredTitle(" TENSORRT detection ", '=', 30); if(TKDNN_VERBOSE) printCenteredTitle(" TENSORRT detection ", '=', 30);
{ {
TKDNN_TSTART TKDNN_TSTART
for(int bi=0; bi<cur_batches;++bi){ for(int bi=0; bi<cur_batches;++bi){
if(!frames[bi].data) if(!frames[bi].data)
FatalError("No image data feed to detection"); FatalError("No image data feed to detection");
originalSize.push_back(frames[bi].size()); originalSize.push_back(frames[bi].size());
preprocess(frames[bi], bi); preprocess(frames[bi], bi);
} }
TKDNN_TSTOP TKDNN_TSTOP
if(save_times) *times<<t_ns<<";"; if(save_times) *times<<t_ns<<";";
@@ -141,11 +141,11 @@ class DetectionNN {
TKDNN_TSTOP TKDNN_TSTOP
if(save_times) *times<<t_ns<<"\n"; if(save_times) *times<<t_ns<<"\n";
} }
} }
/** /**
* Method to draw bounding boxes and labels on a frame. * Method to draw bounding boxes and labels on a frame.
* *
* @param frames original frame to draw bounding box on. * @param frames original frame to draw bounding box on.
*/ */
void draw(std::vector<cv::Mat>& frames) { void draw(std::vector<cv::Mat>& frames) {
@@ -155,11 +155,11 @@ class DetectionNN {
std::string det_class; std::string det_class;
int baseline = 0; int baseline = 0;
float font_scale = 0.5; float font_scale = 0.5;
int thickness = 2; int thickness = 2;
for(int bi=0; bi<frames.size(); ++bi){ for(int bi=0; bi<frames.size(); ++bi){
// draw dets // draw dets
for(int i=0; i<batchDetected[bi].size(); i++) { for(int i=0; i<batchDetected[bi].size(); i++) {
b = batchDetected[bi][i]; b = batchDetected[bi][i];
x0 = b.x; x0 = b.x;
x1 = b.x + b.w; x1 = b.x + b.w;
@@ -168,11 +168,11 @@ class DetectionNN {
det_class = classesNames[b.cl]; det_class = classesNames[b.cl];
// draw rectangle // draw rectangle
cv::rectangle(frames[bi], cv::Point(x0, y0), cv::Point(x1, y1), colors[b.cl], 2); cv::rectangle(frames[bi], cv::Point(x0, y0), cv::Point(x1, y1), colors[b.cl], 2);
// draw label // draw label
cv::Size text_size = getTextSize(det_class, cv::FONT_HERSHEY_SIMPLEX, font_scale, thickness, &baseline); cv::Size text_size = getTextSize(det_class, cv::FONT_HERSHEY_SIMPLEX, font_scale, thickness, &baseline);
cv::rectangle(frames[bi], cv::Point(x0, y0), cv::Point((x0 + text_size.width - 2), (y0 - text_size.height - 2)), colors[b.cl], -1); cv::rectangle(frames[bi], cv::Point(x0, y0), cv::Point((x0 + text_size.width - 2), (y0 - text_size.height - 2)), colors[b.cl], -1);
cv::putText(frames[bi], det_class, cv::Point(x0, (y0 - (baseline / 2))), cv::FONT_HERSHEY_SIMPLEX, font_scale, cv::Scalar(255, 255, 255), thickness); cv::putText(frames[bi], det_class, cv::Point(x0, (y0 - (baseline / 2))), cv::FONT_HERSHEY_SIMPLEX, font_scale, cv::Scalar(255, 255, 255), thickness);
} }
} }
+20 -20
View File
@@ -4,7 +4,7 @@
#include <iostream> #include <iostream>
#include <signal.h> #include <signal.h>
#include <stdlib.h> #include <stdlib.h>
#ifdef __linux__ #ifdef __linux__
#include <unistd.h> #include <unistd.h>
#endif #endif
@@ -17,7 +17,7 @@
#include "tkdnn.h" #include "tkdnn.h"
// #define OPENCV_CUDACONTRIB //if OPENCV has been compiled with CUDA and contrib. #define OPENCV_CUDACONTRIB //if OPENCV has been compiled with CUDA and contrib.
#ifdef OPENCV_CUDACONTRIB #ifdef OPENCV_CUDACONTRIB
#include <opencv2/cudawarping.hpp> #include <opencv2/cudawarping.hpp>
@@ -57,11 +57,11 @@ class DetectionNN3D {
virtual void preprocess(cv::Mat &frame, const int bi=0) = 0; virtual void preprocess(cv::Mat &frame, const int bi=0) = 0;
/** /**
* This method postprocess the output of the NN to obtain the correct * This method postprocess the output of the NN to obtain the correct
* boundig boxes. * boundig boxes.
* *
* @param bi batch index * @param bi batch index
* @param mAP set to true only if all the probabilities for a bounding * @param mAP set to true only if all the probabilities for a bounding
* box are needed, as in some cases for the mAP calculation * box are needed, as in some cases for the mAP calculation
*/ */
virtual void postprocess(const int bi=0,const bool mAP=false) = 0; virtual void postprocess(const int bi=0,const bool mAP=false) = 0;
@@ -69,7 +69,7 @@ class DetectionNN3D {
public: public:
int classes = 0; int classes = 0;
float confThreshold = 0.3; /*threshold on the confidence of the boxes*/ float confThreshold = 0.3; /*threshold on the confidence of the boxes*/
std::vector<tk::dnn::box3D> detected3D; /*bounding boxes in output*/ std::vector<tk::dnn::box3D> detected3D; /*bounding boxes in output*/
std::vector<std::vector<tk::dnn::box3D>> batchDetected; /*bounding boxes in output*/ std::vector<std::vector<tk::dnn::box3D>> batchDetected; /*bounding boxes in output*/
std::vector<double> pre_stats, stats, post_stats, visual_stats; /*keeps track of inference times (ms)*/ std::vector<double> pre_stats, stats, post_stats, visual_stats; /*keeps track of inference times (ms)*/
@@ -79,29 +79,29 @@ class DetectionNN3D {
~DetectionNN3D(){}; ~DetectionNN3D(){};
/** /**
* Method used to initialize the class, allocate memory and compute * Method used to initialize the class, allocate memory and compute
* needed data. * needed data.
* *
* @param tensor_path path to the rt file of the NN. * @param tensor_path path to the rt file of the NN.
* @param n_classes number of classes for the given dataset. * @param n_classes number of classes for the given dataset.
* @param n_batches maximum number of batches to use in inference. * @param n_batches maximum number of batches to use in inference.
* @return true if everything is correct, false otherwise. * @return true if everything is correct, false otherwise.
*/ */
virtual bool init(const std::string& tensor_path, const int n_classes=3, const int n_batches=1, virtual bool init(const std::string& tensor_path, const int n_classes=3, const int n_batches=1,
const float conf_thresh=0.3, const std::vector<cv::Mat>& k_calibs=std::vector<cv::Mat>()) = 0; const float conf_thresh=0.3, const std::vector<cv::Mat>& k_calibs=std::vector<cv::Mat>()) = 0;
/** /**
* This method performs the whole detection of the NN. * This method performs the whole detection of the NN.
* *
* @param frames frames to run detection on. * @param frames frames to run detection on.
* @param cur_batches number of batches to use in inference. * @param cur_batches number of batches to use in inference.
* @param save_times if set to true, preprocess, inference and postprocess times * @param save_times if set to true, preprocess, inference and postprocess times
* are saved on a csv file, otherwise not. * are saved on a csv file, otherwise not.
* @param times pointer to the output stream where to write times. * @param times pointer to the output stream where to write times.
* @param mAP set to true only if all the probabilities for a bounding * @param mAP set to true only if all the probabilities for a bounding
* box are needed, as in some cases for the mAP calculation. * box are needed, as in some cases for the mAP calculation.
*/ */
void update(std::vector<cv::Mat>& frames, const int cur_batches=1, bool save_times=false, void update(std::vector<cv::Mat>& frames, const int cur_batches=1, bool save_times=false,
std::ofstream *times=nullptr, const bool mAP=false){ std::ofstream *times=nullptr, const bool mAP=false){
if(save_times && times==nullptr) if(save_times && times==nullptr)
FatalError("save_times set to true, but no valid ofstream given"); FatalError("save_times set to true, but no valid ofstream given");
@@ -109,17 +109,17 @@ class DetectionNN3D {
FatalError("A batch size greater than nBatches cannot be used"); FatalError("A batch size greater than nBatches cannot be used");
originalSize.clear(); originalSize.clear();
if(TKDNN_VERBOSE) printCenteredTitle(" TENSORRT detection ", '=', 30); if(TKDNN_VERBOSE) printCenteredTitle(" TENSORRT detection ", '=', 30);
{ {
TKDNN_TSTART TKDNN_TSTART
for(int bi=0; bi<cur_batches;++bi){ for(int bi=0; bi<cur_batches;++bi){
if(!frames[bi].data) if(!frames[bi].data)
FatalError("No image data feed to detection"); FatalError("No image data feed to detection");
originalSize.push_back(frames[bi].size()); originalSize.push_back(frames[bi].size());
preprocess(frames[bi], bi); preprocess(frames[bi], bi);
} }
TKDNN_TSTOP TKDNN_TSTOP
pre_stats.push_back(t_ns); pre_stats.push_back(t_ns);
if(save_times) *times<<t_ns<<";"; if(save_times) *times<<t_ns<<";";
} }
@@ -149,11 +149,11 @@ class DetectionNN3D {
/** /**
* Method to draw bounding boxes and labels on a frame. * Method to draw bounding boxes and labels on a frame.
* *
* @param frames original frame to draw bounding box on. * @param frames original frame to draw bounding box on.
*/ */
virtual void draw(std::vector<cv::Mat>& frames){}; virtual void draw(std::vector<cv::Mat>& frames){};
}; };
}} }}