pose_format.torch.masked package

pose_format.torch.masked.tensor module

class pose_format.torch.masked.tensor.MaskedTensor(tensor, mask=None)[source]

Bases: object

Container for a PyTorch tensor, providing utility functions for tensor masking.

Parameters:
  • tensor (torch.Tensor) – Tensor data.

  • mask (torch.Tensor, optional) – A boolean mask tensor of the same shape as tensor. If specified, elements of tensor corresponding to True values in the mask are considered valid. Defaults to a tensor of all True values.

Methods:

arithmetic(action, other)

Helper method to perform arithmetic operations on tensors.

cuda([device, non_blocking])

Moves tensor to the GPU.

div(other[, in_place, update_mask])

Performs element-wise division with another tensor.

fix_nan()

Replaces any NaN values in the tensor with zeros.

matmul(matrix)

Perform matrix multiplication.

permute(dims)

Permute dimensions of tensor.

pow_(exponent)

Raises tensor to power of a given exponent in-place.

rename(*names)

Rename tensor's dimensions.

reshape(shape)

Reshape tensor to given shape.

size(*args)

Get size of tensor for specified dimensions.

split(split_size_or_sections[, dim])

Split tensor into multiple tensors.

squeeze(dim)

Squeeze tensor along chosen dimension.

sum(dim)

Sums along a specified dimension.

to(device)

Moves tensor to a custom device.

transpose(dim0, dim1)

Transposes tensor along two dimensions.

zero_filled()

Get tensor with masked values set to zero.

arithmetic(action, other)[source]

Helper method to perform arithmetic operations on tensors.

Parameters:
  • action (str) – The arithmetic operation to be performed.

  • other (Union[MaskedTensor`, torch.Tensor, float, int]) – The second operand.

Returns:

New MaskedTensor after the operation.

Return type:

MaskedTensor

cuda(device=None, non_blocking=False)[source]

Moves tensor to the GPU.

Parameters:
  • device (str or torch.device, optional) – The target CUDA device.

  • non_blocking (bool, optional) – Whether to perform an operation asynchronously. Default is False.

Returns:

Tensor on CUDA device.

Return type:

MaskedTensor

div(other, in_place=False, update_mask=True)[source]

Performs element-wise division with another tensor.

Parameters:
  • other (MaskedTensor) – The tensor to divide with.

  • in_place (bool, optional) – If True, performs the operation in-place. Default is False.

  • update_mask (bool, optional) – If True, updates the mask after division. Default is True.

Returns:

Resultant tensor after division.

Return type:

MaskedTensor

fix_nan()[source]

Replaces any NaN values in the tensor with zeros.

Returns:

Tensor with NaN values replaced by zeros.

Return type:

MaskedTensor

matmul(matrix)[source]

Perform matrix multiplication.

Parameters:

matrix (torch.Tensor) – matrix to multiply with.

Returns:

New masked tensor after multiplication.

Return type:

MaskedTensor

permute(dims)[source]

Permute dimensions of tensor.

Parameters:

dims (tuple) – Desired ordering of dimensions.

Returns:

Permuted masked tensor.

Return type:

MaskedTensor

pow_(exponent)[source]

Raises tensor to power of a given exponent in-place.

Parameters:

exponent (float) – The exponent value.

Returns:

Masked tensor raised to a given exponent.

Return type:

MaskedTensor

rename(*names)[source]

Rename tensor’s dimensions.

Parameters:

names (tuple) – Desired names for each dimension.

Returns:

Renamed masked tensor.

Return type:

MaskedTensor

reshape(shape)[source]

Reshape tensor to given shape.

Parameters:

shape (tuple) – Desired shape.

Returns:

Reshaped tensor.

Return type:

MaskedTensor

size(*args)[source]

Get size of tensor for specified dimensions.

Returns:

Size of tensor.

Return type:

torch.Size

split(split_size_or_sections, dim=0)[source]

Split tensor into multiple tensors.

Parameters:
  • split_size_or_sections (int or tuple) – Size or sections to split tensor.

  • dim (int, optional) – Dimension along which to split tensor. Default is 0.

Returns:

List of split tensors.

Return type:

list[MaskedTensor]

squeeze(dim)[source]

Squeeze tensor along chosen dimension.

Parameters:

dim (int) – Dimension to squeeze.

Returns:

Squeezed masked tensor.

Return type:

MaskedTensor

sum(dim)[source]

Sums along a specified dimension.

Parameters:

dim (int) – dimension to sum over.

Returns:

Summed tensor along the specified dimension.

Return type:

MaskedTensor

to(device)[source]

Moves tensor to a custom device.

Parameters:

device (str or torch.device) – The target device.

Returns:

Tensor on the other device.

Return type:

MaskedTensor

transpose(dim0, dim1)[source]

Transposes tensor along two dimensions.

Parameters:
  • dim0 (int) – Two dimensions to which to transpose.

  • dim1 (int) – Two dimensions to which to transpose.

Returns:

Transposed masked tensor.

Return type:

MaskedTensor

zero_filled()[source]

Get tensor with masked values set to zero.

Returns:

Tensor with masked values set to zero.

Return type:

torch.Tensor

pose_format.torch.masked.torch module

Classes:

MaskedTorch

class mimicing torch functions and giving support for MaskedTensor.

TorchFallback

Meta class that gives a fallback mechanism to use torch functions on MaskedTensor objects.

class pose_format.torch.masked.torch.MaskedTorch[source]

Bases: object

class mimicing torch functions and giving support for MaskedTensor.

Methods:

cat(tensors, dim)

Concatenate MaskedTensor objects along a specified dimension.

squeeze(masked_tensor)

Remove dimensions of size 1 from MaskedTensor.

stack(tensors, dim)

Stack MaskedTensor objects along a new dimension.

zeros(*size[, dtype])

Creates a MaskedTensor of zeros with a given shape and data type.

static cat(tensors, dim)[source]

Concatenate MaskedTensor objects along a specified dimension.

Parameters:
  • tensors (list) – List of tensors or MaskedTensor objects to be concatenated.

  • dim (int) – Dimension along to concatenate.

Returns:

Concatenated tensor.

Return type:

MaskedTensor

static squeeze(masked_tensor)[source]

Remove dimensions of size 1 from MaskedTensor.

Parameters:

masked_tensor (MaskedTensor) – tensor from which dimensions are to be removed.

Returns:

Squeezed masked tensor.

Return type:

MaskedTensor

static stack(tensors, dim)[source]

Stack MaskedTensor objects along a new dimension.

Parameters:
  • tensors (list) – List of MaskedTensor objects to be stacked.

  • dim (int) – New dimension along which to stack.

Returns:

Stacked maked tensor.

Return type:

MaskedTensor

static zeros(*size, dtype=None)[source]

Creates a MaskedTensor of zeros with a given shape and data type.

Parameters:
  • *size (ints) – Dimensions of desired tensor.

  • dtype (torch.dtype, optional) – Data type of the tensor. If None, defaults to torch.float.

Returns:

masked tensor filled with zeros.

Return type:

MaskedTensor

class pose_format.torch.masked.torch.TorchFallback[source]

Bases: type

Meta class that gives a fallback mechanism to use torch functions on MaskedTensor objects. :noindex:

Attributes:

doesnt_change_mask

doesnt_change_mask = {'acos', 'asin', 'atan', 'cos', 'sin', 'sqrt', 'square', 'tan', 'unsqueeze'}

pose_format.torch.masked.torch_test module

Classes:

TestMaskedTorch([methodName])

Test cases for the MaskedTensor class

class pose_format.torch.masked.torch_test.TestMaskedTorch(methodName='runTest')[source]

Bases: TestCase

Test cases for the MaskedTensor class

Methods:

test_cat()

Test cat method for concatenating MaskedTensor objects along a specified dimension.

test_not_implemented_method()

Tests behavior when invoking an unimplemented method on a MaskedTensor.

test_stack()

Tests stack method for stacking MaskedTensor objects along a new dimension.

test_zeros_mask_value()

Test if the mask in the produced zeros MaskedTensor is initialized with zero values.

test_zeros_tensor_shape()

Test if zeros method correctly produces a MaskedTensor with the desired shape.

test_zeros_tensor_type_bool()

Test if the zeros method produces a MaskedTensor with the correct boolean data type.

test_zeros_tensor_type_float()

Test if the zeros method produces a MaskedTensor with the correct float data type.

test_zeros_tensor_value()

Test if the zeros method produces a MaskedTensor with all zero values.

test_cat()[source]

Test cat method for concatenating MaskedTensor objects along a specified dimension.

test_not_implemented_method()[source]

Tests behavior when invoking an unimplemented method on a MaskedTensor.

test_stack()[source]

Tests stack method for stacking MaskedTensor objects along a new dimension.

test_zeros_mask_value()[source]

Test if the mask in the produced zeros MaskedTensor is initialized with zero values.

test_zeros_tensor_shape()[source]

Test if zeros method correctly produces a MaskedTensor with the desired shape.

test_zeros_tensor_type_bool()[source]

Test if the zeros method produces a MaskedTensor with the correct boolean data type.

test_zeros_tensor_type_float()[source]

Test if the zeros method produces a MaskedTensor with the correct float data type.

test_zeros_tensor_value()[source]

Test if the zeros method produces a MaskedTensor with all zero values.