-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextract_neuralnetwork.cpp
101 lines (88 loc) · 3.06 KB
/
extract_neuralnetwork.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include <iostream>
#include <filesystem>
#include <string>
#include <regex>
#include <fstream>
#include "src/m_learning.h"
#define TRAINMODEL_FOLDER "../resources/trained_model/"
#define SAVE_FILE "../resources/extracted/data.txt"
#define BRAIN_NAME "brain_TP_time_"
namespace fs = std::filesystem;
using namespace std;
std::ifstream::pos_type filesize(const char* filename)
{
std::ifstream in(filename, std::ifstream::ate | std::ifstream::binary);
return in.tellg();
}
int main(int argc, char **argv){
string filename = "";
if(argc==2){
cout << "Extraction du réseau de neurones pour l'envoyer dans l'arduino" << endl;
filename = argv[1];
cout << "fichier:" << filename << endl;
}else if(argc==1){
cout << "Extraction du réseau de neurones pour l'envoyer dans l'arduino" << endl;
std::string path = TRAINMODEL_FOLDER;
cout << "Selection du meilleur réseau de neurones dans "<< path << endl;
int max_score = 0;
smatch m;
regex r((string(BRAIN_NAME)+"([0-9]+).ml").c_str());
for (const auto & entry : fs::directory_iterator(path)){
string name = entry.path();
regex_search(name,m,r);
int score = 0;
if(m.size()>0){
auto v = m[1];
//cout << "score:" << v << endl;
score = stoi(v);
if(score>max_score){
max_score = score;
filename = name;
}
}
}
cout << "filename:" << filename << endl;
}else{
cout << "Argument error" << endl;
cout << argv[0] << " path_to_neuralnetwork.ml" << endl;
return 1;
}
cout << "Chargement du réseau de neurones:"<< filename << endl;
MachineLearning machine;
if(!machine.loadStructure(filename.c_str())){
cout << "Erreur lors de la récupération de la structure" << endl;
}
machine.backupTraining(filename.c_str());
filename = "../resources/tmp_model_arduino/light.ml";
machine.saveTrainingArduino(filename.c_str());
cout << "Ouverture du fichier..." << endl;
ifstream file(filename, ios::binary);
if(file.is_open()){
string data = "";
cout << "Code arduino:" <<endl;
int size = filesize(filename.c_str());
data="const PROGMEM char neuralnetwork_data["+to_string(size)+"]={";
char buffer = 0;
for(int i=0;i<size;i++){
file.read(&buffer,1);
data+=to_string(int(buffer));
if(i!=size-1){
data+=",";
}
}
data+="};\n";
cout << data;
cout << "Enregistrement dans le fichier " << SAVE_FILE << endl;
ofstream save_file(SAVE_FILE);
if(!save_file.is_open()){
cout << "Erreur lors de l'ouverture du fichier" << endl;
return 1;
}
save_file << data;
cout << "Enregistrement effectué avec succés" << endl;
}else{
cout << "Fichier inexistant" << endl;
return 1;
}
return 0;
}