MindOCR在线推理¶
关于在线推理: 在线推理是基于原生MindSpore框架,通过加载模型文件,然后使用MindSpore API运行预测来进行推理。
与离线推理(在MindOCR中的“deploy/py_infer”中实现)相比,在线推理不需要对目标平台进行模型转换,可以直接在训练设备(例如Ascend 910)上运行。但是它需要安装重型AI框架,并且模型没有针对部署进行优化。
因此,在线推理更适合于演示和可视化评估模型对未知数据的泛化能力。
依赖关系和安装¶
环境 | 版本 |
---|---|
MindSpore | >=1.9 |
Python | >=3.7 |
支持的平台:Linux、MacOS、Windows(未测试)
支持的设备:CPU、GPU和Ascend。
请先克隆MindOCR
git clone https://github.com/mindspore-lab/mindocr.git
然后安装依赖项
pip install -r requirements.txt
对于MindSpore(>=1.9)安装,请遵循官方安装说明为您的机器提供最佳匹配。
文本检测¶
要对输入图像或包含多个图像的目录运行文本检测,请执行
python tools/infer/text/predict_det.py --image_dir {path_to_img or dir_to_imgs} --det_algorithm DB++
运行后,推理结果保存在{args.draw_img_save_dir}/det_results.txt
中,其中--draw_img_save_dir
是保存结果的目录,这是./inference_results
的默认设置,这里是一些示例结果。
示例1:
img_108.jpg的可视化结果
其中保存的txt文件如下
img_108.jpg [[[228, 440], [403, 413], [406, 433], [231, 459]], [[282, 280], [493, 252], [499, 293], [288, 321]], [[500, 253], [636, 232], [641, 269], [505, 289]], ...]
示例2:
paper_sam.png的可视化结果
其中保存的txt文件如下
paper_sam.png [[[1161, 340], [1277, 340], [1277, 378], [1161, 378]], [[895, 335], [1152, 340], [1152, 382], [894, 378]], ...]
注意事项:
- 对于高分辨率的输入图像,请将--det_limit_side_len
设置得更大,例如1280。--det_limit_type
可以设置为“min”或“max”,其中“min”表示限制图像大小至少为--det_limit_side_len
,“max”表示限制图像大小最多为--det_limit_side_len
。
-
有关更多参数说明和用法,请运行
python tools/infer/text/predict_det.py -h
或查看tools/infer/text/config.py
-
目前,该脚本可以持续运行以避免动态形状问题并获得更好的性能。
支持的检测算法和网络¶
算法网络在tools/infer/text/predict_det.py
中定义。
文本识别¶
要对输入图像或包含多个图像的目录运行文本识别,请执行
python tools/infer/text/predict_rec.py --image_dir {path_to_img or dir_to_imgs} --rec_algorithm CRNN
{args.draw_img_save_dir}/rec_results.txt
中,其中--draw_img_save_dir
是保存结果的目录,这是./inference_results
的默认设置。下面是一些结果的例子。
- 英文文本识别
word_1216.png
word_1217.png
识别结果:
word_1216.png coffee
word_1217.png club
- 中文文本识别:
cert_id.png
doc_cn3.png
识别结果:
cert_id.png 公民身份号码44052419
doc_cn3.png 马拉松选手不会为短暂的领先感到满意,而是永远在奔跑。
注意事项:
- 有关更多参数说明和用法,请运行python tools/infer/text/predict_rec.py -h
或查看tools/infer/text/config.py
- 支持批量推理和单模推理。默认情况下启用批处理模式以提高速度。您可以通过--rec_batch_size
设置批量大小。您还可以通过设置--det_batch_mode
False在单一模式下运行,如果文本长度变化很大,这可能会提高准确性。
支持的识别算法和网络¶
算法网络在tools/infer/text/predict_rec.py
中定义
目前,所列型号不支持空格字符识别。我们将很快予以支持。
文本检测与识别级联¶
要对输入图像或目录中的多个图像运行文本定位(即检测所有文本区域,然后识别每个文本区域),请运行:
python tools/infer/text/predict_system.py --image_dir {path_to_img or dir_to_imgs} \
--det_algorithm DB++ \
--rec_algorithm CRNN
注意:如果要可视化输入图像上的检测和识别结果,请设置
--visualize_output True
。
运行后,推理结果保存在{args.draw_img_save_dir}/system_results.txt
中,其中--draw_img_save_dir
是保存结果的目录,这是./inference_results
的默认设置。下面是一些结果的例子。
示例1:
img_10.jpg检测和识别的可视化结果
其中保存的txt文件如下
img_10.jpg [{"transcription": "residential", "points": [[43, 88], [149, 78], [151, 101], [44, 111]]}, {"transcription": "areas", "points": [[152, 83], [201, 81], [202, 98], [153, 100]]}, {"transcription": "when", "points": [[36, 56], [101, 56], [101, 78], [36, 78]]}, {"transcription": "you", "points": [[99, 54], [143, 52], [144, 78], [100, 80]]}, {"transcription": "pass", "points": [[140, 54], [186, 50], [188, 74], [142, 78]]}, {"transcription": "by", "points": [[182, 52], [208, 52], [208, 75], [182, 75]]}, {"transcription": "volume", "points": [[199, 30], [254, 30], [254, 46], [199, 46]]}, {"transcription": "your", "points": [[164, 28], [203, 28], [203, 46], [164, 46]]}, {"transcription": "lower", "points": [[109, 25], [162, 25], [162, 46], [109, 46]]}, {"transcription": "please", "points": [[31, 18], [109, 20], [108, 48], [30, 46]]}]
示例2:
web_cvpr.png检测和识别的可视化结果
其中保存的txt文件如下
web_cvpr.png [{"transcription": "canada", "points": [[430, 148], [540, 148], [540, 171], [430, 171]]}, {"transcription": "vancouver", "points": [[263, 148], [420, 148], [420, 171], [263, 171]]}, {"transcription": "cvpr", "points": [[32, 69], [251, 63], [254, 174], [35, 180]]}, {"transcription": "2023", "points": [[194, 44], [256, 45], [255, 72], [194, 70]]}, {"transcription": "june", "points": [[36, 45], [110, 44], [110, 70], [37, 71]]}, {"transcription": "1822", "points": [[114, 43], [190, 45], [190, 70], [113, 69]]}]
注意事项:
1、如需更多参数说明和用法,请运行python tools/infer/text/predict_system.py -h
或查看tools/infer/text/config.py
推理结果的评估¶
为了推理整个(https://rrc.cvc.uab.es/?ch=4&com=downloads)测试集,请运行:
python tools/infer/text/predict_system.py --image_dir /path/to/icdar15/det/test_images /
--det_algorithm {DET_ALGO} /
--rec_algorithm {REC_ALGO} /
--det_limit_type min /
--det_limit_side_len 720
注意:由于ICDAR15中的输入图像具有高分辨率(720x1280),因此我们将
det_limit_type
设置为'min'以获得更好的性能。
运行后,包括图像名称、边界框(points
)和识别文本 (transcription
)在内的结果将保存在{args.draw_img_save_dir}/system_results.txt
中。预测结果格式如下:。
img_1.jpg [{"transcription": "hello", "points": [600, 150, 715, 157, 714, 177, 599, 170]}, {"transcription": "world", "points": [622, 126, 695, 129, 694, 154, 621, 151]}, ...]
img_2.jpg [{"transcription": "apple", "points": [553, 338, 706, 318, 709, 342, 556, 362]}, ...]
...
准备 ground truth 文件(格式同上),可从tools/dataset_converters
中的数据集转换脚本获取,运行以下命令对预测结果进行评估。
python deploy/eval_utils/eval_pipeline.py --gt_path path/to/gt.txt --pred_path path/to/system_results.txt
使用MindSpore 2.0rc1对Ascend 910上的文本定位推理结果的评估如下所示。
注意事项:
1、目前在线推理流水线未进行效率优化,FPS仅用于模型间的比较。如果FPS是您的最高优先级,请参考Ascend 310上的推断,这要快得多。
2、除非另有说明,所有实验均以--det_limit_type
="min"和--det_limit_side
=720运行。
3、SVTR在混合精度模式下运行(amp_level=O2),因为它针对O2进行了优化。
参数列表¶
所有CLI参数定义都可以通过python tools/infer/text/predict_system.py -h
或tools/infer/text/config.py
查看。
开发人员指南-如何添加新的推断模型¶
预处理¶
最佳预处理策略可能因模型而异,特别是对于调整大小设置(keep_ratio, padding等)。对于不同的任务,我们在tools/infer/text/preprocess.py
中为每个模型定义了预处理管道。
如果发现默认的预处理管道或超参数不满足网络要求,请通过更改If-else条件或向tools/infer/text/preprocess.py
中的optimal_hparam
dict添加新的键值对进行扩展,其中key是算法名称,该值是用于目标网络推断的合适的超参数设置。
网络推理¶
在predict_det.py
和predict_rec.py
中的algo_to_model_name
dict中定义了支持的算法及其相应的网络名称(可使用list_model()
API进行检查)。
要添加新的检测模型进行推断,请在algo_to_model_name
dict中添加一个新的键值对,其中键值是算法名称,该值是注册在mindocr/models/{your_model}.py
中的相应网络名称。
默认情况下,模型权重将从mindocr/models/{your_model}.py
中的pro-defined URL加载。如果要加载本地检查点,请将--det_model_dir
或--rec_model_dir
设置为本地检查点的路径或包含模型检查点的目录。
后处理程序¶
与预处理类似,每个算法的后处理方法可能会有所不同。每个算法的后处理方法在tools/infer/text/postprocess.py
中定义。
如果您发现默认的后处理方法或超参数不满足模型需要,请扩展If-else条件或在tools/infer/text/postprocess.py
中的optimal_hparam
dict中添加一个新的键值对,其中键是算法名称,值是超参数设置。