我做了一个自定义生成器功能,可以读取波形文件,提取mfcc和标签。 在训练时,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