|
| 1 | +import warnings |
| 2 | +from mmcv.transforms import LoadAnnotations as MMCV_LoadAnnotations |
| 3 | + |
| 4 | +from deeplake.integrations.mmlab.mmseg.registry import TRANSFORMS |
| 5 | + |
| 6 | + |
| 7 | +@TRANSFORMS.register_module() |
| 8 | +class LoadAnnotations(MMCV_LoadAnnotations): |
| 9 | + """Load annotations for semantic segmentation provided by dataset. |
| 10 | +
|
| 11 | + The annotation format is as the following: |
| 12 | +
|
| 13 | + .. code-block:: python |
| 14 | +
|
| 15 | + { |
| 16 | + # Filename of semantic segmentation ground truth file. |
| 17 | + 'seg_map_path': 'a/b/c' |
| 18 | + } |
| 19 | +
|
| 20 | + After this module, the annotation has been changed to the format below: |
| 21 | +
|
| 22 | + .. code-block:: python |
| 23 | +
|
| 24 | + { |
| 25 | + # in str |
| 26 | + 'seg_fields': List |
| 27 | + # In uint8 type. |
| 28 | + 'gt_seg_map': np.ndarray (H, W) |
| 29 | + } |
| 30 | +
|
| 31 | + Required Keys: |
| 32 | +
|
| 33 | + - seg_map_path (str): Path of semantic segmentation ground truth file. |
| 34 | +
|
| 35 | + Added Keys: |
| 36 | +
|
| 37 | + - seg_fields (List) |
| 38 | + - gt_seg_map (np.uint8) |
| 39 | +
|
| 40 | + Args: |
| 41 | + reduce_zero_label (bool, optional): Whether reduce all label value |
| 42 | + by 1. Usually used for datasets where 0 is background label. |
| 43 | + Defaults to None. |
| 44 | + imdecode_backend (str): The image decoding backend type. The backend |
| 45 | + argument for :func:``mmcv.imfrombytes``. |
| 46 | + See :fun:``mmcv.imfrombytes`` for details. |
| 47 | + Defaults to 'pillow'. |
| 48 | + backend_args (dict): Arguments to instantiate a file backend. |
| 49 | + See https://mmengine.readthedocs.io/en/latest/api/fileio.htm |
| 50 | + for details. Defaults to None. |
| 51 | + Notes: mmcv>=2.0.0rc4, mmengine>=0.2.0 required. |
| 52 | + """ |
| 53 | + |
| 54 | + def __init__( |
| 55 | + self, |
| 56 | + reduce_zero_label=None, |
| 57 | + backend_args=None, |
| 58 | + imdecode_backend="pillow", |
| 59 | + ) -> None: |
| 60 | + super().__init__( |
| 61 | + with_bbox=False, |
| 62 | + with_label=False, |
| 63 | + with_seg=True, |
| 64 | + with_keypoints=False, |
| 65 | + imdecode_backend=imdecode_backend, |
| 66 | + backend_args=backend_args, |
| 67 | + ) |
| 68 | + self.reduce_zero_label = reduce_zero_label |
| 69 | + if self.reduce_zero_label is not None: |
| 70 | + warnings.warn( |
| 71 | + "`reduce_zero_label` will be deprecated, " |
| 72 | + "if you would like to ignore the zero label, please " |
| 73 | + "set `reduce_zero_label=True` when dataset " |
| 74 | + "initialized" |
| 75 | + ) |
| 76 | + self.imdecode_backend = imdecode_backend |
| 77 | + |
| 78 | + def _load_seg_map(self, results: dict) -> None: |
| 79 | + """Private function to load semantic segmentation annotations. |
| 80 | +
|
| 81 | + Args: |
| 82 | + results (dict): Result dict from :obj:``mmcv.BaseDataset``. |
| 83 | +
|
| 84 | + Returns: |
| 85 | + dict: The dict contains loaded semantic segmentation annotations. |
| 86 | + """ |
| 87 | + |
| 88 | + gt_semantic_seg = results.pop("dp_seg_map", None) |
| 89 | + |
| 90 | + # reduce zero_label |
| 91 | + if self.reduce_zero_label: |
| 92 | + # avoid using underflow conversion |
| 93 | + gt_semantic_seg[gt_semantic_seg == 0] = 255 |
| 94 | + gt_semantic_seg = gt_semantic_seg - 1 |
| 95 | + gt_semantic_seg[gt_semantic_seg == 254] = 255 |
| 96 | + # modify if custom classes |
| 97 | + if results.get("label_map", None) is not None: |
| 98 | + # Add deep copy to solve bug of repeatedly |
| 99 | + # replace `gt_semantic_seg`, which is reported in |
| 100 | + # https://github.com/open-mmlab/mmsegmentation/pull/1445/ |
| 101 | + gt_semantic_seg_copy = gt_semantic_seg.copy() |
| 102 | + for old_id, new_id in results["label_map"].items(): |
| 103 | + gt_semantic_seg[gt_semantic_seg_copy == old_id] = new_id |
| 104 | + results["gt_seg_map"] = gt_semantic_seg |
| 105 | + |
| 106 | + def __repr__(self) -> str: |
| 107 | + repr_str = self.__class__.__name__ |
| 108 | + repr_str += f"(reduce_zero_label={self.reduce_zero_label}, " |
| 109 | + repr_str += f"imdecode_backend='{self.imdecode_backend}', " |
| 110 | + repr_str += f"backend_args={self.backend_args})" |
| 111 | + return repr_str |
0 commit comments