diff --git a/deploy/cpp_infer/include/config.h b/deploy/cpp_infer/include/config.h index 4004fc04e5a839c675d38ffbd694f286b31b0079..2adefb73675b3607b3dd993f48517e51e9a3d941 100644 --- a/deploy/cpp_infer/include/config.h +++ b/deploy/cpp_infer/include/config.h @@ -39,6 +39,8 @@ public: this->cpu_math_library_num_threads = stoi(config_map_["cpu_math_library_num_threads"]); + this->use_mkldnn = bool(stoi(config_map_["use_mkldnn"])); + this->max_side_len = stoi(config_map_["max_side_len"]); this->det_db_thresh = stod(config_map_["det_db_thresh"]); @@ -64,6 +66,8 @@ public: int cpu_math_library_num_threads = 1; + bool use_mkldnn = false; + int max_side_len = 960; double det_db_thresh = 0.3; diff --git a/deploy/cpp_infer/include/ocr_det.h b/deploy/cpp_infer/include/ocr_det.h index 611fb5c1a63a6a806bebaa4f59d5cb73e2acf3cf..ed2667eecfea9a09d7da77df37f43a7b9e9bb349 100644 --- a/deploy/cpp_infer/include/ocr_det.h +++ b/deploy/cpp_infer/include/ocr_det.h @@ -39,7 +39,8 @@ public: explicit DBDetector(const std::string &model_dir, const bool &use_gpu, const int &gpu_id, const int &gpu_mem, const int &cpu_math_library_num_threads, - const int &max_side_len, const double &det_db_thresh, + const bool &use_mkldnn, const int &max_side_len, + const double &det_db_thresh, const double &det_db_box_thresh, const double &det_db_unclip_ratio, const bool &visualize) { @@ -47,6 +48,7 @@ public: this->gpu_id_ = gpu_id; this->gpu_mem_ = gpu_mem; this->cpu_math_library_num_threads_ = cpu_math_library_num_threads; + this->use_mkldnn_ = use_mkldnn; this->max_side_len_ = max_side_len; @@ -72,6 +74,7 @@ private: int gpu_id_ = 0; int gpu_mem_ = 4000; int cpu_math_library_num_threads_ = 4; + bool use_mkldnn_ = false; int max_side_len_ = 960; diff --git a/deploy/cpp_infer/include/ocr_rec.h b/deploy/cpp_infer/include/ocr_rec.h index 345f4644ae370acd139daefa0e60efc77b83b9ef..18b31b6b16dc7b45f7ea3622feffdcc33188bda9 100644 --- a/deploy/cpp_infer/include/ocr_rec.h +++ b/deploy/cpp_infer/include/ocr_rec.h @@ -38,11 +38,12 @@ public: explicit CRNNRecognizer(const std::string &model_dir, const bool &use_gpu, const int &gpu_id, const int &gpu_mem, const int &cpu_math_library_num_threads, - const string &label_path) { + const bool &use_mkldnn, const string &label_path) { this->use_gpu_ = use_gpu; this->gpu_id_ = gpu_id; this->gpu_mem_ = gpu_mem; this->cpu_math_library_num_threads_ = cpu_math_library_num_threads; + this->use_mkldnn_ = use_mkldnn; this->label_list_ = Utility::ReadDict(label_path); @@ -61,6 +62,7 @@ private: int gpu_id_ = 0; int gpu_mem_ = 4000; int cpu_math_library_num_threads_ = 4; + bool use_mkldnn_ = false; std::vector label_list_; diff --git a/deploy/cpp_infer/readme.md b/deploy/cpp_infer/readme.md index df30daa533269605c823823be7775f032490cdf9..47aa3a1133267a6c9597ea2da08aa0daa43c83d1 100644 --- a/deploy/cpp_infer/readme.md +++ b/deploy/cpp_infer/readme.md @@ -195,3 +195,8 @@ sh tools/run.sh
+ + +### 2.3 注意 + +* C++预测默认未开启MKLDNN(`tools/config.txt`中的`use_mkldnn`设置为0),如果需要使用MKLDNN进行预测加速,则需要将`use_mkldnn`修改为1,同时使用最新版本的Paddle源码编译预测库。在使用MKLDNN进行CPU预测时,如果同时预测多张图像,则会出现内存泄露的问题(不打开MKLDNN则没有该问题),目前该问题正在修复中,临时解决方案为:预测多张图片时,每隔30张图片左右对识别(`CRNNRecognizer`)和检测类(`DBDetector`)重新初始化一次。 diff --git a/deploy/cpp_infer/src/main.cpp b/deploy/cpp_infer/src/main.cpp index 3bcb6708cc0a94bc1cfac379d78d2224c1a851ae..27c98e5b84367de09f95c901d168c2d318902c43 100644 --- a/deploy/cpp_infer/src/main.cpp +++ b/deploy/cpp_infer/src/main.cpp @@ -50,12 +50,12 @@ int main(int argc, char **argv) { DBDetector det(config.det_model_dir, config.use_gpu, config.gpu_id, config.gpu_mem, config.cpu_math_library_num_threads, - config.max_side_len, config.det_db_thresh, + config.use_mkldnn, config.max_side_len, config.det_db_thresh, config.det_db_box_thresh, config.det_db_unclip_ratio, config.visualize); CRNNRecognizer rec(config.rec_model_dir, config.use_gpu, config.gpu_id, config.gpu_mem, config.cpu_math_library_num_threads, - config.char_list_file); + config.use_mkldnn, config.char_list_file); auto start = std::chrono::system_clock::now(); std::vector>> boxes; diff --git a/deploy/cpp_infer/src/ocr_det.cpp b/deploy/cpp_infer/src/ocr_det.cpp index ee30c1a752870c6ba58fd1eb459fb125c63016e5..cddb0a8d84f4c2b11ecad1b6dc946334c26c46d9 100644 --- a/deploy/cpp_infer/src/ocr_det.cpp +++ b/deploy/cpp_infer/src/ocr_det.cpp @@ -13,7 +13,6 @@ // limitations under the License. #include -#include namespace PaddleOCR { @@ -25,7 +24,9 @@ void DBDetector::LoadModel(const std::string &model_dir) { config.EnableUseGpu(this->gpu_mem_, this->gpu_id_); } else { config.DisableGpu(); - // config.EnableMKLDNN(); // not sugesteed to use for now + if (this->use_mkldnn_) { + config.EnableMKLDNN(); + } config.SetCpuMathLibraryNumThreads(this->cpu_math_library_num_threads_); } diff --git a/deploy/cpp_infer/src/ocr_rec.cpp b/deploy/cpp_infer/src/ocr_rec.cpp index 95ea9a5e743b233271e1afc7fa3c44d11fd4a6b6..446f9bc16baabe11fbaa972e3174e036874d7e74 100644 --- a/deploy/cpp_infer/src/ocr_rec.cpp +++ b/deploy/cpp_infer/src/ocr_rec.cpp @@ -12,21 +12,6 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include "opencv2/core.hpp" -#include "opencv2/imgcodecs.hpp" -#include "opencv2/imgproc.hpp" -#include "paddle_api.h" -#include "paddle_inference_api.h" -#include -#include -#include -#include -#include - -#include -#include -#include - #include namespace PaddleOCR { @@ -128,7 +113,9 @@ void CRNNRecognizer::LoadModel(const std::string &model_dir) { config.EnableUseGpu(this->gpu_mem_, this->gpu_id_); } else { config.DisableGpu(); - // config.EnableMKLDNN(); // not sugesteed to use for now + if (this->use_mkldnn_) { + config.EnableMKLDNN(); + } config.SetCpuMathLibraryNumThreads(this->cpu_math_library_num_threads_); } diff --git a/deploy/cpp_infer/tools/config.txt b/deploy/cpp_infer/tools/config.txt index 2c5591633013fae3c4ac82e1882ac4b23da9b74d..fe7f27a0c7c3c1f680339ff716bdd9540e8dd119 100644 --- a/deploy/cpp_infer/tools/config.txt +++ b/deploy/cpp_infer/tools/config.txt @@ -2,7 +2,8 @@ use_gpu 0 gpu_id 0 gpu_mem 4000 -cpu_math_library_num_threads 1 +cpu_math_library_num_threads 10 +use_mkldnn 0 # det config max_side_len 960 @@ -17,5 +18,5 @@ char_list_file ../../ppocr/utils/ppocr_keys_v1.txt img_path ../../doc/imgs/11.jpg # show the detection results -visualize 1 +visualize 0 diff --git a/deploy/cpp_infer/tools/run.sh b/deploy/cpp_infer/tools/run.sh index 052b363b32ffb4870d57d30f1f4e3414e91d6a5d..fa61da75e3a71262f539ee348c69fb82ed2574fb 100755 --- a/deploy/cpp_infer/tools/run.sh +++ b/deploy/cpp_infer/tools/run.sh @@ -1,2 +1,2 @@ -./build/ocr_system ./tools/config.txt ../../doc/imgs/6.jpg +./build/ocr_system ./tools/config.txt ../../doc/imgs/12.jpg diff --git a/deploy/imgs/cpp_infer_pred_12.png b/deploy/imgs/cpp_infer_pred_12.png index 0ed85a31a0c64f50c165eeb887ceebd52894d785..eb5f64e1f6c329f7ae772c50edce7fc8afcb1211 100644 Binary files a/deploy/imgs/cpp_infer_pred_12.png and b/deploy/imgs/cpp_infer_pred_12.png differ