yacs的使用,进行参数传递预配置 借助argparse

在这里插入图片描述

yacs 是Ross Girshick 开发的一个工具。https://github.com/rbgirshick/yacs

Ross Girshick是目标检测界的领军人物,在2015年提出faster RCNN。

yacs安装:


 
 
  1. D:\PycharmProjects\gitRepository2018\git_yacs\yacs>python setup.py install
  2. running install
  3. running bdist_egg
  4. running egg_info
  5. creating yacs.egg-info
  6. writing yacs.egg-info\PKG-INFO
  7. writing dependency_links to yacs.egg-info\dependency_links.txt
  8. writing requirements to yacs.egg-info\requires.txt
  9. writing top-level names to yacs.egg-info\top_level.txt
  10. writing manifest file 'yacs.egg-info\SOURCES.txt'
  11. reading manifest file 'yacs.egg-info\SOURCES.txt'
  12. writing manifest file 'yacs.egg-info\SOURCES.txt'
  13. installing library code to build\bdist.win-amd64\egg
  14. running install_lib
  15. running build_py
  16. creating build
  17. creating build\lib
  18. creating build\lib\yacs
  19. copying yacs\config.py -> build\lib\yacs
  20. copying yacs\tests.py -> build\lib\yacs
  21. copying yacs\__init_ _.py -> build\lib\yacs
  22. creating build\bdist.win-amd64
  23. creating build\bdist.win-amd64\egg
  24. creating build\bdist.win-amd64\egg\yacs
  25. copying build\lib\yacs\config.py -> build\bdist.win-amd64\egg\yacs
  26. copying build\lib\yacs\tests.py -> build\bdist.win-amd64\egg\yacs
  27. copying build\lib\yacs\__init_ _.py -> build\bdist.win-amd64\egg\yacs
  28. byte-compiling build\bdist.win-amd64\egg\yacs\config.py to config.cpython- 36.pyc
  29. byte-compiling build\bdist.win-amd64\egg\yacs\tests.py to tests.cpython- 36.pyc
  30. byte-compiling build\bdist.win-amd64\egg\yacs\__init_ _.py to __init_ _.cpython- 36.pyc
  31. creating build\bdist.win-amd64\egg\EGG-INFO
  32. copying yacs.egg-info\PKG-INFO -> build\bdist.win-amd64\egg\EGG-INFO
  33. copying yacs.egg-info\SOURCES.txt -> build\bdist.win-amd64\egg\EGG-INFO
  34. copying yacs.egg-info\dependency_links.txt -> build\bdist.win-amd64\egg\EGG-INFO
  35. copying yacs.egg-info\requires.txt -> build\bdist.win-amd64\egg\EGG-INFO
  36. copying yacs.egg-info\top_level.txt -> build\bdist.win-amd64\egg\EGG-INFO
  37. zip_safe flag not set; analyzing archive contents...
  38. creating dist
  39. creating 'dist\yacs-0.1.4-py3.6.egg' and adding 'build\bdist.win-amd64\egg' to it
  40. removing 'build\bdist.win-amd64\egg' ( and everything under it)
  41. Processing yacs- 0. 1.4-py3. 6.egg
  42. Copying yacs- 0. 1.4-py3. 6.egg to g:\programdata\anaconda3\lib\site-packages
  43. Adding yacs 0. 1.4 to easy-install.pth file
  44. Installed g:\programdata\anaconda3\lib\site-packages\yacs- 0. 1.4-py3. 6.egg
  45. Processing dependencies for yacs== 0. 1.4
  46. Searching for PyYAML== 3.12
  47. Best match: PyYAML 3.12
  48. Adding PyYAML 3.12 to easy-install.pth file
  49. Using g:\programdata\anaconda3\lib\site-packages
  50. Finished processing dependencies for yacs== 0. 1.4

config.py


 
 
  1. from yacs.config import CfgNode as CN
  2. _C = CN()
  3. _C.SYSTEM = CN()
  4. _C.SYSTEM.NUM_GPUS = 8
  5. _C.SYSTEM.NUM_WORKERS = 4
  6. _C.TRAIN = CN()
  7. _C.TRAIN.HYPERPARAMETER_1 = 0.1
  8. _C.TRAIN.SCALES = ( 2, 4, 8, 16)
  9. cfg = _C

config.yaml


 
 
  1. SYSTEM:
  2. NUM_GPUS: 2
  3. TRAIN:
  4. SCALES: ( 1, 2)

main.py测试


 
 
  1. from config import cfg
  2. if __name__ == "__main__":
  3. cfg.merge_from_file( "config.yaml")
  4. cfg.freeze()
  5. cfg2 = cfg.clone()
  6. cfg2.defrost()
  7. cfg2.TRAIN.SCALES = ( 8, 16, 32)
  8. cfg2.freeze()
  9. print( "cfg:")
  10. print(cfg)
  11. print( "cfg2:")
  12. print(cfg2)

运行结果如下:


 
 
  1. cfg:
  2. SYSTEM:
  3. NUM_GPUS: 2
  4. NUM_WORKERS: 4
  5. TRAIN:
  6. HYPERPARAMETER_1: 0.1
  7. SCALES: ( 1, 2)
  8. cfg2:
  9. SYSTEM:
  10. NUM_GPUS: 2
  11. NUM_WORKERS: 4
  12. TRAIN:
  13. HYPERPARAMETER_1: 0.1
  14. 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()表示将参数冻结

 
 
  1. opts = [ "SYSTEM.NUM_GPUS", 8, "TRAIN.SCALES", "(1, 2, 3, 4)"]
  2. cfg.merge_from_list(opts)
  3. print( "cfg\n",cfg)

 

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值