-
Notifications
You must be signed in to change notification settings - Fork 82
/
modelnet_shrec_loader.py
295 lines (235 loc) · 9.31 KB
/
modelnet_shrec_loader.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
import torch.utils.data as data
import random
import numbers
import os
import os.path
import numpy as np
import struct
import math
import torch
import torchvision
import matplotlib.pyplot as plt
import h5py
import faiss
from .augmentation import *
# Read numpy array data and label from h5_filename
def load_h5(h5_filename):
f = h5py.File(h5_filename)
data = f['data'][:]
label = f['label'][:]
return (data, label)
def make_dataset_modelnet40_10k(root, mode, opt):
dataset = []
rows = round(math.sqrt(opt.node_num))
cols = rows
f = open(os.path.join(root, 'modelnet%d_shape_names.txt' % opt.classes))
shape_list = [str.rstrip() for str in f.readlines()]
f.close()
if 'train' == mode:
f = open(os.path.join(root, 'modelnet%d_train.txt' % opt.classes), 'r')
lines = [str.rstrip() for str in f.readlines()]
f.close()
elif 'test' == mode:
f = open(os.path.join(root, 'modelnet%d_test.txt' % opt.classes), 'r')
lines = [str.rstrip() for str in f.readlines()]
f.close()
else:
raise Exception('Network mode error.')
for i, name in enumerate(lines):
# locate the folder name
folder = name[0:-5]
file_name = name
# get the label
label = shape_list.index(folder)
# som node locations
som_nodes_folder = '%dx%d_som_nodes' % (rows, cols)
item = (os.path.join(root, folder, file_name + '.npy'),
label,
os.path.join(root, som_nodes_folder, folder, file_name + '.npy'))
dataset.append(item)
return dataset
def make_dataset_shrec2016(root, mode, opt):
rows = round(math.sqrt(opt.node_num))
cols = rows
dataset = []
# load category txt
f = open(os.path.join(root, 'category.txt'), 'r')
category_list = [str.rstrip() for str in f.readlines()]
f.close()
if 'train'==mode:
f = open(os.path.join(root, 'train.txt'), 'r')
lines = [str.rstrip() for str in f.readlines()]
f.close()
elif 'val'==mode:
f = open(os.path.join(root, 'val.txt'), 'r')
lines = [str.rstrip() for str in f.readlines()]
f.close()
elif 'test'==mode:
f = open(os.path.join(root, 'test.txt'), 'r')
lines = [str.rstrip() for str in f.readlines()]
f.close()
else:
raise Exception('Network mode error.')
if 'train' == mode or 'val' == mode:
for line in lines:
line_split = [x.strip() for x in line.split(',')]
name, category = line_split[0], line_split[1]
npz_file = os.path.join(root, '%dx%d'%(rows,cols), mode, 'model_'+name+'.npz')
try:
category = category_list.index(category)
except ValueError:
continue
item = (npz_file, category)
dataset.append(item)
elif 'test' == mode:
for line in lines:
name, category = line, int(line) % 55
npz_file = os.path.join(root, '%dx%d'%(rows,cols), mode, 'model_'+name+'.npz')
item = (npz_file, category)
dataset.append(item)
return dataset
class KNNBuilder:
def __init__(self, k):
self.k = k
self.dimension = 3
def build_nn_index(self, database):
'''
:param database: numpy array of Nx3
:return: Faiss index, in CPU
'''
index = faiss.IndexFlatL2(self.dimension) # dimension is 3
index.add(database)
return index
def search_nn(self, index, query, k):
'''
:param index: Faiss index
:param query: numpy array of Nx3
:return: D: numpy array of Nxk
I: numpy array of Nxk
'''
D, I = index.search(query, k)
return D, I
def self_build_search(self, x):
'''
:param x: numpy array of Nxd
:return: D: numpy array of Nxk
I: numpy array of Nxk
'''
x = np.ascontiguousarray(x, dtype=np.float32)
index = self.build_nn_index(x)
D, I = self.search_nn(index, x, self.k)
return D, I
class FarthestSampler:
def __init__(self):
pass
def calc_distances(self, p0, points):
return ((p0 - points) ** 2).sum(axis=1)
def sample(self, pts, k):
farthest_pts = np.zeros((k, 3))
farthest_pts[0] = pts[np.random.randint(len(pts))]
distances = self.calc_distances(farthest_pts[0], pts)
for i in range(1, k):
farthest_pts[i] = pts[np.argmax(distances)]
distances = np.minimum(distances, self.calc_distances(farthest_pts[i], pts))
return farthest_pts
class ModelNet_Shrec_Loader(data.Dataset):
def __init__(self, root, mode, opt):
super(ModelNet_Shrec_Loader, self).__init__()
self.root = root
self.opt = opt
self.mode = mode
if self.opt.dataset == 'modelnet':
self.dataset = make_dataset_modelnet40_10k(self.root, mode, opt)
elif self.opt.dataset == 'shrec':
self.dataset = make_dataset_shrec2016(self.root, mode, opt)
else:
raise Exception('Dataset incorrect.')
# kNN search on SOM nodes
self.knn_builder = KNNBuilder(self.opt.som_k)
# farthest point sample
self.fathest_sampler = FarthestSampler()
def __len__(self):
return len(self.dataset)
def __getitem__(self, index):
if self.opt.dataset == 'modelnet':
pc_np_file, class_id, som_node_np_file = self.dataset[index]
data = np.load(pc_np_file)
data = data[np.random.choice(data.shape[0], self.opt.input_pc_num, replace=False), :]
pc_np = data[:, 0:3] # Nx3
surface_normal_np = data[:, 3:6] # Nx3
som_node_np = np.load(som_node_np_file) # node_numx3
elif self.opt.dataset == 'shrec':
npz_file, class_id = self.dataset[index]
data = np.load(npz_file)
pc_np = data['pc']
surface_normal_np = data['sn']
som_node_np = data['som_node']
# random choice
choice_idx = np.random.choice(pc_np.shape[0], self.opt.input_pc_num, replace=False)
pc_np = pc_np[choice_idx, :]
surface_normal_np = surface_normal_np[choice_idx, :]
else:
raise Exception('Dataset incorrect.')
# augmentation
if self.mode == 'train':
# rotate by 0/90/180/270 degree over z axis
# pc_np = rotate_point_cloud_90(pc_np)
# som_node_np = rotate_point_cloud_90(som_node_np)
# rotation perturbation, pc and som should follow the same rotation, surface normal rotation is unclear
if self.opt.rot_horizontal:
pc_np, surface_normal_np, som_node_np = rotate_point_cloud_with_normal_som(pc_np, surface_normal_np, som_node_np)
if self.opt.rot_perturbation:
pc_np, surface_normal_np, som_node_np = rotate_perturbation_point_cloud_with_normal_som(pc_np, surface_normal_np, som_node_np)
# random jittering
pc_np = jitter_point_cloud(pc_np)
surface_normal_np = jitter_point_cloud(surface_normal_np)
som_node_np = jitter_point_cloud(som_node_np, sigma=0.04, clip=0.1)
# random scale
scale = np.random.uniform(low=0.8, high=1.2)
pc_np = pc_np * scale
som_node_np = som_node_np * scale
surface_normal_np = surface_normal_np * scale
# random shift
if self.opt.translation_perturbation:
shift = np.random.uniform(-0.1, 0.1, (1,3))
pc_np += shift
som_node_np += shift
# convert to tensor
pc = torch.from_numpy(pc_np.transpose().astype(np.float32)) # 3xN
# surface normal
surface_normal = torch.from_numpy(surface_normal_np.transpose().astype(np.float32)) # 3xN
# som
som_node = torch.from_numpy(som_node_np.transpose().astype(np.float32)) # 3xnode_num
# kNN search: som -> som
if self.opt.som_k >= 2:
D, I = self.knn_builder.self_build_search(som_node_np)
som_knn_I = torch.from_numpy(I.astype(np.int64)) # node_num x som_k
else:
som_knn_I = torch.from_numpy(np.arange(start=0, stop=self.opt.node_num, dtype=np.int64).reshape((self.opt.node_num, 1))) # node_num x 1
# print(som_node_np)
# print(D)
# print(I)
# assert False
if self.opt.dataset == 'shrec':
return pc, surface_normal, class_id, som_node, som_knn_I, index
else:
return pc, surface_normal, class_id, som_node, som_knn_I
if __name__=="__main__":
# dataset = make_dataset_modelnet40('/ssd/dataset/modelnet40_ply_hdf5_2048/', True)
# print(len(dataset))
# print(dataset[0])
class VirtualOpt():
def __init__(self):
self.load_all_data = False
self.input_pc_num = 5000
self.batch_size = 8
self.dataset = '10k'
self.node_num = 64
self.classes = 10
self.som_k = 9
opt = VirtualOpt()
trainset = ModelNet_Shrec_Loader('/ssd/dataset/modelnet40-normal_numpy/', 'train', opt)
print('---')
print(len(trainset))
print(trainset[0])
trainloader = torch.utils.data.DataLoader(trainset, batch_size=opt.batch_size, shuffle=False, num_workers=4)