forked from ohhhyeahhh/PointAttN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdataset.py
276 lines (214 loc) · 9.26 KB
/
dataset.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
import os
import open3d as o3d
import torch
import numpy as np
import torch.utils.data as data
import h5py
import math
import transforms3d
import random
from tensorpack import dataflow
class PCN_pcd(data.Dataset):
def __init__(self, path, prefix="train"):
if prefix=="train":
self.file_path = os.path.join(path,'train')
elif prefix=="val":
self.file_path = os.path.join(path,'val')
elif prefix=="test":
self.file_path = os.path.join(path,'test')
else:
raise ValueError("ValueError prefix should be [train/val/test] ")
self.prefix = prefix
self.label_map ={'02691156': '0', '02933112': '1', '02958343': '2',
'03001627': '3', '03636649': '4', '04256520': '5',
'04379243': '6', '04530566': '7', 'all': '8'}
self.label_map_inverse ={'0': '02691156', '1': '02933112', '2': '02958343',
'3': '03001627', '4': '03636649', '5': '04256520',
'6': '04379243', '7': '04530566', '8': 'all'}
self.input_data, self.labels = self.get_data(os.path.join(self.file_path, 'partial'))
random.shuffle(self.input_data)
self.len = len(self.input_data)
self.scale = 0
self.mirror = 1
self.rot = 0
self.sample = 1
def __len__(self):
return self.len
def read_pcd(self, path):
pcd = o3d.io.read_point_cloud(path)
points = np.asarray(pcd.points)
return points
def get_data(self, path):
cls = os.listdir(path)
data = []
labels = []
for c in cls:
objs = os.listdir(os.path.join(path, c))
for obj in objs:
f_names = os.listdir(os.path.join(path, c, obj))
obj_list = []
for f_name in f_names:
data_path = os.path.join(path, c, obj, f_name)
obj_list.append(data_path)
# points = self.read_pcd(os.path.join(path, c, obj, f_name))
data.append(obj_list)
labels.append(self.label_map[c])
return data, labels
def randomsample(self, ptcloud ,n_points):
choice = np.random.permutation(ptcloud.shape[0])
ptcloud = ptcloud[choice[:n_points]]
if ptcloud.shape[0] < n_points:
zeros = np.zeros((n_points - ptcloud.shape[0], 3))
ptcloud = np.concatenate([ptcloud, zeros])
return ptcloud
def upsample(self, ptcloud, n_points):
curr = ptcloud.shape[0]
need = n_points - curr
if need < 0:
return ptcloud[np.random.permutation(n_points)]
while curr <= need:
ptcloud = np.tile(ptcloud, (2, 1))
# ptcloud = np.concatenate([ptcloud,np.zeros_like(ptcloud)],dim=0)
need -= curr
curr *= 2
choice = np.random.permutation(need)
ptcloud = np.concatenate((ptcloud, ptcloud[choice]))
return ptcloud
def get_transform(self, points):
result = []
rnd_value = np.random.uniform(0, 1)
if self.mirror and self.prefix == 'train':
trfm_mat = transforms3d.zooms.zfdir2mat(1)
trfm_mat_x = np.dot(transforms3d.zooms.zfdir2mat(-1, [1, 0, 0]), trfm_mat)
trfm_mat_z = np.dot(transforms3d.zooms.zfdir2mat(-1, [0, 0, 1]), trfm_mat)
if rnd_value <= 0.25:
trfm_mat = np.dot(trfm_mat_x, trfm_mat)
trfm_mat = np.dot(trfm_mat_z, trfm_mat)
elif rnd_value > 0.25 and rnd_value <= 0.5: # lgtm [py/redundant-comparison]
trfm_mat = np.dot(trfm_mat_x, trfm_mat)
elif rnd_value > 0.5 and rnd_value <= 0.75:
trfm_mat = np.dot(trfm_mat_z, trfm_mat)
for ptcloud in points:
ptcloud[:, :3] = np.dot(ptcloud[:, :3], trfm_mat.T)
if self.scale:
ptcloud = ptcloud * self.scale
result.append(ptcloud)
return result[0],result[1]
def __getitem__(self, index):
partial_path = self.input_data[index]
n_sample = len(partial_path)
idx = random.randint(0, n_sample-1)
partial_path = partial_path[idx]
partial = self.read_pcd(partial_path)
# if self.prefix == 'train' and self.sample:
partial = self.upsample(partial, 2048)
gt_path = partial_path.replace('/'+partial_path.split('/')[-1],'.pcd')
gt_path = gt_path.replace('partial','complete')
if self.prefix == 'train':
complete = self.read_pcd(gt_path)
partial, complete = self.get_transform([partial, complete])
else:
complete = self.read_pcd(gt_path)
complete = torch.from_numpy(complete)
partial = torch.from_numpy(partial)
label = partial_path.split('/')[-3]
label = self.label_map[label]
obj = partial_path.split('/')[-2]
if self.prefix == 'test':
return label, partial, complete, obj
else:
return label, partial, complete
class C3D_h5(data.Dataset):
def __init__(self, path, prefix="train"):
if prefix=="train":
self.file_path = os.path.join(path,'train')
elif prefix=="val":
self.file_path = os.path.join(path,'val')
elif prefix=="test":
self.file_path = os.path.join(path,'test')
else:
raise ValueError("ValueError prefix should be [train/val/test] ")
self.prefix = prefix
self.label_map ={'02691156': '0', '02933112': '1', '02958343': '2',
'03001627': '3', '03636649': '4', '04256520': '5',
'04379243': '6', '04530566': '7', 'all': '8'}
if prefix is not "test":
self.input_data, self.labels = self.get_data(os.path.join(self.file_path, 'partial'))
self.gt_data, _ = self.get_data(os.path.join(self.file_path, 'gt'))
print(len(self.gt_data), len(self.labels))
else:
self.input_data, self.labels = self.get_data(os.path.join(self.file_path, 'partial'))
print(len(self.input_data))
self.len = len(self.input_data)
self.scale = 1
self.mirror = 1
self.rot = 0
self.sample = 1
def __len__(self):
return self.len
def get_data(self, path):
cls = os.listdir(path)
data = []
labels = []
for c in cls:
objs = os.listdir(os.path.join(path, c))
for obj in objs:
data.append(os.path.join(path,c,obj))
if self.prefix == "test":
labels.append(obj)
else:
labels.append(self.label_map[c])
return data, labels
def get_transform(self, points):
result = []
rnd_value = np.random.uniform(0, 1)
angle = random.uniform(0,2*math.pi)
scale = np.random.uniform(1/1.6, 1)
trfm_mat = transforms3d.zooms.zfdir2mat(1)
if self.mirror and self.prefix == 'train':
trfm_mat_x = np.dot(transforms3d.zooms.zfdir2mat(-1, [1, 0, 0]), trfm_mat)
trfm_mat_z = np.dot(transforms3d.zooms.zfdir2mat(-1, [0, 0, 1]), trfm_mat)
if rnd_value <= 0.25:
trfm_mat = np.dot(trfm_mat_x, trfm_mat)
trfm_mat = np.dot(trfm_mat_z, trfm_mat)
elif rnd_value > 0.25 and rnd_value <= 0.5: # lgtm [py/redundant-comparison]
trfm_mat = np.dot(trfm_mat_x, trfm_mat)
elif rnd_value > 0.5 and rnd_value <= 0.75:
trfm_mat = np.dot(trfm_mat_z, trfm_mat)
if self.rot:
trfm_mat = np.dot(transforms3d.axangles.axangle2mat([0,1,0],angle), trfm_mat)
for ptcloud in points:
ptcloud[:, :3] = np.dot(ptcloud[:, :3], trfm_mat.T)
if self.scale:
ptcloud = ptcloud * scale
result.append(ptcloud)
return result[0],result[1]
def __getitem__(self, index):
partial_path = self.input_data[index]
with h5py.File(partial_path, 'r') as f:
partial = np.array(f['data'])
if self.prefix == 'train' and self.sample:
choice = np.random.permutation((partial.shape[0]))
partial = partial[choice[:2048]]
if partial.shape[0] < 2048:
zeros = np.zeros((2048-partial.shape[0],3))
partial = np.concatenate([partial,zeros])
if self.prefix not in ["test"]:
complete_path = partial_path.replace('partial','gt')
with h5py.File(complete_path, 'r') as f:
complete = np.array(f['data'])
partial, complete = self.get_transform([partial, complete])
complete = torch.from_numpy(complete)
label = (self.labels[index])
partial = torch.from_numpy(partial)
return label, partial, complete
else:
partial = torch.from_numpy(partial)
label = (self.labels[index])
return label, partial, partial
if __name__ == '__main__':
dataset = C3D_h5(prefix='test')
dataloader = torch.utils.data.DataLoader(dataset, batch_size=1,
shuffle=True, num_workers=0)
for idx, data in enumerate(dataloader, 0):
print(data.shape)