opendata/20220814/Source/train_audio.py

130 lines
4.1 KiB
Python

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import LabelEncoder, StandardScaler
import keras
from keras import models
from keras import layers
from keras import callbacks
from keras import losses
class History(callbacks.Callback):
def on_train_begin(self,logs={}):
self.losses=[]
self.acces=[]
self.val_acc = []
self.val_losses = []
def on_batch_end(self,batch,logs={}):
self.losses.append(logs.get('loss'))
self.acces.append(logs.get('acc'))
self.val_losses.append(logs.get('val_loss'))
self.val_acc.append(logs.get('val_acc'))
history=History()
def create_model():
model = models.Sequential()
model.add(layers.BatchNormalization(input_shape=(26,), axis=1))
model.add(layers.Dense(512, activation='relu'))
model.add(layers.Dropout(0.2))
model.add(layers.BatchNormalization(axis=1))
model.add(layers.Dense(512, activation='relu'))
model.add(layers.Dropout(0.2))
model.add(layers.BatchNormalization(axis=1))
model.add(layers.Dense(256, activation='relu'))
model.add(layers.Dropout(0.2))
model.add(layers.BatchNormalization(axis=1))
model.add(layers.Dense(128, activation='relu'))
model.add(layers.Dropout(0.2))
model.add(layers.BatchNormalization(axis=1))
model.add(layers.Dense(64, activation='relu'))
model.add(layers.Dropout(0.2))
model.add(layers.BatchNormalization(axis=1))
model.add(layers.Dense(6, activation='softmax'))
return model
def train():
model = create_model()
sgd = keras.optimizers.SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True)
model.compile(optimizer='adam',
loss=losses.sparse_categorical_crossentropy,
metrics=['accuracy'])
checkpointer = callbacks.ModelCheckpoint(monitor='val_loss',filepath="../Output/checkpoint.hdf5", verbose=0, save_best_only=True)
reduce_lr = callbacks.ReduceLROnPlateau(monitor='val_loss', patience=5, mode='auto')
x, x_test, y, y_test = load_audio_data()
x_train, x_val, y_train, y_val = train_test_split(x,y,test_size=0.2)
historys = model.fit(x_train,
y_train,
epochs=1000,
batch_size=1000,
validation_data=(x_val,y_val),shuffle=True,
callbacks=[checkpointer,reduce_lr,history])
results = model.evaluate(x_test, y_test)
print('test_results: ', results)
plt.plot(historys.history['val_loss'],label='val loss')
plt.plot(historys.history['val_acc'],label='val acc')
plt.plot(historys.history['acc'],label='acc')
plt.plot(historys.history['loss'],label='loss')
plt.title("model")
plt.ylabel("Accuracy and Loss")
plt.xlabel("epoch")
plt.legend(loc="lower right")
model.save(MODEL_FILE_PATH)
def load_trained_model():
model = models.load_model(MODEL_FILE_PATH)
return model
def predict(x):
model = load_trained_model()
# scaler = StandardScaler()
x = x.reshape(1, -1)
# x = scaler.fit_transform(x)
return model.predict(x)
def load_audio_data():
data = pd.read_csv('../Output/only_class_data.csv',encoding="ISO-8859-1")
data = data.drop(['filename'], axis=1)
emotion_list = data.iloc[:, -1]
encoder = LabelEncoder()
y = encoder.fit_transform(emotion_list)
x = np.array(data.iloc[:, :-1], dtype=float)
# scaler = StandardScaler().fit(x)
# x = scaler.transform()
return train_test_split(x, y, test_size=0.2)
def load_test_data():
data = pd.read_csv('../Output/only_class_data.csv',encoding="ISO-8859-1")
data = data.drop(['filename'], axis=1)
emotion_list = data.iloc[:, -1]
encoder = LabelEncoder()
y = encoder.fit_transform(emotion_list)
x = np.array(data.iloc[:, :-1], dtype=float)
# scaler = StandardScaler().fit(x)
# x = scaler.transform()
return x,y
if __name__ == '__main__':
MODEL_FILE_PATH = '../Output/english_class.h5'
# train()
#
model = load_trained_model()
x_test,y_test = load_test_data()
results = model.evaluate(x_test,y_test)
print('test_results: ', results)