|
7 | 7 |
|
8 | 8 | ## 引言 |
9 | 9 |
|
10 | | -因为版面分析模型输入图像尺寸固定,故可使用`onnxruntime-gpu`来提速。因为`rapid_layout`库默认依赖是CPU版`onnxruntime`,如果想要使用GPU推理,需要手动安装`onnxruntime-gpu`。详细使用和评测可参见[AI Studio](https://aistudio.baidu.com/projectdetail/8094594) |
| 10 | +版面分析支持多种推理引擎与设备: |
11 | 11 |
|
12 | | -## 使用GPU |
| 12 | +- **ONNX Runtime**:默认引擎,支持 CPU / CUDA / DirectML / CANN,需按需安装对应包。 |
| 13 | +- **OpenVINO**:可选,`pip install openvino` 后通过 `engine_type=EngineType.OPENVINO` 使用。 |
| 14 | + |
| 15 | +默认依赖为 CPU 版 `onnxruntime`;使用 GPU 推理需手动安装 `onnxruntime-gpu`。详细使用和评测可参见 [AI Studio](https://aistudio.baidu.com/projectdetail/8094594)。 |
| 16 | + |
| 17 | +## 使用 ONNX Runtime + GPU (CUDA) |
13 | 18 |
|
14 | 19 | ```bash |
15 | 20 | pip install rapid_layout |
16 | | - |
17 | 21 | # 请确保 onnxruntime-gpu 与当前 GPU/CUDA 版本对应 |
18 | 22 | # 参见 https://onnxruntime.ai/docs/execution-providers/CUDA-ExecutionProvider.html#requirements |
19 | 23 | pip install onnxruntime-gpu |
20 | 24 | ``` |
21 | 25 |
|
22 | | -## 使用 |
23 | | - |
24 | 26 | ```python linenums="1" |
25 | 27 | from rapid_layout import EngineType, ModelType, RapidLayout, RapidLayoutInput |
26 | 28 |
|
27 | 29 | cfg = RapidLayoutInput( |
28 | 30 | model_type=ModelType.PP_LAYOUT_CDLA, |
29 | 31 | engine_type=EngineType.ONNXRUNTIME, |
30 | | - engine_cfg={"use_cuda": True, "cuda_ep_cfg.gpu_id": 1}, |
| 32 | + engine_cfg={"use_cuda": True, "cuda_ep_cfg": {"device_id": 0}}, |
31 | 33 | ) |
32 | 34 | layout_engine = RapidLayout(cfg=cfg) |
33 | 35 |
|
34 | 36 | img_path = "https://raw.githubusercontent.com/RapidAI/RapidLayout/718b60e927ab893c2fad67c98f753b2105a6f421/tests/test_files/layout.jpg" |
35 | 37 | results = layout_engine(img_path) |
36 | 38 | print(results) |
| 39 | +results.vis("layout_res.png") |
| 40 | +``` |
| 41 | + |
| 42 | +多卡时可通过 `cuda_ep_cfg.device_id` 指定卡号(与 [engine_cfg.yaml](https://github.com/RapidAI/RapidLayout/blob/main/rapid_layout/configs/engine_cfg.yaml) 中 `cuda_ep_cfg.device_id` 一致)。 |
| 43 | + |
| 44 | +## 使用 OpenVINO |
| 45 | + |
| 46 | +```bash |
| 47 | +pip install rapid-layout onnxruntime openvino |
| 48 | +``` |
| 49 | + |
| 50 | +```python linenums="1" |
| 51 | +from rapid_layout import EngineType, ModelType, RapidLayout |
37 | 52 |
|
| 53 | +layout_engine = RapidLayout( |
| 54 | + model_type=ModelType.PP_LAYOUT_CDLA, |
| 55 | + engine_type=EngineType.OPENVINO, |
| 56 | +) |
| 57 | +img_path = "https://raw.githubusercontent.com/RapidAI/RapidLayout/718b60e927ab893c2fad67c98f753b2105a6f421/tests/test_files/layout.jpg" |
| 58 | +results = layout_engine(img_path) |
| 59 | +print(results) |
38 | 60 | results.vis("layout_res.png") |
39 | 61 | ``` |
40 | 62 |
|
41 | | -## 使用NPU |
| 63 | +OpenVINO 设备与线程等配置见 [engine_cfg.yaml](https://github.com/RapidAI/RapidLayout/blob/main/rapid_layout/configs/engine_cfg.yaml) 中 `openvino` 段。 |
42 | 64 |
|
43 | | -详细配置参数参见:[engine_cfg.yaml](https://github.com/RapidAI/RapidLayout/blob/a7ab63ff291bd72e1a98ac2bb11860575514f432/rapid_layout/configs/engine_cfg.yaml) |
| 65 | +## 使用 NPU (CANN) |
| 66 | + |
| 67 | +详细配置参数参见:[engine_cfg.yaml](https://github.com/RapidAI/RapidLayout/blob/main/rapid_layout/configs/engine_cfg.yaml) |
44 | 68 |
|
45 | 69 | ```python linenums="1" |
46 | 70 | from rapid_layout import EngineType, ModelType, RapidLayout, RapidLayoutInput |
47 | 71 |
|
48 | 72 | cfg = RapidLayoutInput( |
49 | 73 | model_type=ModelType.PP_LAYOUT_CDLA, |
50 | 74 | engine_type=EngineType.ONNXRUNTIME, |
51 | | - engine_cfg={"use_cann": True, "cann_ep_cfg.gpu_id": 0}, |
| 75 | + engine_cfg={"use_cann": True, "cann_ep_cfg": {"device_id": 0}}, |
52 | 76 | ) |
53 | 77 | layout_engine = RapidLayout(cfg=cfg) |
54 | 78 |
|
55 | 79 | img_path = "https://raw.githubusercontent.com/RapidAI/RapidLayout/718b60e927ab893c2fad67c98f753b2105a6f421/tests/test_files/layout.jpg" |
56 | 80 | results = layout_engine(img_path) |
57 | 81 | print(results) |
58 | | - |
59 | 82 | results.vis("layout_res.png") |
60 | 83 | ``` |
0 commit comments