Unverified Commit 4e6ed910 authored by David-Badiane's avatar David-Badiane Committed by GitHub

Add files via upload

parent 6031bf5b
import numpy as np
def compute_cm_multiclass(gt, predicted):
classes = np.unique(gt)
CM = np.zeros((len(classes), len(classes)))
for i in np.arange(len(classes)):
pred_class = predicted[gt == i]
for j in np.arange(len(pred_class)):
CM[i, int(pred_class[j])] = CM[i, int(pred_class[j])] + 1
print(CM)
\ No newline at end of file
import numpy as np
import executables.modules.analysislab.user_interface
def Trainselected():
return np.load('features_selected.dat', allow_pickle=True)
def featmax():
return np.load('feat_max_min.dat', allow_pickle=True)[0]
def featmin():
return np.load('feat_max_min.dat', allow_pickle=True)[1]
def dict_train_features(c):
return np.load('dict_train_features_' + c + '.dat', allow_pickle=True)
def dict_test_feats(c):
return np.load('dict_test_feats_' + c + '.dat', allow_pickle=True)
def columns_selected():
return np.load('columns_selected.dat', allow_pickle=True)
from sklearn.feature_selection import SelectKBest
from sklearn.feature_selection import chi2
from executables.modules.analysislab.user_interface import kbest
def getfeaturelist(x_train_mc, y_train_mc):
selecta = SelectKBest(chi2, k=kbest()).fit(x_train_mc,
y_train_mc) # feature selection object declaration
columns_selected = selecta.get_support(indices=True) # selected columns of input matrix
X_new = selecta.fit_transform(x_train_mc, y_train_mc) # numpy matrix of selected features
scores_array = selecta.scores_
featurelist = {
'featurematrix': X_new, 'selectedcolumns': columns_selected, 'scores': scores_array}
return featurelist
import numpy as np
from executables.modules.analysislab.user_interface import featuresnames, classes, generate_datasets
from executables.modules import dataloader
import sklearn.svm
import itertools
def compute_metrics(gt_labels, predicted_labels):
TP = np.sum(np.logical_and(predicted_labels == 1, gt_labels == 1))
FP = np.sum(np.logical_and(predicted_labels == 1, gt_labels == 0))
TN = np.sum(np.logical_and(predicted_labels == 0, gt_labels == 0))
FN = np.sum(np.logical_and(predicted_labels == 0, gt_labels == 1))
accuracy = (TP + TN) / (TP + FP + TN + FN)
precision = TP / (TP + FP)
recall = TP / (TP + FN)
F1_score = 2 * precision * recall / (precision + recall)
print("Results : \n accuracy = {} \n precision = {} \n recall = {} \n F1 score = {} \n".format(
accuracy, precision, recall, F1_score))
return accuracy, precision, recall, F1_score
def compute_overall_metrics(metrics_matrix):
accuracy = np.average(metrics_matrix.transpose()[0])
precision = np.average(metrics_matrix.transpose()[1])
recall = np.average(metrics_matrix.transpose()[2])
F1_score = np.average(metrics_matrix.transpose()[3])
print("\n Overall Results : \n accuracy = {} \n precision = {} \n recall = {} \n F1 score = {}".format(
accuracy, precision, recall, F1_score))
def get_metrics(dict_test_features):
metrics_matrix = []
n = len(classes())
m = 4
datasets = generate_datasets
for subset in itertools.combinations(classes(), 2):
class_0 = subset[0]
class_1 = subset[1]
X_train_0 = dataloader.dict_train_features(class_0)
X_train_1 = dataloader.dict_train_features(class_1)
X_train = np.concatenate((X_train_0, X_train_1), axis=0)
X_train = X_train[:, dataloader.columns_selected()]
y_train_0 = np.zeros((X_train_0.shape[0],))
y_train_1 = np.ones((X_train_1.shape[0],))
y_train = np.concatenate((y_train_0, y_train_1), axis=0)
X_test_0 = dict_test_features[class_0]
X_test_1 = dict_test_features[class_1]
X_test = np.concatenate((X_test_0, X_test_1), axis=0)
X_test = X_test[:, dataloader.columns_selected()]
y_test_0 = np.zeros((X_test_0.shape[0],))
y_test_1 = np.ones((X_test_1.shape[0],))
y_test = np.concatenate((y_test_0, y_test_1), axis=0)
feat_max = np.max(X_train, axis=0)
feat_min = np.min(X_train, axis=0)
X_train_normalized = (X_train - feat_min) / (feat_max - feat_min)
X_test_normalized = (X_test - feat_min) / (feat_max - feat_min)
SVM_parameters = {
'C': 1,
'kernel': 'rbf',
}
clf = sklearn.svm.SVC(**SVM_parameters)
clf.fit(X_train_normalized, y_train)
y_test_predicted = clf.predict(X_test_normalized)
print("{} // {}".format(class_0, class_1))
metrics_couple = compute_metrics(y_test, y_test_predicted)
metrics_matrix = np.append(metrics_matrix, metrics_couple)
metrics_matrix = np.reshape(metrics_matrix, ((n, m)))
compute_overall_metrics(metrics_matrix)
from executables.modules.analysislab import user_interface
import numpy as np
def savedata(dict_train_features, featurelst, feat_max, feat_min):
datasets = user_interface.generate_datasets()
classes = user_interface.classes()
if (not datasets):
for c in classes:
dict_train_features[c].dump('dict_train_features_' + c + '.dat')
np.array([feat_max, feat_min]).dump('feat_max_min.dat')
featurelst['featurematrix'].dump('features_selected.dat')
featurelst['selectedcolumns'].dump('columns_selected.dat')
return True
def save_datasets(dict_train_feats, dict_test_feats):
for c in user_interface.classes():
dict_train_feats[c].dump('dict_train_features_' + c + '.dat')
for c in user_interface.classes():
dict_test_feats[c].dump('dict_test_feats_' + c + '.dat')
return True
import sklearn.svm
import numpy as np
from sklearn.model_selection import cross_val_score
from executables.modules.analysislab.user_interface import kfold
def getpredictions(X_train_normalized, y_train, X_test_mc_normalized):
SVM_parameters = {
'C': 1,
'kernel': 'rbf',
}
clf_01 = sklearn.svm.SVC(**SVM_parameters, probability=True)
clf_02 = sklearn.svm.SVC(**SVM_parameters, probability=True)
clf_12 = sklearn.svm.SVC(**SVM_parameters, probability=True)
X_01 = np.concatenate((X_train_normalized[0], X_train_normalized[1]), axis=0)
y_01 = np.concatenate((y_train[0], y_train[1]), axis=0)
X_02 = np.concatenate((X_train_normalized[0], X_train_normalized[2]), axis=0)
y_02 = np.concatenate((y_train[0], y_train[2]), axis=0)
X_12 = np.concatenate((X_train_normalized[1], X_train_normalized[2]), axis=0)
y_12 = np.concatenate((y_train[1], y_train[2]), axis=0)
clf_01.fit(X_01,y_01)
clf_02.fit(X_02, y_02)
clf_12.fit(X_12,y_12)
print("\nCross validated scores, k = 5: \n")
scores = ['accuracy', 'precision_micro', 'recall_macro', 'f1_macro', 'roc_auc']
overall_scores = { 'accuracy':[] ,'precision_micro':[] , 'recall_macro':[], 'f1_macro':[], 'roc_auc':[]}
for s in scores :
score1 = cross_val_score(clf_01, X_01, y_01, cv=kfold(), scoring=s)
score2 = cross_val_score(clf_02, X_02, y_02, cv=kfold(), scoring=s)
score3 = cross_val_score(clf_12, X_12, y_12, cv=kfold(), scoring=s)
score0 = np.concatenate((score1, score2,score3), axis=0)
score = np.average(score0)
print('\n')
print (s)
print('NoFX/Distortion')
print(score1)
print('NoFX/Tremolo')
print(score2)
print('Distortion/Tremolo')
print(score3)
overall_scores[s]=score
print (overall_scores)
y_test_predicted_01 = clf_01.predict(X_test_mc_normalized).reshape(-1, 1)
y_test_predicted_02 = clf_02.predict(X_test_mc_normalized).reshape(-1, 1)
y_test_predicted_12 = clf_12.predict(X_test_mc_normalized).reshape(-1, 1)
y_test_predicted_mc = np.concatenate((y_test_predicted_01, y_test_predicted_02, y_test_predicted_12), axis=1)
y_test_predicted_mc = np.array(y_test_predicted_mc, dtype=np.int)
y_test_predicted_mv = np.zeros((y_test_predicted_mc.shape[0],))
for i, e in enumerate(y_test_predicted_mc):
y_test_predicted_mv[i] = np.bincount(e).argmax()
return y_test_predicted_mv
import numpy as np
import librosa
import os
from executables.modules.analysislab import features, user_interface
def getdicttestfeatures(path):
dict_test_features = {'NoFX': [], 'Distortion': [], 'Tremolo': []}
for c in user_interface.classes(): # loops over classes
n_features = len(user_interface.featuresnames())
test_root = path.format(c)
# test_root = 'Test/{}/'.format(c)
class_test_files = [f for f in os.listdir(test_root) if f.endswith('.wav')]
n_test = len(class_test_files)
test_features = np.zeros((n_test, n_features))
for index, f in enumerate(class_test_files): # loops over all the files of the class
audio, fs = librosa.load(os.path.join(test_root, f), sr=None)
test_features[index, :] = features.getfeatures(audio)
dict_test_features[c] = test_features
return dict_test_features
import numpy as np
import librosa
import os
from executables.modules.analysislab import features, user_interface
def getdicttrainfeatures(path):
dict_train_features = {'NoFX': [], 'Distortion': [], 'Tremolo': []}
fullpath = path
for c in user_interface.classes(): # loops over classes
n_features = len(user_interface.featuresnames())
train_root = fullpath.format(c)
class_train_files = [f for f in os.listdir(train_root) if f.endswith('.wav')]
n_train = len(class_train_files)
train_features = np.zeros((n_train, n_features))
for index, f in enumerate(class_train_files): # loops over all the files of the class
audio, fs = librosa.load(os.path.join(train_root, f), sr=None)
train_features[index, :] = features.getfeatures(audio)
dict_train_features[c] = train_features
return dict_train_features
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment