-
Notifications
You must be signed in to change notification settings - Fork 100
/
datasets.py
389 lines (338 loc) · 16.3 KB
/
datasets.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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
import os
import torch
import numpy as np
from torch.utils.data import Dataset
from torch.utils import data
import random
# taken from https://github.com/optas/latent_3d_points/blob/8e8f29f8124ed5fc59439e8551ba7ef7567c9a37/src/in_out.py
synsetid_to_cate = {
'02691156': 'airplane', '02773838': 'bag', '02801938': 'basket',
'02808440': 'bathtub', '02818832': 'bed', '02828884': 'bench',
'02876657': 'bottle', '02880940': 'bowl', '02924116': 'bus',
'02933112': 'cabinet', '02747177': 'can', '02942699': 'camera',
'02954340': 'cap', '02958343': 'car', '03001627': 'chair',
'03046257': 'clock', '03207941': 'dishwasher', '03211117': 'monitor',
'04379243': 'table', '04401088': 'telephone', '02946921': 'tin_can',
'04460130': 'tower', '04468005': 'train', '03085013': 'keyboard',
'03261776': 'earphone', '03325088': 'faucet', '03337140': 'file',
'03467517': 'guitar', '03513137': 'helmet', '03593526': 'jar',
'03624134': 'knife', '03636649': 'lamp', '03642806': 'laptop',
'03691459': 'speaker', '03710193': 'mailbox', '03759954': 'microphone',
'03761084': 'microwave', '03790512': 'motorcycle', '03797390': 'mug',
'03928116': 'piano', '03938244': 'pillow', '03948459': 'pistol',
'03991062': 'pot', '04004475': 'printer', '04074963': 'remote_control',
'04090263': 'rifle', '04099429': 'rocket', '04225987': 'skateboard',
'04256520': 'sofa', '04330267': 'stove', '04530566': 'vessel',
'04554684': 'washer', '02992529': 'cellphone',
'02843684': 'birdhouse', '02871439': 'bookshelf',
# '02858304': 'boat', no boat in our dataset, merged into vessels
# '02834778': 'bicycle', not in our taxonomy
}
cate_to_synsetid = {v: k for k, v in synsetid_to_cate.items()}
class Uniform15KPC(Dataset):
def __init__(self, root_dir, subdirs, tr_sample_size=10000,
te_sample_size=10000, split='train', scale=1.,
normalize_per_shape=False, random_subsample=False,
normalize_std_per_axis=False,
all_points_mean=None, all_points_std=None,
input_dim=3):
self.root_dir = root_dir
self.split = split
self.in_tr_sample_size = tr_sample_size
self.in_te_sample_size = te_sample_size
self.subdirs = subdirs
self.scale = scale
self.random_subsample = random_subsample
self.input_dim = input_dim
self.all_cate_mids = []
self.cate_idx_lst = []
self.all_points = []
for cate_idx, subd in enumerate(self.subdirs):
# NOTE: [subd] here is synset id
sub_path = os.path.join(root_dir, subd, self.split)
if not os.path.isdir(sub_path):
print("Directory missing : %s" % sub_path)
continue
all_mids = []
for x in os.listdir(sub_path):
if not x.endswith('.npy'):
continue
all_mids.append(os.path.join(self.split, x[:-len('.npy')]))
# NOTE: [mid] contains the split: i.e. "train/<mid>" or "val/<mid>" or "test/<mid>"
for mid in all_mids:
# obj_fname = os.path.join(sub_path, x)
obj_fname = os.path.join(root_dir, subd, mid + ".npy")
try:
point_cloud = np.load(obj_fname) # (15k, 3)
except:
continue
assert point_cloud.shape[0] == 15000
self.all_points.append(point_cloud[np.newaxis, ...])
self.cate_idx_lst.append(cate_idx)
self.all_cate_mids.append((subd, mid))
# Shuffle the index deterministically (based on the number of examples)
self.shuffle_idx = list(range(len(self.all_points)))
random.Random(38383).shuffle(self.shuffle_idx)
self.cate_idx_lst = [self.cate_idx_lst[i] for i in self.shuffle_idx]
self.all_points = [self.all_points[i] for i in self.shuffle_idx]
self.all_cate_mids = [self.all_cate_mids[i] for i in self.shuffle_idx]
# Normalization
self.all_points = np.concatenate(self.all_points) # (N, 15000, 3)
self.normalize_per_shape = normalize_per_shape
self.normalize_std_per_axis = normalize_std_per_axis
if all_points_mean is not None and all_points_std is not None: # using loaded dataset stats
self.all_points_mean = all_points_mean
self.all_points_std = all_points_std
elif self.normalize_per_shape: # per shape normalization
B, N = self.all_points.shape[:2]
self.all_points_mean = self.all_points.mean(axis=1).reshape(B, 1, input_dim)
if normalize_std_per_axis:
self.all_points_std = self.all_points.reshape(B, N, -1).std(axis=1).reshape(B, 1, input_dim)
else:
self.all_points_std = self.all_points.reshape(B, -1).std(axis=1).reshape(B, 1, 1)
else: # normalize across the dataset
self.all_points_mean = self.all_points.reshape(-1, input_dim).mean(axis=0).reshape(1, 1, input_dim)
if normalize_std_per_axis:
self.all_points_std = self.all_points.reshape(-1, input_dim).std(axis=0).reshape(1, 1, input_dim)
else:
self.all_points_std = self.all_points.reshape(-1).std(axis=0).reshape(1, 1, 1)
self.all_points = (self.all_points - self.all_points_mean) / self.all_points_std
self.train_points = self.all_points[:, :10000]
self.test_points = self.all_points[:, 10000:]
self.tr_sample_size = min(10000, tr_sample_size)
self.te_sample_size = min(5000, te_sample_size)
print("Total number of data:%d" % len(self.train_points))
print("Min number of points: (train)%d (test)%d"
% (self.tr_sample_size, self.te_sample_size))
assert self.scale == 1, "Scale (!= 1) is deprecated"
def get_pc_stats(self, idx):
if self.normalize_per_shape:
m = self.all_points_mean[idx].reshape(1, self.input_dim)
s = self.all_points_std[idx].reshape(1, -1)
return m, s
return self.all_points_mean.reshape(1, -1), self.all_points_std.reshape(1, -1)
def renormalize(self, mean, std):
self.all_points = self.all_points * self.all_points_std + self.all_points_mean
self.all_points_mean = mean
self.all_points_std = std
self.all_points = (self.all_points - self.all_points_mean) / self.all_points_std
self.train_points = self.all_points[:, :10000]
self.test_points = self.all_points[:, 10000:]
def __len__(self):
return len(self.train_points)
def __getitem__(self, idx):
tr_out = self.train_points[idx]
if self.random_subsample:
tr_idxs = np.random.choice(tr_out.shape[0], self.tr_sample_size)
else:
tr_idxs = np.arange(self.tr_sample_size)
tr_out = torch.from_numpy(tr_out[tr_idxs, :]).float()
te_out = self.test_points[idx]
if self.random_subsample:
te_idxs = np.random.choice(te_out.shape[0], self.te_sample_size)
else:
te_idxs = np.arange(self.te_sample_size)
te_out = torch.from_numpy(te_out[te_idxs, :]).float()
m, s = self.get_pc_stats(idx)
cate_idx = self.cate_idx_lst[idx]
sid, mid = self.all_cate_mids[idx]
return {
'idx': idx,
'train_points': tr_out,
'test_points': te_out,
'mean': m, 'std': s, 'cate_idx': cate_idx,
'sid': sid, 'mid': mid
}
class ModelNet40PointClouds(Uniform15KPC):
def __init__(self, root_dir="data/ModelNet40.PC15k",
tr_sample_size=10000, te_sample_size=2048,
split='train', scale=1., normalize_per_shape=False,
normalize_std_per_axis=False,
random_subsample=False,
all_points_mean=None, all_points_std=None):
self.root_dir = root_dir
self.split = split
assert self.split in ['train', 'test']
self.sample_size = tr_sample_size
self.cates = []
for cate in os.listdir(root_dir):
if os.path.isdir(os.path.join(root_dir, cate)) \
and os.path.isdir(os.path.join(root_dir, cate, 'train')) \
and os.path.isdir(os.path.join(root_dir, cate, 'test')):
self.cates.append(cate)
assert len(self.cates) == 40, "%s %s" % (len(self.cates), self.cates)
# For non-aligned MN
# self.gravity_axis = 0
# self.display_axis_order = [0,1,2]
# Aligned MN has same axis-order as SN
self.gravity_axis = 1
self.display_axis_order = [0, 2, 1]
super(ModelNet40PointClouds, self).__init__(
root_dir, self.cates, tr_sample_size=tr_sample_size,
te_sample_size=te_sample_size, split=split, scale=scale,
normalize_per_shape=normalize_per_shape,
normalize_std_per_axis=normalize_std_per_axis,
random_subsample=random_subsample,
all_points_mean=all_points_mean, all_points_std=all_points_std,
input_dim=3)
class ModelNet10PointClouds(Uniform15KPC):
def __init__(self, root_dir="data/ModelNet10.PC15k",
tr_sample_size=10000, te_sample_size=2048,
split='train', scale=1., normalize_per_shape=False,
normalize_std_per_axis=False,
random_subsample=False,
all_points_mean=None, all_points_std=None):
self.root_dir = root_dir
self.split = split
assert self.split in ['train', 'test']
self.cates = []
for cate in os.listdir(root_dir):
if os.path.isdir(os.path.join(root_dir, cate)) \
and os.path.isdir(os.path.join(root_dir, cate, 'train')) \
and os.path.isdir(os.path.join(root_dir, cate, 'test')):
self.cates.append(cate)
assert len(self.cates) == 10
# That's prealigned MN
# self.gravity_axis = 0
# self.display_axis_order = [0,1,2]
# Aligned MN has same axis-order as SN
self.gravity_axis = 1
self.display_axis_order = [0, 2, 1]
super(ModelNet10PointClouds, self).__init__(
root_dir, self.cates, tr_sample_size=tr_sample_size,
te_sample_size=te_sample_size, split=split, scale=scale,
normalize_per_shape=normalize_per_shape,
normalize_std_per_axis=normalize_std_per_axis,
random_subsample=random_subsample,
all_points_mean=all_points_mean, all_points_std=all_points_std,
input_dim=3)
class ShapeNet15kPointClouds(Uniform15KPC):
def __init__(self, root_dir="data/ShapeNetCore.v2.PC15k",
categories=['airplane'], tr_sample_size=10000, te_sample_size=2048,
split='train', scale=1., normalize_per_shape=False,
normalize_std_per_axis=False,
random_subsample=False,
all_points_mean=None, all_points_std=None):
self.root_dir = root_dir
self.split = split
assert self.split in ['train', 'test', 'val']
self.tr_sample_size = tr_sample_size
self.te_sample_size = te_sample_size
self.cates = categories
if 'all' in categories:
self.synset_ids = list(cate_to_synsetid.values())
else:
self.synset_ids = [cate_to_synsetid[c] for c in self.cates]
# assert 'v2' in root_dir, "Only supporting v2 right now."
self.gravity_axis = 1
self.display_axis_order = [0, 2, 1]
super(ShapeNet15kPointClouds, self).__init__(
root_dir, self.synset_ids,
tr_sample_size=tr_sample_size,
te_sample_size=te_sample_size,
split=split, scale=scale,
normalize_per_shape=normalize_per_shape,
normalize_std_per_axis=normalize_std_per_axis,
random_subsample=random_subsample,
all_points_mean=all_points_mean, all_points_std=all_points_std,
input_dim=3)
def init_np_seed(worker_id):
seed = torch.initial_seed()
np.random.seed(seed % 4294967296)
def _get_MN40_datasets_(args, data_dir=None):
tr_dataset = ModelNet40PointClouds(
split='train',
tr_sample_size=args.tr_max_sample_points,
te_sample_size=args.te_max_sample_points,
root_dir=(args.data_dir if data_dir is None else data_dir),
normalize_per_shape=args.normalize_per_shape,
normalize_std_per_axis=args.normalize_std_per_axis,
random_subsample=True)
te_dataset = ModelNet40PointClouds(
split='test',
tr_sample_size=args.tr_max_sample_points,
te_sample_size=args.te_max_sample_points,
root_dir=(args.data_dir if data_dir is None else data_dir),
normalize_per_shape=args.normalize_per_shape,
normalize_std_per_axis=args.normalize_std_per_axis,
all_points_mean=tr_dataset.all_points_mean,
all_points_std=tr_dataset.all_points_std,
)
return tr_dataset, te_dataset
def _get_MN10_datasets_(args, data_dir=None):
tr_dataset = ModelNet10PointClouds(
split='train',
tr_sample_size=args.tr_max_sample_points,
te_sample_size=args.te_max_sample_points,
root_dir=(args.data_dir if data_dir is None else data_dir),
normalize_per_shape=args.normalize_per_shape,
normalize_std_per_axis=args.normalize_std_per_axis,
random_subsample=True)
te_dataset = ModelNet10PointClouds(
split='test',
tr_sample_size=args.tr_max_sample_points,
te_sample_size=args.te_max_sample_points,
root_dir=(args.data_dir if data_dir is None else data_dir),
normalize_per_shape=args.normalize_per_shape,
normalize_std_per_axis=args.normalize_std_per_axis,
all_points_mean=tr_dataset.all_points_mean,
all_points_std=tr_dataset.all_points_std,
)
return tr_dataset, te_dataset
def get_datasets(args):
if args.dataset_type == 'shapenet15k':
tr_dataset = ShapeNet15kPointClouds(
categories=args.cates, split='train',
tr_sample_size=args.tr_max_sample_points,
te_sample_size=args.te_max_sample_points,
scale=args.dataset_scale, root_dir=args.data_dir,
normalize_per_shape=args.normalize_per_shape,
normalize_std_per_axis=args.normalize_std_per_axis,
random_subsample=True)
te_dataset = ShapeNet15kPointClouds(
categories=args.cates, split='val',
tr_sample_size=args.tr_max_sample_points,
te_sample_size=args.te_max_sample_points,
scale=args.dataset_scale, root_dir=args.data_dir,
normalize_per_shape=args.normalize_per_shape,
normalize_std_per_axis=args.normalize_std_per_axis,
all_points_mean=tr_dataset.all_points_mean,
all_points_std=tr_dataset.all_points_std,
)
elif args.dataset_type == 'modelnet40_15k':
tr_dataset, te_dataset = _get_MN40_datasets_(args)
elif args.dataset_type == 'modelnet10_15k':
tr_dataset, te_dataset = _get_MN10_datasets_(args)
else:
raise Exception("Invalid dataset type:%s" % args.dataset_type)
return tr_dataset, te_dataset
def get_clf_datasets(args):
return {
'MN40': _get_MN40_datasets_(args, data_dir=args.mn40_data_dir),
'MN10': _get_MN10_datasets_(args, data_dir=args.mn10_data_dir),
}
def get_data_loaders(args):
tr_dataset, te_dataset = get_datasets(args)
train_loader = data.DataLoader(
dataset=tr_dataset, batch_size=args.batch_size,
shuffle=True, num_workers=args.num_workers, drop_last=True,
worker_init_fn=init_np_seed)
train_unshuffle_loader = data.DataLoader(
dataset=tr_dataset, batch_size=args.batch_size,
shuffle=False, num_workers=args.num_workers, drop_last=True,
worker_init_fn=init_np_seed)
test_loader = data.DataLoader(
dataset=te_dataset, batch_size=args.batch_size,
shuffle=False, num_workers=args.num_workers, drop_last=False,
worker_init_fn=init_np_seed)
loaders = {
"test_loader": test_loader,
'train_loader': train_loader,
'train_unshuffle_loader': train_unshuffle_loader,
}
return loaders
if __name__ == "__main__":
shape_ds = ShapeNet15kPointClouds(categories=['airplane'], split='val')
x_tr, x_te = next(iter(shape_ds))
print(x_tr.shape)
print(x_te.shape)