Layout-Parser基础API测试代码异常问题排查
Layout-Parser + Detectron2 在MAC M2+Python3.9环境下的问题解决
环境与测试代码
环境:MAC M2、Python3.9,已验证detectron2与torch安装成功。
测试代码:
import layoutparser as lp import cv2 image = cv2.imread("data/test-image.jpeg") image = image[..., ::-1] model = lp.models.Detectron2LayoutModel( config_path ='lp://PubLayNet/faster_rcnn_R_50_FPN_3x/config', label_map ={0: "Text", 1: "Title", 2: "List", 3:"Table", 4:"Figure"}, extra_config=["MODEL.ROI_HEADS.SCORE_THRESH_TEST", 0.8] ) model.detect(image)
错误1:_pickle.UnpicklingError: invalid load key, '<'
原因
自动下载的模型权重文件损坏或不完整,detectron2尝试加载的文件实际是HTML错误页面(比如下载失败返回的404页面),导致反序列化失败。
解决方案
- 手动下载PubLayNet对应模型的权重文件(faster_rcnn_R_50_FPN_3x),保存到本地路径;
- 修改代码,在
extra_config中指定本地权重文件路径:
model = lp.models.Detectron2LayoutModel( config_path='lp://PubLayNet/faster_rcnn_R_50_FPN_3x/config', label_map={0: "Text", 1: "Title", 2: "List", 3:"Table", 4:"Figure"}, extra_config=[ "MODEL.ROI_HEADS.SCORE_THRESH_TEST", 0.8, "MODEL.WEIGHTS", "/your/local/path/model_final.pth" ] )
或者直接使用本地的config文件和权重文件路径,跳过自动下载流程。
错误2:AttributeError: module 'PIL.Image' has no attribute 'LINEAR'
原因
Detectron2 0.5版本与新版Pillow不兼容——新版Pillow已将Image.LINEAR重命名为Image.BILINEAR,但Detectron2 0.5仍在使用旧名称。
解决方案
二选一即可:
- 方案1:升级Detectron2到0.6及以上版本(兼容新版Pillow),回到最初环境,解决第一个权重加载问题即可正常运行;
- 方案2:降级Pillow到Detectron2 0.5兼容的版本,执行命令:
pip install pillow==9.5.0
内容的提问来源于stack exchange,提问作者Gautam
相关产品推荐
相关产品推荐

