Compose a data pipeline with a sequence of transforms. Args: transforms (list[dict | callable]): Either config dicts of transforms or transform objects.
| 7 | |
| 8 | @PIPELINES.register_module() |
| 9 | class Compose(object): |
| 10 | """Compose a data pipeline with a sequence of transforms. |
| 11 | |
| 12 | Args: |
| 13 | transforms (list[dict | callable]): |
| 14 | Either config dicts of transforms or transform objects. |
| 15 | """ |
| 16 | def __init__(self, transforms): |
| 17 | assert isinstance(transforms, Sequence) |
| 18 | self.transforms = [] |
| 19 | for transform in transforms: |
| 20 | if isinstance(transform, dict): |
| 21 | transform = build_from_cfg(transform, PIPELINES) |
| 22 | self.transforms.append(transform) |
| 23 | elif callable(transform): |
| 24 | self.transforms.append(transform) |
| 25 | else: |
| 26 | raise TypeError('transform must be callable or a dict, but got' |
| 27 | f' {type(transform)}') |
| 28 | |
| 29 | def __call__(self, data): |
| 30 | for t in self.transforms: |
| 31 | data = t(data) |
| 32 | if data is None: |
| 33 | return None |
| 34 | return data |
| 35 | |
| 36 | def __repr__(self): |
| 37 | format_string = self.__class__.__name__ + '(' |
| 38 | for t in self.transforms: |
| 39 | format_string += f'\n {t}' |
| 40 | format_string += '\n)' |
| 41 | return format_string |
no outgoing calls
no test coverage detected