Repository navigation
Expand file tree
/
Copy pathapp.py
More file actions
357 lines (295 loc) · 12.2 KB
/
Copy pathapp.py
File metadata and controls
357 lines (295 loc) · 12.2 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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
"""Gradio UI for LingBot-Map streaming 3D reconstruction.
Usage:
python app.py # auto-downloads checkpoint
python app.py --model_path /path/to/ckpt.pt # uses local checkpoint
python app.py --share # public Gradio link
The app accepts a video or a folder of images, runs streaming inference, and
renders the resulting point cloud as a downloadable GLB you can view in the
browser via Gradio's built-in Model3D viewer.
"""
import argparse
import glob
import os
import shutil
import tempfile
import time
from pathlib import Path
os.environ.setdefault("PYTORCH_CUDA_ALLOC_CONF", "expandable_segments:True")
import gradio as gr
import numpy as np
import torch
from demo import (
load_images,
load_model,
postprocess,
prepare_for_visualization,
)
REPO_ROOT = Path(__file__).resolve().parent
CKPT_DIR = REPO_ROOT / "checkpoints"
EXAMPLE_DIR = REPO_ROOT / "example"
CKPT_DIR.mkdir(exist_ok=True)
DEFAULT_HF_REPO = "robbyant/lingbot-map"
CHECKPOINT_OPTIONS = {
"lingbot-map-long (recommended, long sequences)": "lingbot-map-long.pt",
"lingbot-map (balanced)": "lingbot-map.pt",
"lingbot-map-stage1": "lingbot-map-stage1.pt",
}
_MODEL_CACHE: dict = {}
class Args:
"""Argparse-compatible namespace built from Gradio inputs."""
def __init__(self, **kwargs):
self.image_size = 518
self.patch_size = 14
self.enable_3d_rope = True
self.max_frame_num = 1024
self.num_scale_frames = 8
self.kv_cache_sliding_window = 64
self.use_sdpa = True
self.camera_num_iterations = 4
self.mode = "streaming"
self.model_path = None
for k, v in kwargs.items():
setattr(self, k, v)
def ensure_checkpoint(checkpoint_label: str, custom_path: str = "") -> str:
"""Resolve a checkpoint to a local path, downloading from HuggingFace if needed."""
if custom_path and os.path.isfile(custom_path):
return custom_path
filename = CHECKPOINT_OPTIONS.get(checkpoint_label)
if filename is None:
raise ValueError(f"Unknown checkpoint: {checkpoint_label}")
local = CKPT_DIR / filename
if local.is_file():
return str(local)
from huggingface_hub import hf_hub_download
print(f"Downloading {filename} from {DEFAULT_HF_REPO}...")
path = hf_hub_download(
repo_id=DEFAULT_HF_REPO,
filename=filename,
local_dir=str(CKPT_DIR),
)
return path
def get_model(model_path: str, use_sdpa: bool, camera_num_iterations: int):
key = (model_path, use_sdpa, camera_num_iterations)
if key in _MODEL_CACHE:
return _MODEL_CACHE[key]
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
args = Args(
model_path=model_path,
use_sdpa=use_sdpa,
camera_num_iterations=camera_num_iterations,
mode="streaming",
)
model = load_model(args, device)
if device.type == "cuda":
dtype = torch.bfloat16 if torch.cuda.get_device_capability()[0] >= 8 else torch.float16
if model.aggregator is not None:
model.aggregator = model.aggregator.to(dtype=dtype)
_MODEL_CACHE.clear()
_MODEL_CACHE[key] = (model, device)
return model, device
def _stage_inputs(video_file, image_files, image_folder_path):
"""Resolve UI inputs to either a video path or an image folder path."""
if video_file:
return {"video_path": video_file, "image_folder": None, "cleanup": None}
if image_files:
tmp = tempfile.mkdtemp(prefix="lingbot_uploads_")
for i, f in enumerate(image_files):
ext = os.path.splitext(f)[1] or ".jpg"
shutil.copy(f, os.path.join(tmp, f"{i:06d}{ext}"))
return {"video_path": None, "image_folder": tmp, "cleanup": tmp}
if image_folder_path and os.path.isdir(image_folder_path):
return {"video_path": None, "image_folder": image_folder_path, "cleanup": None}
raise gr.Error("Provide a video, upload images, or pick an example folder.")
def reconstruct(
video_file,
image_files,
image_folder_path,
checkpoint_label,
custom_ckpt_path,
fps,
first_k,
keyframe_interval,
mask_sky,
conf_threshold,
camera_num_iterations,
use_sdpa,
show_cameras,
progress=gr.Progress(track_tqdm=True),
):
if not torch.cuda.is_available():
gr.Warning("No CUDA GPU detected — inference will be very slow on CPU.")
progress(0.0, desc="Resolving inputs")
staged = _stage_inputs(video_file, image_files, image_folder_path)
progress(0.05, desc="Resolving checkpoint")
ckpt_path = ensure_checkpoint(checkpoint_label, custom_ckpt_path)
progress(0.1, desc="Loading model (cached after first run)")
model, device = get_model(
ckpt_path,
use_sdpa=use_sdpa,
camera_num_iterations=int(camera_num_iterations),
)
progress(0.2, desc="Loading frames")
images, paths, resolved_folder = load_images(
image_folder=staged["image_folder"],
video_path=staged["video_path"],
fps=int(fps),
first_k=int(first_k) if first_k and int(first_k) > 0 else None,
)
if images.shape[0] < 2:
raise gr.Error(f"Got only {images.shape[0]} frames — need at least 2.")
images = images.to(device)
num_frames = images.shape[0]
info_lines = [f"Loaded {num_frames} frames at {tuple(images.shape[-2:])}."]
if device.type == "cuda":
dtype = torch.bfloat16 if torch.cuda.get_device_capability()[0] >= 8 else torch.float16
else:
dtype = torch.float32
kf = int(keyframe_interval) if keyframe_interval and int(keyframe_interval) > 0 else None
if kf is None:
kf = max(1, (num_frames + 319) // 320) if num_frames > 320 else 1
info_lines.append(f"keyframe_interval = {kf}")
progress(0.3, desc=f"Running inference ({num_frames} frames)")
t0 = time.time()
with torch.no_grad(), torch.amp.autocast("cuda", dtype=dtype, enabled=device.type == "cuda"):
predictions = model.inference_streaming(
images,
num_scale_frames=8,
keyframe_interval=kf,
output_device=torch.device("cpu"),
)
dt = time.time() - t0
info_lines.append(f"Inference: {dt:.1f}s ({num_frames / max(dt, 1e-6):.1f} FPS)")
progress(0.85, desc="Post-processing")
images_for_post = predictions.get("images", images)
predictions, images_cpu = postprocess(predictions, images_for_post)
vis_pred = prepare_for_visualization(predictions, images_cpu)
if "world_points_from_depth" not in vis_pred:
from lingbot_map.utils.geometry import unproject_depth_map_to_point_map
vis_pred["world_points_from_depth"] = unproject_depth_map_to_point_map(
vis_pred["depth"], vis_pred["extrinsic"], vis_pred["intrinsic"]
)
progress(0.92, desc="Building GLB")
from lingbot_map.vis import predictions_to_glb
target_dir = None
if mask_sky:
target_dir = tempfile.mkdtemp(prefix="lingbot_skydir_")
os.makedirs(os.path.join(target_dir, "images"), exist_ok=True)
os.makedirs(os.path.join(target_dir, "sky_masks"), exist_ok=True)
for p in paths:
shutil.copy(p, os.path.join(target_dir, "images", os.path.basename(p)))
scene = predictions_to_glb(
vis_pred,
conf_thres=float(conf_threshold),
show_cam=bool(show_cameras),
mask_sky=bool(mask_sky),
target_dir=target_dir,
)
out_dir = tempfile.mkdtemp(prefix="lingbot_glb_")
glb_path = os.path.join(out_dir, "reconstruction.glb")
scene.export(glb_path)
if staged["cleanup"]:
shutil.rmtree(staged["cleanup"], ignore_errors=True)
if torch.cuda.is_available():
torch.cuda.empty_cache()
progress(1.0, desc="Done")
info_lines.append(f"GLB saved: {glb_path}")
return glb_path, glb_path, "\n".join(info_lines)
def list_example_folders():
if not EXAMPLE_DIR.is_dir():
return []
return sorted(str(p) for p in EXAMPLE_DIR.iterdir() if p.is_dir())
def build_ui():
with gr.Blocks(title="LingBot-Map") as demo:
gr.Markdown(
"## LingBot-Map — Streaming 3D Reconstruction\n"
"Upload a video or images, run inference, and view the reconstructed "
"point cloud (GLB) right in the browser."
)
with gr.Row():
with gr.Column(scale=1):
with gr.Tabs():
with gr.Tab("Video"):
video_in = gr.Video(label="Input video", sources=["upload"])
with gr.Tab("Images"):
images_in = gr.File(
label="Upload images",
file_count="multiple",
file_types=["image"],
)
with gr.Tab("Example folder"):
examples = list_example_folders()
folder_in = gr.Dropdown(
label="Built-in example",
choices=examples,
value=examples[0] if examples else None,
)
with gr.Accordion("Model & inference", open=True):
ckpt_choice = gr.Dropdown(
label="Checkpoint (auto-downloaded from HuggingFace)",
choices=list(CHECKPOINT_OPTIONS.keys()),
value="lingbot-map-long (recommended, long sequences)",
)
custom_ckpt = gr.Textbox(
label="Custom checkpoint path (optional, overrides above)",
value="",
placeholder="/path/to/your.pt",
)
fps = gr.Slider(1, 30, value=10, step=1, label="Video sampling FPS")
first_k = gr.Number(
value=120,
precision=0,
label="Max frames (0 = all). Keep small for first run.",
)
keyframe_interval = gr.Number(
value=0,
precision=0,
label="keyframe_interval (0 = auto)",
)
camera_iters = gr.Slider(
1, 4, value=4, step=1, label="Camera refinement iterations"
)
use_sdpa = gr.Checkbox(
value=True,
label="Use SDPA (PyTorch native attention) — uncheck only if FlashInfer installed",
)
with gr.Accordion("Visualization", open=True):
mask_sky = gr.Checkbox(value=False, label="Mask sky (outdoor scenes)")
conf_threshold = gr.Slider(
0, 95, value=50, step=1,
label="Confidence percentile filter (higher = stricter)",
)
show_cameras = gr.Checkbox(value=True, label="Show camera frustums")
run_btn = gr.Button("Run reconstruction", variant="primary")
with gr.Column(scale=1):
model3d = gr.Model3D(
label="Reconstructed point cloud",
clear_color=[0.05, 0.05, 0.08, 1.0],
height=520,
)
glb_download = gr.File(label="Download GLB")
info = gr.Textbox(label="Run info", lines=6)
run_btn.click(
reconstruct,
inputs=[
video_in, images_in, folder_in,
ckpt_choice, custom_ckpt,
fps, first_k, keyframe_interval,
mask_sky, conf_threshold, camera_iters, use_sdpa, show_cameras,
],
outputs=[model3d, glb_download, info],
)
return demo
def main():
parser = argparse.ArgumentParser()
parser.add_argument("--share", action="store_true")
parser.add_argument("--server_name", default="0.0.0.0")
parser.add_argument("--server_port", type=int, default=7860)
args = parser.parse_args()
demo = build_ui()
demo.queue().launch(
share=args.share,
server_name=args.server_name,
server_port=args.server_port,
)
if __name__ == "__main__":
main()