返回顶部
热门问答 更多热门问答
技术文章 更多技术文章

阶跃星辰开源GOT-OCR2.0:统一端到端模型,魔搭一站式推理微调最佳实践来啦!

[复制链接]
链载Ai 显示全部楼层 发表于 4 小时前 |阅读模式 打印 上一主题 下一主题
01

引言

ingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;letter-spacing: 0.544px;text-wrap: wrap;visibility: visible;color: rgb(163, 163, 163) !important;">

OCR技术在将现实世界中的文本信息转换为可编辑、可搜索的数据方面发挥着至关重要的作用,它桥接着物理世界与数字世界,是实现信息自动化处理的关键技术。作为AI-1.0时代的技术成果,OCR技术已经相对成熟,被广泛应用于文档管理、自动驾驶、智能监控等多个领域。


然而,随着技术的发展和应用需求的提升,OCR 1.0技术逐渐展现出局限性。传统OCR系统在处理复杂场景、模糊图像或特殊字体时,准确率和效率往往不尽人意。此外,随着AI进入以LLM(Large Language Models)为核心的AI-2.0时代,对OCR技术提出了更高的要求,不仅仅是文本识别,还需要与多模态能力相结合,实现更为丰富的信息提取和理解。


因此,OCR-2.0应运而生,该模型由阶跃星辰多模态团队开源。GOT-OCR2.0将所有的人工光学信号(例如文本、数学/分子公式、表格、图表、乐谱甚至几何形状等)统称为“字符”,并提出通用光学字符识别理论以及一个优秀的模型——GOT来促进OCR-2.0的到来。该模型具有580百万参数,是一个统一、优雅和端到端的模型,由高压缩编码器和长上下文解码器组成。作为OCR-2.0模型,GOT可以处理各种OCR任务中的上述“字符”。在输入方面,模型支持常用的场景和文档风格图像,在切片和整页样式下。在输出方面,GOT可以通过简单的提示生成纯文本或格式化结果(markdown/tikz/smiles/kern)。此外,模型还具备交互式OCR功能,即通过坐标或颜色进行区域级识别。另外,研究团队还将动态分辨率和多页面OCR技术应用于GOT以提高其实用性。GOT-OCR2.0模型未来也将会合入阶跃星辰多模态大模型!整一个期待住了!



传统的OCR识别系统通常基于多模块PipeLine包括字符检测,版面分析和字符识别部分,通用性较低。本文推出的GOT-OCR2.0模型,用于实现各种场景下的文本识别任务。该模型采用了Transformer结构,并结合了图像编码器、线性层和解码器三个模块。在预训练阶段,首先通过纯文本识别任务来预训练视觉编码器,然后连接一个更强大的解码器来进行多任务联合训练,以注入更多的OCR-2.0知识。最后,通过对解码器进行后训练,可以定制化新的OCR功能。


通用 OCR 模型需要够通用,体现在输入输出都要通用上。GOT 的通用具体表现为:


-入方面:模型支持 Scene Text OCR、Document OCR、Fine-grained OCR、More General OCR 等任务。


-输出方面:模型同时支持 plain texts 输出以及可读性强、可编辑的 formatted 文本输出,如 markdown 等。


GOT-OCT-2.0模型结构,采用 vision encoder+input embedding layer+decoder 的 pipeline。



训练模型中主要的改进点分别为

  1. Encoder 主体使用了带 local attention的Vary-tiny编码器VITDet 架构来降低计算成本,不会让 CLIP 方案的全程 global attention 在高分辨率下激活太大,炸显存。

  2. 在数据准备阶段,使用了多种类型的数据引擎,包括合成数据和真实数据,以增强模型的鲁棒性和泛化能力。

  3. 在后训练过程中,只针对解码器进行训练,以实现定制化的OCR功能。


研究团队将整个训练过程分为三个步骤,没有一个阶段锁 LLM,过程中没有存在图像到文本的对齐阶段,进而导致损害 image token 的文字压缩率。



三个训练阶段分别为:

第一阶段:高效预训练 encoder,GOT 在整个训练过程中,没有 A100 级别的卡,为了节省资源,该阶段使用小型 OPT-125M 作为 decoder 为 encoder 提供优化方向,快速灌入大量数据。

第二阶段:联合训练 encoder-decoder,该阶段 GOT 的基本结构搭建完成,为上一阶段预训练好的 encoder,以及 Qwen 团队预训练好的 Qwen0.5B。

研究团队稍稍加大了 decoder 的大小,因为该阶段需要喂入大量 OCR-2. 0的知识,而不少数据(如化学式的 OCR)其实也是带点 reasoning的,不过更小的 decoder 他们未敢尝试。


第三阶段:锁住 encoder,加强 decoder 以适配更多的 OCR 应用场景,如支持坐标或者颜色引导的细粒度 OCR(点读笔可能会用到),支持动态分辨率 OCR 技术(超大分辨率图可能会用到),多页 OCR 技术。


模型链接:

https://modelscope.cn/models/stepfun-ai/GOT-OCR2_0


代码链接:

https://github.com/Ucas-HaoranWei/GOT-OCR2.0/


论文链接:

https://arxiv.org/abs/2409.01704


02

模型体验

ingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;letter-spacing: 0.544px;text-wrap: wrap;visibility: visible;color: rgb(163, 163, 163) !important;">

体验链接


模型下载

modelscopedownload--modelstepfun-ai/GOT-OCR2_0--local_dir./GOT-OCR2_0


03

模型使用

ingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;letter-spacing: 0.544px;text-wrap: wrap;visibility: visible;color: rgb(163, 163, 163) !important;">

安装依赖:

!pipinstallverovio


模型推理:

from modelscope import AutoModel, AutoTokenizer
tokenizer = AutoTokenizer.from_pretrained('stepfun-ai/GOT-OCR2_0', trust_remote_code=True)model = AutoModel.from_pretrained('stepfun-ai/GOT-OCR2_0', trust_remote_code=True, low_cpu_mem_usage=True, device_map='cuda', use_safetensors=True, pad_token_id=tokenizer.eos_token_id)model = model.eval().cuda()

# input your test imageimage_file = '/mnt/workspace/58F3EF14-E073-4BBE-B9D9-53CCFE6AE183.png'
# plain texts OCRres = model.chat(tokenizer, image_file, ocr_type='ocr')
# format texts OCR:# res = model.chat(tokenizer, image_file, ocr_type='format')
# fine-grained OCR:# res = model.chat(tokenizer, image_file, ocr_type='ocr', ocr_box='')# res = model.chat(tokenizer, image_file, ocr_type='format', ocr_box='')# res = model.chat(tokenizer, image_file, ocr_type='ocr', ocr_color='')# res = model.chat(tokenizer, image_file, ocr_type='format', ocr_color='')
# multi-crop OCR:# res = model.chat_crop(tokenizer, image_file, ocr_type='ocr')# res = model.chat_crop(tokenizer, image_file, ocr_type='format')
# render the formatted OCR results:# res = model.chat(tokenizer, image_file, ocr_type='format', render=True, save_render_file = './demo.html')
print(res)

显存占用:


04

模型微调

ingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;letter-spacing: 0.544px;text-wrap: wrap;visibility: visible;color: rgb(163, 163, 163) !important;">

ingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;letter-spacing: 0.544px;text-wrap: wrap;visibility: visible;color: rgb(163, 163, 163) !important;">

我们使用ms-swift对GOT-OCR2.0进行微调。ms-swift是魔搭社区官方提供的大模型与多模态大模型微调推理框架。


ms-swift开源地址:

https://github.com/modelscope/ms-swift


通常,多模态大模型微调会使用自定义数据集进行微调。在这里,我们将展示可直接运行的demo。我们使用Latex-OCR数据集:https://modelscope.cn/datasets/AI-ModelScope/LaTeX_OCR 进行微调。


在开始微调之前,请确保您的环境已准备妥当。

gitclonehttps://github.com/modelscope/ms-swift.gitcdms-swiftpipinstall-e.[llm]

ingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;letter-spacing: 0.544px;text-wrap: wrap;visibility: visible;color: rgb(163, 163, 163) !important;">

ingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;letter-spacing: 0.544px;text-wrap: wrap;visibility: visible;color: rgb(163, 163, 163) !important;text-align: left;">微调脚本:

# 默认:微调 LLM & projector, 冻结 vision encoderCUDA_VISIBLE_DEVICES=0 swift sft \--model_type got-ocr2 \--model_id_or_path stepfun-ai/GOT-OCR2_0 \--sft_type lora \--dataset latex-ocr-print#5000
# Deepspeed ZeRO2NPROC_PER_NODE=4 \CUDA_VISIBLE_DEVICES=0,1,2,3 swift sft \--model_type got-ocr2 \--model_id_or_path stepfun-ai/GOT-OCR2_0 \--sft_type lora \--dataset latex-ocr-print#5000 \--deepspeed default-zero2

ingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;letter-spacing: 0.544px;text-wrap: wrap;visibility: visible;color: rgb(163, 163, 163) !important;">

训练显存占用:


如果要使用自定义数据集,只需按以下方式进行指定:

#val_dataset可选,如果不指定,则会从dataset中切出一部分数据集作为验证集--datasettrain.jsonl\--val_datasetval.jsonl\
{"query":"<image>55555","response":"66666","images":["image_path"]}{"query":"<image><image>eeeee","response":"fffff","history":[],"images":["image_path1","image_path2"]}{"query":"EEEEE","response":"FFFFF","history":[["query1","response1"],["query2","response2"]]}

ingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;letter-spacing: 0.544px;text-wrap: wrap;visibility: visible;color: rgb(163, 163, 163) !important;">

训练loss图:



微调后推理脚本如下:

CUDA_VISIBLE_DEVICES=0 swift infer \--ckpt_dir output/got-ocr2/vx-xxx/checkpoint-xxx \--load_dataset_config true
# or merge-lora & inferCUDA_VISIBLE_DEVICES=0 swift infer \--ckpt_dir output/got-ocr2/vx-xxx/checkpoint-xxx \--load_dataset_config true --merge_lora true

ingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;letter-spacing: 0.544px;text-wrap: wrap;visibility: visible;color: rgb(163, 163, 163) !important;">

微调后模型对验证集进行推理的结果:

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

链载AI是专业的生成式人工智能教程平台。提供Stable Diffusion、Midjourney AI绘画教程,Suno AI音乐生成指南,以及Runway、Pika等AI视频制作与动画生成实战案例。从提示词编写到参数调整,手把手助您从入门到精通。
  • 官方手机版

  • 微信公众号

  • 商务合作

  • Powered by Discuz! X3.5 | Copyright © 2025-2025. | 链载Ai
  • 桂ICP备2024021734号 | 营业执照 | |广西笔趣文化传媒有限公司|| QQ