indoor_augment.py 8.35 KB
Newer Older
liyinhao's avatar
liyinhao committed
1
2
import numpy as np

3
from mmdet.datasets.builder import PIPELINES
liyinhao's avatar
liyinhao committed
4
5


6
@PIPELINES.register_module()
7
class IndoorFlipData(object):
liyinhao's avatar
liyinhao committed
8
    """Indoor flip data.
9

liyinhao's avatar
liyinhao committed
10
    Flip point cloud and ground truth boxes.
liyinhao's avatar
liyinhao committed
11
    The point cloud will be flipped along the yz plane
liyinhao's avatar
liyinhao committed
12
    and the xz plane with a certain probability.
13
14

    Args:
liyinhao's avatar
liyinhao committed
15
16
17
        flip_ratio_yz (float): Probability of being flipped along yz plane.
            Default: 0.5.
        flip_ratio_xz (float): Probability of being flipped along xz plane.
liyinhao's avatar
liyinhao committed
18
            Default: 0.5.
19
20
    """

liyinhao's avatar
liyinhao committed
21
22
23
    def __init__(self, flip_ratio_yz=0.5, flip_ratio_xz=0.5):
        self.flip_ratio_yz = flip_ratio_yz
        self.flip_ratio_xz = flip_ratio_xz
24
25

    def __call__(self, results):
26
27
        points = results['points']
        gt_bboxes_3d = results['gt_bboxes_3d']
liyinhao's avatar
liyinhao committed
28
        aligned = True if gt_bboxes_3d.shape[1] == 6 else False
liyinhao's avatar
liyinhao committed
29
30
        results['flip_yz'] = False
        results['flip_xz'] = False
liyinhao's avatar
liyinhao committed
31
        if np.random.random() < self.flip_ratio_yz:
32
            # Flipping along the YZ plane
liyinhao's avatar
liyinhao committed
33
34
            points[:, 0] = -1 * points[:, 0]
            gt_bboxes_3d[:, 0] = -1 * gt_bboxes_3d[:, 0]
liyinhao's avatar
liyinhao committed
35
            if not aligned:
liyinhao's avatar
liyinhao committed
36
                gt_bboxes_3d[:, 6] = np.pi - gt_bboxes_3d[:, 6]
liyinhao's avatar
liyinhao committed
37
            results['flip_yz'] = True
38

liyinhao's avatar
liyinhao committed
39
        if aligned and np.random.random() < self.flip_ratio_xz:
40
            # Flipping along the XZ plane
liyinhao's avatar
liyinhao committed
41
42
            points[:, 1] = -1 * points[:, 1]
            gt_bboxes_3d[:, 1] = -1 * gt_bboxes_3d[:, 1]
liyinhao's avatar
liyinhao committed
43
            results['flip_xz'] = True
44

liyinhao's avatar
liyinhao committed
45
46
        results['points'] = points
        results['gt_bboxes_3d'] = gt_bboxes_3d
47
48
49
50
        return results

    def __repr__(self):
        repr_str = self.__class__.__name__
liyinhao's avatar
liyinhao committed
51
52
        repr_str += '(flip_ratio_yz={})'.format(self.flip_ratio_yz)
        repr_str += '(flip_ratio_xz={})'.format(self.flip_ratio_xz)
53
54
55
        return repr_str


liyinhao's avatar
liyinhao committed
56
@PIPELINES.register_module()
liyinhao's avatar
liyinhao committed
57
class IndoorPointsColorJitter(object):
liyinhao's avatar
liyinhao committed
58
    """Indoor points color jitter.
liyinhao's avatar
liyinhao committed
59

liyinhao's avatar
liyinhao committed
60
61
    Randomly change the brightness and color of the point cloud, and
    drop out the points' colors with a certain range and probability.
liyinhao's avatar
liyinhao committed
62
63

    Args:
liyinhao's avatar
liyinhao committed
64
        color_mean (list[float]): Mean color of the point cloud.
liyinhao's avatar
liyinhao committed
65
            Default: [0.5, 0.5, 0.5].
liyinhao's avatar
liyinhao committed
66
        bright_range (list[float]): Range of brightness.
liyinhao's avatar
liyinhao committed
67
            Default: [0.8, 1.2].
liyinhao's avatar
liyinhao committed
68
        color_shift_range (list[float]): Range of color shift.
liyinhao's avatar
liyinhao committed
69
            Default: [0.95, 1.05].
liyinhao's avatar
liyinhao committed
70
        jitter_range (list[float]): Range of jittering.
liyinhao's avatar
liyinhao committed
71
            Default: [-0.025, 0.025].
72
        drop_prob (float): Probability to drop out points' color.
liyinhao's avatar
liyinhao committed
73
74
75
76
77
78
79
80
            Default: 0.3
    """

    def __init__(self,
                 color_mean=[0.5, 0.5, 0.5],
                 bright_range=[0.8, 1.2],
                 color_shift_range=[0.95, 1.05],
                 jitter_range=[-0.025, 0.025],
81
                 drop_prob=0.3):
liyinhao's avatar
liyinhao committed
82
83
84
85
        self.color_mean = color_mean
        self.bright_range = bright_range
        self.color_shift_range = color_shift_range
        self.jitter_range = jitter_range
86
        self.drop_prob = drop_prob
liyinhao's avatar
liyinhao committed
87
88

    def __call__(self, results):
89
        points = results['points']
liyinhao's avatar
liyinhao committed
90
91
        assert points.shape[1] >= 6, \
            f'Expect points have channel >=6, got {points.shape[1]}.'
liyinhao's avatar
liyinhao committed
92
93
94
95
96
97
98
99
100
101
102
103
104
        rgb_color = points[:, 3:6] + self.color_mean
        # brightness change for each channel
        rgb_color *= np.random.uniform(self.bright_range[0],
                                       self.bright_range[1], 3)
        # color shift for each channel
        rgb_color += np.random.uniform(self.color_shift_range[0],
                                       self.color_shift_range[1], 3)
        # jittering on each pixel
        rgb_color += np.expand_dims(
            np.random.uniform(self.jitter_range[0], self.jitter_range[1]), -1)
        rgb_color = np.clip(rgb_color, 0, 1)
        # randomly drop out points' colors
        rgb_color *= np.expand_dims(
105
            np.random.random(points.shape[0]) > self.drop_prob, -1)
liyinhao's avatar
liyinhao committed
106
107
108
109
110
111
112
113
114
115
        points[:, 3:6] = rgb_color - self.color_mean
        results['points'] = points
        return results

    def __repr__(self):
        repr_str = self.__class__.__name__
        repr_str += '(color_mean={})'.format(self.color_mean)
        repr_str += '(bright_range={})'.format(self.bright_range)
        repr_str += '(color_shift_range={})'.format(self.color_shift_range)
        repr_str += '(jitter_range={})'.format(self.jitter_range)
116
        repr_str += '(drop_prob={})'.format(self.drop_prob)
liyinhao's avatar
liyinhao committed
117
118


119
120
121
122
# TODO: merge outdoor indoor transform.
# TODO: try transform noise.
@PIPELINES.register_module()
class IndoorGlobalRotScale(object):
liyinhao's avatar
liyinhao committed
123
    """Indoor global rotate and scale.
124
125

    Augment sunrgbd and scannet data with global rotating and scaling.
126
127

    Args:
zhangwenwei's avatar
zhangwenwei committed
128
        shift_height (bool): Whether to use height.
liyinhao's avatar
liyinhao committed
129
            Default: True.
liyinhao's avatar
liyinhao committed
130
        rot_range (list[float]): Range of rotation.
liyinhao's avatar
liyinhao committed
131
            Default: None.
liyinhao's avatar
liyinhao committed
132
        scale_range (list[float]): Range of scale.
liyinhao's avatar
liyinhao committed
133
            Default: None.
134
135
    """

zhangwenwei's avatar
zhangwenwei committed
136
137
    def __init__(self, shift_height=True, rot_range=None, scale_range=None):
        self.shift_height = shift_height
liyinhao's avatar
liyinhao committed
138
        self.rot_range = np.pi * np.array(rot_range)
liyinhao's avatar
liyinhao committed
139
140
        self.scale_range = scale_range

liyinhao's avatar
liyinhao committed
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
    def _rotz(self, t):
        """Rotate About Z.

        Rotation about the z-axis.

        Args:
            t (float): Angle of rotation.

        Returns:
            rot_mat (ndarray): Matrix of rotation.
        """
        c = np.cos(t)
        s = np.sin(t)
        rot_mat = np.array([[c, -s, 0], [s, c, 0], [0, 0, 1]])
        return rot_mat

liyinhao's avatar
liyinhao committed
157
    def _rotate_aligned_boxes(self, input_boxes, rot_mat):
liyinhao's avatar
liyinhao committed
158
        """Rotate aligned boxes.
liyinhao's avatar
liyinhao committed
159
160
161
162
163
164
165
166
167
168
169

        Rotate function for the aligned boxes.

        Args:
            input_boxes (ndarray): 3D boxes.
            rot_mat (ndarray): Rotation matrix.

        Returns:
            rotated_boxes (ndarry): 3D boxes after rotation.
        """
        centers, lengths = input_boxes[:, 0:3], input_boxes[:, 3:6]
liyinhao's avatar
liyinhao committed
170
        new_centers = np.dot(centers, rot_mat.T)
liyinhao's avatar
liyinhao committed
171
172
173
174
175

        dx, dy = lengths[:, 0] / 2.0, lengths[:, 1] / 2.0
        new_x = np.zeros((dx.shape[0], 4))
        new_y = np.zeros((dx.shape[0], 4))

liyinhao's avatar
liyinhao committed
176
177
178
179
180
181
182
        for i, corner in enumerate([(-1, -1), (1, -1), (1, 1), (-1, 1)]):
            corners = np.zeros((dx.shape[0], 3))
            corners[:, 0] = corner[0] * dx
            corners[:, 1] = corner[1] * dy
            corners = np.dot(corners, rot_mat.T)
            new_x[:, i] = corners[:, 0]
            new_y[:, i] = corners[:, 1]
liyinhao's avatar
liyinhao committed
183
184
185
186
187
188

        new_dx = 2.0 * np.max(new_x, 1)
        new_dy = 2.0 * np.max(new_y, 1)
        new_lengths = np.stack((new_dx, new_dy, lengths[:, 2]), axis=1)

        return np.concatenate([new_centers, new_lengths], axis=1)
189
190

    def __call__(self, results):
191
192
        points = results['points']
        gt_bboxes_3d = results['gt_bboxes_3d']
liyinhao's avatar
liyinhao committed
193
        aligned = True if gt_bboxes_3d.shape[1] == 6 else False
liyinhao's avatar
liyinhao committed
194

liyinhao's avatar
liyinhao committed
195
        if self.rot_range is not None:
liyinhao's avatar
liyinhao committed
196
            assert len(self.rot_range) == 2, \
liyinhao's avatar
liyinhao committed
197
                f'Expect length of rot range =2, ' \
liyinhao's avatar
liyinhao committed
198
                f'got {len(self.rot_range)}.'
liyinhao's avatar
liyinhao committed
199
            rot_angle = np.random.uniform(self.rot_range[0], self.rot_range[1])
liyinhao's avatar
liyinhao committed
200
201
            rot_mat = self._rotz(rot_angle)
            points[:, :3] = np.dot(points[:, :3], rot_mat.T)
liyinhao's avatar
liyinhao committed
202
            if aligned:
liyinhao's avatar
liyinhao committed
203
204
                gt_bboxes_3d = self._rotate_aligned_boxes(
                    gt_bboxes_3d, rot_mat)
liyinhao's avatar
liyinhao committed
205
            else:
liyinhao's avatar
liyinhao committed
206
                gt_bboxes_3d[:, :3] = np.dot(gt_bboxes_3d[:, :3], rot_mat.T)
liyinhao's avatar
liyinhao committed
207
                gt_bboxes_3d[:, 6] -= rot_angle
liyinhao's avatar
liyinhao committed
208
            results['rot_angle'] = rot_angle
liyinhao's avatar
liyinhao committed
209

liyinhao's avatar
liyinhao committed
210
        if self.scale_range is not None:
liyinhao's avatar
liyinhao committed
211
            assert len(self.scale_range) == 2, \
liyinhao's avatar
liyinhao committed
212
                f'Expect length of scale range =2, ' \
liyinhao's avatar
liyinhao committed
213
                f'got {len(self.scale_range)}.'
liyinhao's avatar
liyinhao committed
214
            # Augment point cloud scale
liyinhao's avatar
liyinhao committed
215
216
            scale_ratio = np.random.uniform(self.scale_range[0],
                                            self.scale_range[1])
217

liyinhao's avatar
liyinhao committed
218
219
            points[:, :3] *= scale_ratio
            gt_bboxes_3d[:, :3] *= scale_ratio
liyinhao's avatar
liyinhao committed
220
            gt_bboxes_3d[:, 3:6] *= scale_ratio
zhangwenwei's avatar
zhangwenwei committed
221
            if self.shift_height:
222
                points[:, -1] *= scale_ratio
liyinhao's avatar
liyinhao committed
223

liyinhao's avatar
liyinhao committed
224
225
            results['scale_ratio'] = scale_ratio

liyinhao's avatar
liyinhao committed
226
227
        results['points'] = points
        results['gt_bboxes_3d'] = gt_bboxes_3d
228
229
230
231
        return results

    def __repr__(self):
        repr_str = self.__class__.__name__
zhangwenwei's avatar
zhangwenwei committed
232
        repr_str += '(shift_height={})'.format(self.shift_height)
liyinhao's avatar
liyinhao committed
233
234
        repr_str += '(rot_range={})'.format(self.rot_range)
        repr_str += '(scale_range={})'.format(self.scale_range)
235
        return repr_str