kornia.testing#

The testing package contains testing-specific utilities.

kornia.testing.KORNIA_CHECK(condition, msg=None)[source]#

Check any arbitrary boolean condition.

Parameters
  • condition (bool) – the condition to evaluate.

  • msg (Optional[str], optional) – message to show in the exception. Default: None

Raises

Exception – if the confition is met.

Example

>>> x = torch.rand(2, 3, 3)
>>> KORNIA_CHECK(x.shape[-2:] == (3, 3), "Invalid homography")
Return type

None

kornia.testing.KORNIA_CHECK_DM_DESC(desc1, desc2, dm)[source]#

Check whether the provided descriptors match with a distance matrix.

Parameters
  • desc1 (Tensor) – first descriptor tensor to evaluate.

  • desc2 (Tensor) – second descriptor tensor to evaluate.

  • dm (Tensor) – distance matrix tensor to evaluate.

Raises

TypeException – if the descriptors shape do not match with the distance matrix.

Example

>>> desc1 = torch.rand(4)
>>> desc2 = torch.rand(8)
>>> dm = torch.rand(4, 8)
>>> KORNIA_CHECK_DM_DESC(desc1, desc2, dm)
kornia.testing.KORNIA_CHECK_IS_COLOR(x, msg=None)[source]#

Check whether an image tensor is a color images.

Parameters
  • x (Tensor) – image tensor to evaluate.

  • msg (Optional[str], optional) – message to show in the exception. Default: None

Raises

TypeException – if all the input tensor has not a shape \((3,H,W)\).

Example

>>> img = torch.rand(2, 3, 4, 4)
>>> KORNIA_CHECK_IS_COLOR(img, "Image is not color")
kornia.testing.KORNIA_CHECK_IS_GRAY(x, msg=None)[source]#

Check whether an image tensor is grayscale.

Parameters
  • x (Tensor) – image tensor to evaluate.

  • msg (Optional[str], optional) – message to show in the exception. Default: None

Raises

TypeException – if the tensor has not a shape \((1,H,W)\) or \((H,W)\).

Example

>>> img = torch.rand(2, 1, 4, 4)
>>> KORNIA_CHECK_IS_GRAY(img, "Image is not grayscale")
kornia.testing.KORNIA_CHECK_IS_TENSOR(x, msg=None)[source]#

Check the input variable is a Tensor.

Parameters
  • x – any input variable.

  • msg (Optional[str], optional) – message to show in the exception. Default: None

Raises

TypeException – if the input variable does not match with the expected.

Example

>>> x = torch.rand(2, 3, 3)
>>> KORNIA_CHECK_IS_TENSOR(x, "Invalid tensor")
kornia.testing.KORNIA_CHECK_LAF(laf)[source]#

Check whether a Local Affine Frame (laf) has a valid shape.

Parameters

laf (Tensor) – local affine frame tensor to evaluate.

Raises

Exception – if the input laf does not have a shape \((B,N,2,3)\).

Example

>>> lafs = torch.rand(2, 10, 2, 3)
>>> KORNIA_CHECK_LAF(lafs)
Return type

None

kornia.testing.KORNIA_CHECK_SAME_DEVICE(x, y)[source]#

Check whether two tensor in the same device.

Parameters
  • x (Tensor) – first tensor to evaluate.

  • y (Tensor) – sencod tensor to evaluate.

  • msg – message to show in the exception.

Raises

TypeException – if the two tensors are not in the same device.

Example

>>> x1 = torch.rand(2, 3, 3)
>>> x2 = torch.rand(1, 3, 1)
>>> KORNIA_CHECK_SAME_DEVICE(x1, x2)
kornia.testing.KORNIA_CHECK_SAME_DEVICES(tensors, msg=None)[source]#

Check whether a list provided tensors live in the same device.

Parameters
  • x – a list of tensors.

  • msg (Optional[str], optional) – message to show in the exception. Default: None

Raises

Exception – if all the tensors are not in the same device.

Example

>>> x1 = torch.rand(2, 3, 3)
>>> x2 = torch.rand(1, 3, 1)
>>> KORNIA_CHECK_SAME_DEVICES([x1, x2], "Tensors not in the same device")
kornia.testing.KORNIA_CHECK_SHAPE(x, shape)[source]#

Check whether a tensor has a specified shape.

The shape can be specified with a implicit or explicit list of strings. The guard also check whether the variable is a type Tensor.

Parameters
  • x – the tensor to evaluate.

  • shape (List[str]) – a list with strings with the expected shape.

Raises

Exception – if the input tensor is has not the expected shape.

Example

>>> x = torch.rand(2, 3, 4, 4)
>>> KORNIA_CHECK_SHAPE(x, ["B","C", "H", "W"])  # implicit
>>> x = torch.rand(2, 3, 4, 4)
>>> KORNIA_CHECK_SHAPE(x, ["2","3", "H", "W"])  # explicit
Return type

None

kornia.testing.KORNIA_CHECK_TYPE(x, typ, msg=None)[source]#

Check the type of an aribratry variable.

Parameters
  • x – any input variable.

  • typ – the expected type of the variable.

  • msg (Optional[str], optional) – message to show in the exception. Default: None

Raises

TypeException – if the input variable does not match with the expected.

Example

>>> KORNIA_CHECK_TYPE("foo", str, "Invalid string")
kornia.testing.KORNIA_UNWRAP(maybe_obj, typ)[source]#

Unwraps an optional contained value that may or not be present.

Parameters
  • maybe_obj – the object to unwrap.

  • typ – expected type after unwrap.

kornia.testing.create_eye_batch(batch_size, eye_size, device=None, dtype=None)[source]#

Create a batch of identity matrices of shape Bx3x3.

kornia.testing.tensor_to_gradcheck_var(tensor, dtype=torch.float64, requires_grad=True)[source]#

Convert the input tensor to a valid variable to check the gradient.

gradcheck needs 64-bit floating point and requires gradient.

kornia.testing.xla_is_available()[source]#

Return whether torch_xla is available in the system.

Return type

bool