dliu1014's picture Xuweiyi's picture meta-bot's picture
Initial data commit
9c890d3
Raw History Blame Contribute Delete
4.4 kB
"""Portable frozen benchmark inputs, separate from target-camera scoring labels."""
from __future__ import annotations
import hashlib
import json
from dataclasses import dataclass
from pathlib import Path
import numpy as np
import torch
@dataclass(frozen=True)
class ClipInput:
rgbs: torch.Tensor
cameras_to_world: torch.Tensor
intrinsics: torch.Tensor
query_uv: torch.Tensor
query_times: torch.Tensor
query_views: torch.Tensor
world_up: torch.Tensor | None
metadata: dict
def validate(self) -> None:
if (
self.rgbs.ndim != 5
or self.rgbs.shape[2] != 3
or self.rgbs.dtype != torch.uint8
):
raise ValueError("RGB must be uint8 with shape (V, T, 3, H, W)")
views, frames = self.rgbs.shape[:2]
if self.cameras_to_world.shape != (views, frames, 4, 4):
raise ValueError("Camera poses must have shape (V, T, 4, 4)")
if self.intrinsics.shape != (views, frames, 3, 3):
raise ValueError("Intrinsics must have shape (V, T, 3, 3)")
count = len(self.query_times)
if (
count == 0
or self.query_uv.shape != (count, 2)
or self.query_views.shape != (count,)
):
raise ValueError("Expected nonempty, matching UV/time/view queries")
if (
self.query_times.dtype != torch.int64
or self.query_views.dtype != torch.int64
):
raise ValueError("Query times and view indices must be int64")
if bool(((self.query_times < 0) | (self.query_times >= frames)).any()):
raise ValueError("Query times fall outside the native frame timeline")
if bool((self.query_views != 0).any()):
raise ValueError("TAPVid3D queries must select the first configured camera")
if not bool(torch.isfinite(self.query_uv).all()):
raise ValueError("Query pixels must be finite")
if self.world_up is not None and self.world_up.shape != (3,):
raise ValueError("World up must be a three-vector")
if len(self.metadata["camera_ids"]) != views:
raise ValueError("Camera identities differ from the supplied view count")
@dataclass(frozen=True)
class ClipLabels:
tracks_target_camera: np.ndarray
visibility: np.ndarray
visibility_known: np.ndarray | None = None
def load_clip(path: str | Path) -> tuple[ClipInput, ClipLabels | None]:
with np.load(path, allow_pickle=False) as data:
metadata = json.loads(str(data["metadata_json"].item()))
if metadata["format_version"] != 1:
raise ValueError("Unsupported frozen benchmark bundle version")
values = {
name: torch.from_numpy(data[name].copy())
for name in (
"rgbs",
"cameras_to_world",
"intrinsics",
"query_uv",
"query_times",
"query_views",
)
}
clip = ClipInput(
**values,
metadata=metadata,
world_up=torch.from_numpy(data["world_up"].copy())
if "world_up" in data
else None,
)
labels = None
if "gt_tracks_target_camera" in data:
labels = ClipLabels(
data["gt_tracks_target_camera"].copy(),
data["gt_visibility"].copy(),
data["gt_visibility_known"].copy()
if "gt_visibility_known" in data
else None,
)
clip.validate()
return clip, labels
def sha256_file(path: str | Path) -> str:
digest = hashlib.sha256()
with Path(path).open("rb") as handle:
for block in iter(lambda: handle.read(8 * 1024 * 1024), b""):
digest.update(block)
return digest.hexdigest()
def pstudio_camera_ids(target: int, camera_centers: dict[int, np.ndarray]) -> list[int]:
"""Select the target and the two nearest calibrated camera centers."""
if target not in camera_centers or len(camera_centers) < 3:
raise ValueError("PStudio needs the target and at least two support cameras")
distances = sorted(
(float(np.linalg.norm(center - camera_centers[target])), camera)
for camera, center in camera_centers.items()
if camera != target
)
return [target, distances[0][1], distances[1][1]]