Prediction

Various functions for prediction with neural networks. See the Predict tutorial for more details.

Predictors

Ready-to-use predictors.

dpipe.predict.shape.add_extract_dims(n_add: int = 1, n_extract: Optional[int] = None, sequence: bool = False)[source]

Adds n_add dimensions before a prediction and extracts n_extract dimensions after this prediction.

Parameters
  • n_add (int) – number of dimensions to add.

  • n_extract (int, None, optional) – number of dimensions to extract. If None, extracts the same number of dimensions as were added (n_add).

  • sequence – if True - the output is expected to be a sequence, and the dims are extracted for each element of the sequence.

dpipe.predict.shape.divisible_shape(divisor: Union[int, Sequence[int]], axis: Optional[Union[int, Sequence[int]]] = None, padding_values: Union[float, Sequence[float], Callable] = 0, ratio: Union[float, Sequence[float]] = 0.5)[source]

Pads an incoming array to be divisible by divisor along the axes. Afterwards the padding is removed.

Parameters
  • divisor – a value an incoming array should be divisible by.

  • axis – axes along which the array will be padded. If None - the last len(divisor) axes are used.

  • padding_values – values to pad with. If Callable (e.g. numpy.min) - padding_values(x) will be used.

  • ratio – the fraction of the padding that will be applied to the left, 1 - ratio will be applied to the right.

References

pad_to_divisible

dpipe.predict.shape.patches_grid(patch_size: ~typing.Union[int, ~typing.Sequence[int]], stride: ~typing.Union[int, ~typing.Sequence[int]], axis: ~typing.Optional[~typing.Union[int, ~typing.Sequence[int]]] = None, padding_values: ~typing.Union[float, ~typing.Sequence[float], ~typing.Callable] = 0, ratio: ~typing.Union[float, ~typing.Sequence[float]] = 0.5, combiner: ~typing.Type[~dpipe.im.grid.PatchCombiner] = <class 'dpipe.im.grid.Average'>, get_boxes: ~typing.Callable = <function get_boxes>)[source]

Divide an incoming array into patches of corresponding patch_size and stride and then combine the predicted patches by aggregating the overlapping regions using the combiner - Average by default.

If padding_values is not None, the array will be padded to an appropriate shape to make a valid division. Afterwards the padding is removed. Otherwise if input cannot be patched without remainder ValueError is raised.

References

grid.divide, grid.combine, pad_to_shape

Functions

Various functions that can be used to build predictors.

dpipe.predict.functional.chain_decorators(*decorators: Callable, predict: Callable, **kwargs)[source]

Wraps predict into a series of decorators.

kwargs are passed as additional arguments to predict.

Examples

>>> @decorator1
>>> @decorator2
>>> def f(x):
>>>     return x + 1
>>> # same as:
>>> def f(x):
>>>     return x + 1
>>>
>>> f = chain_decorators(decorator1, decorator2, predict=f)
dpipe.predict.functional.preprocess(func, *args, **kwargs)[source]

Applies function func with given parameters before making a prediction.

Examples

>>> from dpipe.im.shape_ops import pad
>>> from dpipe.predict.functional import preprocess
>>>
>>> @preprocess(pad, padding=[10, 10, 10], padding_values=np.min)
>>> def predict(x):
>>>     return model.do_inf_step(x)
performs spatial padding before prediction.

References

postprocess

dpipe.predict.functional.postprocess(func, *args, **kwargs)[source]

Applies function func with given parameters after making a prediction.

References

preprocess