Commit 76eced7b authored by estellearrc's avatar estellearrc
Browse files

merge

parents 04a27cd6 3ca53cd0
This diff is collapsed.
......@@ -8,6 +8,8 @@ import tensorflow as tf
from sklearn.preprocessing import StandardScaler
from sklearn import preprocessing
from matplotlib import pyplot as plt
from sklearn.metrics import ConfusionMatrixDisplay
from sklearn import metrics
def train_ANN(saveWeighs=False, saveAlgo=False):
......@@ -64,7 +66,10 @@ def train_ANN(saveWeighs=False, saveAlgo=False):
# plt.grid(True)
# plt.gca().set_ylim(0, 2) # set the vertical range to [0-1]
# plt.show()
predicted_labels = model.predict(X_test)
ConfusionMatrixDisplay.from_predictions(
labelInd_test, predicted_labels, display_labels=classes)
# Save weights to a .h file
list_weights = []
for layer in model.layers:
......@@ -125,5 +130,23 @@ def save_file_as_csv(data, name_folder):
f.write(genres[i][2:-1]+";"+paths[i][2:]+"\n")
def display_confusion_matrix(true_labels, predicted_labels, classes=[1, 0]):
total_classes = len(classes)
level_labels = [total_classes * [0], list(range(total_classes))]
cm = metrics.confusion_matrix(y_true=true_labels, y_pred=predicted_labels,
labels=classes)
cm_frame = pd.DataFrame(data=cm,
columns=pd.MultiIndex(levels=[['Predicted:'], classes],
codes=level_labels),
index=pd.MultiIndex(levels=[['Actual:'], classes],
codes=level_labels))
print(cm_frame)
disp = ConfusionMatrixDisplay(
confusion_matrix=cm, display_labels=true_labels)
disp.plot()
plt.show()
if __name__ == "__main__":
model, history = train_ANN(saveWeighs=True)
......@@ -12,6 +12,17 @@ typedef int (*vFunctionCall)(std::map<FTYPE, DataVector>
typedef std::string (*vFunctionCallCART)(std::map<FTYPE, DataVector>
&features);
int matrix[10][10] = {{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0}};
real compute_accuracy(std::string name_file, vFunctionCall function, int matrix[][10])
{
std::ifstream myFile(name_file);
......@@ -45,21 +56,15 @@ real compute_accuracy(std::string name_file, vFunctionCall function, int matrix[
std::filesystem::path file_path = data[i].second;
std::map<FTYPE, DataVector> features = compute_features_for(file_path);
result_model = function(features);
if (result_model == genres_inv[data[i].first])
result_real = genres_inv[data[i].first];
matrix[result_real][result_model]++;
if (result_model == result_real)
{
compt_good_result++;
}
}
real result = compt_good_result / (real)data.size();
for (size_t i = 0; i < data.size(); i++)
{
std::filesystem::path file_path = data[i].second;
std::map<FTYPE, DataVector> features = compute_features_for(file_path);
result_model = function(features);
result_real = genres_inv[data[i].first];
matrix[result_real][result_model]++;
}
return result;
}
real compute_accuracy_CART(std::string name_file, vFunctionCallCART function, int matrix[][10])
......@@ -94,21 +99,15 @@ real compute_accuracy_CART(std::string name_file, vFunctionCallCART function, in
std::filesystem::path file_path = data[i].second;
std::map<FTYPE, DataVector> features = compute_features_for(file_path);
std::string result_model = function(features);
// std::cout << result_model << ":" << data[i].first << std::endl;
int result_model_int = genres_inv[function(features)];
int result_real = genres_inv[data[i].first];
matrix[result_real][result_model_int]++;
if (result_model == data[i].first)
{
compt_good_result++;
}
}
real result = compt_good_result / (real)data.size();
for (size_t i = 0; i < data.size(); i++)
{
std::filesystem::path file_path = data[i].second;
std::map<FTYPE, DataVector> features = compute_features_for(file_path);
int result_model = genres_inv[function(features)];
int result_real = genres_inv[data[i].first];
matrix[result_real][result_model]++;
}
return result;
}
......
......@@ -40,6 +40,7 @@ from sklearn.tree import export_graphviz, export_text
# ---------------------------------------->
#
def get_metrics(true_labels, predicted_labels):
print('Accuracy:', np.round(
metrics.accuracy_score(true_labels,
......@@ -64,6 +65,7 @@ def get_metrics(true_labels, predicted_labels):
# ---------------------------------------->
#
def train_predict_model(classifier,
train_features, train_labels,
test_features, test_labels):
......@@ -76,6 +78,7 @@ def train_predict_model(classifier,
# ---------------------------------------->
#
def display_confusion_matrix(true_labels, predicted_labels, classes=[1, 0]):
total_classes = len(classes)
level_labels = [total_classes * [0], list(range(total_classes))]
......@@ -88,7 +91,8 @@ def display_confusion_matrix(true_labels, predicted_labels, classes=[1, 0]):
index=pd.MultiIndex(levels=[['Actual:'], classes],
codes=level_labels))
print(cm_frame)
disp = ConfusionMatrixDisplay(confusion_matrix=cm,display_labels=true_labels)
disp = ConfusionMatrixDisplay(
confusion_matrix=cm, display_labels=true_labels)
disp.plot()
plt.show()
......@@ -123,9 +127,8 @@ def display_model_performance_metrics(true_labels, predicted_labels, classes=[1,
#
def train_RF(saveAlgo=False):
count_estimator = 0 #number of estimators processes in the Random Forest
count_estimator = 0 # number of estimators processes in the Random Forest
dataset = "build/features.csv"
......@@ -171,13 +174,13 @@ def train_RF(saveAlgo=False):
# X_val, labelInd_val, cv=5)
# scores_random_f1 = cross_val_score(model,
# X_val, labelInd_val, cv=5, scoring="f1_micro")
# print("\n\n----------------Random forest---------------------------------------------\n")
# display_model_performance_metrics(
# labelInd_test, prediction_random, labelIndices_unique)
# print("Cross validation mean accuracy:", scores_random.mean())
# print("Cross validation std accuracy:", scores_random.std())
# # Looking for optimal parameters (Grid Search method)
# t0 = time()
# param_grid = {
......@@ -195,12 +198,12 @@ def train_RF(saveAlgo=False):
model_best = RandomForestClassifier(
max_depth=20, n_estimators=1000)
prediction_random_best, history_best = train_predict_model(model_best,
X_train, labelInd_train,
X_test, labelInd_test)
X_train, labelInd_train,
X_test, labelInd_test)
scores_random_best = cross_val_score(model_best,
X_val, labelInd_val, cv=5)
X_val, labelInd_val, cv=5)
scores_random_best_f1 = cross_val_score(model_best,
X_val, labelInd_val, cv=5, scoring="f1_micro")
X_val, labelInd_val, cv=5, scoring="f1_micro")
# print("----------------Random Forest---------------------")
# print("Cross validation accuracy moyenne best:", scores_random_best.mean())
# print("Cross validation accuracy moyenne:", scores_random.mean())
......@@ -212,7 +215,7 @@ def train_RF(saveAlgo=False):
# print("Cross validation f1 std:", scores_random_f1.std())
display_model_performance_metrics(
labelInd_test, prediction_random_best, labelIndices_unique)
# print(prediction_random_best)
# print(model_best.apply(X_train))
# print(model_best.decision_path(X_train))
......@@ -228,24 +231,29 @@ def train_RF(saveAlgo=False):
open(file, 'w').close()
with open(file, 'a') as f:
f.write("#ifndef RFTRAINED_H\n#define RFTRAINED_H\n#include <string>\n#include <numeric>\n#include <fstream>\n#include <map>\n#include \"../Helpers/globals.h\"\n#include <typeinfo>\n")
f.write("const int N_ESTIMATORS = "+str(model_best.n_estimators)+";\nconst int MAX_DEPTH = "+str(model_best.max_depth)+";\n")
f.write("int RFModel(int num_estimator, std::map < FTYPE, DataVector > &features){\n")
f.write("DataVector featureVector = features[FTYPE::BINAVG];\nfeatureVector.insert(featureVector.end(), features[FTYPE::BINSTDEV].begin(), features[FTYPE::BINSTDEV].end());\n")
f.write("const int N_ESTIMATORS = "+str(model_best.n_estimators) +
";\nconst int MAX_DEPTH = "+str(model_best.max_depth)+";\n")
f.write(
"float RFModel(int num_estimator, std::map < FTYPE, DataVector > &features){\n")
f.write(
"DataVector featureVector = features[FTYPE::BINAVG];\nfeatureVector.insert(featureVector.end(), features[FTYPE::BINSTDEV].begin(), features[FTYPE::BINSTDEV].end());\n")
f.close()
for e in model_best.estimators_:
# Write in cpp header file RFTrainedcpp.h
with open(file, 'a') as f:
text = ""
text = str_tree(text, e, model_best.n_estimators, 0, 1)
f.write("\nif(num_estimator == "+str(count_estimator)+"){\n" + text + "}\n")
f.write("\nif(num_estimator == " +
str(count_estimator)+"){\n" + text + "}\n")
f.close()
count_estimator+=1
count_estimator += 1
with open(file, 'a') as f:
f.write("return 0;}\n#endif")
f.close()
print("Save algo in embedded-machine-learning/RF/RFTrained.h file!")
return model_best, history_best
def compute_depth(tree, node):
"""
Returns the depth of the subtree rooted in node.
......@@ -265,7 +273,8 @@ def compute_depth(tree, node):
compute_depth_(node, 1, tree.children_left, tree.children_right, depths)
return max(depths)
def str_tree(text,decision_tree, max_depth, node, depth):
def str_tree(text, decision_tree, max_depth, node, depth):
tree_ = decision_tree.tree_
class_names = decision_tree.classes_
value = None
......@@ -282,10 +291,12 @@ def str_tree(text,decision_tree, max_depth, node, depth):
if depth <= max_depth+1:
# print("hello")
if tree_.children_right[node] != tree_.children_left[node]:
left = str_tree(text,decision_tree, max_depth, tree_.children_left[node], depth+1)
right = str_tree(text,decision_tree, max_depth, tree_.children_right[node], depth+1)
left = str_tree(text, decision_tree, max_depth,
tree_.children_left[node], depth+1)
right = str_tree(text, decision_tree, max_depth,
tree_.children_right[node], depth+1)
secondeLayerIF = "if(" + "featureVector["+str(tree_.feature[node])+"]" + "<="+str(tree_.threshold[node]) + \
"){\n"+ str(left) + "}\n"
"){\n" + str(left) + "}\n"
secondeLayerELSE = "else{\n"+str(right)+"}\n"
text += secondeLayerIF + secondeLayerELSE
print(text)
......@@ -301,4 +312,3 @@ def str_tree(text,decision_tree, max_depth, node, depth):
if __name__ == "__main__":
model, history = train_RF(saveAlgo=False)
......@@ -58,32 +58,9 @@ def trainSVM(saveWeighs=False, saveAlgo=False):
std = transformListToStr(std)
# Write our model
if saveWeighs:
with open('/home/hugo/Documents/embedded-machine-learning/SVM/SVMWeight.h', 'w') as f:
f.write("#ifndef SVMWEIGHT_H\n#define SVMWEIGHT_H\n")
f.write("#include <vector>\n")
f.write("const std::vector<DataVector> coefs=" +
transformArrayToStr(coef)+";\n")
f.write("const DataVector intercept=" +
transformListToStr(intercept)+";\n")
f.write("const DataVector meanNorm=" + str(mean)+";\n")
f.write("const DataVector stdNorm=" + str(std)+";\n")
f.write("#endif")
print("Save weights in embedded-machine-learning/SVM/SVMWeight.h file!")
write_weights(coef, intercept, mean, std)
if saveAlgo:
with open('/home/hugo/Documents/embedded-machine-learning/SVM/apply_SVM_algo.h', 'w') as f:
f.write("#ifndef APPLYSVMALGO_H\n#define APPLYSVMALGO_H\n#include <numeric>\n#include <fstream>\n#include \"../Extraction/features_extraction.h\"\n#include \"../Helpers/globals.h\"\n#include \"../Helpers/signal.h\"\n#include \"../Helpers/au_reading.h\"\n#include <typeinfo>\n")
f.write(
"int SVMModelAlgo(std::map < FTYPE, DataVector > &features){")
f.write("const DataVector meanNorm=" + str(mean)+";")
f.write("const DataVector stdNorm=" + str(std)+";")
f.write(
"DataVector featureVector = features[FTYPE::BINAVG];featureVector.insert(featureVector.end(), features[FTYPE::BINSTDEV].begin(), features[FTYPE::BINSTDEV].end());")
f.write(
"std::transform(featureVector.cbegin(),featureVector.cend(),featureVector.begin(),[&, indexM = -1, indexS = -1](real c) mutable{indexM++;indexS++;return (c - meanNorm[indexM]) / stdNorm[indexS];});")
f.write(writeModel(coef, intercept))
f.write("return nbClass; } #endif")
print("Save algo in embedded-machine-learning/SVM/SVMAlgo.h file!")
writeAlgo(coef, intercept, mean, std)
# PREDICTIONS
Y_pred = svm_clf.predict(X_test)
......@@ -114,16 +91,46 @@ def transformListToStr(list):
return text
def writeModel(coef, intercept):
txt = "int nbClass=10;real hyperplanResult;for (size_t i=0; i < featureVector.size(); i++){"
for i in range(np.shape(coef)[0]):
t1 = "hyperplanResult =0."
for j in range(np.shape(coef)[1]):
t1 += "+("+str(coef[i, j])+")*featureVector["+str(j)+"]"
t1 += "+("+str(intercept[i]) + ");"
txt += t1 + "if (hyperplanResult >= 0){nbClass =" + str(i)+";break;}"
txt += "}"
return txt
def writeAlgo(coef, intercept, mean, std):
with open('/home/hugo/Documents/embedded-machine-learning/SVM/apply_SVM_algo.h', 'w') as f:
# Header part
f.write("#ifndef APPLYSVMALGO_H\n#define APPLYSVMALGO_H\n#include <numeric>\n#include <fstream>\n#include \"../Extraction/features_extraction.h\"\n#include \"../Helpers/globals.h\"\n#include \"../Helpers/signal.h\"\n#include \"../Helpers/au_reading.h\"\n#include <typeinfo>\n")
f.write(
"int SVMModelAlgo(std::map < FTYPE, DataVector > &features){")
# Normalization part
f.write("const DataVector meanNorm=" + str(mean)+";")
f.write("const DataVector stdNorm=" + str(std)+";")
f.write(
"DataVector featureVector = features[FTYPE::BINAVG];featureVector.insert(featureVector.end(), features[FTYPE::BINSTDEV].begin(), features[FTYPE::BINSTDEV].end());")
f.write(
"std::transform(featureVector.cbegin(),featureVector.cend(),featureVector.begin(),[&, indexM = -1, indexS = -1](real c) mutable{indexM++;indexS++;return (c - meanNorm[indexM]) / stdNorm[indexS];});")
# SVM part
txt = "int nbClass=-1\n;real hyperplanResult\n;DataVector hyperplans\n;for (size_t i=0; i < featureVector.size(); i++){"
for i in range(np.shape(coef)[0]):
t1 = "\nhyperplanResult =0."
for j in range(np.shape(coef)[1]):
t1 += "+("+str(coef[i, j])+")*featureVector["+str(j)+"]"
t1 += "+("+str(intercept[i]) + ");"
txt += t1 + "hyperplans.push_back(hyperplanResult);\n"
txt += "auto min = std::max_element(std::begin(hyperplans), std::end(hyperplans));\n"
txt += "for (size_t i = 0; i < hyperplans.size(); i++)\n{if (*min == hyperplans[i])\n{nbClass = i;\n}\n}\nreturn nbClass;}"
f.write(txt)
f.write("\n}\n#endif")
print("Save algo in embedded-machine-learning/SVM/SVMAlgo.h file!")
def write_weights(coef, intercept, mean, std):
with open('/home/hugo/Documents/embedded-machine-learning/SVM/SVMWeight.h', 'w') as f:
f.write("#ifndef SVMWEIGHT_H\n#define SVMWEIGHT_H\n")
f.write("#include <vector>\n")
f.write("const std::vector<DataVector> coefs=" +
transformArrayToStr(coef)+";\n")
f.write("const DataVector intercept=" +
transformListToStr(intercept)+";\n")
f.write("const DataVector meanNorm=" + str(mean)+";\n")
f.write("const DataVector stdNorm=" + str(std)+";\n")
f.write("#endif")
print("Save weights in embedded-machine-learning/SVM/SVMWeight.h file!")
def save_file_as_csv(data, name_folder):
......
This diff is collapsed.
This source diff could not be displayed because it is too large. You can view the blob instead.
......@@ -6,7 +6,7 @@
#include "SVMWeight.h"
#include <typeinfo>
#include <map>
#include <iostream>
int SVMModelWeights(std::map<FTYPE, DataVector>
&features)
{
......@@ -27,15 +27,21 @@ int SVMModelWeights(std::map<FTYPE, DataVector>
return (c - meanFeature[indexM]) / stdFeature[indexS];
});
// SVM
int nbClass = 0;
int nbClass = -1;
real hyperplanResult;
DataVector hyperplans;
for (size_t i = 0; i < coefs.size(); i++)
{
// foreach class ie ligne coef avec intercep (ordonee)
real hyperplanResult = std::inner_product(coefs[i].begin(), coefs[i].end(), featureVector.begin(), 0.) + intercept[i];
if (hyperplanResult >= 0)
hyperplanResult = std::inner_product(coefs[i].begin(), coefs[i].end(), featureVector.begin(), 0.) + intercept[i];
hyperplans.push_back(hyperplanResult);
}
auto min = std::max_element(std::begin(hyperplans), std::end(hyperplans));
for (size_t i = 0; i < hyperplans.size(); i++)
{
if (*min == hyperplans[i])
{
nbClass = i;
break;
}
}
return nbClass;
......
......@@ -22,17 +22,8 @@ int main(int argc, char **argv)
int result3 = SVMModelAlgo(features);
std::cout << genres[result3] << std::endl;
std::cout << "Compute accuracy... \n";
int matrix[10][10] = {{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0}};
real accuracy = compute_accuracy("/home/hugo/Documents/embedded-machine-learning/SVM/file_test.csv", (vFunctionCall)SVMModelWeights, matrix);
real accuracy = compute_accuracy("/home/hugo/Documents/embedded-machine-learning/SVM/file_test.csv", (vFunctionCall)SVMModelAlgo, matrix);
std::cout << "Accuracy: " << accuracy << "\n";
std::cout << "Confusion matrix: \n";
print_2D_array(matrix);
......
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