-
Notifications
You must be signed in to change notification settings - Fork 0
/
randomforest.cpp
217 lines (179 loc) · 7.22 KB
/
randomforest.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#include "randomforest.h"
#include "utility.h"
#include "colorfeatureextractor.h"
#include "lbpfeatureextractor.h"
#include "censusfeatureextractor.h"
#include "edgefeatureextractor.h"
#include <QImage>
#include <QDebug>
RandomForest::RandomForest(QObject *parent) :
QObject(parent)
{
m_numOfFeatures = 0;
}
void RandomForest::train(QString filename,
int positiveCount,
int negativeCount,
int numOfFeatures)
{
qDebug() << "numOfFeatures:" << numOfFeatures;
int numOfSamples = positiveCount + negativeCount;
cv::Mat samples = cv::Mat(numOfSamples, numOfFeatures, CV_32FC1);
cv::Mat labels = cv::Mat(numOfSamples, 1, CV_32FC1);
cv::Mat var_type = cv::Mat(numOfFeatures + 1, 1, CV_8U);
var_type.setTo(cv::Scalar(CV_VAR_NUMERICAL) );
var_type.at<uchar>(numOfFeatures, 0) = CV_VAR_CATEGORICAL;
int ret = loadData(filename.toUtf8().data(), samples, labels, numOfSamples, numOfFeatures);
if (!ret)
{
qDebug() << "RandomForest::train loadData failed.";
return;
}
float priors[] = {(float)positiveCount/numOfSamples, (float)negativeCount/numOfSamples}; // weights of each classification for classes
CvRTParams params = CvRTParams(50, // max depth
numOfSamples * 0.01, // min sample count. (numOfSamples * 0.01)
0, // regression accuracy: N/A here
false, // compute surrogate split, no missing data
2, // max number of categories (use sub-optimal algorithm for larger numbers)
priors, // the array of priors
false, // calculate variable importance
0, // number of variables randomly selected at node and used to find the best split(s).
100, // max number of trees in the forest
0.001f, // forrest accuracy
CV_TERMCRIT_ITER | CV_TERMCRIT_EPS // termination cirteria
);
m_randomForest.train(samples, CV_ROW_SAMPLE, labels, cv::Mat(), cv::Mat(), var_type, cv::Mat(), params);
qDebug() << "Generated number of trees:" << m_randomForest.get_tree_count();
}
float RandomForest::predict(const cv::Mat& sample)
{
return m_randomForest.predict(sample, cv::Mat());
}
float RandomForest::predict_prob(const cv::Mat& sample)
{
float res = m_randomForest.predict_prob(sample, cv::Mat());
return res;
}
QSize RandomForest::workSize() const
{
return m_workSize;
}
void RandomForest::setWorkSize(QSize size)
{
m_workSize = size;
}
int RandomForest::numOfFeatures() const
{
return m_numOfFeatures;
}
void RandomForest::setNumOfFeatures(int numOfFeatures)
{
m_numOfFeatures = numOfFeatures;
}
cv::Mat RandomForest::computeFeatureVectors(QString imgPath, int w, int h)
{
cv::Mat featureVector;
// Scale the image accordingly
QImage image;
image.load(imgPath);
if (image.isNull())
return featureVector;
image = image.scaled(w, h);
cv::Mat cvImage = Utility::QImageToCvMat(image);
featureVector = computeFeatureVectors(cvImage, w, h);
return featureVector;
}
cv::Mat RandomForest::computeFeatureVectors(cv::Mat inputImage, int w, int h)
{
QVector<int> concatenatedFV;
ColorFeatureExtractor colorFeatureExtractor;
LbpFeatureExtractor lbpFeatureExtractor;
CensusFeatureExtractor censusFeatureExtractor;
EdgeFeatureExtractor edgeFeatureExtractor;
// User-defined bins
int horizontalBins = 2;
int verticalBins = 6;
colorFeatureExtractor.compute(inputImage, horizontalBins, verticalBins);
concatenatedFV += colorFeatureExtractor.featureVector();
lbpFeatureExtractor.compute(inputImage, horizontalBins, verticalBins);
concatenatedFV += lbpFeatureExtractor.featureVector();
censusFeatureExtractor.compute(inputImage, horizontalBins, verticalBins);
concatenatedFV += censusFeatureExtractor.featureVector();
edgeFeatureExtractor.compute(inputImage, horizontalBins, verticalBins);
concatenatedFV += edgeFeatureExtractor.featureVector();
m_numOfFeatures = concatenatedFV.length();
cv::Mat featureVector = cv::Mat(1, m_numOfFeatures, CV_32FC1);
for (int attribute = 0; attribute < m_numOfFeatures; attribute++)
{
featureVector.at<float>(attribute) = concatenatedFV.at(attribute);
}
return featureVector;
}
void RandomForest::test(QString filename, int numOfSamples, int numOfFeatures)
{
cv::Mat samples = cv::Mat(numOfSamples, numOfFeatures, CV_32FC1);
cv::Mat labels = cv::Mat(numOfSamples, 1, CV_32FC1);
cv::Mat var_type = cv::Mat(numOfFeatures + 1, 1, CV_8U);
var_type.setTo(cv::Scalar(CV_VAR_NUMERICAL) );
var_type.at<uchar>(numOfFeatures, 0) = CV_VAR_CATEGORICAL;
// TODO: read and parser data
int ret = loadData(filename.toUtf8().data(), samples, labels, numOfSamples, numOfFeatures);
if (!ret)
return;
int correct_class = 0;
int wrong_class = 0;
int false_positives [2] = {0,0};
cv::Mat test_sample;
double result;
for (int tsample = 0; tsample < numOfSamples; tsample++)
{
// extract a row from the testing matrix
test_sample = samples.row(tsample);
result = m_randomForest.predict(test_sample, cv::Mat());
// if the prediction and the (true) testing classification are the same
// (N.B. openCV uses a floating point decision tree implementation!)
if (fabs(result - labels.at<float>(tsample, 0)) >= FLT_EPSILON)
{
// if they differ more than floating point error => wrong class
wrong_class++;
false_positives[(int) result]++;
} else {
// otherwise correct
correct_class++;
}
}
qDebug() << "Training results:";
qDebug() << "Correct classification: " << correct_class << "(" << (double)correct_class*100/numOfSamples << "%)";
qDebug() << "Wrong classification: " << wrong_class << "(" << (double)wrong_class*100/numOfSamples << "%)";
}
int RandomForest::loadData(const char* filename, cv::Mat samples, cv::Mat lables, int numOfSamples, int numOfFeatures)
{
float tmp;
// if we can't read the input file then return 0
FILE* f = fopen(filename, "r");
if (!f)
{
qDebug() << "ERROR: cannot read file: " << filename;
return 0; // all not OK
}
// for each sample in the file
for (int line = 0; line < numOfSamples; line++)
{
// for each attribute on the line in the file
for (int attribute = 0; attribute < (numOfFeatures + 1); attribute++)
{
if (attribute < numOfFeatures)
{
// first numOfFeatures(0~numOfFeatures-1) elements in each line are the attributes
fscanf(f, "%f,", &tmp);
samples.at<float>(line, attribute) = tmp;
} else if (attribute == numOfFeatures) {
// attribute numOfFeatures is the class label
fscanf(f, "%f,", &tmp);
lables.at<float>(line, 0) = tmp;
}
}
}
fclose(f);
return 1; // all OK
}