-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathDataGenerator.py
384 lines (308 loc) · 12.5 KB
/
DataGenerator.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
379
380
381
382
383
384
import argparse
import torch
import torch.nn as nn
import torch.optim as optim
import torch.nn.functional as F
import os
import sys
import json
import random
from sklearn.model_selection import train_test_split
from DataHelpers import *
from DataMixer import *
from Dataset import *
from Layers import *
from Models import *
from Losses import *
from Metrics import *
from Utils import *
from PyFire import Trainer
from VisualizationsAndDemonstrations import *
def str_to_bool(value):
if value.lower() in {'false', 'f', '0', 'no', 'n'}:
return False
elif value.lower() in {'true', 't', '1', 'yes', 'y'}:
return True
raise ValueError(f'{value} is not a valid boolean value')
def generate_labeled_waveforms(animal, *args, **kwargs):
if animal == 'Macaque':
loader = LoadMacaqueData(os=kwargs['os'])
X, Y = loader.run(balance=kwargs['balance'])
elif animal == 'Dolphin':
loader = LoadDolphinData(os=kwargs['os'], n_individuals=kwargs['n_individuals'])
X, Y = loader.run()
elif animal == 'Bat':
loader = LoadBatData(os=kwargs['os'])
X, Y = loader.run(balance=kwargs['balance'])
elif animal == 'SpermWhale':
loader = LoadSpermWhaleData(os=kwargs['os'])
X, Y = loader.run(balance=kwargs['balance'])
return X, Y
def open_closed_split(X, Y, n_open=None, seed=42):
random.seed(seed)
ids = np.unique(Y).tolist()
if n_open is not None:
open_ids = random.sample(list(np.unique(Y)), n_open)
else:
open_ids = []
closed_ids = [id for id in ids if id not in open_ids]
X_closed = [x for x,y in zip(X, Y) if y in closed_ids]
Y_closed = [y for y in Y if y in closed_ids]
if n_open is not None:
X_open = [x for x,y in zip(X, Y) if y in open_ids]
Y_open = [y for y in Y if y in open_ids]
else:
X_open, Y_open = None, None
return X_closed, Y_closed, X_open, Y_open
def generate_classifier_data(X, Y, *args, **kwargs):
X_train, X_test, Y_train, Y_test = train_test_split(X, Y,
test_size=0.2,
random_state=42)
aug_factor = kwargs['augmentation_factor']
if aug_factor is not None:
X_train, Y_train = augmenter(X_train, Y_train,
augmentation_factor=aug_factor,
shift_factor=kwargs['shift_factor'],
pad=kwargs['padding_scheme'],
side=kwargs['padding_side'],
shuffle=True)
X_test, Y_test = augmenter(X_test, Y_test,
augmentation_factor=aug_factor,
shift_factor=kwargs['shift_factor'],
pad=kwargs['padding_scheme'],
side=kwargs['padding_side'],
shuffle=True)
X_train = torch.Tensor(X_train)
X_test = torch.Tensor(X_test)
Y_train = torch.LongTensor(Y_train)
Y_test = torch.LongTensor(Y_test)
return X_train, X_test, Y_train, Y_test
def generate_mixture_data(X, Y, *args, **kwargs):
mixer = SourceMixer(n_src=kwargs['n_src'],
samples=kwargs['n_samples'],
frames=X[0].shape[-1])
x_mix, y_mix, y_mix_id = mixer.mix(X,
Y,
shift_factor=kwargs['shift_factor'],
shift_overlaps=kwargs['shift_overlaps'],
pad=kwargs['padding_scheme'],
side=kwargs['padding_side'])
X = None
Y = None
del X
del Y
x_mix = torch.stack(x_mix, dim=0)
y_mix = torch.stack(y_mix, dim=0)
y_mix_id = torch.stack(y_mix_id, dim=0)
return x_mix, y_mix, y_mix_id
def train_val_split(X, Y, seed, *args, **kwargs):
X_train, X_test, Y_train, Y_test = train_test_split(X, Y,
test_size=0.2,
random_state=seed)
aug_factor = kwargs['augmentation_factor']
if aug_factor is not None:
X_train, Y_train = augmenter(X_train, Y_train,
augmentation_factor=aug_factor,
shift_factor=kwargs['shift_factor'],
pad=kwargs['padding_scheme'],
side=kwargs['padding_side'],
shuffle=True)
X_test, Y_test = augmenter(X_test, Y_test,
augmentation_factor=aug_factor,
shift_factor=kwargs['shift_factor'],
pad=kwargs['padding_scheme'],
side=kwargs['padding_side'],
shuffle=True)
return X_train, X_test, Y_train, Y_test
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser = argparse.ArgumentParser()
parser.add_argument('--os', type=str,
help='specify Windows or Ubuntu',
default='Ubuntu')
parser.add_argument('--data_directory', type=str,
help='root directory for data files, e.g. Data')
parser.add_argument('--animal', type=str,
help='specify the animal to analyze')
parser.add_argument('--config', type=str,
help='config.json file name')
parser.add_argument('--objective', type=str,
help='objective task, e.g. Classification or Separation')
parser.add_argument('--regime', type=str,
help='open or closed regime, e.g. Open or Closed')
parser.add_argument('--seed', type=int,
default=42)
args = parser.parse_args()
animal = args.animal
assert animal in ['Macaque', 'Dolphin', 'Bat', 'SpermWhale']
if not os.path.isdir(animal):
os.mkdir(animal)
with open(animal + r'/' + args.config) as f:
data = f.read()
config = json.loads(data)
global preprocessing_config
preprocessing_config = config['data_preprocessing']
general_preprocessing = preprocessing_config['general']
classifier_preprocessing = preprocessing_config['classifier']
separator_preprocessing = preprocessing_config['separator']
n_src=general_preprocessing['n_src']
root = args.data_directory
if root[-1] != r'/':
root += r'/'
root = animal + '/' + root
if not os.path.isdir(root):
os.mkdir(root)
random.seed(args.seed)
np.random.seed(args.seed)
task = args.objective
assert task in ['Classification', 'Separation']
regime = args.regime
assert regime in ['Open', 'Closed']
if not os.path.isdir(root+'Waveforms'):
waveforms_dir = 'Waveforms/'
os.mkdir(root + waveforms_dir)
X, Y = generate_labeled_waveforms(animal,
os=args.os,
balance=general_preprocessing['balance'],
n_individuals=general_preprocessing['n_individuals'])
X_closed, Y_closed, X_open, Y_open = open_closed_split(X, Y,
n_open=general_preprocessing['n_open'],
seed=args.seed)
np.save(root+f'{waveforms_dir}X_closed.npy', X_closed)
np.save(root+f'{waveforms_dir}Y_closed.npy', Y_closed)
if X_open is not None:
np.save(root+f'{waveforms_dir}X_open.npy', X_open)
np.save(root+f'{waveforms_dir}Y_open.npy', Y_open)
else:
X_closed = [row for row in np.load(root+'Waveforms/X_closed.npy')]
Y_closed = [y for y in np.load(root+'Waveforms/Y_closed.npy')]
try:
X_open = [row for row in np.load(root+'Waveforms/X_open.npy')]
Y_open = [y for y in np.load(root+'Waveforms/Y_open.npy')]
except FileNotFoundError:
assert regime=='Closed', print('Open regime data cannot be found')
zipfile_save = general_preprocessing['zipfile_save']
if task == 'Classification':
X_open, Y_open = None, None
del X_open
del Y_open
X_train, X_test, Y_train, Y_test = train_val_split(X_closed, Y_closed, seed=0,
augmentation_factor=classifier_preprocessing['augmentation_factor'],
shift_factor=classifier_preprocessing['shift_factor'],
padding_scheme=classifier_preprocessing['padding_scheme'],
padding_side=classifier_preprocessing['padding_side'])
X_train = torch.Tensor(X_train)
X_test = torch.Tensor(X_test)
Y_train = torch.LongTensor(Y_train)
Y_test = torch.LongTensor(Y_test)
classifier_root = root + 'Classifier/'
if not os.path.isdir(classifier_root):
os.mkdir(classifier_root)
torch.save(X_train.clone().detach(), classifier_root+'X_train.pt', _use_new_zipfile_serialization=zipfile_save)
torch.save(Y_train.clone().detach(), classifier_root+'Y_train.pt', _use_new_zipfile_serialization=zipfile_save)
torch.save(X_test.clone().detach(), classifier_root+'X_test.pt', _use_new_zipfile_serialization=zipfile_save)
torch.save(Y_test.clone().detach(), classifier_root+'Y_test.pt', _use_new_zipfile_serialization=zipfile_save)
X_train, Y_train, X_test, Y_test = None, None, None, None
del X_train
del Y_train
del X_test
del Y_test
else:
if regime == 'Closed':
X_open, Y_open = None, None
del X_open
del Y_open
X_train, X_test, Y_train, Y_test = train_val_split(X_closed, Y_closed, seed=args.seed,
augmentation_factor=None)
X_closed, Y_closed = None, None
del X_closed
del Y_closed
if separator_preprocessing['mixing_to_memory'] is not None:
mixing_params = separator_preprocessing['mixing_to_memory']
X_train, Y_train, Y_train_id = generate_mixture_data(X_train, Y_train,
n_src=mixing_params['n_src'],
n_samples=mixing_params['training_size'],
shift_factor=mixing_params['shift_factor'],
shift_overlaps=mixing_params['shift_overlaps'],
padding_scheme=mixing_params['padding_scheme'],
padding_side=mixing_params['padding_side'])
task_directory = f'SeparatorClosed{n_src}Speakers/'
task_root = root + task_directory
if not os.path.isdir(task_root):
os.mkdir(task_root)
torch.save(X_train, task_root + 'X_train.pt')
torch.save(Y_train, task_root + 'Y_train.pt')
torch.save(Y_train_id, task_root + 'Y_train_id.pt')
X_train, Y_train, Y_train_id = None, None, None
del X_train
del Y_train
del Y_train_id
X_test, Y_test, Y_test_id = generate_mixture_data(X_test, Y_test,
n_src=mixing_params['n_src'],
n_samples=mixing_params['validation_size'],
shift_factor=mixing_params['shift_factor'],
shift_overlaps=mixing_params['shift_overlaps'],
padding_scheme=mixing_params['padding_scheme'],
padding_side=mixing_params['padding_side'])
torch.save(X_test, task_root + 'X_test.pt')
torch.save(Y_test, task_root + 'Y_test.pt')
torch.save(Y_test_id, task_root + 'Y_test_id.pt')
X_test, Y_test, Y_test_id = None, None, None
del X_test
del Y_test
del Y_test_id
else:
task_directory = f'SeparatorClosed/'
task_root = root + task_directory
if not os.path.isdir(task_root):
os.mkdir(task_root)
X_train = torch.Tensor(X_train)
X_test = torch.Tensor(X_test)
Y_train = torch.LongTensor(Y_train)
Y_test = torch.LongTensor(Y_test)
torch.save(X_train.clone().detach(), task_root+'X_train.pt', _use_new_zipfile_serialization=zipfile_save)
torch.save(Y_train.clone().detach(), task_root+'Y_train.pt', _use_new_zipfile_serialization=zipfile_save)
torch.save(X_test.clone().detach(), task_root+'X_test.pt', _use_new_zipfile_serialization=zipfile_save)
torch.save(Y_test.clone().detach(), task_root+'Y_test.pt', _use_new_zipfile_serialization=zipfile_save)
X_train, Y_train, X_test, Y_test = None, None, None, None
del X_train
del Y_train
del X_test
del Y_test
elif regime == 'Open':
assert general_preprocessing['n_open'], print('The number of IDs to hold out must be specified in the config JSON.')
X_closed, Y_closed = None, None
del X_closed
del Y_closed
if separator_preprocessing['mixing_to_memory'] is not None:
mixing_params = separator_preprocessing['mixing_to_memory']
X_test, Y_test, Y_test_id = generate_mixture_data(X_test, Y_test,
n_src=mixing_params['n_src'],
n_samples=mixing_params['validation_size'],
shift_factor=mixing_params['shift_factor'],
shift_overlaps=mixing_params['shift_overlaps'],
padding_scheme=mixing_params['padding_scheme'],
padding_side=mixing_params['padding_side'])
task_directory = f'SeparatorOpen{n_src}Speakers/'
task_root = root + task_directory
if not os.path.isdir(task_root):
os.mkdir(task_root)
torch.save(X_test, task_root + 'X_test.pt')
torch.save(Y_test, task_root + 'Y_test.pt')
torch.save(Y_test_id, task_root + 'Y_test_id.pt')
X_test, Y_test, Y_test_id = None, None, None
del X_test
del Y_test
del Y_test_id
else:
task_directory = f'SeparatorOpen/'
task_root = root + task_directory
if not os.path.isdir(task_root):
os.mkdir(task_root)
X_test = torch.Tensor(X_open)
Y_test = torch.LongTensor(Y_open)
torch.save(X_test.clone().detach(), task_root+'X_test.pt', _use_new_zipfile_serialization=zipfile_save)
torch.save(Y_test.clone().detach(), task_root+'Y_test.pt', _use_new_zipfile_serialization=zipfile_save)
X_test, Y_test, Y_test_id = None, None, None
del X_test
del Y_test