-
Notifications
You must be signed in to change notification settings - Fork 3
/
util.py
363 lines (304 loc) · 13.1 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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
import torch
import torch.nn as nn
import math
import numpy as np
from sklearn import preprocessing
#import OpenEXR, Imath, array
import torch.nn.functional as F
import os
import os.path as osp
import shutil
import spherical as S360
import supervision as L
def mkdirs(path):
'''Convenience function to make all intermediate folders in creating a directory'''
try:
os.makedirs(path)
except:
pass
def xavier_init(m):
'''Provides Xavier initialization for the network weights and
normally distributes batch norm params'''
classname = m.__class__.__name__
if (classname.find('Conv2d') != -1) or (classname.find('ConvTranspose2d') != -1):
nn.init.xavier_normal_(m.weight.data)
m.bias.data.fill_(0)
def save_checkpoint(state, is_best, filename):
'''Saves a training checkpoints'''
torch.save(state, filename)
if is_best:
basename = osp.basename(filename) # File basename
idx = filename.find(basename) # Index where path ends and basename begins
# Copy the file to a different filename in the same directory
shutil.copyfile(filename, osp.join(filename[:idx], 'model_best.pth'))
def load_partial_model(model, loaded_state_dict):
'''Loaded a save model, even if the model is not a perfect match. This will run even if there is are layers from the current network missing in the saved model.
However, layers without a perfect match will be ignored.'''
model_dict = model.state_dict()
pretrained_dict = {k : v for k,v in loaded_state_dict.items() if k in model_dict}
model_dict.update(pretrained_dict)
model.load_state_dict(model_dict)
# Freeze Unfreeze Function
# freeze_layer ----------------------
def freeze_layer(layer):
for param in layer.parameters():
param.requires_grad = False
# Unfreeze_layer --------------------
def unfreeze_layer(layer):
for param in layer.parameters():
param.requires_grad = True
def load_optimizer(optimizer, loaded_optimizer_dict, device):
'''Loads the saved state of the optimizer and puts it back on the GPU if necessary. Similar to loading the partial model, this will load only the optimization parameters that match the current parameterization.'''
optimizer_dict = optimizer.state_dict()
pretrained_dict = {k : v for k,v in loaded_optimizer_dict.items()
if k in optimizer_dict and k != 'param_groups'}
optimizer_dict.update(pretrained_dict)
optimizer.load_state_dict(optimizer_dict)
for state in optimizer.state.values():
for k, v in state.items():
if torch.is_tensor(v):
state[k] = v.to(device)
def set_caffe_param_mult(m, base_lr, base_weight_decay):
'''Function that allows us to assign a LR multiplier of 2 and a decay multiplier of 0 to the bias weights (which is common in Caffe)'''
param_list = []
for name, params in m.named_parameters():
if name.find('bias') != -1:
param_list.append({'params' : params, 'lr' : 2 * base_lr, 'weight_decay' : 0.0})
else:
param_list.append({'params' : params, 'lr' : base_lr, 'weight_decay' : base_weight_decay})
return param_list
def coords2uv(coords, w, h):
#output uv size w*h*2
uv = np.zeros_like(coords, dtype = np.float32)
middleX = w/2 + 0.5
middleY = h/2 + 0.5
uv[..., 0] = (coords[...,0] - middleX) / w * 2 * np.pi
uv[..., 1] = -(coords[...,1] - middleY) / h * np.pi
return uv
def uv2xyz(uv):
xyz = np.zeros((uv.shape[0], 3), dtype = np.float32)
xyz[:, 0] = np.multiply(np.cos(uv[:, 1]), np.sin(uv[:, 0]))
xyz[:, 1] = np.multiply(np.cos(uv[:, 1]), np.cos(uv[:, 0]))
xyz[:, 2] = np.sin(uv[:, 1])
return xyz
def xyz2uv(xyz):
normXY = torch.sqrt(xyz[:, :, 0]*xyz[:, :, 0] + xyz[:, :, 1]*xyz[:, :, 1])
normXY[normXY < 1e-6] = 1e-6
normXYZ = torch.sqrt(xyz[:, :, 0]*xyz[:, :, 0] + xyz[:, :, 1]*xyz[:, :, 1] + xyz[:, :, 2]*xyz[:, :, 2])
v = torch.asin(xyz[:,:,2]/normXYZ)
u = torch.asin(xyz[:,:,0]/normXY)
valid = (xyz[:, :, 1] < 0) * ( u >= 0)
u[valid] = math.pi - u[valid]
valid = (xyz[:, :, 1] < 0) * ( u <= 0)
u[valid] = -math.pi - u[valid]
uv = torch.cat([u.unsqueeze(-1), v.unsqueeze(-1)], -1)
uv[uv!=uv] = 0 #remove nan
return uv
def uv2coords(uv, w, h):
coords = torch.zeros_like(uv, dtype = torch.float32, device = 'cuda:0')
coords[...,0] = (uv[...,0] + math.pi)/2/math.pi * w + 0.5
coords[...,1] = (math.pi/2 - uv[...,1])/math.pi * h + 0.5
coords[...,0] = torch.min(coords[...,0], torch.cuda.FloatTensor([w]))
coords[...,1] = torch.min(coords[...,1], torch.cuda.FloatTensor([h]))
return coords
def chamfer_distance_with_batch(p1, p2, debug=False):
'''
Calculate Chamfer Distance between two point sets
:param p1: size[B, N, D]
:param p2: size[B, M, D]
:param debug: whether need to output debug info
:return: sum of all batches of Chamfer Distance of two point sets
'''
assert p1.size(0) == p2.size(0) and p1.size(2) == p2.size(2)
if debug:
print(p1[0])
p1 = p1.unsqueeze(1)
p2 = p2.unsqueeze(1)
if debug:
print('p1 size is {}'.format(p1.size()))
print('p2 size is {}'.format(p2.size()))
print(p1[0][0])
p1 = p1.repeat(1, p2.size(2), 1, 1)
if debug:
print('p1 size is {}'.format(p1.size()))
p1 = p1.transpose(1, 2)
if debug:
print('p1 size is {}'.format(p1.size()))
print(p1[0][0])
p2 = p2.repeat(1, p1.size(1), 1, 1)
if debug:
print('p2 size is {}'.format(p2.size()))
print(p2[0][0])
dist = torch.add(p1, torch.neg(p2))
if debug:
print('dist size is {}'.format(dist.size()))
print(dist[0])
dist = torch.norm(dist, 2, dim=3)
if debug:
print('dist size is {}'.format(dist.size()))
print(dist)
dist = torch.min(dist, dim=2)[0]
if debug:
print('dist size is {}'.format(dist.size()))
print(dist)
dist = torch.sum(dist)
if debug:
print('-------')
print(dist)
return dist
def map_coordinates(input, coordinates):
''' PyTorch version of scipy.ndimage.interpolation.map_coordinates
input: (H, W)
coordinates: (2, ...)
'''
h = input.shape[0]
w = input.shape[1]
def _coordinates_pad_wrap(h, w, coordinates):
coordinates[0] = coordinates[0] % h
coordinates[1] = coordinates[1] % w
return coordinates
co_floor = torch.floor(coordinates).long()
co_ceil = torch.ceil(coordinates).long()
d1 = (coordinates[1] - co_floor[1].float())
d2 = (coordinates[0] - co_floor[0].float())
co_floor = _coordinates_pad_wrap(h, w, co_floor)
co_ceil = _coordinates_pad_wrap(h, w, co_ceil)
f00 = input[co_floor[0], co_floor[1]]
f10 = input[co_floor[0], co_ceil[1]]
f01 = input[co_ceil[0], co_floor[1]]
f11 = input[co_ceil[0], co_ceil[1]]
fx1 = f00 + d1 * (f10 - f00)
fx2 = f01 + d1 * (f11 - f01)
output = fx1 + d2 * (fx2 - fx1)
return output
def depth2normal(depth, h=256, w=512):
#depth image size 256x512
#return normal image 256x512x3
#return curvature image 256x512
coords = np.stack(np.meshgrid(range(512), range(256)), -1)
coords = np.reshape(coords, [-1, 2])
coords += 1
uv = coords2uv(coords, 512, 256)
xyz = uv2xyz(uv) #3d coordinates on a sphere
depth = np.reshape(depth, [512*256, 1])
depth = np.tile(depth, [1, 3])
newxyz = np.multiply(xyz, depth)
reshape_xyz = np.reshape(newxyz, [256, 512, 3])
reshape_xyz = np.pad(reshape_xyz, ((1,1),(1,1),(0,0)), 'edge')
vec0 = reshape_xyz[:h, 1:-1, :] - reshape_xyz[2:, 1:-1, :]
vec2 = reshape_xyz[1:-1, :w, :] - reshape_xyz[1:-1, 2:, :]
vec4 = reshape_xyz[2:, 1:-1, :] - reshape_xyz[:h, 1:-1, :]
vec6 = reshape_xyz[1:-1, 2:, :] - reshape_xyz[1:-1, :w, :]
normal = preprocessing.normalize(np.cross(np.reshape(vec2, [w*h, 3]), np.reshape(vec0, [w*h, 3])), norm = 'l2')
normal += preprocessing.normalize(np.cross(np.reshape(vec4, [w*h, 3]), np.reshape(vec2, [w*h, 3])), norm = 'l2')
normal += preprocessing.normalize(np.cross(np.reshape(vec6, [w*h, 3]), np.reshape(vec4, [w*h, 3])), norm = 'l2')
normal += preprocessing.normalize(np.cross(np.reshape(vec0, [w*h, 3]), np.reshape(vec6, [w*h, 3])), norm = 'l2')
normal = preprocessing.normalize(normal, norm='l2')
normal = np.reshape(normal, [256, 512, 3])
reshape_normal = np.pad(normal, ((1,1),(1,1),(0,0)), 'edge')
n1 = reshape_normal[:h, 1:-1, :]
n2 = reshape_normal[2:, 1:-1, :]
n3 = reshape_normal[1:-1, :w, :]
n4 = reshape_normal[1:-1, 2:, :]
cur = (1 - np.einsum('ij,ij->i', np.reshape(n1, [w*h, 3]), np.reshape(n2, [w*h, 3])))/2
cur += (1 - np.einsum('ij,ij->i', np.reshape(n3, [w*h, 3]), np.reshape(n4, [w*h, 3])))/2
cur = cur/2
cur = np.reshape(cur, [256, 512])
cur[cur<1e-6] = 0
normal = (normal+1)/2
return normal, cur
def depth2normal_gpu(depth):
"""
input: depth image, size Bx1xHxW
output: derived boundary
"""
batch, c, h, w = depth.shape
depth = depth.view(batch, -1, 1)
coords = np.stack(np.meshgrid(range(512), range(256)), -1)
coords = np.reshape(coords, [-1, 2])
coords += 1
uv = coords2uv(coords, 512, 256)
xyz = uv2xyz(uv) #3d coordinates on a sphere
xyz = torch.from_numpy(xyz).cuda()
xyz = xyz.unsqueeze(0).repeat(batch, 1, 1)
newxyz = xyz * depth
vertices = newxyz.view(batch, 256, 512, 3).permute(0, 3, 1, 2)
vec0_pad = F.pad(vertices[:, :, :, :-1] - vertices[:, :, :, 1:], pad=[0, 1, 0, 0, 0, 0, 0, 0], mode='constant', value=0)
vec2_pad = F.pad(vertices[:, :, :-1, :] - vertices[:, :, 1:, :], pad=[0, 0, 0, 1, 0, 0, 0, 0], mode='constant', value=0)
vec4_pad = F.pad(vertices[:, :, :, 1:] - vertices[:, :, :, :-1], pad=[1, 0, 0, 0, 0, 0, 0, 0], mode='constant', value=0)
vec6_pad = F.pad(vertices[:, :, 1:, :] - vertices[:, :, :-1, :], pad=[0, 0, 1, 0, 0, 0, 0, 0], mode='constant', value=0)
# 4 connect
cross20 = torch.cross(vec2_pad, vec0_pad)
cross42 = torch.cross(vec4_pad, vec2_pad)
cross64 = torch.cross(vec6_pad, vec4_pad)
cross06 = torch.cross(vec0_pad, vec6_pad)
# normalmap = cross20
normalmap = F.normalize(cross20)
normalmap += F.normalize(cross42)
normalmap += F.normalize(cross64)
normalmap += F.normalize(cross06)
normalmap = F.normalize(normalmap)
# scale the values from (-1, 1) to (0, 1)
#normalmap = (-normalmap + 1) / 2
reshape_normal = F.pad(normalmap, (1,1,1,1), 'constant', 0)
n1 = reshape_normal[:, :, :h, 1:-1].contiguous().view(batch, 3, -1)
n2 = reshape_normal[:, :, 2:, 1:-1].contiguous().view(batch, 3, -1)
n3 = reshape_normal[:, :, 1:-1, :w].contiguous().view(batch, 3, -1)
n4 = reshape_normal[:, :, 1:-1, 2:].contiguous().view(batch, 3, -1)
cur = 1 - torch.einsum('bij,bij->bj', n1, n2)/2
cur += 1 - torch.einsum('bij,bij->bj', n3, n4)/2
cur /= 2
cur = cur.view(batch, 256, 512)
return normalmap, cur
def dibr_vertical(depth, image, uvgrid, sgrid, baseline):
#depth = remove_flying_pixel(depth)
disp = torch.cat(
(
torch.zeros_like(depth),
S360.derivatives.dtheta_vertical(sgrid, depth, baseline)
),
dim=1
)
render_coords = uvgrid + disp
render_coords[torch.isnan(render_coords)] = 0
render_coords[torch.isinf(render_coords)] = 0
rendered,_ = L.splatting.render(image, depth, render_coords, max_depth=8)
return rendered
def dibr_horizontal(depth, image, uvgrid, sgrid, baseline):
#depth = remove_flying_pixel(depth)
disp = torch.cat(
(
S360.derivatives.dphi_horizontal_clip(sgrid, depth, baseline),
S360.derivatives.dtheta_horizontal_clip(sgrid, depth, baseline)
),
dim=1
)
render_coords = uvgrid + disp
render_coords[:, 0, :, :] = torch.fmod(render_coords[:, 0, :, :] + 512, 512)
render_coords[torch.isnan(render_coords)] = 0
render_coords[torch.isinf(render_coords)] = 0
rendered, _ = L.splatting.render(image, depth, render_coords, max_depth=8)
return rendered
def get_sobel_kernel(k=3):
# get range
range = np.linspace(-(k // 2), k // 2, k)
# compute a grid the numerator and the axis-distances
x, y = np.meshgrid(range, range)
sobel_2D_numerator = x
sobel_2D_denominator = (x ** 2 + y ** 2)
sobel_2D_denominator[:, k // 2] = 1 # avoid division by zero
sobel_2D = sobel_2D_numerator / sobel_2D_denominator
return sobel_2D
def remove_flying_pixel(depth, th=5):
sobel_x = get_sobel_kernel()
sobel_y = sobel_x.T
sobel_x = torch.from_numpy(sobel_x).cuda().float()
sobel_y = torch.from_numpy(sobel_y).cuda().float()
sobel_x = nn.Parameter(sobel_x.unsqueeze(0).unsqueeze(0), requires_grad=False)
sobel_y = nn.Parameter(sobel_y.unsqueeze(0).unsqueeze(0), requires_grad=False)
gx = F.conv2d(depth, sobel_x, padding=1)
gy = F.conv2d(depth, sobel_y, padding=1)
edge = (gx ** 2 + gy ** 2) ** 0.5
mask = (edge < th).float()
depth *= mask
return depth