目录
一、build_dataset
build_dataset函数定义
def build_dataset(cfg, default_args=None):
dataset = build_from_cfg(cfg, DATASETS, default_args)
return dataset
二、build_from_cfg
build_from_cfg函数中实现了使用配置参数实例化数据集类
def build_from_cfg(cfg, registry, default_args=None):
return obj_cls(**args
三、class KittiTinyDataset(CustomDataset)
数据集定义,主要的实例化都是在父类CustomDataset中实现的
@DATASETS.register_module()
class KittiTinyDataset(CustomDataset):
CLASSES = ('Car', 'Pedestrian', 'Cyclist')
def load_annotations(self, ann_file):
...
四、class CustomDataset(Dataset)
CustomDataset会根据配置文件初始化图像和标注文件相关变量,加载标注信息load_annotations,最重要的部分是实例化self.pipeline,也就是数据的处理流程
@DATASETS.register_module()
class CustomDataset(Dataset):
CLASSES = None
def __init__(self,
ann_file,
pipeline,
classes=None,
data_root=None,
img_prefix='',
seg_prefix=None,
proposal_file=None,
test_mode=False,
filter_empty_gt=True,
file_client_args=dict(backend='disk')):
self.ann_file = ann_file
self.data_root = data_root
self.img_prefix = img_prefix
self.seg_prefix = seg_prefix
self.proposal_file = proposal_file
self.test_mode = test_mode
self.filter_empty_gt = filter_empty_gt
self.CLASSES = self.get_classes(classes)
self.file_client = mmcv.FileClient(**file_client_args)
if hasattr(self.file_client, 'get_local_path'):
with self.file_client.get_local_path(self.ann_file) as local_path:
self.data_infos = self.load_annotations(local_path)
# processing pipeline
self.pipeline = Compose(pipeline)
五、Compose
因为pipeline是一个list,我们会根据list的内容逐个 去实例化pipeline中的类LoadImageFromFile、LoadAnnotations、Resize、RandomFlip、Normalize、Pad、DefaultFormatBundle、Collect等
@PIPELINES.register_module()
class Compose:
"""Compose multiple transforms sequentially.
Args:
transforms (Sequence[dict | callable]): Sequence of transform object or
config dict to be composed.
"""
def __init__(self, transforms):
assert isinstance(transforms, collections.abc.Sequence)
self.transforms = []
for transform in transforms:
if isinstance(transform, dict):
transform = build_from_cfg(transform, PIPELINES)
self.transforms.append(transform)
elif callable(transform):
self.transforms.append(transform)
else:
raise TypeError('transform must be callable or a dict')
六、build_from_cfg(transform, PIPELINES)
最后再调用build_from_cfg实例化PIPELINES的所有流程