Compare commits
No commits in common. "439238e0708c1bd1c068424b12c01bc5e18fc5a9" and "ca1eeb0a83efa116230697ff1db811593ae7b062" have entirely different histories.
439238e070
...
ca1eeb0a83
8
.gitignore
vendored
8
.gitignore
vendored
@ -1,4 +1,4 @@
|
|||||||
data
|
Exp20201205_2myo_hard**
|
||||||
docs
|
Exp20201205_2myo_soft**
|
||||||
logs
|
Documents
|
||||||
psf_lib
|
python_speech_features**
|
@ -6,8 +6,8 @@ from pathlib import Path
|
|||||||
import numpy as np
|
import numpy as np
|
||||||
from pandas.core.frame import DataFrame
|
from pandas.core.frame import DataFrame
|
||||||
import sys
|
import sys
|
||||||
sys.path.insert(0, '/Users/Markus/Prosjekter git/Slovakia 2021/psf_lib/python_speech_features/python_speech_features')
|
sys.path.insert(0, '/Users/Markus/Prosjekter git/Slovakia 2021/python_speech_features/python_speech_features')
|
||||||
from psf_lib.python_speech_features.python_speech_features import mfcc
|
from python_speech_features.python_speech_features import mfcc
|
||||||
import json
|
import json
|
||||||
|
|
||||||
|
|
||||||
|
@ -1,19 +1,15 @@
|
|||||||
import json
|
import json
|
||||||
|
from python_speech_features.python_speech_features.base import mfcc
|
||||||
from keras import callbacks
|
|
||||||
from psf_lib.python_speech_features.python_speech_features.base import mfcc
|
|
||||||
import numpy as np
|
import numpy as np
|
||||||
from sklearn.model_selection import train_test_split
|
from sklearn.model_selection import train_test_split
|
||||||
import tensorflow as tf
|
import tensorflow as tf
|
||||||
import tensorflow.keras as keras
|
import tensorflow.keras as keras
|
||||||
from keras import backend as K
|
from keras import backend as K
|
||||||
from keras.regularizers import l2
|
from keras.regularizers import l2
|
||||||
from keras.callbacks import Callback, CSVLogger, ModelCheckpoint
|
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
import pandas as pd
|
import pandas as pd
|
||||||
import matplotlib.pyplot as plt
|
import matplotlib.pyplot as plt
|
||||||
import statistics
|
import statistics
|
||||||
import csv
|
|
||||||
|
|
||||||
# Path to json file that stores MFCCs and subject labels for each processed sample
|
# Path to json file that stores MFCCs and subject labels for each processed sample
|
||||||
DATA_PATH_MFCC = str(Path.cwd()) + "/mfcc_data.json"
|
DATA_PATH_MFCC = str(Path.cwd()) + "/mfcc_data.json"
|
||||||
@ -162,16 +158,12 @@ def prepare_datasets_sessions(X, y, session_lengths, test_session_index=4, nr_su
|
|||||||
# Trains the model
|
# Trains the model
|
||||||
# Input: Keras.model, batch_size, nr epochs, training, and validation data
|
# Input: Keras.model, batch_size, nr epochs, training, and validation data
|
||||||
# Ouput: History
|
# Ouput: History
|
||||||
def train( model, X_train, y_train, verbose, batch_size=64, epochs=30,
|
def train(model, X_train, y_train, verbose, batch_size=64, epochs=30, X_validation=None, y_validation=None):
|
||||||
X_validation=None, y_validation=None):
|
|
||||||
|
|
||||||
optimiser = keras.optimizers.Adam(learning_rate=0.0001)
|
optimiser = keras.optimizers.Adam(learning_rate=0.0001)
|
||||||
model.compile(optimizer=optimiser,
|
model.compile(optimizer=optimiser,
|
||||||
loss='categorical_crossentropy',
|
loss='categorical_crossentropy',
|
||||||
metrics=['accuracy'])
|
metrics=['accuracy'])
|
||||||
|
|
||||||
#csv_path = str(Path.cwd()) + '/logs/{}/{}_train_log.csv'.format(MODEL_NAME, MODEL_NAME)
|
|
||||||
#csv_logger = CSVLogger(csv_path, append=False)
|
|
||||||
|
|
||||||
if X_validation != None:
|
if X_validation != None:
|
||||||
history = model.fit(X_train,
|
history = model.fit(X_train,
|
||||||
@ -241,66 +233,24 @@ def batch_formatting(X_train, X_test, y_train, y_test, batch_size=64, nr_classes
|
|||||||
|
|
||||||
return X_train_batch, X_test_batch, y_train_batch, y_test_batch
|
return X_train_batch, X_test_batch, y_train_batch, y_test_batch
|
||||||
|
|
||||||
# Retrieves data sets for each session as test set and evalutes. DOES USE prediction_csv_logger as default
|
# Retrieves data sets for each session as test set and evalutes
|
||||||
# the average of networks trained om them
|
# the average of networks trained om them
|
||||||
# Input: raw data, session_lengths list, total nr of sessions, batch_size, and nr of epochs
|
# Input: raw data, session_lengths list, total nr of sessions, batch_size, and nr of epochs
|
||||||
# Ouput: tuple(cross validation average, list(result for each dataset(len=nr_sessions)))
|
# Ouput: tuple(cross validation average, list(result for each dataset(len=nr_sessions)))
|
||||||
def session_cross_validation(model_name:str, X, y, session_lengths, nr_sessions, log_to_csv=True, batch_size=64, epochs=30):
|
def session_cross_validation_LSTM(X, y, session_lengths, nr_sessions, batch_size=64, epochs=30):
|
||||||
session_training_results = []
|
session_training_results = []
|
||||||
for i in range(nr_sessions):
|
for i in range(nr_sessions):
|
||||||
|
model = LSTM(input_shape=(1, 208))
|
||||||
X_train_session, X_test_session, y_train_session, y_test_session = prepare_datasets_sessions(X, y, session_lengths, i)
|
X_train_session, X_test_session, y_train_session, y_test_session = prepare_datasets_sessions(X, y, session_lengths, i)
|
||||||
|
train(model, X_train_session, y_train_session, verbose=0, batch_size=batch_size, epochs=epochs)
|
||||||
# Model:
|
|
||||||
if model_name == 'LSTM':
|
|
||||||
model = LSTM(input_shape=(1, 208))
|
|
||||||
|
|
||||||
elif model_name == 'GRU':
|
|
||||||
model = GRU(input_shape=(1, 208))
|
|
||||||
|
|
||||||
elif model_name == 'CNN_1D':
|
|
||||||
X_train_session = np.reshape(X_train_session, (X_train_session.shape[0], 208, 1))
|
|
||||||
X_test_session = np.reshape(X_test_session, (X_test_session.shape[0], 208, 1))
|
|
||||||
model = CNN_1D(input_shape=(208, 1))
|
|
||||||
|
|
||||||
elif model_name == 'FFN':
|
|
||||||
model = FFN(input_shape=(1, 208))
|
|
||||||
|
|
||||||
else:
|
|
||||||
raise Exception('Model not found')
|
|
||||||
|
|
||||||
#model.summary()
|
|
||||||
|
|
||||||
|
|
||||||
train(model, X_train_session, y_train_session, verbose=1, batch_size=batch_size, epochs=epochs)
|
|
||||||
test_loss, test_acc = model.evaluate(X_test_session, y_test_session, verbose=2)
|
test_loss, test_acc = model.evaluate(X_test_session, y_test_session, verbose=2)
|
||||||
session_training_results.append(test_acc)
|
session_training_results.append(test_acc)
|
||||||
if log_to_csv:
|
|
||||||
prediction_csv_logger(X_test_session, y_test_session, model_name, model, i)
|
|
||||||
del model
|
del model
|
||||||
K.clear_session()
|
K.clear_session()
|
||||||
#print('Session', i, 'as test data gives accuracy:', test_acc)
|
print('Session', i, 'as test data gives accuracy:', test_acc)
|
||||||
|
|
||||||
average_result = statistics.mean((session_training_results))
|
average_result = statistics.mean((session_training_results))
|
||||||
|
|
||||||
return average_result, session_training_results
|
return average_result, session_training_results
|
||||||
|
|
||||||
# Takes in test data and logs input data and the prediction from a model
|
|
||||||
# Input: raw data, session_lengths list, total nr of sessions, batch_size, and nr of epochs
|
|
||||||
# Ouput: tuple(cross validation average, list(result for each dataset(len=nr_sessions)))
|
|
||||||
def prediction_csv_logger(X, y, model_name, model, session_nr):
|
|
||||||
|
|
||||||
csv_path = str(Path.cwd()) + '/logs/{}/{}_session{}_log.csv'.format(model_name, model_name, session_nr+1)
|
|
||||||
|
|
||||||
layerOutput = model.predict(X, verbose=0)
|
|
||||||
|
|
||||||
with open(csv_path, 'w') as csv_file:
|
|
||||||
writer = csv.writer(csv_file)
|
|
||||||
writer.writerow(['input', 'prediction', 'solution'])
|
|
||||||
data = zip(X, layerOutput, y)
|
|
||||||
writer.writerows(data)
|
|
||||||
csv_file.close()
|
|
||||||
|
|
||||||
|
|
||||||
# ----- MODELS ------
|
# ----- MODELS ------
|
||||||
|
|
||||||
@ -309,9 +259,9 @@ def prediction_csv_logger(X, y, model_name, model, session_nr):
|
|||||||
# Ouput: model:Keras.model
|
# Ouput: model:Keras.model
|
||||||
def LSTM(input_shape, nr_classes=5):
|
def LSTM(input_shape, nr_classes=5):
|
||||||
|
|
||||||
model = keras.Sequential(name='LSTM_model')
|
model = keras.Sequential()
|
||||||
model.add(keras.layers.Bidirectional(keras.layers.LSTM(128), input_shape=input_shape, name='Bidirectional_LSTM'))
|
model.add(keras.layers.Bidirectional(keras.layers.LSTM(64), input_shape=input_shape, name='Bidirectional_LSTM'))
|
||||||
model.add(keras.layers.Dense(128, activation='relu', activity_regularizer=l2(0.005), name='Dense_relu'))
|
model.add(keras.layers.Dense(64, activation='relu', activity_regularizer=l2(0.001), name='Dense_relu'))
|
||||||
model.add(keras.layers.Dropout(0.3, name='Dropout'))
|
model.add(keras.layers.Dropout(0.3, name='Dropout'))
|
||||||
# Output layer
|
# Output layer
|
||||||
model.add(keras.layers.Dense(nr_classes, activation='softmax', name='Dense_relu_output'))
|
model.add(keras.layers.Dense(nr_classes, activation='softmax', name='Dense_relu_output'))
|
||||||
@ -323,44 +273,12 @@ def LSTM(input_shape, nr_classes=5):
|
|||||||
# Ouput: model:Keras.model
|
# Ouput: model:Keras.model
|
||||||
def GRU(input_shape, nr_classes=5):
|
def GRU(input_shape, nr_classes=5):
|
||||||
|
|
||||||
model = keras.Sequential(name='GRU_model')
|
model = keras.Sequential()
|
||||||
model.add(keras.layers.Bidirectional(keras.layers.GRU(128), input_shape=input_shape, name='Bidirectional_GRU'))
|
model.add(keras.layers.Bidirectional(keras.layers.GRU(64), input_shape=input_shape, name='Bidirectional_GRU'))
|
||||||
model.add(keras.layers.Dense(128, activation='relu', activity_regularizer=l2(0.005), name='Dense_relu'))
|
model.add(keras.layers.Dense(64, activation='relu', activity_regularizer=l2(0.001), name='Dense_relu'))
|
||||||
model.add(keras.layers.Dropout(0.3, name='Dropout'))
|
model.add(keras.layers.Dropout(0.3, name='Dropout'))
|
||||||
# Output layer:
|
# Output layer:
|
||||||
model.add(keras.layers.Dense(nr_classes, activation='softmax', name='Softmax'))
|
model.add(keras.layers.Dense(nr_classes, activation='softmax', name='Dense_relu_output'))
|
||||||
|
|
||||||
return model
|
|
||||||
|
|
||||||
# Creates a keras.model with a basic feed-forward-network
|
|
||||||
# Input: input shape, classes of classification
|
|
||||||
# Ouput: model:Keras.model
|
|
||||||
def FFN(input_shape, nr_classes=5):
|
|
||||||
|
|
||||||
model = keras.Sequential(name='FFN_model')
|
|
||||||
model.add(keras.layers.Reshape((input_shape[-1],), input_shape=input_shape))
|
|
||||||
model.add(keras.layers.Dense(256, activation='relu', input_shape=input_shape, name='Dense_relu_1'))
|
|
||||||
model.add(keras.layers.Dense(128, activation='relu', activity_regularizer=l2(0.005), name='Dense_relu_2'))
|
|
||||||
model.add(keras.layers.Dense(64, activation='relu', activity_regularizer=l2(0.005), name='Dense_relu_3'))
|
|
||||||
model.add(keras.layers.Dropout(0.3, name='Dropout'))
|
|
||||||
# Output layer:
|
|
||||||
model.add(keras.layers.Dense(nr_classes, activation='softmax', name='Softmax'))
|
|
||||||
|
|
||||||
return model
|
|
||||||
|
|
||||||
# Creates a keras.model with focus on Convulotion layers
|
|
||||||
# Input: input shape, classes of classification
|
|
||||||
# Ouput: model:Keras.model
|
|
||||||
def CNN_1D(input_shape, nr_classes=5):
|
|
||||||
|
|
||||||
model = keras.Sequential(name='CNN_model')
|
|
||||||
model.add(keras.layers.Conv1D(32, kernel_size=5, activation='relu', input_shape=input_shape))
|
|
||||||
model.add(keras.layers.MaxPooling1D(pool_size=5))
|
|
||||||
model.add(keras.layers.Flatten())
|
|
||||||
model.add(keras.layers.Dense(128, activation='relu'))
|
|
||||||
model.add(keras.layers.Dropout(0.3))
|
|
||||||
# Ouput layer
|
|
||||||
model.add(keras.layers.Dense(nr_classes, activation='softmax', name='Softmax'))
|
|
||||||
|
|
||||||
return model
|
return model
|
||||||
|
|
||||||
@ -377,12 +295,10 @@ if __name__ == "__main__":
|
|||||||
NR_SUBJECTS = 5
|
NR_SUBJECTS = 5
|
||||||
NR_SESSIONS = 4
|
NR_SESSIONS = 4
|
||||||
BATCH_SIZE = 64
|
BATCH_SIZE = 64
|
||||||
EPOCHS = 5
|
EPOCHS = 30
|
||||||
|
|
||||||
TEST_SESSION_NR = 4
|
TEST_SESSION_NR = 4
|
||||||
VERBOSE = 1
|
VERBOSE = 0
|
||||||
MODEL_NAME = 'CNN_1D'
|
|
||||||
LOG = True
|
|
||||||
|
|
||||||
# ----- Get prepared data: train, validation, and test ------
|
# ----- Get prepared data: train, validation, and test ------
|
||||||
# X_train.shape = (2806-X_test, 1, 208)
|
# X_train.shape = (2806-X_test, 1, 208)
|
||||||
@ -393,67 +309,29 @@ if __name__ == "__main__":
|
|||||||
X_train, X_test, y_train, y_test = prepare_datasets_sessions(X, y, session_lengths, TEST_SESSION_NR)
|
X_train, X_test, y_train, y_test = prepare_datasets_sessions(X, y, session_lengths, TEST_SESSION_NR)
|
||||||
|
|
||||||
|
|
||||||
'''
|
#'''
|
||||||
# ----- Make model ------
|
# ----- Make model ------
|
||||||
#model_GRU = GRU(input_shape=(1, 208)) # (timestep, 13*16 MFCC coefficients)
|
model_GRU = GRU(input_shape=(1, 208)) # (timestep, 13*16 MFCC coefficients)
|
||||||
#model_LSTM = LSTM(input_shape=(1, 208)) # (timestep, 13*16 MFCC coefficients)
|
model_LSTM = LSTM(input_shape=(1, 208)) # (timestep, 13*16 MFCC coefficients)
|
||||||
model_CNN_1D = CNN(input_shape=(208, 1)) # (timestep, 13*16 MFCC coefficients)
|
|
||||||
|
|
||||||
model_CNN_1D.summary()
|
model_GRU.summary()
|
||||||
#model_GRU.summary()
|
model_LSTM.summary()
|
||||||
#model_LSTM.summary()
|
|
||||||
|
|
||||||
|
|
||||||
# ----- Train network ------
|
# ----- Train network ------
|
||||||
#history_GRU = train(model_GRU, X_train, y_train, verbose=VERBOSE, batch_size=BATCH_SIZE, epochs=EPOCHS)
|
history_GRU = train(model_GRU, X_train, y_train, verbose=VERBOSE, batch_size=BATCH_SIZE, epochs=EPOCHS)
|
||||||
#history_LSTM = train(model_LSTM, X_train, y_train, verbose=VERBOSE, batch_size=BATCH_SIZE, epochs=EPOCHS)
|
history_LSTM = train(model_LSTM, X_train, y_train, verbose=VERBOSE, batch_size=BATCH_SIZE, epochs=EPOCHS)
|
||||||
history_CNN_1D = train( model_CNN_1D, np.reshape(X_train, (X_train.shape[0], 208, 1)),
|
#average = session_cross_validation_LSTM(X, y, session_lengths, NR_SESSIONS, BATCH_SIZE, EPOCHS)
|
||||||
y_train, verbose=VERBOSE, batch_size=BATCH_SIZE, epochs=EPOCHS)
|
#print('\nCrossvalidated:', average)
|
||||||
|
|
||||||
|
|
||||||
# ----- Plot train accuracy/error -----
|
# ----- Plot train accuracy/error -----
|
||||||
#plot_train_history(history)
|
#plot_train_history(history)
|
||||||
|
|
||||||
|
|
||||||
# ----- Evaluate model on test set ------
|
# ----- Evaluate model on test set ------
|
||||||
|
test_loss, test_acc = model_GRU.evaluate(X_test, y_test, verbose=VERBOSE)
|
||||||
#test_loss, test_acc = model_GRU.evaluate(X_test, y_test, verbose=VERBOSE)
|
print('\nTest accuracy GRU:', test_acc, '\n')
|
||||||
#print('\nTest accuracy GRU:', test_acc, '\n')
|
test_loss, test_acc = model_LSTM.evaluate(X_test, y_test, verbose=VERBOSE)
|
||||||
#test_loss, test_acc = model_LSTM.evaluate(X_test, y_test, verbose=VERBOSE)
|
print('\nTest accuracy LSTM:', test_acc, '\n')
|
||||||
#print('\nTest accuracy LSTM:', test_acc, '\n')
|
#'''
|
||||||
test_loss, test_acc = model_CNN_1D.evaluate(np.reshape(X_test, (X_test.shape[0], 208, 1)), y_test, verbose=0)
|
|
||||||
print('\nTest accuracy CNN_1D:', test_acc, '\n')
|
|
||||||
|
|
||||||
|
|
||||||
# ----- Store test predictions in CSV ------
|
|
||||||
prediction_csv_logger(np.reshape(X_test, (X_test.shape[0], 208, 1)), y_test, MODEL_NAME, model_CNN_1D, TEST_SESSION_NR)
|
|
||||||
'''
|
|
||||||
|
|
||||||
|
|
||||||
#'''
|
|
||||||
# ----- Cross validation ------
|
|
||||||
average_GRU = session_cross_validation('GRU', X, y, session_lengths, nr_sessions=NR_SESSIONS,
|
|
||||||
log_to_csv=LOG,
|
|
||||||
batch_size=BATCH_SIZE,
|
|
||||||
epochs=EPOCHS)
|
|
||||||
average_LSTM = session_cross_validation('LSTM', X, y, session_lengths, nr_sessions=NR_SESSIONS,
|
|
||||||
log_to_csv=LOG,
|
|
||||||
batch_size=BATCH_SIZE,
|
|
||||||
epochs=EPOCHS)
|
|
||||||
average_FFN = session_cross_validation('FFN', X, y, session_lengths, nr_sessions=NR_SESSIONS,
|
|
||||||
log_to_csv=LOG,
|
|
||||||
batch_size=BATCH_SIZE,
|
|
||||||
epochs=EPOCHS)
|
|
||||||
average_CNN = session_cross_validation('CNN_1D', X, y, session_lengths, nr_sessions=NR_SESSIONS,
|
|
||||||
log_to_csv=LOG,
|
|
||||||
batch_size=BATCH_SIZE,
|
|
||||||
epochs=EPOCHS)
|
|
||||||
|
|
||||||
print('\n')
|
|
||||||
print('Crossvalidated GRU:', average_GRU)
|
|
||||||
print('Crossvalidated LSTM:', average_LSTM)
|
|
||||||
print('Crossvalidated FFN:', average_FFN)
|
|
||||||
print('Cross-validated CNN_1D:', average_CNN)
|
|
||||||
print('\n')
|
|
||||||
#'''
|
|
||||||
|
|
||||||
|
10
python_speech_features.egg-info/PKG-INFO
Normal file
10
python_speech_features.egg-info/PKG-INFO
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
Metadata-Version: 1.0
|
||||||
|
Name: python-speech-features
|
||||||
|
Version: 0.6.1
|
||||||
|
Summary: Python Speech Feature extraction
|
||||||
|
Home-page: https://github.com/jameslyons/python_speech_features
|
||||||
|
Author: James Lyons
|
||||||
|
Author-email: james.lyons0@gmail.com
|
||||||
|
License: MIT
|
||||||
|
Description: UNKNOWN
|
||||||
|
Platform: UNKNOWN
|
7
python_speech_features.egg-info/SOURCES.txt
Normal file
7
python_speech_features.egg-info/SOURCES.txt
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
python_speech_features/example.py
|
||||||
|
python_speech_features/setup.py
|
||||||
|
python_speech_features.egg-info/PKG-INFO
|
||||||
|
python_speech_features.egg-info/SOURCES.txt
|
||||||
|
python_speech_features.egg-info/dependency_links.txt
|
||||||
|
python_speech_features.egg-info/requires.txt
|
||||||
|
python_speech_features.egg-info/top_level.txt
|
1
python_speech_features.egg-info/dependency_links.txt
Normal file
1
python_speech_features.egg-info/dependency_links.txt
Normal file
@ -0,0 +1 @@
|
|||||||
|
|
2
python_speech_features.egg-info/requires.txt
Normal file
2
python_speech_features.egg-info/requires.txt
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
numpy
|
||||||
|
scipy
|
1
python_speech_features.egg-info/top_level.txt
Normal file
1
python_speech_features.egg-info/top_level.txt
Normal file
@ -0,0 +1 @@
|
|||||||
|
python_speech_features
|
Loading…
Reference in New Issue
Block a user