Skip to content

Commit 65518bc

Browse files
authored
Merge pull request #25 from RapidAI/develop
feat: refacter code
2 parents 5c5efbc + 0f109af commit 65518bc

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+1508
-984
lines changed

.github/workflows/publish_whl.yml

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,8 @@ jobs:
2626

2727
- name: Unit testings
2828
run: |
29-
wget $RESOURCES_URL
30-
ZIP_NAME=${RESOURCES_URL##*/}
31-
DIR_NAME=${ZIP_NAME%.*}
32-
33-
unzip $DIR_NAME
34-
cp $DIR_NAME/*.onnx rapid_layout/models/
3529
pip install -r requirements.txt
36-
pip install pytest wheel
30+
pip install pytest wheel onnxruntime
3731
pytest tests/test*.py
3832
3933
GenerateWHL_PushPyPi:

README.md

Lines changed: 43 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
</div>
55

66
<a href="https://huggingface.co/spaces/SWHL/RapidLayout" target="_blank"><img src="https://img.shields.io/badge/%F0%9F%A4%97-Hugging Face Demo-blue"></a>
7-
<a href=""><img src="https://img.shields.io/badge/Python->=3.6,<3.13-aff.svg"></a>
7+
<a href=""><img src="https://img.shields.io/badge/Python->=3.6-aff.svg"></a>
88
<a href=""><img src="https://img.shields.io/badge/OS-Linux%2C%20Win%2C%20Mac-pink.svg"></a>
99
<a href="https://pypi.org/project/rapid-layout/"><img alt="PyPI" src="https://img.shields.io/pypi/v/rapid-layout"></a>
1010
<a href="https://pepy.tech/project/rapid-layout"><img src="https://static.pepy.tech/personalized-badge/rapid-layout?period=total&units=abbreviation&left_color=grey&right_color=blue&left_text=Downloads"></a>
@@ -43,75 +43,45 @@ yolov8n系列来源:[360LayoutAnalysis](https://github.com/360AILAB-NLP/360Lay
4343

4444
### TODO
4545

46-
- [ ] [ PP-DocLayout](https://github.com/PaddlePaddle/PaddleX/blob/release/3.0-rc/docs/module_usage/tutorials/ocr_modules/layout_detection.md)整理
46+
- [ ] [PP-DocLayout](https://github.com/PaddlePaddle/PaddleX/blob/release/3.0-rc/docs/module_usage/tutorials/ocr_modules/layout_detection.md)整理
4747

4848
### 安装
4949

5050
由于模型较小,预先将中文版面分析模型(`layout_cdla.onnx`)打包进了whl包内,如果做中文版面分析,可直接安装使用
5151

5252
```bash
53-
pip install rapid-layout
53+
pip install rapid-layout onnxruntime
5454
```
5555

5656
### 使用方式
5757

5858
#### python脚本运行
5959

6060
```python
61-
import cv2
62-
from imread_from_url import imread_from_url # pip install imread_from_url
61+
from rapid_layout import EngineType, ModelType, RapidLayout, RapidLayoutInput
6362

64-
from rapid_layout import RapidLayout, VisLayout
63+
cfg = RapidLayoutInput()
64+
layout_engine = RapidLayout(cfg=cfg)
6565

66-
# model_type类型参见上表。指定不同model_type时,会自动下载相应模型到安装目录下的。
67-
layout_engine = RapidLayout(model_type="doclayout_docstructbench", conf_thres=0.2)
66+
img_path = "https://raw.githubusercontent.com/RapidAI/RapidLayout/refs/heads/develop/tests/test_files/layout.jpg"
67+
results = layout_engine(img_path)
68+
print(results)
6869

69-
img_url = "https://raw.githubusercontent.com/opendatalab/DocLayout-YOLO/refs/heads/main/assets/example/financial.jpg"
70-
img = imread_from_url(img_url)
71-
72-
boxes, scores, class_names, elapse = layout_engine(img)
73-
ploted_img = VisLayout.draw_detections(img, boxes, scores, class_names)
74-
if ploted_img is not None:
75-
cv2.imwrite("layout_res.png", ploted_img)
70+
results.vis("layout_res.png")
7671
```
7772

7873
### 可视化结果
7974

8075
<div align="center">
81-
<img src="https://github.com/RapidAI/RapidLayout/releases/download/v0.0.0/layout_res.png" width="80%">
76+
<img src="https://github.com/RapidAI/RapidLayout/releases/download/v0.0.0/layout_vis.jpg" width="80%" height="80%">
8277
</div>
8378

8479
#### 终端运行
8580

8681
```bash
87-
$ rapid_layout -h
88-
usage: rapid_layout [-h] -img IMG_PATH
89-
[-m {pp_layout_cdla,pp_layout_publaynet,pp_layout_table,yolov8n_layout_paper,yolov8n_layout_report,yolov8n_layout_publaynet,yolov8n_layout_general6,doclayout_docstructbench,doclayout_d4la,doclayout_docsynth}]
90-
[--conf_thres {pp_layout_cdla,pp_layout_publaynet,pp_layout_table,yolov8n_layout_paper,yolov8n_layout_report,yolov8n_layout_publaynet,yolov8n_layout_general6,doclayout_docstructbench,doclayout_d4la,doclayout_docsynth}]
91-
[--iou_thres {pp_layout_cdla,pp_layout_publaynet,pp_layout_table,yolov8n_layout_paper,yolov8n_layout_report,yolov8n_layout_publaynet,yolov8n_layout_general6,doclayout_docstructbench,doclayout_d4la,doclayout_docsynth}]
92-
[--use_cuda] [--use_dml] [-v]
93-
94-
options:
95-
-h, --help show this help message and exit
96-
-img IMG_PATH, --img_path IMG_PATH
97-
Path to image for layout.
98-
-m {pp_layout_cdla,pp_layout_publaynet,pp_layout_table,yolov8n_layout_paper,yolov8n_layout_report,yolov8n_layout_publaynet,yolov8n_layout_general6,doclayout_docstructbench,doclayout_d4la,doclayout_docsynth}, --model_type {pp_layout_cdla,pp_layout_publaynet,pp_layout_table,yolov8n_layout_paper,yolov8n_layout_report,yolov8n_layout_publaynet,yolov8n_layout_general6,doclayout_docstructbench,doclayout_d4la,doclayout_docsynth}
99-
Support model type
100-
--conf_thres {pp_layout_cdla,pp_layout_publaynet,pp_layout_table,yolov8n_layout_paper,yolov8n_layout_report,yolov8n_layout_publaynet,yolov8n_layout_general6,doclayout_docstructbench,doclayout_d4la,doclayout_docsynth}
101-
Box threshold, the range is [0, 1]
102-
--iou_thres {pp_layout_cdla,pp_layout_publaynet,pp_layout_table,yolov8n_layout_paper,yolov8n_layout_report,yolov8n_layout_publaynet,yolov8n_layout_general6,doclayout_docstructbench,doclayout_d4la,doclayout_docsynth}
103-
IoU threshold, the range is [0, 1]
104-
--use_cuda Whether to use cuda.
105-
--use_dml Whether to use DirectML, which only works in Windows10+.
106-
-v, --vis Wheter to visualize the layout results.
82+
rapid_layout test_images/layout.png
10783
```
10884

109-
- 示例:
110-
111-
```bash
112-
rapid_layout -v -img test_images/layout.png
113-
```
114-
11585
### GPU推理
11686

11787
- 因为版面分析模型输入图像尺寸固定,故可使用`onnxruntime-gpu`来提速。
@@ -132,25 +102,41 @@ pip install onnxruntime-gpu
132102
#### 使用
133103

134104
```python
135-
import cv2
136-
from rapid_layout import RapidLayout
137-
from pathlib import Path
105+
from rapid_layout import EngineType, ModelType, RapidLayout, RapidLayoutInput
106+
107+
cfg = RapidLayoutInput(
108+
model_type=ModelType.PP_LAYOUT_CDLA,
109+
engine_type=EngineType.ONNXRUNTIME,
110+
engine_cfg={"use_cuda": True, "cuda_ep_cfg.gpu_id": 1},
111+
)
112+
layout_engine = RapidLayout(cfg=cfg)
113+
114+
img_path = "https://raw.githubusercontent.com/RapidAI/RapidLayout/refs/heads/develop/tests/test_files/layout.jpg"
115+
results = layout_engine(img_path)
116+
print(results)
138117

139-
# 注意:这里需要使用use_cuda指定参数
140-
layout_engine = RapidLayout(model_type="doclayout_yolo", conf_thres=0.2, use_cuda=True)
118+
results.vis("layout_res.png")
119+
```
120+
121+
### NPU使用
122+
123+
详细配置参数参见:[link](https://github.com/RapidAI/RapidLayout/blob/a7ab63ff291bd72e1a98ac2bb11860575514f432/rapid_layout/configs/engine_cfg.yaml)
124+
125+
```python
126+
from rapid_layout import EngineType, ModelType, RapidLayout, RapidLayoutInput
141127

142-
# warm up
143-
layout_engine("images/12027_5.png")
128+
cfg = RapidLayoutInput(
129+
model_type=ModelType.PP_LAYOUT_CDLA,
130+
engine_type=EngineType.ONNXRUNTIME,
131+
engine_cfg={"use_cann": True, "cann_ep_cfg.gpu_id": 0},
132+
)
133+
layout_engine = RapidLayout(cfg=cfg)
144134

145-
elapses = []
146-
img_list = list(Path('images').iterdir())
147-
for img_path in img_list:
148-
boxes, scores, class_names, elapse = layout_engine(img_path)
149-
print(f"{img_path}: {elapse}s")
150-
elapses.append(elapse)
135+
img_path = "https://raw.githubusercontent.com/RapidAI/RapidLayout/refs/heads/develop/tests/test_files/layout.jpg"
136+
results = layout_engine(img_path)
137+
print(results)
151138

152-
avg_elapse = sum(elapses) / len(elapses)
153-
print(f'avg elapse: {avg_elapse:.4f}')
139+
results.vis("layout_res.png")
154140
```
155141

156142
### 参考项目

demo.py

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,12 @@
11
# -*- encoding: utf-8 -*-
22
# @Author: SWHL
33
# @Contact: [email protected]
4-
import cv2
4+
from rapid_layout import EngineType, ModelType, RapidLayout, RapidLayoutInput
55

6-
from rapid_layout import RapidLayout, VisLayout
6+
layout_engine = RapidLayout()
77

8-
layout_engine = RapidLayout(model_type="doclayout_docsynth")
8+
img_path = "https://raw.githubusercontent.com/RapidAI/RapidLayout/refs/heads/develop/tests/test_files/layout.jpg"
9+
results = layout_engine(img_path)
10+
print(results)
911

10-
img_path = "tests/test_files/PMC3576793_00004.jpg"
11-
img = cv2.imread(img_path)
12-
13-
boxes, scores, class_names, elapse = layout_engine(img_path)
14-
ploted_img = VisLayout.draw_detections(img, boxes, scores, class_names)
15-
if ploted_img is not None:
16-
cv2.imwrite("layout_res.png", ploted_img)
12+
results.vis("layout_res.png")

rapid_layout/__init__.py

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,5 @@
1-
# copyright (c) 2020 PaddlePaddle Authors. All Rights Reserve.
2-
#
3-
# Licensed under the Apache License, Version 2.0 (the "License");
4-
# you may not use this file except in compliance with the License.
5-
# You may obtain a copy of the License at
6-
#
7-
# http://www.apache.org/licenses/LICENSE-2.0
8-
#
9-
# Unless required by applicable law or agreed to in writing, software
10-
# distributed under the License is distributed on an "AS IS" BASIS,
11-
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12-
# See the License for the specific language governing permissions and
13-
# limitations under the License.
1+
# -*- encoding: utf-8 -*-
2+
# @Author: SWHL
3+
# @Contact: [email protected]
144
from .main import RapidLayout
15-
from .utils import VisLayout
5+
from .utils.typings import EngineType, ModelType, RapidLayoutInput

rapid_layout/configs/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# -*- encoding: utf-8 -*-
2+
# @Author: SWHL
3+
# @Contact: [email protected]
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
pp_layout_cdla:
2+
model_dir_or_path: https://www.modelscope.cn/models/RapidAI/RapidLayout/resolve/v1.0.0/onnx/pp_layout/layout_cdla.onnx
3+
SHA256: 25b1f27ec56aa932a48f30cbd6293c358a156280f4b20b0a973bab210c39f62c
4+
5+
pp_layout_publaynet:
6+
model_dir_or_path: https://www.modelscope.cn/models/RapidAI/RapidLayout/resolve/v1.0.0/onnx/pp_layout/layout_publaynet.onnx
7+
SHA256: 958aa6dcef1cc1a542d0a513b5976a3d5edbcc37d76460ec1e9f126358e4d100
8+
9+
pp_layout_table:
10+
model_dir_or_path: https://www.modelscope.cn/models/RapidAI/RapidLayout/resolve/v1.0.0/onnx/pp_layout/layout_table.onnx
11+
SHA256: 5b07ba6df1d1889bed2877c9d7501235c6fb6e2212aca8f2f56f4b1b8d0e37b5
12+
13+
yolov8n_layout_paper:
14+
model_dir_or_path: https://www.modelscope.cn/models/RapidAI/RapidLayout/resolve/v1.0.0/onnx/360/yolov8n_layout_paper.onnx
15+
SHA256: bc074c8d8fbe89e5d90c3e21b7e3b52f279c70fe210ae96d73b74141df64347c
16+
17+
yolov8n_layout_report:
18+
model_dir_or_path: https://www.modelscope.cn/models/RapidAI/RapidLayout/resolve/v1.0.0/onnx/360/yolov8n_layout_report.onnx
19+
SHA256: 9d5ada6a69b5825eb255da2b82d2c8d11636a0adae801074d88892527b535980
20+
21+
yolov8n_layout_publaynet:
22+
model_dir_or_path: https://www.modelscope.cn/models/RapidAI/RapidLayout/resolve/v1.0.0/onnx/360/yolov8n_layout_publaynet.onnx
23+
SHA256: 5304bf18e538312a1bd211eb2ad3283524dff956e5cbffcefb3ad294c6e3cba6
24+
25+
yolov8n_layout_general6:
26+
model_dir_or_path: https://www.modelscope.cn/models/RapidAI/RapidLayout/resolve/v1.0.0/onnx/360/yolov8n_layout_general6.onnx
27+
SHA256: 927b6edcb268e896e6a170f7d78980591b408e04b3908f54d58eb69efd018c95
28+
29+
doclayout_docstructbench:
30+
model_dir_or_path: https://www.modelscope.cn/models/RapidAI/RapidLayout/resolve/v1.0.0/onnx/doclayout/doclayout_yolo_docstructbench_imgsz1024.onnx
31+
SHA256: 3b452baef10ecabd615491bc82cc4d49475fbc2cd7a8e535044f2c6bb28fb9fe
32+
33+
doclayout_d4la:
34+
model_dir_or_path: https://www.modelscope.cn/models/RapidAI/RapidLayout/resolve/v1.0.0/onnx/doclayout/doclayout_yolo_d4la_imgsz1600_docsynth_pretrain.onnx
35+
SHA256: 1c81715d45d5bee2e6b644f92563a9eaa5cb4cad3d4293f890f99c0862937e69
36+
37+
doclayout_docsynth:
38+
model_dir_or_path: https://www.modelscope.cn/models/RapidAI/RapidLayout/resolve/v1.0.0/onnx/doclayout/doclayout_yolo_doclaynet_imgsz1120_docsynth_pretrain.onnx
39+
SHA256: 527e60cefc2801dec727dddbfa3a2bd225876a8c5505461c9d3b1193f50a7c84
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
onnxruntime:
2+
intra_op_num_threads: -1
3+
inter_op_num_threads: -1
4+
enable_cpu_mem_arena: false
5+
6+
cpu_ep_cfg:
7+
arena_extend_strategy: "kSameAsRequested"
8+
9+
use_cuda: false
10+
cuda_ep_cfg:
11+
gpu_id: 0
12+
arena_extend_strategy: "kNextPowerOfTwo"
13+
cudnn_conv_algo_search: "EXHAUSTIVE"
14+
do_copy_in_default_stream: true
15+
16+
use_dml: false
17+
dm_ep_cfg: null
18+
19+
use_cann: false
20+
cann_ep_cfg:
21+
gpu_id: 0
22+
arena_extend_strategy: "kNextPowerOfTwo"
23+
npu_mem_limit: 21474836480 # 20 * 1024 * 1024 * 1024
24+
op_select_impl_mode: "high_performance"
25+
optypelist_for_implmode: "Gelu"
26+
enable_cann_graph: true
27+
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# -*- encoding: utf-8 -*-
2+
# @Author: SWHL
3+
# @Contact: [email protected]
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
# -*- encoding: utf-8 -*-
2+
# @Author: SWHL
3+
# @Contact: [email protected]
4+
from abc import ABC, abstractmethod
5+
from pathlib import Path
6+
from typing import Any, Dict, List, Union
7+
8+
import numpy as np
9+
from omegaconf import DictConfig, OmegaConf
10+
11+
from ..utils.logger import Logger
12+
from ..utils.typings import EngineType
13+
from ..utils.utils import import_package
14+
15+
logger = Logger(logger_name=__name__).get_log()
16+
17+
18+
class InferSession(ABC):
19+
cur_dir = Path(__file__).resolve().parent.parent
20+
MODEL_URL_PATH = cur_dir / "configs" / "default_models.yaml"
21+
ENGINE_CFG_PATH = cur_dir / "configs" / "engine_cfg.yaml"
22+
23+
model_info = OmegaConf.load(MODEL_URL_PATH)
24+
DEFAULT_MODEL_PATH = cur_dir / "models"
25+
26+
engine_cfg = OmegaConf.load(ENGINE_CFG_PATH)
27+
28+
@abstractmethod
29+
def __init__(self, config):
30+
pass
31+
32+
@abstractmethod
33+
def __call__(self, input_content: np.ndarray) -> np.ndarray:
34+
pass
35+
36+
@staticmethod
37+
def _verify_model(model_path: Union[str, Path, None]):
38+
if model_path is None:
39+
raise ValueError("model_path is None!")
40+
41+
model_path = Path(model_path)
42+
if not model_path.exists():
43+
raise FileNotFoundError(f"{model_path} does not exists.")
44+
45+
if not model_path.is_file():
46+
raise FileExistsError(f"{model_path} is not a file.")
47+
48+
@abstractmethod
49+
def have_key(self, key: str = "character") -> bool:
50+
pass
51+
52+
@property
53+
def characters(self):
54+
return self.get_character_list()
55+
56+
@abstractmethod
57+
def get_character_list(self, key: str = "character") -> List[str]:
58+
pass
59+
60+
@staticmethod
61+
def update_params(cfg: DictConfig, params: Dict[str, Any]) -> DictConfig:
62+
for k, v in params.items():
63+
OmegaConf.update(cfg, k, v)
64+
return cfg
65+
66+
67+
def get_engine(engine_type: EngineType):
68+
logger.info("Using engine_name: %s", engine_type.value)
69+
70+
if engine_type == EngineType.ONNXRUNTIME:
71+
if not import_package(engine_type.value):
72+
raise ImportError(f"{engine_type.value} is not installed.")
73+
74+
from .onnxruntime import OrtInferSession
75+
76+
return OrtInferSession
77+
78+
raise ValueError(f"Unsupported engine: {engine_type.value}")
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# -*- encoding: utf-8 -*-
2+
# @Author: SWHL
3+
# @Contact: [email protected]
4+
from .main import OrtInferSession

0 commit comments

Comments
 (0)