Commit cf3b095b authored by estellearrc's avatar estellearrc
Browse files

RF saved

parent e1d1af85
......@@ -124,8 +124,9 @@ def display_model_performance_metrics(true_labels, predicted_labels, classes=[1,
def train_RF(saveWeighs=False, saveAlgo=False):
# dataset = r'features.csv'
def train_RF(saveAlgo=False):
count_estimator = 0 #number of estimators processes in the Random Forest
dataset = "build/features.csv"
crop_df = pd.read_csv(dataset, header=0)
......@@ -222,27 +223,80 @@ def train_RF(saveWeighs=False, saveAlgo=False):
# print(e.n_outputs_)
report = export_text(e)
# print(report)
# help(sklearn.tree._tree.Tree)
# Write in cpp header file CARTTrainedcpp.h
# text = str(model_best)
# with open('/home/estellearrc/Documents/Cours/ENSTA_Bretagne_2020_2022/3A_2021_2022/S5/Embedded_machine_learning/embedded-machine-learning/RF/RFTrained.h', 'w') 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(
# "std::string RFModel(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/RF/RFTrained.h file!")
file = '/home/estellearrc/Documents/Cours/ENSTA_Bretagne_2020_2022/3A_2021_2022/S5/Embedded_machine_learning/embedded-machine-learning/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")
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.close()
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 __str__(tree):
if tree.left == tree.right == None:
return "return " + str(tree.label)+" ;"
def compute_depth(tree, node):
"""
Returns the depth of the subtree rooted in node.
"""
def compute_depth_(current_node, current_depth,
children_left, children_right, depths):
depths += [current_depth]
left = children_left[current_node]
right = children_right[current_node]
if left != -1 and right != -1:
compute_depth_(left, current_depth+1,
children_left, children_right, depths)
compute_depth_(right, current_depth+1,
children_left, children_right, depths)
depths = []
compute_depth_(node, 1, tree.children_left, tree.children_right, depths)
return max(depths)
def str_tree(text,decision_tree, max_depth, node, depth):
tree_ = decision_tree.tree_
class_names = decision_tree.classes_
value = None
if tree_.n_outputs == 1:
value = tree_.value[node][0]
else:
value = tree_.value[node].T[0]
class_name = np.argmax(value)
if (tree_.n_classes[0] != 1 and
tree_.n_outputs == 1):
class_name = class_names[class_name]
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)
secondeLayerIF = "if(" + "featureVector["+str(tree_.feature[node])+"]" + "<="+str(tree_.threshold[node]) + \
"){\n"+ str(left) + "}\n"
secondeLayerELSE = "else{\n"+str(right)+"}\n"
text += secondeLayerIF + secondeLayerELSE
print(text)
else: # leaf
text+= "return " + str(class_name)+";\n"
else:
secondeLayerIF = "if(" + "featureVector["+str(tree.feature)+"]" + "<="+str(tree.threshold) + \
"){"+str(tree.left.__str__()) + "}"
secondeLayerELSE = "else{"+str(tree.right.__str__())+"}"
return secondeLayerIF + secondeLayerELSE
subtree_depth = compute_depth(tree_, node)
if subtree_depth == 1:
text+= "return " + str(class_name)+";\n"
# print(text)
return text
def transformArrayToStr(array):
array = array.T
......@@ -271,4 +325,5 @@ def save_file_as_csv(data, name_folder):
if __name__ == "__main__":
model, history = train_RF(saveAlgo=False, saveWeighs=True)
model, history = train_RF(saveAlgo=False)
This diff is collapsed.
// HEADER=all_headers.h
// echo "#ifndef __ALL_HEADERS__" > $HEADER
// echo "#define __ALL_HEADERS__" >> $HEADER
// for file in 'dir/*.h'
// do
// echo "#include <$file>" >> $HEADER
// done
// echo "#endif" >> $HEADER
#include "RFTrained.h"
#include "../Extraction/features_extraction.cpp"
#include <string>
......
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