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-2008, Intel Corporation, all rights reserved.
14 // Copyright (C) 2008-2013, Willow Garage 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"
44 #include "opencv2/ml.hpp"
48 using cv::OutputArray;
51 using cv::softcascade::Octave;
52 using cv::softcascade::FeaturePool;
53 using cv::softcascade::Dataset;
54 using cv::softcascade::ChannelFeatureBuilder;
56 FeaturePool::~FeaturePool(){}
61 class BoostedSoftCascadeOctave : public cv::Boost, public Octave
65 BoostedSoftCascadeOctave(cv::Rect boundingBox = cv::Rect(), int npositives = 0, int nnegatives = 0, int logScale = 0,
66 int shrinkage = 1, cv::Ptr<ChannelFeatureBuilder> builder = ChannelFeatureBuilder::create("HOG6MagLuv"));
67 virtual ~BoostedSoftCascadeOctave();
68 virtual cv::AlgorithmInfo* info() const;
69 virtual bool train(const Dataset* dataset, const FeaturePool* pool, int weaks, int treeDepth);
70 virtual void setRejectThresholds(OutputArray thresholds);
71 virtual void write( cv::FileStorage &fs, const FeaturePool* pool, InputArray thresholds) const;
72 virtual void write( CvFileStorage* fs, cv::String name) const;
74 virtual float predict( InputArray _sample, InputArray _votes, bool raw_mode, bool return_sum ) const;
75 virtual bool train( const cv::Mat& trainData, const cv::Mat& responses, const cv::Mat& varIdx=cv::Mat(),
76 const cv::Mat& sampleIdx=cv::Mat(), const cv::Mat& varType=cv::Mat(), const cv::Mat& missingDataMask=cv::Mat());
78 void processPositives(const Dataset* dataset);
79 void generateNegatives(const Dataset* dataset);
81 float predict( const Mat& _sample, const cv::Range range) const;
83 void traverse(const CvBoostTree* tree, cv::FileStorage& fs, int& nfeatures, int* used, const double* th) const;
84 virtual void initialize_weights(double (&p)[2]);
101 cv::Ptr<ChannelFeatureBuilder> builder;
104 BoostedSoftCascadeOctave::BoostedSoftCascadeOctave(cv::Rect bb, int np, int nn, int ls, int shr,
105 cv::Ptr<ChannelFeatureBuilder> _builder)
106 : logScale(ls), boundingBox(bb), npositives(np), nnegatives(nn), shrinkage(shr)
108 int maxSample = npositives + nnegatives;
109 responses.create(maxSample, 1, CV_32FC1);
111 CvBoostParams _params;
114 _params.max_categories = 10;
115 _params.max_depth = 2;
116 _params.cv_folds = 0;
117 _params.truncate_pruned_tree = false;
118 _params.use_surrogates = false;
119 _params.use_1se_rule = false;
120 _params.regression_accuracy = 0;
123 _params.boost_type = CvBoost::GENTLE;
124 _params.split_criteria = CvBoost::SQERR;
125 _params.weight_trim_rate = 0.95;
128 _params.min_sample_count = 0;
129 _params.weak_count = 1;
136 int w = boundingBox.width;
137 int h = boundingBox.height;
139 integrals.create(npositives + nnegatives, (w / shrinkage + 1) * (h / shrinkage * builder->totalChannels() + 1), CV_32SC1);
142 BoostedSoftCascadeOctave::~BoostedSoftCascadeOctave(){}
144 bool BoostedSoftCascadeOctave::train( const cv::Mat& _trainData, const cv::Mat& _responses, const cv::Mat& varIdx,
145 const cv::Mat& sampleIdx, const cv::Mat& varType, const cv::Mat& missingDataMask)
148 return cv::Boost::train(_trainData, CV_COL_SAMPLE, _responses, varIdx, sampleIdx, varType, missingDataMask, params,
152 void BoostedSoftCascadeOctave::setRejectThresholds(cv::OutputArray _thresholds)
154 // labels decided by classifier
155 cv::Mat desisions(responses.cols, responses.rows, responses.type());
156 float* dptr = desisions.ptr<float>(0);
158 // mask of samples satisfying the condition
159 cv::Mat ppmask(responses.cols, responses.rows, CV_8UC1);
160 uchar* mptr = ppmask.ptr<uchar>(0);
162 int nsamples = npositives + nnegatives;
166 for (int si = 0; si < nsamples; ++si)
168 float decision = dptr[si] = predict(trainData.col(si), stab, false, false);
169 mptr[si] = cv::saturate_cast<uchar>((unsigned int)( (responses.ptr<float>(si)[0] == 1.f) && (decision == 1.f)));
172 int weaks = weak->total;
173 _thresholds.create(1, weaks, CV_64FC1);
174 cv::Mat& thresholds = _thresholds.getMatRef();
175 double* thptr = thresholds.ptr<double>(0);
177 cv::Mat traces(weaks, nsamples, CV_64FC1, cv::Scalar::all(FLT_MAX));
179 for (int w = 0; w < weaks; ++w)
181 double* rptr = traces.ptr<double>(w);
182 for (int si = 0; si < nsamples; ++si)
184 cv::Range curr(0, w + 1);
187 float trace = predict(trainData.col(si), curr);
191 double mintrace = 0.;
192 cv::minMaxLoc(traces.row(w), &mintrace);
197 void BoostedSoftCascadeOctave::processPositives(const Dataset* dataset)
199 int h = boundingBox.height;
201 ChannelFeatureBuilder& _builder = *builder;
204 for (int curr = 0; curr < dataset->available( Dataset::POSITIVE); ++curr)
206 cv::Mat sample = dataset->get( Dataset::POSITIVE, curr);
208 cv::Mat channels = integrals.row(total).reshape(0, h / shrinkage * builder->totalChannels() + 1);
209 sample = sample(boundingBox);
211 _builder(sample, channels);
212 responses.ptr<float>(total)[0] = 1.f;
214 if (++total >= npositives) break;
217 nnegatives = cvRound(nnegatives * total / (double)npositives);
220 void BoostedSoftCascadeOctave::generateNegatives(const Dataset* dataset)
222 using namespace cv::softcascade::internal;
223 // ToDo: set seed, use offsets
224 Random::engine eng(DX_DY_SEED);
225 Random::engine idxEng((Random::seed_type)INDEX_ENGINE_SEED);
227 int h = boundingBox.height;
229 int nimages = dataset->available(Dataset::NEGATIVE);
230 Random::uniform iRand(0, nimages - 1);
235 ChannelFeatureBuilder& _builder = *builder;
236 for (int i = npositives; i < nnegatives + npositives; ++total)
238 int curr = iRand(idxEng);
240 Mat frame = dataset->get(Dataset::NEGATIVE, curr);
242 int maxW = frame.cols - 2 * boundingBox.x - boundingBox.width;
243 int maxH = frame.rows - 2 * boundingBox.y - boundingBox.height;
245 Random::uniform wRand(0, maxW -1);
246 Random::uniform hRand(0, maxH -1);
251 frame = frame(cv::Rect(dx, dy, boundingBox.width, boundingBox.height));
253 cv::Mat channels = integrals.row(i).reshape(0, h / shrinkage * builder->totalChannels() + 1);
254 _builder(frame, channels);
256 // // if (predict(sum))
258 responses.ptr<float>(i)[0] = 0.f;
265 template <typename T> int sgn(T val) {
266 return (T(0) < val) - (val < T(0));
269 void BoostedSoftCascadeOctave::traverse(const CvBoostTree* tree, cv::FileStorage& fs, int& nfeatures, int* used, const double* th) const
271 std::queue<const CvDTreeNode*> nodes;
272 nodes.push( tree->get_root());
273 const CvDTreeNode* tempNode;
275 int internalNodeIdx = 1;
276 float* leafs = new float[(int)pow(2.f, get_params().max_depth)];
279 fs << "treeThreshold" << *th;
280 fs << "internalNodes" << "[";
281 while (!nodes.empty())
283 tempNode = nodes.front();
284 CV_Assert( tempNode->left );
285 if ( !tempNode->left->left && !tempNode->left->right)
287 leafs[-leafValIdx] = (float)tempNode->left->value;
292 nodes.push( tempNode->left );
293 fs << internalNodeIdx++;
295 CV_Assert( tempNode->right );
296 if ( !tempNode->right->left && !tempNode->right->right)
298 leafs[-leafValIdx] = (float)tempNode->right->value;
303 nodes.push( tempNode->right );
304 fs << internalNodeIdx++;
307 int fidx = tempNode->split->var_idx;
309 used[nfeatures++] = fidx;
311 fs << tempNode->split->ord.c;
317 fs << "leafValues" << "[";
318 for (int ni = 0; ni < -leafValIdx; ni++)
328 void BoostedSoftCascadeOctave::write( cv::FileStorage &fso, const FeaturePool* pool, InputArray _thresholds) const
330 CV_Assert(!_thresholds.empty());
331 cv::Mat used( 1, weak->total * ( (int)pow(2.f, params.max_depth) - 1), CV_32SC1);
332 int* usedPtr = used.ptr<int>(0);
334 cv::Mat thresholds = _thresholds.getMat();
336 << "scale" << logScale
337 << "weaks" << weak->total
339 // should be replaced with the H.L. one
341 cvStartReadSeq( weak, &reader);
343 for(int i = 0; i < weak->total; i++ )
346 CV_READ_SEQ_ELEM( tree, reader );
348 traverse(tree, fso, nfeatures, usedPtr, thresholds.ptr<double>(0) + i);
353 fso << "features" << "[";
354 for (int i = 0; i < nfeatures; ++i)
355 pool->write(fso, usedPtr[i]);
360 void BoostedSoftCascadeOctave::initialize_weights(double (&p)[2])
362 double n = data->sample_count;
363 p[0] = n / (2. * (double)(nnegatives));
364 p[1] = n / (2. * (double)(npositives));
367 bool BoostedSoftCascadeOctave::train(const Dataset* dataset, const FeaturePool* pool, int weaks, int treeDepth)
369 CV_Assert(treeDepth == 2);
370 CV_Assert(weaks > 0);
372 params.max_depth = treeDepth;
373 params.weak_count = weaks;
375 // 1. fill integrals and classes
376 processPositives(dataset);
377 generateNegatives(dataset);
379 // 2. only simple case (all features used)
380 int nfeatures = pool->size();
381 cv::Mat varIdx(1, nfeatures, CV_32SC1);
382 int* ptr = varIdx.ptr<int>(0);
384 for (int x = 0; x < nfeatures; ++x)
387 // 3. only simple case (all samples used)
388 int nsamples = npositives + nnegatives;
389 cv::Mat sampleIdx(1, nsamples, CV_32SC1);
390 ptr = sampleIdx.ptr<int>(0);
392 for (int x = 0; x < nsamples; ++x)
395 // 4. ICF has an ordered response.
396 cv::Mat varType(1, nfeatures + 1, CV_8UC1);
397 uchar* uptr = varType.ptr<uchar>(0);
398 for (int x = 0; x < nfeatures; ++x)
399 uptr[x] = CV_VAR_ORDERED;
400 uptr[nfeatures] = CV_VAR_CATEGORICAL;
402 trainData.create(nfeatures, nsamples, CV_32FC1);
403 for (int fi = 0; fi < nfeatures; ++fi)
405 float* dptr = trainData.ptr<float>(fi);
406 for (int si = 0; si < nsamples; ++si)
408 dptr[si] = pool->apply(fi, si, integrals);
414 bool ok = train(trainData, responses, varIdx, sampleIdx, varType, missingMask);
416 CV_Error(CV_StsInternal, "ERROR: tree can not be trained");
421 float BoostedSoftCascadeOctave::predict( cv::InputArray _sample, cv::InputArray _votes, bool raw_mode, bool return_sum ) const
423 cv::Mat sample = _sample.getMat();
424 CvMat csample = sample;
426 return CvBoost::predict(&csample, 0, 0, CV_WHOLE_SEQ, raw_mode, return_sum);
429 cv::Mat votes = _votes.getMat();
430 CvMat cvotes = votes;
431 return CvBoost::predict(&csample, 0, &cvotes, CV_WHOLE_SEQ, raw_mode, return_sum);
435 float BoostedSoftCascadeOctave::predict( const Mat& _sample, const cv::Range range) const
437 CvMat sample = _sample;
438 return CvBoost::predict(&sample, 0, 0, range, false, true);
441 void BoostedSoftCascadeOctave::write( CvFileStorage* fs, cv::String _name) const
443 CvBoost::write(fs, _name.c_str());
448 CV_INIT_ALGORITHM(BoostedSoftCascadeOctave, "Octave.BoostedSoftCascadeOctave", )
452 cv::Ptr<Octave> Octave::create(cv::Rect boundingBox, int npositives, int nnegatives,
453 int logScale, int shrinkage, cv::Ptr<ChannelFeatureBuilder> builder)
455 cv::Ptr<Octave> octave(
456 new BoostedSoftCascadeOctave(boundingBox, npositives, nnegatives, logScale, shrinkage, builder));