File size: 1,558 Bytes
9b33fca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
"""Depth estimation metrics."""

from __future__ import annotations

import numpy as np

from vis4d.common.typing import ArrayLike

from ..utils import check_shape_match, dense_inputs_to_numpy


def end_point_error(prediction: ArrayLike, target: ArrayLike) -> float:
    """Compute the end point error.

    Args:
        prediction (ArrayLike): Prediction UV optical flow, in shape (..., 2).
        target (ArrayLike): Target UV optical flow, in shape (..., 2).

    Returns:
        float: End point error.
    """
    prediction, target = dense_inputs_to_numpy(prediction, target)
    check_shape_match(prediction, target)
    squared_sum = np.sum((prediction - target) ** 2, axis=-1)
    return np.mean(np.sqrt(squared_sum)).item()


def angular_error(
    prediction: ArrayLike, target: ArrayLike, epsilon: float = 1e-6
) -> float:
    """Compute the angular error.

    Args:
        prediction (ArrayLike): Prediction UV optical flow, in shape (..., 2).
        target (ArrayLike): Target UV optical flow, in shape (..., 2).
        epsilon (float, optional): Epsilon value for numerical stability.

    Returns:
        float: Angular error.
    """
    prediction, target = dense_inputs_to_numpy(prediction, target)
    check_shape_match(prediction, target)
    product = np.sum(prediction * target, axis=-1)
    pred_norm = np.linalg.norm(prediction, axis=-1)
    target_norm = np.linalg.norm(target, axis=-1)
    cos_angle = np.abs(product) / (pred_norm * target_norm + epsilon)
    return np.mean(np.arccos(np.clip(cos_angle, 0.0, 1.0))).item()