File size: 4,165 Bytes
9c890d3 | 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 | """Direct SCALE4D inference with the checkpoint's recorded model configuration."""
from __future__ import annotations
from dataclasses import asdict
from pathlib import Path
import numpy as np
import torch
from evaluation.scale4d_inputs import array_clip, npz_clip, video_clip
from evaluation.ucpe_inference import Prediction, UCPEEvaluator
from evaluation.ucpe_inputs import ClipInput
from nets.scale4d import HF_REPO_ID, HF_REVISION, load_model
class SCALE4D:
def __init__(
self,
checkpoint: str | Path = HF_REPO_ID,
*,
device: str | torch.device = "cuda:0",
revision: str | None = None,
config_path: str | Path | None = None,
use_flash_attn: bool = True,
decoder_chunk: int = 512,
max_input_views: int = 3,
) -> None:
device = torch.device(device)
if device.type == "cuda":
if not torch.cuda.is_available():
raise RuntimeError(
"CUDA is unavailable; install a CUDA-compatible PyTorch build"
)
torch.cuda.set_device(device)
model, self.info = load_model(
checkpoint,
revision=revision,
config_path=config_path,
use_flash_attn=use_flash_attn,
)
from evaluation.scale4d_view_count import (
decoder_slot_indices,
extend_decoder_view_embeddings,
)
self.decoder_slots = decoder_slot_indices(max_input_views)
if max_input_views > 3:
extend_decoder_view_embeddings(model, max_input_views)
self.evaluator = UCPEEvaluator(
model,
device,
decoder_chunk=decoder_chunk,
camera_gate_m=self.info.camera_condition_max_diameter_m,
max_input_views=max_input_views,
)
self.checkpoint = str(checkpoint)
self.revision = revision or (
HF_REVISION if self.checkpoint == HF_REPO_ID else None
)
def predict_clip(
self, clip: ClipInput, *, views: int | None = None, camera: str = "off"
) -> Prediction:
result = self.evaluator.predict(
clip, views=clip.rgbs.shape[0] if views is None else views, camera=camera
)
result.metadata.update(
checkpoint=self.checkpoint,
checkpoint_step=self.info.step,
checkpoint_revision=self.revision,
model_config=asdict(self.info),
decoder_slot_indices=list(self.decoder_slots[: len(result.camera_ids)]),
)
return result
def predict(
self,
rgbs: np.ndarray | torch.Tensor,
*,
query_uv: np.ndarray | torch.Tensor | None = None,
query_times: np.ndarray | torch.Tensor | None = None,
cameras_to_world: np.ndarray | torch.Tensor | None = None,
intrinsics: np.ndarray | torch.Tensor | None = None,
world_up: np.ndarray | torch.Tensor | None = None,
grid_size: int = 16,
views: int | None = None,
camera: str = "off",
metadata: dict | None = None,
) -> Prediction:
if camera == "on" and any(
value is None for value in (cameras_to_world, intrinsics, world_up)
):
raise ValueError(
"Camera ON requires cameras_to_world, intrinsics, and world_up"
)
clip = array_clip(
rgbs,
query_uv=query_uv,
query_times=query_times,
cameras_to_world=cameras_to_world,
intrinsics=intrinsics,
world_up=world_up,
grid_size=grid_size,
metadata=metadata,
)
return self.predict_clip(clip, views=views, camera=camera)
def predict_video(self, path: str | Path, *, grid_size: int = 16) -> Prediction:
clip = video_clip(path, self.evaluator.model.encoder.num_frames, grid_size)
return self.predict_clip(clip, camera="off")
def predict_npz(
self, path: str | Path, *, views: int | None = None, camera: str = "off"
) -> Prediction:
return self.predict_clip(npz_clip(path), views=views, camera=camera)
|