refactoring

pull/322/head
marina.kolpakova 12 years ago
parent 883d691c2b
commit bfa26fd447
  1. 16
      apps/sft/include/sft/config.hpp
  2. 15
      apps/sft/include/sft/octave.hpp
  3. 106
      apps/sft/octave.cpp
  4. 41
      apps/sft/sft.cpp

@ -57,6 +57,22 @@ struct Config
void read(const cv::FileNode& node);
// Scaled and shrunk model size.
cv::Size model(ivector::const_iterator it) const
{
float octave = powf(2, *it);
return cv::Size( cvRound(modelWinSize.width * octave) / shrinkage,
cvRound(modelWinSize.height * octave) / shrinkage );
}
// Scaled but, not shrunk bounding box for object in sample image.
cv::Rect bbox(ivector::const_iterator it) const
{
float octave = powf(2, *it);
return cv::Rect( cvRound(offset.x * octave), cvRound(offset.y * octave),
cvRound(modelWinSize.width * octave), cvRound(modelWinSize.height * octave));
}
// Paths to a rescaled data
string trainPath;
string testPath;

@ -76,12 +76,14 @@ struct ICF
float operator() (const Mat& integrals, const cv::Size& model) const
{
const int* ptr = integrals.ptr<int>(0) + (model.height * channel + bb.y) * model.width + bb.x;
int step = model.width + 1;
const int* ptr = integrals.ptr<int>(0) + (model.height * channel + bb.y) * step + bb.x;
int a = ptr[0];
int b = ptr[bb.width];
ptr += bb.height * model.width;
ptr += bb.height * step;
int c = ptr[bb.width];
int d = ptr[0];
@ -92,13 +94,17 @@ struct ICF
private:
cv::Rect bb;
int channel;
friend std::ostream& operator<<(std::ostream& out, const ICF& m);
};
std::ostream& operator<<(std::ostream& out, const ICF& m);
class FeaturePool
{
public:
FeaturePool(cv::Size model, int nfeatures);
~FeaturePool();
int size() const { return (int)pool.size(); }
float apply(int fi, int si, const Mat& integrals) const;
@ -122,7 +128,7 @@ public:
Octave(cv::Rect boundingBox, int npositives, int nnegatives, int logScale, int shrinkage);
virtual ~Octave();
virtual bool train(const Dataset& dataset, const FeaturePool& pool);
virtual bool train(const Dataset& dataset, const FeaturePool& pool, int weaks, int treeDepth);
int logScale;
@ -144,7 +150,6 @@ private:
Mat responses;
CvBoostParams params;
};
}

@ -43,16 +43,6 @@
#include <sft/octave.hpp>
#include <sft/random.hpp>
#if defined VISUALIZE_GENERATION
# define show(a, b) \
do { \
cv::imshow(a,b); \
cv::waitkey(0); \
} while(0)
#else
# define show(a, b)
#endif
#include <glob.h>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
@ -63,13 +53,7 @@ sft::Octave::Octave(cv::Rect bb, int np, int nn, int ls, int shr)
{
int maxSample = npositives + nnegatives;
responses.create(maxSample, 1, CV_32FC1);
}
sft::Octave::~Octave(){}
bool sft::Octave::train( const cv::Mat& trainData, const cv::Mat& _responses, const cv::Mat& varIdx,
const cv::Mat& sampleIdx, const cv::Mat& varType, const cv::Mat& missingDataMask)
{
CvBoostParams _params;
{
// tree params
@ -79,27 +63,35 @@ bool sft::Octave::train( const cv::Mat& trainData, const cv::Mat& _responses, co
_params.truncate_pruned_tree = false;
_params.use_surrogates = false;
_params.use_1se_rule = false;
_params.regression_accuracy = 0.0;
_params.regression_accuracy = 1.0e-6;
// boost params
_params.boost_type = CvBoost::GENTLE;
_params.split_criteria = CvBoost::SQERR;
_params.weight_trim_rate = 0.95;
/// ToDo: move to params
// simple defaults
_params.min_sample_count = 2;
_params.weak_count = 1;
}
std::cout << "WARNING: " << sampleIdx << std::endl;
std::cout << "WARNING: " << trainData << std::endl;
std::cout << "WARNING: " << _responses << std::endl;
std::cout << "WARNING: " << varIdx << std::endl;
std::cout << "WARNING: " << varType << std::endl;
params = _params;
}
sft::Octave::~Octave(){}
bool sft::Octave::train( const cv::Mat& trainData, const cv::Mat& _responses, const cv::Mat& varIdx,
const cv::Mat& sampleIdx, const cv::Mat& varType, const cv::Mat& missingDataMask)
{
std::cout << "WARNING: sampleIdx " << sampleIdx << std::endl;
std::cout << "WARNING: trainData " << trainData << std::endl;
std::cout << "WARNING: _responses " << _responses << std::endl;
std::cout << "WARNING: varIdx" << varIdx << std::endl;
std::cout << "WARNING: varType" << varType << std::endl;
bool update = false;
return cv::Boost::train(trainData, CV_COL_SAMPLE, _responses, varIdx, sampleIdx, varType, missingDataMask, _params,
return cv::Boost::train(trainData, CV_COL_SAMPLE, _responses, varIdx, sampleIdx, varType, missingDataMask, params,
update);
}
@ -164,29 +156,30 @@ public:
};
}
// ToDo: parallelize it
// ToDo: parallelize it, fix curring
// ToDo: sunch model size and shrinced model size usage/ Now model size mean already shrinked model
void sft::Octave::processPositives(const Dataset& dataset, const FeaturePool& pool)
{
Preprocessor prepocessor(shrinkage);
int w = 64 * pow(2, logScale) /shrinkage;
int h = 128 * pow(2, logScale) /shrinkage * 10;
int w = boundingBox.width;
int h = boundingBox.height;
integrals.create(pool.size(), (w + 1) * (h + 1), CV_32SC1);
integrals.create(pool.size(), (w / shrinkage + 1) * (h / shrinkage * 10 + 1), CV_32SC1);
int total = 0;
for (svector::const_iterator it = dataset.pos.begin(); it != dataset.pos.end(); ++it)
{
const string& curr = *it;
dprintf("Process candidate positive image %s\n", curr.c_str());
cv::Mat sample = cv::imread(curr);
cv::Mat channels = integrals.row(total).reshape(0, h + 1);
prepocessor.apply(sample, channels);
cv::Mat sample = cv::imread(curr);
cv::Mat channels = integrals.row(total).reshape(0, h / shrinkage * 10 + 1);
sample = sample(boundingBox);
prepocessor.apply(sample, channels);
responses.ptr<float>(total)[0] = 1.f;
if (++total >= npositives) break;
@ -204,8 +197,8 @@ void sft::Octave::generateNegatives(const Dataset& dataset)
sft::Random::engine eng;
sft::Random::engine idxEng;
int w = 64 * pow(2, logScale) /shrinkage;
int h = 128 * pow(2, logScale) /shrinkage * 10;
int w = boundingBox.width;
int h = boundingBox.height;
Preprocessor prepocessor(shrinkage);
@ -222,15 +215,9 @@ void sft::Octave::generateNegatives(const Dataset& dataset)
dprintf("Process %s\n", dataset.neg[curr].c_str());
Mat frame = cv::imread(dataset.neg[curr]);
prepocessor.apply(frame, sum);
std::cout << "WARNING: " << frame.cols << " " << frame.rows << std::endl;
std::cout << "WARNING: " << frame.cols / shrinkage << " " << frame.rows / shrinkage << std::endl;
int maxW = frame.cols / shrinkage - 2 * boundingBox.x - boundingBox.width;
int maxH = frame.rows / shrinkage - 2 * boundingBox.y - boundingBox.height;
std::cout << "WARNING: " << maxW << " " << maxH << std::endl;
int maxW = frame.cols - 2 * boundingBox.x - boundingBox.width;
int maxH = frame.rows - 2 * boundingBox.y - boundingBox.height;
sft::Random::uniform wRand(0, maxW -1);
sft::Random::uniform hRand(0, maxH -1);
@ -238,19 +225,16 @@ void sft::Octave::generateNegatives(const Dataset& dataset)
int dx = wRand(eng);
int dy = hRand(eng);
std::cout << "WARNING: " << dx << " " << dy << std::endl;
std::cout << "WARNING: " << dx + boundingBox.width + 1 << " " << dy + boundingBox.height + 1 << std::endl;
std::cout << "WARNING: " << sum.cols << " " << sum.rows << std::endl;
frame = frame(cv::Rect(dx, dy, boundingBox.width, boundingBox.height));
sum = sum(cv::Rect(dx, dy, boundingBox.width + 1, boundingBox.height * 10 + 1));
cv::Mat channels = integrals.row(i).reshape(0, h / shrinkage * 10 + 1);
prepocessor.apply(frame, channels);
dprintf("generated %d %d\n", dx, dy);
// if (predict(sum))
// // if (predict(sum))
{
responses.ptr<float>(i)[0] = 0.f;
// sum = sum.reshape(0, 1);
sum.copyTo(integrals.row(i).reshape(0, h + 1));
++i;
}
}
@ -258,11 +242,18 @@ void sft::Octave::generateNegatives(const Dataset& dataset)
dprintf("Processing negatives finished:\n\trequested %d negatives, viewed %d samples.\n", nnegatives, total);
}
bool sft::Octave::train(const Dataset& dataset, const FeaturePool& pool)
bool sft::Octave::train(const Dataset& dataset, const FeaturePool& pool, int weaks, int treeDepth)
{
CV_Assert(treeDepth == 2);
CV_Assert(weaks > 0);
params.max_depth = treeDepth;
params.weak_count = weaks;
// 1. fill integrals and classes
processPositives(dataset, pool);
generateNegatives(dataset);
// exit(0);
// 2. only sumple case (all features used)
int nfeatures = pool.size();
@ -313,8 +304,6 @@ sft::FeaturePool::FeaturePool(cv::Size m, int n) : model(m), nfeatures(n)
fill(nfeatures);
}
sft::FeaturePool::~FeaturePool(){}
float sft::FeaturePool::apply(int fi, int si, const Mat& integrals) const
{
return pool[fi](integrals.row(si), model);
@ -323,13 +312,13 @@ float sft::FeaturePool::apply(int fi, int si, const Mat& integrals) const
void sft::FeaturePool::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);
@ -363,10 +352,19 @@ void sft::FeaturePool::fill(int desired)
sft::ICF f(x, y, w, h, ch);
if (std::find(pool.begin(), pool.end(),f) == pool.end())
{
// std::cout << f << std::endl;
pool.push_back(f);
}
}
}
std::ostream& sft::operator<<(std::ostream& out, const sft::ICF& m)
{
out << m.channel << " " << m.bb;
return out;
}
// ============ Dataset ============ //
namespace {
using namespace sft;

@ -106,47 +106,34 @@ int main(int argc, char** argv)
// 3. Train all octaves
for (ivector::const_iterator it = cfg.octaves.begin(); it != cfg.octaves.end(); ++it)
{
// a. create rangom feature pool
int nfeatures = cfg.poolSize;
cv::Size model = cfg.model(it);
std::cout << "Model " << model << std::endl;
sft::FeaturePool pool(model, nfeatures);
nfeatures = pool.size();
int npositives = cfg.positives;
int nnegatives = cfg.negatives;
int shrinkage = cfg.shrinkage;
int octave = *it;
cv::Size model = cv::Size(cfg.modelWinSize.width / cfg.shrinkage, cfg.modelWinSize.height / cfg.shrinkage );
std::string path = cfg.trainPath;
cv::Rect boundingBox(cfg.offset.x / cfg.shrinkage, cfg.offset.y / cfg.shrinkage,
cfg.modelWinSize.width / cfg.shrinkage, cfg.modelWinSize.height / cfg.shrinkage);
cv::Rect boundingBox = cfg.bbox(it);
std::cout << "Object bounding box" << boundingBox << std::endl;
sft::Octave boost(boundingBox, npositives, nnegatives, octave, shrinkage);
sft::Octave boost(boundingBox, npositives, nnegatives, *it, shrinkage);
sft::FeaturePool pool(model, nfeatures);
std::string path = cfg.trainPath;
sft::Dataset dataset(path, boost.logScale);
if (boost.train(dataset, pool))
if (boost.train(dataset, pool, cfg.weaks, cfg.treeDepth))
{
}
std::cout << "Octave " << octave << " was successfully trained..." << std::endl;
// // d. crain octave
// if (octave.train(pool, cfg.positives, cfg.negatives, cfg.weaks))
// {
std::cout << "Octave " << *it << " was successfully trained..." << std::endl;
// strong.push_back(octave);
// }
}
}
// fso << "]" << "}";
// // 3. create Soft Cascade
// // sft::SCascade cascade(cfg.modelWinSize, cfg.octs, cfg.shrinkage);
// // // 4. Generate feature pool
// // std::vector<sft::ICF> pool;
// // sft::fillPool(pool, cfg.poolSize, cfg.modelWinSize / cfg.shrinkage, cfg.seed);
// // // 5. Train all octaves
// // cascade.train(cfg.trainPath);
// // // 6. Set thresolds
// // cascade.prune();

Loading…
Cancel
Save