From 2ffe07057e550d2fd48588e72099ac8ecaa06ae3 Mon Sep 17 00:00:00 2001 From: perseusdg Date: Sun, 29 Aug 2021 03:18:58 +0530 Subject: [PATCH] Mnist works at the moment with trt8,others like yolo4tiny and mobilenet generate the engine files but crash after throwing nvifer1::CudaRuntimeError and when demo is being run ,it doesnt deserialize properly and crashes --- CMakeLists.txt | 3 +- demo/demo/demo.cpp | 49 +++++++---- include/tkDNN/DarknetParser.h | 3 + include/tkDNN/DetectionNN.h | 2 +- include/tkDNN/NetworkRT.h | 2 +- include/tkDNN/Yolo3Detection.h | 7 +- include/tkDNN/pluginsRT/ReorgRT.h | 2 +- include/tkDNN/pluginsRT/YoloRT.h | 1 + src/DarknetParser.cpp | 141 +++++++++++++++++++++++++++++- src/NetworkRT.cpp | 34 +++++++ src/Yolo3Detection.cpp | 51 +++++++---- tests/mnist/test_mnistRT.cpp | 2 +- 12 files changed, 255 insertions(+), 42 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 3e823dc..d88407e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -33,12 +33,13 @@ SET(CUDA_SEPARABLE_COMPILATION ON) #set(CUDA_NVCC_FLAGS "${CUDA_NVCC_FLAGS} -arch=sm_30 --compiler-options '-fPIC'") set(CUDA_NVCC_FLAGS ${CUDA_NVCC_FLAGS} --maxrregcount=32) + find_package(CUDNN REQUIRED) include_directories(${CUDNN_INCLUDE_DIR}) # compile -file(GLOB tkdnn_CUSRC "src/kernels/*.cu" "src/sorting.cu") +file(GLOB tkdnn_CUSRC "src/kernels/*.cu" "src/sorting.cu" ) cuda_include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include ${CUDA_INCLUDE_DIRS} ${CUDNN_INCLUDE_DIRS}) cuda_add_library(kernels SHARED ${tkdnn_CUSRC}) target_link_libraries(kernels ${CUDA_CUBLAS_LIBRARIES}) diff --git a/demo/demo/demo.cpp b/demo/demo/demo.cpp index 317a574..6e1c2b4 100644 --- a/demo/demo/demo.cpp +++ b/demo/demo/demo.cpp @@ -23,6 +23,18 @@ int main(int argc, char *argv[]) { std::string net = "yolo4tiny_fp32.rt"; + #ifdef __linux__ + std::string cfgPath = "../tests/darknet/cfg/yolo4tiny.cfg"; + #elif _WIN32 + std::string cfgPath = "..\\tests\\darknet\\cfg\\yolo4tiny.cfg"; + #endif + + #ifdef __linux__ + std::string namePath = "../tests/darknet/names/coco.names"; + #elif _WIN32 + std::string namePath = "..\\tests\\darknet\\names\\coco.names"; + #endif + if(argc > 1) net = argv[1]; #ifdef __linux__ @@ -31,23 +43,28 @@ int main(int argc, char *argv[]) { std::string input = "..\\..\\..\\demo\\yolo_test.mp4"; #endif + if(argc > 2) - input = argv[2]; - char ntype = 'y'; + cfgPath = argv[2]; if(argc > 3) - ntype = argv[3][0]; - int n_classes = 80; + namePath = argv[3]; if(argc > 4) - n_classes = atoi(argv[4]); - int n_batch = 1; + input = argv[4]; + char ntype = 'y'; if(argc > 5) - n_batch = atoi(argv[5]); - bool show = true; + ntype = argv[5][0]; + int n_classes = 80; if(argc > 6) - show = atoi(argv[6]); - float conf_thresh=0.3; + n_classes = atoi(argv[6]); + int n_batch = 1; if(argc > 7) - conf_thresh = atof(argv[7]); + n_batch = atoi(argv[7]); + bool show = true; + if(argc > 8) + show = atoi(argv[8]); + float conf_thresh=0.3; + if(argc > 9) + conf_thresh = atof(argv[9]); if(n_batch < 1 || n_batch > 64) FatalError("Batch dim not supported"); @@ -56,8 +73,8 @@ int main(int argc, char *argv[]) { SAVE_RESULT = true; tk::dnn::Yolo3Detection yolo; - tk::dnn::CenternetDetection cnet; - tk::dnn::MobilenetDetection mbnet; + //tk::dnn::CenternetDetection cnet; + //tk::dnn::MobilenetDetection mbnet; tk::dnn::DetectionNN *detNN; @@ -67,17 +84,17 @@ int main(int argc, char *argv[]) { detNN = &yolo; break; case 'c': - detNN = &cnet; + //detNN = &cnet; break; case 'm': - detNN = &mbnet; + //detNN = &mbnet; n_classes++; break; default: FatalError("Network type not allowed (3rd parameter)\n"); } - detNN->init(net, n_classes, n_batch, conf_thresh); + detNN->init(net,cfgPath,namePath,n_classes,n_batch,conf_thresh); gRun = true; diff --git a/include/tkDNN/DarknetParser.h b/include/tkDNN/DarknetParser.h index 089c4d6..c6d2472 100644 --- a/include/tkDNN/DarknetParser.h +++ b/include/tkDNN/DarknetParser.h @@ -47,5 +47,8 @@ namespace tk { namespace dnn { std::vector &netLayers, const std::vector& names); std::vector darknetReadNames(const std::string& names_file); tk::dnn::Network* darknetParser(const std::string& cfg_file, const std::string& wgs_path, const std::string& names_file); + void loadYoloInfo(const std::string &cfg_file,int lineNo,std::vector &mask,std::vector &anchors,int &num,int &classes,float &nms_thresh,int &nms_kind,int &coords); + void loadYoloInitInfo(int &channels,int &width,int &height,const std::string &cfg_file); + std::vector noYolosLine(const std::string &cfg_file); }} diff --git a/include/tkDNN/DetectionNN.h b/include/tkDNN/DetectionNN.h index a8c81f7..3a757ef 100644 --- a/include/tkDNN/DetectionNN.h +++ b/include/tkDNN/DetectionNN.h @@ -87,7 +87,7 @@ class DetectionNN { * @param n_batches maximum number of batches to use in inference * @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 std::string& cfg_path,const std::string& name_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. diff --git a/include/tkDNN/NetworkRT.h b/include/tkDNN/NetworkRT.h index 53d8d91..4a0d5ff 100644 --- a/include/tkDNN/NetworkRT.h +++ b/include/tkDNN/NetworkRT.h @@ -7,6 +7,7 @@ #include "Layer.h" #include "NvInfer.h" #include +#include namespace tk { namespace dnn { @@ -52,7 +53,6 @@ public: - class NetworkRT { public: diff --git a/include/tkDNN/Yolo3Detection.h b/include/tkDNN/Yolo3Detection.h index 100a720..5a29d9c 100644 --- a/include/tkDNN/Yolo3Detection.h +++ b/include/tkDNN/Yolo3Detection.h @@ -4,9 +4,9 @@ #include "opencv2/opencv.hpp" #include "DetectionNN.h" +#include "DarknetParser.h" -namespace tk { namespace dnn { - +namespace tk { namespace dnn { class Yolo3Detection : public DetectionNN { private: @@ -19,12 +19,13 @@ private: tk::dnn::Yolo* getYoloLayer(int n=0); cv::Mat bgr_h; + std::vector noYolos; public: Yolo3Detection() {}; ~Yolo3Detection() {}; - bool init(const std::string& tensor_path, const int n_classes=80, const int n_batches=1, const float conf_thresh=0.3); + bool init(const std::string& tensor_path,const std::string& cfg_path,const std::string& name_path,const int n_classes=80, const int n_batches=1, const float conf_thresh=0.3); void preprocess(cv::Mat &frame, const int bi=0); void postprocess(const int bi=0,const bool mAP=false); }; diff --git a/include/tkDNN/pluginsRT/ReorgRT.h b/include/tkDNN/pluginsRT/ReorgRT.h index 01e5468..fa67d9c 100644 --- a/include/tkDNN/pluginsRT/ReorgRT.h +++ b/include/tkDNN/pluginsRT/ReorgRT.h @@ -41,7 +41,7 @@ public: virtual int enqueue(int batchSize, const void*const * inputs, void* const* outputs, void* workspace, cudaStream_t stream) NOEXCEPT override { - reorgForward((dnnType*)reinterpret_cast(inputs[0]), + reorgForward((dnnType*)reinterpret_cast(inputs[0]), reinterpret_cast(outputs[0]), batchSize, c, h, w, stride, stream); return 0; diff --git a/include/tkDNN/pluginsRT/YoloRT.h b/include/tkDNN/pluginsRT/YoloRT.h index 3511634..d40cd5c 100644 --- a/include/tkDNN/pluginsRT/YoloRT.h +++ b/include/tkDNN/pluginsRT/YoloRT.h @@ -31,6 +31,7 @@ public: classes = readBUF(buf); num = readBUF(buf); n_masks = readBUF(buf); + std::cout<(buf); nms_thresh = readBUF(buf); nms_kind = readBUF(buf); diff --git a/src/DarknetParser.cpp b/src/DarknetParser.cpp index 69b6b29..a370b91 100644 --- a/src/DarknetParser.cpp +++ b/src/DarknetParser.cpp @@ -32,6 +32,16 @@ namespace tk { namespace dnn { return values; } + std::vector fromStringToFloatVec(const std::string& line, const char delimiter){ + std::stringstream linestream(line); + std::string value; + std::vector values; + + while(getline(linestream,value,delimiter)) + values.push_back(std::stof(value)); + return values; + } + bool darknetParseFields(const std::string& line, darknetFields_t& fields){ std::string name,value; @@ -268,7 +278,134 @@ namespace tk { namespace dnn { } return net; } - - + std::vector noYolosLine(const std::string &cfg_file){ + std::ifstream if_cfg(cfg_file); + if(!if_cfg.is_open()) + FatalError("cloud not open cfg file: " + cfg_file); + std::string line; + std::vector lineNo; + int count = 0; + while(std::getline(if_cfg,line)){ + std::size_t found = line.find("#"); + if ( found != std::string::npos ) { + line = line.substr(0, found); + } + // skip empty lines + if(line.empty()) + continue; + if(line == "[yolo]"){ + lineNo.push_back(count); + + + } + count++; + } + return lineNo; + } + void loadYoloInfo(const std::string &cfg_file,int lineNo,std::vector &mask,std::vector &anchors,int &num,int &classes,float &nms_thresh,int &nms_kind,int &coords){ + std::vector maskTemp,anchorsTemp; + int classesTemp,numTemp,nmsKindTemp; + int new_coordsTemp=0; + float nmsThreshTemp=0.45; + + std::ifstream if_cfg(cfg_file); + if(!if_cfg.is_open()) + FatalError("cloud not open cfg file: " + cfg_file); + std::string line; + int count = 0; + while(std::getline(if_cfg,line)){ + std::string name,value; + std::size_t found = line.find("#"); + if ( found != std::string::npos ) { + line = line.substr(0, found); + } + // skip empty lines + if(line.empty()) + continue; + if(count > lineNo && count <=20){ + divideNameAndValue(line,name,value); + if(name == "mask "){ + maskTemp = fromStringToFloatVec(value,','); + } + if(name == "anchors "){ + anchorsTemp = fromStringToFloatVec(value,','); + } + if(name == "classes"){ + classesTemp = std::stoi(value); + } + if(name == "num"){ + numTemp = std::stoi(value); + } + if(name == "nms_kind"){ + if(value == "greedynms"){ + nmsKindTemp = 0; + }else if(value == "diounms"){ + nmsKindTemp=1; + } + else{ + std::cout<<"NMS NOT SUPPORTED DEFAULTING TO GREEDYNMS"< tk::dnn::ActivationLeakyRTPluginCreator::mPluginAttributes; +std::vector tk::dnn::ActivationReLUCeilingPluginCreator::mPluginAttributes; +std::vector tk::dnn::ActivationMishRTPluginCreator::mPluginAttributes; +std::vector tk::dnn::ActivationLogisticRTPluginCreator::mPluginAttributes; +std::vector tk::dnn::DeformableConvRTPluginCreator::mPluginAttributes; +std::vector tk::dnn::RegionRTPluginCreator::mPluginAttributes; +std::vector tk::dnn::ReorgRTPluginCreator::mPluginAttributes; +std::vector tk::dnn::UpsampleRTPluginCreator::mPluginAttributes; +std::vector tk::dnn::ShortcutRTPluginCreator::mPluginAttributes; +std::vector tk::dnn::ReshapeRTPluginCreator::mPluginAttributes; +std::vector tk::dnn::MaxPoolFixedSizeRTPluginCreator::mPluginAttributes; +std::vector tk::dnn::ResizeLayerRTPluginCreator::mPluginAttributes; +std::vector tk::dnn::YoloRTPluginCreator::mPluginAttributes; +std::vector tk::dnn::RouteRTPluginCreator::mPluginAttributes; +std::vector tk::dnn::FlattenConcatRTPluginCreator::mPluginAttributes; + // Logger for info/warning/errors class Logger : public ILogger { void log(Severity severity, const char* msg) NOEXCEPT override { diff --git a/src/Yolo3Detection.cpp b/src/Yolo3Detection.cpp index 9de35e2..26da756 100644 --- a/src/Yolo3Detection.cpp +++ b/src/Yolo3Detection.cpp @@ -3,7 +3,7 @@ namespace tk { namespace dnn { -bool Yolo3Detection::init(const std::string& tensor_path, const int n_classes, const int n_batches, const float conf_thresh) { + bool Yolo3Detection::init(const std::string& tensor_path,const std::string& cfg_path,const std::string& name_path,const int n_classes, const int n_batches, const float conf_thresh) { //convert network to tensorRT std::cout<<(tensor_path).c_str()<<"\n"; @@ -14,28 +14,42 @@ bool Yolo3Detection::init(const std::string& tensor_path, const int n_classes, c tk::dnn::dataDim_t idim = netRT->input_dim; idim.n = nBatches; + std::vector yolosLine = noYolosLine(cfg_path); + noYolos = yolosLine; + int channels,height,width; + loadYoloInitInfo(channels,width,height,cfg_path); - if(netRT->pluginFactory->n_yolos < 2 ) { + + + if(yolosLine.size() < 2 ) { FatalError("this is not yolo3"); } - for(int i=0; ipluginFactory->n_yolos; i++) { - YoloRT *yRT = netRT->pluginFactory->yolos[i]; - classes = yRT->classes; - num = yRT->num; - nMasks = yRT->n_masks; + for(int i=0; i maskTemp,anchorsTemp; + std::vector classNamesTemp; + int classes,nms_kind,coords,numTemp; + float nmsthresh; + loadYoloInfo(cfg_path,yolosLine[i],maskTemp,anchorsTemp,numTemp,classes,nmsthresh,nms_kind,coords); + classNamesTemp = darknetReadNames(name_path); + num = numTemp/maskTemp.size(); + nMasks = maskTemp.size(); + dnnType* maskTempF; + dnnType* biasTempF; + maskTempF = maskTemp.data(); + biasTempF = anchorsTemp.data(); // make a yolo layer to interpret predictions yolo[i] = new tk::dnn::Yolo(nullptr, classes, nMasks, ""); // yolo without input and bias yolo[i]->mask_h = new dnnType[nMasks]; yolo[i]->bias_h = new dnnType[num*nMasks*2]; - memcpy(yolo[i]->mask_h, yRT->mask, sizeof(dnnType)*nMasks); - memcpy(yolo[i]->bias_h, yRT->bias, sizeof(dnnType)*num*nMasks*2); - yolo[i]->input_dim = yolo[i]->output_dim = tk::dnn::dataDim_t(1, yRT->c, yRT->h, yRT->w); - yolo[i]->classesNames = yRT->classesNames; - yolo[i]->nms_thresh = yRT->nms_thresh; - yolo[i]->nsm_kind = (tk::dnn::Yolo::nmsKind_t) yRT->nms_kind; - yolo[i]->new_coords = yRT->new_coords; + memcpy(yolo[i]->mask_h, maskTempF, sizeof(dnnType)*nMasks); + memcpy(yolo[i]->bias_h, biasTempF, sizeof(dnnType)*num*nMasks*2); + yolo[i]->input_dim = yolo[i]->output_dim = tk::dnn::dataDim_t(1, channels, height, width); + yolo[i]->classesNames = classNamesTemp; + yolo[i]->nms_thresh = nmsthresh; + yolo[i]->nsm_kind = (tk::dnn::Yolo::nmsKind_t) nms_kind; + yolo[i]->new_coords = coords; } dets = tk::dnn::Yolo::allocateDetections(tk::dnn::Yolo::MAX_DETECTIONS, classes); @@ -94,10 +108,15 @@ void Yolo3Detection::preprocess(cv::Mat &frame, const int bi){ void Yolo3Detection::postprocess(const int bi, const bool mAP){ + + //get yolo outputs + if(noYolos.size() < 2){ + FatalError("YOLOS WRONG!!"); + } std::vector rt_out; //dnnType *rt_out[netRT->pluginFactory->n_yolos]; - for(int i=0; ipluginFactory->n_yolos; i++) + for(int i=0; ibuffersRT[i+1] + netRT->buffersDIM[i+1].tot()*bi); float x_ratio = float(originalSize[bi].width) / float(netRT->input_dim.w); @@ -105,7 +124,7 @@ void Yolo3Detection::postprocess(const int bi, const bool mAP){ // compute dets nDets = 0; - for(int i=0; ipluginFactory->n_yolos; i++) { + for(int i=0; idstData = rt_out[i]; yolo[i]->computeDetections(dets, nDets, netRT->input_dim.w, netRT->input_dim.h, confThreshold, yolo[i]->new_coords); } diff --git a/tests/mnist/test_mnistRT.cpp b/tests/mnist/test_mnistRT.cpp index fe25696..2f6b7c4 100644 --- a/tests/mnist/test_mnistRT.cpp +++ b/tests/mnist/test_mnistRT.cpp @@ -128,7 +128,7 @@ int main() { builder->setMaxBatchSize(1); config->setMaxWorkspaceSize(1 << 20); - auto engine = builder->buildCudaEngine(*network); + auto engine = builder->buildEngineWithConfig(*network,*config); // we don't need the network any more network->destroy();