-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain.py
291 lines (231 loc) · 9.41 KB
/
train.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
# -*- coding: utf-8 -*-
__version__ = '1.0'
__author__ = 'Wawrzyński Adam, Szypryt Kamil'
import os
import numpy as np
import random
from keras.models import load_model
from time import time
from keras.callbacks import TensorBoard
from keras import backend as K
import modules.models as md
import modules.audio_processing as ap
def decode_batch(result):
"""Returns indexes of the maximum probability for each phoneme prediction."""
out = result
ret = np.empty(1)
for j in range(out.shape[0]):
out_best = list(np.argmax(out[j,:], axis=1))
ret = np.append(ret, out_best)
return ret[1:]
def evaluate_predictions(y_true, y_pred):
"""Returns percent of correct predictions."""
counter = 0.0
for i in range(y_true.shape[0]):
if(y_true[i] == y_pred[i]):
counter = counter + 1
return (counter*100)/y_true.shape[0]
def create_transcription(prediction, path, window_width=25, verbose=False):
"""Prints start and end sound sample of phoneme occurance.
Example:\n
For sound file with 16000 sample per 1 second:\n
<PHONEME> 'sil'\n
<START_SAMPLE> 0\n
<END_SAMPLE> 160000\n
It meas that phoneme 'sil':
* starts with 0/16000 second (0 second) of sound file
* ends in 16000/16000 seconds (1 second) of sound file
"""
old = None
start = 0
stop = 0
with open(path, "w") as fout:
if verbose == True:
print("<PHONEME> <START_SAMPLE> <END_SAMPLE>")
fout.write("<PHONEME>,<START_SAMPLE>,<END_SAMPLE>\n")
for i in range(0, len(prediction)):
if old == prediction[i]:
stop += window_width
elif old is None:
old = prediction[i]
start = i*window_width
stop = start
else:
if verbose == True:
print("{}:\t{} sample\t{} sample".format(old, int(start), int(stop)))
fout.write("{},{},{}\n".format(old, int(start), int(stop)))
start = stop = 0
old = None
if verbose == True:
print("{}:\t{} sample\t{} sample".format(old, int(start), int(stop)))
fout.write("{},{},{}\n".format(old, int(start), int(stop)))
def train_model(name,
model,
model_weights_path,
test_func,
epochs,
alphabet_path,
dataset_path,
restore,
language,
tensorboard=False,
verbose=False):
"""Trains model and saves pretrained weights to file."""
# load alphabet and dataset from given paths
# load alphabet and dataset from given paths
if language == "polish":
dataset = ap.get_dataset_clarin(alphabet_path, dataset_path)
elif language == "english":
dataset = ap.get_dataset(alphabet_path, dataset_path)
else:
print("Lanugage {} is not supported.".format(language))
exit()
if not dataset:
print("Dataset is empty.")
print("Check your dataset path and selected language.")
exit()
# load model to retrain
if restore == True:
if os.path.isfile(model_weights_path):
model.load_weights(model_weights_path)
print("Model weights loaded from disk")
else:
print("Model weights not found")
# run tensorboard logger
tb = TensorBoard(log_dir="logs/" + name)
callbacks = None
if tensorboard == True:
callbacks = [tb]
# split dataset to training and testing
train_test_ratio=0.1
train_dataset = dataset[int(len(dataset)*train_test_ratio):]
test_dataset = dataset[0:int(len(dataset)*train_test_ratio)]
# get one sample of test dataset to validate model
# model expects input data with shape: [batch_size, timesteps, features]
# in our case it is 1 element of batch, variable length and 26 MFCC features
X_test = test_dataset[0].features
X_test = X_test.reshape(1, X_test.shape[0], X_test.shape[1])
y_test = test_dataset[0].phonemes
# create list of input lengths
# CTC loss function expects:
# input_length with shape: [batch_size, 1], with length of input sequence
# label_length with shape: [batch_size, 1], with length of output sequence
input_length = np.ones((1, 1))
label_length = np.ones((1, 1))
for i in range(0, epochs):
random.shuffle(train_dataset)
# dictionary is to store dataset, because of its variable length
for k in train_dataset:
x = k.features
y = k.phonemes
input_length[0][0] = x.shape[0]
label_length[0][0] = y.shape[0]
# reshape input data from [None, 26] to [batch_size, None, 26]
x = x.reshape(1, x.shape[0], x.shape[1])
# reshape output to [batch_size, expected_sequence_length]
y = y.reshape(y.shape[0], -1)
y = y.reshape(y.shape[1], y.shape[0])
# train model
# argument x is a tuple of elements necesary for CTC loss function
# argument y is dummy variable of shape [expected_sequence_length]
model.fit(x=[x, y, input_length, label_length],
y=np.zeros(x.shape[0]),
batch_size=1,
epochs=1,
callbacks=callbacks)
# predict sequence
# it returns matrix of occurance probability for each phoneme
result = test_func([X_test])
# now we choose the biggest probability for each timestep
out = decode_batch(result[0])
out = np.asarray(out)
# print results
if verbose == True:
print("Predicted: \t{}".format(out))
print("Actual: \t{}".format(y_test))
predictions = evaluate_predictions(y_test, out)
print("Correct predictions: {}%".format(round(predictions, 2)))
model.save_weights(model_weights_path)
print("Model weights saved to disk")
def evaluate_model(model,
model_weights_path,
test_func,
alphabet_path,
dataset_path,
language,
verbose=False):
"""Checks and prints accuracy of pretrained model on given dataset."""
# load alphabet and dataset from given paths
if language == "polish":
dataset = ap.get_dataset_clarin(alphabet_path, dataset_path)
elif language == "english":
dataset = ap.get_dataset(alphabet_path, dataset_path)
else:
print("Lanugage {} is not supported.".format(language))
exit()
if not dataset:
print("Dataset is empty.")
print("Check your dataset path and selected language.")
exit()
phonemes = ap.get_feasible_phonemes(alphabet_path)
# load model to retrain
if os.path.isfile(model_weights_path):
model.load_weights(model_weights_path)
print("Model weights loaded from disk")
else:
print("Model weights not found")
exit()
accumulate = 0
for i in range(0, len(dataset)):
# get one sample of test dataset to validate model
# model expects input data with shape: [batch_size, timesteps, features]
# in our case it is 1 element of batch, variable length and 26 MFCC features
X_test = dataset[i].features
X_test = X_test.reshape(1, X_test.shape[0], X_test.shape[1])
y_test = dataset[i].phonemes
# predict sequence
# it returns matrix of occurance probability for each phoneme
result = test_func([X_test])
# now we choose the biggest probability for each timestep
out = decode_batch(result[0])
out = np.asarray(out, dtype=int)
# print results
if verbose == True:
print("Predicted: \t{}".format(out))
print("Actual: \t{}".format(y_test))
predictions = evaluate_predictions(y_test, out)
out = ap.convert_number_to_phoneme(out, phonemes)
accumulate = accumulate + predictions
print("Correct predictions: {}%".format(round(accumulate/len(dataset), 2)))
def predict_model(model,
model_weights_path,
test_func,
audio_path,
transcription_path,
alphabet_path,
frame_width=0.025,
framing_function=np.hamming,
frame_imposition=0.01,
verbose=False):
"""Predicts most probable sequence of phonemes and writes it to file."""
phonemes = ap.get_feasible_phonemes(alphabet_path)
# load model weights
if os.path.isfile(model_weights_path):
model.load_weights(model_weights_path)
print("Model weights loaded from disk")
else:
print("Model weights not found")
exit()
audio, width = ap.process_audio(audio_path,
frame_width=frame_width,
frame_imposition=frame_imposition,
framing_function=framing_function)
audio = audio.reshape(1, audio.shape[0], audio.shape[1])
# predict sequence
# it returns matrix of occurance probability for each phoneme
result = test_func([audio])
# now we choose the biggest probability for each timestep
result = decode_batch(result[0])
result = np.asarray(result, dtype=int)
result = ap.convert_number_to_phoneme(result, phonemes)
create_transcription(result, transcription_path, window_width=width, verbose=verbose)