初始化UnstructuredDetectronModel遇TypeError:config_path参数无效
问题:UnstructuredDetectronModel初始化时TypeError:意外的关键字参数'config_path'
在使用UnstructuredDetectronModel初始化预训练模型时,传入config_path参数触发TypeError,报错信息:
TypeError: __init__() got an unexpected keyword argument 'config_path'
原因
UnstructuredDetectronModel作为layoutparser.models.Detectron2LayoutModel的封装类,其初始化方法并不直接接受config_path参数,而是需要传入已初始化的Detectron2LayoutModel实例,或通过预定义的模型名称加载。
解决方法
方法1:先创建Detectron2LayoutModel实例再传入
先使用layoutparser创建配置好的Detectron2LayoutModel,再将其作为参数传给UnstructuredDetectronModel:
from unstructured_inference.models.detectron2 import UnstructuredDetectronModel import layoutparser as lp # 初始化layoutparser的Detectron2LayoutModel lp_layout_model = lp.Detectron2LayoutModel( config_path='lp://PubLayNet/mask_rcnn_X_101_32x8d_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], ) # 用该实例初始化UnstructuredDetectronModel model = UnstructuredDetectronModel(model=lp_layout_model)
方法2:使用预定义模型名称(适用于官方支持的模型)
如果使用的是UnstructuredDetectronModel预定义的模型,可以直接通过model_name参数指定,无需手动传config_path:
from unstructured_inference.models.detectron2 import UnstructuredDetectronModel # 以pub_lay_net为例,对应PubLayNet预训练模型 model = UnstructuredDetectronModel( model_name="pub_lay_net", label_map={0: "Text", 1: "Title", 2: "List", 3: "Table", 4: "Figure"}, extra_config=["MODEL.ROI_HEADS.SCORE_THRESH_TEST", 0.8], )
内容的提问来源于stack exchange,提问作者marialagerholm
相关产品推荐
相关产品推荐

