-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathspeech2mri_LSTM_train.py
276 lines (202 loc) · 9.43 KB
/
speech2mri_LSTM_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
'''
Written by Tamas Gabor Csapo <[email protected]>
First version Jan 21, 2019
Restructured Jan 21, 2020 - for MRI data
Keras implementation of Csapó T.G., ,,Speaker dependent acoustic-to-articulatory inversion using real-time MRI of the vocal tract'', accepted at Interspeech 2020
code for training LSTM
'''
import numpy as np
import matplotlib.pyplot as plt
import scipy.io.wavfile as io_wav
import os
import os.path
import datetime
import pickle
import cv2
import random
import vocoder_LSP_sptk
from keras.models import Sequential
from keras.layers import Input, Dense, Conv2D, MaxPooling2D, Flatten, UpSampling2D, Reshape, LSTM, TimeDistributed
from keras.callbacks import EarlyStopping, CSVLogger, ModelCheckpoint
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler, MinMaxScaler
# do not use all GPU memory
import tensorflow as tf
from keras.backend.tensorflow_backend import set_session
config = tf.ConfigProto()
# config.gpu_options.per_process_gpu_memory_fraction = 0.3
config.gpu_options.allow_growth = True
set_session(tf.Session(config=config))
# from LipReading with slight modifications
# https://github.com/hassanhub/LipReading/blob/master/codes/data_integration.py
################## VIDEO INPUT ##################
def load_video_3D(path, framesPerSec):
cap = cv2.VideoCapture(path)
frameCount = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
frameHeight = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT ))
frameWidth = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH ))
fps = cap.get(cv2.CAP_PROP_FPS)
# make sure that all the videos are the same FPS
if (np.abs(fps - framesPerSec) > 0.01):
print('fps:', fps, '(' + path + ')')
raise
buf = np.empty((frameHeight, frameWidth, frameCount), np.dtype('float32'))
fc = 0
ret = True
while (fc < frameCount and ret):
ret, frame = cap.read()
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
frame = frame.astype('float32')
# min-max scaling to [0-1]
frame = frame-np.amin(frame)
# make sure not to divide by zero
if np.amax(frame) != 0:
frame = frame/np.amax(frame)
buf[:,:,fc]=frame
fc += 1
cap.release()
return buf
# convert an array of values into a dataset matrix
# code with modifications from
# https://machinelearningmastery.com/time-series-prediction-lstm-recurrent-neural-networks-python-keras/
def create_dataset_img_inverse(data_in_X, data_in_Y, look_back=1):
(dim1_X, dim2_X) = data_in_X.shape
(dim1_Y, dim2_Y, dim3_Y, dim4_Y) = data_in_Y.shape
data_out_X = np.empty((dim1_X - look_back - 1, look_back, dim2_X))
data_out_Y = np.empty((dim1_Y - look_back - 1, dim2_Y, dim3_Y, dim4_Y))
for i in range(dim1_X - look_back - 1):
for j in range(look_back):
data_out_X[i, j] = data_in_X[i + j]
data_out_Y[i] = data_in_Y[i + j]
return data_out_X, data_out_Y
# load vocoder features,
# or calculate, if they are not available
def get_mgc_lsp_coeff(basefilename):
if os.path.isfile(basefilename + '.mgclsp'):
mgc_lsp_coeff = np.fromfile(basefilename + '.mgclsp', dtype=np.float32).reshape(-1, order + 1)
lf0 = np.fromfile(basefilename + '.lf0', dtype=np.float32)
else:
(mgc_lsp_coeff, lf0) = vocoder_LSP_sptk.encode(basefilename, samplingFrequency, frameLength, frameShift, order, alpha, stage)
return (mgc_lsp_coeff, lf0)
for speaker in ['f1', 'f2', 'm1', 'm2']:
# TODO: modify this according to your data path
dir_mri = '/home/csapot/deep_learning_mri/usctimit_mri/' + speaker + '/'
# Parameters of vocoder
samplingFrequency = 20000
frameLength = 1024 #
frameShift = 863 # 43.14 ms at 20000 Hz sampling, correspondong to 23.18 fps (MRI video)
order = 24
alpha = 0.42
stage = 3
n_mgc = order + 1
# context window of LSTM
n_sequence = 10
# properties of MRI videos
framesPerSec = 23.18
n_width = 68
n_height = 68
# USC-TIMIT contains 92 files (460 sentences) for each speaker
# train-valid-test split (random) :
# - 4 files for valid
# - 2 files for test
# - the remaining (86 files) for training
files_mri = dict()
mri = dict()
mgc = dict()
files_mri['all'] = []
if os.path.isdir(dir_mri):
for file in sorted(os.listdir(dir_mri)):
if ".avi" in file:
files_mri['all'] += [file]
# randomize file order
random.seed(17)
random.shuffle(files_mri['all'])
files_mri['valid'] = files_mri['all'][0:4]
files_mri['test'] = files_mri['all'][4:6]
files_mri['train'] = files_mri['all'][6:]
print('valid files', files_mri['valid'])
print('test files', files_mri['test']) # ['usctimit_mri_f1_146_150.avi', 'usctimit_mri_f1_441_445.avi']
for train_valid in ['train', 'valid']:
n_files = len(files_mri[train_valid])
n_file = 0
n_max_mri_frames = n_files * 1000
mri[train_valid] = np.empty((n_max_mri_frames, n_width, n_height))
mgc[train_valid] = np.empty((n_max_mri_frames, n_mgc))
mri_size = 0
mgc_size = 0
for file in files_mri[train_valid]:
try:
print('starting', train_valid, file)
mri_data = load_video_3D(dir_mri + file, framesPerSec)
(mgc_lsp_coeff, lf0) = get_mgc_lsp_coeff(dir_mri + file[:-4])
except ValueError as e:
print("wrong data, check manually!", e)
else:
print('minmax:', np.min(mri_data), np.max(mri_data))
n_file += 1
mgc_mri_len = np.min((mri_data.shape[2], len(mgc_lsp_coeff)))
mri_data = mri_data[:, :, 0:mgc_mri_len]
mgc_lsp_coeff = mgc_lsp_coeff[0:mgc_mri_len]
if mri_size + mgc_mri_len > n_max_mri_frames:
raise
for i in range(mgc_mri_len):
mri[train_valid][mri_size + i] = mri_data[:, :, i] # original, 68x68
mgc[train_valid][mgc_size + i] = mgc_lsp_coeff[i]
mri_size += mgc_mri_len
mgc_size += mgc_mri_len
print('n_frames_all: ', mri_size, 'mgc_size: ', mgc_size)
mri[train_valid] = mri[train_valid][0 : mri_size].reshape(-1, n_width, n_height, 1)
mgc[train_valid] = mgc[train_valid][0 : mgc_size]
# input: normalization to zero mean, unit variance
# feature by feature
mgc_scalers = []
for i in range(n_mgc):
mgc_scaler = StandardScaler(with_mean=True, with_std=True)
mgc_scalers.append(mgc_scaler)
mgc['train'][:, i] = mgc_scalers[i].fit_transform(mgc['train'][:, i].reshape(-1, 1)).ravel()
mgc['valid'][:, i] = mgc_scalers[i].transform(mgc['valid'][:, i].reshape(-1, 1)).ravel()
# target: min max scaler to [0,1] range
# already scaled in load_video
# restructure for LSTM
for train_valid in ['train', 'valid']:
mgc[train_valid], mri[train_valid] = create_dataset_img_inverse(mgc[train_valid], mri[train_valid], look_back = n_sequence)
mri[train_valid] = mri[train_valid].reshape(-1, n_width * n_height)
### single training
model = Sequential()
model.add(TimeDistributed(Dense(575, kernel_initializer='normal', activation='relu', input_shape=(n_mgc,))))
model.add(TimeDistributed(Dense(575, kernel_initializer='normal', activation='relu')))
model.add(TimeDistributed(Dense(575, kernel_initializer='normal', activation='relu')))
model.add(LSTM(575, kernel_initializer='normal', activation='relu', return_sequences=True))
model.add(LSTM(575, kernel_initializer='normal', activation='relu', return_sequences=False))
model.add(Dense(n_width*n_height, kernel_initializer='normal', activation='linear'))
model.build()
model.compile(loss='mean_squared_error', optimizer='adam')
# print(model.summary())
current_date = '{date:%Y-%m-%d_%H-%M-%S}'.format( date=datetime.datetime.now() )
model_name = 'models/SPEECH2MRI_LSTM_' + speaker + '_' + current_date
print('starting training', speaker, current_date)
# early stopping to avoid over-training
# csv logging of loss
# save best model
callbacks = [EarlyStopping(monitor='val_loss', patience=5, verbose=0), \
CSVLogger(model_name + '.csv', append=True, separator=';'),
ModelCheckpoint(model_name + '_weights.h5', monitor='val_loss')]
# run training
history = model.fit(mgc['train'], mri['train'],
epochs = 100, batch_size = 128, shuffle = True, verbose = 1,
validation_data=(mgc['valid'], mri['valid']),
callbacks=callbacks)
# 8.6M parameters
print(model.summary())
# here the training of the DNN is finished
# save model
model_json = model.to_json()
with open(model_name + '_model.json', "w") as json_file:
json_file.write(model_json)
# serialize scalers to pickle
pickle.dump(mgc_scalers, open(model_name + '_mgc_scalers.sav', 'wb'))
# save test files
with open(model_name + '_test_files.txt', 'w') as txt_file:
for file in files_mri['test']:
txt_file.write(file + '\n')
print('finished training', speaker, current_date)