🚀 表格单元格图像手写文本识别模型
本模型可实现从文本行图像中进行手写文本识别。它通过微调国家档案馆的多世纪手写文本识别模型和微软的TrOCR模型,使用20世纪30年代芬兰死亡记录和人口普查记录表格中的文本行图像进行训练,为表格单元格图像的手写文本识别提供了有效的解决方案。
🚀 快速开始
本模型可按以下代码预测图像的文本内容。若有可用的GPU,建议在推理时使用。
from transformers import TrOCRProcessor, VisionEncoderDecoderModel
from PIL import Image
import torch
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model_checkpoint = "Kansallisarkisto/tablecell-htr"
line_image_path = "/path/to/textline_image.jpg"
processor = TrOCRProcessor.from_pretrained(model_checkpoint)
model = VisionEncoderDecoderModel.from_pretrained(model_checkpoint).to(device)
image = Image.open(line_image_path).convert("RGB")
pixel_values = processor(image, return_tensors="pt").pixel_values
generated_ids = model.generate(pixel_values.to(device))
generated_text = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
print(generated_text)
从HuggingFace Hub下载的模型会本地保存到 ~/.cache/huggingface/hub/
。
✨ 主要特性
- 该模型经过特定类型表格单元格数据的训练,能有效识别其中的手写文本。
- 基于微调的国家档案馆多世纪手写文本识别模型和微软TrOCR模型,具有较好的识别能力。
💻 使用示例
基础用法
from transformers import TrOCRProcessor, VisionEncoderDecoderModel
from PIL import Image
import torch
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model_checkpoint = "Kansallisarkisto/tablecell-htr"
line_image_path = "/path/to/textline_image.jpg"
processor = TrOCRProcessor.from_pretrained(model_checkpoint)
model = VisionEncoderDecoderModel.from_pretrained(model_checkpoint).to(device)
image = Image.open(line_image_path).convert("RGB")
pixel_values = processor(image, return_tensors="pt").pixel_values
generated_ids = model.generate(pixel_values.to(device))
generated_text = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
print(generated_text)
高级用法
目前文档未提及高级用法相关内容。
📚 详细文档
预期用途与限制
- 该模型经过特定类型表格单元格数据的训练,对其他数据集的泛化能力可能较差。
- 模型的输入为文本行图像,不建议使用其他类型的输入。
训练数据
模型使用6704张文本行图像进行训练,验证数据集包含836张文本行图像。
训练过程
本模型使用NVIDIA RTX A6000 GPU进行训练,采用以下超参数:
- 训练批次大小:16
- 训练轮数:15
- 优化器:AdamW
- 文本序列最大长度:64
其他参数使用默认值(更多信息请见此处)。训练代码可在 train_trocr.py
文件中找到。
评估结果
使用验证数据集的评估结果如下:
验证损失 |
验证字符错误率(CER) |
验证单词错误率(WER) |
0.903 |
0.107 |
0.237 |
这些指标使用 Evaluate 库计算得出。关于CER指标的更多信息可查看此处,关于WER指标的更多信息可查看此处。
🔧 技术细节
本模型基于微调的国家档案馆多世纪手写文本识别模型和微软的TrOCR模型,使用特定的表格单元格图像数据进行训练。训练过程中使用了NVIDIA RTX A6000 GPU,并设置了一系列超参数以优化模型性能。评估时采用了常见的损失、字符错误率和单词错误率等指标,确保模型的准确性和可靠性。
📄 许可证
本项目采用MIT许可证。
属性 |
详情 |
模型类型 |
图像到文本识别模型 |
训练数据 |
6704张文本行图像用于训练,836张文本行图像用于验证 |