data
Data loading: standard sample keys, packed-batch collation and row offsets, worker seeding, and the data loader.
Classes:
-
DataKeysβStandard keys of a sample dict, shared by the datasets, transforms and models.
-
PointCloudDataLoaderβDataLoaderthat batches point clouds with the packed-batchcollateby default.
Functions:
-
collateβCollate a list of point-cloud sample dicts into one batched dict.
-
mix_pairsβMerge consecutive pairs of samples with a pairwise mix, with probability
pper batch. -
select_inputsβGather the positional inputs of a model's
forwardfrom a batch dict, inkeysorder. -
set_random_statesβRe-seed every seeded random object reachable from
obj, each with its own seed. -
seed_workerβworker_init_fnre-seeding the dataset's seeded random objects from the worker's seed. -
offset_indexβOffset per-element row indices into the packed row layout of a collated batch.
DataKeys [source] ΒΆ
Bases: StrEnum
Standard keys of a sample dict, shared by the datasets, transforms and models.
Members are plain strings, so a key is usable wherever a literal is (e.g. data[DataKeys.POS] and data["pos"] address the same entry). Keys are singular (box, label), while the metric records (Boxes3D, Detection3D) use plural fields (boxes, labels).
PointCloudDataLoader [source] ΒΆ
PointCloudDataLoader(
dataset: Dataset,
*,
batch_from: str = POS,
batch_key: str = BATCH,
stack_keys: Optional[Sequence[str]] = None,
cat_keys: Optional[Sequence[str]] = None,
mix: Optional[
Callable[
[Dict[str, Any], Dict[str, Any]], Dict[str, Any]
]
] = None,
mix_prob: float = 0.0,
**kwargs: Any,
)
Bases: DataLoader
DataLoader that batches point clouds with the packed-batch collate by default.
Wraps torch.utils.data.DataLoader, defaulting collate_fn to collate. How keys collate is set by the spec arguments (batch_from / batch_key for the per-point index, stack_keys for dense per-scene ground truth, cat_keys for ragged per-scene ground truth). These are supplied by the caller, never read off the dataset: transforms rewrite the key set downstream of the dataset (a box key may be derived from an object by a transform), so only the code building the loader knows which keys must stack or cat. Passing collate_fn=... via the usual DataLoader kwarg overrides the spec.
Workers are re-seeded with seed_worker before any worker_init_fn passed in kwargs, so seeded random transforms draw different numbers in every worker and epoch.
Parameters:
-
dataset(Dataset) βThe dataset to load from.
-
batch_from(str, default:POS) βKey whose leading dimension defines the per-point batch index.
-
batch_key(str, default:BATCH) βOutput key for the per-point batch index.
-
stack_keys(Optional[Sequence[str]], default:None) βKeys collated by stacking to a leading batch dim instead of concatenating.
-
cat_keys(Optional[Sequence[str]], default:None) βPacked keys that additionally emit a
batch_<key>per-element scene index. -
mix(Optional[Callable[[Dict[str, Any], Dict[str, Any]], Dict[str, Any]]], default:None) βOptional pairwise mix (e.g.
Mix3D) merging consecutive samples of a batch before collation, with probabilitymix_probper batch; seemix_pairs. -
mix_prob(float, default:0.0) βProbability of mixing a batch.
-
**kwargs(Any, default:{}) βForwarded to
torch.utils.data.DataLoader(batch_size,shuffle,collate_fn, ...).
collate [source] ΒΆ
collate(
data_list: List[Dict[str, Any]],
batch_from: str = POS,
batch_key: str = BATCH,
stack_keys: Optional[KeyCollection] = None,
cat_keys: Optional[KeyCollection] = None,
) -> Dict[str, Any]
Collate a list of point-cloud sample dicts into one batched dict.
By default every per-point tensor is concatenated PyG-style along dim 0 (packed), scalars are stacked, and a per-point batch_key index is synthesized from batch_from. Two extra knobs say how specific keys collate instead:
stack_keys: stack to a new leading batch dim (\((M, \cdot) \to (B, M, \cdot)\), \((N, \cdot) \to (B, N, \cdot)\)) rather than concatenating. Used for fixed-size per-scene tensors that must keep their scene dimension (a plain cat would flatten them).cat_keys: keep these packed (cat) but additionally emit abatch_<key>scene index mirroringbatch_key. Used for ragged per-scene ground truth such asbox\((K, 8)\) ->batch_box\((K,)\).
Every key must be present in every sample; a key missing from a sample raises a ValueError. stack_keys / cat_keys entries absent from all samples are ignored. A key may appear in only one of stack_keys / cat_keys; overlapping entries raise a ValueError.
Parameters:
-
data_list(List[Dict[str, Any]]) βList of sample dicts.
-
batch_from(str, default:POS) βKey whose leading dimension defines the per-point batch index.
-
batch_key(str, default:BATCH) βOutput key for the per-point batch index.
-
stack_keys(Optional[KeyCollection], default:None) βKeys collated by stacking to a leading batch dim instead of concatenating.
-
cat_keys(Optional[KeyCollection], default:None) βPacked keys that additionally emit a
batch_<key>per-element scene index.
Returns:
-
Dict[str, Any]βA single batched dict.
mix_pairs [source] ΒΆ
mix_pairs(
data_list: List[Dict[str, Any]],
mix: Callable[
[Dict[str, Any], Dict[str, Any]], Dict[str, Any]
],
p: float = 1.0,
generator: Optional[Generator] = None,
) -> List[Dict[str, Any]]
Merge consecutive pairs of samples with a pairwise mix, with probability p per batch.
When the draw succeeds, samples \((0, 1), (2, 3), \ldots\) of the batch are each merged into one scene by mix (Mix3D concatenates them), so the batch holds half as many scenes with all their points; an odd last sample passes through. Otherwise the batch is returned unchanged.
Parameters:
-
data_list(List[Dict[str, Any]]) βThe samples of a batch, before collation.
-
mix(Callable[[Dict[str, Any], Dict[str, Any]], Dict[str, Any]]) βPairwise transform called as
mix(first, second). -
p(float, default:1.0) βProbability of mixing the batch.
-
generator(Optional[Generator], default:None) βRandom generator of the draw;
Noneuses the global one.
Returns:
-
List[Dict[str, Any]]βThe (possibly mixed) samples.
select_inputs [source] ΒΆ
Gather the positional inputs of a model's forward from a batch dict, in keys order.
A dotted key reads an attribute of a batch value (e.g. octree.depth). A missing x resolves to None, since models accept x=None and build their features from the positions; any other missing key raises.
Parameters:
-
data(Dict[str, Any]) βCollated batch dict, e.g. from
collateorPointCloudDataLoader. -
keys(Sequence[str]) βBatch keys
forwardtakes positionally, typicallyinfo["input_keys"]fromcreate_model(..., return_info=True).
Returns:
-
List[Any]βThe values to unpack into the model's
forward.
Raises:
-
KeyErrorβIf a key other than
xis missing fromdata.
Example
import torch
import torch_pointcloud as tp
from torch_pointcloud.utils.data import collate, select_inputs
model, info = tp.create_model("pointnet.modelnet40", return_info=True)
samples = [info["transform"]({"pos": torch.rand(2048, 3), "normal": torch.rand(2048, 3)}) for _ in range(2)]
batch = collate(samples)
logits = model(*select_inputs(batch, info["input_keys"]))
set_random_states [source] ΒΆ
Re-seed every seeded random object reachable from obj, each with its own seed.
A random object exposes set_random_state(seed=...) and its generator as R (e.g. a Randomizable transform). Objects are found through sequences, mappings and instance attributes, such as a dataset's transform. Objects drawing from the global generator (R is None) are left as they are.
Parameters:
-
obj(Any) βRoot object, typically a dataset.
-
seed(int) βSeed of the first object; the next ones get
seed + 1,seed + 2, ...
Returns:
-
intβThe seed following the last one used.
seed_worker [source] ΒΆ
worker_init_fn re-seeding the dataset's seeded random objects from the worker's seed.
Every worker receives a copy of the dataset, so a seeded transform would replay the same numbers in every worker. This re-seeds them from torch.utils.data.get_worker_info().seed, which differs per worker and per epoch and is reproducible under torch.manual_seed. PointCloudDataLoader applies it by default; pass it as worker_init_fn to a plain DataLoader.
Parameters:
-
worker_id(int) βIndex of the worker, as passed by the
DataLoader.
offset_index [source] ΒΆ
Offset per-element row indices into the packed row layout of a collated batch.
collate concatenates a row map such as inverse or index as-is, so the entries of each batch element still address that element's own rows. This shifts every entry by the number of rows of the elements collated before it.
Parameters:
-
index(Tensor) βPer-element row indices.
-
index_batch(Tensor) βBatch index of each entry of
index, thebatch_<key>tensorcollateemits forcat_keys. -
batch(Tensor) βBatch index of the rows
indexaddresses (batchfor aninversemap,batch_origin_posfor anindexmap).
Returns:
-
TensorβThe offset row indices.
Shape
index: \((M,)\)index_batch: \((M,)\)batch: \((N,)\)- output: \((M,)\)