Skip to content

Commit

Permalink
add PP-OCRv4 support
Browse files Browse the repository at this point in the history
  • Loading branch information
Melody-Zhou committed Jul 24, 2024
1 parent 712782d commit 65e7480
Show file tree
Hide file tree
Showing 44 changed files with 13,857 additions and 3 deletions.
21 changes: 21 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,19 @@ link_directories(

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -Wall -O0 -Wfatal-errors -pthread -w -g")
set(CUDA_NVCC_FLAGS "${CUDA_NVCC_FLAGS} -std=c++11 -O0 -Xcompiler -fPIC -g -w ${CUDA_GEN_CODE}")

option(BUILD_PANGO_BACKEND "Build with Pango backend" OFF)
option(BUILD_TRUETYPE_BACKEND "Build with TrueType backend" ON)

if(BUILD_PANGO_BACKEND)
set(PANGO_LIBS ${PANGO_LIBS} pango-1.0 cairo pangocairo-1.0 glib-2.0 gobject-2.0)
add_definitions(-DENABLE_TEXT_BACKEND_PANGO)
endif()

if(BUILD_TRUETYPE_BACKEND)
add_definitions(-DENABLE_TEXT_BACKEND_STB)
endif()

file(GLOB_RECURSE cpp_srcs ${PROJECT_SOURCE_DIR}/src/*.cpp)
file(GLOB_RECURSE cuda_srcs ${PROJECT_SOURCE_DIR}/src/*.cu)
cuda_add_library(plugin_list SHARED ${cuda_srcs})
Expand Down Expand Up @@ -77,6 +90,7 @@ target_link_libraries(pro protobuf pthread plugin_list)
target_link_libraries(pro ${OpenCV_LIBS})
target_link_libraries(pro opencv_core opencv_imgproc opencv_videoio opencv_highgui opencv_imgcodecs)
# target_link_libraries(pro dl)
# target_link_libraries(pro ${PANGO_LIBS})

add_custom_target(
yolo
Expand Down Expand Up @@ -134,6 +148,13 @@ add_custom_target(
COMMAND ./pro rtmo
)

add_custom_target(
ppocr
DEPENDS pro
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}/workspace
COMMAND ./pro ppocr
)

add_custom_target(
test_yolo_map
DEPENDS pro
Expand Down
19 changes: 17 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
cc := g++
nvcc = ${lean_cuda}/bin/nvcc

build_pango_backend := no
build_truetype_backend := yes

lean_protobuf := /home/jarvis/protobuf
# lean_tensor_rt := /opt/TensorRT-8.4.1.5
# RT-DETR 必须指定高版本的 tensorRT
Expand Down Expand Up @@ -44,6 +47,15 @@ link_librarys := opencv_core opencv_imgproc opencv_videoio opencv_highgui opencv
cuda cublas cudart cudnn \
stdc++ protobuf dl

ifeq ($(build_pango_backend), yes)
link_librarys += pango-1.0 cairo pangocairo-1.0 glib-2.0 gobject-2.0
cppdefine += -DENABLE_TEXT_BACKEND_PANGO
endif

ifeq ($(build_truetype_backend), yes)
cppdefine += -DENABLE_TEXT_BACKEND_STB
endif

empty :=
export_path := $(subst $(empty) $(empty),:,$(library_paths))

Expand All @@ -52,7 +64,7 @@ include_paths := $(foreach item,$(include_paths),-I$(item))
library_paths := $(foreach item,$(library_paths),-L$(item))
link_librarys := $(foreach item,$(link_librarys),-l$(item))

cpp_compile_flags := -std=c++11 -g -w -O0 -fPIC -pthread -fopenmp
cpp_compile_flags := -std=c++11 -g -w -O0 -fPIC -pthread -fopenmp $(cppdefine)
cu_compile_flags := -std=c++11 -g -w -O0 -Xcompiler "$(cpp_compile_flags)" $(cuda_arch)
link_flags := -pthread -fopenmp -Wl,-rpath='$$ORIGIN'

Expand Down Expand Up @@ -119,6 +131,9 @@ rtdetr : workspace/pro
rtmo : workspace/pro
@cd workspace && ./pro rtmo

ppocr : workspace/pro
@cd workspace && ./pro ppocr

test_yolo_map : workspace/pro
@cd workspace && ./pro test_yolo_map

Expand All @@ -131,5 +146,5 @@ clean :
.PHONY : clean yolo debug

# 导出符号,使得运行时能够链接上
# export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/home/zhouwenguang/lean/TensorRT-8.5.1.7/lib
# export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/home/jarvis/lean/TensorRT-8.6.1.6/lib
export LD_LIBRARY_PATH:=$(export_path):$(LD_LIBRARY_PATH)
50 changes: 49 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

该仓库基于 [shouxieai/tensorRT_Pro](https://github.com/shouxieai/tensorRT_Pro),并进行了调整以支持 YOLOv8 的各项任务。

* 目前已支持 YOLOv8、YOLOv8-Cls、YOLOv8-Seg、YOLOv8-OBB、YOLOv8-Pose、RT-DETR、ByteTrack、YOLOv9、YOLOv10、RTMO 高性能推理!!!🚀🚀🚀
* 目前已支持 YOLOv8、YOLOv8-Cls、YOLOv8-Seg、YOLOv8-OBB、YOLOv8-Pose、RT-DETR、ByteTrack、YOLOv9、YOLOv10、RTMO、PP-OCRv4 高性能推理!!!🚀🚀🚀
* 基于 tensorRT8.x,C++ 高级接口,C++ 部署,服务器/嵌入式使用

<div align=center><img src="./assets/output.jpg" width="50%" height="50%"></div>
Expand All @@ -20,8 +20,14 @@
- 🔥 [MMPose-RTMO推理详解及部署实现(上)](https://blog.csdn.net/qq_40672115/article/details/139364023)
- 🔥 [MMPose-RTMO推理详解及部署实现(下)](https://blog.csdn.net/qq_40672115/article/details/139375752)
- 🔥 [LayerNorm Plugin的使用与说明](https://blog.csdn.net/qq_40672115/article/details/140246052)
- 🔥 [PaddleOCR-PP-OCRv4推理详解及部署实现(上)](https://blog.csdn.net/qq_40672115/article/details/140571346)
- 🔥 [PaddleOCR-PP-OCRv4推理详解及部署实现(中)](https://blog.csdn.net/qq_40672115/article/details/140585830)
- 🔥 [PaddleOCR-PP-OCRv4推理详解及部署实现(下)]

## Top News
- **2024/7/24**
- PP-OCRv4 支持
- cuOSD 支持,代码 copy 自 [Lidar_AI_Solution/libraries/cuOSD](https://github.com/NVIDIA-AI-IOT/Lidar_AI_Solution/tree/master/libraries/cuOSD)
- **2024/7/7**
- LayerNorm Plugin 支持,代码 copy 自 [CUDA-BEVFusion/src/plugins/custom_layernorm.cu](https://github.com/NVIDIA-AI-IOT/Lidar_AI_Solution/blob/master/CUDA-BEVFusion/src/plugins/custom_layernorm.cu)
- 提供 ONNX 模型下载([Baidu Drive](https://pan.baidu.com/s/1MbPYzUEkONsjCPOudiTt1A?pwd=onnx)),方便大家测试使用
Expand Down Expand Up @@ -1214,6 +1220,48 @@ ${TRTEXEC} \
> trtexec_output.log 2>&1
```

</details>

<details>
<summary>PP-OCRv4支持</summary>

1. 导出环境搭建

```shell
conda create --name paddleocr python=3.9
conda activate paddleocr
pip install shapely scikit-image imgaug pyclipper lmdb tqdm numpy==1.26.4 rapidfuzz onnxruntime
pip install "opencv-python<=4.6.0.66" "opencv-contrib-python<=4.6.0.66" cython "Pillow>=10.0.0" pyyaml requests
pip install paddlepaddle paddleocr paddle2onnx
```

2. 项目克隆

```shell
git clone https://github.com/PaddlePaddle/PaddleOCR.git
```

3. 预训练权重下载

- 参考:[🛠️ PP-OCR 系列模型列表(更新中)](https://github.com/PaddlePaddle/PaddleOCR?tab=readme-ov-file#%EF%B8%8F-pp-ocr-%E7%B3%BB%E5%88%97%E6%A8%A1%E5%9E%8B%E5%88%97%E8%A1%A8%E6%9B%B4%E6%96%B0%E4%B8%AD)

4. 导出 onnx 模型,具体流程请参考:[PaddleOCR-PP-OCRv4推理详解及部署实现(上)](https://blog.csdn.net/qq_40672115/article/details/140571346)

5. engine 生成

- **方案一**:利用 **TRT::compile** 接口,HardSwish 算子解析问题可以通过插件或者替换 onnxparser 解析器解决
- **方案二**:利用 **trtexec** 工具生成 engine (**recommend**)

```shell
cd tensorRT_Pro-YOLOv8/workspace
bash ocr_build.sh
```

6. 执行

```shell
make ppocr -j64
```

</details>

Expand Down
21 changes: 21 additions & 0 deletions python/change_hardswish.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import onnx
import onnx_graphsurgeon as gs

# 加载 ONNX 模型
input_model_path = "workspace/ppocr_det.sim.onnx"
output_model_path = "workspace/ppocr_det.sim.plugin.onnx"
graph = gs.import_onnx(onnx.load(input_model_path))

# 遍历图中的所有节点
for node in graph.nodes:
if node.op == "HardSwish":
node.op = "Plugin"
# 添加自定义属性
node.attrs["name"] = "HSwish"
node.attrs["info"] = "This is custom HardSwish node"

# 删除无用的节点和张量
graph.cleanup()

# 导出修改后的模型
onnx.save(gs.export_onnx(graph), output_model_path)
Loading

0 comments on commit 65e7480

Please sign in to comment.