目的:识别三个单词(bed,cat,happy)
github:https://github.com/yaokaishile/three-classification
一:导入需要的包
import librosa
import os
from sklearn.model_selection import train_test_split
from keras.utils import to_categorical
import numpy as np
from tqdm import tqdm
二、定义所需函数
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)
def wav2mfcc(file_path):
wave, sr = librosa.load(file_path, mono=True, sr=None)
mfcc = librosa.feature.mfcc(wave, sr=16000)
return mfcc
def save_data_to_array(path=DATA_PATH):
labels, _, _ = get_labels(path)
for label in labels:
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)
mfcc_vectors.append(mfcc)
np.save(label + '.npy', mfcc_vectors)
def get_train_test(split_ratio=0.6, random_state=42):
labels, indices, _ = get_labels(DATA_PATH)
X = np.load(labels[0] + '.npy')
y = np.zeros(X.shape[0])
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)
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_model():
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_classes, activation='softmax'))
model.compile(loss=keras.losses.categorical_crossentropy,
optimizer=keras.optimizers.Adadelta(),
metrics=['accuracy'])
return model
四、填入数据
%load_ext autoreload
#自动加载模块
%autoreload 2
#%aimport每次执行键入的Python代码之前,每次重新加载所有模块(排除的除外)。
from preprocess import *
import keras
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPooling2D
from keras.utils import to_categorical
feature_dim_2 = 32
save_data_to_array(max_len=feature_dim_2)
X_train, X_test, y_train, y_test = get_train_test()
feature_dim_1 = 20
channel = 1
epochs = 50
batch_size = 100
verbose = 1
num_classes = 3
X_train = X_train.reshape(X_train.shape[0], feature_dim_1, feature_dim_2, channel)
X_test = X_test.reshape(X_test.shape[0], feature_dim_1, feature_dim_2, channel)
y_train_hot = to_categorical(y_train)
y_test_hot = to_categorical(y_test)
五、评价模型
model = get_model()
model.fit(X_train, y_train_hot, batch_size=batch_size, epochs=epochs, verbose=verbose, validation_data=(X_test, y_test_hot))
模型准确率在0.95
大家可以亲自尝试一下。