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.transformmethod, 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_reprmethod, 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.
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
DictTransformchild, recursively through nestedComposeobjects, 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.Noneleaves the children as built.
Example
For example, to chain a random sample and a normalization transform, we can do the following:
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] ΒΆ
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 nostate) 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.
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;
Nonedraws from the global generator (seeRandomizable). -
allow_missing_keys(Optional[bool], default:None) βForwarded to the transforms, as
Composedoes.
Example
Methods:
-
transformβApply the transforms to the input data with probability
p.
DictTransform [source] ΒΆ
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 inself.keysare missing from the input dict.
Methods:
-
transformβApply the transform to the dictionary data.
-
iter_keysβIterate over
self.keyspresent in the data, honoringallow_missing_keys.
transform abstractmethod [source] ΒΆ
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
KeyErrorraised on a missing key.
Returns:
-
NoneβA generator yielding each present key, or a tuple of the key and its values from
-
Noneβextra_iterableswhen any is given.