【mmengine】配置器(config)(入门)读取与使用

一、 介绍

MMEngine 实现了抽象的配置类(Config),为用户提供统一的配置访问接口。

配置类能够支持不同格式的配置文件,包括 python,json,yaml,用户可以根据需求选择自己偏好的格式。

配置类提供了类似字典或者 Python 对象属性的访问接口,用户可以十分自然地进行配置字段的读取和修改。

为了方便算法框架管理配置文件,配置类也实现了一些特性,例如配置文件的字段继承等。

二、 配置文件读取

配置类提供了统一的接口 Config.fromfile(),来读取和解析配置文件。

  1. python格式配置文件:perfon_cfg.py
python 复制代码
# Python 格式
name = "SHUAI"
addr = "Shanghai"
interest = ["food","travel"]
computer = dict(brand='dell', gpu="mx350")
  1. yaml格式配置文件:perfon_cfg.yaml
yaml 复制代码
name: "SHUAI"
addr: "Shanghai"
interest:
    - food
    - travel
computer:
    brand: "dell"
    gpu: mx350
  1. json格式配置文件:perfon_cfg.json
json 复制代码
{
  "name": "SHUAI",
  "addr": "Shanghai",
  "interest": ["food", "travel"],
  "computer": {"brand": "dell", "gpu": "mx350"}
}
  1. mmengine.config.Config.fromfile读取py,yaml,json三种格式的配置信息
python 复制代码
from mmengine.config import Config

# 从py中读取
import person_cfg as cfg_py
print(cfg_py.name)
print(cfg_py.addr)
print(cfg_py.interest)
print(cfg_py.computer)

# 从yaml中读取
cfg_yaml = Config.fromfile('person_cfg.yaml')
print(cfg_yaml)

# 从json中读取
cfg_json = Config.fromfile('person_cfg.json')
print(cfg_json)

三、 配置文件的使用

通过读取配置文件来初始化配置对象后,就可以像使用普通字典或者 Python 类一样来使用这个变量了。提供了两种访问接口,即类似字典的接口 cfg['key'] 或者类似 Python 对象属性的接口 cfg.key。这两种接口都支持读写。

python 复制代码
### 使用 ###
print('------')
print(cfg_yaml.name)
print(cfg_yaml.addr)
print(cfg_yaml.interest)
print(cfg_yaml.computer)

print('------')
print(cfg_json["name"])
print(cfg_json["addr"])
print(cfg_json["interest"])
print(cfg_json["computer"])

注意,配置文件中定义的嵌套字段(即类似字典的字段),在 Config 中会将其转化为 ConfigDict 类,该类继承了 Python 内置字典类型的全部接口,同时也支持以对象属性的方式访问数据。

附上完整代码:

python 复制代码
# 1. person_cfg.py中代码
name = "SHUAI"
addr = "Shanghai"
interest = ["food","travel"]
computer = dict(brand='dell', gpu="mx350")
# 2. person_cfg.json中代码
{
  "name": "SHUAI",
  "addr": "Shanghai",
  "interest": ["food", "travel"],
  "computer": {"brand": "dell", "gpu": "mx350"}
}
# 3. person_cfg.yaml中代码
name: "SHUAI"
addr: "Shanghai"
interest:
    - food
    - travel
computer:
    brand: "dell"
    gpu: mx350


# 4. config.py中代码
from mmengine.config import Config

### 读取 ###
# 从py中读取
import person_cfg as cfg_py
print(cfg_py.name)
print(cfg_py.addr)
print(cfg_py.interest)
print(cfg_py.computer)

# 从yaml中读取
cfg_yaml = Config.fromfile('person_cfg.yaml')
print(cfg_yaml)

# 从json中读取
cfg_json = Config.fromfile('person_cfg.json')
print(cfg_json)

### 使用 ###
print('------')
print(cfg_yaml.name)
print(cfg_yaml.addr)
print(cfg_yaml.interest)
print(cfg_yaml.computer)

print('------')
print(cfg_json["name"])
print(cfg_json["addr"])
print(cfg_json["interest"])
print(cfg_json["computer"])
相关推荐
电气_空空1 天前
自动驾驶汽车横向控制方法研究综述
人工智能·自动驾驶·汽车·毕业设计·毕设
地平线开发者2 天前
征程6 工具链常用工具和 API 整理(含新手示例)
算法·自动驾驶
学步_技术2 天前
自动驾驶系列—自动驾驶发展史介绍
人工智能·机器学习·计算机视觉·自动驾驶·sae
驱动起爆大师x_x2 天前
《Ubuntu20.04环境下的ROS进阶学习8》
linux·笔记·单片机·嵌入式硬件·学习·机器人·自动驾驶
学步_技术2 天前
自动驾驶系列—深度剖析自动驾驶芯片SoC架构:选型指南与应用实战
人工智能·架构·自动驾驶·soc·芯片
驱动起爆大师x_x2 天前
《Ubuntu20.04环境下的ROS进阶学习7》
linux·笔记·学习·机器人·自动驾驶
学步_技术2 天前
自动驾驶系列—自动驾驶MCU架构全方位解析:从单核到多核的选型指南与应用实例
单片机·mcu·架构·自动驾驶
Ai173163915793 天前
NVIDIA 的 Blackwell 架构:解析 B100、B200 和 GB200
服务器·人工智能·机器学习·自动驾驶·电脑·ai写作·gpu算力
开MINI的工科男4 天前
【笔记】自动驾驶预测与决策规划_Part4_时空联合规划
人工智能·笔记·自动驾驶·预测与决策·时空联合规划