kaolin.utils.testing

API

kaolin.utils.testing.assert_images_close(im0, im1, pixel_disagreement_threshold=0.03, max_percent_disagreeing_pixels=3, check_range=True, do_resize=True)

Throws an error if images are not close to each other.

Parameters
  • im0 (torch.FloatTensor) – HxWxC image tensor; 0..1 range expected

  • im1 (torch.FloatTensor) – H2xW2xC image tensor; 0..1 range expected

  • pixel_disagreement_threshold (float) – L2 distance threshold between pixels that counts them as different

  • max_percent_disagreeing_pixels (float) – maximum percent of pixels that are allowed to differ beyond the provided threshold

  • check_range (bool) – if True, will check that image values are not too far beyond 0..1 range (to avoid e.g. passing in 0..256 images)

  • do_resize (bool) – if True, will resize images to the same size before checking, else will assert that their shapes are the same

kaolin.utils.testing.check_allclose(tensor, other, rtol=1e-05, atol=1e-08, equal_nan=False)
kaolin.utils.testing.check_packed_tensor(tensor, total_numel=None, last_dim=None, dtype=None, device=None, throw=True)

Check if packed tensor is valid given set of criteria.

Parameters
  • tensor (torch.Tensor) – the packed tensor to be tested.

  • total_numel (int, optional) – the expected number of elements.

  • last_dim (int, optional) – the expected last dimension size.

  • dtype (torch.dtype, optional) – the expected dtype.

  • device (torch.device, optional) – the expected device.

  • throw (bool) – if True the check will raise an error if failing.

Returns

status of the check.

Return type

(bool)

kaolin.utils.testing.check_padded_tensor(tensor, padding_value=None, shape_per_tensor=None, batch_size=None, max_shape=None, last_dim=None, dtype=None, device=None, throw=True)

Check if padded tensor is valid given set of criteria.

Parameters
  • tensor (torch.Tensor) – the padded tensor to be tested.

  • padding_value (int, optional) – the expected number of elements, shape_per_tensor must be provided with padding_value.

  • shape_per_tensor (torch.LongTensor, optional) – the expected shape_per_tensor.

  • batch_size (int, optional) – the expected batch size.

  • last_dim (int, optional) – the expected last dimension size.

  • dtype (torch.dtype, optional) – the expected dtype.

  • device (torch.device, optional) – the expected device.

  • throw (bool) – if True the check will raise an error if failing.

Returns

status of the check.

Return type

(bool)

kaolin.utils.testing.check_spc_octrees(octrees, lengths, batch_size=None, level=None, device=None, throw=True)
kaolin.utils.testing.check_tensor(tensor, shape=None, dtype=None, device=None, throw=True)

Check if torch.Tensor is valid given set of criteria.

Parameters
  • tensor (torch.Tensor) – the tensor to be tested.

  • shape (list or tuple of int, optional) – the expected shape, if a dimension is set at None then it’s not verified.

  • dtype (torch.dtype, optional) – the expected dtype.

  • device (torch.device, optional) – the expected device.

  • throw (bool) – if true (default), will throw if checks fail

Returns

(bool) True if checks pass

kaolin.utils.testing.check_tensor_attribute_shapes(container, throw=True, **attribute_info)

Checks shape on all specified attributes of the container.

Parameters
  • container (dict, tuple, object) – container with named attributes to be tested

  • throw (bool) – if true (default), will throw error on first check that fails

  • attribute_info – named attribute=shape values, where shape can be list or tuple (see check_tensor)

Returns

(bool) True if checks pass

kaolin.utils.testing.contained_torch_equal(elem, other, approximate=False, print_error_context=None, **allclose_args)

Check for equality (or allclose if approximate) of two objects potentially containing tensors.

torch.equal() do not support data structure like dictionary / arrays and == is ambiguous on torch.Tensor. This class will try to apply recursion through collections.abc.Mapping, collections.abc.Sequence, torch.equal() if the objects are torch.Tensor, of else == operator.

Parameters
  • elem (object, dict, list, tuple) – The first object

  • other (object, dict, list, tuple) – The other object to compare to elem

  • approximate (bool) – if requested will use allclose for comparison instead (default=False)

  • print_error_context (str) – set to any string value to print the context for the first nested failed match

  • allclose_args – arguments to torch.allclose if approximate comparison requested

Return (bool): the comparison result

kaolin.utils.testing.file_contents_equal(file1, file2, exclude_pattern=None)
kaolin.utils.testing.print_dict_attributes(in_dict, name='', prefix='', **tensor_info_kwargs)

Convenience function to print all attributes of a dict with names. Extra info will be provided for tensors.

Parameters
  • in_dict (dict) – input dictionary containing any values

  • name (str) – name of the input dictionary (especially useful if printing nested dictionaries)

  • prefix (str) – prefix to provide to every printed attributes (especially useful if printing nested dictionaries)

  • **tensor_info_kwargs – extra arguments to pass to tensor_info()

kaolin.utils.testing.print_namedtuple_attributes(ntuple, name='', prefix='', **tensor_info_kwargs)

Same as print_dict_attributes(), but with named tuple input.

kaolin.utils.testing.tensor_info(t, name='', print_stats=False, detailed=False)

Convenience method to format diagnostic tensor information, including shape, type, and optional attributes if specified as string. This information can then be logged as: logger.debug(tensor_info(my_tensor, ‘my tensor’))

Log output: my_tensor: [10, 2, 100, 100] (torch.float32)

Parameters
  • t – input pytorch tensor or numpy array or None

  • name – human readable name of the tensor (optional)

  • print_stats – if True, includes mean/max/min statistics (takes compute time)

  • detailed – if True, includes details about tensor properties

Returns

(String) formatted string

Examples

>>> t = torch.Tensor([0., 2., 3.])
>>> tensor_info(t, 'mytensor', True, True)
'mytensor: torch.Size([3]) (torch.float32)  - [min 0.0000, max 3.0000, mean 1.6667]  - req_grad=False, is_leaf=True, device=cpu, layout=torch.strided'
kaolin.utils.testing.with_seed(torch_seed=0, numpy_seed=None, random_seed=None)

Decorator to fix the seed of a function.

Parameters
  • torch_seed (int) – The desired seed for torch module.

  • random_seed (int) – The desired seed for random module. Default: torch_seed value.

  • numpy_seed (int) – The desired seed for numpy module. Default: torch_seed value.