mirror of
https://github.com/opencv/opencv_contrib.git
synced 2025-10-20 12:55:15 +08:00
Derive WaldBoost from Algorithm
This commit is contained in:
@@ -108,56 +108,10 @@ struct CV_EXPORTS WaldBoostParams
|
|||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
class CV_EXPORTS Stump
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
|
|
||||||
/* Initialize zero stump */
|
|
||||||
Stump(): threshold_(0), polarity_(1), pos_value_(1), neg_value_(-1) {}
|
|
||||||
|
|
||||||
/* Initialize stump with given threshold, polarity
|
|
||||||
and classification values */
|
|
||||||
Stump(int threshold, int polarity, float pos_value, float neg_value):
|
|
||||||
threshold_(threshold), polarity_(polarity),
|
|
||||||
pos_value_(pos_value), neg_value_(neg_value) {}
|
|
||||||
|
|
||||||
/* Train stump for given data
|
|
||||||
|
|
||||||
data — matrix of feature values, size M x N, one feature per row
|
|
||||||
|
|
||||||
labels — matrix of sample class labels, size 1 x N. Labels can be from
|
|
||||||
{-1, +1}
|
|
||||||
|
|
||||||
weights — matrix of sample weights, size 1 x N
|
|
||||||
|
|
||||||
Returns chosen feature index. Feature enumeration starts from 0
|
|
||||||
*/
|
|
||||||
int train(const Mat& data, const Mat& labels, const Mat& weights);
|
|
||||||
|
|
||||||
/* Predict object class given
|
|
||||||
|
|
||||||
value — feature value. Feature must be the same as was chosen
|
|
||||||
during training stump
|
|
||||||
|
|
||||||
Returns real value, sign(value) means class
|
|
||||||
*/
|
|
||||||
float predict(int value) const;
|
|
||||||
|
|
||||||
private:
|
|
||||||
/* Stump decision threshold */
|
|
||||||
int threshold_;
|
|
||||||
/* Stump polarity, can be from {-1, +1} */
|
|
||||||
int polarity_;
|
|
||||||
/* Classification values for positive and negative classes */
|
|
||||||
float pos_value_, neg_value_;
|
|
||||||
};
|
|
||||||
|
|
||||||
class CV_EXPORTS WaldBoost : public Algorithm
|
class CV_EXPORTS WaldBoost : public Algorithm
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
/* Initialize WaldBoost cascade with default of specified parameters */
|
|
||||||
WaldBoost(const WaldBoostParams& params = WaldBoostParams());
|
|
||||||
|
|
||||||
/* Train WaldBoost cascade for given data
|
/* Train WaldBoost cascade for given data
|
||||||
|
|
||||||
data — matrix of feature values, size M x N, one feature per row
|
data — matrix of feature values, size M x N, one feature per row
|
||||||
@@ -168,8 +122,8 @@ public:
|
|||||||
Returns feature indices chosen for cascade.
|
Returns feature indices chosen for cascade.
|
||||||
Feature enumeration starts from 0
|
Feature enumeration starts from 0
|
||||||
*/
|
*/
|
||||||
std::vector<int> train(const Mat& data,
|
virtual std::vector<int> train(const Mat& data,
|
||||||
const Mat& labels);
|
const Mat& labels) = 0;
|
||||||
|
|
||||||
/* Predict object class given object that can compute object features
|
/* Predict object class given object that can compute object features
|
||||||
|
|
||||||
@@ -178,17 +132,14 @@ public:
|
|||||||
Returns confidence_value — measure of confidense that object
|
Returns confidence_value — measure of confidense that object
|
||||||
is from class +1
|
is from class +1
|
||||||
*/
|
*/
|
||||||
float predict(const Ptr<ACFFeatureEvaluator>& feature_evaluator);
|
virtual float predict(
|
||||||
|
const Ptr<ACFFeatureEvaluator>& feature_evaluator) const = 0;
|
||||||
|
|
||||||
private:
|
|
||||||
/* Parameters for cascade training */
|
|
||||||
WaldBoostParams params_;
|
|
||||||
/* Stumps in cascade */
|
|
||||||
std::vector<Stump> stumps_;
|
|
||||||
/* Rejection thresholds for linear combination at every stump evaluation */
|
|
||||||
std::vector<float> thresholds_;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
CV_EXPORTS Ptr<WaldBoost>
|
||||||
|
createWaldBoost(const WaldBoostParams& params = WaldBoostParams());
|
||||||
|
|
||||||
struct CV_EXPORTS ICFDetectorParams
|
struct CV_EXPORTS ICFDetectorParams
|
||||||
{
|
{
|
||||||
int feature_count;
|
int feature_count;
|
||||||
|
@@ -137,8 +137,8 @@ void ICFDetector::train(const vector<string>& image_filenames,
|
|||||||
wparams.weak_count = params.weak_count;
|
wparams.weak_count = params.weak_count;
|
||||||
wparams.alpha = 0.001f;
|
wparams.alpha = 0.001f;
|
||||||
|
|
||||||
WaldBoost waldboost(wparams);
|
Ptr<WaldBoost> waldboost = createWaldBoost(wparams);
|
||||||
waldboost.train(data, labels);
|
waldboost->train(data, labels);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool ICFDetector::save(const string&)
|
bool ICFDetector::save(const string&)
|
||||||
|
@@ -43,6 +43,7 @@ the use of this software, even if advised of the possibility of such damage.
|
|||||||
#define __OPENCV_XOBJDETECT_PRECOMP_HPP__
|
#define __OPENCV_XOBJDETECT_PRECOMP_HPP__
|
||||||
|
|
||||||
#include <opencv2/xobjdetect.hpp>
|
#include <opencv2/xobjdetect.hpp>
|
||||||
|
#include <opencv2/xobjdetect/private.hpp>
|
||||||
|
|
||||||
#include <opencv2/imgproc.hpp>
|
#include <opencv2/imgproc.hpp>
|
||||||
#include <opencv2/imgproc/types_c.h>
|
#include <opencv2/imgproc/types_c.h>
|
||||||
|
@@ -41,7 +41,6 @@ the use of this software, even if advised of the possibility of such damage.
|
|||||||
|
|
||||||
#include "precomp.hpp"
|
#include "precomp.hpp"
|
||||||
|
|
||||||
|
|
||||||
using std::swap;
|
using std::swap;
|
||||||
|
|
||||||
using std::vector;
|
using std::vector;
|
||||||
@@ -51,11 +50,30 @@ namespace cv
|
|||||||
namespace xobjdetect
|
namespace xobjdetect
|
||||||
{
|
{
|
||||||
|
|
||||||
WaldBoost::WaldBoost(const WaldBoostParams& params): params_(params)
|
class WaldBoostImpl : public WaldBoost
|
||||||
{
|
{
|
||||||
}
|
public:
|
||||||
|
/* Initialize WaldBoost cascade with default of specified parameters */
|
||||||
|
WaldBoostImpl(const WaldBoostParams& params):
|
||||||
|
params_(params)
|
||||||
|
{}
|
||||||
|
|
||||||
vector<int> WaldBoost::train(const Mat& data, const Mat& labels)
|
virtual std::vector<int> train(const Mat& data,
|
||||||
|
const Mat& labels);
|
||||||
|
|
||||||
|
virtual float predict(
|
||||||
|
const Ptr<ACFFeatureEvaluator>& feature_evaluator) const;
|
||||||
|
|
||||||
|
private:
|
||||||
|
/* Parameters for cascade training */
|
||||||
|
WaldBoostParams params_;
|
||||||
|
/* Stumps in cascade */
|
||||||
|
std::vector<Stump> stumps_;
|
||||||
|
/* Rejection thresholds for linear combination at every stump evaluation */
|
||||||
|
std::vector<float> thresholds_;
|
||||||
|
};
|
||||||
|
|
||||||
|
vector<int> WaldBoostImpl::train(const Mat& data, const Mat& labels)
|
||||||
{
|
{
|
||||||
CV_Assert(labels.rows == 1 && labels.cols == data.cols);
|
CV_Assert(labels.rows == 1 && labels.cols == data.cols);
|
||||||
|
|
||||||
@@ -144,7 +162,8 @@ vector<int> WaldBoost::train(const Mat& data, const Mat& labels)
|
|||||||
return feature_indices;
|
return feature_indices;
|
||||||
}
|
}
|
||||||
|
|
||||||
float WaldBoost::predict(const Ptr<ACFFeatureEvaluator>& feature_evaluator)
|
float WaldBoostImpl::predict(
|
||||||
|
const Ptr<ACFFeatureEvaluator>& feature_evaluator) const
|
||||||
{
|
{
|
||||||
float trace = 0;
|
float trace = 0;
|
||||||
for( size_t i = 0; i < stumps_.size(); ++i )
|
for( size_t i = 0; i < stumps_.size(); ++i )
|
||||||
@@ -157,5 +176,12 @@ float WaldBoost::predict(const Ptr<ACFFeatureEvaluator>& feature_evaluator)
|
|||||||
return trace;
|
return trace;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Ptr<WaldBoost>
|
||||||
|
createWaldBoost(const WaldBoostParams& params)
|
||||||
|
{
|
||||||
|
return Ptr<WaldBoost>(new WaldBoostImpl(params));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
} /* namespace xobjdetect */
|
} /* namespace xobjdetect */
|
||||||
} /* namespace cv */
|
} /* namespace cv */
|
||||||
|
Reference in New Issue
Block a user