Shortcuts

KeyMapper

class mmcv.transforms.KeyMapper(transforms: Optional[Union[Dict, Callable[[Dict], Dict], List[Union[Dict, Callable[[Dict], Dict]]]]] = None, mapping: Optional[Dict] = None, remapping: Optional[Dict] = None, auto_remap: Optional[bool] = None, allow_nonexist_keys: bool = False)[源代码]

A transform wrapper to map and reorganize the input/output of the wrapped transforms (or sub-pipeline).

参数
  • transforms (list[dict | callable], optional) – Sequence of transform object or config dict to be wrapped.

  • mapping (dict) – A dict that defines the input key mapping. The keys corresponds to the inner key (i.e., kwargs of the transform method), and should be string type. The values corresponds to the outer keys (i.e., the keys of the data/results), and should have a type of string, list or dict. None means not applying input mapping. Default: None.

  • 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.

实际案例

>>> # Example 1: KeyMapper 'gt_img' to 'img'
>>> pipeline = [
>>>     # Use KeyMapper to convert outer (original) field name
>>>     # 'gt_img' to inner (used by inner transforms) filed name
>>>     # 'img'
>>>     dict(type='KeyMapper',
>>>         mapping={'img': 'gt_img'},
>>>         # auto_remap=True means output key mapping is the revert of
>>>         # the input key mapping, e.g. inner 'img' will be mapped
>>>         # back to outer 'gt_img'
>>>         auto_remap=True,
>>>         transforms=[
>>>             # In all transforms' implementation just use 'img'
>>>             # as a standard field name
>>>             dict(type='Crop', crop_size=(384, 384)),
>>>             dict(type='Normalize'),
>>>         ])
>>> ]
>>> # Example 2: Collect and structure multiple items
>>> pipeline = [
>>>     # The inner field 'imgs' will be a dict with keys 'img_src'
>>>     # and 'img_tar', whose values are outer fields 'img1' and
>>>     # 'img2' respectively.
>>>     dict(type='KeyMapper',
>>>         dict(
>>>             type='KeyMapper',
>>>             mapping=dict(
>>>                 imgs=dict(
>>>                     img_src='img1',
>>>                     img_tar='img2')),
>>>         transforms=...)
>>> ]
>>> # Example 3: Manually set ignored keys by "..."
>>> pipeline = [
>>>     ...
>>>     dict(type='KeyMapper',
>>>         mapping={
>>>             # map outer key "gt_img" to inner key "img"
>>>             'img': 'gt_img',
>>>             # ignore outer key "mask"
>>>             'mask': ...,
>>>         },
>>>         transforms=[
>>>             dict(type='RandomFlip'),
>>>         ])
>>>     ...
>>> ]
transform(results: Dict)Dict[源代码]

Apply mapping, wrapped transforms and remapping.

Read the Docs v: 2.x
Versions
latest
stable
2.x
v2.0.1
v2.0.0
1.x
v1.7.1
v1.7.0
v1.6.2
v1.6.1
v1.6.0
v1.5.3
v1.5.2_a
v1.5.1
v1.5.0
v1.4.8
v1.4.7
v1.4.6
v1.4.5
v1.4.4
v1.4.3
v1.4.2
v1.4.1
v1.4.0
v1.3.18
v1.3.17
v1.3.16
v1.3.15
v1.3.14
v1.3.13
Downloads
pdf
html
epub
On Read the Docs
Project Home
Builds

Free document hosting provided by Read the Docs.