yaml.constructor.ConstructorError:无法确定标记的构造函数

问题描述

我有一个yaml文件,其中包含以下几行:

grasp_pose: !!python/object/new:geometry_msgs.msg._Pose.Pose
    state:
      - !!python/object/new:geometry_msgs.msg._Point.Point
        state: [0.445,-0.449,0.116]

使用yaml可以毫无问题地加载:

with open(os.path.expanduser(config_path),'r') as f:
    self.config = yaml.load(f,Loader=yaml.Loader)

但是,使用here中所述的hydra和@hydra.main(config_path=config_path,strict=True),我得到了以下错误

    main()
  File "/xxx/lib/python3.6/site-packages/hydra/main.py",line 24,in decorated_main
    strict=strict,File "xxx/lib/python3.6/site-packages/hydra/_internal/utils.py",line 174,in run_hydra
    overrides=args.overrides,File "xxx/lib/python3.6/site-packages/hydra/_internal/hydra.py",line 79,in run
    config_file=config_file,overrides=overrides,with_log_configuration=True
  File "xxx/lib/python3.6/site-packages/hydra/_internal/hydra.py",line 343,in compose_config
    config_file=config_file,strict=strict
  File "xxx/lib/python3.6/site-packages/hydra/_internal/config_loader.py",line 61,in load_configuration
    job_cfg = self._create_cfg(cfg_filename=config_file,record_load=False)
  File "xxx/lib/python3.6/site-packages/hydra/_internal/config_loader.py",line 437,in _create_cfg
    cfg = self._load_config_impl(cfg_filename,record_load=record_load)
  File "xxx/lib/python3.6/site-packages/hydra/_internal/config_loader.py",line 253,in _load_config_impl
    loaded_cfg = OmegaConf.load(fullpath)
  File "xxx/lib/python3.6/site-packages/omegaconf/omegaconf.py",in load
    return OmegaConf.create(yaml.load(f,Loader=get_yaml_loader()))
  File "xxx/lib/python3.6/site-packages/yaml/__init__.py",line 114,in load
    return loader.get_single_data()
  File "xxx/lib/python3.6/site-packages/yaml/constructor.py",line 43,in get_single_data
    return self.construct_document(node)
  File "xxx/lib/python3.6/site-packages/yaml/constructor.py",line 52,in construct_document
    for dummy in generator:
  File "xxx/lib/python3.6/site-packages/yaml/constructor.py",line 404,in construct_yaml_map
    value = self.construct_mapping(node)
  File "xxx/lib/python3.6/site-packages/yaml/constructor.py",line 210,in construct_mapping
    return super().construct_mapping(node,deep=deep)
  File "xxx/lib/python3.6/site-packages/yaml/constructor.py",line 135,in construct_mapping
    value = self.construct_object(value_node,line 92,in construct_object
    data = constructor(self,node)
  File "xxx/lib/python3.6/site-packages/yaml/constructor.py",line 420,in construct_undefined
    node.start_mark)
yaml.constructor.ConstructorError: Could not determine a constructor for the tag 'tag:yaml.org,2002:python/object/new:geometry_msgs.msg._Pose.Pose'
  in "xxxxxxxxx/config/rl_config.yaml",line 85,column 15

我不确定问题是什么,并且缺乏Yaml的工作经验。

解决方法

OmegaConf-在Hydra中为配置对象提供动力的底层库-不支持YAML文件中的任意对象。 但是,OmegaConf和Hydra 1.0中有一项称为“结构化配置”的功能(可作为发行候选版本安装,并将很快发布),该功能为配置对象添加了强大的静态类型。

我建议您阅读slidesdocs以及Hydra 1.0 Structured Configs教程。