utils

class pytorch3d.renderer.utils.TensorAccessor(class_object, index: Union[int, slice])[source]

A helper class to be used with the __getitem__ method. This can be used for getting/setting the values for an attribute of a class at one particular index. This is useful when the attributes of a class are batched tensors and one element in the batch needs to be modified.

__init__(class_object, index: Union[int, slice])[source]
Parameters:
  • class_object – this should be an instance of a class which has attributes which are tensors representing a batch of values.
  • index – int/slice, an index indicating the position in the batch. In __setattr__ and __getattr__ only the value of class attributes at this index will be accessed.
__setattr__(name: str, value: Any)[source]

Update the attribute given by name to the value given by value at the index specified by self.index.

Parameters:
  • name – str, name of the attribute.
  • value – value to set the attribute to.
__getattr__(name: str)[source]

Return the value of the attribute given by “name” on self.class_object at the index specified in self.index.

Parameters:name – string of the attribute name
class pytorch3d.renderer.utils.TensorProperties(dtype=torch.float32, device='cpu', **kwargs)[source]

A mix-in class for storing tensors as properties with helper methods.

__init__(dtype=torch.float32, device='cpu', **kwargs)[source]
Parameters:
  • dtype – data type to set for the inputs
  • device – str or torch.device
  • kwargs – any number of keyword arguments. Any arguments which are of type (float/int/tuple/tensor/array) are broadcasted and other keyword arguments are set as attributes.
isempty() → bool[source]
__getitem__(index: Union[int, slice])[source]
Parameters:index – an int or slice used to index all the fields.
Returns:if index is an index int/slice return a TensorAccessor class with getattribute/setattribute methods which return/update the value at the index in the original camera.
to(device: str = 'cpu')[source]

In place operation to move class properties which are tensors to a specified device. If self has a property “device”, update this as well.

clone(other)[source]

Update the tensor properties of other with the cloned properties of self.

gather_props(batch_idx)[source]

This is an in place operation to reformat all tensor class attributes based on a set of given indices using torch.gather. This is useful when attributes which are batched tensors e.g. shape (N, 3) need to be multiplied with another tensor which has a different first dimension e.g. packed vertices of shape (V, 3).

Example

self.specular_color = (N, 3) tensor of specular colors for each mesh

A lighting calculation may use

verts_packed = meshes.verts_packed()  # (V, 3)

To multiply these two tensors the batch dimension needs to be the same. To achieve this we can do

batch_idx = meshes.verts_packed_to_mesh_idx()  # (V)

This gives index of the mesh for each vertex in verts_packed.

self.gather_props(batch_idx)
self.specular_color = (V, 3) tensor with the specular color for
                         each packed vertex.

torch.gather requires the index tensor to have the same shape as the input tensor so this method takes care of the reshaping of the index tensor to use with class attributes with arbitrary dimensions.

Parameters:batch_idx – shape (B, …) where represents an arbitrary number of dimensions
Returns:self with all properties reshaped. e.g. a property with shape (N, 3) is transformed to shape (B, 3).
pytorch3d.renderer.utils.format_tensor(input, dtype=torch.float32, device: str = 'cpu') → torch.Tensor[source]

Helper function for converting a scalar value to a tensor.

Parameters:
  • input – Python scalar, Python list/tuple, torch scalar, 1D torch tensor
  • dtype – data type for the input
  • device – torch device on which the tensor should be placed.
Returns:

input_vec – torch tensor with optional added batch dimension.

pytorch3d.renderer.utils.convert_to_tensors_and_broadcast(*args, dtype=torch.float32, device: str = 'cpu')[source]

Helper function to handle parsing an arbitrary number of inputs (*args) which all need to have the same batch dimension. The output is a list of tensors.

Parameters:
  • *args

    an arbitrary number of inputs Each of the values in args can be one of the following

    • Python scalar
    • Torch scalar
    • Torch tensor of shape (N, K_i) or (1, K_i) where K_i are an arbitrary number of dimensions which can vary for each value in args. In this case each input is broadcast to a tensor of shape (N, K_i)
  • dtype – data type to use when creating new tensors.
  • device – torch device on which the tensors should be placed.
Output:
args: A list of tensors of shape (N, K_i)