1 /*M///////////////////////////////////////////////////////////////////////////////////////
3 // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
5 // By downloading, copying, installing or using the software you agree to this license.
6 // If you do not agree to this license, do not download, install,
7 // copy or use the software.
11 // For Open Source Computer Vision Library
13 // Copyright (C) 2000, Intel Corporation, all rights reserved.
14 // Copyright (C) 2014, Itseez Inc, all rights reserved.
15 // Third party copyrights are property of their respective owners.
17 // Redistribution and use in source and binary forms, with or without modification,
18 // are permitted provided that the following conditions are met:
20 // * Redistribution's of source code must retain the above copyright notice,
21 // this list of conditions and the following disclaimer.
23 // * Redistribution's in binary form must reproduce the above copyright notice,
24 // this list of conditions and the following disclaimer in the documentation
25 // and/or other materials provided with the distribution.
27 // * The name of the copyright holders may not be used to endorse or promote products
28 // derived from this software without specific prior written permission.
30 // This software is provided by the copyright holders and contributors "as is" and
31 // any express or implied warranties, including, but not limited to, the implied
32 // warranties of merchantability and fitness for a particular purpose are disclaimed.
33 // In no event shall the Intel Corporation or contributors be liable for any direct,
34 // indirect, incidental, special, exemplary, or consequential damages
35 // (including, but not limited to, procurement of substitute goods or services;
36 // loss of use, data, or profits; or business interruption) however caused
37 // and on any theory of liability, whether in contract, strict liability,
38 // or tort (including negligence or otherwise) arising in any way out of
39 // the use of this software, even if advised of the possibility of such damage.
43 #include "precomp.hpp"
48 //////////////////////////////////////////////////////////////////////////////////////////
50 //////////////////////////////////////////////////////////////////////////////////////////
51 RTrees::Params::Params()
52 : DTrees::Params(5, 10, 0.f, false, 10, 0, false, false, Mat())
54 calcVarImportance = false;
56 termCrit = TermCriteria(TermCriteria::EPS + TermCriteria::COUNT, 50, 0.1);
59 RTrees::Params::Params( int _maxDepth, int _minSampleCount,
60 double _regressionAccuracy, bool _useSurrogates,
61 int _maxCategories, const Mat& _priors,
62 bool _calcVarImportance, int _nactiveVars,
63 TermCriteria _termCrit )
64 : DTrees::Params(_maxDepth, _minSampleCount, _regressionAccuracy, _useSurrogates,
65 _maxCategories, 0, false, false, _priors)
67 calcVarImportance = _calcVarImportance;
68 nactiveVars = _nactiveVars;
73 class DTreesImplForRTrees : public DTreesImpl
76 DTreesImplForRTrees() {}
77 virtual ~DTreesImplForRTrees() {}
79 void setRParams(const RTrees::Params& p)
84 RTrees::Params getRParams() const
93 rng = RNG((uint64)-1);
96 const vector<int>& getActiveVars()
98 int i, nvars = (int)allVars.size(), m = (int)activeVars.size();
99 for( i = 0; i < nvars; i++ )
101 int i1 = rng.uniform(0, nvars);
102 int i2 = rng.uniform(0, nvars);
103 std::swap(allVars[i1], allVars[i2]);
105 for( i = 0; i < m; i++ )
106 activeVars[i] = allVars[i];
110 void startTraining( const Ptr<TrainData>& trainData, int flags )
112 DTreesImpl::startTraining(trainData, flags);
113 int nvars = w->data->getNVars();
114 int i, m = rparams.nactiveVars > 0 ? rparams.nactiveVars : cvRound(std::sqrt((double)nvars));
115 m = std::min(std::max(m, 1), nvars);
116 allVars.resize(nvars);
117 activeVars.resize(m);
118 for( i = 0; i < nvars; i++ )
119 allVars[i] = varIdx[i];
124 DTreesImpl::endTraining();
126 std::swap(allVars, a);
127 std::swap(activeVars, b);
130 bool train( const Ptr<TrainData>& trainData, int flags )
132 Params dp(rparams.maxDepth, rparams.minSampleCount, rparams.regressionAccuracy,
133 rparams.useSurrogates, rparams.maxCategories, rparams.CVFolds,
134 rparams.use1SERule, rparams.truncatePrunedTree, rparams.priors);
136 startTraining(trainData, flags);
137 int treeidx, ntrees = (rparams.termCrit.type & TermCriteria::COUNT) != 0 ?
138 rparams.termCrit.maxCount : 10000;
139 int i, j, k, vi, vi_, n = (int)w->sidx.size();
140 int nclasses = (int)classLabels.size();
141 double eps = (rparams.termCrit.type & TermCriteria::EPS) != 0 &&
142 rparams.termCrit.epsilon > 0 ? rparams.termCrit.epsilon : 0.;
144 vector<uchar> oobmask(n);
147 vector<double> oobres(n, 0.);
148 vector<int> oobcount(n, 0);
149 vector<int> oobvotes(n*nclasses, 0);
150 int nvars = w->data->getNVars();
151 int nallvars = w->data->getNAllVars();
152 const int* vidx = !varIdx.empty() ? &varIdx[0] : 0;
153 vector<float> samplebuf(nallvars);
154 Mat samples = w->data->getSamples();
155 float* psamples = samples.ptr<float>();
156 size_t sstep0 = samples.step1(), sstep1 = 1;
157 Mat sample0, sample(nallvars, 1, CV_32F, &samplebuf[0]);
158 int predictFlags = _isClassifier ? (PREDICT_MAX_VOTE + RAW_OUTPUT) : PREDICT_SUM;
160 bool calcOOBError = eps > 0 || rparams.calcVarImportance;
161 double max_response = 0.;
163 if( w->data->getLayout() == COL_SAMPLE )
164 std::swap(sstep0, sstep1);
168 for( i = 0; i < n; i++ )
170 double val = std::abs(w->ord_responses[w->sidx[i]]);
171 max_response = std::max(max_response, val);
175 if( rparams.calcVarImportance )
176 varImportance.resize(nallvars, 0.f);
178 for( treeidx = 0; treeidx < ntrees; treeidx++ )
180 for( i = 0; i < n; i++ )
181 oobmask[i] = (uchar)1;
183 for( i = 0; i < n; i++ )
185 j = rng.uniform(0, n);
186 sidx[i] = w->sidx[j];
187 oobmask[j] = (uchar)0;
189 int root = addTree( sidx );
196 for( i = 0; i < n; i++ )
201 int n_oob = (int)oobidx.size();
202 // if there is no out-of-bag samples, we can not compute OOB error
203 // nor update the variable importance vector; so we proceed to the next tree
206 double ncorrect_responses = 0.;
209 for( i = 0; i < n_oob; i++ )
212 sample = Mat( nallvars, 1, CV_32F, psamples + sstep0*w->sidx[j], sstep1*sizeof(psamples[0]) );
214 double val = predictTrees(Range(treeidx, treeidx+1), sample, predictFlags);
219 double true_val = w->ord_responses[w->sidx[j]];
220 double a = oobres[j]/oobcount[j] - true_val;
222 val = (val - true_val)/max_response;
223 ncorrect_responses += std::exp( -val*val );
227 int ival = cvRound(val);
228 int* votes = &oobvotes[j*nclasses];
231 for( k = 1; k < nclasses; k++ )
232 if( votes[best_class] < votes[k] )
234 int diff = best_class != w->cat_responses[w->sidx[j]];
236 ncorrect_responses += diff == 0;
241 if( rparams.calcVarImportance && n_oob > 1 )
243 oobperm.resize(n_oob);
244 for( i = 0; i < n_oob; i++ )
245 oobperm[i] = oobidx[i];
247 for( vi_ = 0; vi_ < nvars; vi_++ )
249 vi = vidx ? vidx[vi_] : vi_;
250 double ncorrect_responses_permuted = 0;
251 for( i = 0; i < n_oob; i++ )
253 int i1 = rng.uniform(0, n_oob);
254 int i2 = rng.uniform(0, n_oob);
258 for( i = 0; i < n_oob; i++ )
262 sample0 = Mat( nallvars, 1, CV_32F, psamples + sstep0*w->sidx[j], sstep1*sizeof(psamples[0]) );
263 for( k = 0; k < nallvars; k++ )
264 sample.at<float>(k) = sample0.at<float>(k);
265 sample.at<float>(vi) = psamples[sstep0*w->sidx[vj] + sstep1*vi];
267 double val = predictTrees(Range(treeidx, treeidx+1), sample, predictFlags);
270 val = (val - w->ord_responses[w->sidx[j]])/max_response;
271 ncorrect_responses_permuted += exp( -val*val );
274 ncorrect_responses_permuted += cvRound(val) == w->cat_responses[w->sidx[j]];
276 varImportance[vi] += (float)(ncorrect_responses - ncorrect_responses_permuted);
280 if( calcOOBError && oobError < eps )
284 if( rparams.calcVarImportance )
286 for( vi_ = 0; vi_ < nallvars; vi_++ )
287 varImportance[vi_] = std::max(varImportance[vi_], 0.f);
288 normalize(varImportance, varImportance, 1., 0, NORM_L1);
294 void writeTrainingParams( FileStorage& fs ) const
296 DTreesImpl::writeTrainingParams(fs);
297 fs << "nactive_vars" << rparams.nactiveVars;
300 void write( FileStorage& fs ) const
303 CV_Error( CV_StsBadArg, "RTrees have not been trained" );
307 fs << "oob_error" << oobError;
308 if( !varImportance.empty() )
309 fs << "var_importance" << varImportance;
311 int k, ntrees = (int)roots.size();
313 fs << "ntrees" << ntrees
316 for( k = 0; k < ntrees; k++ )
319 writeTree(fs, roots[k]);
326 void readParams( const FileNode& fn )
328 DTreesImpl::readParams(fn);
329 rparams.maxDepth = params0.maxDepth;
330 rparams.minSampleCount = params0.minSampleCount;
331 rparams.regressionAccuracy = params0.regressionAccuracy;
332 rparams.useSurrogates = params0.useSurrogates;
333 rparams.maxCategories = params0.maxCategories;
334 rparams.priors = params0.priors;
336 FileNode tparams_node = fn["training_params"];
337 rparams.nactiveVars = (int)tparams_node["nactive_vars"];
340 void read( const FileNode& fn )
344 //int nclasses = (int)fn["nclasses"];
345 //int nsamples = (int)fn["nsamples"];
346 oobError = (double)fn["oob_error"];
347 int ntrees = (int)fn["ntrees"];
349 fn["var_importance"] >> varImportance;
353 FileNode trees_node = fn["trees"];
354 FileNodeIterator it = trees_node.begin();
355 CV_Assert( ntrees == (int)trees_node.size() );
357 for( int treeidx = 0; treeidx < ntrees; treeidx++, ++it )
359 FileNode nfn = (*it)["nodes"];
364 RTrees::Params rparams;
366 vector<float> varImportance;
367 vector<int> allVars, activeVars;
372 class RTreesImpl : public RTrees
376 virtual ~RTreesImpl() {}
378 String getDefaultModelName() const { return "opencv_ml_rtrees"; }
380 bool train( const Ptr<TrainData>& trainData, int flags )
382 return impl.train(trainData, flags);
385 float predict( InputArray samples, OutputArray results, int flags ) const
387 return impl.predict(samples, results, flags);
390 void write( FileStorage& fs ) const
395 void read( const FileNode& fn )
400 void setRParams(const Params& p) { impl.setRParams(p); }
401 Params getRParams() const { return impl.getRParams(); }
403 Mat getVarImportance() const { return Mat_<float>(impl.varImportance, true); }
404 int getVarCount() const { return impl.getVarCount(); }
406 bool isTrained() const { return impl.isTrained(); }
407 bool isClassifier() const { return impl.isClassifier(); }
409 const vector<int>& getRoots() const { return impl.getRoots(); }
410 const vector<Node>& getNodes() const { return impl.getNodes(); }
411 const vector<Split>& getSplits() const { return impl.getSplits(); }
412 const vector<int>& getSubsets() const { return impl.getSubsets(); }
414 DTreesImplForRTrees impl;
418 Ptr<RTrees> RTrees::create(const Params& params)
420 Ptr<RTreesImpl> p = makePtr<RTreesImpl>();
421 p->setRParams(params);