-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdyspreprocess.py
180 lines (147 loc) · 5.73 KB
/
dyspreprocess.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
#import tensorflow as tf
import librosa
import os
import numpy as np
from tqdm import tqdm
DATA_PATH = "../DeadSimpleSpeechRecognizor_Data/"
DYData_Path = "../DysSpeech_Corpora/Linzy/linzyCut/train/"
DYNPY_PATH = "./mfcc"
DysTrainedModelPath = "trained_models"
# Input: Folder Path
# Output: Tuple (Label, Indices of the labels, one-hot encoded labels)
def get_labels(path=DATA_PATH):
labels = os.listdir(path)
label_indices = np.arange(0, len(labels))
return labels, label_indices, to_categorical(label_indices)
# Handy function to convert wav2mfcc
def wav2mfcc(file_path, max_len=11):
wave, sr = librosa.load(file_path, mono=True, sr=None)
wave = wave[::3]
mfcc = librosa.feature.mfcc(wave, sr=16000)
# If maximum length exceeds mfcc lengths then pad the remaining ones
if (max_len > mfcc.shape[1]):
pad_width = max_len - mfcc.shape[1]
mfcc = np.pad(mfcc, pad_width=((0, 0), (0, pad_width)), mode='constant')
# Else cutoff the remaining parts
else:
mfcc = mfcc[:, :max_len]
return mfcc
def save_data_to_array(path=DATA_PATH, max_len=11):
labels, _, _ = get_labels(path)
print("labels are :",labels)
for label in labels:
# Init mfcc vectors
mfcc_vectors = []
wavfiles = [path + label + '/' + wavfile for wavfile in os.listdir(path + '/' + label)]
for wavfile in tqdm(wavfiles, "Saving vectors of label - '{}'".format(label)):
mfcc = wav2mfcc(wavfile, max_len=max_len)
mfcc_vectors.append(mfcc)
print("length of mfcc_vectors is : ",len(mfcc))
np.save(label + '.npy', mfcc_vectors)
'''
def get_train_test(split_ratio=0.6, random_state=42):
# Get available labels
labels, indices, _ = get_labels(DATA_PATH)
# Getting first arrays
X = np.load(labels[0] + '.npy')
y = np.zeros(X.shape[0])
# Append all of the dataset into one single array, same goes for y
for i, label in enumerate(labels[1:]):
x = np.load(label + '.npy')
X = np.vstack((X, x))
y = np.append(y, np.full(x.shape[0], fill_value= (i + 1)))
assert X.shape[0] == len(y)
return train_test_split(X, y, test_size= (1 - split_ratio), random_state=random_state, shuffle=True)
'''
def prepare_dataset(path=DATA_PATH):
labels, _, _ = get_labels(path)
data = {}
for label in labels:
data[label] = {}
data[label]['path'] = [path + label + '/' + wavfile for wavfile in os.listdir(path + '/' + label)]
vectors = []
for wavfile in data[label]['path']:
wave, sr = librosa.load(wavfile, mono=True, sr=None)
# Downsampling
wave = wave[::3]
mfcc = librosa.feature.mfcc(wave, sr=16000)
vectors.append(mfcc)
data[label]['mfcc'] = vectors
return data
def load_dataset(path=DATA_PATH):
data = prepare_dataset(path)
dataset = []
for key in data:
for mfcc in data[key]['mfcc']:
dataset.append((key, mfcc))
return dataset[:100]
def get_train_set():
labels, indices, _ = get_labels(DYNPY_PATH)
# Getting first arrays
print("labels[0].npy is {} ".format(labels[0]))
print("labels[1:] : {}".format(labels[1:]))
X = np.load(os.path.join(DYNPY_PATH,labels[0]))
y = np.zeros(X.shape[0])
#print("X.shape is {}".format(X.shape))
for i, label in enumerate(labels[1:]):
x = np.load(os.path.join(DYNPY_PATH, label))
X = np.vstack((X, x))
y = np.append(y, np.full(x.shape[0], fill_value=(i + 1)))
assert X.shape[0] == len(y)
print("X.shape[0] is {}".format(X.shape))
print("length y is {}".format(len(y)))
#return train_test_split(X, y, test_size=(1 - split_ratio), random_state=random_state, shuffle=True)
return (X,y)
#import SpeechModels
import keras
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPooling2D
from keras.utils import to_categorical
#from keras.optimizers import adadelta
num_of_classes = 19
feature_dim_1 = 20
feature_dim_2 = 11
channel = 1
def run_main():
X_train, y_train = get_train_set()
#reshape X_train
X_train = X_train.reshape(X_train.shape[0],20,11,1)
print("X_train shape is {}".format(X_train.shape))
y_train_hot = to_categorical(y_train)
print(y_train)
print("Start to train.......")
#adadelta = adadelta(lr=0.000001)
'''
model = Sequential()
model.add(Conv2D(32, kernel_size=(2, 2), activation='relu',
input_shape=(feature_dim_1, feature_dim_2, channel)))
model.add(Conv2D(48, kernel_size=(2, 2), activation='relu'))
model.add(Conv2D(120, kernel_size=(2, 2), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.25))
model.add(Dense(64, activation='relu'))
model.add(Dropout(0.4))
model.add(Dense(num_of_classes, activation='softmax'))
'''
model = Sequential()
model.add(Conv2D(32, kernel_size=(2, 2),
activation='relu', input_shape=(20, 11, 1)))
model.add(MaxPooling2D(pool_size=(2, 2)))
#model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(128, activation='relu'))
#model.add(Dropout(0.25))
model.add(Dense(num_of_classes, activation='softmax'))
model.compile(loss=keras.losses.categorical_crossentropy,
optimizer=keras.optimizers.adadelta(lr=0.000000001),
metrics=['accuracy'])
model.fit(X_train, y_train_hot, batch_size=64, epochs=300)
print("Saving Model.........")
#model.save('AttRNN_model.h5')
model.save(os.path.join('.', DysTrainedModelPath, 'CNN2_model.h5'))
# print(prepare_dataset(DATA_PATH))
if __name__ == "__main__":
run_main()