-
Notifications
You must be signed in to change notification settings - Fork 8
/
datasets_4point.py
381 lines (331 loc) · 16.3 KB
/
datasets_4point.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
# encoding=utf-8
import torch.utils.data as data
import os
import torch
import numpy as np
import random
import os.path
import json
import sys
import re
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
ROOT_DIR = BASE_DIR
sys.path.append(os.path.join(ROOT_DIR, 'utils'))
import provider
from copy import copy
import h5py
from tqdm.auto import tqdm
from torch.utils.data import Dataset,DataLoader
class PartDataset(data.Dataset):
def __init__(self, root, batch_size, npoints1 = 256,npoints2 = 512,npoints3 = 1024,npoints4 = 2048, classification=False, class_choice=None, train=True):
self.npoints1 = npoints1
self.npoints2 = npoints2
self.npoints3 = npoints3
self.npoints4 = npoints4
self.root = root
self.batch_size = batch_size
self.catfile = os.path.join(self.root, 'synsetoffset2category.txt')
self.cat = {}
self.classification = classification
with open(self.catfile, 'r') as f:
for line in f:
ls = line.strip().split()
self.cat[ls[0]] = ls[1]
#print(self.cat)
if not class_choice is None:
self.cat = {k:v for k,v in self.cat.items() if k in class_choice}
self.meta = {}
for item in self.cat:
#print('category', item)
self.meta[item] = []
dir_point = os.path.join(self.root, self.cat[item], 'points')
dir_seg = os.path.join(self.root, self.cat[item], 'points_label')
#print(dir_point, dir_seg)
fns = sorted(os.listdir(dir_point))
if train:
fns = fns[:int(len(fns) * 0.9)]
else:
fns = fns[int(len(fns) * 0.9):]
#print(os.path.basename(fns))
for fn in fns:
token = (os.path.splitext(os.path.basename(fn))[0])
self.meta[item].append((os.path.join(dir_point, token + '.pts'), os.path.join(dir_seg, token + '.seg')))
self.datapath = []
for item in self.cat:
for fn in self.meta[item]:
self.datapath.append((item, fn[0], fn[1]))
self.classes = dict(zip(sorted(self.cat), range(len(self.cat))))
print(self.classes)
self.num_seg_classes = 0
if not self.classification:
for i in range(len(self.datapath) // self.batch_size):
l = len(np.unique(np.loadtxt(self.datapath[i][-1]).astype(np.uint8)))
if l > self.num_seg_classes:
self.num_seg_classes = l
#print(self.num_seg_classes)
def __getitem__(self, index):
fn = self.datapath[index]
cls = self.classes[self.datapath[index][0]]
point_set = np.loadtxt(fn[1]).astype(np.float32)
seg = np.loadtxt(fn[2]).astype(np.int64)
#print(point_set.shape, seg.shape)
if len(seg)>self.npoints4:
choice1 = np.random.choice(len(seg), self.npoints1, replace=False)
choice2 = np.random.choice(len(seg), self.npoints2, replace=False)
choice3 = np.random.choice(len(seg), self.npoints3, replace=False)
choice4 = np.random.choice(len(seg), self.npoints4, replace=False)
else:
choice1 = np.random.choice(len(seg), self.npoints1, replace=True)
choice2 = np.random.choice(len(seg), self.npoints2, replace=True)
choice3 = np.random.choice(len(seg), self.npoints3, replace=True)
choice4 = np.random.choice(len(seg), self.npoints4, replace=True)
#resample
point_set1 = point_set[choice1, :]
point_set2 = point_set[choice2, :]
point_set3 = point_set[choice3, :]
point_set4 = point_set[choice4, :]
seg = seg[choice3]
point_set1 = torch.from_numpy(point_set1)
point_set2 = torch.from_numpy(point_set2)
point_set3 = torch.from_numpy(point_set3)
point_set4 = torch.from_numpy(point_set4)
seg = torch.from_numpy(seg)
cls = torch.from_numpy(np.array([cls]).astype(np.int64))
if self.classification:
return point_set1,point_set2,point_set3,point_set4,cls
else:
return point_set1,point_set2,point_set3,point_set4,seg
def __len__(self):
return len(self.datapath)
def pc_normalize(pc):
l = pc.shape[0]
centroid = np.mean(pc, axis=0)
pc = pc - centroid
m = np.max(np.sqrt(np.sum(pc**2, axis=1)))
pc = pc / m
return pc
class ModelNetDataset():
def __init__(self, root, batch_size = 50, npoints1 = 256,npoints2 = 512,npoints3 = 1024,npoints4 = 2048, split='train', normalize=True, normal_channel=False, modelnet10=False, cache_size=15000, shuffle=None,class_choice=None):
self.root = root+'modelnet40_normal_resampled/'
self.batch_size = batch_size
self.npoints1 = npoints1
self.npoints2 = npoints2
self.npoints3 = npoints3
self.npoints4 = npoints4
self.normalize = normalize
if modelnet10:
self.catfile = os.path.join(self.root, 'modelnet10_shape_names.txt')
else:
self.catfile = os.path.join(self.root, 'modelnet40_shape_names.txt')
self.cat = [line.rstrip() for line in open(self.catfile)]
self.classes = dict(zip(self.cat, range(len(self.cat))))
self.normal_channel = normal_channel
shape_ids = {}
if modelnet10:
shape_ids['train'] = [line.rstrip() for line in open(os.path.join(self.root, 'modelnet10_train.txt'))]
shape_ids['test']= [line.rstrip() for line in open(os.path.join(self.root, 'modelnet10_test.txt'))]
else:
shape_ids['train'] = [line.rstrip() for line in open(os.path.join(self.root, 'modelnet40_train.txt'))]
shape_ids['test']= [line.rstrip() for line in open(os.path.join(self.root, 'modelnet40_test.txt'))]
assert(split=='train' or split=='test')
shape_names = ['_'.join(x.split('_')[0:-1]) for x in shape_ids[split]]
# list of (shape_name, shape_txt_file_path) tuple
self.datapath = [(shape_names[i], os.path.join(self.root, shape_names[i], shape_ids[split][i])+'.txt') for i in range(len(shape_ids[split]))]
self.cache_size = cache_size # how many data points to cache in memory
self.cache = {} # from index to (point_set, cls) tuple
if shuffle is None:
if split == 'train': self.shuffle = True
else: self.shuffle = False
else:
self.shuffle = shuffle
self.reset()
def _augment_batch_data(self, batch_data):
if self.normal_channel:
rotated_data = provider.rotate_point_cloud_with_normal(batch_data)
rotated_data = provider.rotate_perturbation_point_cloud_with_normal(rotated_data)
else:
rotated_data = provider.rotate_point_cloud(batch_data)
rotated_data = provider.rotate_perturbation_point_cloud(rotated_data)
jittered_data = provider.random_scale_point_cloud(rotated_data[:,:,0:3])
jittered_data = provider.shift_point_cloud(jittered_data)
jittered_data = provider.jitter_point_cloud(jittered_data)
rotated_data[:,:,0:3] = jittered_data
return provider.shuffle_points(rotated_data)
def _get_item(self, index):
if index in self.cache:
point_set, cls = self.cache[index]
else:
fn = self.datapath[index]
cls = self.classes[self.datapath[index][0]]
cls = np.array([cls]).astype(np.int32)
point_set = np.loadtxt(fn[1],delimiter=',').astype(np.float32)
# Take the first npoints
point_set1 = point_set[0:self.npoints1,:]
point_set2 = point_set[0:self.npoints2,:]
point_set3 = point_set[0:self.npoints3,:]
point_set4 = point_set[0:self.npoints4,:]
if self.normalize:
point_set1[:,0:3] = pc_normalize(point_set1[:,0:3])
point_set2[:,0:3] = pc_normalize(point_set2[:,0:3])
point_set3[:,0:3] = pc_normalize(point_set3[:,0:3])
point_set4[:,0:3] = pc_normalize(point_set4[:,0:3])
if not self.normal_channel:
point_set1 = point_set1[:,0:3]
point_set2 = point_set2[:,0:3]
point_set3 = point_set3[:,0:3]
point_set4 = point_set4[:,0:3]
if len(self.cache) < self.cache_size:
self.cache[index] = (point_set, cls)
return point_set1,point_set2,point_set3,point_set4,cls
def __getitem__(self, index):
return self._get_item(index)
def __len__(self):
return len(self.datapath)
def num_channel(self):
if self.normal_channel:
return 6
else:
return 3
def reset(self):
self.idxs = np.arange(0, len(self.datapath))
if self.shuffle:
np.random.shuffle(self.idxs)
self.num_batches = (len(self.datapath)+self.batch_size-1) // self.batch_size
self.batch_idx = 0
def has_next_batch(self):
return self.batch_idx < self.num_batches
def next_batch(self, augment=False):
''' returned dimension may be smaller than self.batch_size '''
start_idx = self.batch_idx * self.batch_size
end_idx = min((self.batch_idx+1) * self.batch_size, len(self.datapath))
bsize = end_idx - start_idx
batch_data = np.zeros((bsize, self.npoints, self.num_channel()))
batch_label = np.zeros((bsize), dtype=np.int32)
for i in range(bsize):
ps,cls = self._get_item(self.idxs[i+start_idx])
batch_data[i] = ps
batch_label[i] = cls
self.batch_idx += 1
if augment: batch_data = self._augment_batch_data(batch_data)
return batch_data, batch_label
#------------------------------------------------------------------------------------------
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 ShapeNetCore(Dataset):
GRAVITATIONAL_AXIS = 1
def __init__(self, cates_list, split, scale_mode, path, transform=None):
super().__init__()
cates = [cates_list]
assert isinstance(cates, list), '`cates` must be a list of cate names.'
assert split in ('train', 'val', 'test')
assert scale_mode is None or scale_mode in ('global_unit', 'shape_unit', 'shape_bbox', 'shape_half', 'shape_34')
self.path = path
if 'all' in cates:
cates = cate_to_synsetid.keys()
self.cate_synsetids = [cate_to_synsetid[s] for s in cates]
self.cate_synsetids.sort()
self.split = split
self.scale_mode = scale_mode
self.transform = transform
self.pointclouds = []
self.stats = None
self.get_statistics()
self.load()
def get_statistics(self):
basename = os.path.basename(self.path)
dsetname = basename[:basename.rfind('.')]
stats_dir = os.path.join(os.path.dirname(self.path), dsetname + '_stats')
os.makedirs(stats_dir, exist_ok=True)
if len(self.cate_synsetids) == len(cate_to_synsetid):
stats_save_path = os.path.join(stats_dir, 'stats_all.pt')
else:
stats_save_path = os.path.join(stats_dir, 'stats_' + '_'.join(self.cate_synsetids) + '.pt')
if os.path.exists(stats_save_path):
self.stats = torch.load(stats_save_path)
return self.stats
with h5py.File(self.path, 'r') as f:
pointclouds = []
for synsetid in self.cate_synsetids:
for split in ('train', 'val', 'test'):
pointclouds.append(torch.from_numpy(f[synsetid][split][...]))
all_points = torch.cat(pointclouds, dim=0) # (B, N, 3)
B, N, _ = all_points.size()
mean = all_points.view(B*N, -1).mean(dim=0) # (1, 3)
std = all_points.view(-1).std(dim=0) # (1, )
self.stats = {'mean': mean, 'std': std}
torch.save(self.stats, stats_save_path)
return self.stats
def load(self):
def _enumerate_pointclouds(f):
for synsetid in self.cate_synsetids:
cate_name = synsetid_to_cate[synsetid]
for j, pc in enumerate(f[synsetid][self.split]):
yield torch.from_numpy(pc), j, cate_name
with h5py.File(self.path, mode='r') as f:
for pc, pc_id, cate_name in _enumerate_pointclouds(f):
if self.scale_mode == 'global_unit':
shift = pc.mean(dim=0).reshape(1, 3)
scale = self.stats['std'].reshape(1, 1)
elif self.scale_mode == 'shape_unit':
shift = pc.mean(dim=0).reshape(1, 3)
scale = pc.flatten().std().reshape(1, 1)
elif self.scale_mode == 'shape_half':
shift = pc.mean(dim=0).reshape(1, 3)
scale = pc.flatten().std().reshape(1, 1) / (0.5)
elif self.scale_mode == 'shape_34':
shift = pc.mean(dim=0).reshape(1, 3)
scale = pc.flatten().std().reshape(1, 1) / (0.75)
elif self.scale_mode == 'shape_bbox':
pc_max, _ = pc.max(dim=0, keepdim=True) # (1, 3)
pc_min, _ = pc.min(dim=0, keepdim=True) # (1, 3)
shift = ((pc_min + pc_max) / 2).view(1, 3)
scale = (pc_max - pc_min).max().reshape(1, 1) / 2
else:
shift = torch.zeros([1, 3])
scale = torch.ones([1, 1])
pc = (pc - shift) / scale
self.pointclouds.append({
'pointcloud': pc,
'cate': cate_name,
'id': pc_id,
'shift': shift,
'scale': scale
})
# Deterministically shuffle the dataset
self.pointclouds.sort(key=lambda data: data['id'], reverse=False)
random.Random(2020).shuffle(self.pointclouds)
def __len__(self):
return len(self.pointclouds)
def __getitem__(self, idx):
data = {k:v.clone() if isinstance(v, torch.Tensor) else copy(v) for k, v in self.pointclouds[idx].items()}
if self.transform is not None:
data = self.transform(data)
tr_idxs1 = np.random.choice(data['pointcloud'].shape[0], 256)
tr_idxs2 = np.random.choice(data['pointcloud'].shape[0], 512)
tr_idxs3 = np.random.choice(data['pointcloud'].shape[0], 1024)
data1 = data['pointcloud'][tr_idxs1, :].float()
data2 = data['pointcloud'][tr_idxs2, :].float()
data3 = data['pointcloud'][tr_idxs3, :].float()
return data1,data2,data3,data['pointcloud'],data['cate']