forked from samarth-robo/contactdb_prediction
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvoxel_dataset.py
278 lines (247 loc) · 10.7 KB
/
voxel_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
273
274
275
276
277
278
import torch.utils.data as tdata
import os
import numpy as np
import transforms3d.euler as txe
import utils
from collections import OrderedDict
from IPython.core.debugger import set_trace
osp = os.path
class VoxelDataset(tdata.Dataset):
def __init__(self, data_dir, instruction, train,
grid_size=64, include_sessions=None, exclude_sessions=None,
random_rotation=180, n_ensemble=20, color_thresh=0.4, test_only=False):
super(VoxelDataset, self).__init__()
# print(data_dir)
data_dir = osp.expanduser(data_dir)
self.grid_size = grid_size
self.random_rotation = random_rotation
self.n_ensemble = n_ensemble
self.color_thresh = color_thresh
# list the voxel grids
self.filenames = OrderedDict()
for filename in next(os.walk(data_dir))[-1]:
# print(filename)
if '_solid.npy' not in filename:
continue
if test_only:
if 'testonly' not in filename:
continue
else:
if '_{:s}_'.format(instruction) not in filename:
continue
session_name = filename.split('_')[0]
if include_sessions is not None:
if session_name not in include_sessions:
continue
if exclude_sessions is not None:
if session_name in exclude_sessions:
continue
offset = 1 if test_only else 2
object_name = '_'.join(filename.split('.')[0].split('_')[offset:-1])
if not test_only:
if train:
if object_name in utils.test_objects:
continue
else:
if object_name not in utils.test_objects:
continue
filename = osp.join(data_dir, filename)
if object_name not in self.filenames:
self.filenames[object_name] = [filename]
else:
self.filenames[object_name].append(filename)
def __len__(self):
return len(self.filenames)
def __getitem__(self, index):
# load geometry
object_name = list(self.filenames.keys())[index]
x, y, z, c, xx, yy, zz = np.load(self.filenames[object_name][0])
x, y, z = x.astype(int), y.astype(int), z.astype(int)
pts = np.vstack((xx, yy, zz))
offset = (pts.max(1, keepdims=True) + pts.min(1, keepdims=True)) / 2
pts -= offset
scale = max(pts.max(1) - pts.min(1)) / 2
pts /= scale
pts = np.vstack((np.ones(pts.shape[1]), pts, scale * np.ones(pts.shape[1])))
# print(pts.shape)
# center the object
offset_x = (self.grid_size - x.max() - 1) // 2
offset_y = (self.grid_size - y.max() - 1) // 2
offset_z = (self.grid_size - z.max() - 1) // 2
x += offset_x
y += offset_y
z += offset_z
# random rotation
if abs(self.random_rotation) > 0:
theta = np.random.uniform(-np.pi * self.random_rotation / 180,
np.pi * self.random_rotation / 180)
R = txe.euler2mat(0, 0, theta)
p = np.vstack((x, y, z)) + 0.5
p = p - self.grid_size / 2.0
p = R @ p
s = max(p.max(1) - p.min(1))
p = p * (self.grid_size - 1) / s
s = (p.max(1, keepdims=True) + p.min(1, keepdims=True)) / 2.0
p = p + self.grid_size / 2.0 - s
x, y, z = (p - 0.5).astype(int)
# create occupancy grid
geom = np.zeros((5, self.grid_size, self.grid_size, self.grid_size),
dtype=np.float32)
geom[:, z, y, x] = pts
# load textures
N = len(self.filenames[object_name])
choice = np.arange(N)
if self.n_ensemble > 0 and self.n_ensemble < N:
choice = np.random.choice(N, size=self.n_ensemble, replace=False)
texs = []
filenames = [self.filenames[object_name][c] for c in choice]
for filename in filenames:
_, _, _, c, _, _, _ = np.load(filename)
c = utils.discretize_texture(c, thresh=self.color_thresh)
tex = 2 * np.ones((self.grid_size, self.grid_size, self.grid_size),
dtype=np.float32)
tex[z, y, x] = c
texs.append(tex)
texs = np.stack(texs)
return geom.astype(np.float32), texs.astype(np.int)
class VoxelPredictionDataset(tdata.Dataset):
def __init__(self, data_dir, instruction, train,
grid_size=64, include_sessions=None, exclude_sessions=None,
random_rotation=180, n_ensemble=20, color_thresh=0.4, test_only=False):
super(VoxelPredictionDataset, self).__init__()
data_dir = osp.expanduser(data_dir)
self.grid_size = grid_size
self.random_rotation = random_rotation
self.n_ensemble = n_ensemble
self.color_thresh = color_thresh
self.possible_uses = ["handoff", "use"]
# list the voxel grids
self.filenames = OrderedDict()
for filename in next(os.walk(data_dir))[-1]:
# print(filename)
if '_solid.npy' not in filename:
continue
# if 'banana' not in filename:
# continue
# print(filename)
# if test_only:
# if 'testonly' not in filename:
# continue
if not test_only:
if 'testonly' in filename:
continue
session_name = filename.split('_')[0]
# process inclusions and exclusions
if include_sessions is not None:
if session_name not in include_sessions:
continue
if exclude_sessions is not None:
if session_name in exclude_sessions:
continue
# process offsets and object_names
offset = 1 if test_only else 2
object_name = '_'.join(filename.split('.')[0].split('_')[offset:-1])
use = filename.split('.')[0].split('_')
# print(use)
use = use[1]
# print(object_name, use)
if not test_only:
if train:
if object_name in utils.test_objects:
continue
else:
if object_name not in utils.test_objects:
continue
filename = osp.join(data_dir, filename)
if (object_name, use) not in self.filenames:
self.filenames[(object_name, use)] = []
self.filenames[(object_name, use)].append(filename)
# print(self.filenames.keys())
def __len__(self):
return len(self.filenames)
def __getitem__(self, index):
index = index % len(self.filenames)
# load geometry
object_name, use = list(self.filenames.keys())[index]
one_hot = np.zeros((len(self.filenames.keys())))
one_hot[index] = 1
# print("onehot", one_hot.shape)
# print(object_name, use) #, self.filenames[(object_name, use)])
x, y, z, c, xx, yy, zz = np.load(self.filenames[(object_name, use)][0])
x, y, z = x.astype(int), y.astype(int), z.astype(int)
pts = np.vstack((xx, yy, zz))
offset = (pts.max(1, keepdims=True) + pts.min(1, keepdims=True)) / 2
pts -= offset
scale = max(pts.max(1) - pts.min(1)) / 2
pts /= scale
# print(pts.shape)
pts = np.vstack((np.ones(pts.shape[1]), pts, scale * np.ones(pts.shape[1])))
# print(pts.shape)
# center the object
offset_x = (self.grid_size - x.max() - 1) // 2
offset_y = (self.grid_size - y.max() - 1) // 2
offset_z = (self.grid_size - z.max() - 1) // 2
x += offset_x
y += offset_y
z += offset_z
# random rotation
if abs(self.random_rotation) > 0:
theta = np.random.uniform(-np.pi * self.random_rotation / 180,
np.pi * self.random_rotation / 180)
R = txe.euler2mat(0, 0, theta)
p = np.vstack((x, y, z)) + 0.5
p = p - self.grid_size / 2.0
p = R @ p
s = max(p.max(1) - p.min(1))
p = p * (self.grid_size - 1) / s
s = (p.max(1, keepdims=True) + p.min(1, keepdims=True)) / 2.0
p = p + self.grid_size / 2.0 - s
x, y, z = (p - 0.5).astype(int)
# load textures
N = len(self.filenames[(object_name, use)])
choice = np.arange(N)
if 0 < self.n_ensemble < N:
choice = np.random.choice(N, size=self.n_ensemble, replace=False)
texs = []
filenames = [self.filenames[(object_name, use)][c] for c in choice]
for filename in filenames:
_, _, _, c, _, _, _ = np.load(filename)
# print(c.shape)
c = utils.discretize_texture(c, thresh=self.color_thresh)
# print(c.shape)
tex = 2 * np.ones((self.grid_size, self.grid_size, self.grid_size),
dtype=np.float32)
tex[z, y, x] = c
# print(z.shape, x.shape, y.shape, tex.shape)
texs.append(tex)
texs = np.stack(texs)
# create occupancy grid
geom = np.zeros((5 + self.n_ensemble, self.grid_size, self.grid_size, self.grid_size),
dtype=np.float32)
# print("geom[5, z, y, x]", geom[0:5, z, y, x].shape, index)
geom[0:5, z, y, x] = pts
# print("texs", texs[:, :, :, :].flatten().shape)
# print("pts", pts.shape)
# print("geom[5, z, y, x]", geom[:5, z, y, x].shape, index)
# print(geom.shape)
geom[5:5+self.n_ensemble, :, :, :] = texs
return geom.astype(np.float32), index #one_hot.astype(np.int)
if __name__ == '__main__':
n_ensemble = 10
N_show = 30
# dset = VoxelDataset(osp.join('data', 'voxelized_meshes'), 'use',
# train=True, random_rotation=180, n_ensemble=n_ensemble)
dset = VoxelPredictionDataset(osp.join('data', 'voxelized_meshes'), 'use',
train=True, random_rotation=180, n_ensemble=n_ensemble)
for idx in np.random.choice(len(dset), N_show):
geom, tex, one_hot = dset[idx]
print("geom", geom.shape)
print("tex", tex.shape)
print("one_hot", one_hot.shape)
z, y, x = np.nonzero(geom[0]) # see which voxels are occupied
c = tex[0, z, y, x]
x3d = geom[1, z, y, x]
y3d = geom[2, z, y, x]
z3d = geom[3, z, y, x]
# utils.show_pointcloud(np.vstack((x3d, y3d, z3d)).T, c)
# utils.show_pointcloud(np.vstack((x, y, z)).T, c)