TransformBroadcaster¶
- class mmcv.transforms.TransformBroadcaster(transforms: List[Union[Dict, Callable[[Dict], Dict]]], mapping: Optional[Dict] = None, remapping: Optional[Dict] = None, auto_remap: Optional[bool] = None, allow_nonexist_keys: bool = False, share_random_params: bool = False)[source]¶
A transform wrapper to apply the wrapped transforms to multiple data items. For example, apply Resize to multiple images.
- Parameters
transforms (list[dict | callable]) – Sequence of transform object or config dict to be wrapped.
mapping (dict) – A dict that defines the input key mapping. Note that to apply the transforms to multiple data items, the outer keys of the target items should be remapped as a list with the standard inner key (The key required by the wrapped transform). See the following example and the document of
mmcv.transforms.wrappers.KeyMapper
for details.remapping (dict) – A dict that defines the output key mapping. The keys and values have the same meanings and rules as in the
mapping
. Default: None.auto_remap (bool, optional) – If True, an inverse of the mapping will be used as the remapping. If auto_remap is not given, it will be automatically set True if ‘remapping’ is not given, and vice versa. Default: None.
allow_nonexist_keys (bool) – If False, the outer keys in the mapping must exist in the input data, or an exception will be raised. Default: False.
share_random_params (bool) – If True, the random transform (e.g., RandomFlip) will be conducted in a deterministic way and have the same behavior on all data items. For example, to randomly flip either both input image and ground-truth image, or none. Default: False.
Note
To apply the transforms to each elements of a list or tuple, instead of separating data items, you can map the outer key of the target sequence to the standard inner key. See example 2. example.
Examples
>>> # Example 1: Broadcast to enumerated keys, each contains a single >>> # data element >>> pipeline = [ >>> dict(type='LoadImageFromFile', key='lq'), # low-quality img >>> dict(type='LoadImageFromFile', key='gt'), # ground-truth img >>> # TransformBroadcaster maps multiple outer fields to standard >>> # the inner field and process them with wrapped transforms >>> # respectively >>> dict(type='TransformBroadcaster', >>> # case 1: from multiple outer fields >>> mapping={'img': ['lq', 'gt']}, >>> auto_remap=True, >>> # share_random_param=True means using identical random >>> # parameters in every processing >>> share_random_param=True, >>> transforms=[ >>> dict(type='Crop', crop_size=(384, 384)), >>> dict(type='Normalize'), >>> ]) >>> ]
>>> # Example 2: Broadcast to keys that contains data sequences >>> pipeline = [ >>> dict(type='LoadImageFromFile', key='lq'), # low-quality img >>> dict(type='LoadImageFromFile', key='gt'), # ground-truth img >>> # TransformBroadcaster maps multiple outer fields to standard >>> # the inner field and process them with wrapped transforms >>> # respectively >>> dict(type='TransformBroadcaster', >>> # case 2: from one outer field that contains multiple >>> # data elements (e.g. a list) >>> # mapping={'img': 'images'}, >>> auto_remap=True, >>> share_random_param=True, >>> transforms=[ >>> dict(type='Crop', crop_size=(384, 384)), >>> dict(type='Normalize'), >>> ]) >>> ]
>>> Example 3: Set ignored keys in broadcasting >>> pipeline = [ >>> dict(type='TransformBroadcaster', >>> # Broadcast the wrapped transforms to multiple images >>> # 'lq' and 'gt, but only update 'img_shape' once >>> mapping={ >>> 'img': ['lq', 'gt'], >>> 'img_shape': ['img_shape', ...], >>> }, >>> auto_remap=True, >>> share_random_params=True, >>> transforms=[ >>> # `RandomCrop` will modify the field "img", >>> # and optionally update "img_shape" if it exists >>> dict(type='RandomCrop'), >>> ]) >>> ]