mirror of
https://github.com/nerfstudio-project/nerfstudio.git
synced 2026-12-04 06:49:13 +08:00
Add sample visualizations
This commit is contained in:
1 parent
efa40993e0
commit
803b81fa1d
5 files changed
+68392
-14
No files matched your search
Binary file not shown.
|
After Width: | Height: | Size: 73 KiB |
@@ -8,5 +8,5 @@
|
|||||||
visualize_encoders.ipynb
|
visualize_encoders.ipynb
|
||||||
visualize_samplers.ipynb
|
visualize_samplers.ipynb
|
||||||
visualize_spatial_distortions.ipynb
|
visualize_spatial_distortions.ipynb
|
||||||
|
visualize_samples.ipynb
|
||||||
|
|
||||||
File diff suppressed because it is too large.
Load diff
+12
-12
@@ -219,14 +219,14 @@ class RayBundle(TensorDataclass):
|
|||||||
return self.flatten()[start_idx:end_idx]
|
return self.flatten()[start_idx:end_idx]
|
||||||
|
|
||||||
def get_ray_samples(
|
def get_ray_samples(
|
||||||
self, bin_starts: TensorType["num_rays", "num_samples", 1], bin_ends: TensorType["num_rays", "num_samples", 1]
|
self, bin_starts: TensorType[..., "num_samples", 1], bin_ends: TensorType[..., "num_samples", 1]
|
||||||
) -> RaySamples:
|
) -> RaySamples:
|
||||||
"""Produces samples for each ray by projection points along the ray direction.
|
"""Produces samples for each ray by projection points along the ray direction.
|
||||||
|
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
bin_starts (TensorType["num_rays", "num_samples", 1]): Distance from origin to start of bin.
|
bin_starts (TensorType[..., "num_samples", 1]): Distance from origin to start of bin.
|
||||||
bin_ends (TensorType["num_rays", "num_samples", 1]): Distance from origin to end of bin.
|
bin_ends (TensorType[..., "num_samples", 1]): Distance from origin to end of bin.
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
RaySamples: Samples projected along ray.
|
RaySamples: Samples projected along ray.
|
||||||
@@ -235,7 +235,7 @@ class RayBundle(TensorDataclass):
|
|||||||
|
|
||||||
valid_mask = torch.ones((bin_starts.shape), dtype=torch.bool, device=device)
|
valid_mask = torch.ones((bin_starts.shape), dtype=torch.bool, device=device)
|
||||||
|
|
||||||
dists = bin_ends - bin_starts # [N_rays, N_samples, 1]
|
dists = bin_ends - bin_starts # [..., N_samples, 1]
|
||||||
deltas = dists * torch.norm(self.directions[:, :], dim=-1)[..., None, None]
|
deltas = dists * torch.norm(self.directions[:, :], dim=-1)[..., None, None]
|
||||||
|
|
||||||
if is_not_none(self.camera_indices):
|
if is_not_none(self.camera_indices):
|
||||||
@@ -244,18 +244,18 @@ class RayBundle(TensorDataclass):
|
|||||||
camera_indices = None
|
camera_indices = None
|
||||||
|
|
||||||
frustums = Frustums(
|
frustums = Frustums(
|
||||||
origins=self.origins[:, None, :], # [N_rays, 1, 3]
|
origins=self.origins[..., None, :], # [..., 1, 3]
|
||||||
directions=self.directions[:, None, :], # [N_rays, 1, 3]
|
directions=self.directions[..., None, :], # [..., 1, 3]
|
||||||
starts=bin_starts, # [N_rays, N_samples, 1]
|
starts=bin_starts, # [..., N_samples, 1]
|
||||||
ends=bin_ends, # [N_rays, N_samples, 1]
|
ends=bin_ends, # [..., N_samples, 1]
|
||||||
pixel_area=self.pixel_area[:, None, :], # [N_rays, 1, 1]
|
pixel_area=self.pixel_area[..., None, :], # [..., 1, 1]
|
||||||
).to(device)
|
).to(device)
|
||||||
|
|
||||||
ray_samples = RaySamples(
|
ray_samples = RaySamples(
|
||||||
frustums=frustums,
|
frustums=frustums,
|
||||||
camera_indices=camera_indices, # [N_rays, 1, 1]
|
camera_indices=camera_indices, # [..., 1, 1]
|
||||||
valid_mask=valid_mask, # [N_rays, N_samples, 1]
|
valid_mask=valid_mask, # [..., N_samples, 1]
|
||||||
deltas=deltas, # [N_rays, N_samples, 1]
|
deltas=deltas, # [..., N_samples, 1]
|
||||||
)
|
)
|
||||||
|
|
||||||
return ray_samples
|
return ray_samples
|
||||||
+243
-1
@@ -16,11 +16,15 @@
|
|||||||
Visualization code for plotly.
|
Visualization code for plotly.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
from typing import List, Union
|
||||||
|
|
||||||
import numpy as np
|
import numpy as np
|
||||||
import plotly.graph_objects as go
|
import plotly.graph_objects as go
|
||||||
import torch
|
import torch
|
||||||
|
from plotly import express as ex
|
||||||
|
from torchtyping import TensorType
|
||||||
|
|
||||||
from pyrad.cameras.rays import RayBundle
|
from pyrad.cameras.rays import Frustums, RayBundle
|
||||||
|
|
||||||
|
|
||||||
def color_str(color):
|
def color_str(color):
|
||||||
@@ -125,3 +129,241 @@ def visualize_dataset(camera_origins, ray_bundle: RayBundle):
|
|||||||
)
|
)
|
||||||
fig = go.Figure(data=data, layout=layout)
|
fig = go.Figure(data=data, layout=layout)
|
||||||
return fig
|
return fig
|
||||||
|
|
||||||
|
|
||||||
|
def get_gaussian_ellipsiod(
|
||||||
|
mean: TensorType[3],
|
||||||
|
cov: TensorType[3, 3],
|
||||||
|
n_std: int = 2,
|
||||||
|
color="lightblue",
|
||||||
|
opacity: float = 0.5,
|
||||||
|
resolution: int = 20,
|
||||||
|
name: str = "ellipse",
|
||||||
|
):
|
||||||
|
"""Get a plotly ellipsoid for a Gaussian.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
mean (TensorType[3]): mean of the Gaussian.
|
||||||
|
cov: (TesnorType[3]): covariance of the Gaussian.
|
||||||
|
n_std (int, optional): Standard devation to visualize. Defaults to 2 (95% confidence).
|
||||||
|
color (str, optional): Color of the ellipsoid. Defaults to None.
|
||||||
|
opacity (float, optional): Opacity of the ellipsoid. Defaults to 0.5.
|
||||||
|
resolution (int, optional): Resolution of the ellipsoid. Defaults to 20.
|
||||||
|
name (str, optional): Name of the ellipsoid. Defaults to "ellipse".
|
||||||
|
"""
|
||||||
|
|
||||||
|
phi = torch.linspace(0, 2 * torch.pi, resolution)
|
||||||
|
theta = torch.linspace(-torch.pi / 2, torch.pi / 2, resolution)
|
||||||
|
phi, theta = torch.meshgrid(phi, theta, indexing="ij")
|
||||||
|
|
||||||
|
x = torch.cos(theta) * torch.sin(phi)
|
||||||
|
y = torch.cos(theta) * torch.cos(phi)
|
||||||
|
z = torch.sin(theta)
|
||||||
|
pts = torch.stack((x, y, z), axis=-1)
|
||||||
|
|
||||||
|
eigenvals, eigenvecs = torch.linalg.eigh(cov)
|
||||||
|
idx = torch.sum(cov, axis=0).argsort()
|
||||||
|
idx = eigenvals[idx].argsort()
|
||||||
|
eigenvals = eigenvals[idx][idx]
|
||||||
|
eigenvecs = eigenvecs[:, idx]
|
||||||
|
|
||||||
|
scaling = torch.sqrt(eigenvals) * n_std
|
||||||
|
pts = pts * scaling
|
||||||
|
|
||||||
|
pts = pts @ eigenvecs.t()
|
||||||
|
|
||||||
|
pts += mean
|
||||||
|
|
||||||
|
return go.Mesh3d(
|
||||||
|
{
|
||||||
|
"x": pts[:, :, 0].flatten(),
|
||||||
|
"y": pts[:, :, 1].flatten(),
|
||||||
|
"z": pts[:, :, 2].flatten(),
|
||||||
|
"alphahull": 0,
|
||||||
|
"opacity": opacity,
|
||||||
|
"color": color,
|
||||||
|
"name": name,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def get_frustum_mesh(
|
||||||
|
frustum: Frustums, opacity: float = 0.3, color: str = "#DC203C", resolution: int = 20
|
||||||
|
) -> go.Mesh3d:
|
||||||
|
"""Get a plotly mesh for a single frustum.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
frustum (Frustum): Single frustum
|
||||||
|
opacity (float, optional): Opacity of the mesh. Defaults to 0.3.
|
||||||
|
color (str, optional): Color of the mesh. Defaults to "#DC203C".
|
||||||
|
resolution (int, optional): Resolution of the mesh. Defaults to 20.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
go.Mesh3d: Plotly mesh
|
||||||
|
"""
|
||||||
|
|
||||||
|
if frustum.ndim > 1:
|
||||||
|
raise ValueError("Frustum must be a single Frustum object.")
|
||||||
|
|
||||||
|
base_radius = torch.sqrt(frustum.pixel_area / torch.pi)
|
||||||
|
f_radius = frustum.starts * base_radius
|
||||||
|
b_radius = frustum.ends * base_radius
|
||||||
|
|
||||||
|
x = torch.cat([torch.ones(resolution) * frustum.starts, torch.ones(resolution) * frustum.ends])
|
||||||
|
pts = torch.linspace(0, 2 * torch.pi, resolution)
|
||||||
|
|
||||||
|
y = torch.sin(pts)
|
||||||
|
z = torch.cos(pts)
|
||||||
|
y = torch.cat([y * f_radius, y * b_radius])
|
||||||
|
z = torch.cat([z * f_radius, z * b_radius])
|
||||||
|
|
||||||
|
pts = torch.stack([x, y, z], dim=-1)
|
||||||
|
|
||||||
|
forward = frustum.directions
|
||||||
|
up = torch.nn.functional.normalize(torch.cross(forward, torch.tensor([0.0, 0, 1])), dim=-1)
|
||||||
|
right = torch.nn.functional.normalize(torch.cross(forward, up), dim=-1)
|
||||||
|
rotation = torch.stack([forward, up, right], dim=1)
|
||||||
|
pts = torch.einsum("kj,ij->ki", pts, rotation)
|
||||||
|
|
||||||
|
pts += frustum.origins
|
||||||
|
return go.Mesh3d(
|
||||||
|
x=pts[..., 0],
|
||||||
|
y=pts[..., 1],
|
||||||
|
z=pts[..., 2],
|
||||||
|
opacity=opacity,
|
||||||
|
alphahull=0,
|
||||||
|
color=color,
|
||||||
|
flatshading=True,
|
||||||
|
name="Frustums",
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def get_frustums_mesh_list(
|
||||||
|
frustums: Frustums, opacity: float = 1.0, color: str = "random", resolution: int = 20
|
||||||
|
) -> List[go.Mesh3d]:
|
||||||
|
"""Get a list of plotly meshes for a list of frustums.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
frustums (Frustum): List of frustums
|
||||||
|
opacity (float, optional): Opacity of the mesh. Defaults to 0.3.
|
||||||
|
color (str, optional): Color of the mesh. Defaults to "random".
|
||||||
|
resolution (int, optional): Resolution of the mesh. Defaults to 20.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
List[go.Mesh3d]: List of plotly meshes
|
||||||
|
"""
|
||||||
|
data = []
|
||||||
|
for i, frustum in enumerate(frustums.flatten()):
|
||||||
|
if color == "random":
|
||||||
|
c = ex.colors.qualitative.Plotly[i % len(ex.colors.qualitative.Plotly)]
|
||||||
|
else:
|
||||||
|
c = color
|
||||||
|
data.append(get_frustum_mesh(frustum, opacity=opacity, color=c, resolution=resolution))
|
||||||
|
return data
|
||||||
|
|
||||||
|
|
||||||
|
def get_frustums_gaussian_list(
|
||||||
|
frustums: Frustums, opacity: float = 0.5, color: str = "random", resolution: int = 20
|
||||||
|
) -> List[Union[go.Mesh3d, go.Scatter3d]]:
|
||||||
|
"""Get a list of plotly meshes for frustums.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
frustums (Frustum): Frustums to compute gaussians meshes for.
|
||||||
|
opacity (float, optional): Opacity of the mesh. Defaults to 0.3.
|
||||||
|
color (str, optional): Color of the mesh. Defaults to "random".
|
||||||
|
resolution (int, optional): Resolution of the mesh. Defaults to 20.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
List[go.Mesh3d]: List of plotly meshes
|
||||||
|
"""
|
||||||
|
|
||||||
|
gaussians = frustums.flatten().get_gaussian_blob()
|
||||||
|
|
||||||
|
data = []
|
||||||
|
vis_means = go.Scatter3d(
|
||||||
|
x=gaussians.mean[:, 0],
|
||||||
|
y=gaussians.mean[:, 1],
|
||||||
|
z=gaussians.mean[:, 2],
|
||||||
|
mode="markers",
|
||||||
|
marker=dict(size=2, color="black"),
|
||||||
|
name="Means",
|
||||||
|
)
|
||||||
|
data.append(vis_means)
|
||||||
|
for i in range(gaussians.mean.shape[0]):
|
||||||
|
if color == "random":
|
||||||
|
c = ex.colors.qualitative.Plotly[i % len(ex.colors.qualitative.Plotly)]
|
||||||
|
else:
|
||||||
|
c = color
|
||||||
|
ellipse = get_gaussian_ellipsiod(
|
||||||
|
gaussians.mean[i],
|
||||||
|
cov=gaussians.cov[i],
|
||||||
|
color=c,
|
||||||
|
opacity=opacity,
|
||||||
|
resolution=resolution,
|
||||||
|
)
|
||||||
|
data.append(ellipse)
|
||||||
|
|
||||||
|
return data
|
||||||
|
|
||||||
|
|
||||||
|
def get_frustum_points(
|
||||||
|
frustum: Frustums, opacity: float = 1.0, color: str = "forestgreen", size: float = 5
|
||||||
|
) -> go.Scatter3d:
|
||||||
|
"""Get a set plotly points for frustums centers.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
frustum (Frustum): Frustums to visualize.
|
||||||
|
opacity (float, optional): Opacity of the points. Defaults to 0.3.
|
||||||
|
color (str, optional): Color of the poinst. Defaults to "forestgreen".
|
||||||
|
size (float, optional): Size of points. Defaults to 10.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
go.Scatter3d: Plotly points
|
||||||
|
"""
|
||||||
|
|
||||||
|
frustum = frustum.flatten()
|
||||||
|
pts = frustum.get_positions()
|
||||||
|
|
||||||
|
return go.Scatter3d(
|
||||||
|
x=pts[..., 0],
|
||||||
|
y=pts[..., 1],
|
||||||
|
z=pts[..., 2],
|
||||||
|
mode="markers",
|
||||||
|
marker=dict(
|
||||||
|
size=size,
|
||||||
|
color=color,
|
||||||
|
opacity=opacity,
|
||||||
|
),
|
||||||
|
name="Frustums -> Positions",
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def get_ray_bundle_lines(
|
||||||
|
ray_bundle: RayBundle, length: float = 1.0, color: str = "#DC203C", width: float = 1
|
||||||
|
) -> go.Scatter3d:
|
||||||
|
"""Get a plotly line for a ray bundle.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
ray_bundle (RayBundle): Ray bundle
|
||||||
|
length (float, optional): Length of the line. Defaults to 1.0.
|
||||||
|
color (str, optional): Color of the line.
|
||||||
|
width (float, optional): Width of the line. Defaults to 1.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
go.Scatter3d: Plotly lines
|
||||||
|
"""
|
||||||
|
|
||||||
|
origins = ray_bundle.origins.view(-1, 3)
|
||||||
|
directions = ray_bundle.directions.view(-1, 3)
|
||||||
|
|
||||||
|
lines = torch.empty((origins.shape[0] * 2, 3))
|
||||||
|
lines[0::2] = origins
|
||||||
|
lines[1::2] = origins + directions * length
|
||||||
|
return go.Scatter3d(
|
||||||
|
x=lines[..., 0],
|
||||||
|
y=lines[..., 1],
|
||||||
|
z=lines[..., 2],
|
||||||
|
mode="lines",
|
||||||
|
name="Ray Bundle",
|
||||||
|
line=dict(color=color, width=width),
|
||||||
|
)
|
||||||
Reference in new issue
Block a user