Yacs 配置

Detectron2提供了一个基于键值对的配置系统,可用于获取标准、通用的行为。

该系统使用YAML和yacs。 Yaml是一种功能有限的语言, 因此我们不期望detectron2的所有功能都能通过配置实现。 如果您需要的功能在配置空间中不可用, 请使用detectron2的API编写代码。

随着更强大的LazyConfig系统的引入,我们不再向基于Yacs/Yaml的配置系统添加功能/新键。

基本用法

这里展示了CfgNode对象的一些基本用法。更多详情请参阅文档

from detectron2.config import get_cfg
cfg = get_cfg()    # obtain detectron2's default config
cfg.xxx = yyy      # add new configs for your own custom components
cfg.merge_from_file("my_cfg.yaml")   # load values from a file

cfg.merge_from_list(["MODEL.WEIGHTS", "weights.pth"])   # can also load values from a list of str
print(cfg.dump())  # print formatted configs
with open("output.yaml", "w") as f:
  f.write(cfg.dump())   # save config to file

除了基本的Yaml语法外,配置文件还可以定义_BASE_: base.yaml字段,该字段会首先加载一个基础配置文件。如果存在冲突,基础配置中的值将在子配置中被覆盖。我们为标准模型架构提供了多个基础配置。

detectron2中的许多内置工具支持通过命令行覆盖配置: 命令行中提供的键值对将覆盖配置文件中现有的值。 例如,demo.py可以与以下命令一起使用

./demo.py --config-file config.yaml [--other-options] \
  --opts MODEL.WEIGHTS /path/to/weights INPUT.MIN_SIZE_TEST 1000

要查看detectron2中可用配置的列表及其含义,请查阅Config References

项目中的配置

一个独立于detectron2库的项目可能需要定义自己的配置,这些配置需要被添加才能使项目正常运行,例如:

from detectron2.projects.point_rend import add_pointrend_config
cfg = get_cfg()    # obtain detectron2's default config
add_pointrend_config(cfg)  # add pointrend's default config
# ... ...

配置最佳实践

  1. 将您编写的配置文件视为“代码”:避免复制或重复它们;使用_BASE_在配置文件之间共享公共部分。

  2. 保持你编写的配置简洁:不要包含不影响实验设置的键。