Commit 3ca53cd0 authored by hugopiq's avatar hugopiq
Browse files

SVM ok

parent bde43050
......@@ -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;
}
......
......@@ -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.
......@@ -29,30 +29,21 @@ int SVMModelWeights(std::map<FTYPE, DataVector>
// SVM
int nbClass = -1;
real hyperplanResult;
// DataVector hyperplans;
DataVector hyperplans;
for (size_t i = 0; i < coefs.size(); i++)
{
// foreach class ie ligne coef avec intercep (ordonee)
hyperplanResult = std::inner_product(coefs[i].begin(), coefs[i].end(), featureVector.begin(), 0.) + intercept[i];
// std::cout << hyperplanResult << std::endl;
if (hyperplanResult >= 0.)
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;
}
// hyperplans.push_back(std::abs(hyperplanResult));
}
// auto min = std::min_element(std::begin(hyperplans), std::end(hyperplans));
// for (size_t i = 0; i < hyperplans.size(); i++)
// {
// if (*min == hyperplans[i])
// {
// std::cout << i << std::endl;
// return i;
// }
// }
std::cout << nbClass << std::endl;
return nbClass;
}
#endif
\ No newline at end of file
......@@ -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