---
title: metric
---

<div class="python-reference">
<a id="fastvideo.eval.metrics.vbench.human_action.metric-classes"></a>
<a id="fastvideo.eval.metrics.vbench.human_action.metric-functions"></a>
<a id="fastvideo.eval.metrics.vbench.human_action.metric"></a>
<p>VBench Human Action — UMT ViT-L/16 action classification (Kinetics-400).</p>
<p>Classifies human actions in 16-frame clips. Top-5 predictions with
confidence &gt;= 0.85 are compared against the ground-truth action label.
Score = 1.0 if match found, 0.0 otherwise.</p>
<h2 id="fastvideo.eval.metrics.vbench.human_action.metric.BaseMetric"><code>fastvideo.eval.metrics.vbench.human_action.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.human_action.metric.BaseMetric.to"><code>fastvideo.eval.metrics.vbench.human_action.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.human_action.metric.BaseMetric.setup"><code>fastvideo.eval.metrics.vbench.human_action.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.human_action.metric.BaseMetric.compute"><code>fastvideo.eval.metrics.vbench.human_action.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.human_action.metric.BaseMetric.reset"><code>fastvideo.eval.metrics.vbench.human_action.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.human_action.metric.BaseMetric.accumulate"><code>fastvideo.eval.metrics.vbench.human_action.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.human_action.metric.BaseMetric.finalize"><code>fastvideo.eval.metrics.vbench.human_action.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.human_action.metric.BaseMetric.merge_from"><code>fastvideo.eval.metrics.vbench.human_action.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.human_action.metric.register"><code>fastvideo.eval.metrics.vbench.human_action.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.human_action.metric.MetricResult"><code>fastvideo.eval.metrics.vbench.human_action.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.human_action.metric.extract_frames"><code>fastvideo.eval.metrics.vbench.human_action.metric.extract_frames</code></h2>
<pre class="doc-signature"><code>extract_frames(video: torch.Tensor, n_frames: int | None = None) -&gt; torch.Tensor</code></pre>
<p>Uniformly sample <em>n_frames</em> from a <code>(T, C, H, W)</code> video tensor.</p>
<h2 id="fastvideo.eval.metrics.vbench.human_action.metric.HumanActionMetric"><code>fastvideo.eval.metrics.vbench.human_action.metric.HumanActionMetric</code></h2>
<pre class="doc-signature"><code>HumanActionMetric()</code></pre>


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

    name = &quot;vbench.human_action&quot;
    requires_reference = False
    higher_is_better = True
    needs_gpu = True
    dependencies = &#91;&quot;timm&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 timm.models import create_model
        from fastvideo.eval.models import ensure_checkpoint

        ckpt_path = ensure_checkpoint(
            &quot;umt_l16_kinetics400.pth&quot;,
            source=&quot;OpenGVLab/VBench_Used_Models&quot;,
            filename=&quot;l16_ptk710_ftk710_ftk400_f16_res224.pth&quot;,
        )

        import vbench.third_party.umt.models.modeling_finetune  # noqa: F401

        self._model = create_model(
            &quot;vit_large_patch16_224&quot;,
            pretrained=False,
            num_classes=400,
            all_frames=16,
            tubelet_size=1,
            use_learnable_pos_emb=False,
            fc_drop_rate=0.0,
            drop_rate=0.0,
            drop_path_rate=0.2,
            attn_drop_rate=0.0,
            drop_block_rate=None,
            use_checkpoint=False,
            checkpoint_num=16,
            use_mean_pooling=True,
            init_scale=0.001,
        )
        state_dict = torch.load(ckpt_path, map_location=&quot;cpu&quot;, weights_only=False)
        self._model.load_state_dict(state_dict, strict=False)
        self._model.to(self.device)
        self._model.eval()

    @torch.no_grad()
    def compute(self, sample: dict) -&gt; MetricResult:
        video = sample&#91;&quot;video&quot;&#93;  # (T, C, H, W) &#91;0, 1&#93;
        text_prompt = sample.get(&quot;text_prompt&quot;)
        if text_prompt is None:
            return self._skip(sample, &quot;missing text_prompt with action labels&quot;)

        cat_dict = _load_cat_dict()

        frames = extract_frames(video, 16)  # (16, C, H, W)
        frames = resize(frames, 256, antialias=True)
        frames = center_crop(frames, 224)
        frames = normalize(frames, mean=&#91;0.485, 0.456, 0.406&#93;, std=&#91;0.229, 0.224, 0.225&#93;)
        # UMT expects (C, T, H, W); add a leading batch dim of 1.
        clip_in = frames.permute(1, 0, 2, 3).unsqueeze(0).to(self.device)

        logits = torch.sigmoid(self._model(clip_in))  # (1, 400)
        top_scores, top_indices = torch.topk(logits&#91;0&#93;, 5)
        top_indices = top_indices.tolist()
        top_scores = top_scores.tolist()

        predictions = &#91;
            cat_dict.get(str(idx), &quot;&quot;) for idx, score in zip(top_indices, top_scores, strict=False) if score &gt;= 0.85
        &#93;
        gt_label = text_prompt.lower().strip()
        match = any(pred == gt_label for pred in predictions)
        return MetricResult(
            name=self.name,
            score=1.0 if match else 0.0,
            details=&#123;
                &quot;predictions&quot;: predictions,
                &quot;ground_truth&quot;: gt_label
            &#125;,
        )</code></pre></details>
<h3 id="fastvideo.eval.metrics.vbench.human_action.metric.HumanActionMetric.to"><code>fastvideo.eval.metrics.vbench.human_action.metric.HumanActionMetric.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.human_action.metric.HumanActionMetric.setup"><code>fastvideo.eval.metrics.vbench.human_action.metric.HumanActionMetric.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 timm.models import create_model
    from fastvideo.eval.models import ensure_checkpoint

    ckpt_path = ensure_checkpoint(
        &quot;umt_l16_kinetics400.pth&quot;,
        source=&quot;OpenGVLab/VBench_Used_Models&quot;,
        filename=&quot;l16_ptk710_ftk710_ftk400_f16_res224.pth&quot;,
    )

    import vbench.third_party.umt.models.modeling_finetune  # noqa: F401

    self._model = create_model(
        &quot;vit_large_patch16_224&quot;,
        pretrained=False,
        num_classes=400,
        all_frames=16,
        tubelet_size=1,
        use_learnable_pos_emb=False,
        fc_drop_rate=0.0,
        drop_rate=0.0,
        drop_path_rate=0.2,
        attn_drop_rate=0.0,
        drop_block_rate=None,
        use_checkpoint=False,
        checkpoint_num=16,
        use_mean_pooling=True,
        init_scale=0.001,
    )
    state_dict = torch.load(ckpt_path, map_location=&quot;cpu&quot;, weights_only=False)
    self._model.load_state_dict(state_dict, strict=False)
    self._model.to(self.device)
    self._model.eval()</code></pre></details>
<h3 id="fastvideo.eval.metrics.vbench.human_action.metric.HumanActionMetric.compute"><code>fastvideo.eval.metrics.vbench.human_action.metric.HumanActionMetric.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) &#91;0, 1&#93;
    text_prompt = sample.get(&quot;text_prompt&quot;)
    if text_prompt is None:
        return self._skip(sample, &quot;missing text_prompt with action labels&quot;)

    cat_dict = _load_cat_dict()

    frames = extract_frames(video, 16)  # (16, C, H, W)
    frames = resize(frames, 256, antialias=True)
    frames = center_crop(frames, 224)
    frames = normalize(frames, mean=&#91;0.485, 0.456, 0.406&#93;, std=&#91;0.229, 0.224, 0.225&#93;)
    # UMT expects (C, T, H, W); add a leading batch dim of 1.
    clip_in = frames.permute(1, 0, 2, 3).unsqueeze(0).to(self.device)

    logits = torch.sigmoid(self._model(clip_in))  # (1, 400)
    top_scores, top_indices = torch.topk(logits&#91;0&#93;, 5)
    top_indices = top_indices.tolist()
    top_scores = top_scores.tolist()

    predictions = &#91;
        cat_dict.get(str(idx), &quot;&quot;) for idx, score in zip(top_indices, top_scores, strict=False) if score &gt;= 0.85
    &#93;
    gt_label = text_prompt.lower().strip()
    match = any(pred == gt_label for pred in predictions)
    return MetricResult(
        name=self.name,
        score=1.0 if match else 0.0,
        details=&#123;
            &quot;predictions&quot;: predictions,
            &quot;ground_truth&quot;: gt_label
        &#125;,
    )</code></pre></details>
</div>
