标签:pre val lang 允许 os.path pen imagenet == 键值对
yaml[?j?m?l]: Yet Another Markup Language。yaml 是专门用来写配置文件的语言,非常简洁和强大,之前用ini也能写配置文件,看了yaml后,发现这个更直观,更方便,有点类似于json格式
基本语法:
支持的数据结构:
pip instal pyyaml
# python
{
"user": "admin",
"pwd": "123456",
}
# yaml
user: admin
pwd: 123456
字典嵌套字典
# python
"nb1":{
"user": "admin",
"pwd": "123456",
}
# yaml
nb1:
user: admin
pwd: 123456
yaml里面写一个数组,前面加一个‘-‘符号
- admin1: 123
- admin2: 345
n1: 12.30
n2: true
n4: ~
n6: !!str 123
, int->str# yaml
- user: admin1
pwd: '123'
- user: admin2
pwd: '234'
- user: admin3
pwd: '413'
用python读出来的结果
[
{'user': 'admin1', 'pwd': '123'},
{'user': 'admin2', 'pwd': '234'},
{'user': 'admin3', 'pwd': '413'}
]
Warning: YAMLLoadWarning: calling yaml.load() without Loader=... is deprecated, as the default Loader is unsafe. Please read https://msg.pyyaml.org/load for full details.
YAML 5.1版本后弃用了yaml.load(file)这个用法,因为觉得很不安全,5.1版本之后就修改了需要指定Loader,通过默认加载??器(FullLoader)禁止执行任意函数,该load函数也变得更加安全
e.g.
yaml.load(input, Loader=yaml.FullLoader)
有一个yaml配置文件,希望能让用户自定义神经网络,而不是使用完整的task_list,并且对于这些自定义的网络,也采用自定义的数据类型进行量化
# test.aml
caffe_models:
- imagenet
- mobilenet_v2
- resnet50
- inception_v3
- imagenet
- yolo_v2
- alexnet
- squeezenet_v1.0
- lenet
- mobilenet
dtype_list:
- asymmetric_quantized-u8
大概的思路:
import yaml
import os
yaml_path = os.path.join('test.aml')
f = open(yaml_path, 'r', encoding="utf-8")
cfg = f.read()
content = yaml.load(cfg, Loader=yaml.FullLoader)
task_list = []
for key in content:
if key == "caffe_models":
for value in content[key]:
task = (key, value)
task_list.append(task)
print('#'*100)
print(task_list)
标签:pre val lang 允许 os.path pen imagenet == 键值对
原文地址:https://www.cnblogs.com/sayiqiu/p/10808347.html