flow_metrics.py

Handle and compute metrics for optical flow and related estimations.

This handler is designed according to the torchmetrics specifications. Besides accuracy metrics for optical flow, it can also compute basic metrics for occlusion, motion boundary and flow confidence estimations.

class ptlflow.utils.flow_metrics.FlowMetrics(dist_sync_on_step: bool = False, prefix: str = '', average_mode: str = 'epoch_mean', ema_decay: float = 0.99, f1_mode: str = 'macro', interpolate_pred_to_target_size: bool = False)[source]

Handler for optical flow and related metrics.

Attributes:
average_modestr, default ‘epoch_mean’

How the final metric is averaged. It can be either ‘epoch_mean’ or ‘ema’ (exponential moving average).

ema_decayfloat, default 0.99

The decay to be applied if average_mode is ‘ema’.

prefixstr, optional

A prefix string that will be attached to the metric names.

__init__(dist_sync_on_step: bool = False, prefix: str = '', average_mode: str = 'epoch_mean', ema_decay: float = 0.99, f1_mode: str = 'macro', interpolate_pred_to_target_size: bool = False) None[source]

Initialize FlowMetrics.

Parameters:
dist_sync_on_stepbool, default False

Used by torchmetrics to sync metrics between multiple processes.

prefixstr, optional

A prefix string that will be attached to the metric names.

average_modestr, default ‘epoch_mean’

How the final metric is averaged. It can be either ‘epoch_mean’ or ‘ema’ (exponential moving average).

ema_decayfloat, default 0.99

The decay to be applied if average_mode is ‘ema’.

f1_modefloat, default ‘macro’

How to calculate the f1-score. Accepts one of these options {binary, macro, weighted}. If binary, then the f1-score is calculated only for the positive pixels. If macro, then the f1-score is the average of positive and negative scores. If weighted, then the average is weighted according to the number of positive/negative samples.

interpolate_pred_to_target_sizebool, default False

If True, the prediction is bilinearly interpolated to match the target size, if their sizes are different.

calculate_metrics() Dict[str, Tensor][source]

Compute and return the average of all metrics.

On Pytorch-Lightning < 1.2, compute() automatically calls reset(). Sometimes this is not desirable, so the metrics are calculated here in this other function, which can be called externally.

Returns:
Dict[str, torch.Tensor]

The average of the metrics.

compute() Dict[str, Tensor][source]

Compute and return the average of all metrics.

Called internally by torchmetrics.

Returns:
Dict[str, torch.Tensor]

The average of the metrics.

update(preds: Dict[str, Tensor], targets: Dict[str, Tensor]) None[source]

Compute and update one step of the metrics.

Parameters:
predsdict[str, torch.Tensor]

The predictions of the optical flow model.

targetsdict[str, torch.Tensor]

The groundtruth of the predictions.