From 3c7f6e4d5c16cc154c41270e28d3960552d8cb46 Mon Sep 17 00:00:00 2001 From: dongshuilong Date: Tue, 31 Aug 2021 12:52:17 +0000 Subject: [PATCH 1/2] add cpp whole_chain test --- deploy/cpp/CMakeLists.txt | 6 +- deploy/cpp/external-cmake/auto-log.cmake | 12 +++ deploy/cpp/include/cls.h | 2 +- deploy/cpp/include/cls_config.h | 10 ++- deploy/cpp/src/cls.cpp | 30 +++++--- deploy/cpp/src/main.cpp | 19 ++++- deploy/cpp/tools/build.sh | 4 +- deploy/cpp/tools/config.txt | 5 +- tests/{ => config}/DarkNet53.txt | 0 tests/{ => config}/HRNet_W18_C.txt | 0 tests/{ => config}/LeViT_128S.txt | 0 tests/{ => config}/MobileNetV1.txt | 0 tests/{ => config}/MobileNetV2.txt | 0 tests/{ => config}/MobileNetV3_large_x1_0.txt | 0 tests/{ => config}/ResNeXt101_vd_64x4d.txt | 0 tests/{ => config}/ResNet50_vd.txt | 7 ++ tests/{ => config}/ShuffleNetV2_x1_0.txt | 0 ...winTransformer_tiny_patch4_window7_224.txt | 0 tests/config/cpp_config.txt | 19 +++++ tests/cpp_config.txt | 24 ++++++ tests/prepare.sh | 62 +++++++++++++++- tests/test.sh | 74 ++++++++++++++++++- 22 files changed, 250 insertions(+), 24 deletions(-) create mode 100644 deploy/cpp/external-cmake/auto-log.cmake rename tests/{ => config}/DarkNet53.txt (100%) rename tests/{ => config}/HRNet_W18_C.txt (100%) rename tests/{ => config}/LeViT_128S.txt (100%) rename tests/{ => config}/MobileNetV1.txt (100%) rename tests/{ => config}/MobileNetV2.txt (100%) rename tests/{ => config}/MobileNetV3_large_x1_0.txt (100%) rename tests/{ => config}/ResNeXt101_vd_64x4d.txt (100%) rename tests/{ => config}/ResNet50_vd.txt (92%) rename tests/{ => config}/ShuffleNetV2_x1_0.txt (100%) rename tests/{ => config}/SwinTransformer_tiny_patch4_window7_224.txt (100%) create mode 100755 tests/config/cpp_config.txt create mode 100755 tests/cpp_config.txt diff --git a/deploy/cpp/CMakeLists.txt b/deploy/cpp/CMakeLists.txt index 4f148869f..4b11eec62 100755 --- a/deploy/cpp/CMakeLists.txt +++ b/deploy/cpp/CMakeLists.txt @@ -1,4 +1,5 @@ project(clas_system CXX C) +cmake_minimum_required(VERSION 3.14) option(WITH_MKL "Compile demo with MKL/OpenBlas support, default use MKL." ON) option(WITH_GPU "Compile demo with GPU/CPU, default use CPU." OFF) @@ -13,7 +14,6 @@ SET(TENSORRT_DIR "" CACHE PATH "Compile demo with TensorRT") set(DEMO_NAME "clas_system") - macro(safe_set_static_flag) foreach(flag_var CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_DEBUG CMAKE_CXX_FLAGS_RELEASE @@ -198,6 +198,10 @@ endif() set(DEPS ${DEPS} ${OpenCV_LIBS}) +include(FetchContent) +include(external-cmake/auto-log.cmake) +include_directories(${FETCHCONTENT_BASE_DIR}/extern_autolog-src) + AUX_SOURCE_DIRECTORY(./src SRCS) add_executable(${DEMO_NAME} ${SRCS}) diff --git a/deploy/cpp/external-cmake/auto-log.cmake b/deploy/cpp/external-cmake/auto-log.cmake new file mode 100644 index 000000000..9be9c2fb3 --- /dev/null +++ b/deploy/cpp/external-cmake/auto-log.cmake @@ -0,0 +1,12 @@ +find_package(Git REQUIRED) +include(FetchContent) + +set(FETCHCONTENT_BASE_DIR "${CMAKE_CURRENT_BINARY_DIR}/third-party") + +FetchContent_Declare( + extern_Autolog + PREFIX autolog + GIT_REPOSITORY https://github.com/LDOUBLEV/AutoLog.git + GIT_TAG main +) +FetchContent_MakeAvailable(extern_Autolog) diff --git a/deploy/cpp/include/cls.h b/deploy/cpp/include/cls.h index 3cc5dcc16..600cffbf1 100644 --- a/deploy/cpp/include/cls.h +++ b/deploy/cpp/include/cls.h @@ -61,7 +61,7 @@ public: void LoadModel(const std::string &model_path, const std::string ¶ms_path); // Run predictor - double Run(cv::Mat &img); + double Run(cv::Mat &img, std::vector *times); private: std::shared_ptr predictor_; diff --git a/deploy/cpp/include/cls_config.h b/deploy/cpp/include/cls_config.h index 7377573c3..d74bb7b4d 100644 --- a/deploy/cpp/include/cls_config.h +++ b/deploy/cpp/include/cls_config.h @@ -1,4 +1,4 @@ -// Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved. +// Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -36,8 +36,7 @@ public: this->gpu_mem = stoi(config_map_["gpu_mem"]); - this->cpu_math_library_num_threads = - stoi(config_map_["cpu_math_library_num_threads"]); + this->cpu_threads = stoi(config_map_["cpu_threads"]); this->use_mkldnn = bool(stoi(config_map_["use_mkldnn"])); @@ -51,6 +50,8 @@ public: this->resize_short_size = stoi(config_map_["resize_short_size"]); this->crop_size = stoi(config_map_["crop_size"]); + + this->benchmark = bool(stoi(config_map_["benchmark"])); } bool use_gpu = false; @@ -59,12 +60,13 @@ public: int gpu_mem = 4000; - int cpu_math_library_num_threads = 1; + int cpu_threads = 1; bool use_mkldnn = false; bool use_tensorrt = false; bool use_fp16 = false; + bool benchmark = false; std::string cls_model_path; diff --git a/deploy/cpp/src/cls.cpp b/deploy/cpp/src/cls.cpp index d0fa21f7d..7a141639d 100644 --- a/deploy/cpp/src/cls.cpp +++ b/deploy/cpp/src/cls.cpp @@ -12,7 +12,6 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include #include namespace PaddleClas { @@ -53,11 +52,12 @@ void Classifier::LoadModel(const std::string &model_path, this->predictor_ = CreatePredictor(config); } -double Classifier::Run(cv::Mat &img) { +double Classifier::Run(cv::Mat &img, std::vector *times) { cv::Mat srcimg; cv::Mat resize_img; img.copyTo(srcimg); + auto preprocess_start = std::chrono::steady_clock::now(); this->resize_op_.Run(img, resize_img, this->resize_short_size_); this->crop_op_.Run(resize_img, this->crop_size_); @@ -70,7 +70,9 @@ double Classifier::Run(cv::Mat &img) { 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 start = std::chrono::system_clock::now(); + auto preprocess_end = std::chrono::system_clock::now(); + + auto infer_start = std::chrono::system_clock::now(); input_t->CopyFromCpu(input.data()); this->predictor_->Run(); @@ -83,21 +85,29 @@ double Classifier::Run(cv::Mat &img) { out_data.resize(out_num); output_t->CopyToCpu(out_data.data()); - auto end = std::chrono::system_clock::now(); - auto duration = - std::chrono::duration_cast(end - start); - double cost_time = double(duration.count()) * - std::chrono::microseconds::period::num / - std::chrono::microseconds::period::den; + auto infer_end = std::chrono::system_clock::now(); + auto postprocess_start = std::chrono::system_clock::now(); int maxPosition = max_element(out_data.begin(), out_data.end()) - out_data.begin(); + auto postprocess_end = std::chrono::system_clock::now(); + + // std::chrono::duration preprocess_diff = preprocess_end - + // preprocess_start; + // times->push_back(double(preprocess_diff.count() * 1000)); + std::chrono::duration inference_diff = infer_end - infer_start; + double inference_cost_time = double(inference_diff.count() * 1000); + times->push_back(inference_cost_time); + std::chrono::duration postprocess_diff = + postprocess_end - postprocess_start; + times->push_back(double(postprocess_diff.count() * 1000)); + std::cout << "result: " << std::endl; std::cout << "\tclass id: " << maxPosition << std::endl; std::cout << std::fixed << std::setprecision(10) << "\tscore: " << double(out_data[maxPosition]) << std::endl; - return cost_time; + return inference_cost_time; } } // namespace PaddleClas diff --git a/deploy/cpp/src/main.cpp b/deploy/cpp/src/main.cpp index 0dda50819..4fc191b3c 100644 --- a/deploy/cpp/src/main.cpp +++ b/deploy/cpp/src/main.cpp @@ -1,4 +1,4 @@ -// Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved. +// Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -26,6 +26,7 @@ #include #include +#include #include #include @@ -61,11 +62,12 @@ int main(int argc, char **argv) { Classifier classifier(config.cls_model_path, config.cls_params_path, config.use_gpu, config.gpu_id, config.gpu_mem, - config.cpu_math_library_num_threads, config.use_mkldnn, + config.cpu_threads, config.use_mkldnn, config.use_tensorrt, config.use_fp16, config.resize_short_size, config.crop_size); double elapsed_time = 0.0; + std::vector cls_times; int warmup_iter = img_files_list.size() > 5 ? 5 : 0; for (int idx = 0; idx < img_files_list.size(); ++idx) { std::string img_path = img_files_list[idx]; @@ -78,7 +80,7 @@ int main(int argc, char **argv) { cv::cvtColor(srcimg, srcimg, cv::COLOR_BGR2RGB); - double run_time = classifier.Run(srcimg); + double run_time = classifier.Run(srcimg, &cls_times); if (idx >= warmup_iter) { elapsed_time += run_time; std::cout << "Current image path: " << img_path << std::endl; @@ -90,5 +92,16 @@ int main(int argc, char **argv) { } } + std::string presion = "fp32"; + + 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(); + } return 0; } diff --git a/deploy/cpp/tools/build.sh b/deploy/cpp/tools/build.sh index ad6a727f0..0a3aa044c 100755 --- a/deploy/cpp/tools/build.sh +++ b/deploy/cpp/tools/build.sh @@ -1,5 +1,5 @@ -OPENCV_DIR=/PaddleClas/opencv-3.4.7/opencv3/ -LIB_DIR=/PaddleClas/fluid_inference/ +OPENCV_DIR=/work/project/project/cpp_infer/opencv-3.4.7/opencv3 +LIB_DIR=/work/project/project/cpp_infer/paddle_inference/ CUDA_LIB_DIR=/usr/local/cuda/lib64 CUDNN_LIB_DIR=/usr/lib/x86_64-linux-gnu/ diff --git a/deploy/cpp/tools/config.txt b/deploy/cpp/tools/config.txt index b0becad61..0d915a91a 100755 --- a/deploy/cpp/tools/config.txt +++ b/deploy/cpp/tools/config.txt @@ -2,7 +2,7 @@ use_gpu 0 gpu_id 0 gpu_mem 4000 -cpu_math_library_num_threads 10 +cpu_threads 10 use_mkldnn 1 use_tensorrt 0 use_fp16 0 @@ -12,3 +12,6 @@ cls_model_path /PaddleClas/inference/cls_infer.pdmodel cls_params_path /PaddleClas/inference/cls_infer.pdiparams resize_short_size 256 crop_size 224 + +# for log env info +benchmark 0 diff --git a/tests/DarkNet53.txt b/tests/config/DarkNet53.txt similarity index 100% rename from tests/DarkNet53.txt rename to tests/config/DarkNet53.txt diff --git a/tests/HRNet_W18_C.txt b/tests/config/HRNet_W18_C.txt similarity index 100% rename from tests/HRNet_W18_C.txt rename to tests/config/HRNet_W18_C.txt diff --git a/tests/LeViT_128S.txt b/tests/config/LeViT_128S.txt similarity index 100% rename from tests/LeViT_128S.txt rename to tests/config/LeViT_128S.txt diff --git a/tests/MobileNetV1.txt b/tests/config/MobileNetV1.txt similarity index 100% rename from tests/MobileNetV1.txt rename to tests/config/MobileNetV1.txt diff --git a/tests/MobileNetV2.txt b/tests/config/MobileNetV2.txt similarity index 100% rename from tests/MobileNetV2.txt rename to tests/config/MobileNetV2.txt diff --git a/tests/MobileNetV3_large_x1_0.txt b/tests/config/MobileNetV3_large_x1_0.txt similarity index 100% rename from tests/MobileNetV3_large_x1_0.txt rename to tests/config/MobileNetV3_large_x1_0.txt diff --git a/tests/ResNeXt101_vd_64x4d.txt b/tests/config/ResNeXt101_vd_64x4d.txt similarity index 100% rename from tests/ResNeXt101_vd_64x4d.txt rename to tests/config/ResNeXt101_vd_64x4d.txt diff --git a/tests/ResNet50_vd.txt b/tests/config/ResNet50_vd.txt similarity index 92% rename from tests/ResNet50_vd.txt rename to tests/config/ResNet50_vd.txt index da02c8894..425d0c8ea 100644 --- a/tests/ResNet50_vd.txt +++ b/tests/config/ResNet50_vd.txt @@ -49,3 +49,10 @@ inference:python/predict_cls.py -c configs/inference_cls.yaml -o Global.save_log_path:null -o Global.benchmark:True null:null +null:null +===========================cpp_infer_params=========================== +use_gpu:0|1 +cpu_threads:1|6 +use_mkldnn:0|1 +use_tensorrt:0|1 +use_fp16:0|1 diff --git a/tests/ShuffleNetV2_x1_0.txt b/tests/config/ShuffleNetV2_x1_0.txt similarity index 100% rename from tests/ShuffleNetV2_x1_0.txt rename to tests/config/ShuffleNetV2_x1_0.txt diff --git a/tests/SwinTransformer_tiny_patch4_window7_224.txt b/tests/config/SwinTransformer_tiny_patch4_window7_224.txt similarity index 100% rename from tests/SwinTransformer_tiny_patch4_window7_224.txt rename to tests/config/SwinTransformer_tiny_patch4_window7_224.txt diff --git a/tests/config/cpp_config.txt b/tests/config/cpp_config.txt new file mode 100755 index 000000000..046a5c08e --- /dev/null +++ b/tests/config/cpp_config.txt @@ -0,0 +1,19 @@ +# model load config +gpu_id 0 +gpu_mem 2000 + +# whole chain test will add following config +# use_gpu 0 +# cpu_threads 10 +# use_mkldnn 1 +# use_tensorrt 0 +# use_fp16 0 + +# cls config +cls_model_path inference/inference.pdmodel +cls_params_path inference/inference.pdiparams +resize_short_size 256 +crop_size 224 + +# for log env info +benchmark 1 diff --git a/tests/cpp_config.txt b/tests/cpp_config.txt new file mode 100755 index 000000000..5331cce0a --- /dev/null +++ b/tests/cpp_config.txt @@ -0,0 +1,24 @@ +# model load config +gpu_id 0 +gpu_mem 2000 + +# whole chain test will add following config +# use_gpu 0 +# cpu_threads 10 +# use_mkldnn 1 +# use_tensorrt 0 +# use_fp16 0 + +# cls config +cls_model_path inference/inference.pdmodel +cls_params_path inference/inference.pdiparams +resize_short_size 256 +crop_size 224 + +# for log env info +benchmark 1 +eval "$cpp_use_gpu_key $use_gpu" +eval "$cpp_use_gpu_key" "$use_gpu" +${cpp_use_gpu_key} ${use_gpu} +1 2 +1 2 diff --git a/tests/prepare.sh b/tests/prepare.sh index 55e1f2c7f..35782fd01 100644 --- a/tests/prepare.sh +++ b/tests/prepare.sh @@ -33,7 +33,7 @@ if [ ${MODE} = "lite_train_infer" ] || [ ${MODE} = "whole_infer" ];then mv train.txt train_list.txt mv val.txt val_list.txt cd ../../ -elif [ ${MODE} = "infer" ];then +elif [ ${MODE} = "infer" ] || [ ${MODE} = "cpp_infer" ];then # download data cd dataset rm -rf ILSVRC2012 @@ -58,3 +58,63 @@ elif [ ${MODE} = "whole_train_infer" ];then mv val.txt val_list.txt cd ../../ 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/tests/test.sh b/tests/test.sh index c717f77de..25b854f06 100644 --- a/tests/test.sh +++ b/tests/test.sh @@ -1,6 +1,6 @@ #!/bin/bash FILENAME=$1 -# MODE be one of ['lite_train_infer' 'whole_infer' 'whole_train_infer', 'infer'] +# MODE be one of ['lite_train_infer' 'whole_infer' 'whole_train_infer', 'infer', 'cpp_infer'] MODE=$2 dataline=$(cat ${FILENAME}) @@ -145,10 +145,78 @@ benchmark_value=$(func_parser_value "${lines[49]}") infer_key1=$(func_parser_key "${lines[50]}") infer_value1=$(func_parser_value "${lines[50]}") +if [ ${MODE} = "cpp_infer" ]; then + cpp_use_gpu_key=$(func_parser_key "${lines[53]}") + cpp_use_gpu_list=$(func_parser_value "${lines[53]}") + cpp_cpu_threads_key=$(func_parser_key "${lines[54]}") + cpp_cpu_threads_list=$(func_parser_value "${lines[54]}") + cpp_use_mkldnn_key=$(func_parser_key "${lines[55]}") + cpp_use_mkldnn_list=$(func_parser_value "${lines[55]}") + cpp_use_tensorrt_key=$(func_parser_key "${lines[56]}") + cpp_use_tensorrt_list=$(func_parser_value "${lines[56]}") + cpp_use_fp16_key=$(func_parser_key "${lines[57]}") + cpp_use_fp16_list=$(func_parser_value "${lines[57]}") +fi + LOG_PATH="./tests/output" mkdir -p ${LOG_PATH} status_log="${LOG_PATH}/results.log" +function func_cpp_inference(){ + IFS='|' + _script=$1 + _log_path=$2 + _img_dir=$3 + # inference + for use_gpu in ${cpp_use_gpu_list[*]}; do + if [ ${use_gpu} = "0" ] || [ ${use_gpu} = "cpu" ]; then + for use_mkldnn in ${cpp_use_mkldnn_list[*]}; do + if [ ${use_mkldnn} = "0" ] && [ ${_flag_quant} = "True" ]; then + continue + fi + for threads in ${cpp_cpu_threads_list[*]}; do + _save_log_path="${_log_path}/cpp_infer_cpu_usemkldnn_${use_mkldnn}_threads_${threads}.log" + set_infer_data=$(func_set_params "${cpp_image_dir_key}" "${_img_dir}") + cp ../tests/config/cpp_config.txt cpp_config.txt + echo "${cpp_use_gpu_key} ${use_gpu}" >> cpp_config.txt + echo "${cpp_cpu_threads_key} ${threads}" >> cpp_config.txt + echo "${cpp_use_mkldnn_key} ${use_mkldnn}" >> cpp_config.txt + echo "${cpp_use_tensorrt_key} 0" >> cpp_config.txt + command="${_script} cpp_config.txt ${_img_dir} > ${_save_log_path} 2>&1 " + eval $command + last_status=${PIPESTATUS[0]} + eval "cat ${_save_log_path}" + status_check $last_status "${command}" "${status_log}" + done + done + elif [ ${use_gpu} = "1" ] || [ ${use_gpu} = "gpu" ]; then + for use_trt in ${cpp_use_tensorrt_list[*]}; do + for precision in ${cpp_use_fp16_list[*]}; do + if [[ ${precision} =~ "fp16" || ${precision} =~ "int8" ]] && [ ${use_trt} = "False" ]; then + continue + fi + if [[ ${use_trt} = "False" || ${precision} =~ "int8" ]] && [ ${_flag_quant} = "True" ]; then + continue + fi + _save_log_path="${_log_path}/cpp_infer_gpu_usetrt_${use_trt}_precision_${precision}_batchsize_${batch_size}.log" + cp ../tests/config/cpp_config.txt cpp_config.txt + echo "${cpp_use_gpu_key} ${use_gpu}" >> cpp_config.txt + echo "${cpp_cpu_threads_key} ${threads}" >> cpp_config.txt + echo "${cpp_use_mkldnn_key} ${use_mkldnn}" >> cpp_config.txt + echo "${cpp_use_tensorrt_key} ${precision}" >> cpp_config.txt + command="${_script} cpp_config.txt ${_img_dir} > ${_save_log_path} 2>&1 " + eval $command + last_status=${PIPESTATUS[0]} + eval "cat ${_save_log_path}" + status_check $last_status "${command}" "${status_log}" + + done + done + else + echo "Does not support hardware other than CPU and GPU Currently!" + fi + done +} function func_inference(){ IFS='|' @@ -247,6 +315,10 @@ if [ ${MODE} = "infer" ]; then Count=$(($Count + 1)) done cd .. +elif [ ${MODE} = "cpp_infer" ]; then + cd deploy + func_cpp_inference "./cpp/build/clas_system" "../${LOG_PATH}" "${infer_img_dir}" + cd .. else IFS="|" From 0bdce1d483cb5371215eda52d2b2c55bfec06651 Mon Sep 17 00:00:00 2001 From: dongshuilong Date: Mon, 6 Sep 2021 08:43:07 +0000 Subject: [PATCH 2/2] fix cpp_infer bugs --- deploy/cpp/src/cls.cpp | 8 ++++---- tests/config/cpp_config.txt | 4 ++-- tests/cpp_config.txt | 24 ------------------------ tests/test.sh | 4 +++- 4 files changed, 9 insertions(+), 31 deletions(-) delete mode 100755 tests/cpp_config.txt diff --git a/deploy/cpp/src/cls.cpp b/deploy/cpp/src/cls.cpp index 7a141639d..6ce09e762 100644 --- a/deploy/cpp/src/cls.cpp +++ b/deploy/cpp/src/cls.cpp @@ -57,7 +57,7 @@ double Classifier::Run(cv::Mat &img, std::vector *times) { cv::Mat resize_img; img.copyTo(srcimg); - auto preprocess_start = std::chrono::steady_clock::now(); + auto preprocess_start = std::chrono::system_clock::now(); this->resize_op_.Run(img, resize_img, this->resize_short_size_); this->crop_op_.Run(resize_img, this->crop_size_); @@ -92,9 +92,9 @@ double Classifier::Run(cv::Mat &img, std::vector *times) { max_element(out_data.begin(), out_data.end()) - out_data.begin(); auto postprocess_end = std::chrono::system_clock::now(); - // std::chrono::duration preprocess_diff = preprocess_end - - // preprocess_start; - // times->push_back(double(preprocess_diff.count() * 1000)); + std::chrono::duration preprocess_diff = + preprocess_end - preprocess_start; + times->push_back(double(preprocess_diff.count() * 1000)); std::chrono::duration inference_diff = infer_end - infer_start; double inference_cost_time = double(inference_diff.count() * 1000); times->push_back(inference_cost_time); diff --git a/tests/config/cpp_config.txt b/tests/config/cpp_config.txt index 046a5c08e..2f1286447 100755 --- a/tests/config/cpp_config.txt +++ b/tests/config/cpp_config.txt @@ -10,8 +10,8 @@ gpu_mem 2000 # use_fp16 0 # cls config -cls_model_path inference/inference.pdmodel -cls_params_path inference/inference.pdiparams +cls_model_path ../inference/inference.pdmodel +cls_params_path ../inference/inference.pdiparams resize_short_size 256 crop_size 224 diff --git a/tests/cpp_config.txt b/tests/cpp_config.txt deleted file mode 100755 index 5331cce0a..000000000 --- a/tests/cpp_config.txt +++ /dev/null @@ -1,24 +0,0 @@ -# model load config -gpu_id 0 -gpu_mem 2000 - -# whole chain test will add following config -# use_gpu 0 -# cpu_threads 10 -# use_mkldnn 1 -# use_tensorrt 0 -# use_fp16 0 - -# cls config -cls_model_path inference/inference.pdmodel -cls_params_path inference/inference.pdiparams -resize_short_size 256 -crop_size 224 - -# for log env info -benchmark 1 -eval "$cpp_use_gpu_key $use_gpu" -eval "$cpp_use_gpu_key" "$use_gpu" -${cpp_use_gpu_key} ${use_gpu} -1 2 -1 2 diff --git a/tests/test.sh b/tests/test.sh index 25b854f06..9c0d8236c 100644 --- a/tests/test.sh +++ b/tests/test.sh @@ -182,6 +182,7 @@ function func_cpp_inference(){ echo "${cpp_cpu_threads_key} ${threads}" >> cpp_config.txt echo "${cpp_use_mkldnn_key} ${use_mkldnn}" >> cpp_config.txt echo "${cpp_use_tensorrt_key} 0" >> cpp_config.txt + echo "${cpp_use_fp16_key} 0" >> cpp_config.txt command="${_script} cpp_config.txt ${_img_dir} > ${_save_log_path} 2>&1 " eval $command last_status=${PIPESTATUS[0]} @@ -203,7 +204,8 @@ function func_cpp_inference(){ echo "${cpp_use_gpu_key} ${use_gpu}" >> cpp_config.txt echo "${cpp_cpu_threads_key} ${threads}" >> cpp_config.txt echo "${cpp_use_mkldnn_key} ${use_mkldnn}" >> cpp_config.txt - echo "${cpp_use_tensorrt_key} ${precision}" >> cpp_config.txt + echo "${cpp_use_tensorrt_key} ${use_trt}" >> cpp_config.txt + echo "${cpp_use_fp16_key} ${precision}" >> cpp_config.txt command="${_script} cpp_config.txt ${_img_dir} > ${_save_log_path} 2>&1 " eval $command last_status=${PIPESTATUS[0]}