---
title: metric
---

<div class="python-reference">
<a id="fastvideo.eval.metrics.optical_flow.gt_optical_flow.metric-classes"></a>
<a id="fastvideo.eval.metrics.optical_flow.gt_optical_flow.metric-functions"></a>
<a id="fastvideo.eval.metrics.optical_flow.gt_optical_flow.metric"></a>
<p>Compare optical flow extracted from a generated video against optical
flow extracted from a ground-truth reference video.</p>
<p>Both flows are produced by the same <code>ptlflow</code> model (default
<code>dpflow</code>/<code>things</code>). The resulting per-pixel / per-frame / temporal
metric set is identical to <code>synthetic_optical_flow</code> — only the way the
<em>reference</em> flow is constructed differs.</p>
<h2 id="fastvideo.eval.metrics.optical_flow.gt_optical_flow.metric.BaseMetric"><code>fastvideo.eval.metrics.optical_flow.gt_optical_flow.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.optical_flow.gt_optical_flow.metric.BaseMetric.to"><code>fastvideo.eval.metrics.optical_flow.gt_optical_flow.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.optical_flow.gt_optical_flow.metric.BaseMetric.setup"><code>fastvideo.eval.metrics.optical_flow.gt_optical_flow.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.optical_flow.gt_optical_flow.metric.BaseMetric.compute"><code>fastvideo.eval.metrics.optical_flow.gt_optical_flow.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.optical_flow.gt_optical_flow.metric.BaseMetric.reset"><code>fastvideo.eval.metrics.optical_flow.gt_optical_flow.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.optical_flow.gt_optical_flow.metric.BaseMetric.accumulate"><code>fastvideo.eval.metrics.optical_flow.gt_optical_flow.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.optical_flow.gt_optical_flow.metric.BaseMetric.finalize"><code>fastvideo.eval.metrics.optical_flow.gt_optical_flow.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.optical_flow.gt_optical_flow.metric.BaseMetric.merge_from"><code>fastvideo.eval.metrics.optical_flow.gt_optical_flow.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.optical_flow.gt_optical_flow.metric.aggregate_temporal"><code>fastvideo.eval.metrics.optical_flow.gt_optical_flow.metric.aggregate_temporal</code></h2>
<pre class="doc-signature"><code>aggregate_temporal(per_frame: list&#91;dict&#91;str, float&#93;&#93;) -&gt; dict&#91;str, float | int | None&#93;</code></pre>
<p>Aggregate per-frame metric dicts into mean/std/max/auc/onset summaries.</p>
<p>Port of mhuo's compute_temporal_metrics.</p>
<h2 id="fastvideo.eval.metrics.optical_flow.gt_optical_flow.metric.compute_frame_metrics"><code>fastvideo.eval.metrics.optical_flow.gt_optical_flow.metric.compute_frame_metrics</code></h2>
<pre class="doc-signature"><code>compute_frame_metrics(flow_gt: np.ndarray, flow_gen: np.ndarray, grid_size: int = 8, min_mag: float = 0.5, max_mag_pct: float = 80.0) -&gt; dict&#91;str, float&#93;</code></pre>
<p>Per-frame comparison metrics between two HxWx2 flow fields.</p>
<p>Port of mhuo's compute_frame_metrics — see ptlflow_validation.py.</p>
<h2 id="fastvideo.eval.metrics.optical_flow.gt_optical_flow.metric.extract_video_flows"><code>fastvideo.eval.metrics.optical_flow.gt_optical_flow.metric.extract_video_flows</code></h2>
<pre class="doc-signature"><code>extract_video_flows(model, video: torch.Tensor, *, chunk: int, device: torch.device) -&gt; list&#91;np.ndarray&#93;</code></pre>
<p>Run <em>model</em> on every consecutive frame pair in <em>video</em>.</p>
<p>Returns a list of HxWx2 flow arrays of length <code>T - 1</code>.</p>
<h2 id="fastvideo.eval.metrics.optical_flow.gt_optical_flow.metric.load_ptlflow_model"><code>fastvideo.eval.metrics.optical_flow.gt_optical_flow.metric.load_ptlflow_model</code></h2>
<pre class="doc-signature"><code>load_ptlflow_model(model_name: str, ckpt: str, device: torch.device)</code></pre>
<p>Load a <code>ptlflow</code> model on <em>device</em> in eval mode.</p>
<h2 id="fastvideo.eval.metrics.optical_flow.gt_optical_flow.metric.register"><code>fastvideo.eval.metrics.optical_flow.gt_optical_flow.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.optical_flow.gt_optical_flow.metric.MetricResult"><code>fastvideo.eval.metrics.optical_flow.gt_optical_flow.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.optical_flow.gt_optical_flow.metric.GtOpticalFlowMetric"><code>fastvideo.eval.metrics.optical_flow.gt_optical_flow.metric.GtOpticalFlowMetric</code></h2>
<pre class="doc-signature"><code>GtOpticalFlowMetric(model_name: str = &#x27;dpflow&#x27;, ckpt: str = &#x27;things&#x27;, min_mag: float = 0.5, max_mag_pct: float = 80.0, grid_size: int = 8)</code></pre>
<p>Per-pixel / per-frame / temporal flow comparison vs. a reference video.</p>
<p>The headline <code>score</code> is <code>pixel_epe_mean_mean</code> (lower is better);
every other scalar lives in <code>details</code> so downstream consumers can
pick whichever one they care about.</p>

<details><summary>Source</summary><pre><code>@register(&quot;optical_flow.gt_optical_flow&quot;)
class GtOpticalFlowMetric(BaseMetric):
    &quot;&quot;&quot;Per-pixel / per-frame / temporal flow comparison vs. a reference video.

    The headline ``score`` is ``pixel_epe_mean_mean`` (lower is better);
    every other scalar lives in ``details`` so downstream consumers can
    pick whichever one they care about.
    &quot;&quot;&quot;

    name = &quot;optical_flow.gt_optical_flow&quot;
    requires_reference = True
    higher_is_better = False
    needs_gpu = True
    backbone = &quot;optical_flow&quot;
    dependencies = &#91;&quot;ptlflow&quot;&#93;

    def __init__(
        self,
        model_name: str = &quot;dpflow&quot;,
        ckpt: str = &quot;things&quot;,
        min_mag: float = 0.5,
        max_mag_pct: float = 80.0,
        grid_size: int = 8,
    ) -&gt; None:
        super().__init__()
        self.model_name = model_name
        self.ckpt = ckpt
        self.min_mag = min_mag
        self.max_mag_pct = max_mag_pct
        self.grid_size = grid_size
        self._model = None
        # One frame-pair per DPFlow forward: the cost volume is ~4 GB
        # per pair at 1080p, so batching OOMs. Bump for low-res inputs.
        self._chunk_size = 1

    def to(self, device: str | torch.device) -&gt; GtOpticalFlowMetric:
        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
        self._model = load_ptlflow_model(self.model_name, self.ckpt, self.device)

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

        gen_video = sample&#91;&quot;video&quot;&#93;.float()  # (T, C, H, W)
        ref_video = sample&#91;&quot;reference&quot;&#93;.float()
        n = min(gen_video.shape&#91;0&#93;, ref_video.shape&#91;0&#93;)
        gen_video, ref_video = gen_video&#91;:n&#93;, ref_video&#91;:n&#93;
        if n &lt; 2:
            raise ValueError(&quot;Need at least 2 frames to compute optical flow&quot;)

        chunk = self._chunk_size or 16
        gen_flows = extract_video_flows(
            self._model,
            gen_video,
            chunk=chunk,
            device=self.device,
        )
        ref_flows = extract_video_flows(
            self._model,
            ref_video,
            chunk=chunk,
            device=self.device,
        )
        per_frame = &#91;
            compute_frame_metrics(
                rf,
                gf,
                grid_size=self.grid_size,
                min_mag=self.min_mag,
                max_mag_pct=self.max_mag_pct,
            ) for rf, gf in zip(ref_flows, gen_flows, strict=False)
        &#93;
        summary = aggregate_temporal(per_frame)
        score = summary.get(&quot;pixel_epe_mean_mean&quot;)
        details = dict(summary)
        details&#91;&quot;per_frame_metrics&quot;&#93; = per_frame
        return MetricResult(
            name=self.name,
            score=float(score) if score is not None else None,
            details=details,
        )</code></pre></details>
<h3 id="fastvideo.eval.metrics.optical_flow.gt_optical_flow.metric.GtOpticalFlowMetric.to"><code>fastvideo.eval.metrics.optical_flow.gt_optical_flow.metric.GtOpticalFlowMetric.to</code></h3>
<pre class="doc-signature"><code>to(device: str | torch.device) -&gt; GtOpticalFlowMetric</code></pre>

<details><summary>Source</summary><pre><code>def to(self, device: str | torch.device) -&gt; GtOpticalFlowMetric:
    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.optical_flow.gt_optical_flow.metric.GtOpticalFlowMetric.setup"><code>fastvideo.eval.metrics.optical_flow.gt_optical_flow.metric.GtOpticalFlowMetric.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
    self._model = load_ptlflow_model(self.model_name, self.ckpt, self.device)</code></pre></details>
<h3 id="fastvideo.eval.metrics.optical_flow.gt_optical_flow.metric.GtOpticalFlowMetric.compute"><code>fastvideo.eval.metrics.optical_flow.gt_optical_flow.metric.GtOpticalFlowMetric.compute</code></h3>
<pre class="doc-signature"><code>compute(sample: dict) -&gt; MetricResult</code></pre>

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

    gen_video = sample&#91;&quot;video&quot;&#93;.float()  # (T, C, H, W)
    ref_video = sample&#91;&quot;reference&quot;&#93;.float()
    n = min(gen_video.shape&#91;0&#93;, ref_video.shape&#91;0&#93;)
    gen_video, ref_video = gen_video&#91;:n&#93;, ref_video&#91;:n&#93;
    if n &lt; 2:
        raise ValueError(&quot;Need at least 2 frames to compute optical flow&quot;)

    chunk = self._chunk_size or 16
    gen_flows = extract_video_flows(
        self._model,
        gen_video,
        chunk=chunk,
        device=self.device,
    )
    ref_flows = extract_video_flows(
        self._model,
        ref_video,
        chunk=chunk,
        device=self.device,
    )
    per_frame = &#91;
        compute_frame_metrics(
            rf,
            gf,
            grid_size=self.grid_size,
            min_mag=self.min_mag,
            max_mag_pct=self.max_mag_pct,
        ) for rf, gf in zip(ref_flows, gen_flows, strict=False)
    &#93;
    summary = aggregate_temporal(per_frame)
    score = summary.get(&quot;pixel_epe_mean_mean&quot;)
    details = dict(summary)
    details&#91;&quot;per_frame_metrics&quot;&#93; = per_frame
    return MetricResult(
        name=self.name,
        score=float(score) if score is not None else None,
        details=details,
    )</code></pre></details>
</div>
