---
title: metric
---

<div class="python-reference">
<a id="fastvideo.eval.metrics.vbench.scene.metric-classes"></a>
<a id="fastvideo.eval.metrics.vbench.scene.metric-functions"></a>
<a id="fastvideo.eval.metrics.vbench.scene.metric"></a>
<p>VLM-based scene matching using AVoCaDO (Qwen2.5-Omni).</p>
<p>Replaces VBench's Tag2Text-based scene metric with a modern VLM caption.
The algorithm follows VBench:
  1. Caption the video
  2. Check if all scene keywords appear in the caption
  3. Score = 1.0 if all match, 0.0 otherwise</p>
<p>Unlike VBench (which captions each frame separately with Tag2Text),
AVoCaDO captions the entire video in one pass with rich natural language,
making the keyword check more robust.</p>
<h2 id="fastvideo.eval.metrics.vbench.scene.metric.BaseMetric"><code>fastvideo.eval.metrics.vbench.scene.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.vbench.scene.metric.BaseMetric.to"><code>fastvideo.eval.metrics.vbench.scene.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.vbench.scene.metric.BaseMetric.setup"><code>fastvideo.eval.metrics.vbench.scene.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.vbench.scene.metric.BaseMetric.compute"><code>fastvideo.eval.metrics.vbench.scene.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.vbench.scene.metric.BaseMetric.reset"><code>fastvideo.eval.metrics.vbench.scene.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.vbench.scene.metric.BaseMetric.accumulate"><code>fastvideo.eval.metrics.vbench.scene.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.vbench.scene.metric.BaseMetric.finalize"><code>fastvideo.eval.metrics.vbench.scene.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.vbench.scene.metric.BaseMetric.merge_from"><code>fastvideo.eval.metrics.vbench.scene.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.vbench.scene.metric.register"><code>fastvideo.eval.metrics.vbench.scene.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.vbench.scene.metric.MetricResult"><code>fastvideo.eval.metrics.vbench.scene.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.vbench.scene.metric.SceneMetric"><code>fastvideo.eval.metrics.vbench.scene.metric.SceneMetric</code></h2>
<pre class="doc-signature"><code>SceneMetric(model_path: str = &#x27;AVoCaDO-Captioner/AVoCaDO&#x27;)</code></pre>


<details><summary>Source</summary><pre><code>@register(&quot;vbench.scene&quot;)
class SceneMetric(BaseMetric):

    name = &quot;vbench.scene&quot;
    requires_reference = False
    higher_is_better = True
    needs_gpu = True
    dependencies = &#91;&quot;transformers&quot;, &quot;qwen_omni_utils&quot;&#93;
    backbone = &quot;avocado&quot;

    def __init__(self, model_path: str = &quot;AVoCaDO-Captioner/AVoCaDO&quot;) -&gt; None:
        super().__init__()
        self._model: Any = None
        self._processor: Any = None
        self._model_path = model_path

    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 transformers import Qwen2_5OmniForConditionalGeneration, Qwen2_5OmniProcessor

        # AVoCaDO uses Qwen2.5-Omni — large multimodal model
        envs.setdefault_external(&quot;VIDEO_MAX_PIXELS&quot;, str(20070400))  # 512*28*28*50

        self._model = Qwen2_5OmniForConditionalGeneration.from_pretrained(
            self._model_path,
            torch_dtype=torch.bfloat16,
            device_map=str(self.device) if self.device.type != &quot;cpu&quot; else None,
        )
        self._model.disable_talker()
        self._model.eval()
        self._processor = Qwen2_5OmniProcessor.from_pretrained(self._model_path)

    def _save_temp_video(self, video: torch.Tensor) -&gt; str:
        &quot;&quot;&quot;Save (T, C, H, W) float &#91;0,1&#93; tensor as a temp mp4 file.&quot;&quot;&quot;
        # torchvision expects (T, H, W, C) uint8
        frames = (video * 255).clamp(0, 255).to(torch.uint8).permute(0, 2, 3, 1).cpu()
        # Caller owns the resulting file (it&#x27;s read by Qwen2.5-Omni and
        # cleaned up at end of compute()), so we just need a unique path.
        fd, path = tempfile.mkstemp(suffix=&quot;.mp4&quot;)
        os.close(fd)
        torchvision.io.write_video(path, frames, fps=8, video_codec=&quot;libx264&quot;, options=&#123;&quot;crf&quot;: &quot;18&quot;&#125;)
        return path

    def _generate_caption(self, video_path: str) -&gt; str:
        from qwen_omni_utils import process_mm_info

        conversation = &#91;
            &#123;
                &quot;role&quot;:
                &quot;system&quot;,
                &quot;content&quot;: &#91;&#123;
                    &quot;type&quot;:
                    &quot;text&quot;,
                    &quot;text&quot;: (&quot;You are Qwen, a virtual human developed by the Qwen Team, &quot;
                             &quot;Alibaba Group, capable of perceiving auditory and visual inputs.&quot;)
                &#125;&#93;,
            &#125;,
            &#123;
                &quot;role&quot;:
                &quot;user&quot;,
                &quot;content&quot;: &#91;
                    &#123;
                        &quot;type&quot;: &quot;video&quot;,
                        &quot;video&quot;: video_path,
                        &quot;max_pixels&quot;: 401408
                    &#125;,
                    &#123;
                        &quot;type&quot;: &quot;text&quot;,
                        &quot;text&quot;: _SCENE_PROMPT
                    &#125;,
                &#93;,
            &#125;,
        &#93;

        text = self._processor.apply_chat_template(conversation, add_generation_prompt=True, tokenize=False)
        # AVoCaDO is video+audio; for scene matching we don&#x27;t need audio,
        # but the model expects it so let it process
        audios, images, videos = process_mm_info(conversation, use_audio_in_video=False)
        inputs = self._processor(
            text=text,
            audio=audios,
            images=images,
            videos=videos,
            return_tensors=&quot;pt&quot;,
            padding=True,
            use_audio_in_video=False,
        )
        inputs = inputs.to(self._model.device).to(self._model.dtype)

        with torch.no_grad():
            text_ids = self._model.generate(
                **inputs,
                use_audio_in_video=False,
                return_audio=False,
                do_sample=False,
                thinker_max_new_tokens=512,
            )

        decoded = self._processor.batch_decode(text_ids, skip_special_tokens=True,
                                               clean_up_tokenization_spaces=False)&#91;0&#93;
        return decoded.split(&quot;\nassistant\n&quot;)&#91;-1&#93;.lower()

    @torch.no_grad()
    def compute(self, sample: dict) -&gt; MetricResult:
        video = sample&#91;&quot;video&quot;&#93;  # (T, C, H, W)
        aux = sample.get(&quot;auxiliary_info&quot;) or &#123;&#125;
        if &quot;scene&quot; not in aux:
            return self._skip(sample, &quot;missing &#x27;scene&#x27; in auxiliary_info&quot;)

        scene_keywords = aux&#91;&quot;scene&quot;&#93;
        keywords = &#91;k.strip().lower() for k in scene_keywords.split() if k.strip()&#93;

        tmp_path = self._save_temp_video(video)
        try:
            caption = self._generate_caption(tmp_path)
        finally:
            os.unlink(tmp_path)

        matched = &#91;kw for kw in keywords if kw in caption&#93;
        score = 1.0 if len(matched) == len(keywords) else 0.0
        return MetricResult(
            name=self.name,
            score=score,
            details=&#123;
                &quot;caption&quot;: caption&#91;:500&#93;,
                &quot;keywords&quot;: keywords,
                &quot;matched&quot;: matched,
            &#125;,
        )</code></pre></details>
<h3 id="fastvideo.eval.metrics.vbench.scene.metric.SceneMetric.to"><code>fastvideo.eval.metrics.vbench.scene.metric.SceneMetric.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.vbench.scene.metric.SceneMetric.setup"><code>fastvideo.eval.metrics.vbench.scene.metric.SceneMetric.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 transformers import Qwen2_5OmniForConditionalGeneration, Qwen2_5OmniProcessor

    # AVoCaDO uses Qwen2.5-Omni — large multimodal model
    envs.setdefault_external(&quot;VIDEO_MAX_PIXELS&quot;, str(20070400))  # 512*28*28*50

    self._model = Qwen2_5OmniForConditionalGeneration.from_pretrained(
        self._model_path,
        torch_dtype=torch.bfloat16,
        device_map=str(self.device) if self.device.type != &quot;cpu&quot; else None,
    )
    self._model.disable_talker()
    self._model.eval()
    self._processor = Qwen2_5OmniProcessor.from_pretrained(self._model_path)</code></pre></details>
<h3 id="fastvideo.eval.metrics.vbench.scene.metric.SceneMetric.compute"><code>fastvideo.eval.metrics.vbench.scene.metric.SceneMetric.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:
    video = sample&#91;&quot;video&quot;&#93;  # (T, C, H, W)
    aux = sample.get(&quot;auxiliary_info&quot;) or &#123;&#125;
    if &quot;scene&quot; not in aux:
        return self._skip(sample, &quot;missing &#x27;scene&#x27; in auxiliary_info&quot;)

    scene_keywords = aux&#91;&quot;scene&quot;&#93;
    keywords = &#91;k.strip().lower() for k in scene_keywords.split() if k.strip()&#93;

    tmp_path = self._save_temp_video(video)
    try:
        caption = self._generate_caption(tmp_path)
    finally:
        os.unlink(tmp_path)

    matched = &#91;kw for kw in keywords if kw in caption&#93;
    score = 1.0 if len(matched) == len(keywords) else 0.0
    return MetricResult(
        name=self.name,
        score=score,
        details=&#123;
            &quot;caption&quot;: caption&#91;:500&#93;,
            &quot;keywords&quot;: keywords,
            &quot;matched&quot;: matched,
        &#125;,
    )</code></pre></details>
</div>
