PaddleOCR 使用以及用自己的数据训练
在OCR方面找了很多资料,本来想要找pytorch框架下的算法,无奈很多OCR算法太老,没人更新。感觉这块有点冷门似的,不像其它热门算法一出来就有各个框架版本的复现。
看了paddleOCR真的是相当全面深刻了,还是我们自家的框架接地气。而且百度经常搞直播分享学习什么的,各种训练营还有学习群....
首先github下载:https://github.com/PaddlePaddle/PaddleOCR 并解压
一。试用官方模型
打开这个网址的中文版,下载预训练模型:
https://github.com/PaddlePaddle/PaddleOCR/blob/develop/README_ch.md
先用轻量OCR模型,分别下载检测和识别以及方向分类器。这里关于预训练模型/训练模型/推理模型,官方有说明:
注:更多模型在模型列表中:https://github.com/PaddlePaddle/PaddleOCR/blob/develop/doc/doc_ch/models_list.md
这里我 下载的是infer 推理模型ch_ppocr_mobile_v1.1_xx, 下载完解压到PaddleOCR/inference 下,可以新建inference文件夹:
每个文件夹下有两个文件:model 和params
官方快速开始的教程:https://github.com/PaddlePaddle/PaddleOCR/blob/develop/doc/doc_en/quickstart_en.md
可以使用如下命令等:
python3 tools/infer/predict_system.py --image_dir="./doc/imgs/11.jpg" --det_model_dir="./inference/ch_ppocr_mobile_v1.1_det_infer/" --rec_model_dir="./inference/ch_ppocr_mobile_v1.1_rec_infer/" --cls_model_dir="./inference/ch_ppocr_mobile_v1.1_cls_infer/" --use_angle_cls=True --use_space_char=True
但我习惯在代码里跑:
参数模块在这 tools/infer/utility.py 需要更改的参数:
图片路径 --image_dir = ./doc/imgs/3.jpg
检测模型路径--det_model_dir = ./inference/ch_ppocr_mobile_v1.1_det_infer 识别模型--rec_model_dir = ./inference/ch_ppocr_mobile_v1.1_rec_infer 字典--rec_char_dict_path = ./ppocr/utils/ppocr_keys_v1.txt 这个参数很重要,这个字典文字取决与你的模型是识别中文还是英文数字什么的,这个是中文字典,还有一个ic15_dict.txt 是英文和数字 分类模型--cls_model_dir = ./inference/ch_ppocr_mobile_v1.1_cls_infer draw_ocr_box_txt 函数中 有个字体的路径 font_path= doc/simfang.ttf text_visual 函数中的字体路径也是 font_path= doc/simfang.ttf
ok。 测试图片在doc/imgs下,测试结果输出在 doc/imgs_results 以及 ./inference_results 文件夹下:
*二。训练自己的数据 *
检测部分
官方教程:https://github.com/PaddlePaddle/PaddleOCR/blob/develop/doc/doc_ch/detection.md
主要要注意的是标签的制作,要注意空格。还有每个预训练模型要对应自己的配置文件。
数据准备 训练数据包含以下内容(同一目录下):
train_data/text_localization/
--train_images文件夹下为自己要训练的图片
--test_images 文件夹为测试图片
--train_label.txt 为标签文件,通过官方标签转换工具得到。内容包括:训练图片的路径以及对应的文字内容和坐标信息train_data/gen_label.py
--test_label.txt 同上。标签内容如下:
官方转换方法如下:
python gen_label.py --mode="det" --root_path="icdar_c4_train_imgs/"
--input_path="ch4_training_localization_transcription_gt"
--output_label="train_icdar2015_label.txt"
其中--input_path="ch4_training_localization_transcription_gt" 文件夹为每一张训练图片的数据标注的内容信息,每一个txt文件名对应一张图片名:
txt文本内容:
其中###号表示的应该是负样本 就是没有文字。(不知道能不能这么说)
关于数据标注:
paddleOCR官方前几天刚开源了文字标注工具PPOCRLabel,相当友好了。还可以自动标注。
PPOCRLabel工具就在paddleOCR项目下:https://github.com/PaddlePaddle/PaddleOCR/tree/develop/PPOCRLabel
标注工具不细说,比较简单。
官方提供了三个backbone预训练模型:
MobileNetV3、ResNet18_vd,ResNet50_vd
解压backbone预训练权重文件后,文件夹下包含众多以网络层命名的权重文件。一开始就很疑问为什么是这么个保存法,因为其它模型都是保存了三个文件,
就这几个检测预训练模型几乎每曾单独保存。后来在群里问题paddle的人应该是保存的api问题。而且这预训练模型也不知道怎么测试效果。。。只是一个backbone
数据和预训练模型都有了就可以训练了,就分这三种模型:
MobileNetV3_large_x0_5_pretrained:
train:
python3 tools/train.py -c configs/det/det_mv3_db_v1.1.yml -o Global.pretrain_weights=./pretrain_models/detect_pretrain_models/MobileNetV3_large_x0_5_pretrained/ 2>&1 | tee train_det.log
infer:
python3 tools/infer_det.py -c configs/det_mv3_db_v1.1.yml -o Global.infer_img="./doc/imgs/3.jpg" Global.checkpoints="./output/My_model_det_mv3_db_v1.1/best_accuracy"
ResNet18_vd_ssld_pretrained:
train:
python3 tools/train.py -c configs/det/det_r18_vd_db_v1.1.yml -o Global.pretrain_weights=./pretrain_models/detect_pretrain_models/ResNet18_vd_pretrained/ 2>&1 | tee train_det.log
infer:
python3 tools/infer_det.py -c configs/det/det_r18_vd_db_v1.1.yml -o Global.infer_img="./doc/imgs/3.jpg" Global.checkpoints="./output/my_det_r_18_vd_db/best_accuracy"
ResNet50_vd_ssld_pretrained:这个我还没训练过,因为模型比较大,电脑配置太烂跑不了。。。。
train:
python3 tools/train.py -c configs/det/det_r50_vd_db.yml -o Global.pretrain_weights=./pretrain_models/detect_pretrain_models/my_ResNet50_vd/ 2>&1 | tee train_det.log
infer:
python3 tools/infer_det.py -c configs/det/det_r50_vd_db..yml -o Global.infer_img="./doc/imgs/3.jpg" Global.checkpoints="./output/my_ResNet50_vd/best_accuracy"
其它的检测模型也可以试试,比如sast 就不介绍了:
python3 tools/infer_det.py -c configs/det/det_r50_vd_sast_icdar15.yml -o Global.infer_img="./doc/imgs_en/img623.jpg" Global.checkpoints="./pretrain_models/sast_r50_vd_icdar2015/best_accuracy
训练的时候,注意一开始是有warm up的,学习率是逐渐上升,之后的学习率在0.01好像:
模型训练好后,转换模型为可部署文件:
python3 tools/export_model.py -c configs/det/det_r18_vd_db_v1.1.yml -o Global.checkpoints="./output/my_det_r_18_vd_db/best_accuracy" Global.save_inference_dir="./output/my_det_r_18_vd_db/expor_model"
save success:
识别部分
————————————————
版权声明:本文为CSDN博主「苏三福」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/xingtianyao/article/details/109817368
查看5道真题和解析
