From 95820a98923b3525b74f06a0a43afca3e7167cec Mon Sep 17 00:00:00 2001 From: DefTruth <31974251+DefTruth@users.noreply.github.com> Date: Fri, 17 Mar 2023 17:05:24 +0800 Subject: [PATCH] [Benchmark] Add GetModelResoucesNameFromDir APIs (#1640) --- benchmark/cpp/benchmark_picodet.cc | 18 +++++--- benchmark/cpp/benchmark_ppcls.cc | 15 ++++-- benchmark/cpp/benchmark_ppmatting.cc | 17 +++++-- benchmark/cpp/benchmark_ppocr_cls.cc | 12 +++-- benchmark/cpp/benchmark_ppocr_det.cc | 13 ++++-- benchmark/cpp/benchmark_ppocr_rec.cc | 13 ++++-- benchmark/cpp/benchmark_ppseg.cc | 14 ++++-- benchmark/cpp/benchmark_ppyoloe.cc | 17 ++++--- benchmark/cpp/benchmark_ppyolov5.cc | 16 +++++-- benchmark/cpp/benchmark_ppyolov6.cc | 16 +++++-- benchmark/cpp/benchmark_ppyolov7.cc | 16 +++++-- benchmark/cpp/benchmark_ppyolov8.cc | 16 +++++-- benchmark/cpp/benchmark_ppyolox.cc | 16 +++++-- benchmark/cpp/flags.h | 68 ++++++++++++++++++++++++++++ 14 files changed, 207 insertions(+), 60 deletions(-) mode change 100755 => 100644 benchmark/cpp/benchmark_ppmatting.cc mode change 100755 => 100644 benchmark/cpp/benchmark_ppyolov5.cc mode change 100755 => 100644 benchmark/cpp/benchmark_ppyolov6.cc mode change 100755 => 100644 benchmark/cpp/benchmark_ppyolov7.cc diff --git a/benchmark/cpp/benchmark_picodet.cc b/benchmark/cpp/benchmark_picodet.cc index b17effd29..e10e80bb5 100644 --- a/benchmark/cpp/benchmark_picodet.cc +++ b/benchmark/cpp/benchmark_picodet.cc @@ -32,12 +32,18 @@ int main(int argc, char* argv[]) { std::unordered_map config_info; benchmark::ResultManager::LoadBenchmarkConfig(FLAGS_config_path, &config_info); - auto backend = config_info["backend"]; - auto model_file = FLAGS_model + sep + "model.pdmodel"; - auto params_file = FLAGS_model + sep + "model.pdiparams"; - auto config_file = FLAGS_model + sep + "infer_cfg.yml"; - auto model_picodet = - vision::detection::PicoDet(model_file, params_file, config_file, option); + std::string model_name, params_name, config_name; + auto model_format = fastdeploy::ModelFormat::PADDLE; + if (!UpdateModelResourceName(&model_name, ¶ms_name, &config_name, + &model_format, config_info)) { + return -1; + } + + auto model_file = FLAGS_model + sep + model_name; + auto params_file = FLAGS_model + sep + params_name; + auto config_file = FLAGS_model + sep + config_name; + auto model_picodet = vision::detection::PicoDet( + model_file, params_file, config_file, option, model_format); if (FLAGS_no_nms) { model_picodet.GetPostprocessor().ApplyNMS(); } diff --git a/benchmark/cpp/benchmark_ppcls.cc b/benchmark/cpp/benchmark_ppcls.cc index 486206ac2..506cf054e 100755 --- a/benchmark/cpp/benchmark_ppcls.cc +++ b/benchmark/cpp/benchmark_ppcls.cc @@ -34,11 +34,18 @@ int main(int argc, char* argv[]) { if (config_info["backend"] == "paddle_trt") { option.trt_option.max_batch_size = 1; } - auto model_file = FLAGS_model + sep + "inference.pdmodel"; - auto params_file = FLAGS_model + sep + "inference.pdiparams"; - auto config_file = FLAGS_model + sep + "inference_cls.yaml"; + std::string model_name, params_name, config_name; + auto model_format = fastdeploy::ModelFormat::PADDLE; + if (!UpdateModelResourceName(&model_name, ¶ms_name, &config_name, + &model_format, config_info)) { + return -1; + } + + auto model_file = FLAGS_model + sep + model_name; + auto params_file = FLAGS_model + sep + params_name; + auto config_file = FLAGS_model + sep + config_name; auto model_ppcls = vision::classification::PaddleClasModel( - model_file, params_file, config_file, option); + model_file, params_file, config_file, option, model_format); vision::ClassifyResult res; if (config_info["precision_compare"] == "true") { // Run once at least diff --git a/benchmark/cpp/benchmark_ppmatting.cc b/benchmark/cpp/benchmark_ppmatting.cc old mode 100755 new mode 100644 index 18598c4bb..a7ffb4bc0 --- a/benchmark/cpp/benchmark_ppmatting.cc +++ b/benchmark/cpp/benchmark_ppmatting.cc @@ -34,9 +34,16 @@ int main(int argc, char* argv[]) { std::unordered_map config_info; benchmark::ResultManager::LoadBenchmarkConfig(FLAGS_config_path, &config_info); - auto model_file = FLAGS_model + sep + "model.pdmodel"; - auto params_file = FLAGS_model + sep + "model.pdiparams"; - auto config_file = FLAGS_model + sep + "deploy.yaml"; + std::string model_name, params_name, config_name; + auto model_format = fastdeploy::ModelFormat::PADDLE; + if (!UpdateModelResourceName(&model_name, ¶ms_name, &config_name, + &model_format, config_info)) { + return -1; + } + + auto model_file = FLAGS_model + sep + model_name; + auto params_file = FLAGS_model + sep + params_name; + auto config_file = FLAGS_model + sep + config_name; if (config_info["backend"] == "paddle_trt") { option.paddle_infer_option.collect_trt_shape = true; } @@ -47,8 +54,8 @@ int main(int argc, char* argv[]) { option.trt_option.SetShape("x", trt_shapes[0], trt_shapes[1], trt_shapes[2]); } - auto model_ppmatting = - vision::matting::PPMatting(model_file, params_file, config_file, option); + auto model_ppmatting = vision::matting::PPMatting( + model_file, params_file, config_file, option, model_format); vision::MattingResult res; if (config_info["precision_compare"] == "true") { // Run once at least diff --git a/benchmark/cpp/benchmark_ppocr_cls.cc b/benchmark/cpp/benchmark_ppocr_cls.cc index 40e9fbc97..5b3782b06 100644 --- a/benchmark/cpp/benchmark_ppocr_cls.cc +++ b/benchmark/cpp/benchmark_ppocr_cls.cc @@ -34,9 +34,15 @@ int main(int argc, char* argv[]) { std::unordered_map config_info; benchmark::ResultManager::LoadBenchmarkConfig(FLAGS_config_path, &config_info); + std::string model_name, params_name, config_name; + auto model_format = fastdeploy::ModelFormat::PADDLE; + if (!UpdateModelResourceName(&model_name, ¶ms_name, &config_name, + &model_format, config_info, false)) { + return -1; + } // Classification Model - auto cls_model_file = FLAGS_model + sep + "inference.pdmodel"; - auto cls_params_file = FLAGS_model + sep + "inference.pdiparams"; + auto model_file = FLAGS_model + sep + model_name; + auto params_file = FLAGS_model + sep + params_name; if (config_info["backend"] == "paddle_trt") { option.paddle_infer_option.collect_trt_shape = true; } @@ -48,7 +54,7 @@ int main(int argc, char* argv[]) { trt_shapes[2]); } auto model_ppocr_cls = - vision::ocr::Classifier(cls_model_file, cls_params_file, option); + vision::ocr::Classifier(model_file, params_file, option, model_format); int32_t res_label; float res_score; if (config_info["precision_compare"] == "true") { diff --git a/benchmark/cpp/benchmark_ppocr_det.cc b/benchmark/cpp/benchmark_ppocr_det.cc index 16d4273c1..62c55919f 100644 --- a/benchmark/cpp/benchmark_ppocr_det.cc +++ b/benchmark/cpp/benchmark_ppocr_det.cc @@ -35,8 +35,15 @@ int main(int argc, char* argv[]) { benchmark::ResultManager::LoadBenchmarkConfig(FLAGS_config_path, &config_info); // Detection Model - auto det_model_file = FLAGS_model + sep + "inference.pdmodel"; - auto det_params_file = FLAGS_model + sep + "inference.pdiparams"; + std::string model_name, params_name, config_name; + auto model_format = fastdeploy::ModelFormat::PADDLE; + if (!UpdateModelResourceName(&model_name, ¶ms_name, &config_name, + &model_format, config_info, false)) { + return -1; + } + // Classification Model + auto model_file = FLAGS_model + sep + model_name; + auto params_file = FLAGS_model + sep + params_name; if (config_info["backend"] == "paddle_trt") { option.paddle_infer_option.collect_trt_shape = true; } @@ -48,7 +55,7 @@ int main(int argc, char* argv[]) { trt_shapes[2]); } auto model_ppocr_det = - vision::ocr::DBDetector(det_model_file, det_params_file, option); + vision::ocr::DBDetector(model_file, params_file, option, model_format); std::vector> res; if (config_info["precision_compare"] == "true") { // Run once at least diff --git a/benchmark/cpp/benchmark_ppocr_rec.cc b/benchmark/cpp/benchmark_ppocr_rec.cc index ef8983a0c..8a29c3714 100644 --- a/benchmark/cpp/benchmark_ppocr_rec.cc +++ b/benchmark/cpp/benchmark_ppocr_rec.cc @@ -35,9 +35,14 @@ int main(int argc, char* argv[]) { std::unordered_map config_info; benchmark::ResultManager::LoadBenchmarkConfig(FLAGS_config_path, &config_info); - // Recognition Model - auto rec_model_file = FLAGS_model + sep + "inference.pdmodel"; - auto rec_params_file = FLAGS_model + sep + "inference.pdiparams"; + std::string model_name, params_name, config_name; + auto model_format = fastdeploy::ModelFormat::PADDLE; + if (!UpdateModelResourceName(&model_name, ¶ms_name, &config_name, + &model_format, config_info, false)) { + return -1; + } + auto model_file = FLAGS_model + sep + model_name; + auto params_file = FLAGS_model + sep + params_name; if (config_info["backend"] == "paddle_trt") { option.paddle_infer_option.collect_trt_shape = true; } @@ -49,7 +54,7 @@ int main(int argc, char* argv[]) { trt_shapes[2]); } auto model_ppocr_rec = vision::ocr::Recognizer( - rec_model_file, rec_params_file, FLAGS_rec_label_file, option); + model_file, params_file, FLAGS_rec_label_file, option, model_format); std::string text; float rec_score; if (config_info["precision_compare"] == "true") { diff --git a/benchmark/cpp/benchmark_ppseg.cc b/benchmark/cpp/benchmark_ppseg.cc index 4d2908a8b..419954ad1 100644 --- a/benchmark/cpp/benchmark_ppseg.cc +++ b/benchmark/cpp/benchmark_ppseg.cc @@ -34,9 +34,15 @@ int main(int argc, char* argv[]) { std::unordered_map config_info; benchmark::ResultManager::LoadBenchmarkConfig(FLAGS_config_path, &config_info); - auto model_file = FLAGS_model + sep + "model.pdmodel"; - auto params_file = FLAGS_model + sep + "model.pdiparams"; - auto config_file = FLAGS_model + sep + "deploy.yaml"; + std::string model_name, params_name, config_name; + auto model_format = fastdeploy::ModelFormat::PADDLE; + if (!UpdateModelResourceName(&model_name, ¶ms_name, &config_name, + &model_format, config_info)) { + return -1; + } + auto model_file = FLAGS_model + sep + model_name; + auto params_file = FLAGS_model + sep + params_name; + auto config_file = FLAGS_model + sep + config_name; if (config_info["backend"] == "paddle_trt") { option.paddle_infer_option.collect_trt_shape = true; } @@ -48,7 +54,7 @@ int main(int argc, char* argv[]) { trt_shapes[2]); } auto model_ppseg = vision::segmentation::PaddleSegModel( - model_file, params_file, config_file, option); + model_file, params_file, config_file, option, model_format); vision::SegmentationResult res; if (config_info["precision_compare"] == "true") { // Run once at least diff --git a/benchmark/cpp/benchmark_ppyoloe.cc b/benchmark/cpp/benchmark_ppyoloe.cc index 61de858a6..ef87bbacf 100644 --- a/benchmark/cpp/benchmark_ppyoloe.cc +++ b/benchmark/cpp/benchmark_ppyoloe.cc @@ -32,12 +32,17 @@ int main(int argc, char* argv[]) { std::unordered_map config_info; benchmark::ResultManager::LoadBenchmarkConfig(FLAGS_config_path, &config_info); - auto backend = config_info["backend"]; - auto model_file = FLAGS_model + sep + "model.pdmodel"; - auto params_file = FLAGS_model + sep + "model.pdiparams"; - auto config_file = FLAGS_model + sep + "infer_cfg.yml"; - auto model_ppyoloe = - vision::detection::PPYOLOE(model_file, params_file, config_file, option); + std::string model_name, params_name, config_name; + auto model_format = fastdeploy::ModelFormat::PADDLE; + if (!UpdateModelResourceName(&model_name, ¶ms_name, &config_name, + &model_format, config_info)) { + return -1; + } + auto model_file = FLAGS_model + sep + model_name; + auto params_file = FLAGS_model + sep + params_name; + auto config_file = FLAGS_model + sep + config_name; + auto model_ppyoloe = vision::detection::PPYOLOE( + model_file, params_file, config_file, option, model_format); if (FLAGS_no_nms) { model_ppyoloe.GetPostprocessor().ApplyNMS(); } diff --git a/benchmark/cpp/benchmark_ppyolov5.cc b/benchmark/cpp/benchmark_ppyolov5.cc old mode 100755 new mode 100644 index 3aa61a704..7341843eb --- a/benchmark/cpp/benchmark_ppyolov5.cc +++ b/benchmark/cpp/benchmark_ppyolov5.cc @@ -32,11 +32,17 @@ int main(int argc, char* argv[]) { std::unordered_map config_info; benchmark::ResultManager::LoadBenchmarkConfig(FLAGS_config_path, &config_info); - auto model_file = FLAGS_model + sep + "model.pdmodel"; - auto params_file = FLAGS_model + sep + "model.pdiparams"; - auto config_file = FLAGS_model + sep + "infer_cfg.yml"; - auto model_ppyolov5 = vision::detection::PaddleYOLOv5(model_file, params_file, - config_file, option); + std::string model_name, params_name, config_name; + auto model_format = fastdeploy::ModelFormat::PADDLE; + if (!UpdateModelResourceName(&model_name, ¶ms_name, &config_name, + &model_format, config_info)) { + return -1; + } + auto model_file = FLAGS_model + sep + model_name; + auto params_file = FLAGS_model + sep + params_name; + auto config_file = FLAGS_model + sep + config_name; + auto model_ppyolov5 = vision::detection::PaddleYOLOv5( + model_file, params_file, config_file, option, model_format); vision::DetectionResult res; if (config_info["precision_compare"] == "true") { // Run once at least diff --git a/benchmark/cpp/benchmark_ppyolov6.cc b/benchmark/cpp/benchmark_ppyolov6.cc old mode 100755 new mode 100644 index fdc632e55..cb3fec26c --- a/benchmark/cpp/benchmark_ppyolov6.cc +++ b/benchmark/cpp/benchmark_ppyolov6.cc @@ -32,11 +32,17 @@ int main(int argc, char* argv[]) { std::unordered_map config_info; benchmark::ResultManager::LoadBenchmarkConfig(FLAGS_config_path, &config_info); - auto model_file = FLAGS_model + sep + "model.pdmodel"; - auto params_file = FLAGS_model + sep + "model.pdiparams"; - auto config_file = FLAGS_model + sep + "infer_cfg.yml"; - auto model_ppyolov6 = vision::detection::PaddleYOLOv6(model_file, params_file, - config_file, option); + std::string model_name, params_name, config_name; + auto model_format = fastdeploy::ModelFormat::PADDLE; + if (!UpdateModelResourceName(&model_name, ¶ms_name, &config_name, + &model_format, config_info)) { + return -1; + } + auto model_file = FLAGS_model + sep + model_name; + auto params_file = FLAGS_model + sep + params_name; + auto config_file = FLAGS_model + sep + config_name; + auto model_ppyolov6 = vision::detection::PaddleYOLOv6( + model_file, params_file, config_file, option, model_format); vision::DetectionResult res; if (config_info["precision_compare"] == "true") { // Run once at least diff --git a/benchmark/cpp/benchmark_ppyolov7.cc b/benchmark/cpp/benchmark_ppyolov7.cc old mode 100755 new mode 100644 index 234827851..4d967cfe8 --- a/benchmark/cpp/benchmark_ppyolov7.cc +++ b/benchmark/cpp/benchmark_ppyolov7.cc @@ -32,11 +32,17 @@ int main(int argc, char* argv[]) { std::unordered_map config_info; benchmark::ResultManager::LoadBenchmarkConfig(FLAGS_config_path, &config_info); - auto model_file = FLAGS_model + sep + "model.pdmodel"; - auto params_file = FLAGS_model + sep + "model.pdiparams"; - auto config_file = FLAGS_model + sep + "infer_cfg.yml"; - auto model_ppyolov7 = vision::detection::PaddleYOLOv7(model_file, params_file, - config_file, option); + std::string model_name, params_name, config_name; + auto model_format = fastdeploy::ModelFormat::PADDLE; + if (!UpdateModelResourceName(&model_name, ¶ms_name, &config_name, + &model_format, config_info)) { + return -1; + } + auto model_file = FLAGS_model + sep + model_name; + auto params_file = FLAGS_model + sep + params_name; + auto config_file = FLAGS_model + sep + config_name; + auto model_ppyolov7 = vision::detection::PaddleYOLOv7( + model_file, params_file, config_file, option, model_format); vision::DetectionResult res; if (config_info["precision_compare"] == "true") { // Run once at least diff --git a/benchmark/cpp/benchmark_ppyolov8.cc b/benchmark/cpp/benchmark_ppyolov8.cc index ccffd27a1..264f149ee 100644 --- a/benchmark/cpp/benchmark_ppyolov8.cc +++ b/benchmark/cpp/benchmark_ppyolov8.cc @@ -32,11 +32,17 @@ int main(int argc, char* argv[]) { std::unordered_map config_info; benchmark::ResultManager::LoadBenchmarkConfig(FLAGS_config_path, &config_info); - auto model_file = FLAGS_model + sep + "model.pdmodel"; - auto params_file = FLAGS_model + sep + "model.pdiparams"; - auto config_file = FLAGS_model + sep + "infer_cfg.yml"; - auto model_ppyolov8 = vision::detection::PaddleYOLOv8(model_file, params_file, - config_file, option); + std::string model_name, params_name, config_name; + auto model_format = fastdeploy::ModelFormat::PADDLE; + if (!UpdateModelResourceName(&model_name, ¶ms_name, &config_name, + &model_format, config_info)) { + return -1; + } + auto model_file = FLAGS_model + sep + model_name; + auto params_file = FLAGS_model + sep + params_name; + auto config_file = FLAGS_model + sep + config_name; + auto model_ppyolov8 = vision::detection::PaddleYOLOv8( + model_file, params_file, config_file, option, model_format); vision::DetectionResult res; if (config_info["precision_compare"] == "true") { // Run once at least diff --git a/benchmark/cpp/benchmark_ppyolox.cc b/benchmark/cpp/benchmark_ppyolox.cc index 2e64188f6..407827473 100644 --- a/benchmark/cpp/benchmark_ppyolox.cc +++ b/benchmark/cpp/benchmark_ppyolox.cc @@ -32,11 +32,17 @@ int main(int argc, char* argv[]) { std::unordered_map config_info; benchmark::ResultManager::LoadBenchmarkConfig(FLAGS_config_path, &config_info); - auto model_file = FLAGS_model + sep + "model.pdmodel"; - auto params_file = FLAGS_model + sep + "model.pdiparams"; - auto config_file = FLAGS_model + sep + "infer_cfg.yml"; - auto model_ppyolox = vision::detection::PaddleYOLOX(model_file, params_file, - config_file, option); + std::string model_name, params_name, config_name; + auto model_format = fastdeploy::ModelFormat::PADDLE; + if (!UpdateModelResourceName(&model_name, ¶ms_name, &config_name, + &model_format, config_info)) { + return -1; + } + auto model_file = FLAGS_model + sep + model_name; + auto params_file = FLAGS_model + sep + params_name; + auto config_file = FLAGS_model + sep + config_name; + auto model_ppyolox = vision::detection::PaddleYOLOX( + model_file, params_file, config_file, option, model_format); vision::DetectionResult res; if (config_info["precision_compare"] == "true") { // Run once at least diff --git a/benchmark/cpp/flags.h b/benchmark/cpp/flags.h index 17f0a7df6..b3e773f6a 100755 --- a/benchmark/cpp/flags.h +++ b/benchmark/cpp/flags.h @@ -17,6 +17,9 @@ #include #include "gflags/gflags.h" #include "fastdeploy/benchmark/utils.h" +#include +#include +#include #ifdef WIN32 static const char sep = '\\'; @@ -91,3 +94,68 @@ static void PrintBenchmarkInfo(std::unordered_mapd_name, ".") != 0 && strcmp(ptr->d_name, "..") != 0) { + std::string tmp_file_name = ptr->d_name; + if (tmp_file_name.find(suffix) != std::string::npos) { + if (suffix == "pdiparams") { + if (tmp_file_name.find("info") == std::string::npos) { + find = true; + *resource_name = tmp_file_name; + break; + } + } else { + find = true; + *resource_name = tmp_file_name; + break; + } + } else { + if (suffix == "yml") { + if (tmp_file_name.find("yaml") != std::string::npos) { + find = true; + *resource_name = tmp_file_name; + break; + } + } else if (suffix == "yaml") { + if (tmp_file_name.find("yml") != std::string::npos) { + find = true; + *resource_name = tmp_file_name; + break; + } + } + } + } + } + closedir(p_dir); + return find; +} + +static bool UpdateModelResourceName( + std::string* model_name, std::string* params_name, + std::string* config_name, fastdeploy::ModelFormat* model_format, + std::unordered_map& config_info, + bool use_config_file = true, bool use_quant_model = false) { + *model_format = fastdeploy::ModelFormat::PADDLE; + if (!(GetModelResoucesNameFromDir(FLAGS_model, model_name, "pdmodel") + && GetModelResoucesNameFromDir(FLAGS_model, params_name, "pdiparams"))) { + std::cout << "Can not find Paddle model resources." << std::endl; + return false; + } + if (use_config_file) { + if (!GetModelResoucesNameFromDir(FLAGS_model, config_name, "yml")) { + std::cout << "Can not find config yaml resources." << std::endl; + return false; + } + } + return true; +}