mirror of
https://github.com/nerfstudio-project/nerfstudio.git
synced 2026-12-02 05:48:57 +08:00
distortion optimization
This commit is contained in:
1 parent
60a8eafc3a
commit
b30e1c6ebb
2 files changed
+44
-30
No files matched your search
@@ -61,6 +61,12 @@ class CameraOptimizerConfig(InstantiateConfig):
|
|||||||
|
|
||||||
principal_point_penalty: float = 1e-2
|
principal_point_penalty: float = 1e-2
|
||||||
|
|
||||||
|
distortion_penalty: float = 1e-1
|
||||||
|
|
||||||
|
distortion_std_penalty: float = .1
|
||||||
|
|
||||||
|
start_focal_train: int = 3000
|
||||||
|
|
||||||
|
|
||||||
class CameraOptimizer(nn.Module):
|
class CameraOptimizer(nn.Module):
|
||||||
"""Layer that modifies camera poses to be optimized as well as the field during training."""
|
"""Layer that modifies camera poses to be optimized as well as the field during training."""
|
||||||
@@ -80,6 +86,7 @@ class CameraOptimizer(nn.Module):
|
|||||||
self.config = config
|
self.config = config
|
||||||
self.num_cameras = num_cameras
|
self.num_cameras = num_cameras
|
||||||
self.cameras = cameras
|
self.cameras = cameras
|
||||||
|
self.step = 0
|
||||||
if cameras is not None:
|
if cameras is not None:
|
||||||
self.c2ws = self.cameras.camera_to_worlds
|
self.c2ws = self.cameras.camera_to_worlds
|
||||||
self.Ks = torch.eye(3)
|
self.Ks = torch.eye(3)
|
||||||
@@ -107,7 +114,7 @@ class CameraOptimizer(nn.Module):
|
|||||||
raise ValueError("cameras must be provided to optimize focal length")
|
raise ValueError("cameras must be provided to optimize focal length")
|
||||||
# optimize fx, fy, cx, cy
|
# optimize fx, fy, cx, cy
|
||||||
self.K_adjustment = torch.nn.Parameter(torch.zeros((num_cameras, 4), device=device))
|
self.K_adjustment = torch.nn.Parameter(torch.zeros((num_cameras, 4), device=device))
|
||||||
# self.K_adjustment = torch.nn.Parameter(torch.zeros((4), device=device))
|
self.D_adjustment = torch.nn.Parameter(torch.zeros((num_cameras, 2), device=device))
|
||||||
|
|
||||||
def forward(
|
def forward(
|
||||||
self,
|
self,
|
||||||
@@ -148,28 +155,17 @@ class CameraOptimizer(nn.Module):
|
|||||||
if self.Ks.device != self.pose_adjustment.device:
|
if self.Ks.device != self.pose_adjustment.device:
|
||||||
self.Ks = self.Ks.to(self.pose_adjustment.device)
|
self.Ks = self.Ks.to(self.pose_adjustment.device)
|
||||||
self.Ksinv = self.Ksinv.to(self.pose_adjustment.device)
|
self.Ksinv = self.Ksinv.to(self.pose_adjustment.device)
|
||||||
K_cors = torch.eye(3, device=self.Ks.device)
|
K_cors = self.Ks[indices]
|
||||||
K_cors = K_cors[None, ...].repeat(indices.shape[0], 1, 1)
|
|
||||||
if self.config.optimize_focal:
|
if self.config.optimize_focal:
|
||||||
K_cors[:, 0, 0] *= torch.exp(self.K_adjustment[indices, 0])
|
K_cors[:, 0, 0] *= torch.exp(self.K_adjustment[indices, 0])
|
||||||
K_cors[:, 1, 1] *= torch.exp(self.K_adjustment[indices, 1])
|
K_cors[:, 1, 1] *= torch.exp(self.K_adjustment[indices, 1])
|
||||||
K_cors[:, 0, 2] += self.K_adjustment[indices, 2]
|
K_cors[:, 0, 2] += self.K_adjustment[indices, 2]
|
||||||
K_cors[:, 1, 2] += self.K_adjustment[indices, 3]
|
K_cors[:, 1, 2] += self.K_adjustment[indices, 3]
|
||||||
# optimize all as one
|
|
||||||
# if self.config.optimize_focal:
|
|
||||||
# K_cor[0, 0] *= torch.exp(self.K_adjustment[0])
|
|
||||||
# K_cor[1, 1] *= torch.exp(self.K_adjustment[1])
|
|
||||||
# K_cor[0, 2] += self.K_adjustment[2]
|
|
||||||
# K_cor[1, 2] += self.K_adjustment[3]
|
|
||||||
# K_cors = K_cor[None, ...].repeat(indices.shape[0], 1, 1)
|
|
||||||
return K_cors, pose_corrections
|
return K_cors, pose_corrections
|
||||||
|
|
||||||
def apply_to_raybundle(self, raybundle: RayBundle) -> None:
|
def apply_to_raybundle(self, raybundle: RayBundle) -> None:
|
||||||
"""Apply the pose correction to the raybundle"""
|
"""Apply the pose correction to the raybundle"""
|
||||||
if self.config.mode != "off":
|
if self.config.mode != "off":
|
||||||
if self.config.optimize_focal:
|
|
||||||
print("focal corr", self.K_adjustment.mean(dim=0), self.K_adjustment.std(dim=0))
|
|
||||||
# print("focal corr", self.K_adjustment)
|
|
||||||
K_cors, H_cors = self(raybundle.camera_indices.squeeze()) # type: ignore
|
K_cors, H_cors = self(raybundle.camera_indices.squeeze()) # type: ignore
|
||||||
if self.c2ws.device != K_cors.device:
|
if self.c2ws.device != K_cors.device:
|
||||||
self.c2ws = self.c2ws.to(K_cors.device)
|
self.c2ws = self.c2ws.to(K_cors.device)
|
||||||
@@ -181,28 +177,43 @@ class CameraOptimizer(nn.Module):
|
|||||||
H_cors = torch.cat([H_cors, rows], dim=1)
|
H_cors = torch.cat([H_cors, rows], dim=1)
|
||||||
onesvec = torch.ones((*raybundle.origins.shape[:-1], 1), device=raybundle.origins.device)
|
onesvec = torch.ones((*raybundle.origins.shape[:-1], 1), device=raybundle.origins.device)
|
||||||
# first multiply the H_cor * Hsinv
|
# first multiply the H_cor * Hsinv
|
||||||
|
|
||||||
Hcorinv = torch.bmm(H_cors, Hsinv)
|
Hcorinv = torch.bmm(H_cors, Hsinv)
|
||||||
raybundle.origins = torch.bmm(Hcorinv, torch.cat((raybundle.origins, onesvec), dim=-1)[..., None])
|
raybundle.origins = torch.bmm(Hcorinv, torch.cat((raybundle.origins, onesvec), dim=-1)[..., None])
|
||||||
# then multiply the Hs to get back to world frame
|
# then multiply the Hs to get back to world frame
|
||||||
raybundle.origins = torch.bmm(Hs, raybundle.origins)[..., :3, 0]
|
raybundle.origins = torch.bmm(Hs, raybundle.origins)[..., :3, 0]
|
||||||
|
|
||||||
Ks = self.Ks[raybundle.camera_indices.squeeze()]
|
Ks = self.Ks[raybundle.camera_indices.squeeze()]
|
||||||
Ksinv = self.Ksinv[raybundle.camera_indices.squeeze()]
|
|
||||||
# multiply by the H_cor * Hsinv to bring us into camera local frame (-z is camera axis)
|
# multiply by the H_cor * Hsinv to bring us into camera local frame (-z is camera axis)
|
||||||
raybundle.directions = torch.bmm(Hcorinv[..., :3, :3], raybundle.directions[..., None]).squeeze()
|
raybundle.directions = torch.bmm(Hcorinv[..., :3, :3], raybundle.directions[..., None]).squeeze()
|
||||||
# then apply the focal matrix to take it to focal plane space
|
if self.step > self.config.start_focal_train:
|
||||||
raybundle.directions = torch.bmm(Ks, raybundle.directions[..., None]).squeeze()
|
# then apply the focal matrix to take it to focal plane space
|
||||||
# divide by -z to normalize to focal plane
|
raybundle.directions = torch.bmm(Ks, raybundle.directions[..., None]).squeeze()
|
||||||
raybundle.directions = raybundle.directions / -raybundle.directions[..., 2:3].repeat(1, 3)
|
# divide by -z to normalize to focal plane
|
||||||
# then apply the correction
|
raybundle.directions = raybundle.directions / -raybundle.directions[..., 2:3].repeat(1, 3)
|
||||||
raybundle.directions = torch.bmm(K_cors, raybundle.directions[..., None]).squeeze()
|
# Transform the focal coordinates by the distortion corrections, only using k1 and k2, no tangential
|
||||||
# then apply the inverse of the focal matrix to get back to camera space
|
r = raybundle.directions[..., :2].norm(dim=-1, keepdim=True)
|
||||||
raybundle.directions = torch.bmm(Ksinv, raybundle.directions[..., None]).squeeze()
|
r2 = r**2.0
|
||||||
# then re-normalize
|
r4 = r2**2.0
|
||||||
raybundle.directions = raybundle.directions / raybundle.directions.norm(dim=-1, keepdim=True)
|
radial = (
|
||||||
|
1.0
|
||||||
|
+ r2 * self.D_adjustment[raybundle.camera_indices, 0]
|
||||||
|
+ r4 * self.D_adjustment[raybundle.camera_indices, 1]
|
||||||
|
)
|
||||||
|
raybundle.directions = raybundle.directions * torch.cat(
|
||||||
|
(radial, radial, torch.ones((*radial.shape[:-1], 1), device=radial.device, dtype=radial.dtype)), dim=-1
|
||||||
|
)
|
||||||
|
# then apply the inverse of the focal matrix to get back to camera space
|
||||||
|
raybundle.directions = torch.bmm(torch.inverse(K_cors), raybundle.directions[..., None]).squeeze()
|
||||||
|
# then re-normalize
|
||||||
|
raybundle.directions = raybundle.directions / raybundle.directions.norm(dim=-1, keepdim=True)
|
||||||
# Then apply the Hs to get back to world frame
|
# Then apply the Hs to get back to world frame
|
||||||
raybundle.directions = torch.bmm(Hs[..., :3, :3], raybundle.directions[..., None]).squeeze()
|
raybundle.directions = torch.bmm(Hs[..., :3, :3], raybundle.directions[..., None]).squeeze()
|
||||||
|
|
||||||
|
def set_step(self, step: int) -> None:
|
||||||
|
"""Set the step for the optimizer"""
|
||||||
|
self.step = step
|
||||||
|
|
||||||
def get_loss_dict(self, loss_dict: dict) -> None:
|
def get_loss_dict(self, loss_dict: dict) -> None:
|
||||||
"""Add regularization"""
|
"""Add regularization"""
|
||||||
if self.config.mode != "off":
|
if self.config.mode != "off":
|
||||||
@@ -215,13 +226,16 @@ class CameraOptimizer(nn.Module):
|
|||||||
self.config.focal_std_penalty = self.config.focal_std_penalty.to(self.K_adjustment.device)
|
self.config.focal_std_penalty = self.config.focal_std_penalty.to(self.K_adjustment.device)
|
||||||
loss_dict["camera_opt_std_regularizer"] = (
|
loss_dict["camera_opt_std_regularizer"] = (
|
||||||
self.K_adjustment.std(dim=0) * self.config.focal_std_penalty
|
self.K_adjustment.std(dim=0) * self.config.focal_std_penalty
|
||||||
).sum()
|
).mean()
|
||||||
loss_dict["camera_opt_principal_point_regularizer"] = (
|
loss_dict["camera_opt_principal_point_regularizer"] = (
|
||||||
self.K_adjustment.norm(dim=0)[2:].norm() * self.config.principal_point_penalty
|
self.K_adjustment.norm(dim=0)[2:].norm() * self.config.principal_point_penalty
|
||||||
)
|
)
|
||||||
# loss_dict["camera_opt_principal_point_regularizer"] = (
|
loss_dict["camera_opt_distortion_regularizer"] = (
|
||||||
# self.K_adjustment[2:].norm() * self.config.principal_point_penalty
|
self.D_adjustment.norm(dim=0).norm() * self.config.distortion_penalty
|
||||||
# )
|
)
|
||||||
|
loss_dict["camera_opt_distortion_std_regularizer"] = (
|
||||||
|
self.D_adjustment.std(dim=0) * self.config.distortion_std_penalty
|
||||||
|
).mean()
|
||||||
|
|
||||||
def get_correction_matrices(self):
|
def get_correction_matrices(self):
|
||||||
"""Get optimized pose correction matrices"""
|
"""Get optimized pose correction matrices"""
|
||||||
|
|||||||
@@ -116,7 +116,7 @@ method_configs["nerfacto"] = TrainerConfig(
|
|||||||
},
|
},
|
||||||
"camera_opt": {
|
"camera_opt": {
|
||||||
"optimizer": AdamOptimizerConfig(lr=1e-4, eps=1e-15),
|
"optimizer": AdamOptimizerConfig(lr=1e-4, eps=1e-15),
|
||||||
"scheduler": ExponentialDecaySchedulerConfig(lr_final=1e-5, max_steps=10000, warmup_steps=2500),
|
"scheduler": ExponentialDecaySchedulerConfig(lr_final=1e-5, max_steps=10000, warmup_steps=1000),
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
viewer=ViewerConfig(num_rays_per_chunk=1 << 15),
|
viewer=ViewerConfig(num_rays_per_chunk=1 << 15),
|
||||||
@@ -206,7 +206,7 @@ method_configs["nerfacto-huge"] = TrainerConfig(
|
|||||||
},
|
},
|
||||||
"camera_opt": {
|
"camera_opt": {
|
||||||
"optimizer": AdamOptimizerConfig(lr=1e-4, eps=1e-15),
|
"optimizer": AdamOptimizerConfig(lr=1e-4, eps=1e-15),
|
||||||
"scheduler": ExponentialDecaySchedulerConfig(lr_final=1e-4, max_steps=50000, warmup_steps=5000),
|
"scheduler": ExponentialDecaySchedulerConfig(lr_final=1e-5, max_steps=50000, warmup_steps=5000),
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
viewer=ViewerConfig(num_rays_per_chunk=1 << 15),
|
viewer=ViewerConfig(num_rays_per_chunk=1 << 15),
|
||||||
|
|||||||
Reference in new issue
Block a user