-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathDataProvider.py
346 lines (268 loc) · 11.2 KB
/
DataProvider.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
import numpy as np
class DoubleSourceSlider(object):
def __init__(self, batchsize, shuffle, offset):
self.batchsize = batchsize
self.shuffle = shuffle
self.offset = offset
def feed(self, inputs, targets, flatten=True):
inputs, targets = inputs.flatten(), targets.flatten()
assert inputs.size == targets.size
max_batchsize = inputs.size - 2 * self.offset
if self.batchsize < 0:
self.batchsize = max_batchsize
indices = np.arange(max_batchsize)
if self.shuffle:
np.random.shuffle(indices)
for start_idx in range(0, max_batchsize, self.batchsize):
excerpt = indices[start_idx:start_idx + self.batchsize]
if flatten:
yield np.array([inputs[idx:idx + 2 * self.offset + 1] for idx in excerpt]), \
targets[excerpt + self.offset]
else:
yield np.array([inputs[idx:idx + 2 * self.offset + 1] for idx in excerpt]), \
targets[excerpt + self.offset].reshape(-1, 1)
def generate_test_data(self, inputs, targets, targets_gt, offset, flatten=True):
shuffle = False
inputs, targets = inputs.flatten(), targets.flatten()
assert inputs.size == targets.size
max_batchsize = inputs.size - 2 * offset
batchsize = max_batchsize
#if self.batchsize < 0:
# self.batchsize = max_batchsize
indices = np.arange(max_batchsize)
if shuffle:
np.random.shuffle(indices)
for start_idx in range(0, max_batchsize, batchsize):
excerpt = indices[start_idx:start_idx + batchsize]
if flatten:
yield np.array([inputs[idx:idx + 2 * offset + 1] for idx in excerpt]), \
targets[excerpt + offset], \
targets_gt[excerpt + offset]
else:
yield np.array([inputs[idx:idx + 2 * offset + 1] for idx in excerpt]), \
targets[excerpt + offset].reshape(-1, 1), \
targets_gt[excerpt + offset].reshape(-1, 1)
class DoubleSourceSlider(object):
def __init__(self, batchsize, shuffle, offset):
self.batchsize = batchsize
self.shuffle = shuffle
self.offset = offset
def feed(self, inputs, targets, flatten=True):
inputs, targets = inputs.flatten(), targets.flatten()
assert inputs.size == targets.size
max_batchsize = inputs.size - 2 * self.offset
if self.batchsize < 0:
self.batchsize = max_batchsize
indices = np.arange(max_batchsize)
if self.shuffle:
np.random.shuffle(indices)
for start_idx in range(0, max_batchsize, self.batchsize):
excerpt = indices[start_idx:start_idx + self.batchsize]
if flatten:
yield np.array([inputs[idx:idx + 2 * self.offset + 1] for idx in excerpt]), \
targets[excerpt + self.offset]
else:
yield np.array([inputs[idx:idx + 2 * self.offset + 1] for idx in excerpt]), \
targets[excerpt + self.offset].reshape(-1, 1)
class S2S_Slider(object):
def __init__(self, batchsize, shuffle, length):
self.batchsize = batchsize
self.shuffle = shuffle
self.length = length
def feed(self, inputs, targets, flatten=True):
inputs, targets = inputs.flatten(), targets.flatten()
assert inputs.size == targets.size
max_batchsize = inputs.size - self.length
if self.batchsize < 0:
self.batchsize = max_batchsize
indices = np.arange(max_batchsize)
if self.shuffle:
np.random.shuffle(indices)
for start_idx in range(0, max_batchsize, self.batchsize):
excerpt = indices[start_idx:start_idx + self.batchsize]
yield np.array([inputs[idx:idx + self.length] for idx in excerpt]), \
np.array([targets[idx:idx + self.length] for idx in excerpt])
# def generate_test_data(self, inputs, targets, targets_gt, offset, flatten=True):
#
# shuffle = False
# inputs, targets = inputs.flatten(), targets.flatten()
# assert inputs.size == targets.size
# max_batchsize = inputs.size - 2 * offset
# batchsize = max_batchsize
# # if self.batchsize < 0:
# # self.batchsize = max_batchsize
#
# indices = np.arange(max_batchsize)
# if shuffle:
# np.random.shuffle(indices)
#
# for start_idx in range(0, max_batchsize, batchsize):
# excerpt = indices[start_idx:start_idx + batchsize]
# if flatten:
# yield np.array([inputs[idx:idx + 2 * offset + 1] for idx in excerpt]), \
# targets[excerpt + offset], \
# targets_gt[excerpt + offset]
# else:
# yield np.array([inputs[idx:idx + 2 * offset + 1] for idx in excerpt]), \
# targets[excerpt + offset].reshape(-1, 1), \
# targets_gt[excerpt + offset].reshape(-1, 1)
class MultiApp_Slider(object):
def __init__(self, batchsize, shuffle, offset):
self.batchsize = batchsize
self.shuffle = shuffle
self.offset = offset
def feed(self, inputs, targets, flatten=True):
# inputs, targets = inputs.flatten(), targets.flatten()
inputs = inputs.flatten()
assert inputs.shape[0] == targets.shape[0]
max_batchsize = inputs.size - 2 * self.offset
if self.batchsize < 0:
self.batchsize = max_batchsize
indices = np.arange(max_batchsize)
if self.shuffle:
np.random.shuffle(indices)
for start_idx in range(0, max_batchsize, self.batchsize):
excerpt = indices[start_idx:start_idx + self.batchsize]
# if flatten:
# yield np.array([inputs[idx:idx + 2 * self.offset + 1] for idx in excerpt]), \
# targets[excerpt + self.offset]
# else:
yield np.array([inputs[idx:idx + 2 * self.offset + 1] for idx in excerpt]), \
targets[excerpt + self.offset, :]
# def generate_test_data(self, inputs, targets, targets_gt, offset, flatten=True):
#
# shuffle = False
# inputs, targets = inputs.flatten(), targets.flatten()
# assert inputs.size == targets.size
# max_batchsize = inputs.size - inputs.size - self.length
# batchsize = max_batchsize
# # if self.batchsize < 0:
# # self.batchsize = max_batchsize
#
# indices = np.arange(max_batchsize)
# if shuffle:
# np.random.shuffle(indices)
#
# for start_idx in range(0, max_batchsize, batchsize):
# excerpt = indices[start_idx:start_idx + batchsize]
# if flatten:
# yield np.array([inputs[idx:idx + 2 * offset + 1] for idx in excerpt]), \
# targets[excerpt + offset], \
# targets_gt[excerpt + offset]
# else:
# yield np.array([inputs[idx:idx + 2 * offset + 1] for idx in excerpt]), \
# targets[excerpt + offset].reshape(-1, 1), \
# targets_gt[excerpt + offset].reshape(-1, 1)
class DoubleSourceProvider(object):
def __init__(self, batchsize, shuffle):
self.batchsize = batchsize
self.shuffle = shuffle
def feed(self, inputs, targets):
assert len(inputs) == len(targets)
if self.batchsize == -1:
self.batchsize = len(inputs)
if self.shuffle:
indices = np.arange(len(inputs))
np.random.shuffle(indices)
for start_idx in range(0, len(inputs) - self.batchsize + 1, self.batchsize):
if self.shuffle:
excerpt = indices[start_idx:start_idx + self.batchsize]
else:
excerpt = slice(start_idx, start_idx + self.batchsize)
yield inputs[excerpt], targets[excerpt]
class Transformer(object):
def __init__(self, mu, norm):
self.mu = mu
self.norm = norm
def MuLawQuantisation(self, data, quantization=True):
"""
Perform the mu-law transformation
------------------------------------------
:arg
data: data that needs to be transform
mu: scale
norm: normalisation constant
quantization: quantize to integral, default True
:return
The transformed data
"""
data = data.flatten()
data = data/self.norm
mu_law = np.sign(data)*(np.log(1+self.mu*np.abs(data))/np.log(1+self.mu))*self.mu
if quantization:
return np.round(mu_law)
else:
return mu_law
def InverseMuLaw(self, data, sample=False):
"""
Perform the inverse mu-law transformation
--------------------------------------------
:arg
data: data that needs to be inverse-transformed
mu: scale
norm: normalisation constant
:return
The inverse transformed data
"""
if sample:
means = data.flatten()
cov = np.eye(data.size)*sample
data = np.random.multivariate_normal(means, cov)
self.mu = float(self.mu)
data /= self.mu
recover = np.sign(data)*(1/self.mu)*((1+self.mu)**np.abs(data)-1)
return recover*self.norm
def LinearQuantisation(self, data, quantization=True):
"""
Perform the linear quantisation.
--------------------------------------
:arg
data: data that needs to be transform
mu: scale
norm: normalisation constant
quantization: quantize to integral, default True
:return
The transformed data
"""
data = data.flatten()
gap = int(self.norm/self.mu)
if quantization:
return np.round(data/gap)
else:
return data/gap
def InverseLinear(self, data):
"""
Perform the inverse linear quantisation.
-------------------------------------
:arg
data: data that needs to be inverse-transformed
mu: scale
norm: normalisation constant
:return
The transformed data
"""
return data*int(self.norm/self.mu)
def Normalise(self, data):
"""
Perform the normalisation (data-mu)/norm.
--------------------------------------
:arg
data: data that needs to be transformed
mu: scale
norm: normlisation constant
:return
The normalized data
"""
return (data-self.mu)/self.norm
def InverseNormalise(self, data):
"""
Perform the in-normalisation data*norm+mu.
------------------------------------------------
:arg
data: data that needs to be inverse-transformed
mu: scale
norm: normalisation constant
:return
The in-normalized data
"""
return data*self.norm+self.mu