PaddleHub-GPU镜像制作
接前文,本文介绍制作PaddleOCR/PaddleHub的GPU版本Docker镜像,以便在GPU环境下高效运行。
环境依赖
🔔 镜像制作过程在下述环境中进行的,酌情参考。
基础环境
- ubuntu 22.04
- gcc/g++ 11.04
- Python 3.10
- paddlepaddle-gpu==2.6.1.post120
- paddleocr 2.7.5
- opencv-python 4.9.0.80
- paddlehub 2.4.0
GPU环境
- cuda 12.2
- Driver API Version: 12.4
- Runtime API Version: 11.8
环境变量
必须的步骤
export CUDA_VISIBLE_DEVICES=0
export LD_LIBRARY_PATH=/usr/local/cuda-12.2/lib64:$LD_LIBRARY_PATH
export PATH=/usr/local/cuda-12.2/bin:$PATH
export CUDA_HOME=/usr/local/cuda-12.2
基础镜像
拉取官方镜像并启动容器:
docker run -it --gpus all --name=paddlegpu -d paddlepaddle-gpu:2.6.1
说明:
如果无法拉取这个镜像(或嫌弃该镜像内容太多),也可以使用前文中生成的镜像 paddlehub:base ,然后逐步处理。
安装paddlepaddle-gpu
安装 CUDA12.0 包含 cuDNN 动态链接库的 PaddlePaddle
python3 -m pip install paddlepaddle-gpu==2.6.1.post120 -f https://www.paddlepaddle.org.cn/whl/linux/cudnnin/stable.html
验证
python3.10 -c "import paddle; paddle.utils.run_check()"
cuda和cuDNN安装完成后,验证正常如下:
root@b40610924ca2:/usr/local/cuda-12.2/targets/x86_64-linux/lib# python3.10 -c "import paddle; paddle.utils.run_check()"
Running verify PaddlePaddle program ...
I0511 02:00:54.641423 814 program_interpreter.cc:212] New Executor is Running.
W0511 02:00:54.643276 814 gpu_resources.cc:119] Please NOTE: device: 0, GPU Compute Capability: 8.0, Driver API Version: 12.4, Runtime API Version: 11.8
W0511 02:00:54.643945 814 gpu_resources.cc:164] device: 0, cuDNN Version: 8.9.
I0511 02:00:55.097067 814 interpreter_util.cc:624] Standalone Executor is Used.
PaddlePaddle works well on 1 GPU.
PaddlePaddle is installed successfully! Let's start deep learning with PaddlePaddle now.
可能出现的问题
执行检测指令后出现异常响应:
root@e93431fbf153:/PaddleOCR# python3.10 -c "import paddle; paddle.utils.run_check()"
/usr/local/lib/python3.10/dist-packages/paddle/base/framework.py:688: UserWarning: You are using GPU version Paddle, but your CUDA device is not set properly. CPU device will be used by default.
warnings.warn(
Running verify PaddlePaddle program ...
[2024-05-10 13:17:24,497] [ WARNING] install_check.py:60 - You are using GPU version PaddlePaddle, but there is no GPU detected on your machine. Maybe CUDA devices is not set properly.
Original Error is
I0510 13:17:24.505074 4123 program_interpreter.cc:212] New Executor is Running.
I0510 13:17:24.539232 4123 interpreter_util.cc:624] Standalone Executor is Used.
PaddlePaddle works well on 1 CPU.
PaddlePaddle is installed successfully! Let's start deep learning with PaddlePaddle now.
解决
安装 CUDA 和 cuDNN。
- CUDA
如果宿主机安装了CUDA,可直接拷贝到容器内(宿主机上CUDA安装过程不演示):
docker cp /usr/local/cuda paddlegpu:/usr/local
# 下面的步骤是因为有软连接。如果没有,则省略
# docker cp /usr/local/cuda-12.2 paddlegpu:/usr/local
##添加环境变量
export PATH=/usr/local/cuda/bin:$PATH
export LD_LIBRARY_PATH=/usr/local/cuda/lib64:$LD_LIBRARY_PATH
容器内查看CUDA安装结果:
root@b40610924ca2:/# nvcc --version
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2023 NVIDIA Corporation
Built on Tue_Aug_15_22:02:13_PDT_2023
Cuda compilation tools, release 12.2, V12.2.140
Build cuda_12.2.r12.2/compiler.33191640_0
说明:
如果基础镜像中包含了旧版本的CUDA,可直接删除。然后拷贝新版本使用。
- 安装cuDNN
容器内执行下面这个指令,确保执行成功:
python3 -m pip install paddlepaddle-gpu==2.6.1.post120 -f https://www.paddlepaddle.org.cn/whl/linux/cudnnin/stable.html
安装PoddleOCR
在容器内执行:
wget https://github.com/PaddlePaddle/PaddleOCR/archive/refs/tags/v2.7.5.tar.gz && \
tar -zxvf v2.7.5.tar.gz && \
mv PaddleOCR-2.7.5 PaddleOCR \
&& rm -rf v2.7.5.tar.gz \
&& cd PaddleOCR \
&& pip3.10 install -r requirements.txt -i https://mirror.baidu.com/pypi/simple \
&& mkdir -p /PaddleOCR/inference/ \
&& wget https://paddleocr.bj.bcebos.com/dygraph_v2.0/ch/ch_ppocr_mobile_v2.0_det_infer.tar && \
tar xf ch_ppocr_mobile_v2.0_det_infer.tar -C /PaddleOCR/inference/ && \
wget https://paddleocr.bj.bcebos.com/dygraph_v2.0/ch/ch_ppocr_mobile_v2.0_cls_infer.tar && \
tar xf ch_ppocr_mobile_v2.0_cls_infer.tar -C /PaddleOCR/inference/ && \
wget https://paddleocr.bj.bcebos.com/dygraph_v2.0/ch/ch_ppocr_mobile_v2.0_rec_infer.tar && \
tar xf ch_ppocr_mobile_v2.0_rec_infer.tar -C /PaddleOCR/inference/ \
&& rm -rf ch_ppocr_mobile_v2.0_det_infer.tar \
&& rm -rf ch_ppocr_mobile_v2.0_cls_infer.tar \
&& rm -rf ch_ppocr_mobile_v2.0_rec_infer.tar
安装paddlehub
pip3.10 install --upgrade -i https://mirror.baidu.com/pypi/simple paddlehub
## 以下包 是执行hub serving install 时需要安装的包
pip3.10 install -i https://mirror.baidu.com/pypi/simple shapely
pip3.10 install -i https://mirror.baidu.com/pypi/simple pyclipper
pip3.10 install -i https://mirror.baidu.com/pypi/simple scikit-image
pip3.10 install -i https://mirror.baidu.com/pypi/simple imgaug
pip3.10 install -i https://mirror.baidu.com/pypi/simple lmdb
安装模块
#表格识别
hub install deploy/hubserving/structure_table
#文字识别
hub install deploy/hubserving/ocr_system
#版面识别
hub install deploy/hubserving/structure_layout
不报错,说明安装成功了。
说明:
其他模块按需安装。
其他问题
ImportError: libGL.so.1
File "/usr/local/lib/python3.10/dist-packages/paddlehub/vision/utils.py", line 18, in <module>
import cv2
ImportError: libGL.so.1: cannot open shared object file: No such file or directory
需要安装如下包(容器内):
apt-get update
apt-get install libgl1-mesa-glx -y
apt-get install libglib2.0-0 -y
降级protobuf
报错:
TypeError: Descriptors cannot be created directly.
If this call came from a _pb2.py file, your generated code is out of date and must be regenerated with protoc >= 3.19.0.
If you cannot immediately regenerate your protos, some other possible workarounds are:
1. Downgrade the protobuf package to 3.20.x or lower.
…………
解决方案:降级
pip3.10 install -i https://mirror.baidu.com/pypi/simple protobuf==3.20.2
缺少模型文件
类似如下报错:
raise ValueError(
ValueError: not find model.pdmodel or inference.pdmodel in ./inference/ch_PP-OCRv3_det_infer/
少哪个模型就下载、解压到inference目录下即可。
同时,修改params.py文件:
cd /PaddleOCR
vi deploy/hubserving/structure_table/params.py
def read_params():
cfg = pp_ocr_read_params()
# params for table structure model
cfg.table_max_len = 488
#cfg.table_model_dir = './inference/en_ppocr_mobile_v2.0_table_structure_infer/'
cfg.table_model_dir = './inference/ch_ppstructure_mobile_v2.0_SLANet_infer/'
#cfg.table_char_dict_path = './ppocr/utils/dict/table_structure_dict.txt'
cfg.table_char_dict_path = './ppocr/utils/dict/table_structure_dict_ch.txt'
cfg.show_log = False
return cfg
版本问题
启动gpu版本的服务后,客户端调用时报错ÿ