Skip to content

base

Base classes of the dict transforms and their composition.

Classes:

  • Transform –

    Base class for all point cloud transforms.

  • Compose –

    Compose multiple transforms into a single transform.

  • RandomApply –

    Apply a pipeline of transforms with probability p, else return the data unchanged.

  • DictTransform –

    Base class for dictionary transforms.

Transform [source] ΒΆ

Base class for all point cloud transforms.

A transform is a callable that takes an arbitrary data object and returns a transformed version of it.

While any callable can be used as a transform, this class provides a common interface and some convenience features, such as:

  • a torch_pointcloud.transforms.base.Transform.transform method, which implements the actual transformation logic. It will be called by the __call__ method to apply the transform.
  • a torch_pointcloud.transforms.base.Transform.extra_repr method, which returns a string that describes the transform. This will be used by the __repr__ method to represent the transform as a string.
Note

A transform should avoid modifying the input data in place. Instead, it should return a new object with the transformed data.

If the transform is in-place, it should be clearly stated in its documentation.

Note

Random transforms take a seed. None draws from the global generator, which PyTorch seeds per DataLoader worker and per epoch. An int gives the transform its own stream, re-derived inside each worker so that workers and epochs keep drawing different numbers (see Randomizable).

See Also

torch_pointcloud.transforms.DictTransform for a version of this class that operates on dictionaries.

Example

For example, to create a transform that scales the points in a point cloud, we can subclass the torch_pointcloud.transforms.Transform class and implement the torch_pointcloud.transforms.Transform.transform method as follows:

import torch
from torch import Tensor

from torch_pointcloud.transforms import Transform

# 1. Subclass the Transform class
class MyScale(Transform):
    def __init__(self, factor: float = 1.0):
        self.factor = factor

    def extra_repr(self) -> str:
        return f"factor={self.factor}"

    def transform(self, tensor: Tensor) -> Tensor:
        return tensor * self.factor

# 2. Initialize the transform
transform = MyScale()
# 3. Apply the transform
tensor = torch.randn(4096, 3)
tensor = transform(tensor)

Methods:

  • transform –

    Apply the transform to the input data.

transform abstractmethod [source] ΒΆ

transform(*args: Any, **kwargs: Any) -> Any

Apply the transform to the input data.

This method should be implemented by all subclasses, and do not have any constraints on the input data.

Compose [source] ΒΆ

Compose(
    transforms: Sequence[Transform],
    allow_missing_keys: Optional[bool] = None,
)

Bases: Transform, Randomizable

Compose multiple transforms into a single transform.

This class allows for chaining multiple transforms together.

Note

The order of the transforms is important, as each transform will be applied in the order they are added to the Compose object.

Parameters:

  • transforms (Sequence[Transform]) –

    The transforms to apply, in order.

  • allow_missing_keys (Optional[bool], default: None ) –

    When set, assigned to every DictTransform child, recursively through nested Compose objects, overriding what each child was built with (assigning the attribute later does the same). Registered pipelines are shared objects, so setting it on one mutates its children for every user. None leaves the children as built.

Example

For example, to chain a random sample and a normalization transform, we can do the following:

import torch

from torch_pointcloud.transforms import Compose, RandomSample, Rescale

# 1. Initialize the transforms
transform = Compose([
    RandomSample(keys="pos", num_samples=1024),
    Rescale(keys="pos"),
])

# 2. Apply the transform
data = {"pos": torch.randn(4096, 3)}
data = transform(data)

Methods:

  • set_random_state –

    Seed every random transform of the pipeline, each from its own draw of the pipeline's stream.

  • transform –

    Apply the transforms to the input data.

set_random_state [source] ΒΆ

set_random_state(
    seed: Optional[int] = None,
    state: Optional[Generator] = None,
) -> Self

Seed every random transform of the pipeline, each from its own draw of the pipeline's stream.

Parameters:

  • seed (Optional[int], default: None ) –

    Seed of the pipeline's stream. None (with no state) returns every transform to the global generator.

  • state (Optional[Generator], default: None ) –

    Generator the pipeline draws the per-transform seeds from.

Returns:

  • Self –

    The pipeline itself, for chaining.

transform [source] ΒΆ

transform(data: Any) -> Any

Apply the transforms to the input data.

This method will apply each transform in the order they were added to the torch_pointcloud.transforms.Compose object.

RandomApply [source] ΒΆ

RandomApply(
    transforms: Sequence[Transform],
    p: float = 0.5,
    seed: Optional[int] = None,
    allow_missing_keys: Optional[bool] = None,
)

Bases: Compose

Apply a pipeline of transforms with probability p, else return the data unchanged.

The transforms run in order, as in Compose, when a draw of the pipeline's random stream falls below p. It gates any transform, e.g. zeroing the normals of a shape one time in five: RandomApply([Scale(keys="normal", scale=0.0)], p=0.2).

Parameters:

  • transforms (Sequence[Transform]) –

    The transforms to apply, in order.

  • p (float, default: 0.5 ) –

    Probability of applying the transforms.

  • seed (Optional[int], default: None ) –

    Seed of the gate's random stream, which also seeds the transforms; None draws from the global generator (see Randomizable).

  • allow_missing_keys (Optional[bool], default: None ) –

    Forwarded to the transforms, as Compose does.

Example
import torch
from torch_pointcloud.transforms import RandomApply, Scale
drop_normals = RandomApply([Scale(keys="normal", scale=0.0)], p=1.0)
drop_normals({"normal": torch.ones(4, 3)})["normal"].sum().item()  # 0.0

Methods:

  • transform –

    Apply the transforms to the input data with probability p.

transform [source] ΒΆ

transform(data: Any) -> Any

Apply the transforms to the input data with probability p.

DictTransform [source] ΒΆ

DictTransform(
    keys: Optional[KeyCollection] = None,
    allow_missing_keys: bool = False,
)

Bases: Transform

Base class for dictionary transforms.

This class is used to define transforms that operate on a dictionary of data, and implements utility methods for key iteration and error handling.

Note

allow_missing_keys controls the iteration over self.keys performed by iter_keys. Auxiliary keys read by individual transforms (e.g. mask_key in ApplyMask, pos_key in RemoveNearOrigin / FarthestPointSample, face_key in RandomSampleFaceVertices) document their own missing-key behavior in their respective docstrings.

Parameters:

  • keys (Optional[KeyCollection], default: None ) –

    The keys to apply the transform to.

  • allow_missing_keys (bool, default: False ) –

    If True, the transform will not raise an error if keys listed in self.keys are missing from the input dict.

Methods:

  • transform –

    Apply the transform to the dictionary data.

  • iter_keys –

    Iterate over self.keys present in the data, honoring allow_missing_keys.

transform abstractmethod [source] ΒΆ

transform(data: Dict[str, Any]) -> Dict[str, Any]

Apply the transform to the dictionary data.

Parameters:

  • data (Dict[str, Any]) –

    The dictionary data to apply the transform to.

Returns:

  • Dict[str, Any] –

    The transformed dictionary data.

iter_keys [source] ΒΆ

iter_keys(
    data: Dict[str, Any],
    *extra_iterables: Iterable[Any],
    extra_msg: str = "",
) -> Generator[Any, None, None]

Iterate over self.keys present in the data, honoring allow_missing_keys.

Parameters:

  • data (Dict[str, Any]) –

    The dictionary data the transform is applied to.

  • *extra_iterables (Iterable[Any], default: () ) –

    Per-key values (e.g. one output key per input key) zipped with self.keys.

  • extra_msg (str, default: '' ) –

    Message appended to the KeyError raised on a missing key.

Returns:

  • None –

    A generator yielding each present key, or a tuple of the key and its values from

  • None –

    extra_iterables when any is given.