mmcv.Config.fromfile() 是 MMCV 提供的一个配置加载工具,它会将 .py 配置文件中的变量解析为一个 Config 对象。这个对象允许你通过点号语法(如 cfg.data.train)访问嵌套结构。
from mmcv import Config
cfg = Config.fromfile('path/to/config.py')
作用:读取 config.py 文件中所有定义的变量,并构建成一个 Config 对象。
你可以像访问字典一样访问这些变量:
print(cfg.data.train)print(cfg.model.type)
BEVFormer 工程里train.py有如下代码片段
args = parse_args() #导入配置文件路径 默认为BEVFormer/projects/configs/bevformer/bevformer_tiny.py
cfg = Config.fromfile(args.config)
args.config 默认为bevformer_tiny.py,即将bevformer_tiny.py定义的变量构建一个config对象。
train.py中代码:
if len(cfg.workflow) == 2:
val_dataset = copy.deepcopy(cfg.data.val)
# in case we use a dataset wrapper
if 'dataset' in cfg.data.train:
val_dataset.pipeline = cfg.data.train.dataset.pipeline
else:
val_dataset.pipeline = cfg.data.train.pipeline
找到bevformer_tiny.py定义代码片段:
data = dict(
samples_per_gpu=1,
workers_per_gpu=8,
train=dict(
type=dataset_type,
data_root=data_root,
ann_file=data_root + 'robotruck_infos_train.pkl',
pipeline=train_pipeline,
classes=class_names,
modality=input_modality,
test_mode=False,
use_valid_flag=True,
bev_size=(bev_h_, bev_w_),
queue_length=queue_length,
# we use box_type_3d='LiDAR' in kitti and nuscenes dataset
# and box_type_3d='Depth' in sunrgbd and scannet dataset.
box_type_3d='LiDAR'),
val=dict(type=dataset_type,
data_root=data_root,
ann_file=data_root + 'robotruck_infos_val.pkl',
pipeline=test_pipeline, bev_size=(bev_h_, bev_w_),
classes=class_names, modality=input_modality, samples_per_gpu=1),
test=dict(type=dataset_type,
data_root=data_root,
ann_file=data_root + 'robotruck_infos_val.pkl',
pipeline=test_pipeline, bev_size=(bev_h_, bev_w_),
classes=class_names, modality=input_modality),
shuffler_sampler=dict(type='DistributedGroupSampler'),
nonshuffler_sampler=dict(type='DistributedSampler')
)
data.train无dataset字段,因此val_dataset.pipeline = cfg.data.train.pipeline,此处引用的pipeline即train_pipeline,即:
train_pipeline = [
dict(type='LoadMultiViewImageFromFiles', to_float32=True),
dict(type='PhotoMetricDistortionMultiViewImage'),
dict(type='LoadAnnotations3D', with_bbox_3d=True, with_label_3d=True, with_attr_label=False),
dict(type='ObjectRangeFilter', point_cloud_range=point_cloud_range),
dict(type='ObjectNameFilter', classes=class_names),
dict(type='RandomScaleImageMultiViewImage', scales=[0.3]),
dict(type='NormalizeMultiviewImage', **img_norm_cfg),
dict(type='PadMultiViewImage', size_divisor=32),
dict(type='DefaultFormatBundle3D', class_names=class_names),
dict(type='CustomCollect3D', keys=['gt_bboxes_3d', 'gt_labels_3d', 'img'])
]