boost.cpp 17.7 KB
Newer Older
1 2 3 4 5 6 7 8 9
/*M///////////////////////////////////////////////////////////////////////////////////////
//
//  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
//
//  By downloading, copying, installing or using the software you agree to this license.
//  If you do not agree to this license, do not download, install,
//  copy or use the software.
//
//
10 11
//                           License Agreement
//                For Open Source Computer Vision Library
12 13
//
// Copyright (C) 2000, Intel Corporation, all rights reserved.
14
// Copyright (C) 2014, Itseez Inc, all rights reserved.
15 16 17 18 19 20 21 22 23 24 25 26
// Third party copyrights are property of their respective owners.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
//   * Redistribution's of source code must retain the above copyright notice,
//     this list of conditions and the following disclaimer.
//
//   * Redistribution's in binary form must reproduce the above copyright notice,
//     this list of conditions and the following disclaimer in the documentation
//     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 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
//     derived from this software without specific prior written permission.
//
// This software is provided by the copyright holders and contributors "as is" and
// any express or implied warranties, including, but not limited to, the implied
// warranties of merchantability and fitness for a particular purpose are disclaimed.
// In no event shall the Intel Corporation or contributors be liable for any direct,
// indirect, incidental, special, exemplary, or consequential damages
// (including, but not limited to, procurement of substitute goods or services;
// loss of use, data, or profits; or business interruption) however caused
// and on any theory of liability, whether in contract, strict liability,
// or tort (including negligence or otherwise) arising in any way out of
// the use of this software, even if advised of the possibility of such damage.
//
//M*/

#include "precomp.hpp"

45 46
namespace cv { namespace ml {

47 48 49 50
static inline double
log_ratio( double val )
{
    const double eps = 1e-5;
51 52
    val = std::max( val, eps );
    val = std::min( val, 1. - eps );
53 54 55 56
    return log( val/(1. - val) );
}


57
BoostTreeParams::BoostTreeParams()
58
{
59 60 61
    boostType = Boost::REAL;
    weakCount = 100;
    weightTrimRate = 0.95;
62 63
}

64 65
BoostTreeParams::BoostTreeParams( int _boostType, int _weak_count,
                                  double _weightTrimRate)
66
{
67 68 69
    boostType = _boostType;
    weakCount = _weak_count;
    weightTrimRate = _weightTrimRate;
70 71
}

72
class DTreesImplForBoost CV_FINAL : public DTreesImpl
73 74
{
public:
75
    DTreesImplForBoost()
76
    {
77 78
        params.setCVFolds(0);
        params.setMaxDepth(1);
79
    }
80
    virtual ~DTreesImplForBoost() {}
81

82
    bool isClassifier() const CV_OVERRIDE { return true; }
83

84
    void clear() CV_OVERRIDE
85
    {
86
        DTreesImpl::clear();
87 88
    }

89
    void startTraining( const Ptr<TrainData>& trainData, int flags ) CV_OVERRIDE
90
    {
91
        DTreesImpl::startTraining(trainData, flags);
92
        sumResult.assign(w->sidx.size(), 0.);
93

94
        if( bparams.boostType != Boost::DISCRETE )
95
        {
96 97 98
            _isClassifier = false;
            int i, n = (int)w->cat_responses.size();
            w->ord_responses.resize(n);
99

100
            double a = -1, b = 1;
101
            if( bparams.boostType == Boost::LOGIT )
102
            {
103
                a = -2, b = 2;
104
            }
105 106
            for( i = 0; i < n; i++ )
                w->ord_responses[i] = w->cat_responses[i] > 0 ? b : a;
107 108
        }

109
        normalizeWeights();
110 111
    }

112
    void normalizeWeights()
113
    {
114 115
        int i, n = (int)w->sidx.size();
        double sumw = 0, a, b;
116
        for( i = 0; i < n; i++ )
117 118
            sumw += w->sample_weights[w->sidx[i]];
        if( sumw > DBL_EPSILON )
119
        {
120 121
            a = 1./sumw;
            b = 0;
122 123
        }
        else
124 125 126 127 128 129 130 131 132
        {
            a = 0;
            b = 1;
        }
        for( i = 0; i < n; i++ )
        {
            double& wval = w->sample_weights[w->sidx[i]];
            wval = wval*a + b;
        }
133 134
    }

135
    void endTraining() CV_OVERRIDE
136
    {
137 138 139
        DTreesImpl::endTraining();
        vector<double> e;
        std::swap(sumResult, e);
140
    }
141 142

    void scaleTree( int root, double scale )
143
    {
144 145
        int nidx = root, pidx = 0;
        Node *node = 0;
146

147 148
        // traverse the tree and save all the nodes in depth-first order
        for(;;)
149
        {
150
            for(;;)
151
            {
152 153 154 155 156
                node = &nodes[nidx];
                node->value *= scale;
                if( node->left < 0 )
                    break;
                nidx = node->left;
157 158
            }

159 160 161
            for( pidx = node->parent; pidx >= 0 && nodes[pidx].right == nidx;
                 nidx = pidx, pidx = nodes[pidx].parent )
                ;
162

163 164
            if( pidx < 0 )
                break;
165

166
            nidx = nodes[pidx].right;
167
        }
168
    }
169

170
    void calcValue( int nidx, const vector<int>& _sidx ) CV_OVERRIDE
171 172 173 174
    {
        DTreesImpl::calcValue(nidx, _sidx);
        WNode* node = &w->wnodes[nidx];
        if( bparams.boostType == Boost::DISCRETE )
175
        {
176
            node->value = node->class_idx == 0 ? -1 : 1;
177
        }
178
        else if( bparams.boostType == Boost::REAL )
179
        {
180
            double p = (node->value+1)*0.5;
181
            node->value = 0.5*log_ratio(p);
182 183
        }
    }
184

185
    bool train( const Ptr<TrainData>& trainData, int flags ) CV_OVERRIDE
186 187 188 189
    {
        startTraining(trainData, flags);
        int treeidx, ntrees = bparams.weakCount >= 0 ? bparams.weakCount : 10000;
        vector<int> sidx = w->sidx;
190

191 192 193 194 195 196 197 198 199 200
        for( treeidx = 0; treeidx < ntrees; treeidx++ )
        {
            int root = addTree( sidx );
            if( root < 0 )
                return false;
            updateWeightsAndTrim( treeidx, sidx );
        }
        endTraining();
        return true;
    }
Andrey Kamaev's avatar
Andrey Kamaev committed
201

202 203 204 205
    void updateWeightsAndTrim( int treeidx, vector<int>& sidx )
    {
        int i, n = (int)w->sidx.size();
        int nvars = (int)varIdx.size();
206
        double sumw = 0., C = 1.;
207
        cv::AutoBuffer<double> buf(n + nvars);
208
        double* result = buf.data();
209
        float* sbuf = (float*)(result + n);
210 211 212
        Mat sample(1, nvars, CV_32F, sbuf);
        int predictFlags = bparams.boostType == Boost::DISCRETE ? (PREDICT_MAX_VOTE | RAW_OUTPUT) : PREDICT_SUM;
        predictFlags |= COMPRESSED_INPUT;
213

214 215 216 217
        for( i = 0; i < n; i++ )
        {
            w->data->getSample(varIdx, w->sidx[i], sbuf );
            result[i] = predictTrees(Range(treeidx, treeidx+1), sample, predictFlags);
218 219 220
        }

        // now update weights and other parameters for each type of boosting
221
        if( bparams.boostType == Boost::DISCRETE )
222 223 224 225 226 227
        {
            // Discrete AdaBoost:
            //   weak_eval[i] (=f(x_i)) is in {-1,1}
            //   err = sum(w_i*(f(x_i) != y_i))/sum(w_i)
            //   C = log((1-err)/err)
            //   w_i *= exp(C*(f(x_i) != y_i))
228
            double err = 0.;
229 230 231

            for( i = 0; i < n; i++ )
            {
232 233 234 235
                int si = w->sidx[i];
                double wval = w->sample_weights[si];
                sumw += wval;
                err += wval*(result[i] != w->cat_responses[si]);
236 237 238 239
            }

            if( sumw != 0 )
                err /= sumw;
240
            C = -log_ratio( err );
241
            double scale = std::exp(C);
242 243 244 245

            sumw = 0;
            for( i = 0; i < n; i++ )
            {
246 247 248 249 250 251
                int si = w->sidx[i];
                double wval = w->sample_weights[si];
                if( result[i] != w->cat_responses[si] )
                    wval *= scale;
                sumw += wval;
                w->sample_weights[si] = wval;
252 253
            }

254
            scaleTree(roots[treeidx], C);
255
        }
256
        else if( bparams.boostType == Boost::REAL || bparams.boostType == Boost::GENTLE )
257 258 259 260 261
        {
            // Real AdaBoost:
            //   weak_eval[i] = f(x_i) = 0.5*log(p(x_i)/(1-p(x_i))), p(x_i)=P(y=1|x_i)
            //   w_i *= exp(-y_i*f(x_i))

262 263 264
            // Gentle AdaBoost:
            //   weak_eval[i] = f(x_i) in [-1,1]
            //   w_i *= exp(-y_i*f(x_i))
265 266
            for( i = 0; i < n; i++ )
            {
267
                int si = w->sidx[i];
268
                CV_Assert( std::abs(w->ord_responses[si]) == 1 );
269 270 271
                double wval = w->sample_weights[si]*std::exp(-result[i]*w->ord_responses[si]);
                sumw += wval;
                w->sample_weights[si] = wval;
272 273
            }
        }
274
        else if( bparams.boostType == Boost::LOGIT )
275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
        {
            // LogitBoost:
            //   weak_eval[i] = f(x_i) in [-z_max,z_max]
            //   sum_response = F(x_i).
            //   F(x_i) += 0.5*f(x_i)
            //   p(x_i) = exp(F(x_i))/(exp(F(x_i)) + exp(-F(x_i))=1/(1+exp(-2*F(x_i)))
            //   reuse weak_eval: weak_eval[i] <- p(x_i)
            //   w_i = p(x_i)*1(1 - p(x_i))
            //   z_i = ((y_i+1)/2 - p(x_i))/(p(x_i)*(1 - p(x_i)))
            //   store z_i to the data->data_root as the new target responses
            const double lb_weight_thresh = FLT_EPSILON;
            const double lb_z_max = 10.;

            for( i = 0; i < n; i++ )
            {
290 291 292 293 294 295 296
                int si = w->sidx[i];
                sumResult[i] += 0.5*result[i];
                double p = 1./(1 + std::exp(-2*sumResult[i]));
                double wval = std::max( p*(1 - p), lb_weight_thresh ), z;
                w->sample_weights[si] = wval;
                sumw += wval;
                if( w->ord_responses[si] > 0 )
297 298
                {
                    z = 1./p;
299
                    w->ord_responses[si] = std::min(z, lb_z_max);
300 301 302 303
                }
                else
                {
                    z = 1./(1-p);
304
                    w->ord_responses[si] = -std::min(z, lb_z_max);
305 306 307 308
                }
            }
        }
        else
309
            CV_Error(CV_StsNotImplemented, "Unknown boosting type");
310 311 312 313 314 315 316 317 318 319 320 321 322 323

        /*if( bparams.boostType != Boost::LOGIT )
        {
            double err = 0;
            for( i = 0; i < n; i++ )
            {
                sumResult[i] += result[i]*C;
                if( bparams.boostType != Boost::DISCRETE )
                    err += sumResult[i]*w->ord_responses[w->sidx[i]] < 0;
                else
                    err += sumResult[i]*w->cat_responses[w->sidx[i]] < 0;
            }
            printf("%d trees. C=%.2f, training error=%.1f%%, working set size=%d (out of %d)\n", (int)roots.size(), C, err*100./n, (int)sidx.size(), n);
        }*/
324

325 326 327
        // renormalize weights
        if( sumw > FLT_EPSILON )
            normalizeWeights();
Andrey Kamaev's avatar
Andrey Kamaev committed
328

329 330
        if( bparams.weightTrimRate <= 0. || bparams.weightTrimRate >= 1. )
            return;
331

332 333 334
        for( i = 0; i < n; i++ )
            result[i] = w->sample_weights[w->sidx[i]];
        std::sort(result, result + n);
Andrey Kamaev's avatar
Andrey Kamaev committed
335

336 337 338
        // as weight trimming occurs immediately after updating the weights,
        // where they are renormalized, we assume that the weight sum = 1.
        sumw = 1. - bparams.weightTrimRate;
339

340
        for( i = 0; i < n; i++ )
341
        {
342 343 344 345
            double wval = result[i];
            if( sumw <= 0 )
                break;
            sumw -= wval;
346 347
        }

348 349
        double threshold = i < n ? result[i] : DBL_MAX;
        sidx.clear();
Andrey Kamaev's avatar
Andrey Kamaev committed
350

351
        for( i = 0; i < n; i++ )
352
        {
353 354 355
            int si = w->sidx[i];
            if( w->sample_weights[si] >= threshold )
                sidx.push_back(si);
356 357 358
        }
    }

359
    float predictTrees( const Range& range, const Mat& sample, int flags0 ) const CV_OVERRIDE
360
    {
361 362 363 364 365 366 367 368 369 370
        int flags = (flags0 & ~PREDICT_MASK) | PREDICT_SUM;
        float val = DTreesImpl::predictTrees(range, sample, flags);
        if( flags != flags0 )
        {
            int ival = (int)(val > 0);
            if( !(flags0 & RAW_OUTPUT) )
                ival = classLabels[ival];
            val = (float)ival;
        }
        return val;
371 372
    }

373
    void writeTrainingParams( FileStorage& fs ) const CV_OVERRIDE
374
    {
375 376 377 378 379
        fs << "boosting_type" <<
        (bparams.boostType == Boost::DISCRETE ? "DiscreteAdaboost" :
        bparams.boostType == Boost::REAL ? "RealAdaboost" :
        bparams.boostType == Boost::LOGIT ? "LogitBoost" :
        bparams.boostType == Boost::GENTLE ? "GentleAdaboost" : "Unknown");
380

381 382
        DTreesImpl::writeTrainingParams(fs);
        fs << "weight_trimming_rate" << bparams.weightTrimRate;
383
    }
Andrey Kamaev's avatar
Andrey Kamaev committed
384

385
    void write( FileStorage& fs ) const CV_OVERRIDE
386
    {
387 388
        if( roots.empty() )
            CV_Error( CV_StsBadArg, "RTrees have not been trained" );
389

390
        writeFormat(fs);
391
        writeParams(fs);
Maria Dimashova's avatar
Maria Dimashova committed
392

393
        int k, ntrees = (int)roots.size();
394

395 396
        fs << "ntrees" << ntrees
        << "trees" << "[";
397

398
        for( k = 0; k < ntrees; k++ )
399
        {
400 401 402
            fs << "{";
            writeTree(fs, roots[k]);
            fs << "}";
403 404
        }

405
        fs << "]";
406 407
    }

408
    void readParams( const FileNode& fn ) CV_OVERRIDE
409
    {
410
        DTreesImpl::readParams(fn);
411

412
        FileNode tparams_node = fn["training_params"];
413 414 415
        // check for old layout
        String bts = (String)(fn["boosting_type"].empty() ?
                         tparams_node["boosting_type"] : fn["boosting_type"]);
416 417 418 419 420
        bparams.boostType = (bts == "DiscreteAdaboost" ? Boost::DISCRETE :
                             bts == "RealAdaboost" ? Boost::REAL :
                             bts == "LogitBoost" ? Boost::LOGIT :
                             bts == "GentleAdaboost" ? Boost::GENTLE : -1);
        _isClassifier = bparams.boostType == Boost::DISCRETE;
421 422 423
        // check for old layout
        bparams.weightTrimRate = (double)(fn["weight_trimming_rate"].empty() ?
                                    tparams_node["weight_trimming_rate"] : fn["weight_trimming_rate"]);
424 425
    }

426
    void read( const FileNode& fn ) CV_OVERRIDE
427
    {
428
        clear();
429

430 431
        int ntrees = (int)fn["ntrees"];
        readParams(fn);
432

433 434 435
        FileNode trees_node = fn["trees"];
        FileNodeIterator it = trees_node.begin();
        CV_Assert( ntrees == (int)trees_node.size() );
436

437
        for( int treeidx = 0; treeidx < ntrees; treeidx++, ++it )
438
        {
439 440
            FileNode nfn = (*it)["nodes"];
            readTree(nfn);
441 442
        }
    }
443

444
    BoostTreeParams bparams;
445 446
    vector<double> sumResult;
};
447 448


449
class BoostImpl : public Boost
450
{
451 452 453
public:
    BoostImpl() {}
    virtual ~BoostImpl() {}
454

455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483
    inline int getBoostType() const CV_OVERRIDE { return impl.bparams.boostType; }
    inline void setBoostType(int val) CV_OVERRIDE { impl.bparams.boostType = val; }
    inline int getWeakCount() const CV_OVERRIDE { return impl.bparams.weakCount; }
    inline void setWeakCount(int val) CV_OVERRIDE { impl.bparams.weakCount = val; }
    inline double getWeightTrimRate() const CV_OVERRIDE { return impl.bparams.weightTrimRate; }
    inline void setWeightTrimRate(double val) CV_OVERRIDE { impl.bparams.weightTrimRate = val; }

    inline int getMaxCategories() const CV_OVERRIDE { return impl.params.getMaxCategories(); }
    inline void setMaxCategories(int val) CV_OVERRIDE { impl.params.setMaxCategories(val); }
    inline int getMaxDepth() const CV_OVERRIDE { return impl.params.getMaxDepth(); }
    inline void setMaxDepth(int val) CV_OVERRIDE { impl.params.setMaxDepth(val); }
    inline int getMinSampleCount() const CV_OVERRIDE { return impl.params.getMinSampleCount(); }
    inline void setMinSampleCount(int val) CV_OVERRIDE { impl.params.setMinSampleCount(val); }
    inline int getCVFolds() const CV_OVERRIDE { return impl.params.getCVFolds(); }
    inline void setCVFolds(int val) CV_OVERRIDE { impl.params.setCVFolds(val); }
    inline bool getUseSurrogates() const CV_OVERRIDE { return impl.params.getUseSurrogates(); }
    inline void setUseSurrogates(bool val) CV_OVERRIDE { impl.params.setUseSurrogates(val); }
    inline bool getUse1SERule() const CV_OVERRIDE { return impl.params.getUse1SERule(); }
    inline void setUse1SERule(bool val) CV_OVERRIDE { impl.params.setUse1SERule(val); }
    inline bool getTruncatePrunedTree() const CV_OVERRIDE { return impl.params.getTruncatePrunedTree(); }
    inline void setTruncatePrunedTree(bool val) CV_OVERRIDE { impl.params.setTruncatePrunedTree(val); }
    inline float getRegressionAccuracy() const CV_OVERRIDE { return impl.params.getRegressionAccuracy(); }
    inline void setRegressionAccuracy(float val) CV_OVERRIDE { impl.params.setRegressionAccuracy(val); }
    inline cv::Mat getPriors() const CV_OVERRIDE { return impl.params.getPriors(); }
    inline void setPriors(const cv::Mat& val) CV_OVERRIDE { impl.params.setPriors(val); }

    String getDefaultName() const CV_OVERRIDE { return "opencv_ml_boost"; }

    bool train( const Ptr<TrainData>& trainData, int flags ) CV_OVERRIDE
484
    {
485
        return impl.train(trainData, flags);
486 487
    }

488
    float predict( InputArray samples, OutputArray results, int flags ) const CV_OVERRIDE
489
    {
490
        return impl.predict(samples, results, flags);
491 492
    }

493
    void write( FileStorage& fs ) const CV_OVERRIDE
494
    {
495
        impl.write(fs);
496 497
    }

498
    void read( const FileNode& fn ) CV_OVERRIDE
499
    {
500
        impl.read(fn);
501 502
    }

503
    int getVarCount() const CV_OVERRIDE { return impl.getVarCount(); }
504

505 506
    bool isTrained() const CV_OVERRIDE { return impl.isTrained(); }
    bool isClassifier() const CV_OVERRIDE { return impl.isClassifier(); }
507

508 509 510 511
    const vector<int>& getRoots() const CV_OVERRIDE { return impl.getRoots(); }
    const vector<Node>& getNodes() const CV_OVERRIDE { return impl.getNodes(); }
    const vector<Split>& getSplits() const CV_OVERRIDE { return impl.getSplits(); }
    const vector<int>& getSubsets() const CV_OVERRIDE { return impl.getSubsets(); }
512

513 514
    DTreesImplForBoost impl;
};
515 516


517
Ptr<Boost> Boost::create()
518
{
519
    return makePtr<BoostImpl>();
520 521
}

522 523 524 525 526
Ptr<Boost> Boost::load(const String& filepath, const String& nodeName)
{
    return Algorithm::load<Boost>(filepath, nodeName);
}

527
}}
528 529

/* End of file. */