通用场景OCR文本识别任务-baseline学习(PaddleOCR)
baseline地址 “英特尔创新大师杯”深度学习挑战赛 赛道1:通用场景OCR文本识别任务 比赛地址
一、安装环境和下载数据集
1.首先要安装相应的cuda和cudnn这就不多说了,比如我的是cuda10.2和它对应的cudnn 2.从官网安装paddlepaddle-gpu, 链接 3.将代码clone到本地,并进入该目录
git clone https://gitee.com/coggle/tianchi-intel-PaddleOCR
cd tianchi-intel-PaddleOCR
4.下载训练图片和测试图片
python down_image.py
5.下载预测模型
mkdir inference && cd inference/
wget https://paddleocr.bj.bcebos.com/dygraph_v2.0/ch/ch_ppocr_server_v2.0_det_infer.tar
wget https://paddleocr.bj.bcebos.com/dygraph_v2.0/ch/ch_ppocr_mobile_v2.0_cls_infer.tar
wget https://paddleocr.bj.bcebos.com/dygraph_v2.0/ch/ch_ppocr_server_v2.0_rec_infer.tar
tar -xf ch_ppocr_server_v2.0_rec_infer.tar
tar -xf ch_ppocr_server_v2.0_det_infer.tar
tar -xf ch_ppocr_mobile_v2.0_cls_infer.tar
6.尝试预测模型去预测一张图片
在tianchi-intel-PaddleOCR目录下
python tools/infer/predict_system.py --image_dir="./1.jpg" --det_model_dir="./inference/ch_ppocr_server_v2.0_det_infer/" --rec_model_dir="./inference/ch_ppocr_server_v2.0_rec_infer/" --cls_model_dir='./inference/ch_ppocr_mobile_v2.0_cls_infer/' --use_angle_cls=True --use_space_char=True
7.运行推理文件如果发生报错 缺少geos_c.dll 可以参考此链接
8.继续运行推理文件果又发生报错
Segmentation fault
9.检查paddlepaddle-gpu安装是否正确
import paddle.fluid
paddle.fluid.install_check.run_check()
如果安装正确会出现字样,
>>> paddle.fluid.install_check.run_check()
Running Verify Fluid Program ...
W0710 09:57:15.796396 13492 device_context.cc:404] Please NOT
E: device: 0, GPU Compute Capability: 7.5, Driver API Version
: 11.2, Runtime API Version: 10.2
W0710 09:57:15.811870 13492 device_context.cc:422] device: 0,
cuDNN Version: 7.6.
Your Paddle Fluid works well on SINGLE GPU or CPU.
Your Paddle Fluid works well on MUTIPLE GPU or CPU.
Your Paddle Fluid is installed successfully! Let's start deep
Learning with Paddle Fluid now
否则比如这种
RuntimeError: (PreconditionNotMet) The third-party dynamic li
brary (cudnn64_7.dll) that Paddle depends on is not configure
d correctly. (error code is 126)
Suggestions:
1. Check if the third-party dynamic library (e.g. CUDA, CUD
NN) is installed correctly and its version is matched with pa
ddlepaddle you installed.
les as follows:
- Linux: set LD_LIBRARY_PATH by `export LD_LIBRARY_PATH=...
`
\Paddle_release3\paddle\fluid\platform\dynload\dynamic_loader
.cc:265)
缺少cudnn64_7.dll文件 根据这个链接可以解决
10.再次运行推理 可以发现运行成功了
[2021/07/10 09:57:40] root INFO: dt_boxes num : 2, elapse : 1
.4339828491210938
[2021/07/10 09:57:40] root INFO: cls num : 2, elapse : 0.007
935762405395508
[2021/07/10 09:57:40] root INFO: rec_res num : 2, elapse : 0
.037322998046875
[2021/07/10 09:57:40] root INFO: Predict time of ./1.jpg: 1.5
02s
[2021/07/10 09:57:40] root INFO: 土地整治与土壤修复研究中心,
0.973
[2021/07/10 09:57:40] root INFO: 华南农业大学-东图, 0.992
[2021/07/10 09:57:40] root INFO: The visualized image saved i
n ./inference_results/1.jpg
在目录inference_results中可以看到预测的结果
二、训练模型
1.下载预训练模型
为配置文件ch_det_res18_db_v2.0.yml的预训练模型, 进行下载和解压
cd inference
wget https://paddleocr.bj.bcebos.com/dygraph_v2.0/ch/ch_ppocr_server_v2.0_det_train.tar
tar -xf ch_ppocr_server_v2.0_det_train.tar
2.训练预训练模型
python tools/train.py -c configs/det/ch_ppocr_v2.0/ch_det_res18_db_v2.0.yml -o Global.pretrain_weights=./inference/ch_ppocr_server_v2.0_det_train/
发现卡在这边不会动了
[2021/07/10 12:15:37] root INFO: During the training process,
after the 100th iteration, an evaluation is run every 1000 ite
rations
[2021/07/10 12:15:37] root INFO: Initialize indexs of datasets
:['./train_data/tianchi/train_list.txt']
Segmentation fault
于是在https://github.com/PaddlePaddle/PaddleOCR/issues/2184上找到了解决的办法,就是修改num worker 改为0, batch_size_per_card 也就是batch size改小点。
将目录下 ...\tianchi-intel-PaddleOCR\configs\det\ch_ppocr_v2.0 中ch_det_res18_db_v2.0.yml 复制一份为my_ch_det_res18_db_v2.0.yml
如下图 打开新复制的yml文件
注意是修改Train:下面的num_workers和batch_size_per_card(批次)
将num_workers设置为0 batch_size_per_card设置为比较小的值(比如我这设置了2)
还可以将save_epoch_step设置为1,代表每一周期训练结束都会保存一下模型
再次训练的时候配置文件改成my_ch_det_res18_db_v2.0.yml
python tools/train.py -c configs/det/ch_ppocr_v2.0/my_ch_det_res18_db_v2.0.yml -o Global.pretrain_weights=./inference/ch_ppocr_server_v2.0_det_train/
出现下图这样就是已经开始在训练了
三、预测结果
对测试集进行预测
1.将模型导出
python tools/export_model.py -c configs/det/ch_ppocr_v2.0/my_ch_det_res18_db_v2.0.yml -o Global.pretrained_model=output/ch_db_res18/latest Global.save_inference_dir=output/ch_db_res18/
2.对测试集进行预测
python tools/infer/predict_system_tianchi.py --det_model_dir="output/ch_db_res18/" --rec_model_dir="./inference/ch_ppocr_server_v2.0_rec_infer/" --cls_model_dir='./inference/ch_ppocr_mobile_v2.0_cls_infer/' --use_angle_cls=True --use_space_char=True
3.将结果文件压缩
zip -r submit.zip Xeon1OCR_round1_test*
将submit.zip提交至天池即可,可以拿到62分左右.
四、其他链接:
飞桨AI Studio 有供应GPU使用,但是只能使用Paddle框架 飞桨paddlepaddle官网
|