conversion
Conversions between iterables, batch index representations (offset, bincount, cu_seqlens), and tensor formats.
Functions:
-
is_iterableβCheck if a value is iterable.
-
ensure_iterableβConvert a value to the given iterable type. If the value is a scalar, it will be wrapped in it.
-
ensure_iterable_sizeβConvert a value to a list of a given size.
-
ensure_listβConvert a value to a list. If the value is a numpy array or torch tensor,
-
ensure_list_sizeβConvert a value to a list of a given size.
-
ensure_tupleβConvert a value to a tuple. If the value is a scalar or single element,
-
ensure_tuple_sizeβConvert a value to a tuple of a given size.
-
ensure_optionβEnsure that the provided value is one of the given options.
-
offset_to_bincountβConvert an offset tensor to a bincount tensor.
-
offset_to_batchβConvert an offset tensor to a batch index tensor.
-
offset_to_cu_seqlensβConvert an offset tensor to a cumulative sequence lengths tensor.
-
batch_to_offsetβConvert a batch index tensor to an offset tensor.
-
batch_to_bincountβConvert a batch index tensor to a bincount tensor.
-
batch_to_cu_seqlensβConvert a batch index tensor to a cumulative sequence lengths tensor.
-
bincount_to_offsetβConvert a bincount tensor to an offset tensor.
-
bincount_to_batchβConvert a bincount tensor to a batch index tensor.
-
bincount_to_cu_seqlensβConvert a bincount tensor to a cumulative sequence lengths tensor.
-
cu_seqlens_to_offsetβConvert a cumulative sequence lengths tensor to an offset tensor.
-
cu_seqlens_to_bincountβConvert a cumulative sequence lengths tensor to a bincount tensor.
-
cu_seqlens_to_batchβConvert a cumulative sequence lengths tensor to a batch index tensor.
-
convert_to_spconv_tensorβConvert point features and coordinates to a
spconv.SparseConvTensorsparse tensor. -
convert_from_spconv_tensorβConvert a
spconv.SparseConvTensorback to packed features, coordinates, and batch indices. -
convert_to_tensorβUtility function to convert data to a tensor object. It will convert data following these rules:
-
convert_to_numpyβConvert data to a numpy array. It will convert data following these rules:
-
ensure_msg_listβUtility function to ensure that items are converted in nested lists compatible
-
ensure_msg_list_sizeβValidate the length of a sequence, then nest it for Multi-Scale Grouping (MSG) compatibility.
is_iterable [source] ΒΆ
Check if a value is iterable. A value is considered iterable if it is an instance of Iterable (i.e. list, tuple, set or any iterable defining the __iter__ method) and not an instance of str or bytes.
It is worth noting that torch.Tensor and np.ndarray are considered iterable (here) only if they have a dimension greater than 0 (i.e. not scalars).
Parameters:
-
value(Any) βThe value to check.
Returns:
-
TypeGuard[Iterable[Any]]βTrue if the value is iterable, False otherwise.
ensure_iterable [source] ΒΆ
ensure_iterable(
value: Any,
type: Type[T],
recursive: bool = False,
none_as_empty: bool = False,
) -> T
Convert a value to the given iterable type. If the value is a scalar, it will be wrapped in it.
Numpy arrays and torch tensors are converted to nested lists first, so their elements come back as Python scalars.
Parameters:
-
value(Any) βThe value to convert.
-
type(Type[T]) βThe iterable type to build (e.g.
list,tuple). -
recursive(bool, default:False) βIf True, the function will recursively apply itself to the elements of the iterable.
-
none_as_empty(bool, default:False) βIf True, and the value is None, an empty iterable will be returned.
Returns:
-
TβThe value as an iterable of the given type.
ensure_iterable_size [source] ΒΆ
ensure_iterable_size(
value: Any,
type: Type[T],
size: int,
recursive: bool = False,
none_as_empty: bool = False,
extra_msg: str = "",
) -> T
Convert a value to a list of a given size. If the value is a scalar, it will be repeated to match the size. If the value's length does not match the size, an error will be raised.
ensure_list [source] ΒΆ
Convert a value to a list. If the value is a numpy array or torch tensor, it will be converted to a list. If the value is a scalar, it will be wrapped in a list.
Note
If the value is None, it will be wrapped in a list.
Parameters:
-
value(Any) βThe value to convert.
-
recursive(bool, default:False) βIf True, the function will recursively apply itself to the elements of the list.
-
none_as_empty(bool, default:False) βIf True, and the value is None, an empty list will be returned.
Returns:
-
List[Any]βThe value as a list.
Examples:
ensure_list_size [source] ΒΆ
ensure_list_size(
value: Any,
size: int,
recursive: bool = False,
none_as_empty: bool = False,
extra_msg: str = "",
) -> List[Any]
Convert a value to a list of a given size. If the value is a scalar, it will be repeated to match the size. If the value's length does not match the size, an error will be raised.
ensure_tuple [source] ΒΆ
ensure_tuple(
value: Any,
recursive: bool = False,
none_as_empty: bool = False,
) -> Tuple[Any, ...]
Convert a value to a tuple. If the value is a scalar or single element, it will be wrapped in a tuple.
Parameters:
-
value(Any) βThe value to convert.
-
recursive(bool, default:False) βIf True, the function will recursively apply itself to the elements of the tuple.
-
none_as_empty(bool, default:False) βIf True, and the value is None, an empty tuple will be returned.
Returns:
-
Tuple[Any, ...]βThe value as a tuple.
Examples:
ensure_tuple_size [source] ΒΆ
ensure_tuple_size(
value: Any,
size: int,
recursive: bool = False,
none_as_empty: bool = False,
extra_msg: str = "",
) -> Tuple[Any, ...]
Convert a value to a tuple of a given size. If the value is a scalar, it will be repeated to match the size. If the value's length does not match the size, an error will be raised.
Parameters:
-
value(Any) βThe value to convert.
-
size(int) βThe size of the tuple.
-
recursive(bool, default:False) βIf True, the function will recursively apply itself to the elements of the tuple.
-
none_as_empty(bool, default:False) βIf True, and the value is None, an empty tuple will be returned.
-
extra_msg(str, default:'') βAn additional message to include in the error.
Returns:
-
Tuple[Any, ...]βThe value as a tuple of the given size.
Examples:
ensure_option [source] ΒΆ
Ensure that the provided value is one of the given options. This function will return the value if it is one of the given options. If the value is not a member of the options, a ValueError will be raised.
Supported options are iterables of hashable values, enums, or literal types.
Parameters:
-
value(T) βThe value to check.
-
options(Any) βThe options to check against.
-
name(str, default:'option') βThe name of the option displayed in error messages. This is used to track which parameter is invalid.
Returns:
-
TβThe option.
Examples:
>>> ensure_option("one", ["one", "two"]) # Ok
'one'
>>> ensure_option("one", Literal["one", "two"]) # Ok
'one'
>>> ensure_option(1, Enum("Number", "ONE, TWO")) # Ok
1
>>> ensure_option("three", ["one", "two"]) # Error
Traceback (most recent call last):
...
ValueError: Invalid option: 'three'. Valid options are: 'one', 'two'.
offset_to_cu_seqlens [source] ΒΆ
Convert an offset tensor to a cumulative sequence lengths tensor. A cumulative sequence length is a tensor of shape $ (N + 2,)$ where \(N\) is the size of the batch. The first element is 0 and the last element is the size of the batch. The other elements are the cumulative sum of the batch sizes.
Note
This function was provided to ease the conversion between offset tensor and cu_seqlens tensor format required by spconv.
See Also
This function is related to offset_to_batch.
Parameters:
-
offset(Tensor) βThe offset tensor.
Returns:
-
TensorβThe cumulative sequence lengths tensor.
Examples:
batch_to_cu_seqlens [source] ΒΆ
Convert a batch index tensor to a cumulative sequence lengths tensor. A cumulative sequence length is a tensor of shape $ (N + 2,)$ where \(N\) is the size of the batch. The first element is 0 and the last element is the size of the batch. The other elements are the cumulative sum of the batch sizes.
Note
This function was provided to ease the conversion between batch tensor and cu_seqlens tensor format required by spconv.
See Also
This function is related to batch_to_offset.
Parameters:
-
batch(Tensor) βThe batch indices of the points.
Returns:
-
TensorβThe cumulative sequence lengths tensor.
Examples:
bincount_to_cu_seqlens [source] ΒΆ
Convert a bincount tensor to a cumulative sequence lengths tensor.
Parameters:
-
bincount(Tensor) βThe bincount tensor.
Returns:
-
TensorβThe cumulative sequence lengths tensor.
Examples:
cu_seqlens_to_offset [source] ΒΆ
Convert a cumulative sequence lengths tensor to an offset tensor.
Parameters:
-
cu_seqlens(Tensor) βThe cumulative sequence lengths tensor.
Returns:
-
TensorβThe offset tensor.
Examples:
cu_seqlens_to_bincount [source] ΒΆ
Convert a cumulative sequence lengths tensor to a bincount tensor.
Parameters:
-
cu_seqlens(Tensor) βThe cumulative sequence lengths tensor.
Returns:
-
TensorβThe bincount tensor.
Examples:
cu_seqlens_to_batch [source] ΒΆ
Convert a cumulative sequence lengths tensor to a batch index tensor.
Parameters:
-
cu_seqlens(Tensor) βThe cumulative sequence lengths tensor.
Returns:
-
TensorβThe batch index tensor.
Examples:
convert_to_spconv_tensor [source] ΒΆ
convert_to_spconv_tensor(
x: Tensor,
pos: Tensor,
batch: Tensor,
spatial_shape: Optional[Sequence[int]] = None,
padding: int = 96,
batch_size: Optional[int] = None,
) -> SparseConvTensor
Convert point features and coordinates to a spconv.SparseConvTensor sparse tensor.
Parameters:
-
x(Tensor) βThe point features of shape \((N, C)\).
-
pos(Tensor) βThe integer voxel coordinates of shape \((N, 3)\).
-
batch(Tensor) βThe batch indices of the points of shape \((N,)\).
-
spatial_shape(Optional[Sequence[int]], default:None) βThe spatial shape of the sparse tensor. When
None, it is inferred as the per-axis maximum ofpospluspadding. -
padding(int, default:96) βPadding (in voxels) added to the inferred spatial shape.
-
batch_size(Optional[int], default:None) βNumber of scenes in the batch. When
None, it is inferred asbatch.max() + 1, which drops trailing empty scenes; pass it explicitly when those must be preserved.
Returns:
-
SparseConvTensorβThe
spconv.SparseConvTensorsparse tensor.
Shape
x: \((N, C)\)pos: \((N, 3)\)batch: \((N,)\)
convert_from_spconv_tensor [source] ΒΆ
Convert a spconv.SparseConvTensor back to packed features, coordinates, and batch indices.
Parameters:
-
spconv_tensor(SparseConvTensor) βThe sparse tensor to unpack.
Returns:
-
TensorβTuple
(x, pos, batch)with features \((N, C)\), integer voxel coordinates \((N, 3)\), -
Tensorβand batch indices \((N,)\).
convert_to_tensor [source] ΒΆ
Utility function to convert data to a tensor object. It will convert data following these rules:
- If the data is a tensor, it will be returned as is.
- If the data is a numpy array, it will be converted to a tensor.
- If the data is a scalar, it will be converted to a tensor scalar.
- If the data is a dictionary, each value will be converted recursively.
- If the data is not supported, a
TypeErrorwill be raised unlessstrictis False.
Parameters:
-
data(Any) βThe data to convert.
-
strict(bool, default:True) βIf True, a
TypeErrorwill be raised if the data type is not supported. If False, the data will be returned as is.
Returns:
-
AnyβThe converted data.
Examples:
>>> convert_to_tensor([1, 2, 3])
tensor([1, 2, 3])
>>> convert_to_tensor(np.array([1, 2, 3]))
tensor([1, 2, 3])
>>> convert_to_tensor(torch.tensor([1, 2, 3]))
tensor([1, 2, 3])
>>> convert_to_tensor({"a": [1, 2, 3], "b": 4})
{'a': tensor([1, 2, 3]), 'b': tensor(4)}
>>> convert_to_tensor(None)
Traceback (most recent call last):
...
TypeError: Unsupported data type...
>>> print(convert_to_tensor(None, strict=False))
None
>>> convert_to_tensor("value", strict=False)
'value'
convert_to_numpy [source] ΒΆ
Convert data to a numpy array. It will convert data following these rules:
- If the data is a numpy array, it will be returned as is.
- If the data is a tensor, it will be converted to a numpy array.
- If the data is a scalar, it will be converted to a numpy scalar.
- If the data is a dictionary, each value will be converted recursively.
- If the data is not supported, a
TypeErrorwill be raised unlessstrictis False.
Parameters:
-
data(Any) βThe data to convert.
-
strict(bool, default:True) βIf True, a
TypeErrorwill be raised if the data type is not supported. If False, the data will be returned as is.
Returns:
-
AnyβThe converted data.
Examples:
>>> convert_to_numpy([1, 2, 3])
array([1, 2, 3])
>>> convert_to_numpy(np.array([1, 2, 3]))
array([1, 2, 3])
>>> convert_to_numpy(torch.tensor([1, 2, 3]))
array([1, 2, 3])
>>> convert_to_numpy({"a": [1, 2, 3], "b": 4})
{'a': array([1, 2, 3]), 'b': array(4)}
>>> convert_to_numpy(None)
Traceback (most recent call last):
...
TypeError: Unsupported data type...
>>> print(convert_to_numpy(None, strict=False))
None
>>> convert_to_numpy("value", strict=False)
'value'
ensure_msg_list [source] ΒΆ
Utility function to ensure that items are converted in nested lists compatible with Multi-Scale Grouping (MSG) mode. This function will convert a list of list into a list of list of list.
Example
Let's say we have designed a network where the first two SA blocks are not using MSG mode, but the last SA block is using MSG mode.
Calling ensure_msg_list will make sure the provided channels are compliant with the MSG mode.
ensure_msg_list_size [source] ΒΆ
Validate the length of a sequence, then nest it for Multi-Scale Grouping (MSG) compatibility.
Parameters:
-
value(Sequence[Any]) βSequence of per-block channel specifications.
-
size(int) βExpected number of elements in
value. -
extra_msg(str, default:'') βExtra context appended to the error message.
Returns:
-
Sequence[Any]βThe value converted to a list of lists of lists (one inner list per grouping scale).
Raises:
-
ValueErrorβIf
valuedoes not have exactlysizeelements.