-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_utils.py
284 lines (242 loc) · 9.5 KB
/
data_utils.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
import glob
import os
import pickle as pkl
import random
import sys
import numpy as np
import torch
from pytorch3d.io import load_obj, load_objs_as_meshes
from pytorch3d.renderer import TexturesUV
from pytorch3d.structures import Meshes
from torchvision.utils import save_image
from tqdm import tqdm
def load_wavefront_file(obj_fn, device, offset=[0.0, 0.0, 0.0], scale=1):
verts, faces, aux = load_obj(obj_fn, device=device)
verts = verts.to(device)
verts = (verts * scale) + torch.Tensor(offset).to(device)
face_idxs = faces.verts_idx.to(device)
tex_map = aux.texture_images
if tex_map is not None and len(tex_map) > 0:
verts_uvs = aux.verts_uvs.to(device) # V, 2
faces_uvs = faces.textures_idx.to(device) # V, 2
image = list(tex_map.values())[0].to(device)[None]
tex = TexturesUV(
verts_uvs=[verts_uvs], faces_uvs=[faces_uvs], maps=image
)
else:
tex = None
mesh = Meshes(verts=[verts], faces=[face_idxs], textures=tex)
return verts, face_idxs, tex, mesh
def get_angle_diff(normals, pred_normals):
inner_prod = (normals * pred_normals).sum(dim=1, keepdim=True)
gt_norm = normals.pow(2).sum(dim=1, keepdim=True).pow(0.5)
pred_norm = pred_normals.pow(2).sum(dim=1, keepdim=True).pow(0.5)
cos_angles = inner_prod / (gt_norm * pred_norm)
angle_diff = torch.acos(cos_angles)
return angle_diff
def get_adjacency_matrix(mesh):
edges = mesh.detach().edges_packed().cpu()
adjacency_matrix = torch.zeros((edges.max() + 1, edges.max() + 1)).bool()
adjacency_matrix[edges[:, 0], edges[:, 1]] = 1
adjacency_matrix[edges[:, 1], edges[:, 0]] = 1
adjacency_matrix = adjacency_matrix.to(mesh.device)
return adjacency_matrix
def compute_vertex_normals(meshes):
faces_packed = meshes.faces_packed()
verts_packed = meshes.verts_packed()
verts_normals = torch.zeros_like(verts_packed)
verts_faces = verts_packed[faces_packed]
faces_normals = torch.cross(
verts_faces[:, 2] - verts_faces[:, 1],
verts_faces[:, 0] - verts_faces[:, 1],
dim=1,
)
verts_normals.index_add_(0, faces_packed[:, 0], faces_normals)
verts_normals.index_add_(0, faces_packed[:, 1], faces_normals)
verts_normals.index_add_(0, faces_packed[:, 2], faces_normals)
return torch.nn.functional.normalize(verts_normals, eps=1e-6, dim=1)
def batch_scale_mesh(cano_verts, cano_faces, tex, batch_scale):
batch_size = batch_scale.shape[0]
batch_verts = [cano_verts * scale[0] for scale in batch_scale]
batch_faces = [cano_faces] * batch_size
batch_tex = tex.extend(batch_size)
return Meshes(verts=batch_verts, faces=batch_faces, textures=batch_tex)
def get_vertex_covariance(meshes, adjacency_matrix, packed=True):
n_verts = adjacency_matrix.shape[0]
verts = meshes.verts_packed().reshape(-1, n_verts, 3) # B, N, 1
mask = torch.ones_like(verts[..., :1]) # B, N, 1
batch_adj_matrix = adjacency_matrix.unsqueeze(0).float() # 1, N, N
nb_sum = torch.matmul(batch_adj_matrix, verts) # B, N, 3
count = torch.matmul(batch_adj_matrix, mask) # B, N, 1
nb_mean = nb_sum / count # B, N, 3
edges = meshes.edges_packed() # BxE, 3
e0 = torch.cat([edges[:, 0], edges[:, 1]], dim=0)
e1 = torch.cat([edges[:, 1], edges[:, 0]], dim=0)
nb_verts = verts.reshape(-1, 3)[e1]
diff = nb_verts - nb_mean.reshape(-1, 3)[e0]
diff = diff.unsqueeze(-1) # B*E, 3
cov = torch.matmul(diff, diff.transpose(1, 2)) # B*E, 3, 3
verts_cov = torch.sparse_coo_tensor(
e0.unsqueeze(0),
cov,
size=(verts.shape[0] * verts.shape[1], 3, 3),
).coalesce()
verts_cov = verts_cov.to_dense() # B*N, 3, 3
verts_cov /= count.reshape(-1, 1, 1) # taking average over neighbours
if packed:
return verts_cov # B*N, 3, 3
else:
return verts_cov.reshape(verts.shape[0], verts.shape[1], 3, 3)
def depth2disparity(depth):
mask = (depth > 0).to(dtype=depth.dtype)
disparity = (1 / depth) * mask
disparity = disparity[:, 0, :, :]
return disparity
def rayleigh_quotient_curvature(meshes, adjacency_matrix):
"""RQ Curvature according to Garnet++
https://arxiv.org/pdf/2007.10867v1.pdf.
rq_curvature = g_T * cov * g / g_T * g
Arguments:
verts: vertex input (batch * N, 3)
verts_cov: vertex covariance(batch * N, 3, 3)
"""
verts = meshes.verts_packed()
verts_cov = get_vertex_covariance(meshes, adjacency_matrix, packed=True)
rq_curv = torch.matmul(
torch.matmul(verts.unsqueeze(1), verts_cov), verts.unsqueeze(-1)
) / torch.matmul(verts.unsqueeze(1), verts.unsqueeze(-1))
rq_curv = rq_curv.reshape(-1, adjacency_matrix.shape[0])
nb_rq_curv = (
rq_curv.unsqueeze(-1).repeat([1, 1, adjacency_matrix.shape[-1]])
* adjacency_matrix
)
min_rq_curv, _ = nb_rq_curv.min(-1)
max_rq_curv, _ = nb_rq_curv.max(-1)
return min_rq_curv, max_rq_curv
def get_riemannian_metric(meshes, packed=True):
verts = meshes.verts_packed()
faces = meshes.faces_packed()
n_faces = faces.shape[0]
alpha = torch.zeros((n_faces, 3, 2)).to(
dtype=verts.dtype, device=verts.device
)
V0, V1, V2 = (
verts.index_select(0, faces[:, 0]),
verts.index_select(0, faces[:, 1]),
verts.index_select(0, faces[:, 2]),
)
alpha[..., 0] = V1 - V0
alpha[..., 1] = V2 - V0
g = torch.matmul(alpha.transpose(1, 2), alpha)
if packed:
return g
else:
return g.reshape(len(meshes), -1, 2, 2)
def partition_data(in_dir, seed):
random.seed(seed)
files = glob.glob(f"{in_dir}/perturbation_mode_0/*.obj")
files += glob.glob(f"{in_dir}/perturbation_mode_1/*.obj")
random.shuffle(files)
for mode in ["train", "eval", "test"]:
if os.path.isdir(f"data/{mode}"):
os.system(f"rm -rf data/{mode}")
os.system(f"mkdir data/{mode}")
os.system(f"cp {in_dir}/*.jpg data/{mode}")
os.system(f"cp {in_dir}/*.mtl data/{mode}")
eval_start_idx = int(0.8 * len(files))
test_start_idx = int(0.9 * len(files))
for i, fn in enumerate(files[:eval_start_idx]):
os.system(f"ln -sf {os.path.abspath(fn)} data/train/{i}.obj")
for i, fn in enumerate(files[eval_start_idx:test_start_idx]):
os.system(f"ln -sf {os.path.abspath(fn)} data/eval/{i}.obj")
for i, fn in enumerate(files[test_start_idx:]):
os.system(f"ln -sf {os.path.abspath(fn)} data/test/{i}.obj")
def mesh2image(
device,
camera,
obj_files,
cano_obj_fn,
max_scale=1,
min_scale=1,
max_offset=0,
min_offset=0,
n_image_per_obj=1,
):
cano_verts, _, _, _ = load_wavefront_file(cano_obj_fn, device)
for obj_fn in tqdm(obj_files):
for idx in range(n_image_per_obj):
scale = torch.rand(1) * (max_scale - min_scale) + min_scale
scale = scale.to(device)
offset = torch.rand(3) * (max_offset - min_offset) + min_offset
offset = offset.to(device)
verts, face_idxs, tex, mesh = load_wavefront_file(
obj_fn, device, offset=offset, scale=scale
)
offsets = verts - cano_verts * scale
images = camera.render(mesh.extend(1))
prefix = obj_fn.replace(".obj", f"_{idx}")
with open(f"{prefix}_offset.pkl", "wb") as fp:
pkl.dump(
{
# "rgb": images["rgb"].detach().cpu().numpy(),
"scale": scale,
"depth": images["depth"].detach().cpu()[0],
# "normals": images["normals"].detach().cpu().numpy(),
"offsets": offsets.cpu(),
},
fp,
protocol=2,
)
save_image(
images["rgb"].detach().cpu().permute([0, 3, 1, 2])[:, :3, :, :],
f"{prefix}_rgb.png",
)
# Note: diffcloth is set in y-up coordinate system
# normals = (
# images["normals"].detach().cpu().permute([0, 3, 1, 2]) + 1.0
# ) / 2
# normals = normals[:, [0, 2, 1], :]
save_image(
(images["normals"].detach().cpu().permute([0, 3, 1, 2]) + 1.0)
/ 2,
# normals,
f"{prefix}_normals.png",
)
# generate training data for deform_net
if __name__ == "__main__":
if torch.cuda.is_available():
device = torch.device("cuda:0")
torch.cuda.set_device(device)
else:
device = torch.device("cpu")
from differentiable_rendering import CameraInterface, init_lighting
image_size = 256
cam_dist = 8.0
elevation = [30.0]
azimuth = [45.0]
lights = init_lighting(mode="point", device=device)
camera = CameraInterface(
device,
image_size,
cam_dist,
elevation,
azimuth,
lights,
mode=["rgb", "depth", "normals"],
)
# import sys
# base_dir = sys.argv[1]
base_dir = "/home/zyuwei/Projects/cloth_shape_estimation/data/"
cano_obj_fn = f"{base_dir}/textured_flat_cloth.obj"
for mode in ["eval", "test", "train"]:
if not os.path.isdir(f"{base_dir}/{mode}"):
print(f"==> Partition obj data in {base_dir} into train/eval/test")
partition_data(base_dir, seed=77)
print(f"==> {mode}")
obj_files = glob.glob(f"{base_dir}/{mode}/*.obj")
mesh2image(
device,
camera,
obj_files,
cano_obj_fn,
)