-
Notifications
You must be signed in to change notification settings - Fork 5
/
ray_dataloader.py
205 lines (174 loc) · 7.69 KB
/
ray_dataloader.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
import torch
from mesh import get_k_eigenfunc_vec_vals, load_first_k_eigenfunctions
from dataset import load_preprocessed_data
def create_ray_dataloader(preproc_data_path,
eigenfunctions_path,
k,
feature_strategy,
mesh,
rescale_strategy,
eigenvalues_path,
embed_strategy,
batch_size,
shuffle,
drop_last,
device="cuda"):
# LOAD DATA
# Get input features
if feature_strategy == "efuncs":
features = load_first_k_eigenfunctions(eigenfunctions_path,
k,
rescale_strategy=rescale_strategy,
embed_strategy=embed_strategy,
eigenvalues_path=eigenvalues_path)
elif feature_strategy in ("ff", "rff", "xyz"):
assert mesh is not None
features = torch.from_numpy(mesh.vertices).to(dtype=torch.float32)
else:
raise ValueError(f"Unknown input feature strategy: {feature_strategy}")
# Get ray-mesh intersection information
data = load_preprocessed_data(preproc_data_path)
vertex_idxs_of_hit_faces = data["vertex_idxs_of_hit_faces"]
barycentric_coords = data["barycentric_coords"]
expected_rgbs = data["expected_rgbs"]
# Get view dependence information
unit_ray_dirs = data.get("unit_ray_dirs")
face_idxs = data.get("face_idxs")
return RayDataLoader(features,
feature_strategy,
vertex_idxs_of_hit_faces,
barycentric_coords,
expected_rgbs,
unit_ray_dirs,
face_idxs,
batch_size,
shuffle,
drop_last,
device=device)
class RayDataLoader:
def __init__(self,
features,
feature_strategy,
vertex_idxs_of_hit_faces,
barycentric_coords,
expected_rgbs,
unit_ray_dirs,
face_idxs,
batch_size,
shuffle,
drop_last,
device="cuda"):
self.device = device
# INITIALIZE DATA AND MOVE TO DEVICE
self.features = features.to(self.device)
self.feature_strategy = feature_strategy
self.vertex_idxs_of_hit_faces = vertex_idxs_of_hit_faces.to(self.device)
self.barycentric_coords = barycentric_coords.to(self.device)
self.expected_rgbs = expected_rgbs.to(self.device)
self.unit_ray_dirs = unit_ray_dirs
self.face_idxs = face_idxs
if self.unit_ray_dirs is not None:
assert self.face_idxs is not None
self.unit_ray_dirs = self.unit_ray_dirs.to(self.device)
self.face_idxs = self.face_idxs.to(self.device)
# DATALOADING SPECIFICS
self.shuffle = shuffle
self.drop_last = drop_last
self.B = batch_size
self.N = self.vertex_idxs_of_hit_faces.shape[0]
if self.drop_last:
self.num_batches = self.N // self.B
else:
self.num_batches = (self.N + self.B - 1) // self.B
self.i = 0
self.idxs = torch.arange(self.N, device=self.device)
def __len__(self):
return self.num_batches
def __iter__(self):
if self.shuffle:
self.idxs = torch.randperm(self.N, device=self.device)
self.i = 0
return self
def _get_next_batch_idxs(self):
low = self.i * self.B
high = min((self.i + 1) * self.B, self.N)
self.i += 1
return self.idxs[low:high]
def __next__(self):
if self.i >= self.num_batches:
raise StopIteration
batch_idxs = self._get_next_batch_idxs()
batch = {
"expected_rgbs": self.expected_rgbs[batch_idxs],
}
vertex_idxs_of_hit_faces_local = self.vertex_idxs_of_hit_faces[batch_idxs] # B x 3
barycentric_coords = self.barycentric_coords[batch_idxs] # B x 3
if self.feature_strategy == "efuncs":
efuncs_local = get_k_eigenfunc_vec_vals(self.features,
vertex_idxs_of_hit_faces_local,
barycentric_coords)
assert efuncs_local.dtype == torch.float32
batch["eigenfunctions"] = efuncs_local
elif self.feature_strategy in ("ff", "rff", "xyz"):
features_local = self.features[vertex_idxs_of_hit_faces_local] # B x 3 x 3
batch["xyz"] = torch.bmm(barycentric_coords.unsqueeze(1), features_local).squeeze(1) # B x 3
else:
raise ValueError(f"Unknown input feature strategy: {self.feature_strategy}")
if self.unit_ray_dirs is not None:
assert self.face_idxs is not None
batch["unit_ray_dirs"] = self.unit_ray_dirs[batch_idxs]
batch["hit_face_idxs"] = self.face_idxs[batch_idxs]
return batch
# TESTING
if __name__ == "__main__":
device = "cuda"
vertex_idxs_of_hit_faces = torch.tensor([[0, 1, 2],
[1, 2, 3],
[7, 8, 9],
[5, 6, 7],
[3, 4, 5]
])
barycentric = torch.tensor([[1, 0, 0],
[1, 0, 0],
[1, 0, 0],
[1, 0, 0],
[1, 0, 0]], dtype=torch.float32)
expected_rgbs = torch.ones((5, 3), dtype=torch.float32)
batch_size = 2
# Test Intrinsic Loading
k = 5
efuncs = torch.rand((10, k))
intrinsic_dataloader = RayDataLoader(efuncs,
"efuncs",
vertex_idxs_of_hit_faces,
barycentric,
expected_rgbs,
None,
None,
batch_size,
False,
True,
device=device)
total_elements = 0
for batch in intrinsic_dataloader:
assert (batch_size, k) == batch["eigenfunctions"].shape
total_elements += batch["eigenfunctions"].shape[0]
assert total_elements == ((barycentric.shape[0] // batch_size) * batch_size)
# Test Extrinsic Loading
vertices = torch.rand((10, 3))
extrinsic_dataloader = RayDataLoader(vertices,
"xyz",
vertex_idxs_of_hit_faces,
barycentric,
expected_rgbs,
None,
None,
batch_size,
False,
True,
device=device)
total_elements = 0
for batch in extrinsic_dataloader:
assert (batch_size, 3) == batch["xyz"].shape
total_elements += batch["xyz"].shape[0]
assert total_elements == ((barycentric.shape[0] // batch_size) * batch_size)