当前位置:   article > 正文

PaddleOCR文字识别模型训练

paddleocr文字识别模型训练

本文主要介绍PaddleOCR2.0.0版本中文字识别模型的训练、评估及测试。

数据准备

中文场景文字识别技术创新大赛数据集

数据集共212023张文字图片。数据集地址

  • 训练集

将训练图片放入同一个文件夹(train_images),并用一个txt文件(rec_gt_train.txt)记录图片路径和标签。

注意: 默认请将图片路径和图片标签用 \t 分割,如用其他方式分割将造成训练报错

" 图像文件名                 图像标注信息 "

img_001.jpg   简单可依赖
img_002.jpg   用科技让复杂的世界更简单
  • 1
  • 2
  • 3
  • 4

训练集txt生成代码如下代码:

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Auther: Andy
date: 11/25/20 1:36 PM

desc:
"""
train_list_path = "/data11/shaozhl/datasets/OCR/baidu_rec/train.list"
label_path = "/data11/shaozhl/datasets/OCR/baidu_rec/train_data/rec_data/rec_gt_train.txt"
with open(train_list_path, 'r') as fr:
    with open(label_path, 'w', encoding="utf-8") as fw:
        lines = fr.readlines()
        for line in lines:
            line = line.split("\t")
            fw.writelines(line[-2] + "\t" + line[-1])
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

最终训练集应有如下文件结构:

|-train_data
    |-rec_data
        |- rec_gt_train.txt
        |- train
            |- img_001.png
            |- img_002.jpg
            |- img_003.jpg
            | ...
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

测试集同理,

  • 字典

因为是所用的数据集已经有标注(标注列表为train.list),所以只要将标注的数据转化为字典即可。

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Auther: Andy
date: 11/25/20 11:09 AM

desc: 将中文场景文字识别技术创新大赛数据集label转化为PaddleOCR文字识别label
"""

train_list_path = "/data11/shaozhl/datasets/OCR/baidu_rec/train.list"
dict_path = "../ppocr/utils/baidu_rec_dict.txt"
texts = ""
with open(train_list_path, 'r') as f:
    lines = f.readlines()
    for line in lines:
        line = line.split("\t")
        texts += line[-1].replace("\n", "")

texts = set(texts)
with open(dict_path, 'w') as f:
    for text in texts:
        f.writelines(text+"\n")
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

在 configs/rec/rec_baidu_train.yml 中添加 character_dict_path 字段, 指向您的字典路径。 并将 character_type 设置为 ch,因为该数据集label有的比较长,所以也要设置下max_text_length

character_dict_path: ./ppocr/utils/baidu_rec_dict.txt
character_type: ch
max_text_length: 100
  • 1
  • 2
  • 3

rec_baidu_train.yml可参照rec_icdar15_train.yml重写。
下面是我自己的yml,仅供参考。rec_baidu_train.yml如下

Global:
  algorithm: CRNN
  use_gpu: true
  epoch_num: 
  • 1
  • 2
  • 3
声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号