-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_loader.py
More file actions
89 lines (74 loc) · 2.77 KB
/
data_loader.py
File metadata and controls
89 lines (74 loc) · 2.77 KB
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
from __future__ import absolute_import
import numpy as np
from scipy.io import wavfile
from os import listdir
from os.path import join
from sklearn.model_selection import StratifiedKFold
import random
from transform import *
from tqdm import tqdm
def is_wav_file(filename):
return any(filename.endswith(extension) for extension in [".wav"])
def load_wav_file(filename):
"""Loads an audio file and returns a float PCN-encoded array of samples."""
sample_rate, data = wavfile.read(filename)
return sample_rate, data
def load_data(wave_dir):
labels = listdir(wave_dir)
ulabel = -1
print(labels)
dataset = {'data':[], 'target':[]}
duration = []
count = 0
#DUE TO MEMORY ISSUE, RESTRICT THE NUMBER OF SAMPLES
for label in labels:
data_dir = join(wave_dir, label)
if label == 'carm':
ulabel = 0
elif label == 'traffic':
ulabel = 1
elif label == 'noisy':
ulabel = 2
elif label == 'tv':
ulabel = 3
else:
pass
if ulabel == -1:
#NO SUCH LABEL IN LABELS ERROR
continue
wav_filenames = listdir(data_dir)
for filename in tqdm(wav_filenames):
#RESTRICT MAX NUMBER
'''
if count == 6000:
count = 0
break
'''
filepath = join(data_dir, filename)
sample_rate, data = load_wav_file(filepath)
# SPECIFIED SAMPLING RATE (ALL IDENTICAL)
if sample_rate == 16000:
duration.append(data.shape[0])
dataset['data'].append(data)
dataset['target'].append(ulabel)
count += 1
#min_duration = min(duration)
max_duration = max(duration)
for i in tqdm(range (0, len(dataset['data']))):
tmp = np.zeros(max_duration)
tmp[:dataset['data'][i].shape[0]] = dataset['data'][i]
dataset['data'][i] = tmp
#dataset['data'][i] = dataset['data'][i][:min_duration]
dataset['data'][i] = mfcc(dataset['data'][i], 16000, normalization=1, logscale=1, delta=0)
dataset['data'][i] = dataset['data'][i].flatten()
return dataset
def train_and_test(dataset):
skf = StratifiedKFold(n_splits=5)
train_index, test_index = next(iter(skf.split(dataset['data'], dataset['target'])))
train_set = list(zip(np.asarray(dataset['data'])[train_index], np.asarray(dataset['target'])[list(train_index)]))
random.shuffle(train_set)
X_train = np.asarray([i for i, j in train_set])
Y_train = np.asarray([j for i, j in train_set])
X_test = np.asarray(dataset['data'])[test_index]
Y_test = np.asarray(dataset['target'])[test_index]
return X_train, Y_train, X_test, Y_test