【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"])
相关推荐
QXWZ_IA9 小时前
【深度】打破IoT“米级”魔咒:当RTK SDK遇上具身智能与工业互联,厘米级定位如何重塑边缘计算?
物联网·自动驾驶·嵌入式开发·rtk·北斗导航
地平线开发者13 小时前
征程6|YOLOv5x 在 Horizon 征程6 上的端到端部署实践(下)
算法·自动驾驶
亚远景aspice1 天前
亚远景-GB44721‑2026 自动驾驶安全文档体系:依托ASPICE构建合规交付包
自动驾驶·aspice·gb44721
爱分享的康康5 天前
解锁路测数据价值|康谋自动驾驶采集‑分析全链路方案解读
人工智能·数码相机·自动驾驶
anscos5 天前
案例分享| Latitude AI × Parasoft:搭建 L2-L3 自动驾驶量产验证与功能安全完整链路
人工智能·安全·自动驾驶
地平线开发者6 天前
BEVdet模型解析
算法·自动驾驶
Embedded-Xin11 天前
中间件—zenoh零基础入门
linux·中间件·rust·机器人·自动驾驶·嵌入式
康谋自动驾驶12 天前
高保真+强可控:自动驾驶仿真的混合渲染方案
人工智能·机器学习·自动驾驶
康谋自动驾驶12 天前
GMSL2相机ROS2采集链路:多相机时序偏差降低方案
数码相机·自动驾驶·汽车·仿真测试
惊鸿一博13 天前
基于Diffusion的轨迹优化:扩散模型在自动驾驶中的原理、架构与落地挑战
人工智能·自动驾驶·diffusion·规控