-
Notifications
You must be signed in to change notification settings - Fork 3
/
loc_inference.py
194 lines (142 loc) · 7.28 KB
/
loc_inference.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
#
# Copyright (C) 2023, Inria
# GRAPHDECO research group, https://team.inria.fr/graphdeco
# All rights reserved.
#
# This software is free for non-commercial, research and evaluation use
# under the terms of the LICENSE.md file.
#
# For inquiries contact [email protected]
#
import cv2
import numpy as np
import time
import torch
import torch.optim as optim
from scene import Scene
from tqdm import tqdm
from gaussian_renderer import render
from utils.general_utils import safe_state
from argparse import ArgumentParser
from arguments import ModelParams, PipelineParams, get_combined_args
from gaussian_renderer import GaussianModel
from utils.graphics_utils import getWorld2View2, fov2focal
from encoders.XFeat.modules.xfeat import XFeat
from warping.warping_loss import *
from warping.warp_utils import *
from utils.loc_utils import *
import warnings
warnings.filterwarnings("ignore", category=DeprecationWarning)
def localize_set(model_path, name, views, gaussians, pipeline, background, args):
# Keep track of rotation and translation errors for calculation of the median error.
rErrs = []
tErrs = []
prior_rErr = []
prior_tErr = []
xfeat = XFeat()
gaussian_pcd = gaussians.get_xyz
gaussian_feat = gaussians.get_semantic_feature.squeeze(1)
for _, view in enumerate(tqdm(views, desc="Rendering progress")):
gt_im = view.original_image[0:3, :, :]
# Extract sparse features
gt_keypoints, _, gt_feature = xfeat.detectAndCompute(gt_im[None],
top_k=args.top_k)[0].values()
# Define intrinsic matrix
K = np.eye(3)
focal_length = fov2focal(view.FoVx, view.image_width)
K[0, 0] = K[1, 1] = focal_length
K[0, 2] = view.image_width / 2
K[1, 2] = view.image_height / 2
start = time.time()
# Find initial pose prior via 2D-3D matching
with torch.no_grad():
matched_3d, matched_2d = find_2d3d_correspondences(
gt_keypoints,
gt_feature,
gaussian_pcd,
gaussian_feat
)
gt_R = view.R
gt_t = view.T
print(f"Match speed: {time.time() - start}")
_, R, t, _ = cv2.solvePnPRansac(matched_3d, matched_2d,
K,
distCoeffs=None,
flags=cv2.SOLVEPNP_ITERATIVE,
iterationsCount=args.ransac_iters
)
R, _ = cv2.Rodrigues(R)
# Calculate the rotation and translation errors using existing function
rotError, transError = calculate_pose_errors(gt_R, gt_t, R.T, t)
# Print the errors
print(f"Rotation Error: {rotError} deg")
print(f"Translation Error: {transError} cm")
prior_rErr.append(rotError)
prior_tErr.append(transError)
w2c = torch.eye(4, 4, device='cuda')
w2c[:3, :3] = torch.from_numpy(R).float()
w2c[:3, 3] = torch.from_numpy(t[:, 0]).float()
# Update the view's pose
view.update_RT(R.T, t[:,0])
# Render from the current estimated pose
with torch.no_grad():
render_pkg = render(view, gaussians, pipeline, background)
render_im = render_pkg["render"]
depth = render_pkg["depth"]
quat_opt = rotmat2qvec_tensor(w2c[:3, :3].clone()).view([4]).to(w2c.device)
t_opt = w2c[:3, 3].clone()
optimizer = optim.Adam([quat_opt.requires_grad_(True),
t_opt.requires_grad_(True)], lr=args.warp_lr)
for i in range(args.warp_iters):
# Compute warp loss for optimizing w2c_opt
optimizer.zero_grad()
loss = compute_warping_loss(vr=render_im,
qr=gt_im,
quat_opt=quat_opt,
t_opt=t_opt,
pose=w2c,
K=torch.from_numpy(K).float().to('cuda'),
depth=depth)
loss.backward()
optimizer.step()
if i % (args.warp_iters // 5) == 0:
print(f"Iteration {i}, Loss: {loss.item():.4f}")
# After optimization, update the view's pose
R_est = qvec2rotmat_tensor(quat_opt).detach().cpu().numpy()
t_est = t_opt.detach().cpu().numpy()
# Compute final errors
rotError, transError = calculate_pose_errors(gt_R, gt_t, R_est.T, t_est.reshape(3,1))
print(f"Iteration {i} Rotation Error: {rotError:.2f} deg, Translation Error: {transError:.2f} cm")
# After optimization, update the view's pose
R_est = qvec2rotmat_tensor(quat_opt).detach().cpu().numpy()
t_est = t_opt.detach().cpu().numpy()
# Compute final errors
rotError, transError = calculate_pose_errors(gt_R, gt_t, R_est.T, t_est.reshape(3,1))
print(f"Final Rotation Error: {rotError:.2f} deg, Translation Error: {transError:.2f} cm")
rErrs.append(rotError)
tErrs.append(transError)
print(f"Processed: {view.uid}")
log_errors(model_path, name, prior_rErr, prior_tErr, f"prior")
log_errors(model_path, name, rErrs, tErrs, "warp")
def launch_inference(dataset : ModelParams, pipeline : PipelineParams, args):
gaussians = GaussianModel(dataset.sh_degree)
scene = Scene(dataset, gaussians, load_iteration=args.iteration, shuffle=False)
bg_color = [1,1,1] if dataset.white_background else [0, 0, 0]
background = torch.tensor(bg_color, dtype=torch.float32, device="cuda")
localize_set(dataset.model_path, "test", scene.getTestCameras(), gaussians, pipeline, background, args)
if __name__ == "__main__":
# Set up command line argument parser
parser = ArgumentParser(description="Testing script parameters")
model = ModelParams(parser, sentinel=True)
pipeline = PipelineParams(parser)
parser.add_argument("--quiet", action="store_true")
parser.add_argument("--iteration", default=-1, type=int)
parser.add_argument("--top_k", default=1_000, type=int)
parser.add_argument("--ransac_iters", default=20000, type=int)
parser.add_argument("--warp_lr", default=0.0005, type=float)
parser.add_argument("--warp_iters", default=251, type=int)
args = get_combined_args(parser)
print("Rendering " + args.model_path)
# Initialize system state (RNG)
safe_state(args.quiet)
launch_inference(model.extract(args), pipeline.extract(args), args)