# paddlepaddleocr **Repository Path**: eveyin/paddlepaddleocr ## Basic Information - **Project Name**: paddlepaddleocr - **Description**: paddlepaddleocr - **Primary Language**: Python - **License**: Apache-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 5 - **Created**: 2023-08-17 - **Last Updated**: 2023-08-17 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # PaddleOCR Direction classifier inference model 模型推理指导 #### 概述 PP-OCRv3模型支持中英文混合识别,可以自动识别文本语言,并根据语言特点进行优化。此外,它还支持多种字体、字号、倾斜角度等复杂情况下的识别,具有较强的鲁棒性和适应性。总之,PP-OCRv3是一种高效、准确、轻量级的OCR模型,适用于各种移动设备上的文字识别应用。 - 参考实现 ``` url=https://github.com/PaddlePaddle/PaddleOCR commit_id=?? model_name=Chinese and English ultra-lightweight PP-OCRv3 model ``` ## 0 预置条件: ### 原始模型 ``` |--- ch_PP-OCRv3_det_infer/ |--- |--- inference.pdiparams |--- |--- inference.pdiparams.info |--- |--- inference.pdmodel ``` 原始模型获取链接:[ch_PP-OCRv3_det_infer](https://paddleocr.bj.bcebos.com/PP-OCRv3/chinese/ch_PP-OCRv3_det_infer.tar) ### .om模型推理环境: - 操作系统:ubuntu 18.04 - CPU架构:`aarch64` - NPU型号及驱动版本: ``` +--------------------------------------------------------------------------------------------------------+ | npu-smi 23.0.RC1.b010 Version: 23.0.RC1.b010 | +-------------------------------+-----------------+------------------------------------------------------+ | NPU Name | Health | Power(W) Temp(C) Hugepages-Usage(page) | | Chip Device | Bus-Id | AICore(%) Memory-Usage(MB) | +===============================+=================+======================================================+ | 2048 310P3 | OK | NA 46 0 / 0 | | 0 0 | 0000:81:00.0 | 0 4023 / 21527 | +===============================+=================+======================================================+ ``` - 关键包版本: ``` python: 3.7.5 CANN:6.3T200 ``` ### .engine模型推理环境: - 操作系统:ubuntu 18.04 - CPU架构:`x86_64` - GPU型号及驱动版本: ``` +-----------------------------------------------------------------------------+ | NVIDIA-SMI 525.60.13 Driver Version: 525.60.13 CUDA Version: 12.0 | |-------------------------------+----------------------+----------------------+ | GPU Name Persistence-M| Bus-Id Disp.A | Volatile Uncorr. ECC | | Fan Temp Perf Pwr:Usage/Cap| Memory-Usage | GPU-Util Compute M. | | | | MIG M. | |===============================+======================+======================| | 0 Tesla T4 Off | 00000000:5E:00.0 Off | 0 | | N/A 63C P0 29W / 70W | 5676MiB / 15360MiB | 0% Default | | | | N/A | +-------------------------------+----------------------+----------------------+ ``` - 关键包版本: ``` Python - 3.8 onnx -1.14.0 onnxruntime - 1.15.0 paddlepaddle - 2.4.2 paddleocr - 2.6.1.3 paddle2onnx - 1.0.6 ``` ## 1 模型输入输出数据 - 输入数据 |输入数据|数据类型|大小|数据排布格式| |----|----|----|----| |input| RGB_FP32|batchsize x 3 x width x height|NCHW| - 输出数据 |输入数据|数据类型|大小|数据排布格式| |----|----|----|----| |output1|FLOAT32|batchsize x 2|ND| ## 2 推理全流程 ### 2.0 流程总览 ```mermaid graph LR subgraph 整体流程 direction LR A(paddle模型) --paddle2onnx模型转换--> B(onnx模型) B --atc模型转换--> C(om模型) B --trtexec模型转换--> D(engine模型) C --模型推理:ait benchmark--> E(1.2倍性能提升) D --模型推理:ntrtexec--> E end ``` ### 2.1 获取PaddleOCR Direction Classify的onnx模型 - 获取paddlepaddle模型: ``` # 下载ch_PP-OCRv3_det_infer模型的压缩包 wget https://paddleocr.bj.bcebos.com/PP-OCRv3/chinese/ch_PP-OCRv3_det_infer.tar --no-check-certificate # 解压当前文件夹目录 tar -xf ch_PP-OCRv3_det_infer.tar ``` - 安装paddle2onnx ``` pip3 install paddle2onnx==1.0.6 ``` - 将paddlepaddle模型转换成onnx模型 ``` paddle2onnx --model_dir .\infer\ch_PP-OCRv3_det_infer --model_filename inference.pdmodel --params_filename inference.pdiparams --save_file .\infer\det_onnx\ocr_det.onnx --opset_version 10 --input_shape_dict="{'x':[-1,3,-1,-1]}" --enable_onnx_checker True ``` ### 2.2 准备不同场景om模型 将第一阶段转换成功的ONNX模型使用ATC工具转换为om模型,并在Ascend310P3上使用ais_bench进行性能推理测试。 #### 2.2.1 使用onnxsim将导出的动态shape模型固定输入 ``` onnxsim ocr_det.onnx ocr_det_sim.onnx --overwrite-input-shape "x:1,3,640,640" ``` #### 2.2.2 ATC工具将ONNX模型转换为om模型 ``` # input shape:"x:1,3,224,224" atc --model cls_onnx/ocr_cls.onnx \ --framework 5 \ --soc_version Ascend310P3 \ --output ocr_det \ --input_shape "x:1,3,224,224" ``` #### 2.2.3 AOE工具将ONNX模型转换为om模型(算子流水优化) ``` aoe -f 5 -m ocr_det_sim.onnx -j 2 --output ocr_aoe ``` #### 2.2.4 使用ait benchmark对om模型进行性能推理测试 ``` ait benchmark -om ocr_det.om --loop 1000 ``` #### 2.2.5 对AOE转换所得模型进行性能测试 ``` ait benchmark -om ocr_aoe.om --loop 1000 ``` ### 2.3 使用trtexec工具将模型转换为.engine格式,在GPU上进行性能推理测试 工作内容为在T4服务器上安装好PaddlePaddle-GPU环境,对相关模型进行性能推理测试,而后转换为ONNX格式,再使用trtexec工具将其转换为.engine格式,在GPU上进行性能推理测试。 #### 2.3.1 trtexec工具对ONNX模型进行转换 ``` trtexec --onnx=shijh/ocr_det.onnx --saveEngine=shijh/ocr_det.engine --exportOutput=shijh/trtexec-ocr-det-result.json ``` #### 2.3.2 trtexec对engine文件进行推理 ``` trtexec --loadEngine=shijh/ocr_det.engine --exportOutput=shijh/trtexec-ocr-det-result.json --iterations=1000 ``` #### 2.3.3 T4与Ascend310P3性能对比 | 平台 | 静态模型输入 | 吞吐率 | | --- | --- | --- | | Ascend310P3(ATC转换) | 1 * 3 * 640 * 640 | **678** | | Ascend310P3(AOE转换) | 1 * 3 * 640 * 640 | 666| | NVIDIA T4 | 1 * 3 * 640 * 640 | **331** | ### 2.4 准备测试数据与获得om模型性能与精度 #### 2.4.1 准备测试数据 在PaddleOCR[ICDAR2015数据集](https://rrc.cvc.uab.es/?ch=4&com=downloads)作为测试数据集,该数据集中的**测试集**包括专为文本检测准备的用可穿戴设备采集的**500**张**720 * 1280**的三通道RGB图片 #### 2.4.2 精度测试指标选择 选取文本检测领域的权威指标**H-mean**(参考论文:[TedEval: A Fair Evaluation Metric for Scene Text Detectors](https://arxiv.org/abs/1907.01227))对该模型进行精度测量,且同时记录其检测结果的**Precision与Recall**。 #### 2.4.3 对ICDAR2015数据集中测试集进行标签处理 ```bash # Convert the label file downloaded from the official website to train_icdar2015_label.txt python gen_label.py --mode="det" --root_path="train_data/icdar2015/text_localization/icdar2015_test" --input_path="/path/to/ch4_training_localization_transcription_gt" --output_label="train_data/icdar2015/text_localization/test_icdar2015_label.txt" ``` #### 2.4.4 在PaddlePaddle框架下进行带数据集的推理测试 ```bash python tools/infer/predict_det.py --det_algorithm="DB" --det_model_dir="infer/ch_PP-OCRv3_det_infer" --image_dir="train_data/icdar2015/text_localization/icdar2015_test" --use_gpu=False ``` ### 3 OCR-TextDetection模型在ICDAR2015数据集上的推理精度 -- Ascend310P3 #### 3.1. 构建NPY测试数据集 在原PaddlePaddle框架下的测试代码中,对前处理之后的图片数据保存为NPY文件,构建测试数据集。由于后处理中对bbox的处理需要记录测试图片的shape相关信息,所以在此步骤中另需要保存每张图片的shape_list信息 #### 3.2. 使用ait benchmark在Ascend310P3上对om模型进行推理测试 在环境上安装好ait benchmark组件,ait 安装方法参考:https://gitee.com/ascend/ait/blob/master/ait/docs/install/README.md ```bash ait benchmark -om assets/ocr621.om -i om_infer_dataset/ -o ocr_infer_output/ --outfmt NPY ``` #### 3.3. 对推理结果进行后处理 ```python import os import json import numpy as np from ppocr.postprocess import build_post_process def filter_tag_det_res(dt_boxes, image_shape): img_height, img_width = image_shape[0:2] dt_boxes_new = [] for box in dt_boxes: if type(box) is list: box = np.array(box) box = order_points_clockwise(box) box = clip_det_res(box, img_height, img_width) rect_width = int(np.linalg.norm(box[0] - box[1])) rect_height = int(np.linalg.norm(box[0] - box[3])) if rect_width <= 3 or rect_height <= 3: continue dt_boxes_new.append(box) dt_boxes = np.array(dt_boxes_new) return dt_boxes def order_points_clockwise(pts): rect = np.zeros((4, 2), dtype="float32") s = pts.sum(axis=1) rect[0] = pts[np.argmin(s)] rect[2] = pts[np.argmax(s)] tmp = np.delete(pts, (np.argmin(s), np.argmax(s)), axis=0) diff = np.diff(np.array(tmp), axis=1) rect[1] = tmp[np.argmin(diff)] rect[3] = tmp[np.argmax(diff)] return rect def clip_det_res(points, img_height, img_width): for pno in range(points.shape[0]): points[pno, 0] = int(min(max(points[pno, 0], 0), img_width - 1)) points[pno, 1] = int(min(max(points[pno, 1], 0), img_height - 1)) return points if __name__ == '__main__': # load infer result and shape list info om_infer_res_path = 'train_data/icdar2015/2023_06_25-10_37_29' om_infer_shape_list_path = 'train_data/icdar2015/om_infer_shape_lists' om_infer_res = os.listdir(om_infer_res_path) om_infer_shape_list = os.listdir(om_infer_shape_list_path) om_infer_res.sort() om_infer_shape_list.sort() postprocess_params = dict() postprocess_params['name'] = 'DBPostProcess' postprocess_params["thresh"] = 0.3 postprocess_params["box_thresh"] = 0.6 postprocess_params["max_candidates"] = 1000 postprocess_params["unclip_ratio"] = 1.5 postprocess_params["use_dilation"] = False postprocess_params["score_mode"] = "fast" postprocess_params["box_type"] = "quad" postprocess_op = build_post_process(postprocess_params) save_results = [] for res, shape_list in zip(om_infer_res, om_infer_shape_list): res_npy_path = os.path.join(om_infer_res_path, res) shape_list_npy_path = os.path.join(om_infer_shape_list_path, shape_list) res_npy = np.load(res_npy_path) shape_list_npy = np.load(shape_list_npy_path) preds = dict() preds['maps'] = res_npy post_result = postprocess_op(preds, shape_list_npy) dt_boxes = post_result[0]['points'] save_pred = os.path.basename(res_npy_path) + '\t' + str( json.dumps([x.tolist() for x in dt_boxes]) ) + '\n' save_results.append(save_pred) with open("det_results.txt", 'w') as f: f.writelines(save_results) f.close() ``` ### 4 测量推理结果的Precision、Recall以及H-mean值 参考[TedEval](https://github.com/clovaai/TedEval#tedeval-a-fair-evaluation-metric-for-scene-text-detectors)相关实现,需对文本检测的结果处理为如下格式(自行构造脚本处理),之后方可使用上述测量结果脚本进行指标测量: ``` # gt/gt_img_38.txt 644,101,932,113,932,168,643,156,concierge@L3 477,138,487,139,488,149,477,148,### 344,131,398,130,398,149,344,149,### 1195,148,1277,138,1277,177,1194,187,### 23,270,128,267,128,282,23,284,### # result/res_img_38.txt 644,101,932,113,932,168,643,156,{Transcription},{Confidence} 477,138,487,139,488,149,477,148 344,131,398,130,398,149,344,149 1195,148,1277,138,1277,177,1194,187 23,270,128,267,128,282,23,284 ``` 测试脚本: ```bash python script.py -g=gt/icdar2015_test_gt.zip -s=gt/icdar2015_test_res.zip ``` ### 5 实验总结 #### 5.1 性能测试对比
版本平台输入吞吐率
2.6(latest)Ascend310P3(ATC转换)1 * 3 * 640 * 640678
Ascend310P3(AOE转换)1 * 3 * 640 * 640待更新
NVIDIA T41 * 3 * 640 * 640331
2.5Ascend310P3(ATC转换)1 * 3 * 640 * 640677
Ascend310P3(AOE转换)1 * 3 * 640 * 640待更新
NVIDIA T41 * 3 * 640 * 640328
2.4Ascend310P3(ATC转换)1 * 3 * 640 * 640754
Ascend310P3(AOE转换)1 * 3 * 640 * 640待更新
NVIDIA T41 * 3 * 640 * 640390
2.3Ascend310P3(ATC转换)1 * 3 * 640 * 640723
Ascend310P3(AOE转换)1 * 3 * 640 * 640待更新
NVIDIA T41 * 3 * 640 * 640338
#### 5.2 精度测试对比
版本平台PrecisionRecallH-mean
2.6(latest)PaddlePaddle-CPU0.7790.6460.714
Ascend310P30.8000.6450.715
2.5PaddlePaddle-CPU0.7790.6460.714
Ascend310P30.8000.6460.715
2.4PaddlePaddle-CPU0.8280.5220.640
Ascend310P30.8250.5210.639
2.3PaddlePaddle-CPU0.8280.5220.640
Ascend310P30.8250.5210.639
![image](https://wiki.huawei.com/vision-file-storage/api/file/download/upload-v2/2023/5/25/20230625T1519Z/c21a36b2bf3a4afeb4e1b50b4ef5edbb/image.png)