clips_at_random_indices¶
- torchcodec.samplers.clips_at_random_indices(decoder: VideoDecoder, *, num_clips: int = 1, num_frames_per_clip: int = 1, num_indices_between_frames: int = 1, sampling_range_start: int = 0, sampling_range_end: Optional[int] = None, policy: Literal['repeat_last', 'wrap','error'] = 'repeat_last') FrameBatch [source]¶
在随机索引处采样片段。
- 参数:
decoder (VideoDecoder) – 用于采样片段的
VideoDecoder
实例。num_clips (int, optional) – 要返回的片段数量。默认值:1。
num_frames_per_clip (int, optional) – 每个片段的帧数。默认值:1。
num_indices_between_frames (int, optional) – 片段内帧之间的索引数。默认值:1,表示帧是连续的。这有时称为“膨胀 (dilation)”。
sampling_range_start (int, optional) – 采样范围的起始,定义了片段可能开始的第一个索引。默认值:0,即视频的开始处。
sampling_range_end (int 或 None, optional) – 采样范围的结束,定义了片段可能开始的最后一个索引。此值为独占,即片段只能在 [
sampling_range_start
,sampling_range_end
) 范围内开始。如果为 None(默认值),该值将自动设置,使得片段不会超出视频末尾。例如,如果视频中最后一个有效索引是 99 且片段跨越 10 帧,则此值设置为 99 - 10 + 1 = 90。接受负值,等价于len(video) - val
。当片段超出视频末尾时,policy
参数定义如何构建此类片段。policy (str, optional) –
定义如何构建超出视频末尾的片段。最好用一个例子来说明:假设视频中最后一个有效索引是 99,并且采样了一个从索引 95 开始的片段,其中
num_frames_per_clip=5
且num_indices_between_frames=2
,则片段中帧的索引应该是 [95, 97, 99, 101, 103]。但 101 和 103 是无效索引,因此policy
参数定义了如何用有效索引替换这些帧“repeat_last”:重复片段中最后一个有效帧。我们将得到 [95, 97, 99, 99, 99]。
“wrap”:循环回到片段的开头。我们将得到 [95, 97, 99, 95, 97]。
“error”:引发错误。
默认值为 “repeat_last”。请注意,当
sampling_range_end=None
(默认)时,此策略参数不太可能相关。
- 返回:
采样的片段,作为 5D
FrameBatch
。data
字段的形状是 (num_clips
,num_frames_per_clips
, …),其中 … 是 (H, W, C) 或 (C, H, W),具体取决于VideoDecoder
的dimension_order
参数。pts_seconds
和duration_seconds
字段的形状是 (num_clips
,num_frames_per_clips
)。- 返回类型:
使用
clips_at_random_indices
的示例