diff --git a/benchmark/run_benchmark.sh b/benchmark/run_benchmark.sh index 7c1513e60..8cee6fce7 100644 --- a/benchmark/run_benchmark.sh +++ b/benchmark/run_benchmark.sh @@ -7,7 +7,7 @@ function _set_params(){ batch_size=${2:-"64"} fp_item=${3:-"fp32"} # fp32|fp16 epochs=${4:-"2"} # 可选,如果需要修改代码提前中断 - model_name=${5:-"model_name"} + model_item=${5:-"model_item"} run_log_path=${TRAIN_LOG_DIR:-$(pwd)} # TRAIN_LOG_DIR 后续QA设置该参数 index=1 @@ -23,16 +23,17 @@ function _set_params(){ device=${CUDA_VISIBLE_DEVICES//,/ } arr=(${device}) num_gpu_devices=${#arr[*]} - log_file=${run_log_path}/clas_${model_name}_${run_mode}_bs${batch_size}_${fp_item}_${num_gpu_devices} + log_file=${run_log_path}/clas_${model_item}_${run_mode}_bs${batch_size}_${fp_item}_${num_gpu_devices} + model_name=${model_item}_bs${batch_size}_${fp_item} # model_item 用于yml匹配,model_name用于入库 } function _train(){ echo "Train on ${num_gpu_devices} GPUs" echo "current CUDA_VISIBLE_DEVICES=$CUDA_VISIBLE_DEVICES, gpus=$num_gpu_devices, batch_size=$batch_size" if [ ${fp_item} = "fp32" ];then - model_config=`find ppcls/configs/ImageNet -name ${model_name}.yaml` + model_config=`find ppcls/configs/ImageNet -name ${model_item}.yaml` else - model_config=`find ppcls/configs/ImageNet -name ${model_name}_fp16.yaml` + model_config=`find ppcls/configs/ImageNet -name ${model_item}_fp16.yaml` fi train_cmd="-c ${model_config} -o DataLoader.Train.sampler.batch_size=${batch_size} -o Global.epochs=${epochs} -o Global.eval_during_train=False -o Global.print_batch_step=2" diff --git a/deploy/cpp/readme.md b/deploy/cpp/readme.md index a8af240c9..72ce8dacc 100644 --- a/deploy/cpp/readme.md +++ b/deploy/cpp/readme.md @@ -215,9 +215,9 @@ cp ../configs/inference_cls.yaml tools/ #### 2.3.2 执行 ```shell -./build/clas_system -c inference_cls.yaml +./build/clas_system -c tools/inference_cls.yaml # or -./build/clas_system -config inference_cls.yaml +./build/clas_system -config tools/inference_cls.yaml ``` 最终屏幕上会输出结果,如下图所示。 diff --git a/deploy/cpp_shitu/src/feature_extracter.cpp b/deploy/cpp_shitu/src/feature_extracter.cpp index 9588ff57e..37c7590ff 100644 --- a/deploy/cpp_shitu/src/feature_extracter.cpp +++ b/deploy/cpp_shitu/src/feature_extracter.cpp @@ -59,7 +59,7 @@ namespace Feature { cv::Mat resize_img; std::vector time; - auto preprocess_start = std::chrono::system_clock::now(); + auto preprocess_start = std::chrono::steady_clock::now(); this->resize_op_.Run(img, resize_img, this->resize_short_, this->resize_size_); @@ -70,9 +70,9 @@ namespace Feature { auto input_names = this->predictor_->GetInputNames(); auto input_t = this->predictor_->GetInputHandle(input_names[0]); input_t->Reshape({1, 3, resize_img.rows, resize_img.cols}); - auto preprocess_end = std::chrono::system_clock::now(); + auto preprocess_end = std::chrono::steady_clock::now(); - auto infer_start = std::chrono::system_clock::now(); + auto infer_start = std::chrono::steady_clock::now(); input_t->CopyFromCpu(input.data()); this->predictor_->Run(); @@ -84,18 +84,18 @@ namespace Feature { out_data.resize(out_num); output_t->CopyToCpu(out_data.data()); - auto infer_end = std::chrono::system_clock::now(); + auto infer_end = std::chrono::steady_clock::now(); - auto postprocess_start = std::chrono::system_clock::now(); + auto postprocess_start = std::chrono::steady_clock::now(); if (this->feature_norm) FeatureNorm(out_data); - auto postprocess_end = std::chrono::system_clock::now(); + auto postprocess_end = std::chrono::steady_clock::now(); std::chrono::duration preprocess_diff = preprocess_end - preprocess_start; - time.push_back(double(preprocess_diff.count())); + time.push_back(double(preprocess_diff.count()) * 1000); std::chrono::duration inference_diff = infer_end - infer_start; - double inference_cost_time = double(inference_diff.count()); + double inference_cost_time = double(inference_diff.count() * 1000); time.push_back(inference_cost_time); // std::chrono::duration postprocess_diff = // postprocess_end - postprocess_start; diff --git a/deploy/cpp_shitu/src/main.cpp b/deploy/cpp_shitu/src/main.cpp index d89ba14d5..be37d3afd 100644 --- a/deploy/cpp_shitu/src/main.cpp +++ b/deploy/cpp_shitu/src/main.cpp @@ -179,9 +179,9 @@ int main(int argc, char **argv) { if (config.config_file["Global"]["visual_det"].IsDefined()) { visual_det = config.config_file["Global"]["visual_det"].as(); } - bool run_benchmark = false; + bool benchmark = false; if (config.config_file["Global"]["benchmark"].IsDefined()) { - run_benchmark = config.config_file["Global"]["benchmark"].as(); + benchmark = config.config_file["Global"]["benchmark"].as(); } int max_det_results = 5; if (config.config_file["Global"]["max_det_results"].IsDefined()) { @@ -210,6 +210,8 @@ int main(int argc, char **argv) { // for time log std::vector cls_times = {0, 0, 0}; std::vector det_times = {0, 0, 0}; + std::vector search_times = {0, 0, 0}; + int instance_num = 0; // for read images std::vector batch_imgs; std::vector img_paths; @@ -222,7 +224,12 @@ int main(int argc, char **argv) { // for nms std::vector indeices; - int warmup_iter = img_files_list.size() > 5 ? 5 : 0; + int warmup_iter = img_files_list.size() > 5 ? 5 : img_files_list.size(); + if (benchmark) { + img_files_list.insert(img_files_list.begin(), img_files_list.begin(), + img_files_list.begin() + warmup_iter); + } + for (int idx = 0; idx < img_files_list.size(); ++idx) { std::string img_path = img_files_list[idx]; cv::Mat srcimg = cv::imread(img_path, cv::IMREAD_COLOR); @@ -238,12 +245,14 @@ int main(int argc, char **argv) { // step1: get all detection results DetPredictImage(batch_imgs, img_paths, batch_size, &detector, det_result, - det_bbox_num, det_times, visual_det, run_benchmark); + det_bbox_num, det_times, visual_det, false); // select max_det_results bbox if (det_result.size() > max_det_results) { det_result.resize(max_det_results); } + instance_num += det_result.size(); + // step2: add the whole image for recognition to improve recall Detection::ObjectResult result_whole_img = { {0, 0, srcimg.cols - 1, srcimg.rows - 1}, 0, 1.0}; @@ -262,16 +271,25 @@ int main(int argc, char **argv) { } // step4: get search result + auto search_start = std::chrono::steady_clock::now(); search_result = searcher.Search(features.data(), det_result.size()); + auto search_end = std::chrono::steady_clock::now(); // nms for search result for (int i = 0; i < det_result.size(); ++i) { det_result[i].confidence = search_result.D[search_result.return_k * i]; } NMSBoxes(det_result, searcher.GetThreshold(), rec_nms_thresold, indeices); + auto nms_end = std::chrono::steady_clock::now(); + std::chrono::duration search_diff = search_end - search_start; + search_times[1] += double(search_diff.count() * 1000); + + std::chrono::duration nms_diff = nms_end - search_end; + search_times[2] += double(nms_diff.count() * 1000); // print result - PrintResult(img_path, det_result, indeices, searcher, search_result); + if (not benchmark or (benchmark and idx >= warmup_iter)) + PrintResult(img_path, det_result, indeices, searcher, search_result); // for postprocess batch_imgs.clear(); @@ -281,18 +299,44 @@ int main(int argc, char **argv) { feature.clear(); features.clear(); indeices.clear(); + if (benchmark and warmup_iter == idx + 1) { + det_times = {0, 0, 0}; + cls_times = {0, 0, 0}; + search_times = {0, 0, 0}; + instance_num = 0; + } } - std::string presion = "fp32"; + if (benchmark) { + std::string presion = "fp32"; + if (config.config_file["Global"]["use_fp16"].IsDefined() and + config.config_file["Global"]["use_fp16"].as()) + presion = "fp16"; + bool use_gpu = config.config_file["Global"]["use_gpu"].as(); + bool use_tensorrt = config.config_file["Global"]["use_tensorrt"].as(); + bool enable_mkldnn = + config.config_file["Global"]["enable_mkldnn"].as(); + int cpu_num_threads = + config.config_file["Global"]["cpu_num_threads"].as(); + int batch_size = config.config_file["Global"]["batch_size"].as(); + std::vector shape = + config.config_file["Global"]["image_shape"].as < std::vector < int >> (); + std::string det_shape = std::to_string(shape[0]); + for (int i = 1; i < shape.size(); ++i) + det_shape = det_shape + ", " + std::to_string(shape[i]); - // if (config.use_fp16) - // presion = "fp16"; - // if (config.benchmark) { - // AutoLogger autolog("Classification", config.use_gpu, config.use_tensorrt, - // config.use_mkldnn, config.cpu_threads, 1, - // "1, 3, 224, 224", presion, cls_times, - // img_files_list.size()); - // autolog.report(); - // } + AutoLogger autolog_det("Det", use_gpu, use_tensorrt, enable_mkldnn, + cpu_num_threads, batch_size, det_shape, presion, + det_times, img_files_list.size() - warmup_iter); + autolog_det.report(); + AutoLogger autolog_rec("Rec", use_gpu, use_tensorrt, enable_mkldnn, + cpu_num_threads, batch_size, "3, 224, 224", presion, + cls_times, instance_num); + autolog_rec.report(); + AutoLogger autolog_search("Search", false, use_tensorrt, enable_mkldnn, + cpu_num_threads, batch_size, "dynamic", presion, + search_times, instance_num); + autolog_search.report(); + } return 0; } diff --git a/deploy/cpp_shitu/tools/transform_id_map.py b/deploy/cpp_shitu/tools/transform_id_map.py index 02fe0813e..3ad6b6f9b 100644 --- a/deploy/cpp_shitu/tools/transform_id_map.py +++ b/deploy/cpp_shitu/tools/transform_id_map.py @@ -14,20 +14,21 @@ def parse_args(): def main(): args = parse_args() - with open(args.config)as fd: - config = yaml.load(fd) + with open(args.config) as fd: + config = yaml.load(fd.read(), yaml.FullLoader) index_dir = "" try: index_dir = config["IndexProcess"]["index_dir"] except Exception as e: - print("The IndexProcess.index_dir in config_file dose not exist") + print("The IndexProcess.index_dir in config_file dose not exist") exit(1) id_map_path = os.path.join(index_dir, "id_map.pkl") - assert os.path.exists(id_map_path), "The id_map file dose not exist: {}".format(id_map_path) + assert os.path.exists( + id_map_path), "The id_map file dose not exist: {}".format(id_map_path) - with open(id_map_path, "rb")as fd: + with open(id_map_path, "rb") as fd: ids = pickle.load(fd) - with open(os.path.join(index_dir, "id_map.txt"), "w")as fd: + with open(os.path.join(index_dir, "id_map.txt"), "w") as fd: for k, v in ids.items(): v = v.split("\t")[1] fd.write(str(k) + " " + v + "\n") diff --git a/docs/images/wx_group.png b/docs/images/wx_group.png index 38663ce7c..a97ce9c5e 100644 Binary files a/docs/images/wx_group.png and b/docs/images/wx_group.png differ diff --git a/docs/zh_CN/introduction/more_demo.md b/docs/zh_CN/introduction/more_demo.md index 3db26632e..f12d44be0 100644 --- a/docs/zh_CN/introduction/more_demo.md +++ b/docs/zh_CN/introduction/more_demo.md @@ -1,61 +1,46 @@ ## 识别效果展示 - 商品识别 -
- -
-
- -
-
- -
-
- -
-
- -
-
- -
-
- -
-
- -
+
+
+
+
+
+ +[更多效果图](../../images/recognition/more_demo_images/product.md) + - 动漫人物识别 -
- -
-
- -
-
- -
+ +
+
+
+ +[更多效果图](../../images/recognition/more_demo_images/cartoon.md) + - logo识别 -
- -
-
- -
-
- -
+ +
+
+
+
+ +
+
+ + + + + + + +[更多效果图](../../images/recognition/more_demo_images/logo.md) + - 车辆识别 -
- -
-
- -
-
- -
-[更多效果图](../../images/recognition/more_demo_images) +
+
+
+ +[更多效果图](../../images/recognition/more_demo_images/vehicle.md) diff --git a/docs/zh_CN/quick_start/quick_start_classification_new_user.md b/docs/zh_CN/quick_start/quick_start_classification_new_user.md index b3f3dd5ce..468b35bb0 100644 --- a/docs/zh_CN/quick_start/quick_start_classification_new_user.md +++ b/docs/zh_CN/quick_start/quick_start_classification_new_user.md @@ -11,10 +11,10 @@ - [4. 模型训练](#4) - [4.1 使用 CPU 进行模型训练](#4.1) - [4.1.1 不使用预训练模型进行训练](#4.1.1) - - [4.1.2 不使用预训练模型进行训练](#4.1.2) + - [4.1.2 使用预训练模型进行训练](#4.1.2) - [4.2 使用 GPU 进行模型训练](#4.2) - [4.2.1 不使用预训练模型进行训练](#4.2.1) - - [4.2.2 不使用预训练模型进行训练](#4.2.2) + - [4.2.2 使用预训练模型进行训练](#4.2.2) - [5. 模型预测](#5) diff --git a/docs/zh_CN/quick_start/quick_start_classification_professional.md b/docs/zh_CN/quick_start/quick_start_classification_professional.md index 58708e08f..5a1304185 100644 --- a/docs/zh_CN/quick_start/quick_start_classification_professional.md +++ b/docs/zh_CN/quick_start/quick_start_classification_professional.md @@ -152,7 +152,7 @@ python3 -m paddle.distributed.launch \ * **注意** - * 其他数据增广的配置文件可以参考 `ppcls/configs/DataAugment` 中的配置文件。 + * 其他数据增广的配置文件可以参考 `ppcls/configs/ImageNet/DataAugment/` 中的配置文件。 * 训练 CIFAR100 的迭代轮数较少,因此进行训练时,验证集的精度指标可能会有 1% 左右的波动。 diff --git a/ppcls/configs/ImageNet/PPLCNet/PPLCNet_x0_25.yaml b/ppcls/configs/ImageNet/PPLCNet/PPLCNet_x0_25.yaml index 6773af797..3f328c676 100644 --- a/ppcls/configs/ImageNet/PPLCNet/PPLCNet_x0_25.yaml +++ b/ppcls/configs/ImageNet/PPLCNet/PPLCNet_x0_25.yaml @@ -4,7 +4,6 @@ Global: pretrained_model: null output_dir: ./output/ device: gpu - class_num: 1000 save_interval: 1 eval_during_train: True eval_interval: 1 @@ -17,7 +16,8 @@ Global: # model architecture Arch: name: PPLCNet_x0_25 - + class_num: 1000 + # loss function config for traing/eval process Loss: Train: diff --git a/ppcls/configs/ImageNet/PPLCNet/PPLCNet_x0_35.yaml b/ppcls/configs/ImageNet/PPLCNet/PPLCNet_x0_35.yaml index 36aa3dcb0..a3c01014f 100644 --- a/ppcls/configs/ImageNet/PPLCNet/PPLCNet_x0_35.yaml +++ b/ppcls/configs/ImageNet/PPLCNet/PPLCNet_x0_35.yaml @@ -4,7 +4,6 @@ Global: pretrained_model: null output_dir: ./output/ device: gpu - class_num: 1000 save_interval: 1 eval_during_train: True eval_interval: 1 @@ -17,6 +16,7 @@ Global: # model architecture Arch: name: PPLCNet_x0_35 + class_num: 1000 # loss function config for traing/eval process Loss: diff --git a/ppcls/configs/ImageNet/PPLCNet/PPLCNet_x0_5.yaml b/ppcls/configs/ImageNet/PPLCNet/PPLCNet_x0_5.yaml index 3ffc49385..8f5d2124b 100644 --- a/ppcls/configs/ImageNet/PPLCNet/PPLCNet_x0_5.yaml +++ b/ppcls/configs/ImageNet/PPLCNet/PPLCNet_x0_5.yaml @@ -4,7 +4,6 @@ Global: pretrained_model: null output_dir: ./output/ device: gpu - class_num: 1000 save_interval: 1 eval_during_train: True eval_interval: 1 @@ -17,6 +16,7 @@ Global: # model architecture Arch: name: PPLCNet_x0_5 + class_num: 1000 # loss function config for traing/eval process Loss: diff --git a/ppcls/configs/ImageNet/PPLCNet/PPLCNet_x0_75.yaml b/ppcls/configs/ImageNet/PPLCNet/PPLCNet_x0_75.yaml index 24ad8f373..b3fb75eeb 100644 --- a/ppcls/configs/ImageNet/PPLCNet/PPLCNet_x0_75.yaml +++ b/ppcls/configs/ImageNet/PPLCNet/PPLCNet_x0_75.yaml @@ -4,7 +4,6 @@ Global: pretrained_model: null output_dir: ./output/ device: gpu - class_num: 1000 save_interval: 1 eval_during_train: True eval_interval: 1 @@ -17,6 +16,7 @@ Global: # model architecture Arch: name: PPLCNet_x0_75 + class_num: 1000 # loss function config for traing/eval process Loss: diff --git a/ppcls/configs/ImageNet/PPLCNet/PPLCNet_x1_0.yaml b/ppcls/configs/ImageNet/PPLCNet/PPLCNet_x1_0.yaml index 0921db540..62aff5a47 100644 --- a/ppcls/configs/ImageNet/PPLCNet/PPLCNet_x1_0.yaml +++ b/ppcls/configs/ImageNet/PPLCNet/PPLCNet_x1_0.yaml @@ -4,7 +4,6 @@ Global: pretrained_model: null output_dir: ./output/ device: gpu - class_num: 1000 save_interval: 1 eval_during_train: True eval_interval: 1 @@ -17,6 +16,7 @@ Global: # model architecture Arch: name: PPLCNet_x1_0 + class_num: 1000 # loss function config for traing/eval process Loss: diff --git a/ppcls/configs/ImageNet/PPLCNet/PPLCNet_x1_5.yaml b/ppcls/configs/ImageNet/PPLCNet/PPLCNet_x1_5.yaml index 290d1c0e6..390286dfc 100644 --- a/ppcls/configs/ImageNet/PPLCNet/PPLCNet_x1_5.yaml +++ b/ppcls/configs/ImageNet/PPLCNet/PPLCNet_x1_5.yaml @@ -4,7 +4,6 @@ Global: pretrained_model: null output_dir: ./output/ device: gpu - class_num: 1000 save_interval: 1 eval_during_train: True eval_interval: 1 @@ -17,6 +16,7 @@ Global: # model architecture Arch: name: PPLCNet_x1_5 + class_num: 1000 # loss function config for traing/eval process Loss: diff --git a/ppcls/configs/ImageNet/PPLCNet/PPLCNet_x2_0.yaml b/ppcls/configs/ImageNet/PPLCNet/PPLCNet_x2_0.yaml index c8f7a3cdc..283e8afd9 100644 --- a/ppcls/configs/ImageNet/PPLCNet/PPLCNet_x2_0.yaml +++ b/ppcls/configs/ImageNet/PPLCNet/PPLCNet_x2_0.yaml @@ -4,7 +4,6 @@ Global: pretrained_model: null output_dir: ./output/ device: gpu - class_num: 1000 save_interval: 1 eval_during_train: True eval_interval: 1 @@ -17,7 +16,7 @@ Global: # model architecture Arch: name: PPLCNet_x2_0 - + class_num: 1000 # loss function config for traing/eval process Loss: Train: diff --git a/ppcls/configs/ImageNet/PPLCNet/PPLCNet_x2_5.yaml b/ppcls/configs/ImageNet/PPLCNet/PPLCNet_x2_5.yaml index 2ea69e30d..64efb3432 100644 --- a/ppcls/configs/ImageNet/PPLCNet/PPLCNet_x2_5.yaml +++ b/ppcls/configs/ImageNet/PPLCNet/PPLCNet_x2_5.yaml @@ -4,7 +4,6 @@ Global: pretrained_model: null output_dir: ./output/ device: gpu - class_num: 1000 save_interval: 1 eval_during_train: True eval_interval: 1 @@ -17,6 +16,7 @@ Global: # model architecture Arch: name: PPLCNet_x2_5 + class_num: 1000 # loss function config for traing/eval process Loss: diff --git a/ppcls/engine/engine.py b/ppcls/engine/engine.py index 7568c16c1..83a24a601 100644 --- a/ppcls/engine/engine.py +++ b/ppcls/engine/engine.py @@ -373,7 +373,7 @@ class Engine(object): "inference") if self.quanter: self.quanter.save_quantized_model( - model, + model.base_model, save_path, input_spec=[ paddle.static.InputSpec( diff --git a/test_tipc/config/PP-ShiTu/PPShiTu_linux_gpu_normal_normal_infer_cpp_linux_gpu_cpu.txt b/test_tipc/config/PP-ShiTu/PPShiTu_linux_gpu_normal_normal_infer_cpp_linux_gpu_cpu.txt new file mode 100644 index 000000000..b86aa9b1c --- /dev/null +++ b/test_tipc/config/PP-ShiTu/PPShiTu_linux_gpu_normal_normal_infer_cpp_linux_gpu_cpu.txt @@ -0,0 +1,19 @@ +===========================cpp_infer_params=========================== +model_name:PPShiTu +cpp_infer_type:shitu +feature_inference_model_dir:./feature_inference/ +det_inference_model_dir:./det_inference +cls_inference_url:https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/rec/models/inference/general_PPLCNet_x2_5_lite_v1.0_infer.tar +det_inference_url:https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/rec/models/inference/picodet_PPLCNet_x2_5_mainbody_lite_v1.0_infer.tar +infer_quant:False +inference_cmd:./deploy/cpp_shitu/build/pp_shitu -c inference_drink.yaml +use_gpu:True|False +enable_mkldnn:True|False +cpu_threads:1|6 +batch_size:1 +use_tensorrt:False|True +precision:fp32|fp16 +data_dir:./dataset/drink_dataset_v1.0 +benchmark:True +generate_yaml_cmd:python3 test_tipc/generate_cpp_yaml.py +transform_index_cmd:python3 deploy/cpp_shitu/tools/transform_id_map.py -c inference_drink.yaml diff --git a/test_tipc/config/ResNet/ResNet50_vd_linux_gpu_normal_normal_infer_cpp_linux_gpu_cpu.txt b/test_tipc/config/ResNet/ResNet50_vd_linux_gpu_normal_normal_infer_cpp_linux_gpu_cpu.txt new file mode 100644 index 000000000..51c73f13d --- /dev/null +++ b/test_tipc/config/ResNet/ResNet50_vd_linux_gpu_normal_normal_infer_cpp_linux_gpu_cpu.txt @@ -0,0 +1,18 @@ +===========================cpp_infer_params=========================== +model_name:ResNet50_vd +cpp_infer_type:cls +cls_inference_model_dir:./cls_inference/ +det_inference_model_dir: +cls_inference_url:https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/whole_chain/ResNet50_vd_inference.tar +det_inference_url: +infer_quant:False +inference_cmd:./deploy/cpp/build/clas_system -c inference_cls.yaml +use_gpu:True|False +enable_mkldnn:True|False +cpu_threads:1|6 +batch_size:1 +use_tensorrt:False|True +precision:fp32|fp16 +image_dir:./dataset/ILSVRC2012/val +benchmark:True +generate_yaml_cmd:python3 test_tipc/generate_cpp_yaml.py diff --git a/test_tipc/docs/compare_cpp_right.png b/test_tipc/docs/compare_cpp_right.png new file mode 100644 index 000000000..f9d0ba8ef Binary files /dev/null and b/test_tipc/docs/compare_cpp_right.png differ diff --git a/test_tipc/docs/compare_cpp_wrong.png b/test_tipc/docs/compare_cpp_wrong.png new file mode 100644 index 000000000..621d446bb Binary files /dev/null and b/test_tipc/docs/compare_cpp_wrong.png differ diff --git a/test_tipc/docs/test_inference_cpp.md b/test_tipc/docs/test_inference_cpp.md new file mode 100644 index 000000000..eabf8774d --- /dev/null +++ b/test_tipc/docs/test_inference_cpp.md @@ -0,0 +1,86 @@ +# C++预测功能测试 + +C++预测功能测试的主程序为`test_inference_cpp.sh`,可以测试基于C++预测库的模型推理功能。 + +## 1. 测试结论汇总 + +基于训练是否使用量化,进行本测试的模型可以分为`正常模型`和`量化模型`,这两类模型对应的C++预测功能汇总如下: + +| 模型类型 |device | batchsize | tensorrt | mkldnn | cpu多线程 | +| ---- | ---- | ---- | :----: | :----: | :----: | +| 正常模型 | GPU | 1/6 | fp32/fp16 | - | - | +| 正常模型 | CPU | 1/6 | - | fp32 | 支持 | +| 量化模型 | GPU | 1/6 | int8 | - | - | +| 量化模型 | CPU | 1/6 | - | int8 | 支持 | + +## 2. 测试流程 +运行环境配置请参考[文档](./install.md)的内容配置TIPC的运行环境。 + +### 2.1 功能测试 +先运行`prepare.sh`准备数据和模型,然后运行`test_inference_cpp.sh`进行测试,最终在```test_tipc/output```目录下生成`cpp_infer_*.log`后缀的日志文件。 + +```shell +bash test_tipc/prepare.sh test_tipc/config/ResNet/ResNet50_vd_linux_gpu_normal_normal_infer_cpp_linux_gpu_cpu.txt cpp_infer + +# 用法1: +bash test_tipc/test_inference_cpp.sh test_tipc/config/ResNet/ResNet50_vd_linux_gpu_normal_normal_infer_cpp_linux_gpu_cpu.txt +# 用法2: 指定GPU卡预测,第三个传入参数为GPU卡号 +bash test_tipc/test_inference_cpp.sh test_tipc/config/ResNet/ResNet50_vd_linux_gpu_normal_normal_infer_cpp_linux_gpu_cpu.txt 1 +``` + +运行预测指令后,在`test_tipc/output`文件夹下自动会保存运行日志,包括以下文件: + +```shell +test_tipc/output/ +|- results_cpp.log # 运行指令状态的日志 +|- cls_cpp_infer_cpu_usemkldnn_False_threads_1_precision_fp32_batchsize_1.log # CPU上不开启Mkldnn,线程数设置为1,测试batch_size=1条件下的预测运行日志 +|- cls_cpp_infer_cpu_usemkldnn_False_threads_6_precision_fp32_batchsize_1.log # CPU上不开启Mkldnn,线程数设置为6,测试batch_size=1条件下的预测运行日志 +|- cls_cpp_infer_gpu_usetrt_False_precision_fp32_batchsize_1.log # GPU上不开启TensorRT,测试batch_size=1的fp32精度预测日志 +|- cls_cpp_infer_gpu_usetrt_True_precision_fp16_batchsize_1.log # GPU上开启TensorRT,测试batch_size=1的fp16精度预测日志 +...... +``` +其中results_cpp.log中包含了每条指令的运行状态,如果运行成功会输出: + +``` +Run successfully with command - ./deploy/cpp/build/clas_system -c inference_cls.yaml 2>&1|tee test_tipc/output/cls_cpp_infer_gpu_usetrt_False_precision_fp32_batchsize_1.log +...... +``` +如果运行失败,会输出: +``` +Run failed with command - ./deploy/cpp/build/clas_system -c inference_cls.yaml 2>&1|tee test_tipc/output/cls_cpp_infer_gpu_usetrt_False_precision_fp32_batchsize_1.log +...... +``` +可以很方便的根据results_cpp.log中的内容判定哪一个指令运行错误。 + + +### 2.2 精度测试 + +使用compare_results.py脚本比较模型预测的结果是否符合预期,主要步骤包括: +- 提取日志中的预测坐标; +- 从本地文件中提取保存好的坐标结果; +- 比较上述两个结果是否符合精度预期,误差大于设置阈值时会报错。 + +#### 使用方式 +运行命令: +```shell +python3.7 test_tipc/compare_results.py --gt_file=./test_tipc/results/cls_cpp_*.txt --log_file=./test_tipc/output/cls_cpp_*.log --atol=1e-3 --rtol=1e-3 +``` + +参数介绍: +- gt_file: 指向事先保存好的预测结果路径,支持*.txt 结尾,会自动索引*.txt格式的文件,文件默认保存在test_tipc/result/ 文件夹下 +- log_file: 指向运行test_tipc/test_inference_cpp.sh 脚本的infer模式保存的预测日志,预测日志中打印的有预测结果,比如:文本框,预测文本,类别等等,同样支持cpp_infer_*.log格式传入 +- atol: 设置的绝对误差 +- rtol: 设置的相对误差 + +#### 运行结果 + +正常运行效果如下图: + + +出现不一致结果时的运行输出: + + + +## 3. 更多教程 + +本文档为功能测试用,更详细的c++预测使用教程请参考:[服务器端C++预测](../../docs/zh_CN/inference_deployment/) diff --git a/test_tipc/generate_cpp_yaml.py b/test_tipc/generate_cpp_yaml.py new file mode 100644 index 000000000..2e541de33 --- /dev/null +++ b/test_tipc/generate_cpp_yaml.py @@ -0,0 +1,80 @@ +import os +import yaml +import argparse + + +def str2bool(v): + if v.lower() == 'true': + return True + else: + return False + + +def parse_args(): + parser = argparse.ArgumentParser() + parser.add_argument('--type', required=True, choices=["cls", "shitu"]) + parser.add_argument('--batch_size', type=int, default=1) + parser.add_argument('--mkldnn', type=str2bool, default=True) + parser.add_argument('--gpu', type=str2bool, default=False) + parser.add_argument('--cpu_thread', type=int, default=1) + parser.add_argument('--tensorrt', type=str2bool, default=False) + parser.add_argument('--precision', type=str, choices=["fp32", "fp16"]) + parser.add_argument('--benchmark', type=str2bool, default=True) + parser.add_argument('--gpu_id', type=int, default=0) + parser.add_argument( + '--cls_yaml_path', + type=str, + default="deploy/configs/inference_cls.yaml") + parser.add_argument( + '--shitu_yaml_path', + type=str, + default="deploy/configs/inference_drink.yaml") + parser.add_argument('--data_dir', type=str, required=True) + parser.add_argument('--save_path', type=str, default='./') + parser.add_argument('--cls_model_dir', type=str) + parser.add_argument('--det_model_dir', type=str) + args = parser.parse_args() + return args + + +def main(): + args = parse_args() + if args.type == "cls": + save_path = os.path.join(args.save_path, + os.path.basename(args.cls_yaml_path)) + fd = open(args.cls_yaml_path) + else: + save_path = os.path.join(args.save_path, + os.path.basename(args.shitu_yaml_path)) + fd = open(args.shitu_yaml_path) + config = yaml.load(fd, yaml.FullLoader) + fd.close() + + config["Global"]["batch_size"] = args.batch_size + config["Global"]["use_gpu"] = args.gpu + config["Global"]["enable_mkldnn"] = args.mkldnn + config["Global"]["benchmark"] = args.benchmark + config["Global"]["use_tensorrt"] = args.tensorrt + config["Global"]["use_fp16"] = True if args.precision == "fp16" else False + config["Global"]["gpu_id"] = args.gpu_id + if args.type == "cls": + config["Global"]["infer_imgs"] = args.data_dir + assert args.cls_model_dir + config["Global"]["inference_model_dir"] = args.cls_model_dir + else: + config["Global"]["infer_imgs"] = os.path.join(args.data_dir, + "test_images") + config["IndexProcess"]["index_dir"] = os.path.join(args.data_dir, + "index") + assert args.cls_model_dir + assert args.det_model_dir + config["Global"]["det_inference_model_dir"] = args.det_model_dir + config["Global"]["rec_inference_model_dir"] = args.cls_model_dir + + with open(save_path, 'w') as fd: + yaml.dump(config, fd) + print("Generate new yaml done") + + +if __name__ == "__main__": + main() diff --git a/test_tipc/prepare.sh b/test_tipc/prepare.sh index db9b4fdad..675ef92d9 100644 --- a/test_tipc/prepare.sh +++ b/test_tipc/prepare.sh @@ -33,6 +33,59 @@ function func_parser_value(){ fi } +function func_get_url_file_name(){ + strs=$1 + IFS="/" + array=(${strs}) + tmp=${array[${#array[@]}-1]} + echo ${tmp} +} + +model_name=$(func_parser_value "${lines[1]}") + +if [ ${MODE} = "cpp_infer" ];then + if [[ $FILENAME == *infer_cpp_linux_gpu_cpu.txt ]];then + cpp_type=$(func_parser_value "${lines[2]}") + cls_inference_model_dir=$(func_parser_value "${lines[3]}") + det_inference_model_dir=$(func_parser_value "${lines[4]}") + cls_inference_url=$(func_parser_value "${lines[5]}") + det_inference_url=$(func_parser_value "${lines[6]}") + + if [[ $cpp_type == "cls" ]];then + eval "wget -nc $cls_inference_url" + tar xf "${model_name}_inference.tar" + eval "mv inference $cls_inference_model_dir" + cd dataset + rm -rf ILSVRC2012 + wget -nc https://paddle-imagenet-models-name.bj.bcebos.com/data/whole_chain/whole_chain_infer.tar + tar xf whole_chain_infer.tar + ln -s whole_chain_infer ILSVRC2012 + cd .. + elif [[ $cpp_type == "shitu" ]];then + eval "wget -nc $cls_inference_url" + tar_name=$(func_get_url_file_name "$cls_inference_url") + model_dir=${tar_name%.*} + eval "tar xf ${tar_name}" + eval "mv ${model_dir} ${cls_inference_model_dir}" + + eval "wget -nc $det_inference_url" + tar_name=$(func_get_url_file_name "$det_inference_url") + model_dir=${tar_name%.*} + eval "tar xf ${tar_name}" + eval "mv ${model_dir} ${det_inference_model_dir}" + cd dataset + wget -nc https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/rec/data/drink_dataset_v1.0.tar + tar -xf drink_dataset_v1.0.tar + else + echo "Wrong cpp type in config file in line 3. only support cls, shitu" + fi + exit 0 + else + echo "use wrong config file" + exit 1 + fi +fi + model_name=$(func_parser_value "${lines[1]}") model_url_value=$(func_parser_value "${lines[35]}") model_url_key=$(func_parser_key "${lines[35]}") @@ -114,63 +167,3 @@ if [ ${MODE} = "serving_infer" ];then cd ./deploy/paddleserving wget -nc https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/inference/ResNet50_vd_infer.tar && tar xf ResNet50_vd_infer.tar fi - -if [ ${MODE} = "cpp_infer" ];then - cd deploy/cpp - echo "################### build opencv ###################" - rm -rf 3.4.7.tar.gz opencv-3.4.7/ - wget https://github.com/opencv/opencv/archive/3.4.7.tar.gz - tar -xf 3.4.7.tar.gz - install_path=$(pwd)/opencv-3.4.7/opencv3 - cd opencv-3.4.7/ - - rm -rf build - mkdir build - cd build - cmake .. \ - -DCMAKE_INSTALL_PREFIX=${install_path} \ - -DCMAKE_BUILD_TYPE=Release \ - -DBUILD_SHARED_LIBS=OFF \ - -DWITH_IPP=OFF \ - -DBUILD_IPP_IW=OFF \ - -DWITH_LAPACK=OFF \ - -DWITH_EIGEN=OFF \ - -DCMAKE_INSTALL_LIBDIR=lib64 \ - -DWITH_ZLIB=ON \ - -DBUILD_ZLIB=ON \ - -DWITH_JPEG=ON \ - -DBUILD_JPEG=ON \ - -DWITH_PNG=ON \ - -DBUILD_PNG=ON \ - -DWITH_TIFF=ON \ - -DBUILD_TIFF=ON - make -j - make install - cd ../../ - echo "################### build opencv finished ###################" - - echo "################### build PaddleClas demo ####################" - OPENCV_DIR=$(pwd)/opencv-3.4.7/opencv3/ - LIB_DIR=$(pwd)/Paddle/build/paddle_inference_install_dir/ - CUDA_LIB_DIR=$(dirname `find /usr -name libcudart.so`) - CUDNN_LIB_DIR=$(dirname `find /usr -name libcudnn.so`) - - BUILD_DIR=build - rm -rf ${BUILD_DIR} - mkdir ${BUILD_DIR} - cd ${BUILD_DIR} - cmake .. \ - -DPADDLE_LIB=${LIB_DIR} \ - -DWITH_MKL=ON \ - -DDEMO_NAME=clas_system \ - -DWITH_GPU=OFF \ - -DWITH_STATIC_LIB=OFF \ - -DWITH_TENSORRT=OFF \ - -DTENSORRT_DIR=${TENSORRT_DIR} \ - -DOPENCV_DIR=${OPENCV_DIR} \ - -DCUDNN_LIB=${CUDNN_LIB_DIR} \ - -DCUDA_LIB=${CUDA_LIB_DIR} \ - - make -j - echo "################### build PaddleClas demo finished ###################" -fi diff --git a/test_tipc/test_inference_cpp.sh b/test_tipc/test_inference_cpp.sh index d26954353..129e43956 100644 --- a/test_tipc/test_inference_cpp.sh +++ b/test_tipc/test_inference_cpp.sh @@ -2,6 +2,10 @@ source test_tipc/common_func.sh FILENAME=$1 +GPUID=$2 +if [[ ! $GPUID ]];then + GPUID=0 +fi dataline=$(awk 'NR==1, NR==16{print}' $FILENAME) # parser params @@ -10,37 +14,29 @@ lines=(${dataline}) # parser cpp inference model model_name=$(func_parser_value "${lines[1]}") -use_opencv=$(func_parser_value "${lines[2]}") -cpp_infer_model_dir_list=$(func_parser_value "${lines[3]}") -cpp_infer_is_quant=$(func_parser_value "${lines[4]}") +cpp_infer_type=$(func_parser_value "${lines[2]}") +cpp_infer_model_dir=$(func_parser_value "${lines[3]}") +cpp_det_infer_model_dir=$(func_parser_value "${lines[4]}") +cpp_infer_is_quant=$(func_parser_value "${lines[7]}") # parser cpp inference -inference_cmd=$(func_parser_value "${lines[5]}") -cpp_use_gpu_key=$(func_parser_key "${lines[6]}") -cpp_use_gpu_list=$(func_parser_value "${lines[6]}") -cpp_use_mkldnn_key=$(func_parser_key "${lines[7]}") -cpp_use_mkldnn_list=$(func_parser_value "${lines[7]}") -cpp_cpu_threads_key=$(func_parser_key "${lines[8]}") -cpp_cpu_threads_list=$(func_parser_value "${lines[8]}") -cpp_batch_size_key=$(func_parser_key "${lines[9]}") -cpp_batch_size_list=$(func_parser_value "${lines[9]}") -cpp_use_trt_key=$(func_parser_key "${lines[10]}") -cpp_use_trt_list=$(func_parser_value "${lines[10]}") -cpp_precision_key=$(func_parser_key "${lines[11]}") -cpp_precision_list=$(func_parser_value "${lines[11]}") -cpp_infer_model_key=$(func_parser_key "${lines[12]}") -cpp_image_dir_key=$(func_parser_key "${lines[13]}") -cpp_infer_img_dir=$(func_parser_value "${lines[13]}") -cpp_infer_key1=$(func_parser_key "${lines[14]}") -cpp_infer_value1=$(func_parser_value "${lines[14]}") -cpp_benchmark_key=$(func_parser_key "${lines[15]}") -cpp_benchmark_value=$(func_parser_value "${lines[15]}") +inference_cmd=$(func_parser_value "${lines[8]}") +cpp_use_gpu_list=$(func_parser_value "${lines[9]}") +cpp_use_mkldnn_list=$(func_parser_value "${lines[10]}") +cpp_cpu_threads_list=$(func_parser_value "${lines[11]}") +cpp_batch_size_list=$(func_parser_value "${lines[12]}") +cpp_use_trt_list=$(func_parser_value "${lines[13]}") +cpp_precision_list=$(func_parser_value "${lines[14]}") +cpp_image_dir_value=$(func_parser_value "${lines[15]}") +cpp_benchmark_value=$(func_parser_value "${lines[16]}") +generate_yaml_cmd=$(func_parser_value "${lines[17]}") +transform_index_cmd=$(func_parser_value "${lines[18]}") LOG_PATH="./test_tipc/output" mkdir -p ${LOG_PATH} status_log="${LOG_PATH}/results_cpp.log" +# generate_yaml_cmd="python3 test_tipc/generate_cpp_yaml.py" - -function func_cpp_inference(){ +function func_shitu_cpp_inference(){ IFS='|' _script=$1 _model_dir=$2 @@ -48,6 +44,7 @@ function func_cpp_inference(){ _img_dir=$4 _flag_quant=$5 # inference + for use_gpu in ${cpp_use_gpu_list[*]}; do if [ ${use_gpu} = "False" ] || [ ${use_gpu} = "cpu" ]; then for use_mkldnn in ${cpp_use_mkldnn_list[*]}; do @@ -60,17 +57,14 @@ function func_cpp_inference(){ if [ ${use_mkldnn} = "False" ] && [ ${_flag_quant} = "True" ]; then precison="int8" fi - _save_log_path="${_log_path}/cpp_infer_cpu_usemkldnn_${use_mkldnn}_threads_${threads}_precision_${precision}_batchsize_${batch_size}.log" - set_infer_data=$(func_set_params "${cpp_image_dir_key}" "${_img_dir}") - set_benchmark=$(func_set_params "${cpp_benchmark_key}" "${cpp_benchmark_value}") - set_batchsize=$(func_set_params "${cpp_batch_size_key}" "${batch_size}") - set_cpu_threads=$(func_set_params "${cpp_cpu_threads_key}" "${threads}") - set_model_dir=$(func_set_params "${cpp_infer_model_key}" "${_model_dir}") - set_infer_params1=$(func_set_params "${cpp_infer_key1}" "${cpp_infer_value1}") - command="${_script} ${cpp_use_gpu_key}=${use_gpu} ${cpp_use_mkldnn_key}=${use_mkldnn} ${set_cpu_threads} ${set_model_dir} ${set_batchsize} ${set_infer_data} ${set_benchmark} ${set_infer_params1} > ${_save_log_path} 2>&1 " - eval $command + _save_log_path="${_log_path}/shitu_cpp_infer_cpu_usemkldnn_${use_mkldnn}_threads_${threads}_precision_${precision}_batchsize_${batch_size}.log" + + command="${generate_yaml_cmd} --type shitu --batch_size ${batch_size} --mkldnn ${use_mkldnn} --gpu ${use_gpu} --cpu_thread ${threads} --tensorrt False --precision ${precision} --data_dir ${_img_dir} --benchmark True --cls_model_dir ${cpp_infer_model_dir} --det_model_dir ${cpp_det_infer_model_dir} --gpu_id ${GPUID}" + eval $command + eval $transform_index_cmd + command="${_script} 2>&1|tee ${_save_log_path}" + eval $command last_status=${PIPESTATUS[0]} - eval "cat ${_save_log_path}" status_check $last_status "${command}" "${status_log}" done done @@ -88,20 +82,75 @@ function func_cpp_inference(){ continue fi for batch_size in ${cpp_batch_size_list[*]}; do - _save_log_path="${_log_path}/cpp_infer_gpu_usetrt_${use_trt}_precision_${precision}_batchsize_${batch_size}.log" - set_infer_data=$(func_set_params "${cpp_image_dir_key}" "${_img_dir}") - set_benchmark=$(func_set_params "${cpp_benchmark_key}" "${cpp_benchmark_value}") - set_batchsize=$(func_set_params "${cpp_batch_size_key}" "${batch_size}") - set_tensorrt=$(func_set_params "${cpp_use_trt_key}" "${use_trt}") - set_precision=$(func_set_params "${cpp_precision_key}" "${precision}") - set_model_dir=$(func_set_params "${cpp_infer_model_key}" "${_model_dir}") - set_infer_params1=$(func_set_params "${cpp_infer_key1}" "${cpp_infer_value1}") - command="${_script} ${cpp_use_gpu_key}=${use_gpu} ${set_tensorrt} ${set_precision} ${set_model_dir} ${set_batchsize} ${set_infer_data} ${set_benchmark} ${set_infer_params1} > ${_save_log_path} 2>&1 " + _save_log_path="${_log_path}/shitu_cpp_infer_gpu_usetrt_${use_trt}_precision_${precision}_batchsize_${batch_size}.log" + command="${generate_yaml_cmd} --type shitu --batch_size ${batch_size} --mkldnn False --gpu ${use_gpu} --cpu_thread 1 --tensorrt ${use_trt} --precision ${precision} --data_dir ${_img_dir} --benchmark True --cls_model_dir ${cpp_infer_model_dir} --det_model_dir ${cpp_det_infer_model_dir} --gpu_id ${GPUID}" eval $command + eval $transform_index_cmd + command="${_script} 2>&1|tee ${_save_log_path}" + eval $command + last_status=${PIPESTATUS[0]} + status_check $last_status "${_script}" "${status_log}" + done + done + done + else + echo "Does not support hardware other than CPU and GPU Currently!" + fi + done +} + +function func_cls_cpp_inference(){ + IFS='|' + _script=$1 + _model_dir=$2 + _log_path=$3 + _img_dir=$4 + _flag_quant=$5 + # inference + + for use_gpu in ${cpp_use_gpu_list[*]}; do + if [ ${use_gpu} = "False" ] || [ ${use_gpu} = "cpu" ]; then + for use_mkldnn in ${cpp_use_mkldnn_list[*]}; do + if [ ${use_mkldnn} = "False" ] && [ ${_flag_quant} = "True" ]; then + continue + fi + for threads in ${cpp_cpu_threads_list[*]}; do + for batch_size in ${cpp_batch_size_list[*]}; do + precision="fp32" + if [ ${use_mkldnn} = "False" ] && [ ${_flag_quant} = "True" ]; then + precison="int8" + fi + _save_log_path="${_log_path}/cls_cpp_infer_cpu_usemkldnn_${use_mkldnn}_threads_${threads}_precision_${precision}_batchsize_${batch_size}.log" + + command="${generate_yaml_cmd} --type cls --batch_size ${batch_size} --mkldnn ${use_mkldnn} --gpu ${use_gpu} --cpu_thread ${threads} --tensorrt False --precision ${precision} --data_dir ${_img_dir} --benchmark True --cls_model_dir ${cpp_infer_model_dir} --gpu_id ${GPUID}" + eval $command + command1="${_script} 2>&1|tee ${_save_log_path}" + eval ${command1} + last_status=${PIPESTATUS[0]} + status_check $last_status "${command1}" "${status_log}" + done + done + done + elif [ ${use_gpu} = "True" ] || [ ${use_gpu} = "gpu" ]; then + for use_trt in ${cpp_use_trt_list[*]}; do + for precision in ${cpp_precision_list[*]}; do + if [[ ${_flag_quant} = "False" ]] && [[ ${precision} =~ "int8" ]]; then + continue + fi + if [[ ${precision} =~ "fp16" || ${precision} =~ "int8" ]] && [ ${use_trt} = "False" ]; then + continue + fi + if [[ ${use_trt} = "False" || ${precision} =~ "int8" ]] && [ ${_flag_quant} = "True" ]; then + continue + fi + for batch_size in ${cpp_batch_size_list[*]}; do + _save_log_path="${_log_path}/cls_cpp_infer_gpu_usetrt_${use_trt}_precision_${precision}_batchsize_${batch_size}.log" + command="${generate_yaml_cmd} --type cls --batch_size ${batch_size} --mkldnn False --gpu ${use_gpu} --cpu_thread 1 --tensorrt ${use_trt} --precision ${precision} --data_dir ${_img_dir} --benchmark True --cls_model_dir ${cpp_infer_model_dir} --gpu_id ${GPUID}" + eval $command + command="${_script} 2>&1|tee ${_save_log_path}" + eval $command last_status=${PIPESTATUS[0]} - eval "cat ${_save_log_path}" status_check $last_status "${command}" "${status_log}" - done done done @@ -112,24 +161,56 @@ function func_cpp_inference(){ } -cd deploy/cpp_infer -if [ ${use_opencv} = "True" ]; then - if [ -d "opencv-3.4.7/opencv3/" ] && [ $(md5sum opencv-3.4.7.tar.gz | awk -F ' ' '{print $1}') = "faa2b5950f8bee3f03118e600c74746a" ];then - echo "################### build opencv skipped ###################" - else - echo "################### build opencv ###################" - rm -rf opencv-3.4.7.tar.gz opencv-3.4.7/ - wget https://paddleocr.bj.bcebos.com/dygraph_v2.0/test/opencv-3.4.7.tar.gz - tar -xf opencv-3.4.7.tar.gz +if [[ $cpp_infer_type == "cls" ]]; then + cd deploy/cpp +elif [[ $cpp_infer_type == "shitu" ]]; then + cd deploy/cpp_shitu +else + echo "Only support cls and shitu" + exit 0 +fi - cd opencv-3.4.7/ - install_path=$(pwd)/opencv3 +if [[ $cpp_infer_type == "shitu" ]]; then + echo "################### update cmake ###################" + wget -nc https://github.com/Kitware/CMake/releases/download/v3.22.0/cmake-3.22.0.tar.gz + tar xf cmake-3.22.0.tar.gz + cd ./cmake-3.22.0 + export root_path=$PWD + export install_path=${root_path}/cmake + eval "./bootstrap --prefix=${install_path}" + make -j + make install + export PATH=${install_path}/bin:$PATH + cd .. + echo "################### update cmake done ###################" - rm -rf build - mkdir build - cd build + echo "################### build faiss ###################" + apt-get install -y libopenblas-dev + git clone https://github.com/facebookresearch/faiss.git + cd faiss + export faiss_install_path=$PWD/faiss_install + eval "cmake -B build . -DFAISS_ENABLE_PYTHON=OFF -DCMAKE_INSTALL_PREFIX=${faiss_install_path}" + make -C build -j faiss + make -C build install + cd .. +fi - cmake .. \ +if [ -d "opencv-3.4.7/opencv3/" ] && [ $(md5sum opencv-3.4.7.tar.gz | awk -F ' ' '{print $1}') = "faa2b5950f8bee3f03118e600c74746a" ];then + echo "################### build opencv skipped ###################" +else + echo "################### build opencv ###################" + rm -rf opencv-3.4.7.tar.gz opencv-3.4.7/ + wget https://paddleocr.bj.bcebos.com/dygraph_v2.0/test/opencv-3.4.7.tar.gz + tar -xf opencv-3.4.7.tar.gz + + cd opencv-3.4.7/ + install_path=$(pwd)/opencv3 + + rm -rf build + mkdir build + cd build + + cmake .. \ -DCMAKE_INSTALL_PREFIX=${install_path} \ -DCMAKE_BUILD_TYPE=Release \ -DBUILD_SHARED_LIBS=OFF \ @@ -147,20 +228,15 @@ if [ ${use_opencv} = "True" ]; then -DWITH_TIFF=ON \ -DBUILD_TIFF=ON - make -j - make install - cd ../ - echo "################### build opencv finished ###################" - fi + make -j + make install + cd ../../ + echo "################### build opencv finished ###################" fi - -echo "################### build PaddleOCR demo ####################" -if [ ${use_opencv} = "True" ]; then - OPENCV_DIR=$(pwd)/opencv-3.4.7/opencv3/ -else - OPENCV_DIR='' -fi +echo "################### build PaddleClas demo ####################" +OPENCV_DIR=$(pwd)/opencv-3.4.7/opencv3/ +# LIB_DIR=/work/project/project/test/paddle_inference/ LIB_DIR=$(pwd)/Paddle/build/paddle_inference_install_dir/ CUDA_LIB_DIR=$(dirname `find /usr -name libcudart.so`) CUDNN_LIB_DIR=$(dirname `find /usr -name libcudnn.so`) @@ -169,40 +245,59 @@ BUILD_DIR=build rm -rf ${BUILD_DIR} mkdir ${BUILD_DIR} cd ${BUILD_DIR} -cmake .. \ - -DPADDLE_LIB=${LIB_DIR} \ - -DWITH_MKL=ON \ - -DWITH_GPU=OFF \ - -DWITH_STATIC_LIB=OFF \ - -DWITH_TENSORRT=OFF \ - -DOPENCV_DIR=${OPENCV_DIR} \ - -DCUDNN_LIB=${CUDNN_LIB_DIR} \ - -DCUDA_LIB=${CUDA_LIB_DIR} \ - -DTENSORRT_DIR=${TENSORRT_DIR} \ - +if [[ $cpp_infer_type == cls ]]; then + cmake .. \ + -DPADDLE_LIB=${LIB_DIR} \ + -DWITH_MKL=ON \ + -DWITH_GPU=ON \ + -DWITH_STATIC_LIB=OFF \ + -DWITH_TENSORRT=OFF \ + -DOPENCV_DIR=${OPENCV_DIR} \ + -DCUDNN_LIB=${CUDNN_LIB_DIR} \ + -DCUDA_LIB=${CUDA_LIB_DIR} \ + -DTENSORRT_DIR=${TENSORRT_DIR} +else + cmake ..\ + -DPADDLE_LIB=${LIB_DIR} \ + -DWITH_MKL=ON \ + -DWITH_GPU=ON \ + -DWITH_STATIC_LIB=OFF \ + -DWITH_TENSORRT=OFF \ + -DOPENCV_DIR=${OPENCV_DIR} \ + -DCUDNN_LIB=${CUDNN_LIB_DIR} \ + -DCUDA_LIB=${CUDA_LIB_DIR} \ + -DTENSORRT_DIR=${TENSORRT_DIR} \ + -DFAISS_DIR=${faiss_install_path} \ + -DFAISS_WITH_MKL=OFF +fi make -j cd ../../../ -echo "################### build PaddleOCR demo finished ###################" +# cd ../../ +echo "################### build PaddleClas demo finished ###################" # set cuda device -GPUID=$2 -if [ ${#GPUID} -le 0 ];then - env=" " -else - env="export CUDA_VISIBLE_DEVICES=${GPUID}" -fi -set CUDA_VISIBLE_DEVICES -eval $env +# GPUID=$2 +# if [ ${#GPUID} -le 0 ];then +# env="export CUDA_VISIBLE_DEVICES=0" +# else +# env="export CUDA_VISIBLE_DEVICES=${GPUID}" +# fi +# set CUDA_VISIBLE_DEVICES +# eval $env echo "################### run test ###################" export Count=0 IFS="|" infer_quant_flag=(${cpp_infer_is_quant}) -for infer_model in ${cpp_infer_model_dir_list[*]}; do +for infer_model in ${cpp_infer_model_dir[*]}; do #run inference is_quant=${infer_quant_flag[Count]} - func_cpp_inference "${inference_cmd}" "${infer_model}" "${LOG_PATH}" "${cpp_infer_img_dir}" ${is_quant} + if [[ $cpp_infer_type == "cls" ]]; then + func_cls_cpp_inference "${inference_cmd}" "${infer_model}" "${LOG_PATH}" "${cpp_image_dir_value}" ${is_quant} + else + func_shitu_cpp_inference "${inference_cmd}" "${infer_model}" "${LOG_PATH}" "${cpp_image_dir_value}" ${is_quant} + fi Count=$(($Count + 1)) done