-
Notifications
You must be signed in to change notification settings - Fork 516
/
batch_sampler.py
147 lines (125 loc) · 4.77 KB
/
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
#
# For licensing see accompanying LICENSE file.
# Copyright (C) 2024 Apple Inc. All Rights Reserved.
#
import argparse
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
@SAMPLER_REGISTRY.register(name="batch_sampler")
class BatchSampler(BaseSampler):
"""Standard Batch Sampler for data parallel. This sampler yields batches of fixed batch size
and spatial resolutions.
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,
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
)
# spatial dimensions
crop_size_w: int = getattr(opts, "sampler.bs.crop_size_width")
crop_size_h: int = getattr(opts, "sampler.bs.crop_size_height")
self.crop_size_w = crop_size_w
self.crop_size_h = crop_size_h
def __iter__(self) -> Iterator[Tuple[int, int, int]]:
img_indices = self.get_indices()
start_index = 0
batch_size = self.batch_size_gpu0
n_samples = len(img_indices)
while start_index < n_samples:
end_index = min(start_index + batch_size, n_samples)
batch_ids = img_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) for b_id in batch_ids
]
yield batch
def extra_repr(self) -> str:
extra_repr_str = super().extra_repr()
extra_repr_str += (
f"\n\tbase_im_size=(h={self.crop_size_h}, w={self.crop_size_w})"
f"\n\tbase_batch_size={self.batch_size_gpu0}"
)
return extra_repr_str
@classmethod
def add_arguments(cls, parser: argparse.ArgumentParser) -> argparse.ArgumentParser:
if cls != BatchSampler:
# 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.crop-size-width",
default=DEFAULT_IMAGE_WIDTH,
type=int,
help="Base crop size (along width) during training",
)
group.add_argument(
"--sampler.bs.crop-size-height",
default=DEFAULT_IMAGE_HEIGHT,
type=int,
help="Base crop size (along height) during training",
)
return parser
@SAMPLER_REGISTRY.register(name="batch_sampler_ddp")
class BatchSamplerDDP(BaseSamplerDDP):
"""DDP variant of BatchSampler
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,
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: int = getattr(
opts, "sampler.bs.crop_size_width", DEFAULT_IMAGE_WIDTH
)
crop_size_h: int = getattr(
opts, "sampler.bs.crop_size_height", DEFAULT_IMAGE_HEIGHT
)
self.crop_size_w = crop_size_w
self.crop_size_h = crop_size_h
def __iter__(self) -> Iterator[Tuple[int, int, int]]:
indices_rank_i = self.get_indices_rank_i()
start_index = 0
batch_size = self.batch_size_gpu0
n_samples_rank_i = len(indices_rank_i)
while start_index < n_samples_rank_i:
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 = [
(self.crop_size_h, self.crop_size_w, b_id) for b_id in batch_ids
]
yield batch
def extra_repr(self) -> str:
extra_repr_str = super().extra_repr()
extra_repr_str += (
f"\n\tbase_im_size=(h={self.crop_size_h}, w={self.crop_size_w})"
f"\n\tbase_batch_size={self.batch_size_gpu0}"
)
return extra_repr_str