Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1,267 changes: 1,267 additions & 0 deletions examples/simple_trainer_3dcs.py

Large diffs are not rendered by default.

224 changes: 148 additions & 76 deletions examples/simple_viewer.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from pathlib import Path
from gsplat._helper import load_test_data
from gsplat.distributed import cli
from gsplat.rendering import rasterization
from gsplat.rendering import rasterization, rasterization_3dcs

from nerfview import CameraState, RenderTabState, apply_float_colormap
from gsplat_viewer import GsplatViewer, GsplatRenderTabState
Expand Down Expand Up @@ -101,55 +101,81 @@ def main(local_rank: int, world_rank, world_size: int, args):
)
else:
means, quats, scales, opacities, sh0, shN = [], [], [], [], [], []
for ckpt_path in args.ckpt:
ckpt = torch.load(ckpt_path, map_location=device)["splats"]
means.append(ckpt["means"])
quats.append(F.normalize(ckpt["quats"], p=2, dim=-1))
scales.append(torch.exp(ckpt["scales"]))
opacities.append(torch.sigmoid(ckpt["opacities"]))
sh0.append(ckpt["sh0"])
shN.append(ckpt["shN"])
means = torch.cat(means, dim=0)
quats = torch.cat(quats, dim=0)
scales = torch.cat(scales, dim=0)
opacities = torch.cat(opacities, dim=0)
sh0 = torch.cat(sh0, dim=0)
shN = torch.cat(shN, dim=0)
colors = torch.cat([sh0, shN], dim=-2)
sh_degree = int(math.sqrt(colors.shape[-2]) - 1)

# # crop
# aabb = torch.tensor((-1.0, -1.0, -1.0, 1.0, 1.0, 0.7), device=device)
# edges = aabb[3:] - aabb[:3]
# sel = ((means >= aabb[:3]) & (means <= aabb[3:])).all(dim=-1)
# sel = torch.where(sel)[0]
# means, quats, scales, colors, opacities = (
# means[sel],
# quats[sel],
# scales[sel],
# colors[sel],
# opacities[sel],
# )
convex_points, delta, sigma, num_points_per_convex, cumsum_of_points_per_convex = [], [], [], [], []
if args.backend == "3dcs":
for ckpt_path in args.ckpt:
hyperparam = torch.load(os.path.join(ckpt_path, "hyperparameters.pt"), map_location=device, weights_only=False)
pc = torch.load(os.path.join(ckpt_path, "point_cloud_state_dict.pt"), map_location=device, weights_only=False)
convex_points.append(pc['convex_points'])
delta.append(torch.exp(pc['delta']))
sigma.append(torch.exp(pc['sigma']))
opacities.append(torch.sigmoid(pc["opacity"]).squeeze())
num_points_per_convex.append(torch.tensor([6]))
cumsum_of_points_per_convex.append(hyperparam["cumsum_of_points_per_convex"])
sh0.append(pc["features_dc"])
shN.append(pc["features_rest"])
convex_points = torch.cat(convex_points, dim=0)
delta = torch.cat(delta, dim=0)
sigma = torch.cat(sigma, dim=0)
num_points_per_convex = torch.cat(num_points_per_convex, dim=0)
cumsum_of_points_per_convex = torch.cat(cumsum_of_points_per_convex, dim=0)
opacities = torch.cat(opacities, dim=0)
sh0 = torch.cat(sh0, dim=0)
shN = torch.cat(shN, dim=0)
colors = torch.cat([sh0, shN], dim=-2)
sh_degree = int(pc["active_sh_degree"])
print("Number of 3D convexes:", convex_points.shape[0]*convex_points.shape[1])
else:
for ckpt_path in args.ckpt:
ckpt = torch.load(ckpt_path, map_location=device)["splats"]
means.append(ckpt["means"])
quats.append(F.normalize(ckpt["quats"], p=2, dim=-1))
scales.append(torch.exp(ckpt["scales"]))
opacities.append(torch.sigmoid(ckpt["opacities"]))
sh0.append(ckpt["sh0"])
shN.append(ckpt["shN"])
means = torch.cat(means, dim=0)
quats = torch.cat(quats, dim=0)
scales = torch.cat(scales, dim=0)
opacities = torch.cat(opacities, dim=0)
sh0 = torch.cat(sh0, dim=0)
shN = torch.cat(shN, dim=0)
colors = torch.cat([sh0, shN], dim=-2)
sh_degree = int(math.sqrt(colors.shape[-2]) - 1)

# # crop
# aabb = torch.tensor((-1.0, -1.0, -1.0, 1.0, 1.0, 0.7), device=device)
# edges = aabb[3:] - aabb[:3]
# sel = ((means >= aabb[:3]) & (means <= aabb[3:])).all(dim=-1)
# sel = torch.where(sel)[0]
# means, quats, scales, colors, opacities = (
# means[sel],
# quats[sel],
# scales[sel],
# colors[sel],
# opacities[sel],
# )

# # repeat the scene into a grid (to mimic a large-scale setting)
# repeats = args.scene_grid
# gridx, gridy = torch.meshgrid(
# [
# torch.arange(-(repeats // 2), repeats // 2 + 1, device=device),
# torch.arange(-(repeats // 2), repeats // 2 + 1, device=device),
# ],
# indexing="ij",
# )
# grid = torch.stack([gridx, gridy, torch.zeros_like(gridx)], dim=-1).reshape(
# -1, 3
# )
# means = means[None, :, :] + grid[:, None, :] * edges[None, None, :]
# means = means.reshape(-1, 3)
# quats = quats.repeat(repeats**2, 1)
# scales = scales.repeat(repeats**2, 1)
# colors = colors.repeat(repeats**2, 1, 1)
# opacities = opacities.repeat(repeats**2)
print("Number of Gaussians:", len(means))
# # repeat the scene into a grid (to mimic a large-scale setting)
# repeats = args.scene_grid
# gridx, gridy = torch.meshgrid(
# [
# torch.arange(-(repeats // 2), repeats // 2 + 1, device=device),
# torch.arange(-(repeats // 2), repeats // 2 + 1, device=device),
# ],
# indexing="ij",
# )
# grid = torch.stack([gridx, gridy, torch.zeros_like(gridx)], dim=-1).reshape(
# -1, 3
# )
# means = means[None, :, :] + grid[:, None, :] * edges[None, None, :]
# means = means.reshape(-1, 3)
# quats = quats.repeat(repeats**2, 1)
# scales = scales.repeat(repeats**2, 1)
# colors = colors.repeat(repeats**2, 1, 1)
# opacities = opacities.repeat(repeats**2)
print("Number of Gaussians:", len(means))

# register and open viewer
@torch.no_grad()
Expand All @@ -174,34 +200,74 @@ def viewer_render_fn(camera_state: CameraState, render_tab_state: RenderTabState
"alpha": "RGB",
}

render_colors, render_alphas, info = rasterization(
means, # [N, 3]
quats, # [N, 4]
scales, # [N, 3]
opacities, # [N]
colors, # [N, S, 3]
viewmat[None], # [1, 4, 4]
K[None], # [1, 3, 3]
width,
height,
sh_degree=(
min(render_tab_state.max_sh_degree, sh_degree)
if sh_degree is not None
else None
),
near_plane=render_tab_state.near_plane,
far_plane=render_tab_state.far_plane,
radius_clip=render_tab_state.radius_clip,
eps2d=render_tab_state.eps2d,
backgrounds=torch.tensor([render_tab_state.backgrounds], device=device)
/ 255.0,
render_mode=RENDER_MODE_MAP[render_tab_state.render_mode],
rasterize_mode=render_tab_state.rasterize_mode,
camera_model=render_tab_state.camera_model,
packed=False,
)
render_tab_state.total_gs_count = len(means)
render_tab_state.rendered_gs_count = (info["radii"] > 0).all(-1).sum().item()
if args.backend == "gsplat":
rasterization_fn = rasterization
elif args.backend == "3dcs":
rasterization_fn = rasterization_3dcs
elif args.backend == "inria":
from gsplat import rasterization_inria_wrapper

rasterization_fn = rasterization_inria_wrapper
else:
raise ValueError

if args.backend == "3dcs":
render_colors, render_alphas, info = rasterization_fn(
convex_points,
delta,
sigma,
num_points_per_convex,
cumsum_of_points_per_convex,
opacities,
colors,
viewmat[None], # [1, 4, 4]
K[None], # [1, 3, 3]
width,
height,
packed=False,
sh_degree=(
min(render_tab_state.max_sh_degree, sh_degree)
if sh_degree is not None
else None
),
near_plane=render_tab_state.near_plane,
far_plane=render_tab_state.far_plane,
radius_clip=render_tab_state.radius_clip,
eps2d=render_tab_state.eps2d,
backgrounds=torch.tensor([render_tab_state.backgrounds], device=device)
/ 255.0,
render_mode=RENDER_MODE_MAP[render_tab_state.render_mode],
rasterize_mode=render_tab_state.rasterize_mode,
camera_model=render_tab_state.camera_model,
)
else:
render_colors, render_alphas, info = rasterization(
means, # [N, 3]
quats, # [N, 4]
scales, # [N, 3]
opacities, # [N]
colors, # [N, S, 3]
viewmat[None], # [1, 4, 4]
K[None], # [1, 3, 3]
width,
height,
sh_degree=(
min(render_tab_state.max_sh_degree, sh_degree)
if sh_degree is not None
else None
),
near_plane=render_tab_state.near_plane,
far_plane=render_tab_state.far_plane,
radius_clip=render_tab_state.radius_clip,
eps2d=render_tab_state.eps2d,
backgrounds=torch.tensor([render_tab_state.backgrounds], device=device)
/ 255.0,
render_mode=RENDER_MODE_MAP[render_tab_state.render_mode],
rasterize_mode=render_tab_state.rasterize_mode,
camera_model=render_tab_state.camera_model,
)
render_tab_state.total_gs_count = len(means)
render_tab_state.rendered_gs_count = (info["radii"] > 0).all(-1).sum().item()

if render_tab_state.render_mode == "rgb":
# colors represented with sh are not guranteed to be in [0, 1]
Expand Down Expand Up @@ -267,6 +333,12 @@ def viewer_render_fn(camera_state: CameraState, render_tab_state: RenderTabState
parser.add_argument(
"--ckpt", type=str, nargs="+", default=None, help="path to the .pt file"
)
parser.add_argument(
"--ply", type=str, nargs="+", default=None, help="path to the .ply file"
)
parser.add_argument(
"--backend", type=str, default="gsplat", choices=["gsplat", "3dcs", "inria"], help="backend to use for rendering",
)
parser.add_argument(
"--port", type=int, default=8080, help="port for the viewer server"
)
Expand Down
7 changes: 7 additions & 0 deletions gsplat/_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@
import torch
import torch.nn.functional as F

def load_ply_data(
data_path: Optional[str] = None,
device="cuda",
scene_crop: Tuple[float, float, float, float, float, float] = (-2, -2, -2, 2, 2, 2),
scene_grid: int = 1,
):
assert True

def load_test_data(
data_path: Optional[str] = None,
Expand Down
Loading
Loading