Spaces:
Running on Zero
Running on Zero
measured GPU durations, lean defaults, drop debug output
Browse files26 unmodified lines27282930313233343536373839404142434445464715 unmodified lines63646566676869124 unmodified lines19419519619719819920018 unmodified lines219220221222223224225226227228229110 unmodified lines3403413423433443453463473483493503513521 unmodified line3543553563573583593603613623633643653669 unmodified lines37637737837938038138222 unmodified lines40540640740840941041135 unmodified lines44744844945045145245345426 unmodified linesexcept Exception:_pip_no_deps("git+https://github.com/microsoft/MoGe.git@a8c37341bc0325ca99b9d57981cc3bb2bd3e255b")import faulthandler # noqa: E402faulthandler.enable(all_threads=True)import types # noqa: E402def _cuda_libs(tag):"""Debug: which CUDA libraries are mapped into this process (libcuda in the main process poisons the fork)."""try:maps = open("/proc/self/maps").read()libs = sorted({l.split("/")[-1] for l in maps.splitlines() if any(k in l for k in ("libcuda.so", "libnvrtc", "libcudart"))})print(f"[cuda-libs] {tag}: {libs}", flush=True)except Exception as e:print(f"[cuda-libs] {tag}: {e}", flush=True)# open3d's Linux wheels are CUDA builds; SAM 3D imports it only for plane estimation (unused here).sys.modules.setdefault("open3d", types.ModuleType("open3d"))15 unmodified linesfrom huggingface_hub import hf_hub_download, snapshot_download # noqa: E402from PIL import Image # noqa: E402_cuda_libs("after torch/gradio")import anything4d # noqa: E402,F401 (sets SAM 3D env defaults + kaolin stand-in)# ----------------------------------------------------------------------------------------------124 unmodified linesDECODER.load_state_dict({k: v for k, v in _st["model"].items() if not k.startswith("frozen.")}, strict=True)del _st_cuda_libs("after sam3d/4DAnything build")PIPE.models.to("cuda")for _emb in PIPE.condition_embedders.values():if _emb is not None:18 unmodified linesDINO = AutoModelForZeroShotObjectDetection.from_pretrained(a4d_seg.DINO_ID).eval().to("cuda")DINO_PROC = AutoProcessor.from_pretrained(a4d_seg.DINO_ID)_cuda_libs("after SAM2/DINO/MoGe")from rembg import new_session, remove # noqa: E402BIREFNET = new_session("birefnet-general", providers=["CPUExecutionProvider"])_cuda_libs("after rembg")# ---- reference functions, rewired to the preloaded models (logic copied 1:1) ----110 unmodified lines# GPU stages# ----------------------------------------------------------------------------------------------def _pre_duration(video, clip_dir, prompt, box, points, num_frames, stride, *a, **k):return 150@spaces.GPU(duration=_pre_duration)def gpu_precompute(video, clip_dir, prompt, box, points, num_frames, stride):t0 = time.perf_counter()print(f"[gpu_precompute] start, cuda={torch.cuda.is_available()}", flush=True)a4d_pre.precompute(video, clip_dir, prompt=prompt or None, box=box, points=points,num_frames=num_frames, stride=stride, long_side=854,pointmap_model="moge")1 unmodified linedef _rec_duration(clip_dir, seed, fps, num_frames, *a, **k):return 300@spaces.GPU(duration=_rec_duration)def gpu_reconstruct(clip_dir, seed, fps, num_frames):t0 = time.perf_counter()print("[gpu_reconstruct] start", flush=True)a4d_rec.reconstruct(clip_dir, WS, None, None, None, None, None, canonical_frame=0, pose_smooth_window=5,seed=int(seed))t1 = time.perf_counter()9 unmodified linesreturn 24.0def reconstruct_4d(video: str, object_name: str = "", num_frames: int = 24, frame_stride: int = 2, seed: int = 42,progress=gr.Progress(track_tqdm=True)):"""Reconstruct one dynamic object from a monocular video as a complete 4D Gaussian asset.22 unmodified linesraise gr.Error("The video is too short; it needs at least 3 frames after striding.")box, pt = salient_box(frames[len(frames) // 2])points = [pt]print(f"[salient] box={box} point={pt}", flush=True)progress(0.05, desc="Masks (SAM 2.1) + pointmaps (MoGe)")t_pre = gpu_precompute(video, clip_dir, object_name, box, points, num_frames, frame_stride)35 unmodified linesobject_in = gr.Textbox(label="Object (optional)", placeholder="e.g. kangaroo — empty picks the most salient object")run_btn = gr.Button("Reconstruct 4D", variant="primary")with gr.Accordion("Advanced settings", open=False):frames_in = gr.Slider(8, 32, value=24, step=1, label="Frames")stride_in = gr.Slider(1, 4, value=2, step=1, label="Frame stride")seed_in = gr.Number(value=42, precision=0, label="Seed")mask_out = gr.Image(label="Tracked object (middle frame)", interactive=False)with gr.Column(scale=2):26 unmodified lines27282930313233343515 unmodified lines515253545556124 unmodified lines18118218318418518618 unmodified lines205206207208209210211212213110 unmodified lines3243253263273283293303313323333343351 unmodified line3373383393403413423433443453463473483499 unmodified lines35936036136236336436522 unmodified lines38838939039139239335 unmodified lines42943043143243343443543626 unmodified linesexcept Exception:_pip_no_deps("git+https://github.com/microsoft/MoGe.git@a8c37341bc0325ca99b9d57981cc3bb2bd3e255b")import types # noqa: E402# open3d's Linux wheels are CUDA builds; SAM 3D imports it only for plane estimation (unused here).sys.modules.setdefault("open3d", types.ModuleType("open3d"))15 unmodified linesfrom huggingface_hub import hf_hub_download, snapshot_download # noqa: E402from PIL import Image # noqa: E402import anything4d # noqa: E402,F401 (sets SAM 3D env defaults + kaolin stand-in)# ----------------------------------------------------------------------------------------------124 unmodified linesDECODER.load_state_dict({k: v for k, v in _st["model"].items() if not k.startswith("frozen.")}, strict=True)del _stPIPE.models.to("cuda")for _emb in PIPE.condition_embedders.values():if _emb is not None:18 unmodified linesDINO = AutoModelForZeroShotObjectDetection.from_pretrained(a4d_seg.DINO_ID).eval().to("cuda")DINO_PROC = AutoProcessor.from_pretrained(a4d_seg.DINO_ID)from rembg import new_session, remove # noqa: E402BIREFNET = new_session("birefnet-general", providers=["CPUExecutionProvider"])# ---- reference functions, rewired to the preloaded models (logic copied 1:1) ----110 unmodified lines# GPU stages# ----------------------------------------------------------------------------------------------def _pre_duration(video, clip_dir, prompt, box, points, num_frames, stride, *a, **k):return int(25 + 0.8 * int(num_frames))@spaces.GPU(duration=_pre_duration)def gpu_precompute(video, clip_dir, prompt, box, points, num_frames, stride):t0 = time.perf_counter()a4d_pre.precompute(video, clip_dir, prompt=prompt or None, box=box, points=points,num_frames=num_frames, stride=stride, long_side=854,pointmap_model="moge")1 unmodified linedef _rec_duration(clip_dir, seed, fps, num_frames, *a, **k):F = int(num_frames)return int(1.15 * (6.9 * F + 0.104 * F * F) + 10)@spaces.GPU(duration=_rec_duration)def gpu_reconstruct(clip_dir, seed, fps, num_frames):t0 = time.perf_counter()a4d_rec.reconstruct(clip_dir, WS, None, None, None, None, None, canonical_frame=0, pose_smooth_window=5,seed=int(seed))t1 = time.perf_counter()9 unmodified linesreturn 24.0def reconstruct_4d(video: str, object_name: str = "", num_frames: int = 16, frame_stride: int = 3, seed: int = 42,progress=gr.Progress(track_tqdm=True)):"""Reconstruct one dynamic object from a monocular video as a complete 4D Gaussian asset.22 unmodified linesraise gr.Error("The video is too short; it needs at least 3 frames after striding.")box, pt = salient_box(frames[len(frames) // 2])points = [pt]progress(0.05, desc="Masks (SAM 2.1) + pointmaps (MoGe)")t_pre = gpu_precompute(video, clip_dir, object_name, box, points, num_frames, frame_stride)35 unmodified linesobject_in = gr.Textbox(label="Object (optional)", placeholder="e.g. kangaroo — empty picks the most salient object")run_btn = gr.Button("Reconstruct 4D", variant="primary")with gr.Accordion("Advanced settings", open=False):frames_in = gr.Slider(8, 24, value=16, step=1, label="Frames")stride_in = gr.Slider(1, 4, value=3, step=1, label="Frame stride")seed_in = gr.Number(value=42, precision=0, label="Seed")mask_out = gr.Image(label="Tracked object (middle frame)", interactive=False)with gr.Column(scale=2):
app.py+6-24