-
Notifications
You must be signed in to change notification settings - Fork 0
/
boostTraining.cpp
95 lines (76 loc) · 2.3 KB
/
boostTraining.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
#include <cstdlib>
#include "opencv/cv.h"
#include "opencv/ml.h"
#include <vector>
#include <fstream>
#include <sstream>
using namespace std;
using namespace cv;
// function declare
void generateData();
vector< vector<float> > readText(char* fileName);
int maxClusterSize, maxFrameIndex;
int main(int argc, char** argv) {
generateData();
/* STEP 2. Opening the file */
//1. Declare a structure to keep the data
CvMLData cvml;
//2. Read the file
cvml.read_csv("samples.csv");
//3. Indicate which column is the response
cvml.set_response_idx(0);
/* STEP 3. Splitting the samples */
//1. Select 40 for the training
CvTrainTestSplit cvtts(15, true);
//2. Assign the division to the data
cvml.set_train_test_split(&cvtts);
printf("Training ... ");
/* STEP 4. The training */
//1. Declare the classifier
CvBoost boost;
//2. Train it with 100 features
boost.train(&cvml, CvBoostParams(CvBoost::REAL, 100, 0, 1, false, 0), false);
/* STEP 5. Calculating the testing and training error */
// 1. Declare a couple of vectors to save the predictions of each sample
vector<float> train_responses;
vector<float> test_responses;
// 2. Calculate the training error
float fl1 = boost.calc_error(&cvml,CV_TRAIN_ERROR,&train_responses);
// 3. Calculate the test error
float fl2 = boost.calc_error(&cvml,CV_TEST_ERROR,&test_responses);
printf("Error train %f \n", fl1);
printf("Error test %f \n", fl2);
/* STEP 6. Save your classifier */
// Save the trained classifier
boost.save("./trained_boost.xml", "boost");
return EXIT_SUCCESS;
}
// generate Training data and test data
void generateData(){
maxClusterSize = 0;
maxFrameIndex = 0;
vector< vector<float> > loca_diff = readText("loca_diff.txt");
}
vector< vector<float> > readText(char* fileName){
ifstream infile(fileName);
int clusterNumber, frameIndex;
float data;
vector< vector<float> > v;
while( !infile.eof() && infile>>clusterNumber>>frameIndex>>data){
if(maxClusterSize < clusterNumber + 1){
v.resize(clusterNumber + 1);
for (int i = 0; i < v.size(); ++i){
v[i].resize(maxFrameIndex + 1);
}
maxClusterSize = clusterNumber + 1;
}
if(maxFrameIndex < frameIndex + 1){
for (int i = 0; i < v.size(); ++i){
v[i].resize(frameIndex + 1);
}
maxFrameIndex = frameIndex;
}
v[clusterNumber][frameIndex] = data;
}
return v;
}