-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathutil.py
321 lines (265 loc) · 10.4 KB
/
util.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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
import numpy as np
import torch
from pyquaternion import Quaternion
from read_write_model import read_model
import torch.nn.functional as F
import torchvision
from torchvision import transforms as T
class Vgg19(torch.nn.Module):
def __init__(self, requires_grad=False):
super(Vgg19, self).__init__()
vgg_pretrained_features = torchvision.models.vgg19(pretrained=True).features
self.slice1 = torch.nn.Sequential()
self.slice2 = torch.nn.Sequential()
self.slice3 = torch.nn.Sequential()
self.slice4 = torch.nn.Sequential()
self.slice5 = torch.nn.Sequential()
for x in range(2):
self.slice1.add_module(str(x), vgg_pretrained_features[x])
for x in range(2, 7):
self.slice2.add_module(str(x), vgg_pretrained_features[x])
for x in range(7, 12):
self.slice3.add_module(str(x), vgg_pretrained_features[x])
for x in range(12, 21):
self.slice4.add_module(str(x), vgg_pretrained_features[x])
for x in range(21, 30):
self.slice5.add_module(str(x), vgg_pretrained_features[x])
if not requires_grad:
for param in self.parameters():
param.requires_grad = False
def forward(self, X):
h_relu1 = self.slice1(X)
h_relu2 = self.slice2(h_relu1)
h_relu3 = self.slice3(h_relu2)
h_relu4 = self.slice4(h_relu3)
h_relu5 = self.slice5(h_relu4)
out = [h_relu1, h_relu2, h_relu3, h_relu4, h_relu5]
return out
class VGGLoss(torch.nn.Module):
def __init__(self):
super(VGGLoss, self).__init__()
self.vgg = Vgg19().cuda()
self.criterion = torch.nn.L1Loss()
self.normalize = T.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
self.weights = [1.0/32, 1.0/16, 1.0/8, 1.0/4, 1.0]
def forward(self, x, y):
x_vgg, y_vgg = self.vgg(self.normalize(x)), self.vgg(self.normalize(y))
loss = 0
for i in range(len(x_vgg)):
loss += self.weights[i] * self.criterion(x_vgg[i], y_vgg[i].detach())
return loss
def get_camera_dict(data_dir):
colmap_cameras, colmap_images, colmap_points3D = read_model('{}/0'.format(data_dir), '.txt')
camera_dict = parse_camera_dict(colmap_cameras, colmap_images)
return camera_dict
def parse_camera_dict(colmap_cameras, colmap_images):
camera_dict = {}
for image_id in colmap_images:
image = colmap_images[image_id]
img_name = image.name
cam = colmap_cameras[image.camera_id]
# assert(cam.model == 'SIMPLE_PINHOLE')
img_size = [cam.width, cam.height]
params = list(cam.params)
qvec = list(image.qvec)
tvec = list(image.tvec)
# w, h, fx, fy, cx, cy, qvec, tvec
# camera_dict[img_name] = img_size + params + qvec + tvec
camera_dict[img_name] = {}
camera_dict[img_name]['img_size'] = img_size
try:
f, cx, cy = params
except:
f, cx, cy, _ = params
K = np.eye(4)
K[0, 0] = f
K[1, 1] = f
K[0, 2] = cx
K[1, 2] = cy
camera_dict[img_name]['K'] = list(K.flatten())
rot = Quaternion(qvec[0], qvec[1], qvec[2], qvec[3]).rotation_matrix
W2C = np.eye(4)
W2C[:3, :3] = rot
W2C[:3, 3] = np.array(tvec)
camera_dict[img_name]['W2C'] = list(W2C.flatten())
return camera_dict
def convert_pose(C2W):
flip_yz = np.eye(3)
flip_yz[0, 0] = -1
flip_yz[1, 1] = -1
C2W = np.matmul(C2W, flip_yz)
return C2W
def get_cam_param(img_id, camera_dict, data_dir):
img_name = img_id + '.png'
img_info = camera_dict[img_name]
K = np.array(img_info['K']).reshape((4, 4))
src_pts = np.load('{}/deca_out/{}/{}.npy'.format(data_dir, img_id, img_id))
x = src_pts[0, 0]
y = src_pts[0, 1]
r = src_pts[1, 1] - src_pts[0, 1]
f = K[0, 0] * 224 / r
cy = (K[1, 2] - y) / r * 224
cx = (K[0, 2] - x) / r * 224
focal_length = torch.zeros((1, 1), dtype=torch.float32)
focal_length[0, 0] = f
principal_point = torch.zeros((1, 2), dtype=torch.float32)
principal_point[0, 0] = cx
principal_point[0, 1] = cy
image_size = torch.zeros((1, 2), dtype=torch.float32)
image_size[0, 0] = 224
image_size[0, 1] = 224
return focal_length, principal_point, image_size
def vertex_normals(vertices, faces):
"""
:param vertices: [batch size, number of vertices, 3]
:param faces: [batch size, number of faces, 3]
:return: [batch size, number of vertices, 3]
"""
assert (vertices.ndimension() == 3)
assert (faces.ndimension() == 3)
assert (vertices.shape[0] == faces.shape[0])
assert (vertices.shape[2] == 3)
assert (faces.shape[2] == 3)
bs, nv = vertices.shape[:2]
bs, nf = faces.shape[:2]
device = vertices.device
normals = torch.zeros(bs * nv, 3).to(device)
faces = faces + (torch.arange(bs, dtype=torch.int32).to(device) * nv)[:, None, None] # expanded faces
vertices_faces = vertices.reshape((bs * nv, 3))[faces.long()]
faces = faces.reshape(-1, 3)
vertices_faces = vertices_faces.reshape(-1, 3, 3)
normals.index_add_(0, faces[:, 1].long(),
torch.cross(vertices_faces[:, 2] - vertices_faces[:, 1], vertices_faces[:, 0] - vertices_faces[:, 1]))
normals.index_add_(0, faces[:, 2].long(),
torch.cross(vertices_faces[:, 0] - vertices_faces[:, 2], vertices_faces[:, 1] - vertices_faces[:, 2]))
normals.index_add_(0, faces[:, 0].long(),
torch.cross(vertices_faces[:, 1] - vertices_faces[:, 0], vertices_faces[:, 2] - vertices_faces[:, 0]))
normals = F.normalize(normals, eps=1e-6, dim=1)
normals = normals.reshape((bs, nv, 3))
# pytorch only supports long and byte tensors for indexing
return normals
# borrowed from https://github.com/daniilidis-group/neural_renderer/blob/master/neural_renderer/vertices_to_faces.py
def face_vertices(vertices, faces):
"""
:param vertices: [batch size, number of vertices, 3]
:param faces: [batch size, number of faces, 3]
:return: [batch size, number of faces, 3, 3]
"""
assert (vertices.ndimension() == 3)
assert (faces.ndimension() == 3)
assert (vertices.shape[0] == faces.shape[0])
assert (vertices.shape[2] == 3)
assert (faces.shape[2] == 3)
bs, nv = vertices.shape[:2]
bs, nf = faces.shape[:2]
device = vertices.device
faces = faces + (torch.arange(bs, dtype=torch.int32).to(device) * nv)[:, None, None]
vertices = vertices.reshape((bs * nv, 3))
# pytorch only supports long and byte tensors for indexing
return vertices[faces.long()]
def dict2obj(d):
# if isinstance(d, list):
# d = [dict2obj(x) for x in d]
if not isinstance(d, dict):
return d
class C(object):
pass
o = C()
for k in d:
o.__dict__[k] = dict2obj(d[k])
return o
# ---------------------------- process/generate vertices, normals, faces
def generate_triangles(h, w, margin_x=2, margin_y=5, mask = None):
# quad layout:
# 0 1 ... w-1
# w w+1
#.
# w*h
triangles = []
for x in range(margin_x, w-1-margin_x):
for y in range(margin_y, h-1-margin_y):
triangle0 = [y*w + x, y*w + x + 1, (y+1)*w + x]
triangle1 = [y*w + x + 1, (y+1)*w + x + 1, (y+1)*w + x]
triangles.append(triangle0)
triangles.append(triangle1)
triangles = np.array(triangles)
triangles = triangles[:,[0,2,1]]
return triangles
def sph2cart(pts_sph, convention='lat-lng'):
"""Inverse of :func:`cart2sph`.
See :func:`cart2sph`.
"""
pts_sph = np.array(pts_sph)
# Validate inputs
is_one_point = False
if pts_sph.shape == (3,):
is_one_point = True
pts_sph = pts_sph.reshape(1, 3)
elif pts_sph.ndim != 2 or pts_sph.shape[1] != 3:
raise ValueError("Shape of input must be either (3,) or (n, 3)")
# Degrees?
_warn_degree(pts_sph[:, 1:])
# Convert to latitude-longitude convention, if necessary
if convention == 'lat-lng':
pts_r_lat_lng = pts_sph
elif convention == 'theta-phi':
pts_r_lat_lng = _convert_sph_conventions(
pts_sph, 'theta-phi_to_lat-lng')
else:
raise NotImplementedError(convention)
# Compute x, y and z
r = pts_r_lat_lng[:, 0]
lat = pts_r_lat_lng[:, 1]
lng = pts_r_lat_lng[:, 2]
z = r * np.sin(lat)
x = r * np.cos(lat) * np.cos(lng)
y = r * np.cos(lat) * np.sin(lng)
# Assemble and return
pts_cart = np.stack((x, y, z), axis=-1)
if is_one_point:
pts_cart = pts_cart.reshape(3)
return pts_cart
def _warn_degree(angles):
if (np.abs(angles) > 2 * np.pi).any():
logger.warning((
"Some input value falls outside [-2pi, 2pi]. You sure inputs are "
"in radians?"))
# borrow from https://github.com/xiumingzhang/xiuminglib
def gen_light_xyz(envmap_h, envmap_w, envmap_radius=1e2):
"""Additionally returns the associated solid angles, for integration.
"""
# OpenEXR "latlong" format
# lat = pi/2
# lng = pi
# +--------------------+
# | |
# | |
# +--------------------+
# lat = -pi/2
# lng = -pi
lat_step_size = np.pi / (envmap_h + 2)
lng_step_size = 2 * np.pi / (envmap_w + 2)
# Try to exclude the problematic polar points
lats = np.linspace(
np.pi / 2 - lat_step_size, -np.pi / 2 + lat_step_size, envmap_h)
lngs = np.linspace(
np.pi - lng_step_size, -np.pi + lng_step_size, envmap_w)
lngs, lats = np.meshgrid(lngs, lats)
# To Cartesian
rlatlngs = np.dstack((envmap_radius * np.ones_like(lats), lats, lngs))
rlatlngs = rlatlngs.reshape(-1, 3)
xyz = sph2cart(rlatlngs)
xyz = xyz.reshape(envmap_h, envmap_w, 3)
# Calculate the area of each pixel on the unit sphere (useful for
# integration over the sphere)
sin_colat = np.sin(np.pi / 2 - lats)
areas = 4 * np.pi * sin_colat / np.sum(sin_colat)
assert 0 not in areas, \
"There shouldn't be light pixel that doesn't contribute"
xyz = torch.from_numpy(xyz.astype(np.float32))
areas = torch.from_numpy(areas.astype(np.float32))
t = torch.tensor([[0., 0., 1.],
[1., 0., 0.],
[0., 1., 0.]], dtype=torch.float32)
xyz = torch.mm(xyz.reshape(-1, 3), t).reshape(envmap_h, envmap_w, 3)
return xyz, areas