torchoutil.nn.functional.segments module¶
- activity_to_segments(
- x: Tensor,
Extracts segments start and end positions from a boolean activity/mask tensor.
Example 1¶
>>> x = torch.as_tensor([0, 1, 1, 0, 0, 1, 1, 1, 1, 0]).bool() >>> starts, ends = extract_segments(x) >>> starts ... tensor([1, 5]) >>> ends ... tensor([3, 9])
Example 2¶
>>> x = torch.as_tensor([[1, 1, 1, 0], [1, 0, 0, 1]]).bool() >>> indices, starts, ends = extract_segments(x) >>> indices ... tensor([0, 1, 1]) >>> starts ... tensor([0, 0, 3]) >>> ends ... tensor([3, 1, 4])
- Args:
x: (…, N) bool tensor containing D dims
- Returns:
- segments: (D+1, M) tensor, where M is the total number of segments
When D > 1, segments also contains indices of the source column for each start and end value. See Example 2 for details.
- extract_segments(
- x: Tensor,
Extracts segments start and end positions from a boolean activity/mask tensor.
Example 1¶
>>> x = torch.as_tensor([0, 1, 1, 0, 0, 1, 1, 1, 1, 0]).bool() >>> starts, ends = extract_segments(x) >>> starts ... tensor([1, 5]) >>> ends ... tensor([3, 9])
Example 2¶
>>> x = torch.as_tensor([[1, 1, 1, 0], [1, 0, 0, 1]]).bool() >>> indices, starts, ends = extract_segments(x) >>> indices ... tensor([0, 1, 1]) >>> starts ... tensor([0, 0, 3]) >>> ends ... tensor([3, 1, 4])
- Args:
x: (…, N) bool tensor containing D dims
- Returns:
- segments: (D+1, M) tensor, where M is the total number of segments
When D > 1, segments also contains indices of the source column for each start and end value. See Example 2 for details.
- segments_list_to_activity(
- segments_list: List[Tuple[int, int]] | Tensor | list,
- maxsize: int | None = None,
- *,
- device: device | None | Literal['default', 'cuda_if_available'] | str | int = None,
Convert list of (start, end) tuples to activity boolean tensor.
- Args:
segments_list: list of (start, end) tuples of shape (*, N, 2). maxsize: Optional max size. If None, use segments_list.max(). defaults to None. device: Optional output device. If None and segments_list is a tensor, it will use the same device. defaults to None.
- Returns:
activity boolean tensor of shape (*, maxsize)
- segments_to_activity( ) BoolTensor[source]¶
Convert stacked list/tensor of starts end stops separated to activity boolean tensor.
- segments_to_list( ) List[Tuple[int, int]] | list[source]¶
Convert stacked list/tensor of starts end stops separated to list of (start, end) tuples.
- Args:
x: (2+C, N) tensor, where C defines indices in dimensions of a segments for 3D activity tensors. maxsize: Optional max size. If None, use x.max().
- Returns:
- list of (start, end) tuples of shape (*, N, 2).
note: (*) corresponds to C batched dimensions.
- segments_to_segments_list( ) List[Tuple[int, int]] | list[source]¶
Convert stacked list/tensor of starts end stops separated to list of (start, end) tuples.
- Args:
x: (2+C, N) tensor, where C defines indices in dimensions of a segments for 3D activity tensors. maxsize: Optional max size. If None, use x.max().
- Returns:
- list of (start, end) tuples of shape (*, N, 2).
note: (*) corresponds to C batched dimensions.