-
Notifications
You must be signed in to change notification settings - Fork 512
/
video_batch_sampler.py
150 lines (129 loc) · 4.89 KB
/
video_batch_sampler.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#
# For licensing see accompanying LICENSE file.
# Copyright (C) 2024 Apple Inc. All Rights Reserved.
#
import argparse
from typing import Iterator, Tuple
from corenet.data.sampler import SAMPLER_REGISTRY
from corenet.data.sampler.batch_sampler import BatchSampler, BatchSamplerDDP
@SAMPLER_REGISTRY.register(name="video_batch_sampler")
class VideoBatchSampler(BatchSampler):
"""Standard Batch Sampler for videos. This sampler yields batches of fixed (1) batch size,
(2) spatial resolutions, (3) frames per clip, and (4) number of clips per video.
Args:
opts: command line argument
n_data_samples: Number of samples in the dataset
is_training: Training or validation mode. Default: False
"""
def __init__(
self,
opts: argparse.Namespace,
n_data_samples: int,
is_training: bool = False,
*args,
**kwargs,
) -> None:
super().__init__(
opts=opts, n_data_samples=n_data_samples, is_training=is_training
)
self.default_frames = getattr(opts, "sampler.bs.num_frames_per_clip")
self.clips_per_video = getattr(opts, "sampler.bs.clips_per_video")
def __iter__(self) -> Iterator[Tuple[int, int, int, int, int]]:
indices = self.get_indices()
start_index = 0
batch_size = self.batch_size_gpu0
indices_len = len(indices)
while start_index < indices_len:
end_index = min(start_index + batch_size, indices_len)
batch_ids = indices[start_index:end_index]
start_index += batch_size
if len(batch_ids) > 0:
batch = [
(
self.crop_size_h,
self.crop_size_w,
b_id,
self.default_frames,
self.clips_per_video,
)
for b_id in batch_ids
]
yield batch
@classmethod
def add_arguments(cls, parser: argparse.ArgumentParser) -> argparse.Namespace:
if cls != VideoBatchSampler:
# Don't re-register arguments in subclasses that don't override `add_arguments()`.
return parser
group = parser.add_argument_group(cls.__name__)
group.add_argument(
"--sampler.bs.num-frames-per-clip",
default=8,
type=int,
help="Number of frames per video clip. Defaults to 8.",
)
group.add_argument(
"--sampler.bs.clips-per-video",
default=1,
type=int,
help="Number of clips per video. Defaults to 1.",
)
return parser
def extra_repr(self) -> str:
extra_repr_str = super().extra_repr()
extra_repr_str += (
f"\n\t n_clips={self.clips_per_video}"
f"\n\t n_frames_per_clip={self.default_frames}"
)
return extra_repr_str
@SAMPLER_REGISTRY.register(name="video_batch_sampler_ddp")
class VideoBatchSamplerDDP(BatchSamplerDDP):
"""DDP version of VideoBatchSampler
Args:
opts: command line argument
n_data_samples: Number of samples in the dataset
is_training: Training or validation mode. Default: False
"""
def __init__(
self,
opts: argparse.Namespace,
n_data_samples: int,
is_training: bool = False,
*args,
**kwargs,
) -> None:
super().__init__(
opts=opts, n_data_samples=n_data_samples, is_training=is_training
)
self.default_frames = getattr(opts, "sampler.bs.num_frames_per_clip")
self.clips_per_video = getattr(opts, "sampler.bs.clips_per_video")
def __iter__(self) -> Iterator[Tuple[int, int, int, int, int]]:
indices_rank_i = self.get_indices_rank_i()
start_index = 0
batch_size = self.batch_size_gpu0
indices_len = len(indices_rank_i)
while start_index < indices_len:
end_index = min(start_index + batch_size, indices_len)
batch_ids = indices_rank_i[start_index:end_index]
n_batch_samples = len(batch_ids)
if n_batch_samples != batch_size:
batch_ids += indices_rank_i[: (batch_size - n_batch_samples)]
start_index += batch_size
if len(batch_ids) > 0:
batch = [
(
self.crop_size_h,
self.crop_size_w,
b_id,
self.default_frames,
self.clips_per_video,
)
for b_id in batch_ids
]
yield batch
def extra_repr(self) -> str:
extra_repr_str = super().extra_repr()
extra_repr_str += (
f"\n\t n_clips={self.clips_per_video}"
f"\n\t n_frames_per_clip={self.default_frames}"
)
return extra_repr_str