Skip to content
FastVideo
Esc
↑↓navigate↵open⌘Jpreview
On this page

metric

VBench Object Class — GRiT object detection for class matching.

Checks if a target object class is detected in each of 16 sampled frames. Score = matching_frames / total_frames.

fastvideo.eval.metrics.vbench.object_class.metric.BaseMetric

BaseMetric()

Abstract base class for all eval metrics.

Two execution shapes:

  • Per-sample (is_set_metric=False, default) — implement :meth:compute. The Evaluator calls it once per input sample and returns one :class:MetricResult per sample.

  • Set-vs-set (is_set_metric=True) — implement :meth:accumulate (called once per sample to buffer features) and :meth:finalize (called once after all samples to compute the corpus-level result). Use :meth:reset to clear buffers and :meth:merge_from to fold multi-GPU per-worker state together.

Optionally override :meth:setup to eagerly load models. Metrics that chunk along the time dim for memory hardcode their own chunk size in __init__ (see optical_flow for the canonical example). Eval always processes one video per :meth:Evaluator.evaluate call; compute / accumulate receive a single sample, not a batch.

fastvideo.eval.metrics.vbench.object_class.metric.BaseMetric.to

to(device: str | torch.device) -> BaseMetric

Move metric (and its internal models) to device.

fastvideo.eval.metrics.vbench.object_class.metric.BaseMetric.setup

setup() -> None

Eagerly load models. Called once by :class:EvalWorker.

Default is a no-op; metrics with no eager state (pixel math, closed-form ops) inherit this. Override only if your metric needs to load weights.

fastvideo.eval.metrics.vbench.object_class.metric.BaseMetric.compute

compute(sample: dict) -> MetricResult

Per-sample metrics: compute the score for one sample.

sample["video"] is (T, C, H, W) float in [0, 1]. sample["reference"] (if used) has the same shape. Return self._skip(sample, reason) for missing inputs.

fastvideo.eval.metrics.vbench.object_class.metric.BaseMetric.reset

reset() -> None

Clear accumulator state at the start of each evaluate() call.

fastvideo.eval.metrics.vbench.object_class.metric.BaseMetric.accumulate

accumulate(sample: dict) -> None

Buffer per-sample features for a corpus-level metric.

fastvideo.eval.metrics.vbench.object_class.metric.BaseMetric.finalize

finalize() -> MetricResult

Compute the corpus-level result from buffered state.

fastvideo.eval.metrics.vbench.object_class.metric.BaseMetric.merge_from

merge_from(other: BaseMetric) -> None

Multi-GPU: fold another worker's accumulator state into this one.

fastvideo.eval.metrics.vbench.object_class.metric.register

register(name: str)

Decorator to register a metric class.

Usage::

@register("ssim")
class SSIMMetric(BaseMetric):
    ...

fastvideo.eval.metrics.vbench.object_class.metric.MetricResult

MetricResult(name: str, score: float | None, details: dict[str, Any] = dict())

Standard result container returned by all metrics.

score is None when the metric was skipped (e.g. missing required input). Check details["skipped"] for the reason.

fastvideo.eval.metrics.vbench.object_class.metric.ObjectClassMetric

ObjectClassMetric()
Source
@register("vbench.object_class")
class ObjectClassMetric(BaseMetric):
name = "vbench.object_class"
requires_reference = False
higher_is_better = True
needs_gpu = True
dependencies = ["detectron2"]

def __init__(self) -> None:
    super().__init__()
    self._model = None

def setup(self) -> None:
    if self._model is not None:
        return
    from fastvideo.eval.metrics.vbench._grit_helper import load_grit_model
    # VBench's object_class uses ObjectDet head (init_submodules → "ObjectDet")
    self._model = load_grit_model(self.device, task="ObjectDet")

@torch.no_grad()
def compute(self, sample: dict) -> MetricResult:
    from fastvideo.eval.metrics.vbench._grit_helper import prepare_frames, detect_frames

    video = sample["video"]  # (T, C, H, W)
    aux = sample.get("auxiliary_info") or {}
    if "object" not in aux:
        return self._skip(sample, "missing 'object' in auxiliary_info")

    object_key = aux["object"]
    if " and " in object_key:
        # multiple_objects' territory; skip this row for object_class.
        return self._skip(sample, "'object' contains ' and ' (multi-object)")

    frames_np = prepare_frames(video)
    preds = detect_frames(self._model, frames_np)

    matching = 0
    for frame_pred in preds:
        try:
            obj_set = set(frame_pred[0][2]) if frame_pred else set()
        except (IndexError, TypeError):
            obj_set = set()
        if object_key in obj_set:
            matching += 1

    total = len(preds)
    score = matching / total if total > 0 else 0.0
    return MetricResult(
        name=self.name,
        score=float(score),
        details={
            "matching_frames": matching,
            "total_frames": total
        },
    )</code></pre></details>

fastvideo.eval.metrics.vbench.object_class.metric.ObjectClassMetric.setup

setup() -> None
Source
def setup(self) -> None:
    if self._model is not None:
        return
    from fastvideo.eval.metrics.vbench._grit_helper import load_grit_model
    # VBench's object_class uses ObjectDet head (init_submodules → "ObjectDet")
    self._model = load_grit_model(self.device, task="ObjectDet")

fastvideo.eval.metrics.vbench.object_class.metric.ObjectClassMetric.compute

compute(sample: dict) -> MetricResult
Source
@torch.no_grad()
def compute(self, sample: dict) -> MetricResult:
    from fastvideo.eval.metrics.vbench._grit_helper import prepare_frames, detect_frames
video = sample&#91;&quot;video&quot;&#93;  # (T, C, H, W)
aux = sample.get(&quot;auxiliary_info&quot;) or &#123;&#125;
if &quot;object&quot; not in aux:
    return self._skip(sample, &quot;missing &#x27;object&#x27; in auxiliary_info&quot;)

object_key = aux&#91;&quot;object&quot;&#93;
if &quot; and &quot; in object_key:
    # multiple_objects&#x27; territory; skip this row for object_class.
    return self._skip(sample, &quot;&#x27;object&#x27; contains &#x27; and &#x27; (multi-object)&quot;)

frames_np = prepare_frames(video)
preds = detect_frames(self._model, frames_np)

matching = 0
for frame_pred in preds:
    try:
        obj_set = set(frame_pred&#91;0&#93;&#91;2&#93;) if frame_pred else set()
    except (IndexError, TypeError):
        obj_set = set()
    if object_key in obj_set:
        matching += 1

total = len(preds)
score = matching / total if total &gt; 0 else 0.0
return MetricResult(
    name=self.name,
    score=float(score),
    details=&#123;
        &quot;matching_frames&quot;: matching,
        &quot;total_frames&quot;: total
    &#125;,
)</code></pre></details>

Was this page helpful?