provider.py 10 KB
Newer Older
1
"""
esang's avatar
esang committed
2
Adapted from https://github.com/yanx27/Pointnet_Pointnet2_pytorch/blob/master/provider.py
3
"""
esang's avatar
esang committed
4
5
import numpy as np

6

esang's avatar
esang committed
7
def normalize_data(batch_data):
8
9
10
11
12
    """Normalize the batch data, use coordinates of the block centered at origin,
    Input:
        BxNxC array
    Output:
        BxNxC array
esang's avatar
esang committed
13
14
15
16
17
18
19
    """
    B, N, C = batch_data.shape
    normal_data = np.zeros((B, N, C))
    for b in range(B):
        pc = batch_data[b]
        centroid = np.mean(pc, axis=0)
        pc = pc - centroid
20
        m = np.max(np.sqrt(np.sum(pc**2, axis=1)))
esang's avatar
esang committed
21
22
23
24
25
26
        pc = pc / m
        normal_data[b] = pc
    return normal_data


def shuffle_data(data, labels):
27
28
29
30
31
32
    """Shuffle data and labels.
    Input:
      data: B,N,... numpy array
      label: B,... numpy array
    Return:
      shuffled data, label and shuffle indices
esang's avatar
esang committed
33
34
35
36
37
    """
    idx = np.arange(len(labels))
    np.random.shuffle(idx)
    return data[idx, ...], labels[idx], idx

38

esang's avatar
esang committed
39
def shuffle_points(batch_data):
40
41
42
43
44
45
    """Shuffle orders of points in each point cloud -- changes FPS behavior.
    Use the same shuffling idx for the entire batch.
    Input:
        BxNxC array
    Output:
        BxNxC array
esang's avatar
esang committed
46
47
48
    """
    idx = np.arange(batch_data.shape[1])
    np.random.shuffle(idx)
49
50
    return batch_data[:, idx, :]

esang's avatar
esang committed
51
52

def rotate_point_cloud(batch_data):
53
54
55
56
57
58
    """Randomly rotate the point clouds to augument the dataset
    rotation is per shape based along up direction
    Input:
      BxNx3 array, original batch of point clouds
    Return:
      BxNx3 array, rotated batch of point clouds
esang's avatar
esang committed
59
60
61
62
63
64
    """
    rotated_data = np.zeros(batch_data.shape, dtype=np.float32)
    for k in range(batch_data.shape[0]):
        rotation_angle = np.random.uniform() * 2 * np.pi
        cosval = np.cos(rotation_angle)
        sinval = np.sin(rotation_angle)
65
66
67
        rotation_matrix = np.array(
            [[cosval, 0, sinval], [0, 1, 0], [-sinval, 0, cosval]]
        )
esang's avatar
esang committed
68
        shape_pc = batch_data[k, ...]
69
70
71
        rotated_data[k, ...] = np.dot(
            shape_pc.reshape((-1, 3)), rotation_matrix
        )
esang's avatar
esang committed
72
73
    return rotated_data

74

esang's avatar
esang committed
75
def rotate_point_cloud_z(batch_data):
76
77
78
79
80
81
    """Randomly rotate the point clouds to augument the dataset
    rotation is per shape based along up direction
    Input:
      BxNx3 array, original batch of point clouds
    Return:
      BxNx3 array, rotated batch of point clouds
esang's avatar
esang committed
82
83
84
85
86
87
    """
    rotated_data = np.zeros(batch_data.shape, dtype=np.float32)
    for k in range(batch_data.shape[0]):
        rotation_angle = np.random.uniform() * 2 * np.pi
        cosval = np.cos(rotation_angle)
        sinval = np.sin(rotation_angle)
88
89
90
        rotation_matrix = np.array(
            [[cosval, sinval, 0], [-sinval, cosval, 0], [0, 0, 1]]
        )
esang's avatar
esang committed
91
        shape_pc = batch_data[k, ...]
92
93
94
        rotated_data[k, ...] = np.dot(
            shape_pc.reshape((-1, 3)), rotation_matrix
        )
esang's avatar
esang committed
95
96
    return rotated_data

97

esang's avatar
esang committed
98
def rotate_point_cloud_with_normal(batch_xyz_normal):
99
100
101
102
103
104
    """Randomly rotate XYZ, normal point cloud.
    Input:
        batch_xyz_normal: B,N,6, first three channels are XYZ, last 3 all normal
    Output:
        B,N,6, rotated XYZ, normal point cloud
    """
esang's avatar
esang committed
105
106
107
108
    for k in range(batch_xyz_normal.shape[0]):
        rotation_angle = np.random.uniform() * 2 * np.pi
        cosval = np.cos(rotation_angle)
        sinval = np.sin(rotation_angle)
109
110
111
112
113
114
115
116
117
118
119
        rotation_matrix = np.array(
            [[cosval, 0, sinval], [0, 1, 0], [-sinval, 0, cosval]]
        )
        shape_pc = batch_xyz_normal[k, :, 0:3]
        shape_normal = batch_xyz_normal[k, :, 3:6]
        batch_xyz_normal[k, :, 0:3] = np.dot(
            shape_pc.reshape((-1, 3)), rotation_matrix
        )
        batch_xyz_normal[k, :, 3:6] = np.dot(
            shape_normal.reshape((-1, 3)), rotation_matrix
        )
esang's avatar
esang committed
120
121
    return batch_xyz_normal

122
123
124
125
126
127
128
129
130

def rotate_perturbation_point_cloud_with_normal(
    batch_data, angle_sigma=0.06, angle_clip=0.18
):
    """Randomly perturb the point clouds by small rotations
    Input:
      BxNx6 array, original batch of point clouds and point normals
    Return:
      BxNx3 array, rotated batch of point clouds
esang's avatar
esang committed
131
132
133
    """
    rotated_data = np.zeros(batch_data.shape, dtype=np.float32)
    for k in range(batch_data.shape[0]):
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
        angles = np.clip(
            angle_sigma * np.random.randn(3), -angle_clip, angle_clip
        )
        Rx = np.array(
            [
                [1, 0, 0],
                [0, np.cos(angles[0]), -np.sin(angles[0])],
                [0, np.sin(angles[0]), np.cos(angles[0])],
            ]
        )
        Ry = np.array(
            [
                [np.cos(angles[1]), 0, np.sin(angles[1])],
                [0, 1, 0],
                [-np.sin(angles[1]), 0, np.cos(angles[1])],
            ]
        )
        Rz = np.array(
            [
                [np.cos(angles[2]), -np.sin(angles[2]), 0],
                [np.sin(angles[2]), np.cos(angles[2]), 0],
                [0, 0, 1],
            ]
        )
        R = np.dot(Rz, np.dot(Ry, Rx))
        shape_pc = batch_data[k, :, 0:3]
        shape_normal = batch_data[k, :, 3:6]
        rotated_data[k, :, 0:3] = np.dot(shape_pc.reshape((-1, 3)), R)
        rotated_data[k, :, 3:6] = np.dot(shape_normal.reshape((-1, 3)), R)
esang's avatar
esang committed
163
164
165
166
    return rotated_data


def rotate_point_cloud_by_angle(batch_data, rotation_angle):
167
168
169
170
171
    """Rotate the point cloud along up direction with certain angle.
    Input:
      BxNx3 array, original batch of point clouds
    Return:
      BxNx3 array, rotated batch of point clouds
esang's avatar
esang committed
172
173
174
    """
    rotated_data = np.zeros(batch_data.shape, dtype=np.float32)
    for k in range(batch_data.shape[0]):
175
        # rotation_angle = np.random.uniform() * 2 * np.pi
esang's avatar
esang committed
176
177
        cosval = np.cos(rotation_angle)
        sinval = np.sin(rotation_angle)
178
179
180
181
182
183
184
        rotation_matrix = np.array(
            [[cosval, 0, sinval], [0, 1, 0], [-sinval, 0, cosval]]
        )
        shape_pc = batch_data[k, :, 0:3]
        rotated_data[k, :, 0:3] = np.dot(
            shape_pc.reshape((-1, 3)), rotation_matrix
        )
esang's avatar
esang committed
185
186
    return rotated_data

187

esang's avatar
esang committed
188
def rotate_point_cloud_by_angle_with_normal(batch_data, rotation_angle):
189
190
191
192
193
194
    """Rotate the point cloud along up direction with certain angle.
    Input:
      BxNx6 array, original batch of point clouds with normal
      scalar, angle of rotation
    Return:
      BxNx6 array, rotated batch of point clouds iwth normal
esang's avatar
esang committed
195
196
197
    """
    rotated_data = np.zeros(batch_data.shape, dtype=np.float32)
    for k in range(batch_data.shape[0]):
198
        # rotation_angle = np.random.uniform() * 2 * np.pi
esang's avatar
esang committed
199
200
        cosval = np.cos(rotation_angle)
        sinval = np.sin(rotation_angle)
201
202
203
204
205
206
207
208
209
210
211
        rotation_matrix = np.array(
            [[cosval, 0, sinval], [0, 1, 0], [-sinval, 0, cosval]]
        )
        shape_pc = batch_data[k, :, 0:3]
        shape_normal = batch_data[k, :, 3:6]
        rotated_data[k, :, 0:3] = np.dot(
            shape_pc.reshape((-1, 3)), rotation_matrix
        )
        rotated_data[k, :, 3:6] = np.dot(
            shape_normal.reshape((-1, 3)), rotation_matrix
        )
esang's avatar
esang committed
212
213
214
    return rotated_data


215
216
217
218
219
220
221
222
def rotate_perturbation_point_cloud(
    batch_data, angle_sigma=0.06, angle_clip=0.18
):
    """Randomly perturb the point clouds by small rotations
    Input:
      BxNx3 array, original batch of point clouds
    Return:
      BxNx3 array, rotated batch of point clouds
esang's avatar
esang committed
223
224
225
    """
    rotated_data = np.zeros(batch_data.shape, dtype=np.float32)
    for k in range(batch_data.shape[0]):
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
        angles = np.clip(
            angle_sigma * np.random.randn(3), -angle_clip, angle_clip
        )
        Rx = np.array(
            [
                [1, 0, 0],
                [0, np.cos(angles[0]), -np.sin(angles[0])],
                [0, np.sin(angles[0]), np.cos(angles[0])],
            ]
        )
        Ry = np.array(
            [
                [np.cos(angles[1]), 0, np.sin(angles[1])],
                [0, 1, 0],
                [-np.sin(angles[1]), 0, np.cos(angles[1])],
            ]
        )
        Rz = np.array(
            [
                [np.cos(angles[2]), -np.sin(angles[2]), 0],
                [np.sin(angles[2]), np.cos(angles[2]), 0],
                [0, 0, 1],
            ]
        )
        R = np.dot(Rz, np.dot(Ry, Rx))
esang's avatar
esang committed
251
252
253
254
255
256
        shape_pc = batch_data[k, ...]
        rotated_data[k, ...] = np.dot(shape_pc.reshape((-1, 3)), R)
    return rotated_data


def jitter_point_cloud(batch_data, sigma=0.01, clip=0.05):
257
258
259
260
261
    """Randomly jitter points. jittering is per point.
    Input:
      BxNx3 array, original batch of point clouds
    Return:
      BxNx3 array, jittered batch of point clouds
esang's avatar
esang committed
262
263
    """
    B, N, C = batch_data.shape
264
265
    assert clip > 0
    jittered_data = np.clip(sigma * np.random.randn(B, N, C), -1 * clip, clip)
esang's avatar
esang committed
266
267
268
    jittered_data += batch_data
    return jittered_data

269

esang's avatar
esang committed
270
def shift_point_cloud(batch_data, shift_range=0.1):
271
272
273
274
275
    """Randomly shift point cloud. Shift is per point cloud.
    Input:
      BxNx3 array, original batch of point clouds
    Return:
      BxNx3 array, shifted batch of point clouds
esang's avatar
esang committed
276
277
    """
    B, N, C = batch_data.shape
278
    shifts = np.random.uniform(-shift_range, shift_range, (B, 3))
esang's avatar
esang committed
279
    for batch_index in range(B):
280
        batch_data[batch_index, :, :] += shifts[batch_index, :]
esang's avatar
esang committed
281
282
283
284
    return batch_data


def random_scale_point_cloud(batch_data, scale_low=0.8, scale_high=1.25):
285
286
287
288
289
    """Randomly scale the point cloud. Scale is per point cloud.
    Input:
        BxNx3 array, original batch of point clouds
    Return:
        BxNx3 array, scaled batch of point clouds
esang's avatar
esang committed
290
291
292
293
    """
    B, N, C = batch_data.shape
    scales = np.random.uniform(scale_low, scale_high, B)
    for batch_index in range(B):
294
        batch_data[batch_index, :, :] *= scales[batch_index]
esang's avatar
esang committed
295
296
    return batch_data

297

esang's avatar
esang committed
298
def random_point_dropout(batch_pc, max_dropout_ratio=0.875):
299
    """batch_pc: BxNx3"""
esang's avatar
esang committed
300
    for b in range(batch_pc.shape[0]):
301
302
303
304
305
306
307
308
309
310
311
        dropout_ratio = np.random.random() * max_dropout_ratio  # 0~0.875
        drop_idx = np.where(
            np.random.random((batch_pc.shape[1])) <= dropout_ratio
        )[0]
        if len(drop_idx) > 0:
            dropout_ratio = (
                np.random.random() * max_dropout_ratio
            )  # 0~0.875 # not need
            batch_pc[b, drop_idx, :] = batch_pc[
                b, 0, :
            ]  # set to the first point
esang's avatar
esang committed
312
    return batch_pc