Optical Flow EPE with Masks (OmniWorld-style metric)

Medium
Computer Vision

Compute the mean End-Point Error (EPE) between a predicted optical flow field and its ground-truth, optionally using an occlusion/validity mask and an outlier clip (max_flow). Your function should ignore invalid values (NaN/±Inf), support Python lists or NumPy arrays with shape (H, W, 2), and return -1 if inputs are malformed or no valid pixels remain.

Examples

Example 1:
Input: pred = [[[1,0],[0,1]], [[-1,0],[0,-1]]]; gt = [[[0,0],[0,0]], [[0,0],[0,0]]] print(round(flow_epe(pred, gt), 4))
Output: 1.0
Explanation: Each of the four pixels has EPE = 1, so the mean is 1.0.

Starter Code

from typing import Optional, Union

try:
    import numpy as np
except Exception:
    np = None

ArrayLike = Union[list, "np.ndarray"]

def flow_epe(pred: ArrayLike,
             gt: ArrayLike,
             mask: Optional[ArrayLike] = None,
             max_flow: Optional[float] = None) -> float:
    """
    Compute mean End-Point Error (EPE) between predicted and ground-truth optical flow.

    Args:
        pred, gt: (H, W, 2) lists or NumPy arrays.
        mask: optional (H, W) or broadcastable to (H, W); 1=include, 0=ignore.
        max_flow: optional float; clip per-pixel EPE to this value.

    Returns:
        float: mean EPE over valid pixels. Returns -1 on invalid input or if no valid pixels.
    """
    # Your implementation here
    pass
Lines: 1Characters: 0
Ready
The AI Interview - Master AI/ML Interviews