-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathutils.py
35 lines (28 loc) · 943 Bytes
/
utils.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
import numpy as np
class DataGenerator:
def __init__(self, inputs, shuffle=True, batch_size=32):
assert len(inputs) > 0
self.inputs = inputs
self.idx = np.arange(len(inputs[0]))
self.shuffle = shuffle
self.batch_size = batch_size
self.on_epoch_end()
def data_length(self):
return len(self.idx)
def __len__(self):
n = self.data_length()
len_ = n // self.batch_size
return len_ if n % self.batch_size == 0 else len_ + 1
def __getitem__(self, index):
start = index * self.batch_size
end = start + self.batch_size
index = self.idx[start:end]
data = []
for x in self.inputs:
data.append(x[start:end])
return data
def on_epoch_end(self):
if self.shuffle:
np.random.shuffle(self.idx)
def set_batch_size(self, batch_size):
self.batch_size = batch_size