|
1 | 1 | # Structures
|
| 2 | + |
| 3 | +To unify input and output interfaces between different models and modules, OpenMMLab 2.0 MMEngine defines an abstract data structure, |
| 4 | +it has implemented basic functions of `Create`, `Read`, `Update`, `Delete`, supported data transferring among different types of devices |
| 5 | +and tensor-like or dictionary-like operations such as `.cpu()`, `.cuda()`, `.get()` and `.detach()`. |
| 6 | +More details can be found [here](https://github.com/open-mmlab/mmengine/blob/main/docs/en/advanced_tutorials/data_element.md). |
| 7 | + |
| 8 | +MMSegmentation also follows this interface protocol and defines `SegDataSample` which is used to encapsulate the data of semantic segmentation task. |
| 9 | + |
| 10 | +## Semantic Segmentation Data SegDataSample |
| 11 | + |
| 12 | +[SegDataSample](mmseg.structures.SegDataSample) includes three main fields `gt_sem_seg`, `pred_sem_seg` and `seg_logits`, which are used to store the annotation information and prediction results respectively. |
| 13 | + |
| 14 | +| Field | Type | Description | |
| 15 | +| -------------- | ------------------------- | ------------------------------------------ | |
| 16 | +| gt_sem_seg | [`PixelData`](#pixeldata) | Annotation information. | |
| 17 | +| pred_instances | [`PixelData`](#pixeldata) | The predicted result. | |
| 18 | +| seg_logits | [`PixelData`](#pixeldata) | The raw (non-normalized) predicted result. | |
| 19 | + |
| 20 | +The following sample code demonstrates the use of `SegDataSample`. |
| 21 | + |
| 22 | +```python |
| 23 | +import torch |
| 24 | +from mmengine.structures import PixelData |
| 25 | +from mmseg.structures import SegDataSample |
| 26 | + |
| 27 | +img_meta = dict(img_shape=(4, 4, 3), |
| 28 | + pad_shape=(4, 4, 3)) |
| 29 | +data_sample = SegDataSample() |
| 30 | +# defining gt_segmentations for encapsulate the ground truth data |
| 31 | +gt_segmentations = PixelData(metainfo=img_meta) |
| 32 | +gt_segmentations.data = torch.randint(0, 2, (1, 4, 4)) |
| 33 | + |
| 34 | +# add and process property in SegDataSample |
| 35 | +data_sample.gt_sem_seg = gt_segmentations |
| 36 | +assert 'gt_sem_seg' in data_sample |
| 37 | +assert 'sem_seg' in data_sample.gt_sem_seg |
| 38 | +assert 'img_shape' in data_sample.gt_sem_seg.metainfo_keys() |
| 39 | +print(data_sample.gt_sem_seg.shape) |
| 40 | +''' |
| 41 | +(4, 4) |
| 42 | +''' |
| 43 | +print(data_sample) |
| 44 | +''' |
| 45 | +<SegDataSample( |
| 46 | +
|
| 47 | + META INFORMATION |
| 48 | +
|
| 49 | + DATA FIELDS |
| 50 | + gt_sem_seg: <PixelData( |
| 51 | +
|
| 52 | + META INFORMATION |
| 53 | + img_shape: (4, 4, 3) |
| 54 | + pad_shape: (4, 4, 3) |
| 55 | +
|
| 56 | + DATA FIELDS |
| 57 | + data: tensor([[[1, 1, 1, 0], |
| 58 | + [1, 0, 1, 1], |
| 59 | + [1, 1, 1, 1], |
| 60 | + [0, 1, 0, 1]]]) |
| 61 | + ) at 0x1c2b4156460> |
| 62 | +) at 0x1c2aae44d60> |
| 63 | +''' |
| 64 | + |
| 65 | +# delete and change property in SegDataSample |
| 66 | +data_sample = SegDataSample() |
| 67 | +gt_segmentations = PixelData(metainfo=img_meta) |
| 68 | +gt_segmentations.data = torch.randint(0, 2, (1, 4, 4)) |
| 69 | +data_sample.gt_sem_seg = gt_segmentations |
| 70 | +data_sample.gt_sem_seg.set_metainfo(dict(img_shape=(4,4,9), pad_shape=(4,4,9))) |
| 71 | +del data_sample.gt_sem_seg.img_shape |
| 72 | + |
| 73 | +# Tensor-like operations |
| 74 | +data_sample = SegDataSample() |
| 75 | +gt_segmentations = PixelData(metainfo=img_meta) |
| 76 | +gt_segmentations.data = torch.randint(0, 2, (1, 4, 4)) |
| 77 | +cuda_gt_segmentations = gt_segmentations.cuda() |
| 78 | +cuda_gt_segmentations = gt_segmentations.to('cuda:0') |
| 79 | +cpu_gt_segmentations = cuda_gt_segmentations.cpu() |
| 80 | +cpu_gt_segmentations = cuda_gt_segmentations.to('cpu') |
| 81 | +``` |
| 82 | + |
| 83 | +## Customize New Property in SegDataSample |
| 84 | + |
| 85 | +If you want to customize new property in `SegDataSample`, you may follow [SegDataSample](https://github.com/open-mmlab/mmsegmentation/blob/1.x/mmseg/structures/seg_data_sample.py) below: |
| 86 | + |
| 87 | +```python |
| 88 | +class SegDataSample(BaseDataElement): |
| 89 | + ... |
| 90 | + |
| 91 | + @property |
| 92 | + def xxx_property(self) -> xxxData: |
| 93 | + return self._xxx_property |
| 94 | + |
| 95 | + @xxx_property.setter |
| 96 | + def xxx_property(self, value: xxxData) -> None: |
| 97 | + self.set_field(value, '_xxx_property', dtype=xxxData) |
| 98 | + |
| 99 | + @xxx_property.deleter |
| 100 | + def xxx_property(self) -> None: |
| 101 | + del self._xxx_property |
| 102 | +``` |
| 103 | + |
| 104 | +Then a new property would be added to `SegDataSample`. |
0 commit comments