1、我是基于容器做的,两个我都试了,有CPU的,有GPU的(我用的CUDA12)
CPU:docker pull registry.baidubce.com/paddlepaddle/paddle:2.5.0rc0-cpu
GPU:docker pull registry.baidubce.com/paddlepaddle/paddle:2.6.1-gpu-cuda12.0-cudnn8.9-trt8.6
2、容器启动
2.1、CPU启动容器
数据挂载到了【/data/docker-mount/paddle】下
mkdir -r /data/docker-mount/paddle
docker run -d --name handwriting_ppocr1 \
--network=host \
-v /data/docker-mount/paddle:/paddle \
-it registry.baidubce.com/paddlepaddle/paddle:2.5.0rc0-cpu
2.2、GPU启动容器,需要安装显卡,用的nvidia,同时docker也要安装显卡
直接启动会报错
docker: Error response from daemon: could not select device driver “” with capabilities: [[gpu]].
2.2.1、安装docker的显卡
搜了一下,网上说这么安装,但是也装不上啊,索性把下面的步骤挨个肢解
distribution=$(. /etc/os-release;echo $ID$VERSION_ID)
curl -s -L https://nvidia.github.io/nvidia-docker/gpgkey | sudo apt-key add -
curl -s -L https://nvidia.github.io/nvidia-docker/$distribution/nvidia-docker.list | sudo tee /etc/apt/sources.list.d/nvidia-docker.list
sudo apt-get update && sudo apt-get install -y nvidia-container-toolkit
sudo systemctl restart docker
2.2.1.1、下载gpgkey文件, 我是ubuntu
下载地址:https://nvidia.github.io/nvidia-docker/gpgkey
弄不下来的话,这有网盘,伺候到位么
链接:https://pan.baidu.com/s/1vvSKYqlp_N2Fe5_00bAwXw?pwd=1234
提取码:1234
2.2.1.2、建了个文件夹,将gpgkey写入文件
mkdir /home/aiuser/docker-nvidia
sudo apt-key add gpgkey
2.2.1.3、获取【https://nvidia.github.io/nvidia-docker/distribution/nvidia-docker.list】这个链接的内容,$distribution是ubuntu的版本号,我的是20.04
#获取版本号命令
lsb_release -a
访问【https://nvidia.github.io/nvidia-docker/20.04/nvidia-docker.list】,我帮你们看了,是如下内容
deb https://nvidia.github.io/libnvidia-container/stable/ubuntu18.04/$(ARCH) /
#deb https://nvidia.github.io/libnvidia-container/experimental/ubuntu18.04/$(ARCH) /
deb https://nvidia.github.io/nvidia-container-runtime/stable/ubuntu18.04/$(ARCH) /
#deb https://nvidia.github.io/nvidia-container-runtime/experimental/ubuntu18.04/$(ARCH) /
deb https://nvidia.github.io/nvidia-docker/ubuntu18.04/$(ARCH) /
2.2.1.4、将上面的内容写到【/etc/apt/sources.list.d/nvidia-docker.list】文件中
vim /etc/apt/sources.list.d/nvidia-docker.list
2.2.1.5、然后执行下面命令,并重启docker
sudo apt-get update && sudo apt-get install -y nvidia-container-toolkit
sudo systemctl restart docker
2.2.1.6、这个时候你的容器就具备了显卡的超能力,咱来试试,就是下面这个效果
sudo docker run --rm --gpus all nvidia/cuda:11.0.3-base-ubuntu20.04 nvidia-smi
2.2.2、GPU启动容器
数据挂载到了【/data/docker-mount/paddlenew】下
mkdir -r /data/docker-mount/paddlenew
docker run -d --name handwriting_ppocr2 \
--network=host \
--gpus all \
-v /data/docker-mount/paddlenew:/paddle \
-it registry.baidubce.com/paddlepaddle/paddle:2.6.1-gpu-cuda12.0-cudnn8.9-trt8.6
3、进入容器,开始PaddlePaddle的操作
3.1、先升级pip
pip install -U pip
3.2、拉取PaddleOCR代码,并安装必备的环境
cd /paddle
git clone https://gitee.com/paddlepaddle/PaddleOCR.git
cd PaddleOCR
pip install -r requirements.txt -i https://mirror.baidu.com/pypi/simple
3.3、准备训练的数据,这个是中科院手写数据和网上开源数据合并组合的数据集
https://aistudio.baidu.com/datasetdetail/102884/0
3.4、下载预训练模型
# 使用该指令下载需要的预训练模型
wget -P ./pretrained_models/ https://paddleocr.bj.bcebos.com/PP-OCRv3/chinese/ch_PP-OCRv3_rec_train.tar
# 解压预训练模型文件
tar -xf ./pretrained_models/ch_PP-OCRv3_rec_train.tar -C pretrained_models
3.5、修改配置文件
vim /paddle/PaddleOCR/configs/rec/PP-OCRv3/ch_PP-OCRv3_rec_distillation.yml
epoch_num: 100 # 训练epoch数
save_model_dir: ./output/ch_PP-OCR_v3_rec
save_epoch_step: 10
eval_batch_step: [0, 100] # 评估间隔,每隔100step评估一次
pretrained_model: ./pretrained_models/ch_PP-OCRv3_rec_train/best_accuracy # 预训练模型路径
lr:
name: Cosine # 修改学习率衰减策略为Cosine
learning_rate: 0.0001 # 修改fine-tune的学习率
warmup_epoch: 2 # 修改warmup轮数
Train:
dataset:
name: SimpleDataSet
data_dir: ./train_data # 训练集图片路径
ext_op_transform_idx: 1
label_file_list:
- ./train_data/train.txt
ratio_list:
- 1.0
loader:
shuffle: true
batch_size_per_card: 64
drop_last: true
num_workers: 0
Eval:
dataset:
name: SimpleDataSet
data_dir: ./train_data # 测试集图片路径
label_file_list:
- ./train_data/test.txt
loader:
shuffle: false
drop_last: false
batch_size_per_card: 64
num_workers: 0
由于数据集大多是长文本,因此需要注释掉下面的数据增广策略,以便训练出更好的模型
- RecConAug:
prob: 0.5
ext_data_num: 2
image_shape: [48, 320, 3]
3.6、训练之前可以评估一下模型
# 评估预训练模型
python tools/eval.py -c configs/rec/PP-OCRv3/ch_PP-OCRv3_rec_distillation.yml -o Global.pretrained_model="./pretrained_models/ch_PP-OCRv3_rec_train/best_accuracy"
CPU版本评估可能会报错,安装一下
pip install scikit-image -i https://mirror.baidu.com/pypi/simple
pip install imgaug -i https://mirror.baidu.com/pypi/simple
pip install pyclipper -i https://mirror.baidu.com/pypi/simple
pip install lmdb -i https://mirror.baidu.com/pypi/simple
pip install tqdm -i https://mirror.baidu.com/pypi/simple
pip install rapidfuzz -i https://mirror.baidu.com/pypi/simple
pip install visualdl -i https://mirror.baidu.com/pypi/simple
pip install rapidfuzz -i https://mirror.baidu.com/pypi/simple
3.7、模型训练
# 开始训练识别模型
python tools/train.py -c configs/rec/PP-OCRv3/ch_PP-OCRv3_rec_distillation.yml
3.8、训练完成之后,再对模型进行评估
# 评估finetune效果
python tools/eval.py -c configs/rec/PP-OCRv3/ch_PP-OCRv3_rec_distillation.yml -o Global.pretrained_model="./output/ch_PP-OCR_v3_rec/best_accuracy"
3.9、模型导出,导出之后分为Student(小、快、准确率稍低)和Teacher(大、慢、准确率高)
# 转化为推理模型
python tools/export_model.py -c configs/rec/PP-OCRv3/ch_PP-OCRv3_rec_distillation.yml -o Global.pretrained_model="./output/ch_PP-OCR_v3_rec/best_accuracy" Global.save_inference_dir="./inference/rec_ppocrv3/"
3.10、模型推理
# 推理预测
python tools/infer/predict_rec.py --image_dir="train_data/train_data/o.jpg" --rec_model_dir="./inference/rec_ppocrv3/Student"
4、使用Paddle Serving启动服务
4.1、下载相关的whl
cd /paddle/PaddleOCR
# 安装serving,用于启动服务
wget https://paddle-serving.bj.bcebos.com/test-dev/whl/paddle_serving_server-0.8.3-py3-none-any.whl
pip install paddle_serving_server-0.8.3-py3-none-any.whl -i https://pypi.tuna.tsinghua.edu.cn/simple
# 安装client,用于向服务发送请求
wget https://paddle-serving.bj.bcebos.com/test-dev/whl/paddle_serving_client-0.8.3-cp37-none-any.whl
pip install paddle_serving_client-0.8.3-cp37-none-any.whl -i https://pypi.tuna.tsinghua.edu.cn/simple
# 安装serving-app
wget https://paddle-serving.bj.bcebos.com/test-dev/whl/paddle_serving_app-0.8.3-py3-none-any.whl
pip install paddle_serving_app-0.8.3-py3-none-any.whl -i https://pypi.tuna.tsinghua.edu.cn/simple
4.2、下载文本检测和文本识别模型
cd deploy/pdserving/
# 下载并解压 OCR 文本检测模型
wget https://paddleocr.bj.bcebos.com/PP-OCRv3/chinese/ch_PP-OCRv3_det_infer.tar -O ch_PP-OCRv3_det_infer.tar && tar -xf ch_PP-OCRv3_det_infer.tar
# 下载并解压 OCR 文本识别模型
wget https://paddleocr.bj.bcebos.com/PP-OCRv3/chinese/ch_PP-OCRv3_rec_infer.tar -O ch_PP-OCRv3_rec_infer.tar && tar -xf ch_PP-OCRv3_rec_infer.tar
4.3、用安装的paddle_serving_client把下载的inference模型转换成易于server部署的模型格式
# 转换检测模型
python -m paddle_serving_client.convert --dirname ./ch_PP-OCRv3_det_infer/ \
--model_filename inference.pdmodel \
--params_filename inference.pdiparams \
--serving_server ./ppocr_det_v3_serving/ \
--serving_client ./ppocr_det_v3_client/
# 转换识别模型
python -m paddle_serving_client.convert --dirname ./ch_PP-OCRv3_rec_infer/ \
--model_filename inference.pdmodel \
--params_filename inference.pdiparams \
--serving_server ./ppocr_rec_v3_serving/ \
--serving_client ./ppocr_rec_v3_client/
这个时候,可以将Student拷贝过来./deploy/pdserving/mymodel/Student,然后使用咱们训练好的识别模型
# 转换识别模型
python -m paddle_serving_client.convert --dirname ./mymodel/Student/ \
--model_filename inference.pdmodel \
--params_filename inference.pdiparams \
--serving_server ./ppocr_rec_v3_serving/ \
--serving_client ./ppocr_rec_v3_client/
4.4、启动服务
#启动服务,测试时使用,窗口关闭服务停止
python web_service.py --config=config.yml
# 启动服务(后台运行),运行日志保存在log.txt
nohup python web_service.py --config=config.yml &>log.txt &
tail -f ./log.txt