Skip to content

Commit 0628c29

Browse files
author
Jianbo Ye
committed
better handling ply_color_mode=='rgb' when sh_degree>0
1 parent 6321db3 commit 0628c29

File tree

1 file changed

+15
-20
lines changed

1 file changed

+15
-20
lines changed

nerfstudio/scripts/exporter.py

+15-20
Original file line numberDiff line numberDiff line change
@@ -568,33 +568,28 @@ def main(self) -> None:
568568
map_to_tensors["ny"] = np.zeros(n, dtype=np.float32)
569569
map_to_tensors["nz"] = np.zeros(n, dtype=np.float32)
570570

571-
if model.config.sh_degree > 0:
571+
if self.ply_color_mode == "rgb":
572+
colors = torch.clamp(model.colors.clone(), 0.0, 1.0).data.cpu().numpy()
573+
colors = (colors * 255).astype(np.uint8)
574+
map_to_tensors["red"] = colors[:, 0]
575+
map_to_tensors["green"] = colors[:, 1]
576+
map_to_tensors["blue"] = colors[:, 2]
577+
elif self.ply_color_mode == "sh_coeffs":
572578
shs_0 = model.shs_0.contiguous().cpu().numpy()
573579
for i in range(shs_0.shape[1]):
574580
map_to_tensors[f"f_dc_{i}"] = shs_0[:, i, None]
575581

582+
if model.config.sh_degree > 0:
576583
if self.ply_color_mode == "rgb":
577584
CONSOLE.print(
578-
"Warning: model has higher level of spherical harmonics, option ply_color_mode is ignored."
585+
"Warning: model has higher level of spherical harmonics, ignoring them and only export rgb."
579586
)
580-
# transpose(1, 2) was needed to match the sh order in Inria version
581-
shs_rest = model.shs_rest.transpose(1, 2).contiguous().cpu().numpy()
582-
shs_rest = shs_rest.reshape((n, -1))
583-
for i in range(shs_rest.shape[-1]):
584-
map_to_tensors[f"f_rest_{i}"] = shs_rest[:, i, None]
585-
else:
586-
if self.ply_color_mode == "sh_coeffs":
587-
shs_0 = model.shs_0.contiguous().cpu().numpy()
588-
for i in range(shs_0.shape[1]):
589-
map_to_tensors[f"f_dc_{i}"] = RGB2SH(
590-
torch.clamp(model.colors.clone(), 0.0, 1.0).data.cpu().numpy()
591-
)[:, i]
592-
elif self.ply_color_mode == "rgb":
593-
colors = torch.clamp(model.colors.clone(), 0.0, 1.0).data.cpu().numpy()
594-
colors = (colors * 255).astype(np.uint8)
595-
map_to_tensors["red"] = colors[:, 0]
596-
map_to_tensors["green"] = colors[:, 1]
597-
map_to_tensors["blue"] = colors[:, 2]
587+
elif self.ply_color_mode == "sh_coeffs":
588+
# transpose(1, 2) was needed to match the sh order in Inria version
589+
shs_rest = model.shs_rest.transpose(1, 2).contiguous().cpu().numpy()
590+
shs_rest = shs_rest.reshape((n, -1))
591+
for i in range(shs_rest.shape[-1]):
592+
map_to_tensors[f"f_rest_{i}"] = shs_rest[:, i, None]
598593

599594
map_to_tensors["opacity"] = model.opacities.data.cpu().numpy()
600595

0 commit comments

Comments
 (0)