Я сделал пользовательскую функцию генератора для чтения волновых файлов, извлечения mf cc и меток. Во время тренировки Model.fit или Model.fit_generator застревает на эпохе 1 и больше ничего не показывает? Также удаление генератора проверки не работает .. Это мой код:
import glob
import numpy as np
import random
from python_speech_features import mfcc
from python_speech_features import delta
from sklearn.model_selection import train_test_split
import scipy.io.wavfile as wav
from sklearn.preprocessing import LabelBinarizer
from keras.layers import LSTM, Dense, Dropout, Flatten, Input
from keras.models import Sequential
from keras.optimizers import Adam
from keras.callbacks import EarlyStopping, ModelCheckpoint
from keras.activations import softmax
Data_Dir = './wav_files/'
files = glob.glob(Data_Dir + "*.wav")
x_train, x_val = train_test_split(files, test_size = 0.1, random_state = 42)
print('# Training Examples:{}'.format(len(x_train)))
print('# Validation Examples:{}'.format(len(x_val)))
Вывод "" "Примеры обучения: 2149 Примеры проверки: 239" ""
labels = []
for i in range(len(files)):
label = files[i].split('/')[-1].split('_')[0]
if label not in labels:
labels.append(label)
label_binarizer = LabelBinarizer()
label_binarizer.fit(list(set(labels)))
def one_hot_encode(x): return
label_binarizer.transform(x)
def batch_generator(data, batch_size = 1):
while 1:
random.shuffle(data)
X, y = [], []
for i in range(batch_size):
wave = data[i]
(rate, sig) = wav.read(wave)
mfcc_feat = mfcc(sig, rate, preemph=0.98)
d_mfcc_feat = delta(mfcc_feat, 2)
d_d_mfcc_feat = delta(d_mfcc_feat, 2)
label = wave.split('/')[-1].split('_')[0]
y.append(label)
k = np.concatenate((mfcc_feat, d_mfcc_feat, d_d_mfcc_feat))
k = np.pad(k, ((1300 - len(k), 0), (0, 0)), mode='constant', constant_values = 0)
X = np.asarray(k)
X = np.expand_dims(X, -1)
#X = np.squeeze(X)
#y = np.expand_dims(y, -1)
X.reshape([1300, 13, 1])
yield X, np.array(y)
model = Sequential()
model.add(LSTM(4, return_sequences=True,batch_input_shape=(1, 13, 300),dropout=dropout))
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(len(labels), activation='softmax'))
opt = Adam(lr=1e-3)
model.compile(loss='sparse_categorical_crossentropy', optimizer=opt,metrics=['accuracy'])
checkpoint_filepath = 'weights.{epoch:02d}-{val_loss:.2f}.hdf5'
callbacks =[ModelCheckpoint(filepath = checkpoint_filepath,save_best_only=True),EarlyStopping(monitor='val_acc', patience=2)]
history = model.fit_generator(batch_generator(x_train, batch_size),steps_per_epoch=steps_per_epoch,epochs=2)
Вывод
Epoch 1/2