indoor_augment.py 7.42 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

10
11
12
    Flip point_cloud and groundtruth boxes.

    Args:
liyinhao's avatar
liyinhao committed
13
14
        flip_ratio (float): Probability of being flipped.
            Default: 0.5.
15
16
    """

liyinhao's avatar
liyinhao committed
17
18
    def __init__(self, flip_ratio=0.5):
        self.flip_ratio = flip_ratio
19
20

    def __call__(self, results):
21
22
        points = results['points']
        gt_bboxes_3d = results['gt_bboxes_3d']
liyinhao's avatar
liyinhao committed
23
        name = 'scannet' if gt_bboxes_3d.shape[1] == 6 else 'sunrgbd'
liyinhao's avatar
liyinhao committed
24
        if np.random.random() > self.flip_ratio:
25
            # Flipping along the YZ plane
liyinhao's avatar
liyinhao committed
26
27
            points[:, 0] = -1 * points[:, 0]
            gt_bboxes_3d[:, 0] = -1 * gt_bboxes_3d[:, 0]
28
            if name == 'sunrgbd':
liyinhao's avatar
liyinhao committed
29
30
                gt_bboxes_3d[:, 6] = np.pi - gt_bboxes_3d[:, 6]
            results['gt_boxes'] = gt_bboxes_3d
31
32
33

        if name == 'scannet' and np.random.random() > 0.5:
            # Flipping along the XZ plane
liyinhao's avatar
liyinhao committed
34
35
36
37
            points[:, 1] = -1 * points[:, 1]
            gt_bboxes_3d[:, 1] = -1 * gt_bboxes_3d[:, 1]
            results['gt_bboxes_3d'] = gt_bboxes_3d
        results['points'] = points
38
39
40
41
42

        return results

    def __repr__(self):
        repr_str = self.__class__.__name__
liyinhao's avatar
liyinhao committed
43
        repr_str += '(flip_ratio={})'.format(self.flip_ratio)
44
45
46
        return repr_str


liyinhao's avatar
liyinhao committed
47
@PIPELINES.register_module()
liyinhao's avatar
liyinhao committed
48
49
class IndoorPointsColorJitter(object):
    """Indoor Points Color Jitter.
liyinhao's avatar
liyinhao committed
50
51
52
53
54
55
56
57
58
59
60
61

    Augment the color of points.

    Args:
        color_mean (List[float]): Mean color of the point cloud.
            Default: [0.5, 0.5, 0.5].
        bright_range (List[float]): Range of brightness.
            Default: [0.8, 1.2].
        color_shift_range (List[float]): Range of color shift.
            Default: [0.95, 1.05].
        jitter_range (List[float]): Range of jittering.
            Default: [-0.025, 0.025].
62
        drop_prob (float): Probability to drop out points' color.
liyinhao's avatar
liyinhao committed
63
64
65
66
67
68
69
70
            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],
71
                 drop_prob=0.3):
liyinhao's avatar
liyinhao committed
72
73
74
75
        self.color_mean = color_mean
        self.bright_range = bright_range
        self.color_shift_range = color_shift_range
        self.jitter_range = jitter_range
76
        self.drop_prob = drop_prob
liyinhao's avatar
liyinhao committed
77
78

    def __call__(self, results):
79
        points = results['points']
liyinhao's avatar
liyinhao committed
80
81
82
83
84
85
86
87
88
89
90
91
92
93
        assert points.shape[1] >= 6
        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(
94
            np.random.random(points.shape[0]) > self.drop_prob, -1)
liyinhao's avatar
liyinhao committed
95
96
97
98
99
100
101
102
103
104
        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)
105
        repr_str += '(drop_prob={})'.format(self.drop_prob)
liyinhao's avatar
liyinhao committed
106
107


108
109
110
111
112
113
114
# TODO: merge outdoor indoor transform.
# TODO: try transform noise.
@PIPELINES.register_module()
class IndoorGlobalRotScale(object):
    """Indoor Global Rotate Scale.

    Augment sunrgbd and scannet data with global rotating and scaling.
115
116

    Args:
liyinhao's avatar
liyinhao committed
117
        use_height (bool): Whether to use height.
liyinhao's avatar
liyinhao committed
118
119
120
121
122
            Default: True.
        rot_range (List[float]): Range of rotation.
            Default: None.
        scale_range (List[float]): Range of scale.
            Default: None.
123
124
    """

liyinhao's avatar
liyinhao committed
125
    def __init__(self, use_height=True, rot_range=None, scale_range=None):
liyinhao's avatar
liyinhao committed
126
127
128
129
        self.use_height = use_height
        self.rot_range = rot_range
        self.scale_range = scale_range

liyinhao's avatar
liyinhao committed
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
    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
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
    def _rotate_aligned_boxes(self, input_boxes, rot_mat):
        """Rotate Aligned Boxes.

        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]
        new_centers = np.dot(centers, np.transpose(rot_mat))

        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))

        for i, crnr in enumerate([(-1, -1), (1, -1), (1, 1), (-1, 1)]):
            crnrs = np.zeros((dx.shape[0], 3))
            crnrs[:, 0] = crnr[0] * dx
            crnrs[:, 1] = crnr[1] * dy
            crnrs = np.dot(crnrs, np.transpose(rot_mat))
            new_x[:, i] = crnrs[:, 0]
            new_y[:, i] = crnrs[:, 1]

        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)
178
179

    def __call__(self, results):
180
181
        points = results['points']
        gt_bboxes_3d = results['gt_bboxes_3d']
liyinhao's avatar
liyinhao committed
182
        name = 'scannet' if gt_bboxes_3d.shape[1] == 6 else 'sunrgbd'
liyinhao's avatar
liyinhao committed
183

liyinhao's avatar
liyinhao committed
184
        if self.rot_range is not None:
185
            assert len(self.rot_range) == 2
liyinhao's avatar
liyinhao committed
186
            rot_angle = np.random.uniform(self.rot_range[0], self.rot_range[1])
liyinhao's avatar
liyinhao committed
187
188
            rot_mat = self._rotz(rot_angle)
            points[:, :3] = np.dot(points[:, :3], rot_mat.T)
liyinhao's avatar
liyinhao committed
189
            if name == 'scannet':
liyinhao's avatar
liyinhao committed
190
191
                gt_bboxes_3d = self._rotate_aligned_boxes(
                    gt_bboxes_3d, rot_mat)
liyinhao's avatar
liyinhao committed
192
            else:
liyinhao's avatar
liyinhao committed
193
                gt_bboxes_3d[:, :3] = np.dot(gt_bboxes_3d[:, :3], rot_mat.T)
liyinhao's avatar
liyinhao committed
194
                gt_bboxes_3d[:, 6] -= rot_angle
liyinhao's avatar
liyinhao committed
195

liyinhao's avatar
liyinhao committed
196
        if self.scale_range is not None:
197
            assert len(self.scale_range) == 2
liyinhao's avatar
liyinhao committed
198
            # Augment point cloud scale
liyinhao's avatar
liyinhao committed
199
200
            scale_ratio = np.random.uniform(self.scale_range[0],
                                            self.scale_range[1])
201

liyinhao's avatar
liyinhao committed
202
203
            points[:, :3] *= scale_ratio
            gt_bboxes_3d[:, :3] *= scale_ratio
liyinhao's avatar
liyinhao committed
204
            gt_bboxes_3d[:, 3:6] *= scale_ratio
liyinhao's avatar
liyinhao committed
205
            if self.use_height:
206
                points[:, -1] *= scale_ratio
liyinhao's avatar
liyinhao committed
207

liyinhao's avatar
liyinhao committed
208
209
        results['points'] = points
        results['gt_bboxes_3d'] = gt_bboxes_3d
210
211
212
213
        return results

    def __repr__(self):
        repr_str = self.__class__.__name__
liyinhao's avatar
liyinhao committed
214
        repr_str += '(use_height={})'.format(self.use_height)
liyinhao's avatar
liyinhao committed
215
216
        repr_str += '(rot_range={})'.format(self.rot_range)
        repr_str += '(scale_range={})'.format(self.scale_range)
217
        return repr_str