Spaces:
Running on Zero
Running on Zero
File size: 7,730 Bytes
7405e85 | 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 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 | from math import isqrt
from typing import Literal
import torch
from einops import rearrange, repeat
from jaxtyping import Float
from torch import Tensor
from gsplat.rendering import rasterization
GsplatRenderMode = Literal["RGB", "RGB+D", "RGB+ED"]
GsplatRasterizeMode = Literal["classic", "antialiased"]
def normalized_intrinsics_to_pixel_K(
intrinsics: Float[Tensor, "batch view 3 3"],
image_shape: tuple[int, int],
) -> Float[Tensor, "batch view 3 3"]:
h, w = image_shape
K = intrinsics.clone().float()
K[..., 0, 0] *= w
K[..., 1, 1] *= h
K[..., 0, 2] *= w
K[..., 1, 2] *= h
return K
def _identity_quats_like(
means: Float[Tensor, "batch gaussian 3"],
) -> Float[Tensor, "batch gaussian 4"]:
quats = torch.zeros((*means.shape[:-1], 4), device=means.device, dtype=means.dtype)
# gsplat uses wxyz quaternions.
quats[..., 0] = 1.0
return quats
def _xyzw_to_wxyz(
quats: Float[Tensor, "batch gaussian 4"],
) -> Float[Tensor, "batch gaussian 4"]:
return torch.cat((quats[..., 3:4], quats[..., :3]), dim=-1)
@torch.autocast("cuda", enabled=False)
def render_gsplat(
extrinsics: Float[Tensor, "batch view 4 4"],
intrinsics: Float[Tensor, "batch view 3 3"],
near: Float[Tensor, "batch view"],
far: Float[Tensor, "batch view"],
image_shape: tuple[int, int],
background_color: Float[Tensor, "batch 3"],
gaussian_means: Float[Tensor, "batch gaussian 3"],
gaussian_covariances: Float[Tensor, "batch gaussian 3 3"] | None,
gaussian_sh_coefficients: Float[Tensor, "batch gaussian 3 d_sh"],
gaussian_opacities: Float[Tensor, "batch gaussian"],
gaussian_scales: Float[Tensor, "batch gaussian 3"] | None = None,
gaussian_rotations: Float[Tensor, "batch gaussian 4"] | None = None,
render_mode: GsplatRenderMode = "RGB",
use_covariances: bool = True,
eps2d: float = 0.1,
packed: bool = True,
sparse_grad: bool = False,
absgrad: bool = False,
rasterize_mode: GsplatRasterizeMode = "antialiased",
channel_chunk: int = 32,
scale_invariant: bool = False,
) -> tuple[
Float[Tensor, "batch view 3 height width"] | None,
Float[Tensor, "batch view height width"] | None,
Float[Tensor, "batch view height width"],
]:
b, v, _, _ = extrinsics.shape
h, w = image_shape
extrinsics = extrinsics.float()
intrinsics = intrinsics.float()
near = near.float()
far = far.float()
background_color = background_color.float()
gaussian_means = gaussian_means.float()
gaussian_sh_coefficients = gaussian_sh_coefficients.float()
gaussian_opacities = gaussian_opacities.float()
covars = gaussian_covariances.float() if gaussian_covariances is not None else None
scales = gaussian_scales.float() if gaussian_scales is not None else None
# The adapter stores quaternions as xyzw, while gsplat expects wxyz.
quats = (
_xyzw_to_wxyz(gaussian_rotations.float())
if gaussian_rotations is not None
else None
)
if use_covariances:
if covars is None:
raise ValueError("gsplat covariance mode requires gaussians.covariances.")
if scales is None:
scales = torch.ones_like(gaussian_means)
if quats is None:
quats = _identity_quats_like(gaussian_means)
else:
covars = None
if scales is None or quats is None:
raise ValueError(
"gsplat scale/quaternion mode requires gaussians.scales and "
"gaussians.rotations. Prefer use_covariances=true."
)
if scale_invariant:
scale = 1.0 / near[:, :1].clamp_min(1e-8)
extrinsics = extrinsics.clone()
extrinsics[..., :3, 3] = extrinsics[..., :3, 3] * scale[..., None]
gaussian_means = gaussian_means * scale[:, :, None]
near = near * scale
far = far * scale
if covars is not None:
covars = covars * (scale[:, :, None, None] ** 2)
elif scales is not None:
scales = scales * scale[:, :, None]
Ks = normalized_intrinsics_to_pixel_K(intrinsics, image_shape)
viewmats = torch.linalg.inv(extrinsics)
sh_degree = isqrt(gaussian_sh_coefficients.shape[-1]) - 1
shs = rearrange(
gaussian_sh_coefficients, "b g c n -> b g n c"
).contiguous()
render_rgb = "RGB" in render_mode
render_depth = "D" in render_mode
# gsplat's packed path has limited background broadcasting across render
# modes. Our datasets use black backgrounds, so omitting the background keeps
# the memory-efficient packed path available without changing semantics.
backgrounds = (
None
if packed
else repeat(background_color, "b c -> b v c", v=v).to(Ks)
)
render_out, alpha, _ = rasterization(
means=gaussian_means,
quats=quats,
scales=scales,
opacities=gaussian_opacities,
colors=shs,
viewmats=viewmats,
Ks=Ks,
width=w,
height=h,
near_plane=float(near.detach().min().item()),
far_plane=float(far.detach().max().item()),
eps2d=eps2d,
sh_degree=sh_degree,
packed=packed,
backgrounds=backgrounds,
render_mode=render_mode,
sparse_grad=sparse_grad,
absgrad=absgrad,
rasterize_mode=rasterize_mode,
channel_chunk=channel_chunk,
covars=covars,
)
color = (
rearrange(render_out[..., :3], "b v h w c -> b v c h w")
if render_rgb
else None
)
depth = render_out[..., -1] if render_depth else None
return color, depth, alpha.squeeze(-1)
def render_depth_gsplat(
extrinsics: Float[Tensor, "batch view 4 4"],
intrinsics: Float[Tensor, "batch view 3 3"],
near: Float[Tensor, "batch view"],
far: Float[Tensor, "batch view"],
image_shape: tuple[int, int],
background_color: Float[Tensor, "batch 3"],
gaussian_means: Float[Tensor, "batch gaussian 3"],
gaussian_covariances: Float[Tensor, "batch gaussian 3 3"] | None,
gaussian_sh_coefficients: Float[Tensor, "batch gaussian 3 d_sh"],
gaussian_opacities: Float[Tensor, "batch gaussian"],
gaussian_scales: Float[Tensor, "batch gaussian 3"] | None = None,
gaussian_rotations: Float[Tensor, "batch gaussian 4"] | None = None,
mode: Literal["depth", "disparity", "relative_disparity", "log"] = "depth",
use_covariances: bool = True,
eps2d: float = 0.1,
packed: bool = True,
sparse_grad: bool = False,
absgrad: bool = False,
rasterize_mode: GsplatRasterizeMode = "antialiased",
channel_chunk: int = 32,
scale_invariant: bool = False,
) -> Float[Tensor, "batch view height width"]:
render_mode = "RGB+ED" if mode == "relative_disparity" else "RGB+D"
_, depth, _ = render_gsplat(
extrinsics,
intrinsics,
near,
far,
image_shape,
background_color,
gaussian_means,
gaussian_covariances,
gaussian_sh_coefficients,
gaussian_opacities,
gaussian_scales=gaussian_scales,
gaussian_rotations=gaussian_rotations,
render_mode=render_mode,
use_covariances=use_covariances,
eps2d=eps2d,
packed=packed,
sparse_grad=sparse_grad,
absgrad=absgrad,
rasterize_mode=rasterize_mode,
channel_chunk=channel_chunk,
scale_invariant=scale_invariant,
)
assert depth is not None
if mode in ("disparity", "relative_disparity"):
depth = 1.0 / depth.clamp_min(1e-8)
elif mode == "log":
depth = depth.clamp_min(near[..., None, None]).clamp_max(
far[..., None, None]
).log()
return depth
|