-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClassifier.cpp
More file actions
373 lines (280 loc) · 9.19 KB
/
Classifier.cpp
File metadata and controls
373 lines (280 loc) · 9.19 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
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
#include "Classifier.h"
LcValidator::LcValidator( float _tp, float _fp, float _fn , float _tn)
{
tp = _tp; fp = _fp; fn = _fn; tn = _tn;
}
LcValidator LcValidator::operator +(const LcValidator & a)
{
return LcValidator( a.tp + tp , a.fp + fp , a.fn + fn , a.tn + tn);
}
void LcValidator::display()
{
//cout << "tp:" << tp << " fp:" << fp << " tn:"<< tn << " fn:" << fn << endl;
cout << "Precision:" << getPrecision(1) << " " << getPrecision(0) << "(back) " << endl;
cout << " Recall :" << getRecall(1) << " " << getRecall(0) << "(back) " << endl;
cout << "F:" << getF1() << " 0-1:" << getZeroOne() << endl;
}
float LcValidator::getZeroOne()
{
return (tp+tn)/(tp+tn+fp+fn);
}
float LcValidator::getPrecision(int i)
{
if(i){return tp/(1e-5f+tp+fp);}
else {return tn/(1e-5f+tn+fn);}
}
float LcValidator::getRecall(int i)
{
if(i){return tp/(1e-5f+tp+fn);}
else {return tn/(1e-5f+fp+tn);}
}
float LcValidator::getF1(int i)
{
float p = getPrecision(i);
float r = getRecall(i);
return 2*p*r/(1e-5f+p+r);
}
LcValidator::LcValidator( Mat & res, Mat & lab)
{
count( res, lab, 0.5, tp, fp, tn, fn);
}
void LcValidator::count( Mat & res, Mat & lab, float th, float & tp, float & fp, float & tn, float & fn)
{
if( res.rows != lab.rows){ cout << " size unmatch while predicting " << endl; return;}
tp = fp = tn = fn = 0.0f;
float * p_res = (float*) res.data;
float * p_lab = (float*) lab.data;
for(int sz = res.rows * res.cols ; sz>0; sz--, p_res++, p_lab++)
{
if( *p_res > th)
{
if( *p_lab >th) tp += 1.0f;
else fp += 1.0f;
}
else
{
if( *p_lab >th) fn += 1.0f;
else tn += 1.0f;
}
}
{
float n = float( res.rows*res.cols);
fp/=n; tp/=n; tn/=n; fn/=n;
}
}
//==============================
void LcRandomTreesR::train(Mat & feature, Mat & label)
{
//_params.max_depth = 10;
//_params.regression_accuracy = 0.1f;
//_params.use_1se_rule = true;
//_params.use_surrogates = true;
//_params.truncate_pruned_tree = false;
//_params.min_sample_count = 10;
_params.max_depth = 10;
_params.regression_accuracy = 0.00f;
_params.min_sample_count = 10;
double t = double(getTickCount());
cout << " Training random forest regression model ...";
Mat varType = Mat::ones(feature.cols+1,1,CV_8UC1) * CV_VAR_NUMERICAL;
_random_tree.train(feature,CV_ROW_SAMPLE,label,Mat(),Mat(),varType,Mat(), _params);
t = (getTickCount()-t)/getTickFrequency();
cout << " time to train:" << t << " secs." << endl;
}
LcValidator LcRandomTreesR::predict( Mat & feature, Mat & res, Mat & label)
{
int n = feature.rows;
res = Mat::zeros( n, 1, 5);
for(int i = 0; i< n ; i++)
{
res.at<float>(i,0) = _random_tree.predict( feature.row(i) );
//res.at<float>(i,0) = _random_tree.predict_prob( feature.row(i) );
}
if( label.rows == feature.rows ) return LcValidator( res, label);
else return LcValidator();
}
LcValidator LcRandomTreesR::predict( Mat & feature, Mat & res)
{
Mat label;
return predict(feature,res,label);
}
void LcRandomTreesR::save( string filename_prefix ){
string filename = filename_prefix + "_rdtr.xml";
cout << " Classifier: Saving " << filename << endl;
_random_tree.save( filename.c_str());
}
void LcRandomTreesR::load( string filename_prefix ){
string filename = filename_prefix + "_rdtr.xml";
cout << " Classifier: Loading " << filename << endl;
_random_tree.load( filename.c_str());
}
void LcRandomTreesR::load_full( string full_filename ){
cout << " Classifier: Loading " << full_filename << endl;
_random_tree.load( full_filename.c_str());
}
//==============================
void LcRandomTreesC::train(Mat & feature, Mat & label) // Multi-class Classifier
{
//_params.max_depth = 10;
//_params.regression_accuracy = 0.1f;
//_params.use_1se_rule = true;
//_params.use_surrogates = true;
//_params.truncate_pruned_tree = false;
//_params.min_sample_count = 10;
_params.max_depth = 100;
_params.min_sample_count = 40;
//_params.use_1se_rule = true;
//_params.use_surrogates = true;
double t = double(getTickCount());
if( veb ) cout << "Train Random Tree Multi-Class Classifier model ...";
Mat varType = Mat::ones(feature.cols+1,1,CV_8UC1) * CV_VAR_NUMERICAL; // all floats
varType.at<uchar>(feature.cols,0) = CV_VAR_CATEGORICAL;
_random_tree.train(feature,CV_ROW_SAMPLE,label,Mat(),Mat(),varType,Mat(), _params);
t = (getTickCount()-t)/getTickFrequency();
if( veb ) cout << " time:" << t << " secs." << endl;
}
LcValidator LcRandomTreesC::predict( Mat & feature, Mat & res, Mat & label)
{
int n = feature.rows;
res = Mat::zeros( n, 1, 5);
for(int i = 0; i< n ; i++)
{
res.at<float>(i,0) = _random_tree.predict( feature.row(i) );
//res.at<float>(i,0) = _random_tree.predict_prob( feature.row(i) );
}
if( label.rows == feature.rows ) return LcValidator( res, label);
else return LcValidator();
}
LcValidator LcRandomTreesC::predict( Mat & feature, Mat & res)
{
Mat label;
return predict(feature,res,label);
}
void LcRandomTreesC::save( string filename_prefix ){
string filename = filename_prefix + "_rdtc.xml";
_random_tree.save( filename.c_str());
}
void LcRandomTreesC::load( string filename_prefix ){
string filename = filename_prefix + "_rdtc.xml";
_random_tree.load( filename.c_str());
}
//==============================
void LcDecisionTree::train(Mat & feature, Mat & label)
{
int TREE_DEPTH = 10;
_params = CvDTreeParams(TREE_DEPTH,10,0.0,true,TREE_DEPTH,4,true,true,0);
double t = double(getTickCount());
if( veb ) cout << "Train decision tree model ...";
Mat varType = Mat::ones(feature.cols+1,1,CV_8UC1) * CV_VAR_NUMERICAL; // all floats
varType.at<uchar>(feature.cols,0) = CV_VAR_CATEGORICAL;
_tree.train(feature,CV_ROW_SAMPLE,label,Mat(),Mat(),varType,Mat(),_params);
t = (getTickCount()-t)/getTickFrequency();
if( veb ) cout << " time:" << t << " secs." << endl;
}
LcValidator LcDecisionTree::predict( Mat & feature, Mat & res, Mat & label)
{
int n = feature.rows;
res = Mat::zeros( n, 1, 5);
for(int i = 0; i< n ; i++)
{
CvDTreeNode *node;
node = _tree.predict( feature.row(i) ,Mat(),false);
res.at<float>(i,0) = float(node->value);
}
if( label.rows == feature.rows ) return LcValidator( res, label);
else return LcValidator();
}
void LcDecisionTree::save( string filename_prefix ){
string filename = filename_prefix + "_dt.xml";
_tree.save( filename.c_str() );
}
void LcDecisionTree::load( string filename_prefix ){
string filename = filename_prefix + "_dt.xml";
_tree.load( filename.c_str() );
}
//==============================
void LcAdaBoosting::train(Mat & feature, Mat & label)
{
int boost_type = CvBoost::GENTLE; //CvBoost::REAL; //CvBoost::GENTLE;
int weak_count = 100;
double weight_trim_rate = 0.95;
int max_depth = 1;
bool use_surrogates = false;
const float* priors = NULL;
_params = CvBoostParams(boost_type, weak_count,weight_trim_rate,max_depth,use_surrogates,priors);
Mat varType = Mat::ones(feature.cols+1,1,CV_8UC1) * CV_VAR_NUMERICAL; // all floats
varType.at<uchar>(feature.cols,0) = CV_VAR_CATEGORICAL;
//lab = lab*2-1;
//cout << lab << endl;
//lab.convertTo(lab,CV_8UC1);
double t = (double)getTickCount();
if(veb) cout << "Train (Gentle) AdaBoost model ...";
_boost.train(feature,CV_ROW_SAMPLE,label,Mat(),Mat(),varType,Mat(),_params,false);
t = (getTickCount()-t)/getTickFrequency();
if(veb) cout << " time:" << t << " secs." << endl;
}
LcValidator LcAdaBoosting::predict( Mat & feature, Mat & res, Mat & label)
{
int n = feature.rows;
res = Mat::zeros( n, 1, 5);
for(int i = 0; i< n ; i++)
{
res.at<float>(i,0) = _boost.predict( feature.row(i) );
}
if( label.rows == feature.rows ) return LcValidator( res, label);
else return LcValidator();
}
void LcAdaBoosting::save( string filename_prefix ){
string filename = filename_prefix + "_ada.xml";
_boost.save( filename.c_str() );
}
void LcAdaBoosting::load( string filename_prefix ){
string filename = filename_prefix + "_ada.xml";
_boost.load( filename.c_str() );
}
//==============================
LcKNN::LcKNN()
{
rotation_kernel = Mat();
}
LcValidator LcKNN::predict(Mat & feature, Mat & res, Mat & label)
{
cv::flann::Index _flann(_feat, cv::flann::KDTreeIndexParams(4));
int n = feature.rows;
res = Mat::zeros( n, 1, 5);
Mat inds; Mat dists;
_flann.knnSearch(feature, inds, dists,knn,cv::flann::SearchParams(64));
for(int i = 0; i< n ; i++)
{
float sum_weight = 0.0f;
float sum_ans = 0.0f;
for(int k = 0;k< knn ;k++)
{
float m_weight = 1;//exp(- dists[k]/scale);
int & id = inds.at<int>(i,k);
sum_weight += m_weight;
sum_ans += m_weight * _lab.at<float>(id,0);
}
res.at<float>( i,0) = float( sum_ans/sum_weight);
}
if( label.rows == feature.rows ) return LcValidator( res, label);
else return LcValidator();
}
void LcKNN::train(Mat & feature, Mat & label)
{
knn = 5;
feature.copyTo(_feat);
label.copyTo(_lab);
}
void LcKNN::save( string filename_prefix ){
string feature_name = filename_prefix + "_knn_feat.bin";
lc::LcMat2Bin( feature_name.c_str(), _feat);
string label_name = filename_prefix + "_knn_lab.bin";
lc::LcMat2Bin( label_name.c_str(), _lab);
}
void LcKNN::load( string filename_prefix ){
string feature_name = filename_prefix + "_knn_feat.bin";
lc::LcBin2Mat( feature_name.c_str(), _feat);
string label_name = filename_prefix + "_knn_lab.bin";
lc::LcBin2Mat( label_name.c_str(), _lab);
}