PaddleOCR icon indicating copy to clipboard operation
PaddleOCR copied to clipboard

KIE: KeyError: 'pred_id'

Open hchintada opened this issue 2 years ago • 1 comments

请提供下述完整信息以便快速定位问题/Please provide the following information to quickly locate the problem

  • 系统环境/System Environment:Tried both Windows10 and Linux Ubuntu18
  • 版本号/Version:Paddle:2.6 PaddleOCR: 问题相关组件/Related components:
  • 运行指令/Command Code:python tools/infer_kie_token_ser.py -c configs/kie/vi_layoutxlm/ser_vi_layoutxlm_xfund_zh.yml -o Architecture.Backbone.checkpoints=./output/ser_vi_layoutxlm_xxx/best_accuracy Global.infer_img=train_data/xxx/val/xxx.jpg
  • 完整报错/Complete Error Message:
Traceback (most recent call last):
  File "tools/infer_kie_token_ser.py", line 154, in <module>
    img_res = draw_ser_results(img_path, result)
  File "xxx\PaddleOCR\ppocr\utils\visual.py", line 41, in draw_ser_results
    if ocr_info["pred_id"] not in color_map:
KeyError: 'pred_id'

Was getting this error when trying to run inference on custom trained KIE model. The issue occurs when whitespaces are present in the OCR output. Was able to temporarily fix it by adding the below command

ocr_info = [ocr_ for ocr_ in ocr_info if not ocr_['transcription'].isspace()]

in line 72 inside the _infer method in the VQASerTokenLayoutLMPostProcess class in https://github.com/PaddlePaddle/PaddleOCR/blob/e4a03137a1669837350ddb90553febed113365fc/ppocr/postprocess/vqa_token_ser_layoutlm_postprocess.py

hchintada avatar Oct 11 '22 07:10 hchintada

你好,我这边运行没有问题,麻烦拉下最新的release2.6代码,然后安装下这个paddnelp看下:https://paddleocr.bj.bcebos.com/ppstructure/whl/paddlenlp-2.3.0.dev0-py3-none-any.whl

littletomatodonkey avatar Oct 11 '22 08:10 littletomatodonkey

环境 paddleocr==2.7, paddlenlp==2.5.2 运行 PaddleOCR-release-2.7/tools/infer_kie_token_ser.py,可视化步骤出现相同问题,是因为SER结果中某些文本没有预测结果引起的:

{'transcription': '有疑问请在样品保存期内提出!', 'label': 'other', 'points': [[88, 1482], [336, 1482], [336, 1498], [88, 1498]], 'id': 451, 'linking': [], 'bbox': [88, 1482, 336, 1498], 'pred_id': 0, 'pred': 'O'}
{'transcription': '检验者: ', 'label': 'other', 'points': [[510, 1466], [572, 1466], [572, 1484], [510, 1484]], 'id': 449, 'linking': [], 'bbox': [510, 1466, 572, 1484], 'pred_id': 0, 'pred': 'O'}
{'transcription': '审核者: ', 'label': 'other', 'points': [[804, 1466], [866, 1466], [866, 1484], [804, 1484]], 'id': 450, 'linking': [], 'bbox': [804, 1466, 866, 1484]}
Traceback (most recent call last):
  File "/home/chenxiyuan/data/layoutxlm/PaddleOCR-release-2.7/tools/infer_kie_token_ser.py", line 153, in <module>
    img_res = draw_ser_results(img_path, result, config['Global']['font_path'])
  File "/home/chenxiyuan/data/layoutxlm/PaddleOCR-release-2.7/ppocr/utils/visual.py", line 43, in draw_ser_results
    if ocr_info["pred_id"] not in color_map:
KeyError: 'pred_id'

可以将 https://github.com/PaddlePaddle/PaddleOCR/blob/0525f6bb01bfed401f767894619f6a25ee750892/ppocr/utils/visual.py#L41 修改为:

    for ocr_info in ocr_results:
        # somtimes "pred_id" and "pred" are not predicted
        if 'pred' not in ocr_info.keys() or 'pred_id' not in ocr_info.keys():
            continue
        if ocr_info["pred_id"] not in color_map:
            continue

XiYuan68 avatar Feb 02 '24 04:02 XiYuan68