io_adapter.py

Helper to facilitate the use of inputs and outputs of the models.

class ptlflow.utils.io_adapter.IOAdapter(model: BaseModel, input_size: Tuple[int, int], target_size: Tuple[int, int] | None = None, target_scale_factor: float | None = None, interpolation_mode: str = 'bilinear', interpolation_align_corners: bool = False, cuda: bool = False, fp16: bool = False)[source]

Handle the inputs and outputs of optical flow models.

__init__(model: BaseModel, input_size: Tuple[int, int], target_size: Tuple[int, int] | None = None, target_scale_factor: float | None = None, interpolation_mode: str = 'bilinear', interpolation_align_corners: bool = False, cuda: bool = False, fp16: bool = False) None[source]

Initialize IOAdapter.

Parameters:
modelBaseModel

An instance of optical flow mode that will be used for estimation.

input_sizeTuple[int, int]

The shape of the original inputs, must be a tuple with at least two elements. It is assumed that the last two elements are (height, with).

target_sizeOptional[Tuple[int, int]], optional

If provided, the inputs will be resized to target_size. target_size is defined as a tuple (height, with).

target_scale_factorOptional[float], default 1.0

This value is only used if size is None. The multiplier that will be applied to the original shape to scale the input.

interpolation_modestr, default ‘bilinear’

How to perform the interpolation. It must be a value accepted by the ‘mode’ argument from torch.nn.functional.interpolate function.

interpolation_align_cornersbool, default False

Whether the interpolation keep the corners aligned. As defined in torch.nn.functional.interpolate.

cudabool

If True, the input tensors are transferred to GPU (if a GPU is available).

prepare_inputs(images: ndarray | List[ndarray] | None = None, flows: ndarray | List[ndarray] | None = None, inputs: Dict[str, Any] | None = None, image_only: bool = False, **kwargs: ndarray | List[ndarray]) Dict[str, Tensor][source]

Transform numpy inputs into the input format of the optical flow models.

This basically consists on transform the numpy arrays into torch tensors, and then putting them into a dict.

Parameters:
imagesUnion[np.ndarray, List[np.ndarray]]

One or more images to use to estimate the optical flow. Typically, it will be a least with two images in the HWC format.

flowsOptional[Union[np.ndarray, List[np.ndarray]]], optional

One or more groundtruth optical flow, which can be used for validation. Typically it will be an array HWC.

inputsOptional[Dict[str, Any]]

Dict containing input tensors or other metadata. Only the tensors will be transformed.

image_onlyOptional[bool]

If True, only applies scaling and padding to the images.

kwargsUnion[np.ndarray, List[np.ndarray]]

Any other array inputs can be provided as keyworded arguments. This function will create an entry in the input dict for each keyworded array given.

Returns:
Dict[str, Any]

The inputs converted and transformed to the input format of the optical flow models.

unscale(outputs: Dict[str, Any], image_only: bool = False) Dict[str, Tensor][source]

Remove scaling to restore the original shapes.

The inputs and outputs obtained from prepare_inputs() may have been scaled. This function can be used to revert those operations.

Typically, this function should be used on both the inputs and outputs of the model after the model generated the predictions.

Parameters:
outputsDict[str, Any]

It can be either the inputs or the outputs of optical flow models. All tensors will have the extra scaling removed.

image_onlyOptional[bool]

If True, only removes scaling from the images.

Returns:
Dict[str, Any]

The same tensors, with the padding and scaling removed.

See also

prepare_inputs