MindOCR是一个基于MindSpore 框架开发的OCR开源工具箱,集成系列主流文字检测识别的算法、模型,并提供易用的训练和推理工具,可以帮助用户快速开发和应用业界SoTA文本检测、文本识别模型,如DBNet/DBNet++和CRNN/SVTR,满足图像文档理解的需求。
MindOCR基于MindSpore AI框架(支持CPU/GPU/NPU)开发,并适配以下框架版本。安装方式请参见下方的安装链接。
pip install -r requirements.txt
git clone https://github.com/mindspore-lab/mindocr.git
cd mindocr
pip install -e .
使用
-e
代表可编辑模式,可以帮助解决潜在的模块导入问题。
pip install mindocr
由于此项目正在积极开发中,从PyPI安装的版本目前已过期,我们将很快更新,敬请期待。
安装完MindOCR后,我们就很方便地进行任意图像的文本检测和识别,如下。
python tools/infer/text/predict_system.py --image_dir {path_to_img or dir_to_imgs} \
--det_algorithm DB++ \
--rec_algorithm CRNN
运行结束后,结果将被默认保存在./inference_results
路径,可视化结果如下:
文本检测、识别结果可视化
可以看到图像中的文字块均被检测出来并正确识别。更详细的用法介绍,请参考推理教程。
使用tools/train.py
脚本可以很容易地训练OCR模型,该脚本可支持文本检测和识别模型训练。
python tools/train.py --config {path/to/model_config.yaml}
--config
参数用于指定yaml文件的路径,该文件定义要训练的模型和训练策略,包括数据处理流程、优化器、学习率调度器等。
MindOCR在configs
文件夹中提供系列SoTA的OCR模型及其训练策略,用户可以快速将其适配到自己的任务或数据集上,参考例子如下
# train text detection model DBNet++ on icdar15 dataset
python tools/train.py --config configs/det/dbnet/db++_r50_icdar15.yaml
# train text recognition model CRNN on icdar15 dataset
python tools/train.py --config configs/rec/crnn/crnn_icdar15.yaml
类似的,使用tools/eval.py
脚本可以很容易地评估已训练好的模型,如下所示:
python tools/eval.py \
--config {path/to/model_config.yaml} \
--opt eval.dataset_root={path/to/your_dataset} eval.ckpt_load_path={path/to/ckpt_file}
更多使用方法,请参考使用教程中的模型训练章节。
你可以在MindOCR中对MindOCR自研模型或第三方模型(如PaddleOCR、MMOCR等)进行MindSpore Lite推理。
请见MindOCR自研模型推理-快速开始或第三方模型推理-快速开始。
关于以上模型的具体训练方法和结果,请参见configs下各模型子目录的readme文档。
关于MindSpore Lite和ACL模型推理的支持列表,
请参见MindOCR自研模型推理支持列表 和 第三方模型推理支持列表(如PaddleOCR、MMOCR等)。
MindOCR提供了数据格式转换工具 ,以支持不同格式的OCR数据集,支持用户自定义的数据集。
当前已在模型训练评估中验证过的公开OCR数据集如下。
我们会在更多的数据集上进行模型训练和验证。该列表将持续更新。
model
字段下增加resume
参数,允许传入具体路径resume: /path/to/train_resume.ckpt
或者通过设置resume: True
来加载在ckpt_save_dir下保存的trian_resume.ckpteval.dataset.output_columns
列表中增加"shape_list"实现。pred_cast_fp32
,修复存在无效多边形的错误。model-pretrained
设置为checkpoint url或本地路径来使用。train-ema
(默认值:False)和train-ema_decay
来启用。num_columns_to_net
-> net_input_column_index
: 输入网络的columns数量改为输入网络的columns索引num_columns_of_labels
-> label_column_index
: 用索引替换数量,以表示label的位置。grouping_strategy
参数以选择预定义的分组策略,或使用no_weight_decay_params
参数选择要从权重衰减中排除的层(例如,bias、norm)。示例可参考configs/rec/crn/crnn_icdar15.yaml
gradient_accumulation_steps
,全局批量大小=batch_size * devices * gradient_aaccumulation_steps。示例可参考configs/rec/crn/crnn_icdar15.yaml
grad_clip
设置为True来启用。loss_scale
字段并将type
参数设为dynamic
,参考例子请见configs/rec/crnn/crnn_icdar15.yaml
output_keys
-> output_columns
;num_keys_to_net
-> num_columns_to_net
; i) 在OpenI云平台上创建一个训练任务;
ii) 在网页上关联数据集,如ic15_mindocr;
iii) 增加 `config` 参数,在网页的UI界面配置yaml文件路径,如'/home/work/user-job-dir/V0001/configs/rec/test.yaml';
iv) 在网页的UI界面增加运行参数`enable_modelarts`并将其设置为True;
v) 填写其他项并启动训练任务。
我们欢迎包括问题单和PR在内的所有贡献,来让MindOCR变得更好。
请参考CONTRIBUTING.md作为贡献指南,请按照Model Template and Guideline的指引贡献一个适配所有接口的模型,多谢合作。
本项目遵从Apache License 2.0开源许可。
如果本项目对您的研究有帮助,请考虑引用:
@misc{MindSpore OCR 2023,
title={{MindSpore OCR }:MindSpore OCR Toolbox},
author={MindSpore Team},
howpublished = {\url{https://github.com/mindspore-lab/mindocr/}},
year={2023}
}
Dear OpenI User
Thank you for your continuous support to the Openl Qizhi Community AI Collaboration Platform. In order to protect your usage rights and ensure network security, we updated the Openl Qizhi Community AI Collaboration Platform Usage Agreement in January 2024. The updated agreement specifies that users are prohibited from using intranet penetration tools. After you click "Agree and continue", you can continue to use our services. Thank you for your cooperation and understanding.
For more agreement content, please refer to the《Openl Qizhi Community AI Collaboration Platform Usage Agreement》