---
title: metric
---

<div class="python-reference">
<a id="fastvideo.eval.metrics.audio.imagebind_score.metric-classes"></a>
<a id="fastvideo.eval.metrics.audio.imagebind_score.metric-functions"></a>
<a id="fastvideo.eval.metrics.audio.imagebind_score.metric"></a>
<p>ImageBind audio↔video cosine similarity (<code>IB-Score</code>).</p>
<p>Per-sample. Reads <code>sample["video_path"]</code> (or <code>sample["video"].source</code>
for a :class:<code>Video</code> wrapper) and <code>sample["audio"]</code>; ImageBind decodes
its own clips so the <em>path</em> is required, not the pool-decoded tensor.</p>
<h2 id="fastvideo.eval.metrics.audio.imagebind_score.metric.BaseMetric"><code>fastvideo.eval.metrics.audio.imagebind_score.metric.BaseMetric</code></h2>
<pre class="doc-signature"><code>BaseMetric()</code></pre>
<p>Abstract base class for all eval metrics.</p>
<p>Two execution shapes:</p>
<ul>
<li>
<p><strong>Per-sample</strong> (<code>is_set_metric=False</code>, default) — implement
  :meth:<code>compute</code>. The Evaluator calls it once per input sample and
  returns one :class:<code>MetricResult</code> per sample.</p>
</li>
<li>
<p><strong>Set-vs-set</strong> (<code>is_set_metric=True</code>) — implement
  :meth:<code>accumulate</code> (called once per sample to buffer features)
  and :meth:<code>finalize</code> (called once after all samples to compute
  the corpus-level result). Use :meth:<code>reset</code> to clear buffers and
  :meth:<code>merge_from</code> to fold multi-GPU per-worker state together.</p>
</li>
</ul>
<p>Optionally override :meth:<code>setup</code> to eagerly load models. Metrics
that chunk along the time dim for memory hardcode their own chunk
size in <code>__init__</code> (see <code>optical_flow</code> for the canonical
example). Eval always processes one video per
:meth:<code>Evaluator.evaluate</code> call; <code>compute</code> / <code>accumulate</code>
receive a single sample, not a batch.</p>

<h3 id="fastvideo.eval.metrics.audio.imagebind_score.metric.BaseMetric.to"><code>fastvideo.eval.metrics.audio.imagebind_score.metric.BaseMetric.to</code></h3>
<pre class="doc-signature"><code>to(device: str | torch.device) -&gt; BaseMetric</code></pre>
<p>Move metric (and its internal models) to <em>device</em>.</p>
<h3 id="fastvideo.eval.metrics.audio.imagebind_score.metric.BaseMetric.setup"><code>fastvideo.eval.metrics.audio.imagebind_score.metric.BaseMetric.setup</code></h3>
<pre class="doc-signature"><code>setup() -&gt; None</code></pre>
<p>Eagerly load models. Called once by :class:<code>EvalWorker</code>.</p>
<p>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.</p>
<h3 id="fastvideo.eval.metrics.audio.imagebind_score.metric.BaseMetric.compute"><code>fastvideo.eval.metrics.audio.imagebind_score.metric.BaseMetric.compute</code></h3>
<pre class="doc-signature"><code>compute(sample: dict) -&gt; MetricResult</code></pre>
<p>Per-sample metrics: compute the score for one sample.</p>
<p><code>sample["video"]</code> is <code>(T, C, H, W)</code> float in <code>[0, 1]</code>.
<code>sample["reference"]</code> (if used) has the same shape. Return
<code>self._skip(sample, reason)</code> for missing inputs.</p>
<h3 id="fastvideo.eval.metrics.audio.imagebind_score.metric.BaseMetric.reset"><code>fastvideo.eval.metrics.audio.imagebind_score.metric.BaseMetric.reset</code></h3>
<pre class="doc-signature"><code>reset() -&gt; None</code></pre>
<p>Clear accumulator state at the start of each evaluate() call.</p>
<h3 id="fastvideo.eval.metrics.audio.imagebind_score.metric.BaseMetric.accumulate"><code>fastvideo.eval.metrics.audio.imagebind_score.metric.BaseMetric.accumulate</code></h3>
<pre class="doc-signature"><code>accumulate(sample: dict) -&gt; None</code></pre>
<p>Buffer per-sample features for a corpus-level metric.</p>
<h3 id="fastvideo.eval.metrics.audio.imagebind_score.metric.BaseMetric.finalize"><code>fastvideo.eval.metrics.audio.imagebind_score.metric.BaseMetric.finalize</code></h3>
<pre class="doc-signature"><code>finalize() -&gt; MetricResult</code></pre>
<p>Compute the corpus-level result from buffered state.</p>
<h3 id="fastvideo.eval.metrics.audio.imagebind_score.metric.BaseMetric.merge_from"><code>fastvideo.eval.metrics.audio.imagebind_score.metric.BaseMetric.merge_from</code></h3>
<pre class="doc-signature"><code>merge_from(other: BaseMetric) -&gt; None</code></pre>
<p>Multi-GPU: fold another worker's accumulator state into this one.</p>
<h2 id="fastvideo.eval.metrics.audio.imagebind_score.metric.ensure_checkpoint"><code>fastvideo.eval.metrics.audio.imagebind_score.metric.ensure_checkpoint</code></h2>
<pre class="doc-signature"><code>ensure_checkpoint(name: str, source: str, filename: str | None = None) -&gt; str</code></pre>
<p>Resolve a model checkpoint path, downloading on miss.</p>
<p>See module docstring for the full source contract. <em>name</em> is used
only as the local cache filename for URL sources; ignored otherwise.</p>
<h2 id="fastvideo.eval.metrics.audio.imagebind_score.metric.register"><code>fastvideo.eval.metrics.audio.imagebind_score.metric.register</code></h2>
<pre class="doc-signature"><code>register(name: str)</code></pre>
<p>Decorator to register a metric class.</p>
<p>Usage::</p>
<pre><code>@register("ssim")
class SSIMMetric(BaseMetric):
    ...
</code></pre>
<h2 id="fastvideo.eval.metrics.audio.imagebind_score.metric.MetricResult"><code>fastvideo.eval.metrics.audio.imagebind_score.metric.MetricResult</code></h2>
<pre class="doc-signature"><code>MetricResult(name: str, score: float | None, details: dict&#91;str, Any&#93; = dict())</code></pre>
<p>Standard result container returned by all metrics.</p>
<p><code>score</code> is <code>None</code> when the metric was skipped (e.g. missing
required input).  Check <code>details["skipped"]</code> for the reason.</p>

<h2 id="fastvideo.eval.metrics.audio.imagebind_score.metric.Video"><code>fastvideo.eval.metrics.audio.imagebind_score.metric.Video</code></h2>
<pre class="doc-signature"><code>Video(source: Any, fps: float | None = None, frames: Any = None, audio: Any = None, audio_sr: int | None = None)</code></pre>
<p>Path-backed media handle. The :class:<code>VideoPool</code> populates
<code>frames</code> (and optionally <code>audio</code>) before the metric loop sees
the sample.</p>

<h3 id="fastvideo.eval.metrics.audio.imagebind_score.metric.Video.has_frames"><code>fastvideo.eval.metrics.audio.imagebind_score.metric.Video.has_frames</code></h3>
<pre class="doc-signature"><code>has_frames() -&gt; bool</code></pre>

<h3 id="fastvideo.eval.metrics.audio.imagebind_score.metric.Video.has_audio"><code>fastvideo.eval.metrics.audio.imagebind_score.metric.Video.has_audio</code></h3>
<pre class="doc-signature"><code>has_audio() -&gt; bool</code></pre>

<h2 id="fastvideo.eval.metrics.audio.imagebind_score.metric.ImageBindScoreMetric"><code>fastvideo.eval.metrics.audio.imagebind_score.metric.ImageBindScoreMetric</code></h2>
<pre class="doc-signature"><code>ImageBindScoreMetric()</code></pre>
<p>ImageBind audio↔video cosine similarity, per-sample.</p>

<details><summary>Source</summary><pre><code>@register(&quot;audio.imagebind_score&quot;)
class ImageBindScoreMetric(BaseMetric):
    &quot;&quot;&quot;ImageBind audio↔video cosine similarity, per-sample.&quot;&quot;&quot;

    name = &quot;audio.imagebind_score&quot;
    requires_reference = False
    higher_is_better = True
    needs_gpu = True
    is_set_metric = False
    backbone = &quot;imagebind&quot;
    dependencies = &#91;&quot;imagebind&quot;, &quot;decord&quot;, &quot;pytorchvideo&quot;&#93;

    def __init__(self) -&gt; None:
        super().__init__()
        self._model: Any = None

    def to(self, device):
        super().to(device)
        if self._model is not None:
            self._model = self._model.to(self.device)
        return self

    def setup(self) -&gt; None:
        if self._model is not None:
            return
        from imagebind.models import imagebind_model
        # Route the state-dict through ``ensure_checkpoint``; the stock
        # ``imagebind_huge(pretrained=True)`` hardcodes ``.checkpoints/`` in cwd.
        model = imagebind_model.imagebind_huge(pretrained=False)
        ckpt = ensure_checkpoint(_IMAGEBIND_NAME, _IMAGEBIND_URL)
        model.load_state_dict(torch.load(ckpt, weights_only=True, map_location=&quot;cpu&quot;))
        self._model = model.eval().to(self.device)

    @torch.no_grad()
    def compute(self, sample: dict) -&gt; MetricResult:
        if self._model is None:
            self.setup()

        video_path = _video_source(sample)
        audio_path = sample.get(&quot;audio&quot;)
        missing = &#91;&#93;
        if video_path is None:
            missing.append(&quot;&#x27;video_path&#x27; or &#x27;video&#x27; with a path source&quot;)
        if audio_path is None:
            missing.append(&quot;&#x27;audio&#x27;&quot;)
        if missing:
            return self._skip(sample, f&quot;missing &#123;&#x27;, &#x27;.join(missing)&#125;&quot;)

        import decord
        from imagebind import data as ib_data
        from imagebind.models.imagebind_model import ModalityType

        # decord.bridge is thread-local with a missing-``global`` upstream
        # bug; worker threads otherwise see ``&quot;native&quot;`` and pytorchvideo
        # crashes on the resulting NDArray. Re-set it on this thread.
        decord.bridge.set_bridge(&quot;torch&quot;)
        with _IB_DECODE_LOCK:
            inputs = &#123;
                ModalityType.VISION: ib_data.load_and_transform_video_data(&#91;video_path&#93;, self.device),
                ModalityType.AUDIO: ib_data.load_and_transform_audio_data(&#91;audio_path&#93;, self.device),
            &#125;
        embeds = self._model(inputs)
        score = F.cosine_similarity(embeds&#91;ModalityType.VISION&#93;, embeds&#91;ModalityType.AUDIO&#93;, dim=-1)&#91;0&#93;.item()
        return MetricResult(name=self.name, score=float(score), details=&#123;&#125;)</code></pre></details>
<h3 id="fastvideo.eval.metrics.audio.imagebind_score.metric.ImageBindScoreMetric.to"><code>fastvideo.eval.metrics.audio.imagebind_score.metric.ImageBindScoreMetric.to</code></h3>
<pre class="doc-signature"><code>to(device)</code></pre>

<details><summary>Source</summary><pre><code>def to(self, device):
    super().to(device)
    if self._model is not None:
        self._model = self._model.to(self.device)
    return self</code></pre></details>
<h3 id="fastvideo.eval.metrics.audio.imagebind_score.metric.ImageBindScoreMetric.setup"><code>fastvideo.eval.metrics.audio.imagebind_score.metric.ImageBindScoreMetric.setup</code></h3>
<pre class="doc-signature"><code>setup() -&gt; None</code></pre>

<details><summary>Source</summary><pre><code>def setup(self) -&gt; None:
    if self._model is not None:
        return
    from imagebind.models import imagebind_model
    # Route the state-dict through ``ensure_checkpoint``; the stock
    # ``imagebind_huge(pretrained=True)`` hardcodes ``.checkpoints/`` in cwd.
    model = imagebind_model.imagebind_huge(pretrained=False)
    ckpt = ensure_checkpoint(_IMAGEBIND_NAME, _IMAGEBIND_URL)
    model.load_state_dict(torch.load(ckpt, weights_only=True, map_location=&quot;cpu&quot;))
    self._model = model.eval().to(self.device)</code></pre></details>
<h3 id="fastvideo.eval.metrics.audio.imagebind_score.metric.ImageBindScoreMetric.compute"><code>fastvideo.eval.metrics.audio.imagebind_score.metric.ImageBindScoreMetric.compute</code></h3>
<pre class="doc-signature"><code>compute(sample: dict) -&gt; MetricResult</code></pre>

<details><summary>Source</summary><pre><code>@torch.no_grad()
def compute(self, sample: dict) -&gt; MetricResult:
    if self._model is None:
        self.setup()

    video_path = _video_source(sample)
    audio_path = sample.get(&quot;audio&quot;)
    missing = &#91;&#93;
    if video_path is None:
        missing.append(&quot;&#x27;video_path&#x27; or &#x27;video&#x27; with a path source&quot;)
    if audio_path is None:
        missing.append(&quot;&#x27;audio&#x27;&quot;)
    if missing:
        return self._skip(sample, f&quot;missing &#123;&#x27;, &#x27;.join(missing)&#125;&quot;)

    import decord
    from imagebind import data as ib_data
    from imagebind.models.imagebind_model import ModalityType

    # decord.bridge is thread-local with a missing-``global`` upstream
    # bug; worker threads otherwise see ``&quot;native&quot;`` and pytorchvideo
    # crashes on the resulting NDArray. Re-set it on this thread.
    decord.bridge.set_bridge(&quot;torch&quot;)
    with _IB_DECODE_LOCK:
        inputs = &#123;
            ModalityType.VISION: ib_data.load_and_transform_video_data(&#91;video_path&#93;, self.device),
            ModalityType.AUDIO: ib_data.load_and_transform_audio_data(&#91;audio_path&#93;, self.device),
        &#125;
    embeds = self._model(inputs)
    score = F.cosine_similarity(embeds&#91;ModalityType.VISION&#93;, embeds&#91;ModalityType.AUDIO&#93;, dim=-1)&#91;0&#93;.item()
    return MetricResult(name=self.name, score=float(score), details=&#123;&#125;)</code></pre></details>
</div>
