EMG_Biometrics_2021/Neural_Network_Analysis.py

136 lines
4.3 KiB
Python
Raw Normal View History

2021-07-02 12:46:31 +00:00
import json
import numpy as np
from sklearn.model_selection import train_test_split
2021-07-05 10:43:45 +00:00
import tensorflow.keras as keras
2021-07-02 12:46:31 +00:00
import tensorflow as tf
from pathlib import Path
2021-07-05 10:43:45 +00:00
import pandas as pd
import matplotlib.pyplot as plt
2021-07-02 12:46:31 +00:00
2021-07-05 10:43:45 +00:00
# path to json file that stores MFCCs and subject labels for each processed sample
DATA_PATH = str(Path.cwd()) + "/mfcc_data.json"
2021-07-02 12:46:31 +00:00
def load_data(data_path):
with open(data_path, "r") as fp:
data = json.load(fp)
# convert lists to numpy arrays
X = np.array(data["mfcc"])
y = np.array(data["labels"])
2021-07-05 10:43:45 +00:00
#X = np.asarray(X).astype('float32')
#y = np.asarray(y).astype('float32')
#y = tf.expand_dims(y, axis=1)
2021-07-02 12:46:31 +00:00
print("Data succesfully loaded!")
2021-07-05 10:43:45 +00:00
return X, y
def plot_history(history):
"""Plots accuracy/loss for training/validation set as a function of the epochs
:param history: Training history of model
:return:
"""
fig, axs = plt.subplots(2)
# create accuracy sublpot
axs[0].plot(history.history["accuracy"], label="train accuracy")
axs[0].plot(history.history["val_accuracy"], label="test accuracy")
axs[0].set_ylabel("Accuracy")
axs[0].legend(loc="lower right")
axs[0].set_title("Accuracy eval")
# create error sublpot
axs[1].plot(history.history["loss"], label="train error")
axs[1].plot(history.history["val_loss"], label="test error")
axs[1].set_ylabel("Error")
axs[1].set_xlabel("Epoch")
axs[1].legend(loc="upper right")
axs[1].set_title("Error eval")
plt.show()
def prepare_datasets(test_size=0.25, validation_size=0.2):
"""Loads data and splits it into train, validation and test sets.
:param test_size (float): Value in [0, 1] indicating percentage of data set to allocate to test split
:param validation_size (float): Value in [0, 1] indicating percentage of train set to allocate to validation split
:return X_train (ndarray): Input training set
:return X_validation (ndarray): Input validation set
:return X_test (ndarray): Input test set
:return y_train (ndarray): Target training set
:return y_validation (ndarray): Target validation set
:return y_test (ndarray): Target test set
"""
2021-07-02 12:46:31 +00:00
# load data
X, y = load_data(DATA_PATH)
2021-07-05 10:43:45 +00:00
# create train, validation and test split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=test_size)
X_train, X_validation, y_train, y_validation = train_test_split(X_train, y_train, test_size=validation_size)
return X_train, X_validation, X_test, y_train, y_validation, y_test
def build_model(input_shape, nr_classes=5):
"""Generates RNN-LSTM model
:param input_shape (tuple): Shape of input set
:return model: RNN-LSTM model
"""
2021-07-02 12:46:31 +00:00
# build network topology
2021-07-05 10:43:45 +00:00
model = keras.Sequential()
2021-07-02 12:46:31 +00:00
2021-07-05 10:43:45 +00:00
# 2 LSTM layers
model.add(keras.layers.LSTM(64, input_shape=input_shape, return_sequences=True))
model.add(keras.layers.LSTM(64))
2021-07-02 12:46:31 +00:00
2021-07-05 10:43:45 +00:00
# dense layer
model.add(keras.layers.Dense(64, activation='relu'))
model.add(keras.layers.Dropout(0.3))
2021-07-02 12:46:31 +00:00
2021-07-05 10:43:45 +00:00
# output layer
model.add(keras.layers.Dense(nr_classes, activation='softmax'))
2021-07-02 12:46:31 +00:00
2021-07-05 10:43:45 +00:00
return model
2021-07-02 12:46:31 +00:00
2021-07-05 10:43:45 +00:00
if __name__ == "__main__":
# get train, validation, test splits
X_train, X_validation, X_test, y_train, y_validation, y_test = prepare_datasets(0.25, 0.2)
# create network
print(X_train.shape)
X_train = np.reshape(X_train, (X_train.shape[0], 1, X_train.shape[1]))
X_test = np.reshape(X_test, (X_test.shape[0], 1, X_test.shape[1]))
#X_validation = np.reshape(X_validation, (X_test.shape[0], 1, X_test.shape[1]))
print(X_train.shape)
print(X_train.shape[0])
print(X_train.shape[1])
input_shape = (X_train.shape[1], X_train.shape[2]) # 300, 13
model = build_model(input_shape)
2021-07-02 12:46:31 +00:00
# compile model
optimiser = keras.optimizers.Adam(learning_rate=0.0001)
model.compile(optimizer=optimiser,
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
model.summary()
# train model
2021-07-05 10:43:45 +00:00
history = model.fit(X_train, y_train, validation_data=(X_validation, y_validation), batch_size=16, epochs=30)
# plot accuracy/error for training and validation
plot_history(history)
# evaluate model on test set
test_loss, test_acc = model.evaluate(X_test, y_test, verbose=2)
print('\nTest accuracy:', test_acc)