From f06151c7a0a39602b7c226af687bbe4d048a38b0 Mon Sep 17 00:00:00 2001 From: jario Date: Fri, 12 Jan 2024 21:58:08 +0800 Subject: [PATCH] finish model manager v1 --- .../common_det/cuda/common_det_cuda_impl.cpp | 33 +++++++++- .../cuda/landing_det_cuda_impl.cpp | 11 ++++ algorithm/sot/ocv470/sot_ocv470_impl.cpp | 31 +++++++++ algorithm/veri/cuda/veri_det_cuda_impl.cpp | 11 ++++ utils/gason.cpp | 2 +- utils/sv_util.cpp | 65 +++++++++++++++++++ utils/sv_util.h | 3 + 7 files changed, 153 insertions(+), 3 deletions(-) diff --git a/algorithm/common_det/cuda/common_det_cuda_impl.cpp b/algorithm/common_det/cuda/common_det_cuda_impl.cpp index 43dcb7b..5f3d7a1 100644 --- a/algorithm/common_det/cuda/common_det_cuda_impl.cpp +++ b/algorithm/common_det/cuda/common_det_cuda_impl.cpp @@ -1,6 +1,7 @@ #include "common_det_cuda_impl.h" #include #include +#include "sv_util.h" #define SV_MODEL_DIR "/SpireCV/models/" #define SV_ROOT_DIR "/SpireCV/" @@ -364,15 +365,43 @@ bool CommonObjectDetectorCUDAImpl::cudaSetup(CommonObjectDetectorBase* base_, bo double thrs_nms = base_->getThrsNms(); std::string engine_fn = get_home() + SV_MODEL_DIR + dataset + ".engine"; + std::vector files; + _list_dir(get_home() + SV_MODEL_DIR, files, "-online.engine", dataset + "-yolov5s-"); + if (files.size() > 0) + { + std::sort(files.rbegin(), files.rend(), _comp_str_lesser); + engine_fn = get_home() + SV_MODEL_DIR + files[0]; + } + if (input_w == 1280) { - engine_fn = get_home() + SV_MODEL_DIR + dataset + "_HD.engine"; + files.clear(); + _list_dir(get_home() + SV_MODEL_DIR, files, "-online.engine", dataset + "-yolov5s6-"); + if (files.size() > 0) + { + std::sort(files.rbegin(), files.rend(), _comp_str_lesser); + engine_fn = get_home() + SV_MODEL_DIR + files[0]; + } + else + { + engine_fn = get_home() + SV_MODEL_DIR + dataset + "_HD.engine"; + } } if (with_segmentation) { base_->setInputH(640); base_->setInputW(640); - engine_fn = get_home() + SV_MODEL_DIR + dataset + "_SEG.engine"; + files.clear(); + _list_dir(get_home() + SV_MODEL_DIR, files, "-online.engine", dataset + "-yolov5s-seg-"); + if (files.size() > 0) + { + std::sort(files.rbegin(), files.rend(), _comp_str_lesser); + engine_fn = get_home() + SV_MODEL_DIR + files[0]; + } + else + { + engine_fn = get_home() + SV_MODEL_DIR + dataset + "_SEG.engine"; + } } std::cout << "Load: " << engine_fn << std::endl; if (!is_file_exist(engine_fn)) diff --git a/algorithm/landing_det/cuda/landing_det_cuda_impl.cpp b/algorithm/landing_det/cuda/landing_det_cuda_impl.cpp index a6fa04a..97fa80c 100644 --- a/algorithm/landing_det/cuda/landing_det_cuda_impl.cpp +++ b/algorithm/landing_det/cuda/landing_det_cuda_impl.cpp @@ -1,6 +1,7 @@ #include "landing_det_cuda_impl.h" #include #include +#include "sv_util.h" #define SV_MODEL_DIR "/SpireCV/models/" #define SV_ROOT_DIR "/SpireCV/" @@ -50,6 +51,16 @@ bool LandingMarkerDetectorCUDAImpl::cudaSetup() { #ifdef WITH_CUDA std::string trt_model_fn = get_home() + SV_MODEL_DIR + "LandingMarker.engine"; + + std::vector files; + _list_dir(get_home() + SV_MODEL_DIR, files, "-online.engine", "LandingMarker-"); + if (files.size() > 0) + { + std::sort(files.rbegin(), files.rend(), _comp_str_lesser); + trt_model_fn = get_home() + SV_MODEL_DIR + files[0]; + } + std::cout << "Load: " << trt_model_fn << std::endl; + if (!is_file_exist(trt_model_fn)) { throw std::runtime_error("SpireCV (104) Error loading the LandingMarker TensorRT model (File Not Exist)"); diff --git a/algorithm/sot/ocv470/sot_ocv470_impl.cpp b/algorithm/sot/ocv470/sot_ocv470_impl.cpp index b711f2e..027ae2c 100644 --- a/algorithm/sot/ocv470/sot_ocv470_impl.cpp +++ b/algorithm/sot/ocv470/sot_ocv470_impl.cpp @@ -1,6 +1,7 @@ #include "sot_ocv470_impl.h" #include #include +#include "sv_util.h" #define SV_MODEL_DIR "/SpireCV/models/" #define SV_ROOT_DIR "/SpireCV/" @@ -30,9 +31,39 @@ bool SingleObjectTrackerOCV470Impl::ocv470Setup(SingleObjectTrackerBase* base_) std::string net = get_home() + SV_MODEL_DIR + "dasiamrpn_model.onnx"; std::string kernel_cls1 = get_home() + SV_MODEL_DIR + "dasiamrpn_kernel_cls1.onnx"; std::string kernel_r1 = get_home() + SV_MODEL_DIR + "dasiamrpn_kernel_r1.onnx"; + + std::vector files1, files2, files3; + _list_dir(get_home() + SV_MODEL_DIR, files1, "-online.engine", "DaSiamRPN-Model-"); + _list_dir(get_home() + SV_MODEL_DIR, files2, "-online.engine", "DaSiamRPN-Kernel-CLS1-"); + _list_dir(get_home() + SV_MODEL_DIR, files3, "-online.engine", "DaSiamRPN-Kernel-R1-"); + if (files1.size() > 0 && files2.size() > 0 && files3.size() > 0) + { + std::sort(files1.rbegin(), files1.rend(), _comp_str_lesser); + std::sort(files2.rbegin(), files2.rend(), _comp_str_lesser); + std::sort(files3.rbegin(), files3.rend(), _comp_str_lesser); + net = get_home() + SV_MODEL_DIR + files1[0]; + kernel_cls1 = get_home() + SV_MODEL_DIR + files2[0]; + kernel_r1 = get_home() + SV_MODEL_DIR + files3[0]; + } + std::cout << "Load: " << net << std::endl; + std::cout << "Load: " << kernel_cls1 << std::endl; + std::cout << "Load: " << kernel_r1 << std::endl; std::string backbone = get_home() + SV_MODEL_DIR + "nanotrack_backbone_sim.onnx"; std::string neckhead = get_home() + SV_MODEL_DIR + "nanotrack_head_sim.onnx"; + + std::vector files4, files5; + _list_dir(get_home() + SV_MODEL_DIR, files4, "-online.engine", "NanoTrack-Backbone-SIM-"); + _list_dir(get_home() + SV_MODEL_DIR, files5, "-online.engine", "NanoTrack-Head-SIM-"); + if (files4.size() > 0 && files5.size() > 0) + { + std::sort(files4.rbegin(), files4.rend(), _comp_str_lesser); + std::sort(files5.rbegin(), files5.rend(), _comp_str_lesser); + backbone = get_home() + SV_MODEL_DIR + files4[0]; + neckhead = get_home() + SV_MODEL_DIR + files5[0]; + } + std::cout << "Load: " << backbone << std::endl; + std::cout << "Load: " << neckhead << std::endl; try { diff --git a/algorithm/veri/cuda/veri_det_cuda_impl.cpp b/algorithm/veri/cuda/veri_det_cuda_impl.cpp index bf1fcc4..c42c722 100644 --- a/algorithm/veri/cuda/veri_det_cuda_impl.cpp +++ b/algorithm/veri/cuda/veri_det_cuda_impl.cpp @@ -1,6 +1,7 @@ #include "veri_det_cuda_impl.h" #include #include +#include "sv_util.h" #define SV_MODEL_DIR "/SpireCV/models/" #define SV_ROOT_DIR "/SpireCV/" @@ -75,6 +76,16 @@ namespace sv { #ifdef WITH_CUDA std::string trt_model_fn = get_home() + SV_MODEL_DIR + "veri.engine"; + + std::vector files; + _list_dir(get_home() + SV_MODEL_DIR, files, "-online.engine", "VERI-"); + if (files.size() > 0) + { + std::sort(files.rbegin(), files.rend(), _comp_str_lesser); + trt_model_fn = get_home() + SV_MODEL_DIR + files[0]; + } + std::cout << "Load: " << trt_model_fn << std::endl; + if (!is_file_exist(trt_model_fn)) { throw std::runtime_error("SpireCV (104) Error loading the VeriDetector TensorRT model (File Not Exist)"); diff --git a/utils/gason.cpp b/utils/gason.cpp index 3443243..a490bda 100644 --- a/utils/gason.cpp +++ b/utils/gason.cpp @@ -379,7 +379,7 @@ void _load_all_json(std::string json_fn, JsonValue& value, JsonAllocator& alloca } fin.close(); // std::cout << json_str << std::endl; - char source[1024 * 1024]; // 1M + char source[1024 * 256]; // 256K char *endptr; strcpy(source, json_str.c_str()); diff --git a/utils/sv_util.cpp b/utils/sv_util.cpp index b37ff19..d42f23f 100644 --- a/utils/sv_util.cpp +++ b/utils/sv_util.cpp @@ -6,6 +6,10 @@ #include #include #include +#include +#include +#include + using namespace std; @@ -25,6 +29,15 @@ bool _is_file_exist(std::string& fn) return f.good(); } +bool _comp_str_greater(const std::string& a, const std::string& b) +{ + return a > b; +} + +bool _comp_str_lesser(const std::string& a, const std::string& b) +{ + return a < b; +} void _get_sys_time(TimeInfo& t_info) { @@ -136,4 +149,56 @@ int _comp_str_idx(const std::string& in_str, const std::string* str_list, int le return -1; } + +void _list_dir(std::string dir, std::vector& files, std::string suffixs, std::string prefix, bool r) { + // assert(_endswith(dir, "/") || _endswith(dir, "\\")); + + DIR *pdir; + struct dirent *ent; + string childpath; + string absolutepath; + pdir = opendir(dir.c_str()); + assert(pdir != NULL); + + vector suffixd(0); + if (!suffixs.empty() && suffixs != "") { + suffixd = _split(suffixs, "|"); + } + + while ((ent = readdir(pdir)) != NULL) { + if (ent->d_type & DT_DIR) { + if (strcmp(ent->d_name, ".") == 0 || strcmp(ent->d_name, "..") == 0) { + continue; + } + if (r) { // If need to traverse subdirectories + childpath = dir + ent->d_name; + _list_dir(childpath, files); + } + } + else { + if (suffixd.size() > 0) { + bool can_push = false, cancan_push = true; + for (int i = 0; i < (int)suffixd.size(); i++) { + if (_endswith(ent->d_name, suffixd[i])) + can_push = true; + } + if (prefix.size() > 0) { + if (!_startswith(ent->d_name, prefix)) + cancan_push = false; + } + if (can_push && cancan_push) { + absolutepath = dir + ent->d_name; + files.push_back(ent->d_name); // filepath + } + } + else { + absolutepath = dir + ent->d_name; + files.push_back(ent->d_name); // filepath + } + } + } + sort(files.begin(), files.end()); //sort names +} + + } diff --git a/utils/sv_util.h b/utils/sv_util.h index d46b9db..cfd0b1d 100644 --- a/utils/sv_util.h +++ b/utils/sv_util.h @@ -24,10 +24,13 @@ bool _startswith(const std::string& str, const std::string& start); bool _endswith(const std::string& str, const std::string& end); std::string _trim(const std::string& str); int _comp_str_idx(const std::string& in_str, const std::string* str_list, int len); +bool _comp_str_greater(const std::string& a, const std::string& b); +bool _comp_str_lesser(const std::string& a, const std::string& b); /************* file-related functions ***************/ std::string _get_home(); bool _is_file_exist(std::string& fn); +void _list_dir(std::string dir, std::vector& files, std::string suffixs="", std::string prefix="", bool r=false); }