mmagic.structures
¶
Package Contents¶
Classes¶
A data structure interface of MMagic. They are used as interfaces |
- class mmagic.structures.DataSample(*, metainfo: Optional[dict] = None, **kwargs)[source]¶
Bases:
mmengine.structures.BaseDataElement
A data structure interface of MMagic. They are used as interfaces between different components, e.g., model, visualizer, evaluator, etc. Typically, DataSample contains all the information and data from ground- truth and predictions.
- DataSample inherits from BaseDataElement. See more details in:
https://mmengine.readthedocs.io/en/latest/advanced_tutorials/data_element.html Specifically, an instance of BaseDataElement consists of two components, -
metainfo
, which contains some meta information,e.g., img_shape, img_id, color_order, etc.
data
, which contains the data used in the loop.
The attributes in
DataSample
are divided into several parts:gt_img
: Ground truth image(s).pred_img
: Image(s) of model predictions.ref_img
: Reference image(s).mask
: Mask in Inpainting.trimap
: Trimap in Matting.gt_alpha
: Ground truth alpha image in Matting.pred_alpha
: Predicted alpha image in Matting.gt_fg
: Ground truth foreground image in Matting.pred_fg
: Predicted foreground image in Matting.gt_bg
: Ground truth background image in Matting.pred_bg
: Predicted background image in Matting.gt_merged
: Ground truth merged image in Matting.
Examples:
>>> import torch >>> import numpy as np >>> from mmagic.structures import DataSample >>> img_meta = dict(img_shape=(800, 1196, 3)) >>> img = torch.rand((3, 800, 1196)) >>> data_sample = DataSample(gt_img=img, metainfo=img_meta) >>> assert 'img_shape' in data_sample.metainfo_keys() >>> data_sample <DataSample( META INFORMATION img_shape: (800, 1196, 3) DATA FIELDS gt_img: tensor(...) ) at 0x1f6a5a99a00>
We also support stack and split operation to handle a batch of data samples:
>>> import torch >>> import numpy as np >>> from mmagic.structures import DataSample >>> img_meta1 = img_meta2 = dict(img_shape=(800, 1196, 3)) >>> img1 = torch.rand((3, 800, 1196)) >>> img2 = torch.rand((3, 800, 1196)) >>> data_sample1 = DataSample(gt_img=img1, metainfo=img_meta1) >>> data_sample2 = DataSample(gt_img=img2, metainfo=img_meta1)
>>> # stack them and then use as batched-tensor! >>> data_sample = DataSample.stack([data_sample1, data_sample2]) >>> print(data_sample.gt_img.shape) torch.Size([2, 3, 800, 1196]) >>> print(data_sample.metainfo) {'img_shape': [(800, 1196, 3), (800, 1196, 3)]}
>>> # split them if you want >>> data_sample1_, data_sample2_ = data_sample.split() >>> assert (data_sample1_.gt_img == img1).all() >>> assert (data_sample2_.gt_img == img2).all()
- property gt_label¶
This the function to fetch gt label.
- Returns
gt label.
- Return type
LabelData
- META_KEYS¶
- DATA_KEYS¶
- set_predefined_data(data: dict) None [source]¶
set or change pre-defined key-value pairs in
data_field
by parameterdata
.- Parameters
data (dict) – A dict contains annotations of image or model predictions.
- set_tensor_data(data: dict) None [source]¶
convert input data to tensor, and then set or change key-value pairs in
data_field
by parameterdata
.- Parameters
data (dict) – A dict contains annotations of image or model predictions.
- set_gt_label(value: Union[numpy.ndarray, torch.Tensor, Sequence[numbers.Number], numbers.Number]) DataSample [source]¶
Set label of
gt_label
.
- gt_label()¶
Delete gt label.
- classmethod stack(data_samples: Sequence[DataSample]) DataSample [source]¶
Stack a list of data samples to one. All tensor fields will be stacked at first dimension. Otherwise the values will be saved in a list.
- Parameters
data_samples (Sequence['DataSample']) – A sequence of DataSample to stack.
- Returns
The stacked data sample.
- Return type
- split(allow_nonseq_value: bool = False) Sequence[DataSample] [source]¶
Split a sequence of data sample in the first dimension.
- Parameters
allow_nonseq_value (bool) – Whether allow non-sequential data in split operation. If True, non-sequential data will be copied for all split data samples. Otherwise, an error will be raised. Defaults to False.
- Returns
The list of data samples after splitting.
- Return type
Sequence[DataSample]