Skip to content

Commit 6c4a08e

Browse files
felixhjhjiangjiajunleiqing1heliqiwjj19950828
authored
[Other] PPOCR models support model clone function (PaddlePaddle#1072)
* Refactor PaddleSeg with preprocessor && postprocessor * Fix bugs * Delete redundancy code * Modify by comments * Refactor according to comments * Add batch evaluation * Add single test script * Add ppliteseg single test script && fix eval(raise) error * fix bug * Fix evaluation segmentation.py batch predict * Fix segmentation evaluation bug * Fix evaluation segmentation bugs * Update segmentation result docs * Update old predict api and DisableNormalizeAndPermute * Update resize segmentation label map with cv::INTER_NEAREST * Add Model Clone function for PaddleClas && PaddleDet && PaddleSeg * Add multi thread demo * Add python model clone function * Add multi thread python && C++ example * Fix bug * Update python && cpp multi_thread examples * Add cpp && python directory * Add README.md for examples * Delete redundant code * Create README_CN.md * Rename README_CN.md to README.md * Update README.md * Update README.md * Update VERSION_NUMBER * Update requirements.txt * Update README.md * update version in doc: * [Serving]Update Dockerfile (PaddlePaddle#1037) Update Dockerfile * Add license notice for RVM onnx model file (PaddlePaddle#1060) * [Model] Add GPL-3.0 license (PaddlePaddle#1065) Add GPL-3.0 license * PPOCR model support model clone * Update README.md * Update PPOCRv2 && PPOCRv3 clone code * Update PPOCR python __init__ * Add multi thread ocr example code * Update README.md * Update README.md * Update ResNet50_vd_infer multi process code * Add PPOCR multi process && thread example * Update README.md * Update README.md * Update multi-thread docs Co-authored-by: Jason <[email protected]> Co-authored-by: leiqing <[email protected]> Co-authored-by: heliqi <[email protected]> Co-authored-by: WJJ1995 <[email protected]>
1 parent abba2af commit 6c4a08e

28 files changed

+1201
-96
lines changed

examples/vision/matting/rvm/README.md

100755100644
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ For developers' testing, models exported by RobustVideoMatting are provided belo
1717

1818
| Model | Parameter Size | Accuracy | Note |
1919
|:---------------------------------------------------------------- |:----- |:----- | :------ |
20-
| [rvm_mobilenetv3_fp32.onnx](https://bj.bcebos.com/paddlehub/fastdeploy/rvm_mobilenetv3_fp32.onnx) | 15MB | - |
21-
| [rvm_resnet50_fp32.onnx](https://bj.bcebos.com/paddlehub/fastdeploy/rvm_resnet50_fp32.onnx) | 103MB | - |
22-
| [rvm_mobilenetv3_trt.onnx](https://bj.bcebos.com/paddlehub/fastdeploy/rvm_mobilenetv3_trt.onnx) | 15MB | - |
23-
| [rvm_resnet50_trt.onnx](https://bj.bcebos.com/paddlehub/fastdeploy/rvm_resnet50_trt.onnx) | 103MB | - |
20+
| [rvm_mobilenetv3_fp32.onnx](https://bj.bcebos.com/paddlehub/fastdeploy/rvm_mobilenetv3_fp32.onnx) | 15MB ||exported from [RobustVideoMatting](https://github.com/PeterL1n/RobustVideoMatting/commit/81a1093),GPL-3.0 License |
21+
| [rvm_resnet50_fp32.onnx](https://bj.bcebos.com/paddlehub/fastdeploy/rvm_resnet50_fp32.onnx) | 103MB | |exported from [RobustVideoMatting](https://github.com/PeterL1n/RobustVideoMatting/commit/81a1093),GPL-3.0 License |
22+
| [rvm_mobilenetv3_trt.onnx](https://bj.bcebos.com/paddlehub/fastdeploy/rvm_mobilenetv3_trt.onnx) | 15MB | |exported from [RobustVideoMatting](https://github.com/PeterL1n/RobustVideoMatting/commit/81a1093),GPL-3.0 License |
23+
| [rvm_resnet50_trt.onnx](https://bj.bcebos.com/paddlehub/fastdeploy/rvm_resnet50_trt.onnx) | 103MB | |exported from [RobustVideoMatting](https://github.com/PeterL1n/RobustVideoMatting/commit/81a1093),GPL-3.0 License |
2424

2525
**Note**
2626
- If you want to use TensorRT for inference, download onnx model file with the trt suffix is necessary.

fastdeploy/vision/ocr/ppocr/classifier.cc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,12 @@ bool Classifier::Initialize() {
5353
return true;
5454
}
5555

56+
std::unique_ptr<Classifier> Classifier::Clone() const {
57+
std::unique_ptr<Classifier> clone_model = utils::make_unique<Classifier>(Classifier(*this));
58+
clone_model->SetRuntime(clone_model->CloneRuntime());
59+
return clone_model;
60+
}
61+
5662
bool Classifier::Predict(const cv::Mat& img, int32_t* cls_label, float* cls_score) {
5763
std::vector<int32_t> cls_labels(1);
5864
std::vector<float> cls_scores(1);

fastdeploy/vision/ocr/ppocr/classifier.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "fastdeploy/vision/ocr/ppocr/utils/ocr_postprocess_op.h"
2020
#include "fastdeploy/vision/ocr/ppocr/cls_postprocessor.h"
2121
#include "fastdeploy/vision/ocr/ppocr/cls_preprocessor.h"
22+
#include "fastdeploy/utils/unique_ptr.h"
2223

2324
namespace fastdeploy {
2425
namespace vision {
@@ -41,6 +42,13 @@ class FASTDEPLOY_DECL Classifier : public FastDeployModel {
4142
Classifier(const std::string& model_file, const std::string& params_file = "",
4243
const RuntimeOption& custom_option = RuntimeOption(),
4344
const ModelFormat& model_format = ModelFormat::PADDLE);
45+
46+
/** \brief Clone a new Classifier with less memory usage when multiple instances of the same model are created
47+
*
48+
* \return new Classifier* type unique pointer
49+
*/
50+
virtual std::unique_ptr<Classifier> Clone() const;
51+
4452
/// Get model's name
4553
std::string ModelName() const { return "ppocr/ocr_cls"; }
4654

@@ -53,6 +61,7 @@ class FASTDEPLOY_DECL Classifier : public FastDeployModel {
5361
*/
5462
virtual bool Predict(const cv::Mat& img,
5563
int32_t* cls_label, float* cls_score);
64+
5665
/** \brief BatchPredict the input image and get OCR classification model cls_result.
5766
*
5867
* \param[in] images The list of input image data, comes from cv::imread(), is a 3-D array with layout HWC, BGR format.

fastdeploy/vision/ocr/ppocr/dbdetector.cc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,12 @@ bool DBDetector::Initialize() {
5353
return true;
5454
}
5555

56+
std::unique_ptr<DBDetector> DBDetector::Clone() const {
57+
std::unique_ptr<DBDetector> clone_model = utils::make_unique<DBDetector>(DBDetector(*this));
58+
clone_model->SetRuntime(clone_model->CloneRuntime());
59+
return clone_model;
60+
}
61+
5662
bool DBDetector::Predict(const cv::Mat& img,
5763
std::vector<std::array<int, 8>>* boxes_result) {
5864
std::vector<std::vector<std::array<int, 8>>> det_results;

fastdeploy/vision/ocr/ppocr/dbdetector.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "fastdeploy/vision/ocr/ppocr/utils/ocr_postprocess_op.h"
2020
#include "fastdeploy/vision/ocr/ppocr/det_postprocessor.h"
2121
#include "fastdeploy/vision/ocr/ppocr/det_preprocessor.h"
22+
#include "fastdeploy/utils/unique_ptr.h"
2223

2324
namespace fastdeploy {
2425
namespace vision {
@@ -42,8 +43,16 @@ class FASTDEPLOY_DECL DBDetector : public FastDeployModel {
4243
DBDetector(const std::string& model_file, const std::string& params_file = "",
4344
const RuntimeOption& custom_option = RuntimeOption(),
4445
const ModelFormat& model_format = ModelFormat::PADDLE);
46+
47+
/** \brief Clone a new DBDetector with less memory usage when multiple instances of the same model are created
48+
*
49+
* \return new DBDetector* type unique pointer
50+
*/
51+
virtual std::unique_ptr<DBDetector> Clone() const;
52+
4553
/// Get model's name
4654
std::string ModelName() const { return "ppocr/ocr_det"; }
55+
4756
/** \brief Predict the input image and get OCR detection model result.
4857
*
4958
* \param[in] img The input image data, comes from cv::imread(), is a 3-D array with layout HWC, BGR format.
@@ -52,6 +61,7 @@ class FASTDEPLOY_DECL DBDetector : public FastDeployModel {
5261
*/
5362
virtual bool Predict(const cv::Mat& img,
5463
std::vector<std::array<int, 8>>* boxes_result);
64+
5565
/** \brief BatchPredict the input image and get OCR detection model result.
5666
*
5767
* \param[in] images The list input of image data, comes from cv::imread(), is a 3-D array with layout HWC, BGR format.

fastdeploy/vision/ocr/ppocr/ppocr_pybind.cc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ void BindPPOCRv3(pybind11::module& m) {
2727
fastdeploy::vision::ocr::Recognizer*>())
2828
.def_property("cls_batch_size", &pipeline::PPOCRv3::GetClsBatchSize, &pipeline::PPOCRv3::SetClsBatchSize)
2929
.def_property("rec_batch_size", &pipeline::PPOCRv3::GetRecBatchSize, &pipeline::PPOCRv3::SetRecBatchSize)
30+
.def("clone", [](pipeline::PPOCRv3& self) {
31+
return self.Clone();
32+
})
3033
.def("predict", [](pipeline::PPOCRv3& self,
3134
pybind11::array& data) {
3235
auto mat = PyArrayToCvMat(data);
@@ -56,6 +59,9 @@ void BindPPOCRv2(pybind11::module& m) {
5659
fastdeploy::vision::ocr::Recognizer*>())
5760
.def_property("cls_batch_size", &pipeline::PPOCRv2::GetClsBatchSize, &pipeline::PPOCRv2::SetClsBatchSize)
5861
.def_property("rec_batch_size", &pipeline::PPOCRv2::GetRecBatchSize, &pipeline::PPOCRv2::SetRecBatchSize)
62+
.def("clone", [](pipeline::PPOCRv2& self) {
63+
return self.Clone();
64+
})
5965
.def("predict", [](pipeline::PPOCRv2& self,
6066
pybind11::array& data) {
6167
auto mat = PyArrayToCvMat(data);

fastdeploy/vision/ocr/ppocr/ppocr_v2.cc

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,17 @@ bool PPOCRv2::Initialized() const {
7474
}
7575
return true;
7676
}
77+
78+
std::unique_ptr<PPOCRv2> PPOCRv2::Clone() const {
79+
std::unique_ptr<PPOCRv2> clone_model = utils::make_unique<PPOCRv2>(PPOCRv2(*this));
80+
clone_model->detector_ = detector_->Clone().release();
81+
if (classifier_ != nullptr) {
82+
clone_model->classifier_ = classifier_->Clone().release();
83+
}
84+
clone_model->recognizer_ = recognizer_->Clone().release();
85+
return clone_model;
86+
}
87+
7788
bool PPOCRv2::Predict(cv::Mat* img,
7889
fastdeploy::vision::OCRResult* result) {
7990
return Predict(*img, result);

fastdeploy/vision/ocr/ppocr/ppocr_v2.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "fastdeploy/vision/ocr/ppocr/dbdetector.h"
2525
#include "fastdeploy/vision/ocr/ppocr/recognizer.h"
2626
#include "fastdeploy/vision/ocr/ppocr/utils/ocr_postprocess_op.h"
27+
#include "fastdeploy/utils/unique_ptr.h"
2728

2829
namespace fastdeploy {
2930
/** \brief This pipeline can launch detection model, classification model and recognition model sequentially. All OCR pipeline APIs are defined inside this namespace.
@@ -52,6 +53,12 @@ class FASTDEPLOY_DECL PPOCRv2 : public FastDeployModel {
5253
PPOCRv2(fastdeploy::vision::ocr::DBDetector* det_model,
5354
fastdeploy::vision::ocr::Recognizer* rec_model);
5455

56+
/** \brief Clone a new PPOCRv2 with less memory usage when multiple instances of the same model are created
57+
*
58+
* \return new PPOCRv2* type unique pointer
59+
*/
60+
std::unique_ptr<PPOCRv2> Clone() const;
61+
5562
/** \brief Predict the input image and get OCR result.
5663
*
5764
* \param[in] im The input image data, comes from cv::imread(), is a 3-D array with layout HWC, BGR format.
@@ -69,6 +76,7 @@ class FASTDEPLOY_DECL PPOCRv2 : public FastDeployModel {
6976
*/
7077
virtual bool BatchPredict(const std::vector<cv::Mat>& images,
7178
std::vector<fastdeploy::vision::OCRResult>* batch_result);
79+
7280
bool Initialized() const override;
7381
bool SetClsBatchSize(int cls_batch_size);
7482
int GetClsBatchSize();
@@ -83,7 +91,6 @@ class FASTDEPLOY_DECL PPOCRv2 : public FastDeployModel {
8391
private:
8492
int cls_batch_size_ = 1;
8593
int rec_batch_size_ = 6;
86-
/// Launch the detection process in OCR.
8794
};
8895

8996
namespace application {

fastdeploy/vision/ocr/ppocr/ppocr_v3.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,20 @@ class FASTDEPLOY_DECL PPOCRv3 : public PPOCRv2 {
4949
// The only difference between v2 and v3
5050
recognizer_->GetPreprocessor().rec_image_shape_[1] = 48;
5151
}
52+
53+
/** \brief Clone a new PPOCRv3 with less memory usage when multiple instances of the same model are created
54+
*
55+
* \return new PPOCRv3* type unique pointer
56+
*/
57+
std::unique_ptr<PPOCRv3> Clone() const {
58+
std::unique_ptr<PPOCRv3> clone_model = utils::make_unique<PPOCRv3>(PPOCRv3(*this));
59+
clone_model->detector_ = detector_->Clone().release();
60+
if (classifier_ != nullptr) {
61+
clone_model->classifier_ = classifier_->Clone().release();
62+
}
63+
clone_model->recognizer_ = recognizer_->Clone().release();
64+
return clone_model;
65+
}
5266
};
5367

5468
} // namespace pipeline

fastdeploy/vision/ocr/ppocr/recognizer.cc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,12 @@ bool Recognizer::Initialize() {
5656
return true;
5757
}
5858

59+
std::unique_ptr<Recognizer> Recognizer::Clone() const {
60+
std::unique_ptr<Recognizer> clone_model = utils::make_unique<Recognizer>(Recognizer(*this));
61+
clone_model->SetRuntime(clone_model->CloneRuntime());
62+
return clone_model;
63+
}
64+
5965
bool Recognizer::Predict(const cv::Mat& img, std::string* text, float* rec_score) {
6066
std::vector<std::string> texts(1);
6167
std::vector<float> rec_scores(1);

fastdeploy/vision/ocr/ppocr/recognizer.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "fastdeploy/vision/ocr/ppocr/utils/ocr_postprocess_op.h"
2020
#include "fastdeploy/vision/ocr/ppocr/rec_preprocessor.h"
2121
#include "fastdeploy/vision/ocr/ppocr/rec_postprocessor.h"
22+
#include "fastdeploy/utils/unique_ptr.h"
2223

2324
namespace fastdeploy {
2425
namespace vision {
@@ -43,8 +44,16 @@ class FASTDEPLOY_DECL Recognizer : public FastDeployModel {
4344
const std::string& label_path = "",
4445
const RuntimeOption& custom_option = RuntimeOption(),
4546
const ModelFormat& model_format = ModelFormat::PADDLE);
47+
4648
/// Get model's name
4749
std::string ModelName() const { return "ppocr/ocr_rec"; }
50+
51+
/** \brief Clone a new Recognizer with less memory usage when multiple instances of the same model are created
52+
*
53+
* \return new Recognizer* type unique pointer
54+
*/
55+
virtual std::unique_ptr<Recognizer> Clone() const;
56+
4857
/** \brief Predict the input image and get OCR recognition model result.
4958
*
5059
* \param[in] img The input image data, comes from cv::imread(), is a 3-D array with layout HWC, BGR format.
@@ -53,6 +62,7 @@ class FASTDEPLOY_DECL Recognizer : public FastDeployModel {
5362
* \return true if the prediction is successed, otherwise false.
5463
*/
5564
virtual bool Predict(const cv::Mat& img, std::string* text, float* rec_score);
65+
5666
/** \brief BatchPredict the input image and get OCR recognition model result.
5767
*
5868
* \param[in] images The list of input image data, comes from cv::imread(), is a 3-D array with layout HWC, BGR format.
@@ -62,6 +72,7 @@ class FASTDEPLOY_DECL Recognizer : public FastDeployModel {
6272
*/
6373
virtual bool BatchPredict(const std::vector<cv::Mat>& images,
6474
std::vector<std::string>* texts, std::vector<float>* rec_scores);
75+
6576
virtual bool BatchPredict(const std::vector<cv::Mat>& images,
6677
std::vector<std::string>* texts, std::vector<float>* rec_scores,
6778
size_t start_index, size_t end_index,

0 commit comments

Comments
 (0)