-
Notifications
You must be signed in to change notification settings - Fork 512
/
variable_batch_sampler.py
378 lines (330 loc) · 14.8 KB
/
variable_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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
#
# For licensing see accompanying LICENSE file.
# Copyright (C) 2024 Apple Inc. All Rights Reserved.
#
import argparse
import random
from typing import Iterator, Tuple
from corenet.constants import DEFAULT_IMAGE_HEIGHT, DEFAULT_IMAGE_WIDTH
from corenet.data.sampler import SAMPLER_REGISTRY
from corenet.data.sampler.base_sampler import BaseSampler, BaseSamplerDDP
from corenet.data.sampler.utils import image_batch_pairs
from corenet.utils import logger
@SAMPLER_REGISTRY.register(name="variable_batch_sampler")
class VariableBatchSampler(BaseSampler):
"""Variably-size multi-scale batch sampler <https://arxiv.org/abs/2110.02178?context=cs.LG>` for data parallel.
This sampler yields batches with variable spatial resolution and batch size.
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
)
crop_size_w = getattr(opts, "sampler.vbs.crop_size_width")
crop_size_h = getattr(opts, "sampler.vbs.crop_size_height")
min_crop_size_w = getattr(opts, "sampler.vbs.min_crop_size_width")
max_crop_size_w = getattr(opts, "sampler.vbs.max_crop_size_width")
min_crop_size_h = getattr(opts, "sampler.vbs.min_crop_size_height")
max_crop_size_h = getattr(opts, "sampler.vbs.max_crop_size_height")
check_scale_div_factor = getattr(opts, "sampler.vbs.check_scale")
max_img_scales = getattr(opts, "sampler.vbs.max_n_scales")
scale_inc = getattr(opts, "sampler.vbs.scale_inc")
min_scale_inc_factor = getattr(opts, "sampler.vbs.min_scale_inc_factor")
max_scale_inc_factor = getattr(opts, "sampler.vbs.max_scale_inc_factor")
scale_ep_intervals = getattr(opts, "sampler.vbs.ep_intervals")
if isinstance(scale_ep_intervals, int):
scale_ep_intervals = [scale_ep_intervals]
self.min_crop_size_w = min_crop_size_w
self.max_crop_size_w = max_crop_size_w
self.min_crop_size_h = min_crop_size_h
self.max_crop_size_h = max_crop_size_h
self.crop_size_w = crop_size_w
self.crop_size_h = crop_size_h
self.min_scale_inc_factor = min_scale_inc_factor
self.max_scale_inc_factor = max_scale_inc_factor
self.scale_ep_intervals = scale_ep_intervals
self.max_img_scales = max_img_scales
self.check_scale_div_factor = check_scale_div_factor
self.scale_inc = scale_inc
if is_training:
self.img_batch_tuples = image_batch_pairs(
crop_size_h=self.crop_size_h,
crop_size_w=self.crop_size_w,
batch_size_gpu0=self.batch_size_gpu0,
n_gpus=self.n_gpus,
max_scales=self.max_img_scales,
check_scale_div_factor=self.check_scale_div_factor,
min_crop_size_w=self.min_crop_size_w,
max_crop_size_w=self.max_crop_size_w,
min_crop_size_h=self.min_crop_size_h,
max_crop_size_h=self.max_crop_size_h,
)
else:
self.img_batch_tuples = [(crop_size_h, crop_size_w, self.batch_size_gpu0)]
def __iter__(self) -> Iterator[Tuple[int, int, int]]:
img_indices = self.get_indices()
start_index = 0
n_samples = len(img_indices)
while start_index < n_samples:
crop_h, crop_w, batch_size = random.choice(self.img_batch_tuples)
end_index = min(start_index + batch_size, n_samples)
batch_ids = img_indices[start_index:end_index]
n_batch_samples = len(batch_ids)
if len(batch_ids) != batch_size:
batch_ids += img_indices[: (batch_size - n_batch_samples)]
start_index += batch_size
if len(batch_ids) > 0:
batch = [(crop_h, crop_w, b_id) for b_id in batch_ids]
yield batch
def update_scales(
self, epoch: int, is_master_node: bool = False, *args, **kwargs
) -> None:
"""Update the scales in variable batch sampler at specified epoch intervals during training."""
if epoch in self.scale_ep_intervals and self.scale_inc:
self.min_crop_size_w += int(
self.min_crop_size_w * self.min_scale_inc_factor
)
self.max_crop_size_w += int(
self.max_crop_size_w * self.max_scale_inc_factor
)
self.min_crop_size_h += int(
self.min_crop_size_h * self.min_scale_inc_factor
)
self.max_crop_size_h += int(
self.max_crop_size_h * self.max_scale_inc_factor
)
self.img_batch_tuples = image_batch_pairs(
crop_size_h=self.crop_size_h,
crop_size_w=self.crop_size_w,
batch_size_gpu0=self.batch_size_gpu0,
n_gpus=self.n_gpus,
max_scales=self.max_img_scales,
check_scale_div_factor=self.check_scale_div_factor,
min_crop_size_w=self.min_crop_size_w,
max_crop_size_w=self.max_crop_size_w,
min_crop_size_h=self.min_crop_size_h,
max_crop_size_h=self.max_crop_size_h,
)
if is_master_node:
logger.log("Scales updated in {}".format(self.__class__.__name__))
logger.log("New scales: {}".format(self.img_batch_tuples))
def extra_repr(self) -> str:
extra_repr_str = super().extra_repr()
extra_repr_str += (
f"\n\t base_im_size=(h={self.crop_size_h}, w={self.crop_size_w})"
f"\n\t base_batch_size={self.batch_size_gpu0}"
f"\n\t scales={self.img_batch_tuples}"
f"\n\t scale_inc={self.scale_inc}"
f"\n\t min_scale_inc_factor={self.min_scale_inc_factor}"
f"\n\t max_scale_inc_factor={self.max_scale_inc_factor}"
f"\n\t ep_intervals={self.scale_ep_intervals}"
)
return extra_repr_str
@classmethod
def add_arguments(cls, parser: argparse.ArgumentParser) -> argparse.ArgumentParser:
if cls != VariableBatchSampler:
# 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.vbs.crop-size-width",
default=DEFAULT_IMAGE_WIDTH,
type=int,
help=f"Base crop size (along width) during training. Defaults to {DEFAULT_IMAGE_WIDTH}.",
)
group.add_argument(
"--sampler.vbs.crop-size-height",
default=DEFAULT_IMAGE_HEIGHT,
type=int,
help=f"Base crop size (along height) during training. Defaults to {DEFAULT_IMAGE_HEIGHT}.",
)
group.add_argument(
"--sampler.vbs.min-crop-size-width",
default=160,
type=int,
help="Min. crop size along width during training. Defaults to 160.",
)
group.add_argument(
"--sampler.vbs.max-crop-size-width",
default=320,
type=int,
help="Max. crop size along width during training. Defaults to 320.",
)
group.add_argument(
"--sampler.vbs.min-crop-size-height",
default=160,
type=int,
help="Min. crop size along height during training. Defaults to 160.",
)
group.add_argument(
"--sampler.vbs.max-crop-size-height",
default=320,
type=int,
help="Max. crop size along height during training. Defaults to 320.",
)
group.add_argument(
"--sampler.vbs.max-n-scales",
default=5,
type=int,
help="Max. scales in variable batch sampler. Defaults to 5.",
)
group.add_argument(
"--sampler.vbs.check-scale",
default=32,
type=int,
help="Image scales should be divisible by this factor. Defaults to 32.",
)
group.add_argument(
"--sampler.vbs.ep-intervals",
default=[40],
type=int,
help="Epoch intervals at which scales should be adjusted. Defaults to 40.",
)
group.add_argument(
"--sampler.vbs.min-scale-inc-factor",
default=1.0,
type=float,
help="Factor by which we should increase the minimum scale. Defaults to 1.0",
)
group.add_argument(
"--sampler.vbs.max-scale-inc-factor",
default=1.0,
type=float,
help="Factor by which we should increase the maximum scale. Defaults to 1.0",
)
group.add_argument(
"--sampler.vbs.scale-inc",
action="store_true",
default=False,
help="Increase image scales during training. Defaults to False.",
)
return parser
@SAMPLER_REGISTRY.register(name="variable_batch_sampler_ddp")
class VariableBatchSamplerDDP(BaseSamplerDDP):
"""DDP version of VariableBatchSampler
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
)
crop_size_w = getattr(opts, "sampler.vbs.crop_size_width")
crop_size_h = getattr(opts, "sampler.vbs.crop_size_height")
min_crop_size_w = getattr(opts, "sampler.vbs.min_crop_size_width")
max_crop_size_w = getattr(opts, "sampler.vbs.max_crop_size_width")
min_crop_size_h = getattr(opts, "sampler.vbs.min_crop_size_height")
max_crop_size_h = getattr(opts, "sampler.vbs.max_crop_size_height")
check_scale_div_factor = getattr(opts, "sampler.vbs.check_scale")
max_img_scales = getattr(opts, "sampler.vbs.max_n_scales")
scale_inc = getattr(opts, "sampler.vbs.scale_inc")
min_scale_inc_factor = getattr(opts, "sampler.vbs.min_scale_inc_factor")
max_scale_inc_factor = getattr(opts, "sampler.vbs.max_scale_inc_factor")
scale_ep_intervals = getattr(opts, "sampler.vbs.ep_intervals")
if isinstance(scale_ep_intervals, int):
scale_ep_intervals = [scale_ep_intervals]
self.crop_size_h = crop_size_h
self.crop_size_w = crop_size_w
self.min_crop_size_h = min_crop_size_h
self.max_crop_size_h = max_crop_size_h
self.min_crop_size_w = min_crop_size_w
self.max_crop_size_w = max_crop_size_w
self.min_scale_inc_factor = min_scale_inc_factor
self.max_scale_inc_factor = max_scale_inc_factor
self.scale_ep_intervals = scale_ep_intervals
self.max_img_scales = max_img_scales
self.check_scale_div_factor = check_scale_div_factor
self.scale_inc = scale_inc
if is_training:
self.img_batch_tuples = image_batch_pairs(
crop_size_h=self.crop_size_h,
crop_size_w=self.crop_size_w,
batch_size_gpu0=self.batch_size_gpu0,
n_gpus=self.num_replicas,
max_scales=self.max_img_scales,
check_scale_div_factor=self.check_scale_div_factor,
min_crop_size_w=self.min_crop_size_w,
max_crop_size_w=self.max_crop_size_w,
min_crop_size_h=self.min_crop_size_h,
max_crop_size_h=self.max_crop_size_h,
)
else:
self.img_batch_tuples = [
(self.crop_size_h, self.crop_size_w, self.batch_size_gpu0)
]
def __iter__(self) -> Iterator[Tuple[int, int, int]]:
indices_rank_i = self.get_indices_rank_i()
start_index = 0
n_samples_rank_i = len(indices_rank_i)
while start_index < n_samples_rank_i:
crop_h, crop_w, batch_size = random.choice(self.img_batch_tuples)
end_index = min(start_index + batch_size, n_samples_rank_i)
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 = [(crop_h, crop_w, b_id) for b_id in batch_ids]
yield batch
def update_scales(self, epoch: int, is_master_node=False, *args, **kwargs) -> None:
"""Update the scales in variable batch sampler at specified epoch intervals during training."""
if (epoch in self.scale_ep_intervals) and self.scale_inc:
self.min_crop_size_w += int(
self.min_crop_size_w * self.min_scale_inc_factor
)
self.max_crop_size_w += int(
self.max_crop_size_w * self.max_scale_inc_factor
)
self.min_crop_size_h += int(
self.min_crop_size_h * self.min_scale_inc_factor
)
self.max_crop_size_h += int(
self.max_crop_size_h * self.max_scale_inc_factor
)
self.img_batch_tuples = image_batch_pairs(
crop_size_h=self.crop_size_h,
crop_size_w=self.crop_size_w,
batch_size_gpu0=self.batch_size_gpu0,
n_gpus=self.num_replicas,
max_scales=self.max_img_scales,
check_scale_div_factor=self.check_scale_div_factor,
min_crop_size_w=self.min_crop_size_w,
max_crop_size_w=self.max_crop_size_w,
min_crop_size_h=self.min_crop_size_h,
max_crop_size_h=self.max_crop_size_h,
)
if is_master_node:
logger.log("Scales updated in {}".format(self.__class__.__name__))
logger.log("New scales: {}".format(self.img_batch_tuples))
def extra_repr(self) -> str:
extra_repr_str = super().extra_repr()
extra_repr_str += (
f"\n\t base_im_size=(h={self.crop_size_h}, w={self.crop_size_w})"
f"\n\t base_batch_size={self.batch_size_gpu0}"
f"\n\t scales={self.img_batch_tuples}"
f"\n\t scale_inc={self.scale_inc}"
f"\n\t min_scale_inc_factor={self.min_scale_inc_factor}"
f"\n\t max_scale_inc_factor={self.max_scale_inc_factor}"
f"\n\t ep_intervals={self.scale_ep_intervals}"
)
return extra_repr_str