chore: add to the readme and restructure

This commit is contained in:
Skudalen 2021-07-09 09:39:10 +02:00
parent 8de9efb0ac
commit 503742e231
3 changed files with 44 additions and 38 deletions

BIN
.DS_Store vendored

Binary file not shown.

View File

@ -9,7 +9,7 @@ import pandas as pd
import matplotlib.pyplot as plt
# path to json file that stores MFCCs and subject labels for each processed sample
DATA_PATH = str(Path.cwd()) + "/mfcc_data.json"
DATA_PATH_MFCC = str(Path.cwd()) + "/mfcc_data.json"
def load_data_from_json(data_path):
@ -57,29 +57,16 @@ def plot_history(history):
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
"""
# load data
X, y = load_data(DATA_PATH)
def prepare_datasets_percentsplit(X, y, shuffle_vars, validation_size=0.2, test_size=0.25,):
# 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)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=test_size, shuffle=shuffle_vars)
X_train, X_validation, y_train, y_validation = train_test_split(X_train, y_train, test_size=validation_size, shuffle=shuffle_vars)
return X_train, X_validation, X_test, y_train, y_validation, y_test
def build_model(input_shape, nr_classes=5):
def RNN_LSTM(input_shape, nr_classes=5):
"""Generates RNN-LSTM model
:param input_shape (tuple): Shape of input set
:return model: RNN-LSTM model
@ -101,29 +88,39 @@ def build_model(input_shape, nr_classes=5):
return model
def train(model, batch_size, epochs, X_train, X_validation, y_train, y_validation):
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)
print(X_train.shape[1], X_train.shape[2])
# create network
input_shape = (X_train.shape[1], X_train.shape[2]) # (~2800), 1, 208
model = build_model(input_shape)
# compile model
optimiser = keras.optimizers.Adam(learning_rate=0.0001)
model.compile(optimizer=optimiser,
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
history = model.fit(X_train,
y_train,
validation_data=(X_validation, y_validation),
batch_size=batch_size,
epochs=epochs)
return history
if __name__ == "__main__":
# Load data
X, y = load_data_from_json(DATA_PATH_MFCC)
# Get prepared data: train, validation, and test
X_train, X_validation, X_test, y_train, y_validation, y_test = prepare_datasets_percentsplit(X, y,
validation_size=0.2,
test_size=0.25,
shuffle_vars=True)
#print(X_train.shape[1], X_train.shape[2])
# Make model
model = RNN_LSTM(input_shape=(1, 208))
model.summary()
# train model
history = model.fit(X_train, y_train, validation_data=(X_validation, y_validation), batch_size=64, epochs=30)
# Train network
history = train(model, X_train, X_validation, y_train, y_validation, batch_size=64, epochs=30)
# plot accuracy/error for training and validation
plot_history(history)

View File

@ -17,9 +17,9 @@ Scripts to handle CSV files composed by 2 * 8 EMG sensors(left & right) devided
#### Credits for insporational code
* Kapre - Keunwoochoi
* Kapre: Keunwoochoi
* Audio-Classification: seth814
* DeepLearningForAudioWithPyhton - musikalkemist
* DeepLearningForAudioWithPyhton: musikalkemist
## Table of Contents
@ -31,7 +31,16 @@ Scripts to handle CSV files composed by 2 * 8 EMG sensors(left & right) devided
| Neural_Network_Analysis.py | Contains functions to load, build and execute analysis with Neural Networks. Main functions are <br>load_data_from_json(), build_model(), and main() |
## How to use it
1. Clone the repo
2. Place the data files in the working directory
3. (For now) Add the session filenames in the desired load_data() function
4. Assuming NN analysis:
1. Create `CSV_handler` object
2. Load data with `load_data(CSV_handler, <datatype>)`
3. Create `NN_handler` object with <CSV_handler> as input
4. Load MFCC data into the `NN_handler` with `store_mfcc_samples()`
5. Run `save_json_mfcc()`
6. Run `Neural_Network_Analysis.py`