PaddlePaddle / PaddleOCR

Awesome multilingual OCR toolkits based on PaddlePaddle (practical ultra lightweight OCR system, support 80+ languages recognition, provide data annotation and synthesis tools, support training and deployment among server, mobile, embedded and IoT devices)
https://paddlepaddle.github.io/PaddleOCR/
Apache License 2.0
41.35k stars 7.57k forks source link

Kie SDMG-R训练时开启AMP导致Kie_backbone的forward参数形状改变,通过hardcode可以workaround #6973

Closed ChenNima closed 1 year ago

ChenNima commented 2 years ago

首先开启AMP: kie_unet_sdmgr.yml:

Global:
  ...
  use_amp: True

推理和评估模型正常,但是在训练时报错:

IndexError: (OutOfRange) The starting index 1 of slice is out of bounds in tensor 0-th axis, it shound be in the range of [-1, 1). (at /paddle/paddle/fluid/pybind/slice_utils.h:197)

出错位置为 kie_unet_sdmgr.py#Kie_backbone.forward

relations, texts, gt_bboxes, tag, img_size = inputs[1], inputs[
            2], inputs[3], inputs[5], inputs[-1]

开启AMP状态下, print出forward的参数inputs.shape, 值为 [1, 3, 1024, 1024]。关闭AMP时该参数为KeepKeys处理后的list,打开AMP后该参数变为了list的第一项,即image。

尝试修改KeepKeys,将输出从return data_list改为return [data_list], 开启AMP后kie_unet_sdmgr.py#Kie_backbone.forward成功获取到正确的参数。

而且kie_sdmgr_loss.py#SDMGRLoss.forward中的参数batch形状则会同时发生改变,需要

batch = batch[0]

KeepKeys的更改还原。此时AMP正常工作,可以观察到训练内存占用下降,训练速度增加。

综上所述,SDMG-R在开启AMP后,Kie_backbone训练时主干网络前向计算input输入从list改变为list的第一项,网络其他结构没有发生变化。

LDOUBLEV commented 2 years ago

感谢反馈,分析的很详细也很准确