torchwrench.nn.functional.segments module¶
- torchwrench.nn.functional.segments.activity_to_segments(x: Tensor) LongTensor[source]¶
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.
- torchwrench.nn.functional.segments.activity_to_segments_list(x: Tensor) list[tuple[int, int]] | list[source]¶
-
torchwrench.nn.functional.segments.segments_list_to_activity(segments_list: list[tuple[int, int]] | Tensor | list, maxsize: int | None =
None, *, device: device | None | 'default' | 'cuda_if_available' | str | int =None) BoolTensor[source]¶ Convert list of (start, end) tuples to activity boolean tensor.
Example¶
`python >>> segments = [(3, 6), (8, 9)] >>> segments_list_to_activity(segments) ... tensor([False, False, False, True, True, True, False, False, True]) `- 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)
-
torchwrench.nn.functional.segments.segments_to_activity(x: Tensor, maxsize: int | None =
None) BoolTensor[source]¶ Convert stacked list/tensor of starts end stops separated to activity boolean tensor.
-
torchwrench.nn.functional.segments.segments_to_segments_list(segments: Tensor, maxsize: int | tuple[int, ...] | None =
None) 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.