Commit d82623ef authored by hugopiq's avatar hugopiq
Browse files

modif path

parent 304e5092
This diff is collapsed.
......@@ -17,16 +17,12 @@ public:
void print() const;
private:
const std::vector<DataVector> _w0;
const std::vector<DataVector> _w1;
const std::vector<DataVector> _w2;
const std::vector<DataVector> _w3;
const std::map<std::vector<DataVector>, DataVector> _layer1;
const std::map<std::vector<DataVector>, DataVector> _layer2;
const std::map<std::vector<DataVector>, DataVector> _layer3;
const std::map<std::vector<DataVector>, DataVector> _layer4;
const DataVector _stdNorm;
const DataVector _meanNorm;
const DataVector _b0;
const DataVector _b1;
const DataVector _b2;
const DataVector _b3;
};
// definition au sein de la classe (inline)
inline int ANN::predict(std::map<FTYPE, DataVector>
......@@ -47,13 +43,11 @@ inline int ANN::predict(std::map<FTYPE, DataVector>
});
return 0;
}
ANN::ANN() : _w0(w0), _w1(w1), _w2(w2), _w3(w3), _stdNorm(stdNorm), _meanNorm(meanNorm), _b0(b0), _b1(b1), _b2(b2), _b3(b3)
ANN::ANN() : _stdNorm(stdNorm), _meanNorm(meanNorm), _layer1(w0, b0),
{
}
#endif
// Complexite algo
// Finir Object
// Get better hyperparameters
// Comparaison sklearn Cart qui ne marche pas
\ No newline at end of file
// Get better hyperparameters
\ No newline at end of file
......@@ -12,8 +12,8 @@ from sklearn.metrics import ConfusionMatrixDisplay
from sklearn.metrics import ConfusionMatrixDisplay, confusion_matrix
def train_ANN(saveWeighs=False, saveAlgo=False):
dataset = "build/Extraction/features.csv"
def train_ANN(saveWeighs=False, saveAlgo=False, path=''):
dataset = path+"build/Extraction/features.csv"
df = pd.read_csv(dataset, header=0)
features = df.columns.values[:-2]
Y = df.Style.values
......@@ -30,7 +30,7 @@ def train_ANN(saveWeighs=False, saveAlgo=False):
new_df = pd.DataFrame(X_test)
files = new_df.iloc[:, -2:]
files.columns = ["Classes", "Paths"]
save_file_as_csv(files, "ANN")
save_file_as_csv(files, "ANN", path)
X_test = np.delete(X_test, [-1, -2], axis=1)
# encodage indiciel des classes
encName2Ind = preprocessing.LabelEncoder()
......@@ -84,12 +84,12 @@ def train_ANN(saveWeighs=False, saveAlgo=False):
mean = transformListToStr(mean)
std = transformListToStr(std)
if saveWeighs:
writeWreighs(list_weights, mean, std)
writeWreighs(list_weights, mean, std, path)
return model, history
def writeWreighs(list_weights, mean, std):
with open('/home/hugo/Documents/embedded-machine-learning/ANN/ANNWeight.h', 'w') as f:
def writeWreighs(list_weights, mean, std, path):
with open(path+'ANN/ANNWeight.h', 'w') as f:
f.write(
"#ifndef ANNWEIGHT_H\n#define ANNWEIGHT_H\n#include \"../Helpers/globals.h\"\n#include <vector>\n")
for i in range(len(list_weights)):
......@@ -100,7 +100,7 @@ def writeWreighs(list_weights, mean, std):
f.write("const DataVector meanNorm=" + str(mean)+";")
f.write("const DataVector stdNorm=" + str(std)+";")
f.write("\n#endif")
print("Save weights in embedded-machine-learning/ANN/ANNWeight.h file!")
print("Save weights in "+path+"/ANN/ANNWeight.h file!")
def transformArrayToStr(array):
......@@ -123,15 +123,16 @@ def transformListToStr(list):
return text
def save_file_as_csv(data, name_folder):
def save_file_as_csv(data, name_folder, path):
genres = data["Classes"].tolist()
paths = data["Paths"].tolist()
print(genres)
with open('/home/hugo/Documents/embedded-machine-learning/' +
with open(path +
name_folder+'/file_test.csv', 'w') as f:
for i in range(len(genres)):
f.write(genres[i][2:-1]+";"+paths[i][2:]+"\n")
if __name__ == "__main__":
model, history = train_ANN(saveWeighs=True)
path = ''
model, history = train_ANN(saveWeighs=True, path=path)
......@@ -19,7 +19,7 @@ int main(int argc, char **argv)
std::cout << "Predicted genre (version weight saved): " << genres[result] << " with " << duration.count() << " microseconds" << std::endl;
}
std::cout << "Compute accuracy... \n";
real accuracy = compute_accuracy("/home/hugo/Documents/embedded-machine-learning/ANN/file_test.csv", (vFunctionCall)ANNModelWeights, matrix);
real accuracy = compute_accuracy("../../ANN/file_test.csv", (vFunctionCall)ANNModelWeights, matrix);
std::cout << "Accuracy: " << accuracy << "\n";
std::cout << "Confusion matrix: \n";
print_2D_array(matrix);
......
......@@ -9,7 +9,12 @@ import graphviz
from CART import CartTree
def trainCart(comparisonSKLearn=False):
"""
File to launch from racine or change path
"""
def trainCart(comparisonSKLearn=False, path=""):
dataset = "build/Extraction/features.csv"
df = pd.read_csv(dataset, header=0)
features = df.columns.values[:-2]
......@@ -21,41 +26,26 @@ def trainCart(comparisonSKLearn=False):
X, Y, test_size=0.33, random_state=42)
X_test, X_val, Y_test, Y_val = train_test_split(
X_test, Y_test, test_size=0.33, random_state=42)
X_train = np.delete(X_train, [-1, -2], axis=1)
X_val = np.delete(X_val, [-1, -2], axis=1)
new_df = pd.DataFrame(X_test)
files = new_df.iloc[:, -2:]
files.columns = ["Classes", "Paths"]
save_file_as_csv(files, "CART")
print("Splitting test, train", X_train.shape, X_test.shape)
save_file_as_csv(files, "CART", path)
X_test = np.delete(X_test, [-1, -2], axis=1)
# My CART_TREES TREE
print("Training !")
myclassifier = CartTree(max_depth=12)
myclassifier.fit(X_train, Y_train)
# Write in cpp header file CARTTrainedcpp.h
write_model(myclassifier)
write_model(myclassifier, path)
# print()
# Comparison with SKLearn:
if comparisonSKLearn:
# SKLEARN CART_TREES TREE
clf = tree.DecisionTreeClassifier(random_state=None, max_depth=12)
clf = clf.fit(X_train, Y_train)
# dot_data = tree.export_graphviz(clf,
# out_file=None,
# feature_names=features,
# class_names=classes,
# filled=True,
# rounded=True,
# special_characters=True)
# graph = graphviz.Source(dot_data)
# graph.render("rCrop recommandations")
# PREDICTIONS
Y_pred = myclassifier.predict(X_test)
print("accuracy :", accuracy_score(Y_test, Y_pred))
......@@ -65,32 +55,33 @@ def trainCart(comparisonSKLearn=False):
plt.title("My predictions")
plt.show()
# Y_pred = clf.predict(X_test)
# print("accuracy :", accuracy_score(Y_test, Y_pred))
# ConfusionMatrixDisplay.from_predictions(
# Y_test, Y_pred, display_labels=classes)
# plt.title("SKLEARN")
# plt.show()
Y_pred = clf.predict(X_test)
print("accuracy :", accuracy_score(Y_test, Y_pred))
ConfusionMatrixDisplay.from_predictions(
Y_test, Y_pred, display_labels=classes)
plt.title("SKLEARN")
plt.show()
def write_model(classifier):
def write_model(classifier, path):
text = str(classifier)
with open('/home/hugo/Documents/embedded-machine-learning/CART/CARTTrained.h', 'w') as f:
with open(path+'CART/CARTTrained.h', 'w') as f:
f.write(" # ifndef CARTTRAINED_H\n#define CARTTRAINED_H\n#include <string>\n#include <numeric>\n#include <fstream>\n#include <map>\n#include \"../Helpers/globals.h\"\n#include <typeinfo>\n")
f.write(
"std::string cartModel(std::map < FTYPE, DataVector > &features){" + "DataVector featureVector = features[FTYPE::BINAVG];featureVector.insert(featureVector.end(), features[FTYPE::BINSTDEV].begin(), features[FTYPE::BINSTDEV].end());"+text + "return 0;}\n#endif")
print("Save algo in embedded-machine-learning/CART/CARTTrained.h file!")
print("Save algo in"+path + "/CART/CARTTrained.h file!")
def save_file_as_csv(data, name_folder):
def save_file_as_csv(data, name_folder, path):
genres = data["Classes"].tolist()
paths = data["Paths"].tolist()
print(genres)
with open('/home/hugo/Documents/embedded-machine-learning/' +
with open(path +
name_folder+'/file_test.csv', 'w') as f:
for i in range(len(genres)):
f.write(genres[i][2:-1]+";"+paths[i][2:]+"\n")
if __name__ == "__main__":
trainCart(comparisonSKLearn=True)
path = ""
trainCart(comparisonSKLearn=True, path=path)
......@@ -17,7 +17,7 @@ int main(int argc, char **argv)
std::cout << "Predicted genre (version weight saved): " << result2 << " with " << duration.count() << " microseconds" << std::endl;
}
std::cout << "Compute accuracy... \n";
real accuracy = compute_accuracy_CART("/home/hugo/Documents/embedded-machine-learning/CART/file_test.csv", (vFunctionCallCART)cartModel, matrix);
real accuracy = compute_accuracy_CART("../../CART/file_test.csv", (vFunctionCallCART)cartModel, matrix);
std::cout << "Accuracy: " << accuracy << "\n";
std::cout << "Confusion matrix: \n";
print_2D_array(matrix);
......
......@@ -13,7 +13,7 @@ add_subdirectory(Extraction)
# Extraction/*.cpp
# )
add_subdirectory(CART)
# add_subdirectory(RF)
add_subdirectory(RF)
add_subdirectory(SVM)
add_subdirectory(ANN)
set(TBB_SHARED_OBJECT "/usr/local/lib/libtbb.so")
......
......@@ -127,10 +127,9 @@ def display_model_performance_metrics(true_labels, predicted_labels, classes=[1,
#
def train_RF(saveAlgo=False):
def train_RF(saveAlgo=False, path=""):
count_estimator = 0 # number of estimators processes in the Random Forest
dataset = "build/features.csv"
dataset = path+"build/Extraction/features.csv"
df = pd.read_csv(dataset, header=0)
features = df.columns.values[:-2]
Y = df.Style.values
......@@ -147,7 +146,7 @@ def train_RF(saveAlgo=False):
new_df = pd.DataFrame(X_test)
files = new_df.iloc[:, -2:]
files.columns = ["Classes", "Paths"]
save_file_as_csv(files, "RF")
save_file_as_csv(files, "RF", path)
X_test = np.delete(X_test, [-1, -2], axis=1)
# encodage indiciel des classes
......@@ -164,7 +163,6 @@ def train_RF(saveAlgo=False):
X_train = scaler.fit_transform(X_train)
X_val = scaler.fit_transform(X_val)
X_test = scaler.fit_transform(X_test)
print(X_train.shape)
# # Random forest
# model = RandomForestClassifier()
......@@ -230,7 +228,7 @@ def train_RF(saveAlgo=False):
report = export_text(e)
# print(report)
# help(sklearn.tree._tree.Tree)
file = '/home/estellearrc/Documents/Cours/ENSTA_Bretagne_2020_2022/3A_2021_2022/S5/Embedded_machine_learning/embedded-machine-learning/RF/RFTrained.h'
file = path+'RF/RFTrained.h'
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")
......@@ -253,7 +251,7 @@ def train_RF(saveAlgo=False):
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!")
print("Save algo in "+path + "/RF/RFTrained.h file!")
return model_best, history_best
......@@ -304,23 +302,25 @@ def str_tree(text, decision_tree, max_depth, node, depth):
text += secondeLayerIF + secondeLayerELSE
print(text)
else: # leaf
text+= "return " + str(int(class_name))+";\n"
text += "return " + str(int(class_name))+";\n"
else:
subtree_depth = compute_depth(tree_, node)
if subtree_depth == 1:
text+= "return " + str(int(class_name))+";\n"
text += "return " + str(int(class_name))+";\n"
# print(text)
return text
def save_file_as_csv(data, name_folder):
def save_file_as_csv(data, name_folder, path):
genres = data["Classes"].tolist()
paths = data["Paths"].tolist()
print(genres)
with open('/home/estellearrc/Documents/Cours/ENSTA_Bretagne_2020_2022/3A_2021_2022/S5/Embedded_machine_learning/embedded-machine-learning/' +
with open(path +
name_folder+'/file_test.csv', 'w') as f:
for i in range(len(genres)):
f.write(genres[i][2:-1]+";"+paths[i][2:]+"\n")
if __name__ == "__main__":
model, history = train_RF(saveAlgo=False)
path = ''
model, history = train_RF(saveAlgo=False, path=path)
This diff is collapsed.
......@@ -7,26 +7,30 @@
#include <iostream>
#include <vector>
std::string majorityVoting(std::vector<int> results){
std::vector<float> probas(CLASS_N,0.0);
for(int r : results){
probas[r] += 1/N_ESTIMATORS;
std::string majorityVoting(std::vector<int> results)
{
std::vector<float> probas(CLASS_N, 0.0);
for (int r : results)
{
probas[r] += 1 / N_ESTIMATORS;
}
float max = *max_element(probas.begin(), probas.end());
for(int k = 0; k < CLASS_N; k++){
if(probas[k] == max)
for (int k = 0; k < CLASS_N; k++)
{
if (probas[k] == max)
return genres[k];
}
}
std::string RFAlgo(std::map<FTYPE, DataVector> features){
std::string RFAlgo(std::map<FTYPE, DataVector> features)
{
std::vector<int> results;
for(int k = 0; k < N_ESTIMATORS; k++)
for (int k = 0; k < N_ESTIMATORS; k++)
{
results.push_back(RFModel(k, features));
}
std::string result = majorityVoting(results);
std::cout << result << std::endl;
// std::cout << result << std::endl;
return result;
}
......
This diff is collapsed.
......@@ -6,16 +6,13 @@ int main(int argc, char **argv)
if (argc > 1)
{
file_path = argv[1];
std::map<FTYPE, DataVector> features = compute_features_for(file_path);
std::string result = RFAlgo(features);
}
else
{
file_path = "/home/estellearrc/Documents/Cours/ENSTA_Bretagne_2020_2022/3A_2021_2022/S5/Embedded_machine_learning/embedded-machine-learning/DATASETS/genres/disco/disco.00010.au";
}
std::map<FTYPE, DataVector> features = compute_features_for(file_path);
std::string result = RFAlgo(features);
real accuracy = compute_accuracy_CART("/home/estellearrc/Documents/Cours/ENSTA_Bretagne_2020_2022/3A_2021_2022/S5/Embedded_machine_learning/embedded-machine-learning/RF/file_test.csv", (vFunctionCallCART)RFAlgo, matrix);
std::cout << "Compute accuracy... \n";
real accuracy = compute_accuracy_CART("../../RF/file_test.csv", (vFunctionCallCART)RFAlgo, matrix);
std::cout << "Accuracy: " << accuracy << "\n";
// std::cout << "Confusion matrix: \n";
// print_2D_array(matrix);
std::cout << "Confusion matrix: \n";
print_2D_array(matrix);
return 0;
}
\ No newline at end of file
......@@ -11,10 +11,14 @@ import graphviz
from sklearn.metrics import accuracy_score
from sklearn.model_selection import GridSearchCV
"""
File to launch from racine or change path
"""
def trainSVM(saveWeighs=False, saveAlgo=False):
def trainSVM(saveWeighs=False, saveAlgo=False, path=''):
# Get data
dataset = 'build/Extraction/features.csv'
dataset = path+'build/Extraction/features.csv'
df = pd.read_csv(dataset, header=0)
# features = df.columns.values[:-2]
Y = df.Style.values
......@@ -32,14 +36,13 @@ def trainSVM(saveWeighs=False, saveAlgo=False):
new_df = pd.DataFrame(X_test)
files = new_df.iloc[:, -2:]
files.columns = ["Classes", "Paths"]
save_file_as_csv(files, "SVM")
save_file_as_csv(files, "SVM", path)
X_test = np.delete(X_test, [-1, -2], axis=1)
svm_clf = Pipeline([
("scaler", StandardScaler()),
("linear_svc", LinearSVC(C=0.01, loss="squared_hinge")),
])
# Train our
param_grid = {
"n_estimators": [1, 10, 100, 500, 1000],
"max_depth": [5, 10, 20],
......@@ -58,9 +61,9 @@ def trainSVM(saveWeighs=False, saveAlgo=False):
std = transformListToStr(std)
# Write our model
if saveWeighs:
write_weights(coef, intercept, mean, std)
write_weights(coef, intercept, mean, std, path)
if saveAlgo:
writeAlgo(coef, intercept, mean, std)
writeAlgo(coef, intercept, mean, std, path)
# PREDICTIONS
Y_pred = svm_clf.predict(X_test)
......@@ -91,8 +94,8 @@ def transformListToStr(list):
return text
def writeAlgo(coef, intercept, mean, std):
with open('/home/hugo/Documents/embedded-machine-learning/SVM/apply_SVM_algo.h', 'w') as f:
def writeAlgo(coef, intercept, mean, std, path):
with open(path+'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(
......@@ -116,11 +119,11 @@ def writeAlgo(coef, intercept, mean, std):
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!")
print("Save algo in " + path+"/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:
def write_weights(coef, intercept, mean, std, path):
with open(path+'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=" +
......@@ -130,19 +133,19 @@ def write_weights(coef, intercept, mean, std):
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!")
print("Save weights in " + path+"/SVM/SVMWeight.h file!")
def save_file_as_csv(data, name_folder):
def save_file_as_csv(data, name_folder, path):
genres = data["Classes"].tolist()
paths = data["Paths"].tolist()
print(genres)
with open('/home/hugo/Documents/embedded-machine-learning/' +
name_folder+'/file_test.csv', 'w') as f:
with open(path + name_folder+'/file_test.csv', 'w') as f:
for i in range(len(genres)):
f.write(genres[i][2:-1]+";"+paths[i][2:]+"\n")
if __name__ == "__main__":
trainSVM(saveAlgo=True, saveWeighs=True)
path = ""
trainSVM(saveAlgo=True, saveWeighs=True, path=path)
This source diff could not be displayed because it is too large. You can view the blob instead.
This diff is collapsed.
......@@ -25,7 +25,7 @@ int main(int argc, char **argv)
}
std::cout << "Compute accuracy... \n";
real accuracy = compute_accuracy("/home/hugo/Documents/embedded-machine-learning/SVM/file_test.csv", (vFunctionCall)SVMModelAlgo, matrix);
real accuracy = compute_accuracy("../../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