-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathvisualize_slam_map.py
More file actions
317 lines (256 loc) · 10.6 KB
/
Copy pathvisualize_slam_map.py
File metadata and controls
317 lines (256 loc) · 10.6 KB
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
#!/usr/bin/env python3
"""
Standalone script to visualize SLAM maps saved with SLAMMap.save()
Displays two point clouds: one with RGB colors, one with PCA-colored embeddings.
Usage:
python visualize_slam_map.py <path_to_saved_map.pt> [--pca-basis projector_state.pt]
"""
import argparse
from pathlib import Path
from typing import Any
import numpy as np
import rerun as rr
import torch
def _resolve_device(device: str | torch.device) -> torch.device:
"""Normalize device inputs to torch.device."""
return device if isinstance(device, torch.device) else torch.device(device)
def load_slam_map(path: Path, device: str = "cpu"):
"""Load the SLAM map from disk."""
torch_device = _resolve_device(device)
data = torch.load(path, map_location=torch_device)
return data
def _extract_pca_basis(state: Any) -> tuple[torch.Tensor, torch.Tensor]:
"""
Attempt to locate (mean, components) tensors inside an arbitrary container saved on disk.
Supports plain dicts, nested dicts, or objects exposing .mean/.components (e.g., PCABasis).
"""
if isinstance(state, dict):
if "mean" in state and "components" in state:
return state["mean"], state["components"]
for value in state.values():
if isinstance(value, dict):
try:
return _extract_pca_basis(value)
except (KeyError, TypeError):
continue
elif hasattr(state, "mean") and hasattr(state, "components"):
return getattr(state, "mean"), getattr(state, "components")
raise KeyError("Unable to locate 'mean' and 'components' tensors in the provided PCA state.")
def load_pca_basis(path: Path, device: str = "cpu") -> tuple[torch.Tensor, torch.Tensor]:
"""
Load PCA basis tensors (mean, components) from disk.
Args:
path: Path to a .pt/.pth file that stores a PCAProjector.state_dict() or equivalent.
device: Device to map the tensors to when loading.
"""
torch_device = _resolve_device(device)
state = torch.load(path, map_location=torch_device)
mean, components = _extract_pca_basis(state)
if not isinstance(mean, torch.Tensor):
mean = torch.as_tensor(mean, device=torch_device, dtype=torch.float32)
if not isinstance(components, torch.Tensor):
components = torch.as_tensor(components, device=torch_device, dtype=torch.float32)
return mean, components
def decode_embeddings_with_pca(
encoded_embeddings: torch.Tensor,
mean: torch.Tensor,
components: torch.Tensor,
) -> torch.Tensor:
"""
Decode PCA-compressed embeddings back to the original feature dimension.
Args:
encoded_embeddings: (N, K) tensor containing PCA codes.
mean: (C,) tensor representing the feature mean used during PCA fit.
components: (C, K) tensor with PCA components.
"""
if encoded_embeddings is None:
raise ValueError("Cannot decode embeddings because the map does not contain any.")
if encoded_embeddings.dim() != 2:
raise ValueError(f"Expected encoded embeddings to be 2D, got shape {encoded_embeddings.shape}.")
comps = components.to(device=encoded_embeddings.device, dtype=encoded_embeddings.dtype)
mean = mean.to(device=encoded_embeddings.device, dtype=encoded_embeddings.dtype)
if encoded_embeddings.shape[1] != comps.shape[1]:
raise ValueError(
f"PCA code dimension mismatch: encoded dim {encoded_embeddings.shape[1]} "
f"!= components dim {comps.shape[1]}."
)
decoded = encoded_embeddings @ comps.transpose(0, 1) + mean.unsqueeze(0)
print(f"decoding shape {decoded.shape}")
return decoded
def get_full_embedding_map(map_path: Path, pca_basis_path: Path, device: str = "cpu"):
"""
Load a saved SLAM map and attach decoded full-dimensional embeddings under
the key ``dense_disp_embeddings_full``.
"""
data = load_slam_map(map_path, device=device)
if data.get("dense_disp_embeddings_full") is not None:
return data
embeddings = data.get("dense_disp_embeddings")
if embeddings is None:
raise ValueError("SLAM map does not contain embeddings to decode.")
mean, components = load_pca_basis(pca_basis_path, device=device)
decoded = decode_embeddings_with_pca(embeddings, mean, components)
data["dense_disp_embeddings_full"] = decoded
return data
def pca_to_rgb(embeddings: torch.Tensor, n_components: int = 3) -> np.ndarray:
"""
Apply PCA to embeddings and convert to RGB visualization.
Args:
embeddings: (N, D) tensor of embeddings
n_components: Number of PCA components (3 for RGB)
Returns:
(N, 3) RGB array with values in [0, 255]
"""
# Convert to numpy and ensure float64 for numerical stability
emb_np = embeddings.cpu().numpy().astype(np.float64)
# Center the data
mean = emb_np.mean(axis=0)
centered = emb_np - mean
# Compute covariance matrix
cov = np.cov(centered.T)
# Compute eigenvalues and eigenvectors
eigenvalues, eigenvectors = np.linalg.eigh(cov)
# Sort by eigenvalues (descending)
idx = eigenvalues.argsort()[::-1]
eigenvectors = eigenvectors[:, idx]
# Project onto top n_components
pca_features = centered @ eigenvectors[:, :n_components]
# Normalize each component independently to [0, 1]
rgb = np.zeros((pca_features.shape[0], 3))
for i in range(min(n_components, 3)):
channel = pca_features[:, i]
min_val, max_val = channel.min(), channel.max()
if max_val - min_val > 1e-8:
rgb[:, i] = (channel - min_val) / (max_val - min_val)
else:
rgb[:, i] = 0.5
# Convert to uint8
return (rgb * 255).astype(np.uint8)
def visualize_slam_map(map_path: Path, device: str = "cpu", pca_basis_path: Path | None = None):
"""
Main visualization function.
Args:
map_path: Path to the saved SLAM map (.pt file)
"""
print(f"Loading SLAM map from: {map_path}")
if pca_basis_path is not None:
print(f"Decoding embeddings using PCA basis from: {pca_basis_path}")
data = get_full_embedding_map(map_path, pca_basis_path, device=device)
else:
data = load_slam_map(map_path, device=device)
# Extract data
xyz = data["dense_disp_xyz"]
rgb = data["dense_disp_rgb"]
full_embeddings = data.get("dense_disp_embeddings_full")
embeddings = data.get("dense_disp_embeddings")
embedding_valid = data.get("dense_disp_embedding_valid")
print(f"Point cloud size: {xyz.shape[0]} points")
if full_embeddings is not None:
print("Using decoded full-dimensional embeddings for visualization.")
embeddings_for_vis = full_embeddings
else:
embeddings_for_vis = embeddings
print(f"Has embeddings: {embeddings_for_vis is not None}")
# Initialize Rerun
rr.init("SLAM Map Viewer", spawn=True)
# Convert to numpy
xyz_np = xyz.cpu().numpy()
rgb_np = (rgb.cpu().numpy() * 255).astype(np.uint8)
# Log RGB point cloud
print("Logging RGB point cloud...")
rr.log(
"world/point_cloud/rgb",
rr.Points3D(
positions=xyz_np,
colors=rgb_np,
radii=0.01,
),
)
# Log embedding-colored point cloud if available
if embeddings_for_vis is not None:
print("Processing embeddings with PCA...")
# Filter by validity mask if available
if embedding_valid is not None:
valid_mask = embedding_valid.cpu().numpy()
valid_xyz = xyz_np[valid_mask]
valid_embeddings = embeddings_for_vis[embedding_valid]
print(f"Valid embeddings: {valid_embeddings.shape[0]} / {embeddings_for_vis.shape[0]}")
else:
valid_xyz = xyz_np
valid_embeddings = embeddings_for_vis
# Apply PCA to get RGB colors
pca_colors = pca_to_rgb(valid_embeddings, n_components=3)
print("Logging PCA-colored point cloud...")
rr.log(
"world/point_cloud/pca_embeddings",
rr.Points3D(
positions=valid_xyz,
colors=pca_colors,
radii=0.01,
),
)
# Log embedding statistics
emb_mean = valid_embeddings.mean().item()
emb_std = valid_embeddings.std().item()
emb_dim = valid_embeddings.shape[1]
source = "decoded_full" if full_embeddings is not None else "encoded"
rr.log("stats/embedding_dim", rr.TextLog(f"{source} embedding dimension: {emb_dim}"))
rr.log("stats/embedding_source", rr.TextLog(f"Embedding source: {source}"))
rr.log("stats/embedding_mean", rr.Scalar(emb_mean))
rr.log("stats/embedding_std", rr.Scalar(emb_std))
else:
print("No embeddings found in the map.")
# Log metadata
rr.log("stats/total_points", rr.Scalar(xyz.shape[0]))
rr.log("stats/map_path", rr.TextLog(str(map_path)))
# Compute and log bounding box
bbox_min = xyz_np.min(axis=0)
bbox_max = xyz_np.max(axis=0)
bbox_center = (bbox_min + bbox_max) / 2
bbox_size = bbox_max - bbox_min
print(f"\nBounding box:")
print(f" Center: {bbox_center}")
print(f" Size: {bbox_size}")
rr.log(
"world/bounding_box",
rr.Boxes3D(
sizes=[bbox_size],
centers=[bbox_center],
colors=[[255, 255, 0, 128]],
),
)
print("\nVisualization complete! Use the Rerun viewer to explore.")
print("Toggle between point clouds in the left panel:")
print(" - world/point_cloud/rgb: Original RGB colors")
if embeddings_for_vis is not None:
print(" - world/point_cloud/pca_embeddings: PCA-colored embeddings")
def main():
parser = argparse.ArgumentParser(description="Visualize SLAM map with RGB and PCA-colored embeddings")
parser.add_argument(
"map_path",
type=Path,
help="Path to the saved SLAM map (.pt file)",
)
parser.add_argument(
"--pca-basis",
type=Path,
default=None,
help="Optional path to a PCA basis (.pt/.pth) with 'mean' and 'components' tensors to decode full embeddings.",
)
parser.add_argument(
"--device",
type=str,
default="cpu",
choices=["cpu", "cuda"],
help="Device to load tensors on (default: cpu)",
)
args = parser.parse_args()
if not args.map_path.exists():
print(f"Error: Map file not found: {args.map_path}")
return
if args.pca_basis is not None and not args.pca_basis.exists():
print(f"Error: PCA basis file not found: {args.pca_basis}")
return
visualize_slam_map(args.map_path, device=args.device, pca_basis_path=args.pca_basis)
if __name__ == "__main__":
main()