PaddleOCR训练自己的文字识别模型(数码管AI读数)
PaddlePaddle建议版本:2.0.2
优质博客:
上述训练数据结构安排行不通的话,如下设置:
配置文件:
train.txt中的内容:
...
# Add a custom dictionary, such as modify the dictionary, please point the path to the new dictionary
character_dict_path: ppocr/utils/ppocr_keys_v1.txt
# Modify character type
character_type: ch
...
# Whether to recognize spaces
use_space_char: True
Optimizer:
...
# Add learning rate decay strategy
lr:
name: Cosine
learning_rate: 0.001
...
...
Train:
dataset:
# Type of dataset,we support LMDBDataSet and SimpleDataSet
name: SimpleDataSet
# Path of dataset
data_dir: ./train_data/
# Path of train list
label_file_list: ["./train_data/train_list.txt"]
transforms:
...
- RecResizeImg:
# Modify image_shape to fit long text
image_shape: [3, 32, 320]
...
loader:
...
# Train batch_size for Single card
batch_size_per_card: 256
...
Eval:
dataset:
# Type of dataset,we support LMDBDataSet and SimpleDataSet
name: SimpleDataSet
# Path of dataset
data_dir: ./train_data
# Path of eval list
label_file_list: ["./train_data/val_list.txt"]
transforms:
...
- RecResizeImg:
# Modify image_shape to fit long text
image_shape: [3, 32, 320]
...
loader:
# Eval batch_size for Single card
batch_size_per_card: 256
...
注意预测/评估的配置文件必须与训练一致。
3 评价
可以通过修改文件中的Eval.dataset.label_file_list字段来设置评估数据集configs/rec/rec_icdar15_train.yml。
python3 -m paddle.distributed.launch --gpus '0' tools/eval.py -c configs/rec/rec_icdar15_train.yml -o Global.checkpoints={path/to/weights}/best_accuracy
4 预测
4.1 训练引擎预测
使用paddleocr训练的模型,可以通过以下脚本快速得到预测。
默认预测图片存储在 中infer_img,权重通过 指定-o Global.checkpoints:
python3 tools/infer_rec.py -c configs/rec/ch_ppocr_v2.0/rec_chinese_lite_train_v2.0.yml -o Global.pretrained_model={path/to/weights}/best_accuracy Global.load_static_weights=false Global.infer_img=doc/imgs_words/en/word_1.jpg
获取输入图像的预测结果:
infer_img: doc/imgs_words/en/word_1.png
result: ('joint', 0.9998967)
用于预测的配置文件必须与训练一致。比如你用 完成了中文模型的训练python3 tools/train.py -c configs/rec/ch_ppocr_v2.0/rec_chinese_lite_train_v2.0.yml,可以使用如下命令来预测中文模型:
python3 tools/infer_rec.py -c configs/rec/ch_ppocr_v2.0/rec_chinese_lite_train_v2.0.yml -o Global.pretrained_model={path/to/weights}/best_accuracy Global.load_static_weights=false Global.infer_img=doc/imgs_words/ch/word_1.jpg