Skip to content

Commit

Permalink
Add Initialize function to PP-OCR (PaddlePaddle#326)
Browse files Browse the repository at this point in the history
* Imporve OCR Readme

* Improve OCR Readme

* Improve OCR Readme

* Improve OCR Readme

* Improve OCR Readme

* Add Initialize function to PP-OCR

* Add Initialize function to PP-OCR

* Add Initialize function to PP-OCR

Co-authored-by: Jason <[email protected]>
  • Loading branch information
yunyaoXYY and jiangjiajun authored Oct 9, 2022
1 parent 56730a6 commit ff5e798
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 0 deletions.
5 changes: 5 additions & 0 deletions examples/vision/ocr/PP-OCRv2/cpp/infer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ void InitAndInfer(const std::string& det_model_dir, const std::string& cls_model
// auto ocr_system_v2 = fastdeploy::application::ocrsystem::PPOCRSystemv2(&det_model, &rec_model);
auto ocr_system_v2 = fastdeploy::application::ocrsystem::PPOCRSystemv2(&det_model, &cls_model, &rec_model);

if(!ocr_system_v2.Initialized()){
std::cerr << "Failed to initialize OCR system." << std::endl;
return;
}

auto im = cv::imread(image_file);
auto im_bak = im.clone();

Expand Down
5 changes: 5 additions & 0 deletions examples/vision/ocr/PP-OCRv3/cpp/infer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ void InitAndInfer(const std::string& det_model_dir, const std::string& cls_model
// auto ocr_system_v3 = fastdeploy::application::ocrsystem::PPOCRSystemv3(&det_model, &rec_model);
auto ocr_system_v3 = fastdeploy::application::ocrsystem::PPOCRSystemv3(&det_model, &cls_model, &rec_model);

if(!ocr_system_v3.Initialized()){
std::cerr << "Failed to initialize OCR system." << std::endl;
return;
}

auto im = cv::imread(image_file);
auto im_bak = im.clone();

Expand Down
38 changes: 38 additions & 0 deletions examples/vision/ocr/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,41 @@
| ch_PP-OCRv2_mobile |[ch_ppocr_mobile_v2.0_det](https://bj.bcebos.com/paddlehub/fastdeploy/ch_ppocr_mobile_v2.0_det_infer.tar.gz) | [ch_ppocr_mobile_v2.0_cls](https://bj.bcebos.com/paddlehub/fastdeploy/ch_ppocr_mobile_v2.0_cls_infer.tar.gz) | [ch_ppocr_mobile_v2.0_rec](https://bj.bcebos.com/paddlehub/fastdeploy/ch_ppocr_mobile_v2.0_rec_infer.tar.gz) | [ppocr_keys_v1.txt](https://bj.bcebos.com/paddlehub/fastdeploy/ppocr_keys_v1.txt) | OCRv2系列原始超轻量模型,支持中英文、多语种文本检测,比PPOCRv2更加轻量 |
| ch_PP-OCRv2_server |[ch_ppocr_server_v2.0_det](https://bj.bcebos.com/paddlehub/fastdeploy/ch_ppocr_server_v2.0_det_infer.tar.gz) | [ch_ppocr_mobile_v2.0_cls](https://bj.bcebos.com/paddlehub/fastdeploy/ch_ppocr_mobile_v2.0_cls_infer.tar.gz) | [ch_ppocr_server_v2.0_rec](https://bj.bcebos.com/paddlehub/fastdeploy/ch_ppocr_server_v2.0_rec_infer.tar.gz) |[ppocr_keys_v1.txt](https://bj.bcebos.com/paddlehub/fastdeploy/ppocr_keys_v1.txt) | OCRv2服务器系列模型, 支持中英文、多语种文本检测,比超轻量模型更大,但效果更好|

### OCR 模型的处理说明

为了让OCR系列模型在FastDeploy多个推理后端上正确推理,以上表格中的部分模型的输入shape,和PaddleOCR套件提供的模型有差异.
例如,由PaddleOCR套件库提供的英文版PP-OCRv3_det模型,输入的shape是`[-1,3,960,960]`, 而FastDeploy提供的此模型输入shape为`[-1,3,-1,-1]`.

**差异存在的原因**: 当我们在ORT和OpenVINO上部署输入shape固定的模型时(指定了高和宽),由于OCR的输入图片尺寸是变化的,会报例如下面所示的错误,导致无法推理:
```
Failed to Infer: Got invalid dimensions for input: x for the following indices
index: 3 Got: 608 Expected: 960
```
**解决办法**:除了直接下载FastDeploy提供的模型外,用户还可以使用如下工具仓库, 修改模型的输入shape.

**仓库链接**: https://github.com/jiangjiajun/PaddleUtils

使用示例如下:
```
#该用例将en_PP-OCRv3_det_infer模型的输入shape, 改为[-1,3,-1,-1], 并将新模型存放至output文件夹下
git clone [email protected]:jiangjiajun/PaddleUtils.git
cd paddle
python paddle_infer_shape.py --model_dir en_PP-OCRv3_det_infer/ \
--model_filename inference.pdmodel \
--params_filename inference.pdiparams \
--save_dir output \
--input_shape_dict="{'x':[-1,3,-1,-1]}"
```

#### OCR模型输入shape更改记录
以下表格记录了FastDeploy修改过的OCR模型的输入`('输入名':[shape])`, 供用户参考.

| OCR版本 | 模型 | 修改前 | 修改后 |
|:----|:----|:----|:----|
|PPOCRv3 |en_PP-OCRv3_det|'x':[-1,3,960,960]|'x':[-1,3,-1,-1]|
|PPOCRv2 |ch_PP-OCRv2_det|'x':[-1,3,960,960]|'x':[-1,3,-1,-1]|
|PPOCRv2 |ch_PP-OCRv2_rec|'x':[-1,3,32,100]|'x':[-1,3,-1,-1]|
|PPOCRv2_mobile |ch_ppocr_mobile_v2.0_det|'x':[-1,3,640,640]|'x':[-1,3,-1,-1]|
|PPOCRv2_mobile|ch_ppocr_mobile_v2.0_rec|'x':[-1,3,32,100]|'x':[-1,3,-1,-1]|
|PPOCRv2_server|ch_ppocr_server_v2.0_det|'x':[-1,3,640,640]|'x':[-1,3,-1,-1]|
|PPOCRv2_server |ch_ppocr_server_v2.0_rec|'x':[-1,3,32,100]|'x':[-1,3,-1,-1]|
16 changes: 16 additions & 0 deletions fastdeploy/vision/ocr/ppocr/ppocr_system_v2.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,22 @@ PPOCRSystemv2::PPOCRSystemv2(fastdeploy::vision::ocr::DBDetector* det_model,
recognizer_->rec_image_shape[1] = 32;
}

bool PPOCRSystemv2::Initialized() const {

if (detector_ != nullptr && !detector_->Initialized()){
return false;
}

if (classifier_ != nullptr && !classifier_->Initialized()){
return false;
}

if (recognizer_ != nullptr && !recognizer_->Initialized()){
return false;
}
return true;
}

bool PPOCRSystemv2::Detect(cv::Mat* img,
fastdeploy::vision::OCRResult* result) {
if (!detector_->Predict(img, &(result->boxes))) {
Expand Down
1 change: 1 addition & 0 deletions fastdeploy/vision/ocr/ppocr/ppocr_system_v2.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class FASTDEPLOY_DECL PPOCRSystemv2 : public FastDeployModel {
fastdeploy::vision::ocr::Recognizer* rec_model);

virtual bool Predict(cv::Mat* img, fastdeploy::vision::OCRResult* result);
bool Initialized() const override;

protected:
fastdeploy::vision::ocr::DBDetector* detector_ = nullptr;
Expand Down

0 comments on commit ff5e798

Please sign in to comment.