Skip to content

Commit

Permalink
[Cherry-pick] #10441 #10512 (#10593)
Browse files Browse the repository at this point in the history
* fix memory leak (#10441)

* fix memory leak

* update: Using smart pointers instead of raw pointers

* update: Usinig intuitive initialization of duration (#10512)
  • Loading branch information
moehuster authored Aug 14, 2023
1 parent 5616098 commit 549376e
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 56 deletions.
8 changes: 4 additions & 4 deletions deploy/cpp_infer/include/paddleocr.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ namespace PaddleOCR {
class PPOCR {
public:
explicit PPOCR();
~PPOCR();
~PPOCR() = default;

std::vector<std::vector<OCRPredictResult>> ocr(std::vector<cv::Mat> img_list,
bool det = true,
Expand All @@ -47,9 +47,9 @@ class PPOCR {
std::vector<OCRPredictResult> &ocr_results);

private:
DBDetector *detector_ = nullptr;
Classifier *classifier_ = nullptr;
CRNNRecognizer *recognizer_ = nullptr;
std::unique_ptr<DBDetector> detector_;
std::unique_ptr<Classifier> classifier_;
std::unique_ptr<CRNNRecognizer> recognizer_;
};

} // namespace PaddleOCR
6 changes: 3 additions & 3 deletions deploy/cpp_infer/include/paddlestructure.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ namespace PaddleOCR {
class PaddleStructure : public PPOCR {
public:
explicit PaddleStructure();
~PaddleStructure();
~PaddleStructure() = default;

std::vector<StructurePredictResult> structure(cv::Mat img,
bool layout = false,
Expand All @@ -37,8 +37,8 @@ class PaddleStructure : public PPOCR {
std::vector<double> time_info_table = {0, 0, 0};
std::vector<double> time_info_layout = {0, 0, 0};

StructureTableRecognizer *table_model_ = nullptr;
StructureLayoutRecognizer *layout_model_ = nullptr;
std::unique_ptr<StructureTableRecognizer> table_model_;
std::unique_ptr<StructureLayoutRecognizer> layout_model_;

void layout(cv::Mat img,
std::vector<StructurePredictResult> &structure_result);
Expand Down
4 changes: 2 additions & 2 deletions deploy/cpp_infer/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ void check_params() {
}

void ocr(std::vector<cv::String> &cv_all_img_names) {
PPOCR ocr = PPOCR();
PPOCR ocr;

if (FLAGS_benchmark) {
ocr.reset_timer();
Expand Down Expand Up @@ -120,7 +120,7 @@ void ocr(std::vector<cv::String> &cv_all_img_names) {
}

void structure(std::vector<cv::String> &cv_all_img_names) {
PaddleOCR::PaddleStructure engine = PaddleOCR::PaddleStructure();
PaddleOCR::PaddleStructure engine;

if (FLAGS_benchmark) {
engine.reset_timer();
Expand Down
9 changes: 3 additions & 6 deletions deploy/cpp_infer/src/ocr_cls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,9 @@ void Classifier::Run(std::vector<cv::Mat> img_list,
std::vector<int> &cls_labels,
std::vector<float> &cls_scores,
std::vector<double> &times) {
std::chrono::duration<float> preprocess_diff =
std::chrono::steady_clock::now() - std::chrono::steady_clock::now();
std::chrono::duration<float> inference_diff =
std::chrono::steady_clock::now() - std::chrono::steady_clock::now();
std::chrono::duration<float> postprocess_diff =
std::chrono::steady_clock::now() - std::chrono::steady_clock::now();
std::chrono::duration<float> preprocess_diff = std::chrono::duration<float>::zero();
std::chrono::duration<float> inference_diff = std::chrono::duration<float>::zero();
std::chrono::duration<float> postprocess_diff = std::chrono::duration<float>::zero();

int img_num = img_list.size();
std::vector<int> cls_image_shape = {3, 48, 192};
Expand Down
9 changes: 3 additions & 6 deletions deploy/cpp_infer/src/ocr_rec.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,9 @@ void CRNNRecognizer::Run(std::vector<cv::Mat> img_list,
std::vector<std::string> &rec_texts,
std::vector<float> &rec_text_scores,
std::vector<double> &times) {
std::chrono::duration<float> preprocess_diff =
std::chrono::steady_clock::now() - std::chrono::steady_clock::now();
std::chrono::duration<float> inference_diff =
std::chrono::steady_clock::now() - std::chrono::steady_clock::now();
std::chrono::duration<float> postprocess_diff =
std::chrono::steady_clock::now() - std::chrono::steady_clock::now();
std::chrono::duration<float> preprocess_diff = std::chrono::duration<float>::zero();
std::chrono::duration<float> inference_diff = std::chrono::duration<float>::zero();
std::chrono::duration<float> postprocess_diff = std::chrono::duration<float>::zero();

int img_num = img_list.size();
std::vector<float> width_list;
Expand Down
30 changes: 9 additions & 21 deletions deploy/cpp_infer/src/paddleocr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,28 +21,28 @@ namespace PaddleOCR {

PPOCR::PPOCR() {
if (FLAGS_det) {
this->detector_ = new DBDetector(
this->detector_.reset(new DBDetector(
FLAGS_det_model_dir, FLAGS_use_gpu, FLAGS_gpu_id, FLAGS_gpu_mem,
FLAGS_cpu_threads, FLAGS_enable_mkldnn, FLAGS_limit_type,
FLAGS_limit_side_len, FLAGS_det_db_thresh, FLAGS_det_db_box_thresh,
FLAGS_det_db_unclip_ratio, FLAGS_det_db_score_mode, FLAGS_use_dilation,
FLAGS_use_tensorrt, FLAGS_precision);
FLAGS_use_tensorrt, FLAGS_precision));
}

if (FLAGS_cls && FLAGS_use_angle_cls) {
this->classifier_ = new Classifier(
this->classifier_.reset(new Classifier(
FLAGS_cls_model_dir, FLAGS_use_gpu, FLAGS_gpu_id, FLAGS_gpu_mem,
FLAGS_cpu_threads, FLAGS_enable_mkldnn, FLAGS_cls_thresh,
FLAGS_use_tensorrt, FLAGS_precision, FLAGS_cls_batch_num);
FLAGS_use_tensorrt, FLAGS_precision, FLAGS_cls_batch_num));
}
if (FLAGS_rec) {
this->recognizer_ = new CRNNRecognizer(
this->recognizer_.reset(new CRNNRecognizer(
FLAGS_rec_model_dir, FLAGS_use_gpu, FLAGS_gpu_id, FLAGS_gpu_mem,
FLAGS_cpu_threads, FLAGS_enable_mkldnn, FLAGS_rec_char_dict_path,
FLAGS_use_tensorrt, FLAGS_precision, FLAGS_rec_batch_num,
FLAGS_rec_img_h, FLAGS_rec_img_w);
FLAGS_rec_img_h, FLAGS_rec_img_w));
}
};
}

std::vector<std::vector<OCRPredictResult>>
PPOCR::ocr(std::vector<cv::Mat> img_list, bool det, bool rec, bool cls) {
Expand All @@ -51,7 +51,7 @@ PPOCR::ocr(std::vector<cv::Mat> img_list, bool det, bool rec, bool cls) {
if (!det) {
std::vector<OCRPredictResult> ocr_result;
ocr_result.resize(img_list.size());
if (cls && this->classifier_ != nullptr) {
if (cls && this->classifier_) {
this->cls(img_list, ocr_result);
for (int i = 0; i < img_list.size(); i++) {
if (ocr_result[i].cls_label % 2 == 1 &&
Expand Down Expand Up @@ -92,7 +92,7 @@ std::vector<OCRPredictResult> PPOCR::ocr(cv::Mat img, bool det, bool rec,
img_list.push_back(crop_img);
}
// cls
if (cls && this->classifier_ != nullptr) {
if (cls && this->classifier_) {
this->cls(img_list, ocr_result);
for (int i = 0; i < img_list.size(); i++) {
if (ocr_result[i].cls_label % 2 == 1 &&
Expand Down Expand Up @@ -190,16 +190,4 @@ void PPOCR::benchmark_log(int img_num) {
}
}

PPOCR::~PPOCR() {
if (this->detector_ != nullptr) {
delete this->detector_;
}
if (this->classifier_ != nullptr) {
delete this->classifier_;
}
if (this->recognizer_ != nullptr) {
delete this->recognizer_;
}
};

} // namespace PaddleOCR
22 changes: 8 additions & 14 deletions deploy/cpp_infer/src/paddlestructure.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,20 @@ namespace PaddleOCR {

PaddleStructure::PaddleStructure() {
if (FLAGS_layout) {
this->layout_model_ = new StructureLayoutRecognizer(
this->layout_model_.reset(new StructureLayoutRecognizer(
FLAGS_layout_model_dir, FLAGS_use_gpu, FLAGS_gpu_id, FLAGS_gpu_mem,
FLAGS_cpu_threads, FLAGS_enable_mkldnn, FLAGS_layout_dict_path,
FLAGS_use_tensorrt, FLAGS_precision, FLAGS_layout_score_threshold,
FLAGS_layout_nms_threshold);
FLAGS_layout_nms_threshold));
}
if (FLAGS_table) {
this->table_model_ = new StructureTableRecognizer(
this->table_model_.reset(new StructureTableRecognizer(
FLAGS_table_model_dir, FLAGS_use_gpu, FLAGS_gpu_id, FLAGS_gpu_mem,
FLAGS_cpu_threads, FLAGS_enable_mkldnn, FLAGS_table_char_dict_path,
FLAGS_use_tensorrt, FLAGS_precision, FLAGS_table_batch_num,
FLAGS_table_max_len, FLAGS_merge_no_span_structure);
FLAGS_table_max_len, FLAGS_merge_no_span_structure));
}
};
}

std::vector<StructurePredictResult>
PaddleStructure::structure(cv::Mat srcimg, bool layout, bool table, bool ocr) {
Expand Down Expand Up @@ -65,7 +65,7 @@ PaddleStructure::structure(cv::Mat srcimg, bool layout, bool table, bool ocr) {
}

return structure_results;
};
}

void PaddleStructure::layout(
cv::Mat img, std::vector<StructurePredictResult> &structure_result) {
Expand Down Expand Up @@ -123,7 +123,7 @@ void PaddleStructure::table(cv::Mat img,
structure_result.cell_box = structure_boxes[i];
structure_result.html_score = structure_scores[i];
}
};
}

std::string
PaddleStructure::rebuild_table(std::vector<std::string> structure_html_tags,
Expand Down Expand Up @@ -286,10 +286,4 @@ void PaddleStructure::benchmark_log(int img_num) {
}
}

PaddleStructure::~PaddleStructure() {
if (this->table_model_ != nullptr) {
delete this->table_model_;
}
};

} // namespace PaddleOCR
} // namespace PaddleOCR

0 comments on commit 549376e

Please sign in to comment.