forked from minghanz/EquivReg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fields.py
222 lines (165 loc) · 6.01 KB
/
fields.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
import os
import numpy as np
import utils.binvox_rw as binvox_rw
import logging
class Field(object):
''' Data fields class.
'''
def load(self, data_path):
''' Loads a data point.
Args:
data_path (str): path to data file
idx (int): index of data point
category (int): index of category
'''
raise NotImplementedError
def check_complete(self, files):
''' Checks if set is complete.
Args:
files: files
'''
raise NotImplementedError
class PointsField(Field):
''' Point Field.
It provides the field to load point data. This is used for the points
randomly sampled in the bounding volume of the 3D shape.
Args:
file_name (str): file name
transform (list): list of transformations which will be applied to the
points tensor
with_transforms (bool): whether scaling and rotation data should be
provided
'''
def __init__(self, file_name, transform=None, unpackbits=False):
self.file_name = file_name
self.transform = transform
self.unpackbits = unpackbits
def load(self, model_path):
# Load data
file_path = os.path.join(model_path, self.file_name)
points_dict = np.load(file_path)
# Points
points = points_dict['points']
# Break symmetry if given in float16:
if points.dtype == np.float16:
points = points.astype(np.float32)
points += 1e-4 * np.random.randn(*points.shape)
else:
points = points.astype(np.float32)
# Occupancies
occupancies = points_dict['occupancies']
if self.unpackbits:
occupancies = np.unpackbits(occupancies)[:points.shape[0]]
occupancies = occupancies.astype(np.float32)
# Output dict
data = {
None: points,
'occ': occupancies,
}
if self.transform is not None:
data = self.transform(data)
return data
class VoxelsField(Field):
''' Voxel field class.
It provides the class used for voxel-based data.
Args:
file_name (str): file name
transform (list): list of transformations applied to data points
'''
def __init__(self, file_name, transform=None):
self.file_name = file_name
self.transform = transform
def load(self, model_path):
file_path = os.path.join(model_path, self.file_name)
with open(file_path, 'rb') as f:
voxels = binvox_rw.read_as_3d_array(f)
voxels = voxels.data.astype(np.float32)
if self.transform is not None:
voxels = self.transform(voxels)
return voxels
def check_complete(self, files):
complete = (self.file_name in files)
return complete
class PointCloudField(Field):
''' Point cloud field.
It provides the field used for point cloud data. These are the points
randomly sampled on the mesh.
Args:
file_name (str): file name
transform (list): list of transformations applied to data points
with_transforms (bool): whether scaling and rotation dat should be
provided
'''
def __init__(self, file_name, transform=None):
self.file_name = file_name
self.transform = transform
def load_dict(self, pointcloud_dict):
points = pointcloud_dict['points'].astype(np.float32)
normals = pointcloud_dict['normals'].astype(np.float32)
# print("points.shape", points.shape) # 100000, 3
data = {
None: points,
'normals': normals,
}
if self.transform is not None:
data = self.transform(data)
return data
def load_array(self, pointcloud_array):
data = {None: pointcloud_array}
if self.transform is not None:
data = self.transform(data)
return data
def load(self, model_path):
file_path = os.path.join(model_path, self.file_name)
pointcloud_dict = np.load(file_path)
if isinstance(pointcloud_dict, np.lib.npyio.NpzFile):
data = self.load_dict(pointcloud_dict)
elif isinstance(pointcloud_dict, np.ndarray):
data = self.load_array(pointcloud_dict)
else:
raise ValueError('pointcloud file content {} unexpected: {}'.format(type(pointcloud_dict), file_path))
return data
def check_complete(self, files):
complete = (self.file_name in files)
return complete
class RotationField(Field):
'''It provides the field used for a rotation transformation.
When benchmarking registration performance,
it is useful to have fixed initial transformations instead of random ones.
'''
def __init__(self, file_name) -> None:
super().__init__()
self.file_name = file_name
def load(self, model_path):
file_path = os.path.join(model_path, self.file_name)
data = np.load(file_path)
assert 'T' in data and 'deg' in data, data
data_out = {
None: data['T'],
'deg': data['deg'],
}
return data_out
class TransformationField(Field):
'''It provides the field used for a rigid body transformation.
When benchmarking registration performance,
it is useful to have fixed initial transformations instead of random ones.
'''
def __init__(self, file_name) -> None:
super().__init__()
self.file_name = file_name
def load(self, model_path):
file_path = os.path.join(model_path, self.file_name)
T = np.load(file_path)
return T
class IndexField(Field):
''' Basic index field.'''
def check_complete(self, files):
return True
class CategoryField(Field):
''' Basic category field.'''
def check_complete(self, files):
''' Check if field is complete.
Args:
files: files
'''
return True