-
Notifications
You must be signed in to change notification settings - Fork 4
/
utils.py
147 lines (117 loc) · 4.85 KB
/
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
# Utility functions
import os
import cv2
import copy
import torch
import random
import numpy as np
def sample_pdf(bins, weights, n_samples, det=False):
# This implementation is from NeRF
# bins: [B, T], old_z_vals
# weights: [B, T - 1], bin weights.
# return: [B, n_samples], new_z_vals
# Get pdf
weights = weights + 1e-5 # prevent nans
pdf = weights / torch.sum(weights, -1, keepdim=True)
cdf = torch.cumsum(pdf, -1)
cdf = torch.cat([torch.zeros_like(cdf[..., :1]), cdf], -1)
# Take uniform samples
if det:
u = torch.linspace(0. + 0.5 / n_samples, 1. - 0.5 / n_samples, steps=n_samples).to(weights.device)
u = u.expand(list(cdf.shape[:-1]) + [n_samples])
else:
u = torch.rand(list(cdf.shape[:-1]) + [n_samples]).to(weights.device)
# Invert CDF
u = u.contiguous()
inds = torch.searchsorted(cdf, u, right=True)
below = torch.max(torch.zeros_like(inds - 1), inds - 1)
above = torch.min((cdf.shape[-1] - 1) * torch.ones_like(inds), inds)
inds_g = torch.stack([below, above], -1) # (B, n_samples, 2)
matched_shape = [inds_g.shape[0], inds_g.shape[1], cdf.shape[-1]]
cdf_g = torch.gather(cdf.unsqueeze(1).expand(matched_shape), 2, inds_g)
bins_g = torch.gather(bins.unsqueeze(1).expand(matched_shape), 2, inds_g)
denom = (cdf_g[..., 1] - cdf_g[..., 0])
denom = torch.where(denom < 1e-5, torch.ones_like(denom), denom)
t = (u - cdf_g[..., 0]) / denom
samples = bins_g[..., 0] + t * (bins_g[..., 1] - bins_g[..., 0])
return samples
def get_GPU_mem():
num = torch.cuda.device_count()
mem, mems = 0, []
for i in range(num):
mem_free, mem_total = torch.cuda.mem_get_info(i)
mems.append(int(((mem_total - mem_free)/1024**3)*1000)/1000)
mem += mems[-1]
return mem, mems
def custom_meshgrid(*args):
# ref: https://pytorch.org/docs/stable/generated/torch.meshgrid.html?highlight=meshgrid#torch.meshgrid
if pver.parse(torch.__version__) < pver.parse('1.10'):
return torch.meshgrid(*args)
else:
return torch.meshgrid(*args, indexing='ij')
# Set seed for reproducibility
def seed_everything(seed):
random.seed(seed)
os.environ['PYTHONHASHSEED'] = str(seed)
np.random.seed(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
def safe_normalize(x, eps=1e-20):
return x / torch.sqrt(torch.clamp(torch.sum(x * x, -1, keepdim=True), min=eps))
def mse2psnr(x):
return -10. * torch.log(x) / torch.log(torch.Tensor([10.])).to(x)
def coordinates(voxel_dim, device: torch.device, flatten=True):
if type(voxel_dim) is int:
nx = ny = nz = voxel_dim
else:
nx, ny, nz = voxel_dim[0], voxel_dim[1], voxel_dim[2]
x = torch.arange(0, nx, dtype=torch.long, device=device)
y = torch.arange(0, ny, dtype=torch.long, device=device)
z = torch.arange(0, nz, dtype=torch.long, device=device)
x, y, z = torch.meshgrid(x, y, z, indexing="ij")
if not flatten:
return torch.stack([x, y, z], dim=-1)
return torch.stack((x.flatten(), y.flatten(), z.flatten()))
def get_sdf_loss(z_vals, target_d, predicted_sdf, truncation, mask=None):
predicted_sdf = predicted_sdf[...,None]
depth_mask = target_d > 0.
front_mask = (z_vals < (target_d - truncation))
# bask_mask = (z_vals > (target_d + truncation)) & depth_mask
front_mask = (front_mask | ((target_d < 0.) & (z_vals < 3.5)))
bound = (target_d - z_vals)
bound[target_d[:,0] < 0., :] = 10. # TODO: maybe use noisy depth for bound?
sdf_mask = (bound.abs() <= truncation) & depth_mask
if mask is not None:
#front_mask = (front_mask | (mask < 0.5))
sdf_mask = (sdf_mask & (mask > 0.5))
sum_of_samples = front_mask.sum(dim=-1) + sdf_mask.sum(dim=-1) + 1e-8
rays_w_depth = torch.count_nonzero(target_d)
fs_loss = (torch.max(torch.exp(-5. * predicted_sdf) - 1., predicted_sdf - bound).clamp(min=0.) * front_mask)
fs_loss = (fs_loss.sum(dim=-1) / sum_of_samples).sum() / rays_w_depth
sdf_loss = ((torch.abs(predicted_sdf - bound) * sdf_mask).sum(dim=-1) / sum_of_samples).sum() / rays_w_depth
return fs_loss, sdf_loss
def load_K_Rt_from_P(filename, P=None):
if P is None:
lines = open(filename).read().splitlines()
if len(lines) == 4:
lines = lines[1:]
lines = [[x[0], x[1], x[2], x[3]] for x in (x.split(" ") for x in lines)]
P = np.asarray(lines).astype(np.float32).squeeze()
out = cv2.decomposeProjectionMatrix(P)
K = out[0]
R = out[1]
t = out[2]
K = K / K[2, 2]
intrinsics = np.eye(4)
intrinsics[:3, :3] = K
pose = np.eye(4, dtype=np.float32)
pose[:3, :3] = R.transpose()
pose[:3, 3] = (t[:3] / t[3])[:, 0]
return intrinsics, pose
def cv2gl(c2w):
c2w = copy.deepcopy(c2w)
c2w[:, 1] *= -1
c2w[:, 2] *= -1
return c2w
def gl2cv(c2w):
return cv2gl(c2w)