yacs--Python代码运行时的配置系统

参考   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)

这里面有四个常用函数经常用来配置超参数:

  • cfg = get_cfg(): 获取已经配置好默认参数的cfg
  • cfg.merge_from_file(args.config_file):config_file是指定的yaml配置文件,通过merge_from_file这个函数会将yaml文件中指定的超参数对默认值进行覆盖。
  • cfg.merge_from_list(args.opts):merge_from_list作用同上面的类似,只不过是通过命令行的方式覆盖。
    例如
  • cfg.freeze()表示将参数冻结
opts = ["SYSTEM.NUM_GPUS", 8, "TRAIN.SCALES", "(1, 2, 3, 4)"]

cfg.merge_from_list(opts)

print("cfg\n",cfg)

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Wanderer001

ROIAlign原理

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值