-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathtyping_config.py
210 lines (174 loc) · 8.07 KB
/
typing_config.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
from collections.abc import Sized
from typing import Dict, List, Optional, Tuple, Union
import numpy as np
import torch
from mmdet.models.task_modules.samplers import SamplingResult
from mmengine.config import ConfigDict
from mmengine.structures import BaseDataElement, InstanceData
class Det3DDataElement(BaseDataElement):
@property
def gt_instances_3d(self) -> InstanceData:
return self._gt_instances_3d
@gt_instances_3d.setter
def gt_instances_3d(self, value: InstanceData) -> None:
self.set_field(value, '_gt_instances_3d', dtype=InstanceData)
@gt_instances_3d.deleter
def gt_instances_3d(self) -> None:
del self._gt_instances_3d
@property
def pred_instances_3d(self) -> InstanceData:
return self._pred_instances_3d
@pred_instances_3d.setter
def pred_instances_3d(self, value: InstanceData) -> None:
self.set_field(value, '_pred_instances_3d', dtype=InstanceData)
@pred_instances_3d.deleter
def pred_instances_3d(self) -> None:
del self._pred_instances_3d
IndexType = Union[str, slice, int, list, torch.LongTensor,
torch.cuda.LongTensor, torch.BoolTensor,
torch.cuda.BoolTensor, np.ndarray]
class PointData(BaseDataElement):
"""Data structure for point-level annotations or predictions.
All data items in ``data_fields`` of ``PointData`` meet the following
requirements:
- They are all one dimension.
- They should have the same length.
`PointData` is used to save point-level semantic and instance mask,
it also can save `instances_labels` and `instances_scores` temporarily.
In the future, we would consider to move the instance-level info into
`gt_instances_3d` and `pred_instances_3d`.
Examples:
>>> metainfo = dict(
... sample_idx=random.randint(0, 100))
>>> points = np.random.randint(0, 255, (100, 3))
>>> point_data = PointData(metainfo=metainfo,
... points=points)
>>> print(len(point_data))
100
>>> # slice
>>> slice_data = point_data[10:60]
>>> assert len(slice_data) == 50
>>> # set
>>> point_data.pts_semantic_mask = torch.randint(0, 255, (100,))
>>> point_data.pts_instance_mask = torch.randint(0, 255, (100,))
>>> assert tuple(point_data.pts_semantic_mask.shape) == (100,)
>>> assert tuple(point_data.pts_instance_mask.shape) == (100,)
"""
def __setattr__(self, name: str, value: Sized) -> None:
"""setattr is only used to set data.
The value must have the attribute of `__len__` and have the same length
of `PointData`.
"""
if name in ('_metainfo_fields', '_data_fields'):
if not hasattr(self, name):
super().__setattr__(name, value)
else:
raise AttributeError(f'{name} has been used as a '
'private attribute, which is immutable.')
else:
assert isinstance(value,
Sized), 'value must contain `__len__` attribute'
# TODO: make sure the input value share the same length
super().__setattr__(name, value)
__setitem__ = __setattr__
def __getitem__(self, item: IndexType) -> 'PointData':
"""
Args:
item (str, int, list, :obj:`slice`, :obj:`numpy.ndarray`,
:obj:`torch.LongTensor`, :obj:`torch.BoolTensor`):
Get the corresponding values according to item.
Returns:
:obj:`PointData`: Corresponding values.
"""
if isinstance(item, list):
item = np.array(item)
if isinstance(item, np.ndarray):
# The default int type of numpy is platform dependent, int32 for
# windows and int64 for linux. `torch.Tensor` requires the index
# should be int64, therefore we simply convert it to int64 here.
# Mode details in https://github.com/numpy/numpy/issues/9464
item = item.astype(np.int64) if item.dtype == np.int32 else item
item = torch.from_numpy(item)
assert isinstance(
item, (str, slice, int, torch.LongTensor, torch.cuda.LongTensor,
torch.BoolTensor, torch.cuda.BoolTensor))
if isinstance(item, str):
return getattr(self, item)
if isinstance(item, int):
if item >= len(self) or item < -len(self): # type: ignore
raise IndexError(f'Index {item} out of range!')
else:
# keep the dimension
item = slice(item, None, len(self))
new_data = self.__class__(metainfo=self.metainfo)
if isinstance(item, torch.Tensor):
assert item.dim() == 1, 'Only support to get the' \
' values along the first dimension.'
if isinstance(item, (torch.BoolTensor, torch.cuda.BoolTensor)):
assert len(item) == len(self), 'The shape of the ' \
'input(BoolTensor) ' \
f'{len(item)} ' \
'does not match the shape ' \
'of the indexed tensor ' \
'in results_field ' \
f'{len(self)} at ' \
'first dimension.'
for k, v in self.items():
if isinstance(v, torch.Tensor):
new_data[k] = v[item]
elif isinstance(v, np.ndarray):
new_data[k] = v[item.cpu().numpy()]
elif isinstance(
v, (str, list, tuple)) or (hasattr(v, '__getitem__')
and hasattr(v, 'cat')):
# convert to indexes from BoolTensor
if isinstance(item,
(torch.BoolTensor, torch.cuda.BoolTensor)):
indexes = torch.nonzero(item).view(
-1).cpu().numpy().tolist()
else:
indexes = item.cpu().numpy().tolist()
slice_list = []
if indexes:
for index in indexes:
slice_list.append(slice(index, None, len(v)))
else:
slice_list.append(slice(None, 0, None))
r_list = [v[s] for s in slice_list]
if isinstance(v, (str, list, tuple)):
new_value = r_list[0]
for r in r_list[1:]:
new_value = new_value + r
else:
new_value = v.cat(r_list)
new_data[k] = new_value
else:
raise ValueError(
f'The type of `{k}` is `{type(v)}`, which has no '
'attribute of `cat`, so it does not '
'support slice with `bool`')
else:
# item is a slice
for k, v in self.items():
new_data[k] = v[item]
return new_data # type: ignore
def __len__(self) -> int:
"""int: The length of `PointData`."""
if len(self._data_fields) > 0:
return len(self.values()[0])
else:
return 0
# Type hint of config data
ConfigType = Union[ConfigDict, dict]
OptConfigType = Optional[ConfigType]
# Type hint of one or more config data
MultiConfig = Union[ConfigType, List[ConfigType]]
OptMultiConfig = Optional[MultiConfig]
InstanceList = List[InstanceData]
OptInstanceList = Optional[InstanceList]
ForwardResults = Union[Dict[str, torch.Tensor], List[Det3DDataElement],
Tuple[torch.Tensor], torch.Tensor]
SamplingResultList = List[SamplingResult]
OptSamplingResultList = Optional[SamplingResultList]
SampleList = List[Det3DDataElement]
OptSampleList = Optional[SampleList]