refactor feature pool

pull/322/head
marina.kolpakova 12 years ago
parent b4aa33b6d3
commit 0b039f3c6b
  1. 6
      apps/sft/fpool.cpp
  2. 3
      apps/sft/include/sft/fpool.hpp
  3. 2
      apps/sft/sft.cpp
  4. 5
      modules/softcascade/include/opencv2/softcascade/softcascade.hpp
  5. 45
      modules/softcascade/src/soft_cascade_octave.cpp

@ -53,12 +53,6 @@ sft::ICFFeaturePool::ICFFeaturePool(cv::Size m, int n) : FeaturePool(), model(m)
{ {
CV_Assert(m != cv::Size() && n > 0); CV_Assert(m != cv::Size() && n > 0);
fill(nfeatures); fill(nfeatures);
builder = cv::ChannelFeatureBuilder::create();
}
void sft::ICFFeaturePool::preprocess(cv::InputArray frame, cv::OutputArray integrals) const
{
(*builder)(frame, integrals);
} }
float sft::ICFFeaturePool::apply(int fi, int si, const Mat& integrals) const float sft::ICFFeaturePool::apply(int fi, int si, const Mat& integrals) const

@ -61,7 +61,6 @@ public:
virtual int size() const { return (int)pool.size(); } virtual int size() const { return (int)pool.size(); }
virtual float apply(int fi, int si, const cv::Mat& integrals) const; virtual float apply(int fi, int si, const cv::Mat& integrals) const;
virtual void preprocess(cv::InputArray _frame, cv::OutputArray _integrals) const;
virtual void write( cv::FileStorage& fs, int index) const; virtual void write( cv::FileStorage& fs, int index) const;
virtual ~ICFFeaturePool(); virtual ~ICFFeaturePool();
@ -77,8 +76,6 @@ private:
static const unsigned int seed = 0; static const unsigned int seed = 0;
cv::Ptr<cv::ChannelFeatureBuilder> builder;
enum { N_CHANNELS = 10 }; enum { N_CHANNELS = 10 };
}; };

@ -130,7 +130,7 @@ int main(int argc, char** argv)
typedef cv::SoftCascadeOctave Octave; typedef cv::SoftCascadeOctave Octave;
cv::Ptr<Octave> boost = Octave::create(boundingBox, npositives, nnegatives, *it, shrinkage); cv::Ptr<Octave> boost = Octave::create(boundingBox, npositives, nnegatives, *it, shrinkage, nfeatures);
std::string path = cfg.trainPath; std::string path = cfg.trainPath;
sft::ScaledDataset dataset(path, *it); sft::ScaledDataset dataset(path, *it);

@ -71,9 +71,6 @@ public:
virtual int size() const = 0; virtual int size() const = 0;
virtual float apply(int fi, int si, const Mat& integrals) const = 0; virtual float apply(int fi, int si, const Mat& integrals) const = 0;
virtual void write( cv::FileStorage& fs, int index) const = 0; virtual void write( cv::FileStorage& fs, int index) const = 0;
virtual void preprocess(InputArray frame, OutputArray integrals) const = 0;
virtual ~FeaturePool(); virtual ~FeaturePool();
}; };
@ -196,7 +193,7 @@ public:
virtual ~SoftCascadeOctave(); virtual ~SoftCascadeOctave();
static cv::Ptr<SoftCascadeOctave> create(cv::Rect boundingBox, int npositives, int nnegatives, static cv::Ptr<SoftCascadeOctave> create(cv::Rect boundingBox, int npositives, int nnegatives,
int logScale, int shrinkage); int logScale, int shrinkage, int poolSize);
virtual bool train(const Dataset* dataset, const FeaturePool* pool, int weaks, int treeDepth) = 0; virtual bool train(const Dataset* dataset, const FeaturePool* pool, int weaks, int treeDepth) = 0;
virtual void setRejectThresholds(OutputArray thresholds) = 0; virtual void setRejectThresholds(OutputArray thresholds) = 0;

@ -64,11 +64,15 @@ using cv::Mat;
cv::FeaturePool::~FeaturePool(){} cv::FeaturePool::~FeaturePool(){}
cv::Dataset::~Dataset(){} cv::Dataset::~Dataset(){}
namespace {
class BoostedSoftCascadeOctave : public cv::Boost, public cv::SoftCascadeOctave class BoostedSoftCascadeOctave : public cv::Boost, public cv::SoftCascadeOctave
{ {
public: public:
BoostedSoftCascadeOctave(cv::Rect boundingBox = cv::Rect(), int npositives = 0, int nnegatives = 0, int logScale = 0, int shrinkage = 1); BoostedSoftCascadeOctave(cv::Rect boundingBox = cv::Rect(), int npositives = 0, int nnegatives = 0, int logScale = 0,
int shrinkage = 1, int poolSize = 0);
virtual ~BoostedSoftCascadeOctave(); virtual ~BoostedSoftCascadeOctave();
virtual cv::AlgorithmInfo* info() const; virtual cv::AlgorithmInfo* info() const;
virtual bool train(const Dataset* dataset, const FeaturePool* pool, int weaks, int treeDepth); virtual bool train(const Dataset* dataset, const FeaturePool* pool, int weaks, int treeDepth);
@ -80,8 +84,8 @@ protected:
virtual bool train( const cv::Mat& trainData, const cv::Mat& responses, const cv::Mat& varIdx=cv::Mat(), virtual bool train( const cv::Mat& trainData, const cv::Mat& responses, const cv::Mat& varIdx=cv::Mat(),
const cv::Mat& sampleIdx=cv::Mat(), const cv::Mat& varType=cv::Mat(), const cv::Mat& missingDataMask=cv::Mat()); const cv::Mat& sampleIdx=cv::Mat(), const cv::Mat& varType=cv::Mat(), const cv::Mat& missingDataMask=cv::Mat());
void processPositives(const Dataset* dataset, const FeaturePool* pool); void processPositives(const Dataset* dataset);
void generateNegatives(const Dataset* dataset, const FeaturePool* pool); void generateNegatives(const Dataset* dataset);
float predict( const Mat& _sample, const cv::Range range) const; float predict( const Mat& _sample, const cv::Range range) const;
private: private:
@ -102,9 +106,11 @@ private:
CvBoostParams params; CvBoostParams params;
Mat trainData; Mat trainData;
cv::Ptr<cv::ChannelFeatureBuilder> builder;
}; };
BoostedSoftCascadeOctave::BoostedSoftCascadeOctave(cv::Rect bb, int np, int nn, int ls, int shr) BoostedSoftCascadeOctave::BoostedSoftCascadeOctave(cv::Rect bb, int np, int nn, int ls, int shr, int poolSize)
: logScale(ls), boundingBox(bb), npositives(np), nnegatives(nn), shrinkage(shr) : logScale(ls), boundingBox(bb), npositives(np), nnegatives(nn), shrinkage(shr)
{ {
int maxSample = npositives + nnegatives; int maxSample = npositives + nnegatives;
@ -132,6 +138,13 @@ BoostedSoftCascadeOctave::BoostedSoftCascadeOctave(cv::Rect bb, int np, int nn,
} }
params = _params; params = _params;
builder = cv::ChannelFeatureBuilder::create();
int w = boundingBox.width;
int h = boundingBox.height;
integrals.create(poolSize, (w / shrinkage + 1) * (h / shrinkage * 10 + 1), CV_32SC1);
} }
BoostedSoftCascadeOctave::~BoostedSoftCascadeOctave(){} BoostedSoftCascadeOctave::~BoostedSoftCascadeOctave(){}
@ -191,12 +204,11 @@ void BoostedSoftCascadeOctave::setRejectThresholds(cv::OutputArray _thresholds)
} }
} }
void BoostedSoftCascadeOctave::processPositives(const Dataset* dataset, const FeaturePool* pool) void BoostedSoftCascadeOctave::processPositives(const Dataset* dataset)
{ {
int w = boundingBox.width;
int h = boundingBox.height; int h = boundingBox.height;
integrals.create(pool->size(), (w / shrinkage + 1) * (h / shrinkage * 10 + 1), CV_32SC1); cv::ChannelFeatureBuilder& _builder = *builder;
int total = 0; int total = 0;
for (int curr = 0; curr < dataset->available( Dataset::POSITIVE); ++curr) for (int curr = 0; curr < dataset->available( Dataset::POSITIVE); ++curr)
@ -206,7 +218,7 @@ void BoostedSoftCascadeOctave::processPositives(const Dataset* dataset, const Fe
cv::Mat channels = integrals.row(total).reshape(0, h / shrinkage * 10 + 1); cv::Mat channels = integrals.row(total).reshape(0, h / shrinkage * 10 + 1);
sample = sample(boundingBox); sample = sample(boundingBox);
pool->preprocess(sample, channels); _builder(sample, channels);
responses.ptr<float>(total)[0] = 1.f; responses.ptr<float>(total)[0] = 1.f;
if (++total >= npositives) break; if (++total >= npositives) break;
@ -238,7 +250,7 @@ void BoostedSoftCascadeOctave::processPositives(const Dataset* dataset, const Fe
#undef USE_LONG_SEEDS #undef USE_LONG_SEEDS
void BoostedSoftCascadeOctave::generateNegatives(const Dataset* dataset, const FeaturePool* pool) void BoostedSoftCascadeOctave::generateNegatives(const Dataset* dataset)
{ {
// ToDo: set seed, use offsets // ToDo: set seed, use offsets
sft::Random::engine eng(DX_DY_SEED); sft::Random::engine eng(DX_DY_SEED);
@ -251,6 +263,8 @@ void BoostedSoftCascadeOctave::generateNegatives(const Dataset* dataset, const F
int total = 0; int total = 0;
Mat sum; Mat sum;
cv::ChannelFeatureBuilder& _builder = *builder;
for (int i = npositives; i < nnegatives + npositives; ++total) for (int i = npositives; i < nnegatives + npositives; ++total)
{ {
int curr = iRand(idxEng); int curr = iRand(idxEng);
@ -269,7 +283,7 @@ void BoostedSoftCascadeOctave::generateNegatives(const Dataset* dataset, const F
frame = frame(cv::Rect(dx, dy, boundingBox.width, boundingBox.height)); frame = frame(cv::Rect(dx, dy, boundingBox.width, boundingBox.height));
cv::Mat channels = integrals.row(i).reshape(0, h / shrinkage * 10 + 1); cv::Mat channels = integrals.row(i).reshape(0, h / shrinkage * 10 + 1);
pool->preprocess(frame, channels); _builder(frame, channels);
dprintf("generated %d %d\n", dx, dy); dprintf("generated %d %d\n", dx, dy);
// // if (predict(sum)) // // if (predict(sum))
@ -392,8 +406,8 @@ bool BoostedSoftCascadeOctave::train(const Dataset* dataset, const FeaturePool*
params.weak_count = weaks; params.weak_count = weaks;
// 1. fill integrals and classes // 1. fill integrals and classes
processPositives(dataset, pool); processPositives(dataset);
generateNegatives(dataset, pool); generateNegatives(dataset);
// 2. only simple case (all features used) // 2. only simple case (all features used)
int nfeatures = pool->size(); int nfeatures = pool->size();
@ -462,13 +476,16 @@ void BoostedSoftCascadeOctave::write( CvFileStorage* fs, std::string _name) cons
CvBoost::write(fs, _name.c_str()); CvBoost::write(fs, _name.c_str());
} }
}
CV_INIT_ALGORITHM(BoostedSoftCascadeOctave, "SoftCascadeOctave.BoostedSoftCascadeOctave", ); CV_INIT_ALGORITHM(BoostedSoftCascadeOctave, "SoftCascadeOctave.BoostedSoftCascadeOctave", );
cv::SoftCascadeOctave::~SoftCascadeOctave(){} cv::SoftCascadeOctave::~SoftCascadeOctave(){}
cv::Ptr<cv::SoftCascadeOctave> cv::SoftCascadeOctave::create(cv::Rect boundingBox, int npositives, int nnegatives, cv::Ptr<cv::SoftCascadeOctave> cv::SoftCascadeOctave::create(cv::Rect boundingBox, int npositives, int nnegatives,
int logScale, int shrinkage) int logScale, int shrinkage, int poolSize)
{ {
cv::Ptr<cv::SoftCascadeOctave> octave(new BoostedSoftCascadeOctave(boundingBox, npositives, nnegatives, logScale, shrinkage)); cv::Ptr<cv::SoftCascadeOctave> octave(
new BoostedSoftCascadeOctave(boundingBox, npositives, nnegatives, logScale, shrinkage, poolSize));
return octave; return octave;
} }

Loading…
Cancel
Save