move feature pool to softcascade module
This commit is contained in:
@@ -41,101 +41,10 @@
|
|||||||
//M*/
|
//M*/
|
||||||
|
|
||||||
#include <sft/fpool.hpp>
|
#include <sft/fpool.hpp>
|
||||||
#include <sft/random.hpp>
|
|
||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
#include <queue>
|
#include <queue>
|
||||||
|
|
||||||
// ========= FeaturePool ========= //
|
|
||||||
|
|
||||||
sft::ICFFeaturePool::ICFFeaturePool(cv::Size m, int n) : FeaturePool(), model(m), nfeatures(n)
|
|
||||||
{
|
|
||||||
CV_Assert(m != cv::Size() && n > 0);
|
|
||||||
fill(nfeatures);
|
|
||||||
}
|
|
||||||
|
|
||||||
float sft::ICFFeaturePool::apply(int fi, int si, const Mat& integrals) const
|
|
||||||
{
|
|
||||||
return pool[fi](integrals.row(si), model);
|
|
||||||
}
|
|
||||||
|
|
||||||
void sft::ICFFeaturePool::write( cv::FileStorage& fs, int index) const
|
|
||||||
{
|
|
||||||
CV_Assert((index > 0) && (index < (int)pool.size()));
|
|
||||||
fs << pool[index];
|
|
||||||
}
|
|
||||||
|
|
||||||
sft::ICFFeaturePool::~ICFFeaturePool(){}
|
|
||||||
|
|
||||||
#if defined _WIN32 && (_WIN32 || _WIN64)
|
|
||||||
# if _WIN64
|
|
||||||
# define USE_LONG_SEEDS
|
|
||||||
# endif
|
|
||||||
#endif
|
|
||||||
#if defined (__GNUC__) &&__GNUC__
|
|
||||||
# if defined(__x86_64__) || defined(__ppc64__)
|
|
||||||
# define USE_LONG_SEEDS
|
|
||||||
# endif
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined USE_LONG_SEEDS
|
|
||||||
# define FEATURE_RECT_SEED 8854342234LU
|
|
||||||
#else
|
|
||||||
# define FEATURE_RECT_SEED 88543422LU
|
|
||||||
#endif
|
|
||||||
# define DCHANNELS_SEED 314152314LU
|
|
||||||
#undef USE_LONG_SEEDS
|
|
||||||
|
|
||||||
void sft::ICFFeaturePool::fill(int desired)
|
|
||||||
{
|
|
||||||
int mw = model.width;
|
|
||||||
int mh = model.height;
|
|
||||||
|
|
||||||
int maxPoolSize = (mw -1) * mw / 2 * (mh - 1) * mh / 2 * N_CHANNELS;
|
|
||||||
|
|
||||||
nfeatures = std::min(desired, maxPoolSize);
|
|
||||||
dprintf("Requeste feature pool %d max %d suggested %d\n", desired, maxPoolSize, nfeatures);
|
|
||||||
|
|
||||||
pool.reserve(nfeatures);
|
|
||||||
|
|
||||||
sft::Random::engine eng(FEATURE_RECT_SEED);
|
|
||||||
sft::Random::engine eng_ch(DCHANNELS_SEED);
|
|
||||||
|
|
||||||
sft::Random::uniform chRand(0, N_CHANNELS - 1);
|
|
||||||
|
|
||||||
sft::Random::uniform xRand(0, model.width - 2);
|
|
||||||
sft::Random::uniform yRand(0, model.height - 2);
|
|
||||||
|
|
||||||
sft::Random::uniform wRand(1, model.width - 1);
|
|
||||||
sft::Random::uniform hRand(1, model.height - 1);
|
|
||||||
|
|
||||||
while (pool.size() < size_t(nfeatures))
|
|
||||||
{
|
|
||||||
int x = xRand(eng);
|
|
||||||
int y = yRand(eng);
|
|
||||||
|
|
||||||
int w = 1 + wRand(eng, model.width - x - 1);
|
|
||||||
int h = 1 + hRand(eng, model.height - y - 1);
|
|
||||||
|
|
||||||
CV_Assert(w > 0);
|
|
||||||
CV_Assert(h > 0);
|
|
||||||
|
|
||||||
CV_Assert(w + x < model.width);
|
|
||||||
CV_Assert(h + y < model.height);
|
|
||||||
|
|
||||||
int ch = chRand(eng_ch);
|
|
||||||
|
|
||||||
cv::ChannelFeature f(x, y, w, h, ch);
|
|
||||||
|
|
||||||
if (std::find(pool.begin(), pool.end(),f) == pool.end())
|
|
||||||
{
|
|
||||||
pool.push_back(f);
|
|
||||||
std::cout << f << std::endl;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// ============ Dataset ============ //
|
// ============ Dataset ============ //
|
||||||
namespace {
|
namespace {
|
||||||
using namespace sft;
|
using namespace sft;
|
||||||
|
@@ -51,36 +51,8 @@
|
|||||||
namespace sft
|
namespace sft
|
||||||
{
|
{
|
||||||
|
|
||||||
using cv::FeaturePool;
|
|
||||||
using cv::Dataset;
|
using cv::Dataset;
|
||||||
|
|
||||||
class ICFFeaturePool : public cv::FeaturePool
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
ICFFeaturePool(cv::Size model, int nfeatures);
|
|
||||||
|
|
||||||
virtual int size() const { return (int)pool.size(); }
|
|
||||||
virtual float apply(int fi, int si, const cv::Mat& integrals) const;
|
|
||||||
virtual void write( cv::FileStorage& fs, int index) const;
|
|
||||||
|
|
||||||
virtual ~ICFFeaturePool();
|
|
||||||
|
|
||||||
private:
|
|
||||||
|
|
||||||
void fill(int desired);
|
|
||||||
|
|
||||||
cv::Size model;
|
|
||||||
int nfeatures;
|
|
||||||
|
|
||||||
std::vector<cv::ChannelFeature> pool;
|
|
||||||
|
|
||||||
static const unsigned int seed = 0;
|
|
||||||
|
|
||||||
enum { N_CHANNELS = 10 };
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class ScaledDataset : public Dataset
|
class ScaledDataset : public Dataset
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
@@ -1,117 +0,0 @@
|
|||||||
/*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.
|
|
||||||
//
|
|
||||||
//
|
|
||||||
// License Agreement
|
|
||||||
// For Open Source Computer Vision Library
|
|
||||||
//
|
|
||||||
// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
|
|
||||||
// Copyright (C) 2008-2012, Willow Garage Inc., all rights reserved.
|
|
||||||
// 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.
|
|
||||||
//
|
|
||||||
// * The name of the copyright holders may not be used to endorse or promote products
|
|
||||||
// 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*/
|
|
||||||
|
|
||||||
#ifndef __SFT_RANDOM_HPP__
|
|
||||||
#define __SFT_RANDOM_HPP__
|
|
||||||
|
|
||||||
#if defined(_MSC_VER) && _MSC_VER >= 1600
|
|
||||||
|
|
||||||
# include <random>
|
|
||||||
namespace sft {
|
|
||||||
struct Random
|
|
||||||
{
|
|
||||||
typedef std::mt19937 engine;
|
|
||||||
typedef std::uniform_int<int> uniform;
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
#elif (__GNUC__) && __GNUC__ > 3 && __GNUC_MINOR__ > 1 && !defined(__ANDROID__)
|
|
||||||
|
|
||||||
# if defined (__cplusplus) && __cplusplus > 201100L
|
|
||||||
# include <random>
|
|
||||||
namespace sft {
|
|
||||||
struct Random
|
|
||||||
{
|
|
||||||
typedef std::mt19937 engine;
|
|
||||||
typedef std::uniform_int<int> uniform;
|
|
||||||
};
|
|
||||||
}
|
|
||||||
# else
|
|
||||||
# include <tr1/random>
|
|
||||||
|
|
||||||
namespace sft {
|
|
||||||
struct Random
|
|
||||||
{
|
|
||||||
typedef std::tr1::mt19937 engine;
|
|
||||||
typedef std::tr1::uniform_int<int> uniform;
|
|
||||||
};
|
|
||||||
}
|
|
||||||
# endif
|
|
||||||
|
|
||||||
#else
|
|
||||||
#include <opencv2/core/core.hpp>
|
|
||||||
namespace rnd {
|
|
||||||
|
|
||||||
typedef cv::RNG engine;
|
|
||||||
|
|
||||||
template<typename T>
|
|
||||||
struct uniform_int
|
|
||||||
{
|
|
||||||
uniform_int(const int _min, const int _max) : min(_min), max(_max) {}
|
|
||||||
T operator() (engine& eng, const int bound) const
|
|
||||||
{
|
|
||||||
return (T)eng.uniform(min, bound);
|
|
||||||
}
|
|
||||||
|
|
||||||
T operator() (engine& eng) const
|
|
||||||
{
|
|
||||||
return (T)eng.uniform(min, max);
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
|
||||||
int min;
|
|
||||||
int max;
|
|
||||||
};
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
namespace sft {
|
|
||||||
struct Random
|
|
||||||
{
|
|
||||||
typedef rnd::engine engine;
|
|
||||||
typedef rnd::uniform_int<int> uniform;
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#endif
|
|
@@ -118,8 +118,8 @@ int main(int argc, char** argv)
|
|||||||
int nfeatures = cfg.poolSize;
|
int nfeatures = cfg.poolSize;
|
||||||
cv::Size model = cfg.model(it);
|
cv::Size model = cfg.model(it);
|
||||||
std::cout << "Model " << model << std::endl;
|
std::cout << "Model " << model << std::endl;
|
||||||
sft::ICFFeaturePool pool(model, nfeatures);
|
cv::Ptr<cv::FeaturePool> pool = cv::FeaturePool::create(model, nfeatures);
|
||||||
nfeatures = pool.size();
|
nfeatures = pool->size();
|
||||||
|
|
||||||
|
|
||||||
int npositives = cfg.positives;
|
int npositives = cfg.positives;
|
||||||
@@ -135,7 +135,7 @@ int main(int argc, char** argv)
|
|||||||
std::string path = cfg.trainPath;
|
std::string path = cfg.trainPath;
|
||||||
sft::ScaledDataset dataset(path, *it);
|
sft::ScaledDataset dataset(path, *it);
|
||||||
|
|
||||||
if (boost->train(&dataset, &pool, cfg.weaks, cfg.treeDepth))
|
if (boost->train(&dataset, pool, cfg.weaks, cfg.treeDepth))
|
||||||
{
|
{
|
||||||
CvFileStorage* fout = cvOpenFileStorage(cfg.resPath(it).c_str(), 0, CV_STORAGE_WRITE);
|
CvFileStorage* fout = cvOpenFileStorage(cfg.resPath(it).c_str(), 0, CV_STORAGE_WRITE);
|
||||||
boost->write(fout, cfg.cascadeName);
|
boost->write(fout, cfg.cascadeName);
|
||||||
@@ -145,7 +145,7 @@ int main(int argc, char** argv)
|
|||||||
cv::Mat thresholds;
|
cv::Mat thresholds;
|
||||||
boost->setRejectThresholds(thresholds);
|
boost->setRejectThresholds(thresholds);
|
||||||
|
|
||||||
boost->write(fso, &pool, thresholds);
|
boost->write(fso, pool, thresholds);
|
||||||
|
|
||||||
cv::FileStorage tfs(("thresholds." + cfg.resPath(it)).c_str(), cv::FileStorage::WRITE);
|
cv::FileStorage tfs(("thresholds." + cfg.resPath(it)).c_str(), cv::FileStorage::WRITE);
|
||||||
tfs << "thresholds" << thresholds;
|
tfs << "thresholds" << thresholds;
|
||||||
|
@@ -64,15 +64,7 @@ struct CV_EXPORTS Detection
|
|||||||
int kind;
|
int kind;
|
||||||
};
|
};
|
||||||
|
|
||||||
class CV_EXPORTS FeaturePool
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
|
|
||||||
virtual int size() 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 ~FeaturePool();
|
|
||||||
};
|
|
||||||
|
|
||||||
class CV_EXPORTS Dataset
|
class CV_EXPORTS Dataset
|
||||||
{
|
{
|
||||||
@@ -84,6 +76,22 @@ public:
|
|||||||
virtual ~Dataset();
|
virtual ~Dataset();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// ========================================================================== //
|
||||||
|
// Public interface feature pool.
|
||||||
|
// ========================================================================== //
|
||||||
|
|
||||||
|
class CV_EXPORTS FeaturePool
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
|
||||||
|
virtual int size() const = 0;
|
||||||
|
virtual float apply(int fi, int si, const Mat& channels) const = 0;
|
||||||
|
virtual void write( cv::FileStorage& fs, int index) const = 0;
|
||||||
|
virtual ~FeaturePool();
|
||||||
|
|
||||||
|
static cv::Ptr<FeaturePool> create(const cv::Size& model, int nfeatures);
|
||||||
|
};
|
||||||
|
|
||||||
// ========================================================================== //
|
// ========================================================================== //
|
||||||
// First order channel feature.
|
// First order channel feature.
|
||||||
// ========================================================================== //
|
// ========================================================================== //
|
||||||
|
@@ -41,6 +41,7 @@
|
|||||||
//M*/
|
//M*/
|
||||||
|
|
||||||
#include "precomp.hpp"
|
#include "precomp.hpp"
|
||||||
|
#include "_random.hpp"
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
|
|
||||||
@@ -160,3 +161,116 @@ std::ostream& cv::operator<<(std::ostream& out, const cv::ChannelFeature& m)
|
|||||||
}
|
}
|
||||||
|
|
||||||
cv::ChannelFeature::~ChannelFeature(){}
|
cv::ChannelFeature::~ChannelFeature(){}
|
||||||
|
|
||||||
|
namespace {
|
||||||
|
|
||||||
|
class ChannelFeaturePool : public cv::FeaturePool
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
ChannelFeaturePool(cv::Size m, int n) : FeaturePool(), model(m)
|
||||||
|
{
|
||||||
|
CV_Assert(m != cv::Size() && n > 0);
|
||||||
|
fill(n);
|
||||||
|
}
|
||||||
|
|
||||||
|
virtual int size() const { return (int)pool.size(); }
|
||||||
|
virtual float apply(int fi, int si, const cv::Mat& integrals) const;
|
||||||
|
virtual void write( cv::FileStorage& fs, int index) const;
|
||||||
|
|
||||||
|
virtual ~ChannelFeaturePool() {}
|
||||||
|
|
||||||
|
private:
|
||||||
|
|
||||||
|
void fill(int desired);
|
||||||
|
|
||||||
|
cv::Size model;
|
||||||
|
std::vector<cv::ChannelFeature> pool;
|
||||||
|
enum { N_CHANNELS = 10 };
|
||||||
|
};
|
||||||
|
|
||||||
|
float ChannelFeaturePool::apply(int fi, int si, const cv::Mat& integrals) const
|
||||||
|
{
|
||||||
|
return pool[fi](integrals.row(si), model);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ChannelFeaturePool::write( cv::FileStorage& fs, int index) const
|
||||||
|
{
|
||||||
|
CV_Assert((index > 0) && (index < (int)pool.size()));
|
||||||
|
fs << pool[index];
|
||||||
|
}
|
||||||
|
|
||||||
|
#if defined _WIN32 && (_WIN32 || _WIN64)
|
||||||
|
# if _WIN64
|
||||||
|
# define USE_LONG_SEEDS
|
||||||
|
# endif
|
||||||
|
#endif
|
||||||
|
#if defined (__GNUC__) &&__GNUC__
|
||||||
|
# if defined(__x86_64__) || defined(__ppc64__)
|
||||||
|
# define USE_LONG_SEEDS
|
||||||
|
# endif
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if defined USE_LONG_SEEDS
|
||||||
|
# define FEATURE_RECT_SEED 8854342234LU
|
||||||
|
#else
|
||||||
|
# define FEATURE_RECT_SEED 88543422LU
|
||||||
|
#endif
|
||||||
|
# define DCHANNELS_SEED 314152314LU
|
||||||
|
#undef USE_LONG_SEEDS
|
||||||
|
|
||||||
|
void ChannelFeaturePool::fill(int desired)
|
||||||
|
{
|
||||||
|
int mw = model.width;
|
||||||
|
int mh = model.height;
|
||||||
|
|
||||||
|
int maxPoolSize = (mw -1) * mw / 2 * (mh - 1) * mh / 2 * N_CHANNELS;
|
||||||
|
|
||||||
|
int nfeatures = std::min(desired, maxPoolSize);
|
||||||
|
// dprintf("Requeste feature pool %d max %d suggested %d\n", desired, maxPoolSize, nfeatures);
|
||||||
|
|
||||||
|
pool.reserve(nfeatures);
|
||||||
|
|
||||||
|
sft::Random::engine eng(FEATURE_RECT_SEED);
|
||||||
|
sft::Random::engine eng_ch(DCHANNELS_SEED);
|
||||||
|
|
||||||
|
sft::Random::uniform chRand(0, N_CHANNELS - 1);
|
||||||
|
|
||||||
|
sft::Random::uniform xRand(0, model.width - 2);
|
||||||
|
sft::Random::uniform yRand(0, model.height - 2);
|
||||||
|
|
||||||
|
sft::Random::uniform wRand(1, model.width - 1);
|
||||||
|
sft::Random::uniform hRand(1, model.height - 1);
|
||||||
|
|
||||||
|
while (pool.size() < size_t(nfeatures))
|
||||||
|
{
|
||||||
|
int x = xRand(eng);
|
||||||
|
int y = yRand(eng);
|
||||||
|
|
||||||
|
int w = 1 + wRand(eng, model.width - x - 1);
|
||||||
|
int h = 1 + hRand(eng, model.height - y - 1);
|
||||||
|
|
||||||
|
CV_Assert(w > 0);
|
||||||
|
CV_Assert(h > 0);
|
||||||
|
|
||||||
|
CV_Assert(w + x < model.width);
|
||||||
|
CV_Assert(h + y < model.height);
|
||||||
|
|
||||||
|
int ch = chRand(eng_ch);
|
||||||
|
|
||||||
|
cv::ChannelFeature f(x, y, w, h, ch);
|
||||||
|
|
||||||
|
if (std::find(pool.begin(), pool.end(),f) == pool.end())
|
||||||
|
{
|
||||||
|
pool.push_back(f);
|
||||||
|
std::cout << f << std::endl;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
cv::Ptr<cv::FeaturePool> cv::FeaturePool::create(const cv::Size& model, int nfeatures)
|
||||||
|
{
|
||||||
|
cv::Ptr<cv::FeaturePool> pool(new ChannelFeaturePool(model, nfeatures));
|
||||||
|
return pool;
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user