-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsampler.py
227 lines (175 loc) · 7.8 KB
/
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
import numpy as np
from torch.utils.data.sampler import (
BatchSampler,
WeightedRandomSampler,
)
class SamplerFactory:
"""
Factory class to create balanced samplers.
"""
def get(self, class_idxs, batch_size, n_batches, alpha, kind):
"""
Parameters
----------
class_idxs : 2D list of ints
List of sample indices for each class. Eg. [[0, 1], [2, 3]] implies indices 0, 1
belong to class 0, and indices 2, 3 belong to class 1.
batch_size : int
The batch size to use.
n_batches : int
The number of batches per epoch.
alpha : numeric in range [0, 1]
Weighting term used to determine weights of each class in each batch.
When `alpha` == 0, the batch class distribution will approximate the training population
class distribution.
When `alpha` == 1, the batch class distribution will approximate a uniform distribution,
with equal number of samples from each class.
kind : str ['fixed' | 'random']
The kind of sampler. `Fixed` will ensure each batch contains a constant proportion of
samples from each class. `Random` will simply sample with replacement according to the
calculated weights.
"""
if kind == "random":
return self.random(class_idxs, batch_size, n_batches, alpha)
if kind == "fixed":
return self.fixed(class_idxs, batch_size, n_batches, alpha)
raise Exception(f"Received kind {kind}, must be `random` or `fixed`")
def random(self, class_idxs, batch_size, n_batches, alpha):
print(f"Creating `{WeightedRandomBatchSampler.__name__}`...")
class_sizes, weights = self._weight_classes(class_idxs, alpha)
sample_rates = self._sample_rates(weights, class_sizes)
return WeightedRandomBatchSampler(
sample_rates, class_idxs, batch_size, n_batches
)
def fixed(self, class_idxs, batch_size, n_batches, alpha):
print(f"Creating `{WeightedFixedBatchSampler.__name__}`...")
class_sizes, weights = self._weight_classes(class_idxs, alpha)
class_samples_per_batch = self._fix_batches(
weights, class_sizes, batch_size, n_batches
)
return WeightedFixedBatchSampler(class_samples_per_batch, class_idxs, n_batches)
def _weight_classes(self, class_idxs, alpha):
class_sizes = np.asarray([len(idxs) for idxs in class_idxs])
n_samples = class_sizes.sum()
n_classes = len(class_idxs)
original_weights = np.asarray([size / n_samples for size in class_sizes])
uniform_weights = np.repeat(1 / n_classes, n_classes)
print(f"Sample population absolute class sizes: {class_sizes}")
print(f"Sample population relative class sizes: {original_weights}")
weights = self._balance_weights(uniform_weights, original_weights, alpha)
return class_sizes, weights
def _balance_weights(self, weight_a, weight_b, alpha):
assert (
alpha >= 0 and alpha <= 1
), f"invalid alpha {alpha}, must be 0 <= alpha <= 1"
beta = 1 - alpha
weights = (alpha * weight_a) + (beta * weight_b)
print(f"Target batch class distribution {weights} using alpha={alpha}")
return weights
def _sample_rates(self, weights, class_sizes):
return weights / class_sizes
def _fix_batches(self, weights, class_sizes, batch_size, n_batches):
"""
Calculates the number of samples of each class to include in each batch, and the number
of batches required to use all the data in an epoch.
"""
class_samples_per_batch = np.round((weights * batch_size)).astype(int)
# cleanup rounding edge-cases
remainder = batch_size - class_samples_per_batch.sum()
largest_class = np.argmax(class_samples_per_batch)
class_samples_per_batch[largest_class] += remainder
assert class_samples_per_batch.sum() == batch_size
proportions_of_class_per_batch = class_samples_per_batch / batch_size
print(f"Rounded batch class distribution {proportions_of_class_per_batch}")
proportions_of_samples_per_batch = class_samples_per_batch / class_sizes
print(
f"Expecting {class_samples_per_batch} samples of each class per batch, "
f"over {n_batches} batches of size {batch_size}"
)
oversample_rates = proportions_of_samples_per_batch * n_batches
print(f"Sampling rates: {oversample_rates}")
return class_samples_per_batch
class WeightedRandomBatchSampler(BatchSampler):
"""
Samples with replacement according to the provided weights.
Parameters
----------
class_weights : `numpy.array(int)`
The number of samples of each class to include in each batch.
class_idxs : 2D list of ints
The indices that correspond to samples of each class.
batch_size : int
The size of each batch yielded.
n_batches : int
The number of batches to yield.
"""
def __init__(self, class_weights, class_idxs, batch_size, n_batches):
self.sample_idxs = []
for idxs in class_idxs:
self.sample_idxs.extend(idxs)
sample_weights = []
for c, weight in enumerate(class_weights):
sample_weights.extend([weight] * len(class_idxs[c]))
self.sampler = WeightedRandomSampler(
sample_weights, batch_size, replacement=True
)
self.n_batches = n_batches
def __iter__(self):
for bidx in range(self.n_batches):
selected = []
for idx in self.sampler:
selected.append(self.sample_idxs[idx])
yield selected
def __len__(self):
return self.n_batches
class WeightedFixedBatchSampler(BatchSampler):
"""
Ensures each batch contains a given class distribution.
The lists of indices for each class are shuffled at the start of each call to `__iter__`.
Parameters
----------
class_samples_per_batch : `numpy.array(int)`
The number of samples of each class to include in each batch.
class_idxs : 2D list of ints
The indices that correspond to samples of each class.
n_batches : int
The number of batches to yield.
"""
def __init__(self, class_samples_per_batch, class_idxs, n_batches):
self.class_samples_per_batch = class_samples_per_batch
self.class_idxs = [CircularList(idx) for idx in class_idxs]
self.n_batches = n_batches
self.n_classes = len(self.class_samples_per_batch)
self.batch_size = self.class_samples_per_batch.sum()
assert len(self.class_samples_per_batch) == len(self.class_idxs)
assert isinstance(self.n_batches, int)
def _get_batch(self, start_idxs):
selected = []
for c, size in enumerate(self.class_samples_per_batch):
selected.extend(
self.class_idxs[c][start_idxs[c] : start_idxs[c] + size]
) # noqa
np.random.shuffle(selected)
return selected
def __iter__(self):
[cidx.shuffle() for cidx in self.class_idxs]
start_idxs = np.zeros(self.n_classes, dtype=int)
for bidx in range(self.n_batches):
yield self._get_batch(start_idxs)
start_idxs += self.class_samples_per_batch
def __len__(self):
return self.n_batches
class CircularList:
"""
Applies modulo function to indexing.
"""
def __init__(self, items):
self._items = items
self._mod = len(self._items)
self.shuffle()
def shuffle(self):
np.random.shuffle(self._items)
def __getitem__(self, key):
if isinstance(key, slice):
return [self[i] for i in range(key.start, key.stop)]
return self._items[key % self._mod]