-
Notifications
You must be signed in to change notification settings - Fork 3
/
data_utils.py
317 lines (264 loc) · 11.6 KB
/
data_utils.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
"""
@Author: Yue Wang
@Contact: [email protected]
@File: data.py
@Time: 2018/10/13 6:21 PM
Modified by
@Author: Manxi Lin
@Contact: [email protected]
@Time: 2022/7/7 3:00 PM
"""
import os
import glob
import h5py
import numpy as np
import torch
from torch.utils.data import Dataset
from models.utils import get_dist
def load_data_mdn(partition):
base_dir = os.path.dirname(os.path.abspath(__file__))
data_dir = os.path.join(base_dir, 'data')
all_data = []
all_label = []
for h5_name in glob.glob(os.path.join(data_dir, 'modelnet40_ply_hdf5_2048', '*%s*.h5'%partition)):
f = h5py.File(h5_name, 'r+')
data = f['data'][:].astype('float32')
label = f['label'][:].astype('int64')
f.close()
all_data.append(data)
all_label.append(label)
all_data = np.concatenate(all_data, axis=0)
all_label = np.concatenate(all_label, axis=0)
return all_data, all_label
def load_data_sonn(partition, bg):
base_dir = os.path.dirname(os.path.abspath(__file__))
data_dir = os.path.join(base_dir, 'data')
if bg:
head = 'main_split'
else:
head = 'main_split_nobg'
if partition == 'train':
partition = 'training'
h5_name = os.path.join(data_dir, 'h5_files', head, '%s_objectdataset.h5'%(partition))
f = h5py.File(h5_name, 'r+')
data = f['data'][:].astype('float32')
label = f['label'][:].astype('int64')
return data, label
def load_data_seg(partition):
base_dir = os.path.dirname(os.path.abspath(__file__))
data_dir = os.path.join(base_dir, 'data')
point_dir = os.path.join(data_dir, 'Toronto_3D', partition, '*_point.npy')
label_dir = os.path.join(data_dir, 'Toronto_3D', partition, '*_label.npy')
all_data = glob.glob(point_dir)
all_label = glob.glob(label_dir)
return all_data, all_label
def load_data_partseg(partition):
base_dir = os.path.dirname(os.path.abspath(__file__))
data_dir = os.path.join(base_dir, 'data')
all_data = []
all_label = []
all_seg = []
if partition == 'trainval':
file = glob.glob(os.path.join(data_dir, 'shapenet_part_seg_hdf5_data', '*train*.h5')) \
+ glob.glob(os.path.join(data_dir, 'shapenet_part_seg_hdf5_data', '*val*.h5'))
else:
file = glob.glob(os.path.join(data_dir, 'shapenet_part_seg_hdf5_data', '*%s*.h5'%partition))
for h5_name in file:
f = h5py.File(h5_name, 'r+')
data = f['data'][:].astype('float32')
label = f['label'][:].astype('int64')
seg = f['pid'][:].astype('int64')
f.close()
all_data.append(data)
all_label.append(label)
all_seg.append(seg)
all_data = np.concatenate(all_data, axis=0)
all_label = np.concatenate(all_label, axis=0)
all_seg = np.concatenate(all_seg, axis=0)
return all_data, all_label, all_seg
def translate_pointcloud(pointcloud):
xyz1 = np.random.uniform(low=2./3., high=3./2., size=[3])
xyz2 = np.random.uniform(low=-0.2, high=0.2, size=[3])
translated_pointcloud = np.add(np.multiply(pointcloud, xyz1), xyz2).astype('float32')
return translated_pointcloud
def jitter_pointcloud(pointcloud, sigma=0.01, clip=0.02):
N, C = pointcloud.shape
pointcloud += np.clip(sigma * np.random.randn(N, C), -1*clip, clip)
return pointcloud
def rotate_pointcloud(pointcloud):
theta = np.pi*2 * np.random.uniform()
rotation_matrix = np.array([[np.cos(theta), -np.sin(theta)],[np.sin(theta), np.cos(theta)]])
pointcloud[:,[0,2]] = pointcloud[:,[0,2]].dot(rotation_matrix) # random rotation (x,z)
return pointcloud
class ModelNet40(Dataset):
def __init__(self, num_points, partition='train'):
self.data, self.label = load_data_mdn(partition)
self.num_points = num_points
self.partition = partition
def __getitem__(self, item):
pointcloud = self.data[item][:self.num_points]
label = self.label[item]
if self.partition == 'train':
pointcloud = translate_pointcloud(pointcloud)
np.random.shuffle(pointcloud)
return pointcloud, label
def __len__(self):
return self.data.shape[0]
class ModelNet40Noise(Dataset):
def __init__(self, num_points, num_noise, partition='test'):
assert partition == "test",'Noise study can only be applied during evaluation'
self.data, self.label = load_data_mdn(partition)
self.num_points = num_points
self.partition = partition
self.num_noise = num_noise
assert self.num_noise <= self.num_points,'number of noise points should be less than the point number'
def __getitem__(self, item):
pointcloud = self.data[item][:self.num_points]
label = self.label[item]
pointcloud = pointcloud[:-self.num_noise, :]
noise = np.random.rand(self.num_noise, 3)*2-1
pointcloud = np.concatenate((pointcloud, noise), axis=0).astype('float32')
np.random.shuffle(pointcloud)
return pointcloud, label
def __len__(self):
return self.data.shape[0]
class ModelNet40Resplit(Dataset):
def __init__(self, num_points, partition='train'):
train_data, train_label = load_data_mdn('train')
test_data, test_label = load_data_mdn('test')
self.num_points = num_points
self.partition = partition
all_data = np.concatenate((train_data, test_data), axis=0)
all_label = np.concatenate((train_label, test_label), axis=0)
indices = list(range(all_data.shape[0]))
np.random.shuffle(indices)
if partition == 'train':
self.data = all_data[indices[:8617], ...]
self.label = all_label[indices[:8617], ...]
elif partition == 'test':
self.data = all_data[indices[8617:8617+1847], ...]
self.label = all_label[indices[8617:8617+1847], ...]
elif partition == 'vali':
self.data = all_data[indices[8617+1847:], ...]
self.label = all_label[indices[8617+1847:], ...]
else:
raise NameError
def __getitem__(self, item):
pointcloud = self.data[item][:self.num_points]
label = self.label[item]
if self.partition == 'train':
pointcloud = translate_pointcloud(pointcloud)
np.random.shuffle(pointcloud)
return pointcloud, label
def __len__(self):
return self.data.shape[0]
class ScanObjectNN(Dataset):
def __init__(self, num_points, partition='train', bg=False):
self.data, self.label = load_data_sonn(partition, bg)
self.num_points = num_points
self.partition = partition
def __getitem__(self, item):
pointcloud = self.data[item][:self.num_points]
label = self.label[item]
if self.partition == 'train':
pointcloud = translate_pointcloud(pointcloud)
pointcloud = rotate_pointcloud(pointcloud)
pointcloud = jitter_pointcloud(pointcloud)
np.random.shuffle(pointcloud)
return pointcloud, label
def __len__(self):
return self.data.shape[0]
class ModelNet40C(Dataset):
def __init__(self, corruption, severity):
base_dir = os.path.dirname(os.path.abspath(__file__))
data_dir = os.path.join(base_dir, 'data', 'modelnet40_c')
DATA_DIR = os.path.join(data_dir, 'data_' + corruption + '_' +str(severity) + '.npy')
LABEL_DIR = os.path.join(data_dir, 'label.npy')
self.data = np.load(DATA_DIR)
self.label = np.load(LABEL_DIR)
def __getitem__(self, item):
pointcloud = self.data[item]
label = self.label[item]
label = label
return torch.from_numpy(pointcloud), torch.from_numpy(label).long()
def __len__(self):
return self.data.shape[0]
class Toronto3D(Dataset):
def __init__(self, num_points=2048, partition='train'):
self.data, self.seg = load_data_seg(partition)
self.num_points = num_points
self.partition = partition
def __getitem__(self, item):
pointcloud = np.load(self.data[item])
seg = np.load(self.seg[item])
pointcloud = pointcloud[:self.num_points]
seg = seg[:self.num_points]
# normalize point cloud
pointcloud = pointcloud - np.min(pointcloud, axis=0)
pointcloud /= 5 # normalize point cloud in 5x5 blocks
if self.partition == 'train':
indices = list(range(pointcloud.shape[0]))
pointcloud = translate_pointcloud(pointcloud)
pointcloud = jitter_pointcloud(pointcloud)
pointcloud[:,:3] = rotate_pointcloud(pointcloud[:,:3])
np.random.shuffle(indices)
pointcloud = pointcloud[indices]
seg = seg[indices]
seg = torch.LongTensor(seg)
pointcloud = torch.from_numpy(pointcloud)
return pointcloud, seg
def __len__(self):
return len(self.data)
class ShapeNetPart(Dataset):
def __init__(self, num_points, partition='train'):
self.data, self.label, self.seg = load_data_partseg(partition)
self.seg_num = [4, 2, 2, 4, 4, 3, 3, 2, 4, 2, 6, 2, 3, 3, 3, 3]
self.index_start = [0, 4, 6, 8, 12, 16, 19, 22, 24, 28, 30, 36, 38, 41, 44, 47]
self.num_points = num_points
self.partition = partition
self.seg_num_all = 50
self.seg_start_index = 0
def __getitem__(self, item):
pointcloud = self.data[item][:self.num_points]
label = self.label[item]
seg = self.seg[item][:self.num_points]
if self.partition == 'trainval':
pointcloud = translate_pointcloud(pointcloud)
indices = list(range(pointcloud.shape[0]))
np.random.shuffle(indices)
pointcloud = pointcloud[indices]
seg = seg[indices]
return pointcloud, label, seg
def __len__(self):
return self.data.shape[0]
class ShapeNetPartNoise(Dataset):
def __init__(self, num_points, num_noise, partition='test'):
assert partition == "test",'Noise study can only be applied during evaluation'
self.data, self.label, self.seg = load_data_partseg(partition)
self.seg_num = [4, 2, 2, 4, 4, 3, 3, 2, 4, 2, 6, 2, 3, 3, 3, 3]
self.index_start = [0, 4, 6, 8, 12, 16, 19, 22, 24, 28, 30, 36, 38, 41, 44, 47]
self.seg_num_all = 50
self.seg_start_index = 0
self.num_points = num_points
self.num_noise = num_noise
assert self.num_noise <= self.num_points,'number of noise points should be less than the point number'
def __getitem__(self, item):
pointcloud = self.data[item][:self.num_points]
label = self.label[item]
seg = self.seg[item][:self.num_points]
noise = np.random.rand(self.num_noise, 3)*2-1
dist = get_dist(torch.from_numpy(noise.astype('float32')).cuda().unsqueeze(0),
torch.from_numpy(pointcloud.astype('float32')).cuda().unsqueeze(0))
noise_idx = torch.min(dist, dim=-1)[1].squeeze(0).cpu().numpy()
noise_seg = seg[noise_idx]
pointcloud = pointcloud[:-self.num_noise, :]
seg = seg[:-self.num_noise]
pointcloud = np.concatenate((pointcloud, noise), axis=0).astype('float32')
seg = np.concatenate((seg, noise_seg), axis=0)
indices = list(range(pointcloud.shape[0]))
np.random.shuffle(indices)
pointcloud = pointcloud[indices]
seg = seg[indices]
return pointcloud, label, seg
def __len__(self):
return self.data.shape[0]