参考 yacs--Python代码运行时的配置系统 - 云+社区 - 腾讯云
yacs 是Ross Girshick 开发的一个工具。https://github.com/rbgirshick/yacs
Ross Girshick是目标检测界的领军人物,在2015年提出faster RCNN。
yacs安装:
D:\PycharmProjects\gitRepository2018\git_yacs\yacs>python setup.py install
running install
running bdist_egg
running egg_info
creating yacs.egg-info
writing yacs.egg-info\PKG-INFO
writing dependency_links to yacs.egg-info\dependency_links.txt
writing requirements to yacs.egg-info\requires.txt
writing top-level names to yacs.egg-info\top_level.txt
writing manifest file 'yacs.egg-info\SOURCES.txt'
reading manifest file 'yacs.egg-info\SOURCES.txt'
writing manifest file 'yacs.egg-info\SOURCES.txt'
installing library code to build\bdist.win-amd64\egg
running install_lib
running build_py
creating build
creating build\lib
creating build\lib\yacs
copying yacs\config.py -> build\lib\yacs
copying yacs\tests.py -> build\lib\yacs
copying yacs\__init__.py -> build\lib\yacs
creating build\bdist.win-amd64
creating build\bdist.win-amd64\egg
creating build\bdist.win-amd64\egg\yacs
copying build\lib\yacs\config.py -> build\bdist.win-amd64\egg\yacs
copying build\lib\yacs\tests.py -> build\bdist.win-amd64\egg\yacs
copying build\lib\yacs\__init__.py -> build\bdist.win-amd64\egg\yacs
byte-compiling build\bdist.win-amd64\egg\yacs\config.py to config.cpython-36.pyc
byte-compiling build\bdist.win-amd64\egg\yacs\tests.py to tests.cpython-36.pyc
byte-compiling build\bdist.win-amd64\egg\yacs\__init__.py to __init__.cpython-36.pyc
creating build\bdist.win-amd64\egg\EGG-INFO
copying yacs.egg-info\PKG-INFO -> build\bdist.win-amd64\egg\EGG-INFO
copying yacs.egg-info\SOURCES.txt -> build\bdist.win-amd64\egg\EGG-INFO
copying yacs.egg-info\dependency_links.txt -> build\bdist.win-amd64\egg\EGG-INFO
copying yacs.egg-info\requires.txt -> build\bdist.win-amd64\egg\EGG-INFO
copying yacs.egg-info\top_level.txt -> build\bdist.win-amd64\egg\EGG-INFO
zip_safe flag not set; analyzing archive contents...
creating dist
creating 'dist\yacs-0.1.4-py3.6.egg' and adding 'build\bdist.win-amd64\egg' to it
removing 'build\bdist.win-amd64\egg' (and everything under it)
Processing yacs-0.1.4-py3.6.egg
Copying yacs-0.1.4-py3.6.egg to g:\programdata\anaconda3\lib\site-packages
Adding yacs 0.1.4 to easy-install.pth file
Installed g:\programdata\anaconda3\lib\site-packages\yacs-0.1.4-py3.6.egg
Processing dependencies for yacs==0.1.4
Searching for PyYAML==3.12
Best match: PyYAML 3.12
Adding PyYAML 3.12 to easy-install.pth file
Using g:\programdata\anaconda3\lib\site-packages
Finished processing dependencies for yacs==0.1.4
config.py
from yacs.config import CfgNode as CN
_C = CN()
_C.SYSTEM = CN()
_C.SYSTEM.NUM_GPUS = 8
_C.SYSTEM.NUM_WORKERS = 4
_C.TRAIN = CN()
_C.TRAIN.HYPERPARAMETER_1 = 0.1
_C.TRAIN.SCALES = (2, 4, 8, 16)
cfg = _C
config.yaml
SYSTEM:
NUM_GPUS: 2
TRAIN:
SCALES: (1, 2)
main.py测试
from config import cfg
if __name__ == "__main__":
cfg.merge_from_file("config.yaml")
cfg.freeze()
cfg2 = cfg.clone()
cfg2.defrost()
cfg2.TRAIN.SCALES = (8, 16, 32)
cfg2.freeze()
print("cfg:")
print(cfg)
print("cfg2:")
print(cfg2)
运行结果如下:
cfg:
SYSTEM:
NUM_GPUS: 2
NUM_WORKERS: 4
TRAIN:
HYPERPARAMETER_1: 0.1
SCALES: (1, 2)
cfg2:
SYSTEM:
NUM_GPUS: 2
NUM_WORKERS: 4
TRAIN:
HYPERPARAMETER_1: 0.1
SCALES: (8, 16, 32)
这里面有四个常用函数经常用来配置超参数:
merge_from_file
这个函数会将yaml文件中指定的超参数对默认值进行覆盖。merge_from_list
作用同上面的类似,只不过是通过命令行的方式覆盖。opts = ["SYSTEM.NUM_GPUS", 8, "TRAIN.SCALES", "(1, 2, 3, 4)"]
cfg.merge_from_list(opts)
print("cfg\n",cfg)