You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用自有数据集微调PaddleOCR?求数据结构及运行代码

PaddleOCR 自有数据集微调指南

问题背景

使用PaddleOCR韩语预训练模型后效果未达预期,需基于自有数据集(含图片、标注框(x,y,width,height)及文本内容)进行微调,但不清楚所需数据结构及执行代码。

当前使用代码

ocr = PaddleOCR(cls=True,
                use_angle_cls=False,
                lang="korean",use_gpu=True,
                rec_model_dir='/ocr/korean_PP-OCRv3_rec_train/'
)
enhancer = ImageEnhance.Contrast(object_crops[l])
factor = 2.5
enhanced_image = enhancer.enhance(factor)
result = ocr.ocr(np.array(enhanced_image))
ed=time.time()
ocr_result = result[0]

解决方案

一、数据集格式转换

PaddleOCR的检测(Det)和识别(Rec)任务需对应不同数据集格式,根据你的标注信息可按以下方式处理:

1. 识别任务(Rec)数据集格式

识别任务优先使用单文本区域裁剪图,生成train.txt和val.txt,每行格式为:

图片绝对路径\t文本内容

示例:

/data/ocr/train/img_001_crop.jpg\t안녕하세요
/data/ocr/train/img_002_crop.jpg\t반갑습니다

批量裁剪并生成标注文件的脚本示例:

import os
from PIL import Image

def crop_text_regions(img_path, x, y, w, h, save_dir):
    img = Image.open(img_path)
    crop_img = img.crop((x, y, x+w, y+h))
    filename = os.path.basename(img_path).split('.')[0] + '_crop.jpg'
    save_path = os.path.join(save_dir, filename)
    crop_img.save(save_path)
    return save_path

# 替换为你的标注文件路径与保存目录
anno_file = 'your_annotation.txt'  # 每行格式:img_path x y w h text
save_dir = '/data/ocr/rec_crops'
os.makedirs(save_dir, exist_ok=True)

with open(anno_file, 'r', encoding='utf-8') as f, open('train.txt', 'w', encoding='utf-8') as out_f:
    for line in f:
        parts = line.strip().split('\t')
        img_path, x, y, w, h, text = parts[0], int(parts[1]), int(parts[2]), int(parts[3]), int(parts[4]), parts[5]
        crop_path = crop_text_regions(img_path, x, y, w, h, save_dir)
        out_f.write(f"{crop_path}\t{text}\n")

2. 检测任务(Det)数据集格式

检测任务保留原图,生成train.txt和val.txt,每行格式为:

图片绝对路径\t[{"transcription": "文本内容", "points": [[x1,y1],[x2,y2],[x3,y3],[x4,y4]]}]

需将标注框(x,y,w,h)转换为四个顶点坐标:左上角(x,y)、右上角(x+w,y)、右下角(x+w,y+h)、左下角(x,y+h)。

示例:

/data/ocr/train/img_001.jpg\t[{"transcription": "안녕하세요", "points": [[10,20],[110,20],[110,50],[10,50]]}]

批量转换脚本示例:

anno_file = 'your_annotation.txt'
with open(anno_file, 'r', encoding='utf-8') as f, open('det_train.txt', 'w', encoding='utf-8') as out_f:
    for line in f:
        parts = line.strip().split('\t')
        img_path, x, y, w, h, text = parts[0], int(parts[1]), int(parts[2]), int(parts[3]), int(parts[4]), parts[5]
        points = [[x,y], [x+w,y], [x+w,y+h], [x,y+h]]
        anno_str = f'[{{"transcription": "{text}", "points": {points}}}]'
        out_f.write(f"{img_path}\t{anno_str}\n")

二、微调执行步骤

1. 识别任务微调

(1)修改配置文件

复制PaddleOCR官方配置文件configs/rec/PP-OCRv3/rec_korean_lite_train_v3.yml,命名为rec_korean_finetune.yml,修改以下核心参数:

  • Train.dataset.data_dir:识别训练图片目录
  • Train.dataset.label_file_list:指向生成的train.txt
  • Eval.dataset.data_dir:识别验证图片目录
  • Eval.dataset.label_file_list:指向生成的val.txt
  • Global.pretrained_model:你的预训练模型路径(如/ocr/korean_PP-OCRv3_rec_train/)
  • Global.save_model_dir:微调后模型保存目录
(2)启动训练

执行命令:

python tools/train.py -c configs/rec/PP-OCRv3/rec_korean_finetune.yml -o Global.use_gpu=True

2. 检测任务微调

(1)修改配置文件

复制configs/det/ch_PP-OCRv3_det.yml为det_korean_finetune.yml,修改:

  • Train.dataset.data_dir:检测训练图片目录
  • Train.dataset.label_file_list:指向det_train.txt
  • Eval.dataset.data_dir:检测验证图片目录
  • Eval.dataset.label_file_list:指向det_val.txt
  • Global.pretrained_model:韩语检测预训练模型路径
  • Global.save_model_dir:检测模型保存目录
(2)启动训练

执行命令:

python tools/train.py -c configs/det/det_korean_finetune.yml -o Global.use_gpu=True

三、微调后模型使用

微调完成后,将模型目录指定为微调后的路径即可:

ocr = PaddleOCR(cls=True,
                use_angle_cls=False,
                lang="korean",use_gpu=True,
                rec_model_dir='/path/to/finetuned_rec_model/',
                det_model_dir='/path/to/finetuned_det_model/'  # 若微调了检测模型可添加
)

内容的提问来源于stack exchange,提问作者도본무

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.21 19:43:17