-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathtrain_classifierNM1.cpp
More file actions
63 lines (47 loc) · 2.12 KB
/
Copy pathtrain_classifierNM1.cpp
File metadata and controls
63 lines (47 loc) · 2.12 KB
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
#include <cstdlib>
#include <vector>
#include <fstream>
#include "opencv2/core/core.hpp"
#include "opencv2/ml/ml.hpp"
using namespace std;
using namespace cv;
using namespace cv::ml;
int main(int argc, char** argv) {
//Read the data from csv file
Ptr<TrainData> cvml = TrainData::loadFromCSV(string("char_datasetNM1.csv"),0,0);
//Select 90% for the training
cvml->setTrainTestSplitRatio(0.9, true);
Ptr<Boost> boost;
ifstream ifile("./trained_classifierNM1.xml");
if (ifile)
{
//The file exists, so we don't want to train
printf("Found trained_boost_char.xml file, remove it if you want to retrain with new data ... \n");
boost = StatModel::load<Boost>("./trained_classifierNM1.xml");
} else {
//Train with 100 features
printf("Training ... \n");
boost = StatModel::train<Boost>(cvml, Boost::Params(Boost::REAL, 100, 0.0, 1, false, Mat()));
}
//Calculate the test and train errors
Mat train_responses, test_responses;
float fl1 = boost->calcError(cvml,false,train_responses);
float fl2 = boost->calcError(cvml,true,test_responses);
printf("Error train %f \n", fl1);
printf("Error test %f \n", fl2);
//Try a char
Mat sample = (Mat_<float>(1,4) << 1.063830,0.083372,0.000000,2.000000);
float prediction = boost->predict( sample, noArray(), 0 );
float votes = boost->predict( sample, noArray(), DTrees::PREDICT_SUM | StatModel::RAW_OUTPUT );
printf("\n The char sample is predicted as: %f (with number of votes = %f)\n", prediction,votes);
printf(" Class probability (using Logistic Correction) is P(r|character) = %f\n", (float)1-(float)1/(1+exp(-2*votes)));
//Try a NONchar
Mat sample2 = (Mat_<float>(1,4) << 2.000000,0.235702,0.000000,2.000000);
prediction = boost->predict( Mat(sample2), noArray(), 0 );
votes = boost->predict( Mat(sample2), noArray(), DTrees::PREDICT_SUM | StatModel::RAW_OUTPUT );
printf("\n The non_char sample is predicted as: %f (with number of votes = %f)\n", prediction,votes);
printf(" Class probability (using Logistic Correction) is P(r|character) = %f\n\n", (float)1-(float)1/(1+exp(-2*votes)));
// Save the trained classifier
boost->save(string("./trained_classifierNM1.xml"));
return EXIT_SUCCESS;
}