mirror of
https://github.com/RapidAI/RapidOCR.git
synced 2026-11-13 14:31:00 +08:00
organzie documents to wiki
This commit is contained in:
1 parent
79eb2d3787
commit
ae529c2ba7
9 files changed
+7
-714
No files matched your search
@@ -13,10 +13,6 @@
|
||||
- [简介和说明](#简介和说明)
|
||||
- [(推荐)pip安装快速使用](#推荐pip安装快速使用)
|
||||
- [源码使用步骤](#源码使用步骤)
|
||||
- [`config.yaml`中常用参数介绍](#configyaml中常用参数介绍)
|
||||
- [onnxruntime-gpu版相关说明](#onnxruntime-gpu版相关说明)
|
||||
- [onnxruntime-gpu版推理配置](#onnxruntime-gpu版推理配置)
|
||||
- [OpenVINO GPU推理配置](#openvino-gpu推理配置)
|
||||
</details>
|
||||
|
||||
|
||||
@@ -249,138 +245,3 @@
|
||||
```bash
|
||||
python demo.py
|
||||
```
|
||||
|
||||
### [`config.yaml`](https://github.com/RapidAI/RapidOCR/blob/main/python/rapidocr_onnxruntime/config.yaml)中常用参数介绍
|
||||
- `Global`部分
|
||||
| 参数名称 | 取值范围 | 默认值 | 作用 |
|
||||
|------------: | :----------: | :-----: | :----------------------------------------------|
|
||||
| `text_score` | [0, 1] | 0.5 | 文本识别结果置信度,值越大,把握越大 |
|
||||
| `use_angle_cls` | `bool` | `true` | 是否使用文本行的方向分类 |
|
||||
| `print_verbose` | `bool` | `true` | 是否打印各个部分耗时信息 |
|
||||
| `min_height` | `int` | 30 | 图像最小高度(单位是像素)<br/>低于这个值,会跳过文本检测阶段,直接进行后续识别 |
|
||||
|`width_height_ratio`| `int`| 8| 如果输入图像的宽高比大于`width_height_ratio`,则会跳过文本检测,直接进行后续识别<br/>`width_height_ratio=-1`:不用这个参数 |
|
||||
|
||||
- `min_height`是用来过滤只有一行文本的图像(如下图),这类图像不会进入文本检测模块,直接进入后续过程。
|
||||
|
||||

|
||||
|
||||
- `Det`部分
|
||||
| 参数名称 | 取值范围 | 默认值 | 作用 |
|
||||
| ------------: | :----------: | :-----: | :----------------------------------------------|
|
||||
| `use_cuda` | `bool` | `false` | 是否使用CUDA,加速推理 |
|
||||
|`limit_side_len`| - | 736 | 限制图像边的长度的像素值 |
|
||||
|`limit_type`| `[min, max]` | `min` | 限制图像的最小边长度还是最大边为`limit_side_len` <br/> 示例解释:当`limit_type=min`和`limit_side_len=736`时,图像最小边小于736时,<br/>会将图像最小边拉伸到736,另一边则按图像原始比例等比缩放。 |
|
||||
| `thresh` | [0, 1] | 0.3 | 图像中文字部分和背景部分分割阈值<br/>值越大,文字部分会越小 |
|
||||
| `box_thresh` | [0, 1] | 0.5 | 文本检测所得框是否保留的阈值,值越大,召回率越低 |
|
||||
|`max_candidates`| - | 1000 | 图像中最大可检测到的文本框数目,一般够用|
|
||||
| `unclip_ratio` | [1.6, 2.0] | 1.6 | 控制文本检测框的大小,值越大,检测框整体越大 |
|
||||
|`use_dilation`| `bool` | `true` | 是否使用形态学中的膨胀操作,一般采用默认值即可 |
|
||||
|`score_mode` | `string`| `fast` | `fast`是求rectangle区域的平均分数,容易造成弯曲文本漏检,`slow`是求polygon区域的平均分数,会更准确,但速度有所降低,可按需选择 |
|
||||
|
||||
- `Cls`部分
|
||||
| 参数名称 | 取值范围 | 默认值 | 作用 |
|
||||
| ------------: | :----------: | :-----: | :----------------------------------------------|
|
||||
|`cls_img_shape`| - |`[3, 48, 192]`| 输入方向分类模型的图像Shape(CHW) |
|
||||
|`cls_batch_num`| - | 6 | 批次推理的batch大小,一般采用默认值即可,太大并没有明显提速,效果还可能会差 |
|
||||
|`cls_thresh`|`[0, 1]`|0.9| 方向分类结果的置信度|
|
||||
|`label_list`| - | `[0, 180]` | 方向分类的标签,0°或者180°,**该参数不能动** |
|
||||
|
||||
- `Rec`部分
|
||||
| 参数名称 | 取值范围 | 默认值 | 作用 |
|
||||
| ------------: | :----------: | :-----: | :----------------------------------------------|
|
||||
|`rec_img_shape`| - |`[3, 48, 320]`| 输入文本识别模型的图像Shape(CHW) |
|
||||
|`rec_batch_num`| - | 6 | 批次推理的batch大小,一般采用默认值即可,太大并没有明显提速,效果还可能会差 |
|
||||
|
||||
### onnxruntime-gpu版相关说明
|
||||
- 目前已知在onnxruntime-gpu上测试过的小伙伴,反映都是GPU推理速度比在CPU上慢很多。经过探索,初步确定原因为onnxruntime在推理动态图输入时,速度就会慢很多。关于该问题,已经提了相关issue,具体可参见[onnxruntime issue#13198](https://github.com/microsoft/onnxruntime/issues/13198)
|
||||
- 为了便于比较onnxruntime上推理的基准比较,简单整理了一个[AI Studio: TestOrtInfer](https://aistudio.baidu.com/aistudio/projectdetail/4634684?contributionType=1&sUid=57084&shared=1&ts=1664700017761)项目,小伙伴想要测试的,可以直接Fork来运行查看。
|
||||
|
||||
### onnxruntime-gpu版推理配置
|
||||
1. **onnxruntime-gpu**需要严格按照与CUDA、cuDNN版本对应来安装,具体参考[文档](https://onnxruntime.ai/docs/execution-providers/CUDA-ExecutionProvider.html#requirements),**这一步关乎后面是否可以成功调用GPU**。
|
||||
- 以下是安装示例:
|
||||
- 所用机器环境情况:
|
||||
- `nvcc-smi`显示**CUDA Driver API**版本:11.7
|
||||
- `nccc -V`显示**CUDA Runtime API**版本:11.6
|
||||
- 以上两个版本的对应关系,可参考[博客](https://blog.csdn.net/weixin_39518984/article/details/111406728)
|
||||
- 具体安装命令如下:
|
||||
```bash
|
||||
conda install cudatoolkit=11.6.0
|
||||
conda install cudnn=8.3.2.44
|
||||
pip install onnxruntime-gpu==1.12.0
|
||||
```
|
||||
- 验证是否可以`onnxruntime-gpu`正常调用GPU
|
||||
1. 验证`get_device()`是否可返回GPU
|
||||
```python
|
||||
import onnxruntime as ort
|
||||
|
||||
print(ort.get_device())
|
||||
# GPU
|
||||
```
|
||||
2. 如果第一步满足了,继续验证`onnxruntime-gpu`加载模型时是否可以调用GPU
|
||||
```python
|
||||
import onnxruntime as ort
|
||||
|
||||
providers = [
|
||||
('CUDAExecutionProvider', {
|
||||
'device_id': 0,
|
||||
'arena_extend_strategy': 'kNextPowerOfTwo',
|
||||
'gpu_mem_limit': 2 * 1024 * 1024 * 1024,
|
||||
'cudnn_conv_algo_search': 'EXHAUSTIVE',
|
||||
'do_copy_in_default_stream': True,
|
||||
}),
|
||||
'CPUExecutionProvider',
|
||||
]
|
||||
|
||||
# download link: https://github.com/openvinotoolkit/openvino/files/9355419/super_resolution.zip
|
||||
model_path = 'super_resolution.onnx'
|
||||
session = ort.InferenceSession(model_path, providers=providers)
|
||||
|
||||
print(session.get_providers())
|
||||
# 如果输出中含有CUDAExecutionProvider,则证明可以正常调用GPU
|
||||
# ['CUDAExecutionProvider', 'CPUExecutionProvider']
|
||||
```
|
||||
2. 更改[`config.yaml`](https://github.com/RapidAI/RapidOCR/blob/main/python/rapidocr_onnxruntime/config.yaml)中对应部分的参数即可,详细参数介绍参见[官方文档](https://onnxruntime.ai/docs/execution-providers/CUDA-ExecutionProvider.html)。
|
||||
```yaml
|
||||
use_cuda: true
|
||||
CUDAExecutionProvider:
|
||||
device_id: 0
|
||||
arena_extend_strategy: kNextPowerOfTwo
|
||||
gpu_mem_limit: 2 * 1024 * 1024 * 1024
|
||||
cudnn_conv_algo_search: EXHAUSTIVE
|
||||
do_copy_in_default_stream: true
|
||||
```
|
||||
|
||||
3. 推理情况
|
||||
1. 下载基准测试数据集(`test_images_benchmark`),放到`tests/benchmark`目录下。
|
||||
- [百度网盘](https://pan.baidu.com/s/1R4gYtJt2G3ypGkLWGwUCKg?pwd=ceuo) | [Google Drive](https://drive.google.com/drive/folders/1IIOCcUXdWa43Tfpsiy6UQJmPsZLnmgFh?usp=sharing)
|
||||
- 最终目录结构如下:
|
||||
```text
|
||||
tests/benchmark/
|
||||
├── benchmark.py
|
||||
├── config_gpu.yaml
|
||||
├── config.yaml
|
||||
└── test_images_benchmark
|
||||
```
|
||||
2. 运行以下代码(`python`目录下运行):
|
||||
```shell
|
||||
# CPU
|
||||
python tests/benchmark/benchmark.py --yaml_path config.yaml
|
||||
|
||||
# GPU
|
||||
python tests/benchmark/benchmark.py --yaml_path config_gpu.yaml
|
||||
```
|
||||
3. 运行相关信息汇总:(以下仅为个人测试情况,具体情况请自行测试)
|
||||
- 环境
|
||||
|测试者|设备|OS|CPU|GPU|onnxruntime-gpu|
|
||||
|:--|:--|:--|:--|:--|:--|
|
||||
|[1][zhsunlight](https://github.com/zhsunlight)|宏碁(Acer) 暗影骑士·威N50-N93游戏台式机|Windows|十代i5-10400F 16G 512G SSD|NVIDIA GeForce GTX 1660Super 6G|1.11.0|
|
||||
|[2][SWHL](https://github.com/SWHL)|服务器|Linux|AMD R9 5950X|NVIDIA GeForce RTX 3090|1.12.1|
|
||||
- 耗时
|
||||
|对应上面序号|CPU总耗时(s)|CPU平均耗时(s/img)|GPU总耗时(s)|GPU平均耗时(s/img)||
|
||||
|:---:|:---:|:---:|:---:|:---:|:---:|
|
||||
|[1]|296.8841|1.18282|646.14667|2.57429|
|
||||
|[2]|149.35427|0.50504|250.81760|0.99927|
|
||||
|
||||
### OpenVINO GPU推理配置
|
||||
- 官方参考文档:[docs](https://docs.openvino.ai/latest/api/ie_python_api/_autosummary/openvino.runtime.Core.html?highlight=compile_model#openvino.runtime.Core.compile_model)
|
||||
- 考虑到openvino只能使用自家显卡推理,通用性不高,这里暂不作相关配置说明。
|
||||
@@ -1,97 +0,0 @@
|
||||
## rapidocr_openvino
|
||||
|
||||
❗❗❗注意:基于目前`openvino==2022.3.0`版,存在申请内存不释放的问题,这也就意味着当推理图像很大时,推理完之后,内存会一直占用。详情可参见[issue11939](https://github.com/openvinotoolkit/openvino/issues/11939)
|
||||
|
||||
<details open>
|
||||
<summary>目录</summary>
|
||||
|
||||
- [rapidocr\_openvino](#rapidocr_openvino)
|
||||
- [安装](#安装)
|
||||
- [模型问题](#模型问题)
|
||||
- [关于OpenVINO](#关于openvino)
|
||||
- [OpenVINO与ONNXRuntime性能对比](#openvino与onnxruntime性能对比)
|
||||
- [OpenVINO与ONNXRuntime推理代码写法对比](#openvino与onnxruntime推理代码写法对比)
|
||||
</details>
|
||||
|
||||
|
||||
#### 安装
|
||||
```bash
|
||||
$ pip install openvino
|
||||
|
||||
# 里面含有mo
|
||||
$ pip install openvino-dev
|
||||
```
|
||||
|
||||
#### 模型问题
|
||||
- 因为OpenVINO可以直接推理ONNX模型,故这里暂时不作转换,直接推理之前ONNX模型即可
|
||||
- 这里仍然给出转换的代码,用作参考:
|
||||
```bash
|
||||
mo --input_model models/ch_PP-OCRv2_det_infer.onnx --output_dir models/IR/
|
||||
|
||||
mo --input_model models/ch_PP-OCRv2_det_infer.onnx \
|
||||
--output_dir models/IR/static \
|
||||
--input_shape "[1,3,12128,800]"
|
||||
```
|
||||
|
||||
|
||||
#### 关于OpenVINO
|
||||
- OpenVINO可以直接推理IR、ONNX和PaddlePaddle模型,具体如下(图来源:[link](https://docs.openvino.ai/latest/openvino_docs_OV_UG_OV_Runtime_User_Guide.html#doxid-openvino-docs-o-v-u-g-o-v-runtime-user-guide)):
|
||||
|
||||
<div align="center">
|
||||
<img src="https://docs.openvino.ai/latest/_images/BASIC_FLOW_IE_C.svg">
|
||||
</div>
|
||||
|
||||
- 和ONNXRuntime同时推理同一个ONNX模型,OpenVINO推理速度更快
|
||||
- 但是从对比来看,OpenVINO占用内存更大,其原因是拿空间换的时间
|
||||
- 当指定`input_shape`在一个区间范围时,推理时内存占用会减少一些
|
||||
- 示例命令:
|
||||
```bash
|
||||
mo --input_model models/ch_PP-OCRv2_det_infer.onnx \
|
||||
--output_dir models/IR/static \
|
||||
--input_shape "[1,3,960:1200,800]"
|
||||
```
|
||||
|
||||
#### OpenVINO与ONNXRuntime性能对比
|
||||
- 推理设备:`Windows 64位 Intel(R) Core(TM) i5-4210M CPU @ 2.60GHz 2.59 GHz`
|
||||
- [测试图像宽高](https://drive.google.com/file/d/1iJcGvOVIdUlyOS52bBdvO8uzx8QORo5M/view?usp=sharing): `12119x810`
|
||||
|
||||
| 测试模型 | 推理框架 | 占用内存(3次平均) | 推理时间(3次平均) |
|
||||
| ------------------------------------ | -------------------- | ----------------- | ----------------- |
|
||||
| `ch_PP-OCRv2_det_infer.onnx` | `ONNXRuntime=1.10.0` | 0.8G | 5.354s |
|
||||
| `ch_PP-OCRv2_det_infer.onnx` | `openvino=2022.1.0` | 3.225G | 2.53s |
|
||||
| `ch_PP-OCRv2_det_infer.xml` FP32 动态图 | `openvino=2022.1.0` | 3.175G | 2.0455s |
|
||||
|
||||
|
||||
#### OpenVINO与ONNXRuntime推理代码写法对比
|
||||
NOTE: 以`ch_ppocr_mobile_v2_det`中推理代码为例子
|
||||
|
||||
- ONNXRuntime
|
||||
```python
|
||||
import onnxruntime
|
||||
|
||||
# 声明
|
||||
sess_opt = onnxruntime.SessionOptions()
|
||||
sess_opt.log_severity_level = 4
|
||||
sess_opt.enable_cpu_mem_arena = False
|
||||
session = onnxruntime.InferenceSession(det_model_path, sess_opt)
|
||||
input_name = session.get_inputs()[0].name
|
||||
output_name = session.get_outputs()[0].name
|
||||
|
||||
# 推理
|
||||
preds = session.run([output_name], {input_name: img})
|
||||
```
|
||||
|
||||
- OpenVINO
|
||||
```python
|
||||
from openvino.runtime import Core
|
||||
|
||||
# 初始化
|
||||
ie = Core()
|
||||
model_onnx = ie.read_model(det_model_path)
|
||||
compile_model = ie.compile_model(model=model_onnx, device_name='CPU')
|
||||
vino_session = compile_model.create_infer_request()
|
||||
|
||||
# 推理
|
||||
vino_session.infer(inputs=[img])
|
||||
vino_preds = vino_session.get_output_tensor().data
|
||||
```
|
||||
Reference in new issue
Block a user