Commit c957b63c authored by estellearrc's avatar estellearrc
Browse files
parents c742ae02 fa71dc58
This diff is collapsed.
......@@ -13,23 +13,25 @@ from matplotlib import pyplot as plt
def train_ANN(saveWeighs=False, saveAlgo=False):
# dataset = r'features.csv'
dataset = "build/Extraction/features.csv"
crop_df = pd.read_csv(dataset, header=0)
Y = crop_df.Style.values
files = crop_df.get(['Style', 'FileName'])
# WARNING get only test file!
save_file_as_csv(files, "ANN")
crop_df = crop_df.drop(['Style', 'FileName'], axis=1)
X = crop_df.values
df = pd.read_csv(dataset, header=0)
features = df.columns.values[:-2]
# print(features)
Y = df.Style.values
X = df.values
classes = np.unique(Y)
# print(classes)
# TRAIN/TEST SPLIT
X_train, X_test, Y_train, Y_test = train_test_split(
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)
print(X_test)
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, "ANN")
X_test = np.delete(X_test, [-1, -2], axis=1)
# encodage indiciel des classes
encName2Ind = preprocessing.LabelEncoder()
encName2Ind.fit(classes)
......@@ -44,9 +46,8 @@ def train_ANN(saveWeighs=False, 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)
#Validation+ accuracy
# Validation+ accuracy
model = keras.models.Sequential([keras.layers.Dense(units=512, activation='relu'), keras.layers.Dense(
units=64, activation='relu'), keras.layers.Dense(units=32, activation='relu'), keras.layers.Dense(units=10, activation='softmax')])
......@@ -134,8 +135,13 @@ def transformListToStr(list):
def save_file_as_csv(data, name_folder):
data.to_csv('/home/hugo/Documents/embedded-machine-learning/' +
name_folder+'/file_test.csv', index=False)
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:
for i in range(len(genres)):
f.write(genres[i][2:-1]+";"+paths[i][2:]+"\n")
if __name__ == "__main__":
......
......@@ -5,6 +5,7 @@
#include "ANNWeight.h"
#include <typeinfo>
#include <map>
#include <iostream>
void softmaxF(DataVector &X)
{
......
This diff is collapsed.
#include "../Extraction/features_extraction.cpp"
// #include "../Extraction/features_extraction.cpp"
#include "apply_ANN_weights.h"
#include "../Helpers/evaluate_model.h"
#include <string>
......@@ -17,7 +17,9 @@ int main(int argc, char **argv)
}
std::map<FTYPE, DataVector> features = compute_features_for(file_path);
int result = ANNModelWeights(features);
std::cout << genres[result];
real accuracy = compute_accuracy("file_test.csv", std::function(ANNModelWeights));
std::cout << "Style: " << genres[result] << "\n";
std::cout << "Compute accuracy... \n";
real accuracy = compute_accuracy("/home/hugo/Documents/embedded-machine-learning/ANN/file_test.csv", (vFunctionCall)ANNModelWeights);
std::cout << "Accuracy: " << accuracy << "\n";
return 0;
}
\ No newline at end of file
......@@ -6,53 +6,48 @@
#include <iostream>
#include "../Helpers/globals.h"
real compute_accuracy(std::string name_file, std::function<int(std::map<FTYPE, DataVector>)> function)
typedef int (*vFunctionCall)(std::map<FTYPE, DataVector>
&features);
real compute_accuracy(std::string name_file, vFunctionCall function)
{
std::ifstream myFile(name_file);
real result;
std::vector<std::pair<std::string, std::string>> data;
if (!myFile.is_open())
throw std::runtime_error("Could not open file");
std::string line, colname;
std::string line, line2;
std::string val;
// Read each line
int index = 0;
while (std::getline(myFile, line))
{
if (index != 0)
{
// Create a stringstream of the current line
std::stringstream ss(line);
std::pair<std::string, std::string> pair;
// Keep track of the current column index
int colIdx = 0;
// Extract each integer
ss >> val;
pair.first = val; // paths file
ss >> val; // "," ignore
ss >> val;
pair.second = val; // paths file
data.push_back(pair);
index++;
}
std::pair<std::string, std::string> pair;
std::stringstream s(line);
std::getline(s, line2, ';');
std::stringstream ss(line2);
ss >> val;
pair.first = val; // style
std::getline(s, line2, ';');
std::stringstream ss2(line2);
ss2 >> val;
pair.second = val; // paths file
data.push_back(pair);
}
myFile.close();
int compt_good_result = 0;
real compt_good_result = 0.;
for (size_t i = 0; i < data.size(); i++)
{
std::filesystem::path file_path = data[i].first;
std::filesystem::path file_path = data[i].second;
std::map<FTYPE, DataVector> features = compute_features_for(file_path);
std::string result_model = genres[function(features)];
if (result_model == data[i].second)
// std::cout << result_model << " " << data[i].first << std::endl;
if (result_model == data[i].first)
{
compt_good_result++;
}
}
result = compt_good_result / data.size();
real result = compt_good_result / (real)data.size();
return result;
}
#endif
\ No newline at end of file
......@@ -15,19 +15,25 @@ from sklearn.model_selection import GridSearchCV
def trainSVM(saveWeighs=False, saveAlgo=False):
dataset = 'build/Extraction/features.csv'
crop_df = pd.read_csv(dataset, header=0)
Y = crop_df.Style.values
features = crop_df.columns.values[:-2]
crop_df = crop_df.drop(['Style', 'FileName'], axis=1)
X = crop_df.values
df = pd.read_csv(dataset, header=0)
features = df.columns.values[:-2]
# print(features)
Y = df.Style.values
X = df.values
classes = np.unique(Y)
# TRAIN/TEST SPLIT
X_train, X_test, Y_train, Y_test = train_test_split(
X, Y, test_size=0.33, random_state=42)
X_test, X_valid, Y_test, Y_valid = train_test_split(
X_test, X_val, Y_test, Y_val = train_test_split(
X_test, Y_test, test_size=0.33, random_state=42)
print("Splitting test, train", X_train.shape, X_test.shape)
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, "SVM")
X_test = np.delete(X_test, [-1, -2], axis=1)
svm_clf = Pipeline([
("scaler", StandardScaler()),
......@@ -39,9 +45,9 @@ def trainSVM(saveWeighs=False, saveAlgo=False):
"max_depth": [5, 10, 20],
"min_samples_leaf": [1, 5, 10]
}
clf = GridSearchCV(svm_clf, param_grid)
clf = clf.fit(X_valid, Y_valid)
print("Best estimateur:", clf.best_estimator_)
# clf = GridSearchCV(svm_clf, param_grid)
# clf = clf.fit(X_valid, Y_valid)
# print("Best estimateur:", clf.best_estimator_)
# Compute better parameters
svm_clf.fit(X_train, Y_train)
coef = svm_clf.named_steps['linear_svc'].coef_
......@@ -120,5 +126,16 @@ def writeModel(coef, intercept):
return txt
def save_file_as_csv(data, name_folder):
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:
for i in range(len(genres)):
f.write(genres[i][2:-1]+";"+paths[i][2:]+"\n")
if __name__ == "__main__":
trainSVM(saveAlgo=False, saveWeighs=False)
trainSVM(saveAlgo=True, saveWeighs=True)
This diff is collapsed.
This diff is collapsed.
metal;../../DATASETS/genres/metal/metal.00035.au
hiphop;../../DATASETS/genres/hiphop/hiphop.00029.au
jazz;../../DATASETS/genres/jazz/jazz.00006.au
rock;../../DATASETS/genres/rock/rock.00022.au
metal;../../DATASETS/genres/metal/metal.00089.au
metal;../../DATASETS/genres/metal/metal.00097.au
metal;../../DATASETS/genres/metal/metal.00062.au
reggae;../../DATASETS/genres/reggae/reggae.00036.au
pop;../../DATASETS/genres/pop/pop.00061.au
jazz;../../DATASETS/genres/jazz/jazz.00081.au
hiphop;../../DATASETS/genres/hiphop/hiphop.00030.au
disco;../../DATASETS/genres/disco/disco.00019.au
metal;../../DATASETS/genres/metal/metal.00068.au
jazz;../../DATASETS/genres/jazz/jazz.00022.au
disco;../../DATASETS/genres/disco/disco.00007.au
jazz;../../DATASETS/genres/jazz/jazz.00043.au
blues;../../DATASETS/genres/blues/blues.00060.au
metal;../../DATASETS/genres/metal/metal.00095.au
jazz;../../DATASETS/genres/jazz/jazz.00090.au
pop;../../DATASETS/genres/pop/pop.00064.au
blues;../../DATASETS/genres/blues/blues.00088.au
pop;../../DATASETS/genres/pop/pop.00007.au
blues;../../DATASETS/genres/blues/blues.00070.au
jazz;../../DATASETS/genres/jazz/jazz.00027.au
jazz;../../DATASETS/genres/jazz/jazz.00084.au
disco;../../DATASETS/genres/disco/disco.00033.au
reggae;../../DATASETS/genres/reggae/reggae.00066.au
pop;../../DATASETS/genres/pop/pop.00017.au
country;../../DATASETS/genres/country/country.00013.au
jazz;../../DATASETS/genres/jazz/jazz.00059.au
classical;../../DATASETS/genres/classical/classical.00099.au
rock;../../DATASETS/genres/rock/rock.00098.au
pop;../../DATASETS/genres/pop/pop.00083.au
hiphop;../../DATASETS/genres/hiphop/hiphop.00008.au
jazz;../../DATASETS/genres/jazz/jazz.00087.au
reggae;../../DATASETS/genres/reggae/reggae.00010.au
disco;../../DATASETS/genres/disco/disco.00006.au
reggae;../../DATASETS/genres/reggae/reggae.00022.au
reggae;../../DATASETS/genres/reggae/reggae.00096.au
hiphop;../../DATASETS/genres/hiphop/hiphop.00062.au
pop;../../DATASETS/genres/pop/pop.00098.au
reggae;../../DATASETS/genres/reggae/reggae.00000.au
rock;../../DATASETS/genres/rock/rock.00001.au
country;../../DATASETS/genres/country/country.00027.au
metal;../../DATASETS/genres/metal/metal.00013.au
jazz;../../DATASETS/genres/jazz/jazz.00036.au
rock;../../DATASETS/genres/rock/rock.00085.au
country;../../DATASETS/genres/country/country.00008.au
rock;../../DATASETS/genres/rock/rock.00078.au
rock;../../DATASETS/genres/rock/rock.00049.au
pop;../../DATASETS/genres/pop/pop.00028.au
classical;../../DATASETS/genres/classical/classical.00055.au
metal;../../DATASETS/genres/metal/metal.00070.au
classical;../../DATASETS/genres/classical/classical.00065.au
rock;../../DATASETS/genres/rock/rock.00041.au
hiphop;../../DATASETS/genres/hiphop/hiphop.00095.au
pop;../../DATASETS/genres/pop/pop.00040.au
hiphop;../../DATASETS/genres/hiphop/hiphop.00032.au
jazz;../../DATASETS/genres/jazz/jazz.00026.au
reggae;../../DATASETS/genres/reggae/reggae.00068.au
reggae;../../DATASETS/genres/reggae/reggae.00092.au
hiphop;../../DATASETS/genres/hiphop/hiphop.00065.au
disco;../../DATASETS/genres/disco/disco.00059.au
hiphop;../../DATASETS/genres/hiphop/hiphop.00045.au
country;../../DATASETS/genres/country/country.00092.au
rock;../../DATASETS/genres/rock/rock.00047.au
blues;../../DATASETS/genres/blues/blues.00067.au
jazz;../../DATASETS/genres/jazz/jazz.00023.au
rock;../../DATASETS/genres/rock/rock.00059.au
metal;../../DATASETS/genres/metal/metal.00077.au
classical;../../DATASETS/genres/classical/classical.00037.au
rock;../../DATASETS/genres/rock/rock.00095.au
blues;../../DATASETS/genres/blues/blues.00002.au
country;../../DATASETS/genres/country/country.00099.au
pop;../../DATASETS/genres/pop/pop.00078.au
metal;../../DATASETS/genres/metal/metal.00092.au
blues;../../DATASETS/genres/blues/blues.00033.au
jazz;../../DATASETS/genres/jazz/jazz.00048.au
disco;../../DATASETS/genres/disco/disco.00055.au
disco;../../DATASETS/genres/disco/disco.00098.au
reggae;../../DATASETS/genres/reggae/reggae.00084.au
metal;../../DATASETS/genres/metal/metal.00036.au
classical;../../DATASETS/genres/classical/classical.00098.au
rock;../../DATASETS/genres/rock/rock.00002.au
disco;../../DATASETS/genres/disco/disco.00061.au
pop;../../DATASETS/genres/pop/pop.00021.au
jazz;../../DATASETS/genres/jazz/jazz.00044.au
reggae;../../DATASETS/genres/reggae/reggae.00049.au
blues;../../DATASETS/genres/blues/blues.00078.au
reggae;../../DATASETS/genres/reggae/reggae.00079.au
hiphop;../../DATASETS/genres/hiphop/hiphop.00036.au
blues;../../DATASETS/genres/blues/blues.00077.au
reggae;../../DATASETS/genres/reggae/reggae.00002.au
country;../../DATASETS/genres/country/country.00060.au
blues;../../DATASETS/genres/blues/blues.00096.au
disco;../../DATASETS/genres/disco/disco.00027.au
hiphop;../../DATASETS/genres/hiphop/hiphop.00048.au
classical;../../DATASETS/genres/classical/classical.00020.au
classical;../../DATASETS/genres/classical/classical.00001.au
metal;../../DATASETS/genres/metal/metal.00028.au
country;../../DATASETS/genres/country/country.00047.au
classical;../../DATASETS/genres/classical/classical.00092.au
jazz;../../DATASETS/genres/jazz/jazz.00021.au
rock;../../DATASETS/genres/rock/rock.00035.au
country;../../DATASETS/genres/country/country.00090.au
jazz;../../DATASETS/genres/jazz/jazz.00041.au
pop;../../DATASETS/genres/pop/pop.00016.au
metal;../../DATASETS/genres/metal/metal.00056.au
country;../../DATASETS/genres/country/country.00011.au
rock;../../DATASETS/genres/rock/rock.00007.au
reggae;../../DATASETS/genres/reggae/reggae.00027.au
country;../../DATASETS/genres/country/country.00056.au
hiphop;../../DATASETS/genres/hiphop/hiphop.00079.au
blues;../../DATASETS/genres/blues/blues.00063.au
metal;../../DATASETS/genres/metal/metal.00087.au
blues;../../DATASETS/genres/blues/blues.00084.au
blues;../../DATASETS/genres/blues/blues.00054.au
metal;../../DATASETS/genres/metal/metal.00043.au
metal;../../DATASETS/genres/metal/metal.00001.au
blues;../../DATASETS/genres/blues/blues.00003.au
jazz;../../DATASETS/genres/jazz/jazz.00001.au
blues;../../DATASETS/genres/blues/blues.00076.au
disco;../../DATASETS/genres/disco/disco.00042.au
blues;../../DATASETS/genres/blues/blues.00029.au
hiphop;../../DATASETS/genres/hiphop/hiphop.00068.au
rock;../../DATASETS/genres/rock/rock.00086.au
metal;../../DATASETS/genres/metal/metal.00003.au
disco;../../DATASETS/genres/disco/disco.00044.au
jazz;../../DATASETS/genres/jazz/jazz.00075.au
hiphop;../../DATASETS/genres/hiphop/hiphop.00011.au
pop;../../DATASETS/genres/pop/pop.00013.au
metal;../../DATASETS/genres/metal/metal.00021.au
classical;../../DATASETS/genres/classical/classical.00009.au
metal;../../DATASETS/genres/metal/metal.00049.au
country;../../DATASETS/genres/country/country.00086.au
country;../../DATASETS/genres/country/country.00039.au
jazz;../../DATASETS/genres/jazz/jazz.00014.au
blues;../../DATASETS/genres/blues/blues.00025.au
jazz;../../DATASETS/genres/jazz/jazz.00042.au
disco;../../DATASETS/genres/disco/disco.00031.au
blues;../../DATASETS/genres/blues/blues.00055.au
jazz;../../DATASETS/genres/jazz/jazz.00076.au
reggae;../../DATASETS/genres/reggae/reggae.00067.au
blues;../../DATASETS/genres/blues/blues.00079.au
reggae;../../DATASETS/genres/reggae/reggae.00059.au
classical;../../DATASETS/genres/classical/classical.00007.au
pop;../../DATASETS/genres/pop/pop.00096.au
metal;../../DATASETS/genres/metal/metal.00004.au
rock;../../DATASETS/genres/rock/rock.00038.au
disco;../../DATASETS/genres/disco/disco.00009.au
disco;../../DATASETS/genres/disco/disco.00028.au
hiphop;../../DATASETS/genres/hiphop/hiphop.00022.au
metal;../../DATASETS/genres/metal/metal.00031.au
metal;../../DATASETS/genres/metal/metal.00064.au
hiphop;../../DATASETS/genres/hiphop/hiphop.00049.au
country;../../DATASETS/genres/country/country.00031.au
rock;../../DATASETS/genres/rock/rock.00018.au
rock;../../DATASETS/genres/rock/rock.00025.au
hiphop;../../DATASETS/genres/hiphop/hiphop.00040.au
rock;../../DATASETS/genres/rock/rock.00091.au
classical;../../DATASETS/genres/classical/classical.00048.au
hiphop;../../DATASETS/genres/hiphop/hiphop.00082.au
disco;../../DATASETS/genres/disco/disco.00081.au
jazz;../../DATASETS/genres/jazz/jazz.00035.au
pop;../../DATASETS/genres/pop/pop.00067.au
rock;../../DATASETS/genres/rock/rock.00074.au
country;../../DATASETS/genres/country/country.00049.au
rock;../../DATASETS/genres/rock/rock.00048.au
blues;../../DATASETS/genres/blues/blues.00005.au
metal;../../DATASETS/genres/metal/metal.00044.au
jazz;../../DATASETS/genres/jazz/jazz.00078.au
pop;../../DATASETS/genres/pop/pop.00037.au
reggae;../../DATASETS/genres/reggae/reggae.00037.au
metal;../../DATASETS/genres/metal/metal.00079.au
jazz;../../DATASETS/genres/jazz/jazz.00096.au
disco;../../DATASETS/genres/disco/disco.00070.au
country;../../DATASETS/genres/country/country.00066.au
hiphop;../../DATASETS/genres/hiphop/hiphop.00051.au
country;../../DATASETS/genres/country/country.00061.au
disco;../../DATASETS/genres/disco/disco.00057.au
country;../../DATASETS/genres/country/country.00004.au
rock;../../DATASETS/genres/rock/rock.00073.au
pop;../../DATASETS/genres/pop/pop.00053.au
country;../../DATASETS/genres/country/country.00050.au
country;../../DATASETS/genres/country/country.00098.au
pop;../../DATASETS/genres/pop/pop.00077.au
jazz;../../DATASETS/genres/jazz/jazz.00029.au
disco;../../DATASETS/genres/disco/disco.00052.au
pop;../../DATASETS/genres/pop/pop.00099.au
blues;../../DATASETS/genres/blues/blues.00023.au
jazz;../../DATASETS/genres/jazz/jazz.00070.au
hiphop;../../DATASETS/genres/hiphop/hiphop.00086.au
pop;../../DATASETS/genres/pop/pop.00049.au
pop;../../DATASETS/genres/pop/pop.00011.au
disco;../../DATASETS/genres/disco/disco.00065.au
disco;../../DATASETS/genres/disco/disco.00010.au
country;../../DATASETS/genres/country/country.00021.au
hiphop;../../DATASETS/genres/hiphop/hiphop.00039.au
blues;../../DATASETS/genres/blues/blues.00031.au
pop;../../DATASETS/genres/pop/pop.00089.au
classical;../../DATASETS/genres/classical/classical.00074.au
jazz;../../DATASETS/genres/jazz/jazz.00095.au
classical;../../DATASETS/genres/classical/classical.00096.au
reggae;../../DATASETS/genres/reggae/reggae.00029.au
hiphop;../../DATASETS/genres/hiphop/hiphop.00064.au
rock;../../DATASETS/genres/rock/rock.00016.au
rock;../../DATASETS/genres/rock/rock.00034.au
disco;../../DATASETS/genres/disco/disco.00021.au
jazz;../../DATASETS/genres/jazz/jazz.00099.au
classical;../../DATASETS/genres/classical/classical.00058.au
reggae;../../DATASETS/genres/reggae/reggae.00026.au
pop;../../DATASETS/genres/pop/pop.00014.au
metal;../../DATASETS/genres/metal/metal.00055.au
hiphop;../../DATASETS/genres/hiphop/hiphop.00025.au
classical;../../DATASETS/genres/classical/classical.00033.au
jazz;../../DATASETS/genres/jazz/jazz.00049.au
rock;../../DATASETS/genres/rock/rock.00042.au
hiphop;../../DATASETS/genres/hiphop/hiphop.00093.au
jazz;../../DATASETS/genres/jazz/jazz.00054.au
hiphop;../../DATASETS/genres/hiphop/hiphop.00090.au
rock;../../DATASETS/genres/rock/rock.00036.au
#include "apply_SVM_weights.h"
#include "apply_SVM_algo.h"
#include "../Extraction/features_extraction.cpp"
#include "../Helpers/evaluate_model.h"
#include <string>
#include <iostream>
......@@ -20,5 +20,8 @@ int main(int argc, char **argv)
std::cout << genres[result2] << std::endl;
int result3 = SVMModelAlgo(features);
std::cout << genres[result3] << std::endl;
std::cout << "Compute accuracy... \n";
real accuracy = compute_accuracy("/home/hugo/Documents/embedded-machine-learning/SVM/file_test.csv", (vFunctionCall)SVMModelAlgo);
std::cout << "Accuracy: " << accuracy << "\n";
return 0;
}
\ No newline at end of file
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