Commit bfa26fd4 authored by marina.kolpakova's avatar marina.kolpakova

refactoring

parent 883d691c
...@@ -57,6 +57,22 @@ struct Config ...@@ -57,6 +57,22 @@ struct Config
void read(const cv::FileNode& node); 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 // Paths to a rescaled data
string trainPath; string trainPath;
string testPath; string testPath;
......
...@@ -76,12 +76,14 @@ struct ICF ...@@ -76,12 +76,14 @@ struct ICF
float operator() (const Mat& integrals, const cv::Size& model) const 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 a = ptr[0];
int b = ptr[bb.width]; int b = ptr[bb.width];
ptr += bb.height * model.width; ptr += bb.height * step;
int c = ptr[bb.width]; int c = ptr[bb.width];
int d = ptr[0]; int d = ptr[0];
...@@ -92,13 +94,17 @@ struct ICF ...@@ -92,13 +94,17 @@ struct ICF
private: private:
cv::Rect bb; cv::Rect bb;
int channel; int channel;
friend std::ostream& operator<<(std::ostream& out, const ICF& m);
}; };
std::ostream& operator<<(std::ostream& out, const ICF& m);
class FeaturePool class FeaturePool
{ {
public: public:
FeaturePool(cv::Size model, int nfeatures); FeaturePool(cv::Size model, int nfeatures);
~FeaturePool();
int size() const { return (int)pool.size(); } int size() const { return (int)pool.size(); }
float apply(int fi, int si, const Mat& integrals) const; float apply(int fi, int si, const Mat& integrals) const;
...@@ -122,7 +128,7 @@ public: ...@@ -122,7 +128,7 @@ public:
Octave(cv::Rect boundingBox, int npositives, int nnegatives, int logScale, int shrinkage); Octave(cv::Rect boundingBox, int npositives, int nnegatives, int logScale, int shrinkage);
virtual ~Octave(); 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; int logScale;
...@@ -144,7 +150,6 @@ private: ...@@ -144,7 +150,6 @@ private:
Mat responses; Mat responses;
CvBoostParams params; CvBoostParams params;
}; };
} }
......
...@@ -43,16 +43,6 @@ ...@@ -43,16 +43,6 @@
#include <sft/octave.hpp> #include <sft/octave.hpp>
#include <sft/random.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 <glob.h>
#include <opencv2/imgproc/imgproc.hpp> #include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp> #include <opencv2/highgui/highgui.hpp>
...@@ -63,13 +53,7 @@ sft::Octave::Octave(cv::Rect bb, int np, int nn, int ls, int shr) ...@@ -63,13 +53,7 @@ sft::Octave::Octave(cv::Rect bb, int np, int nn, int ls, int shr)
{ {
int maxSample = npositives + nnegatives; int maxSample = npositives + nnegatives;
responses.create(maxSample, 1, CV_32FC1); 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; CvBoostParams _params;
{ {
// tree params // tree params
...@@ -79,27 +63,35 @@ bool sft::Octave::train( const cv::Mat& trainData, const cv::Mat& _responses, co ...@@ -79,27 +63,35 @@ bool sft::Octave::train( const cv::Mat& trainData, const cv::Mat& _responses, co
_params.truncate_pruned_tree = false; _params.truncate_pruned_tree = false;
_params.use_surrogates = false; _params.use_surrogates = false;
_params.use_1se_rule = false; _params.use_1se_rule = false;
_params.regression_accuracy = 0.0; _params.regression_accuracy = 1.0e-6;
// boost params // boost params
_params.boost_type = CvBoost::GENTLE; _params.boost_type = CvBoost::GENTLE;
_params.split_criteria = CvBoost::SQERR; _params.split_criteria = CvBoost::SQERR;
_params.weight_trim_rate = 0.95; _params.weight_trim_rate = 0.95;
// simple defaults
/// ToDo: move to params
_params.min_sample_count = 2; _params.min_sample_count = 2;
_params.weak_count = 1; _params.weak_count = 1;
} }
std::cout << "WARNING: " << sampleIdx << std::endl; params = _params;
std::cout << "WARNING: " << trainData << std::endl; }
std::cout << "WARNING: " << _responses << std::endl;
std::cout << "WARNING: " << varIdx << std::endl; sft::Octave::~Octave(){}
std::cout << "WARNING: " << varType << std::endl;
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; 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); update);
} }
...@@ -164,29 +156,30 @@ public: ...@@ -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 // 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) void sft::Octave::processPositives(const Dataset& dataset, const FeaturePool& pool)
{ {
Preprocessor prepocessor(shrinkage); Preprocessor prepocessor(shrinkage);
int w = 64 * pow(2, logScale) /shrinkage; int w = boundingBox.width;
int h = 128 * pow(2, logScale) /shrinkage * 10; 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; int total = 0;
for (svector::const_iterator it = dataset.pos.begin(); it != dataset.pos.end(); ++it) for (svector::const_iterator it = dataset.pos.begin(); it != dataset.pos.end(); ++it)
{ {
const string& curr = *it; const string& curr = *it;
dprintf("Process candidate positive image %s\n", curr.c_str()); dprintf("Process candidate positive image %s\n", curr.c_str());
cv::Mat sample = cv::imread(curr); cv::Mat sample = cv::imread(curr);
cv::Mat channels = integrals.row(total).reshape(0, h + 1);
prepocessor.apply(sample, channels);
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; responses.ptr<float>(total)[0] = 1.f;
if (++total >= npositives) break; if (++total >= npositives) break;
...@@ -204,8 +197,8 @@ void sft::Octave::generateNegatives(const Dataset& dataset) ...@@ -204,8 +197,8 @@ void sft::Octave::generateNegatives(const Dataset& dataset)
sft::Random::engine eng; sft::Random::engine eng;
sft::Random::engine idxEng; sft::Random::engine idxEng;
int w = 64 * pow(2, logScale) /shrinkage; int w = boundingBox.width;
int h = 128 * pow(2, logScale) /shrinkage * 10; int h = boundingBox.height;
Preprocessor prepocessor(shrinkage); Preprocessor prepocessor(shrinkage);
...@@ -222,15 +215,9 @@ void sft::Octave::generateNegatives(const Dataset& dataset) ...@@ -222,15 +215,9 @@ void sft::Octave::generateNegatives(const Dataset& dataset)
dprintf("Process %s\n", dataset.neg[curr].c_str()); dprintf("Process %s\n", dataset.neg[curr].c_str());
Mat frame = cv::imread(dataset.neg[curr]); Mat frame = cv::imread(dataset.neg[curr]);
prepocessor.apply(frame, sum);
std::cout << "WARNING: " << frame.cols << " " << frame.rows << std::endl; int maxW = frame.cols - 2 * boundingBox.x - boundingBox.width;
std::cout << "WARNING: " << frame.cols / shrinkage << " " << frame.rows / shrinkage << std::endl; int maxH = frame.rows - 2 * boundingBox.y - boundingBox.height;
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;
sft::Random::uniform wRand(0, maxW -1); sft::Random::uniform wRand(0, maxW -1);
sft::Random::uniform hRand(0, maxH -1); sft::Random::uniform hRand(0, maxH -1);
...@@ -238,19 +225,16 @@ void sft::Octave::generateNegatives(const Dataset& dataset) ...@@ -238,19 +225,16 @@ void sft::Octave::generateNegatives(const Dataset& dataset)
int dx = wRand(eng); int dx = wRand(eng);
int dy = hRand(eng); int dy = hRand(eng);
std::cout << "WARNING: " << dx << " " << dy << std::endl; frame = frame(cv::Rect(dx, dy, boundingBox.width, boundingBox.height));
std::cout << "WARNING: " << dx + boundingBox.width + 1 << " " << dy + boundingBox.height + 1 << std::endl;
std::cout << "WARNING: " << sum.cols << " " << sum.rows << std::endl;
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); dprintf("generated %d %d\n", dx, dy);
// if (predict(sum)) // // if (predict(sum))
{ {
responses.ptr<float>(i)[0] = 0.f; responses.ptr<float>(i)[0] = 0.f;
// sum = sum.reshape(0, 1);
sum.copyTo(integrals.row(i).reshape(0, h + 1));
++i; ++i;
} }
} }
...@@ -258,11 +242,18 @@ void sft::Octave::generateNegatives(const Dataset& dataset) ...@@ -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); 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 // 1. fill integrals and classes
processPositives(dataset, pool); processPositives(dataset, pool);
generateNegatives(dataset); generateNegatives(dataset);
// exit(0);
// 2. only sumple case (all features used) // 2. only sumple case (all features used)
int nfeatures = pool.size(); int nfeatures = pool.size();
...@@ -313,8 +304,6 @@ sft::FeaturePool::FeaturePool(cv::Size m, int n) : model(m), nfeatures(n) ...@@ -313,8 +304,6 @@ sft::FeaturePool::FeaturePool(cv::Size m, int n) : model(m), nfeatures(n)
fill(nfeatures); fill(nfeatures);
} }
sft::FeaturePool::~FeaturePool(){}
float sft::FeaturePool::apply(int fi, int si, const Mat& integrals) const float sft::FeaturePool::apply(int fi, int si, const Mat& integrals) const
{ {
return pool[fi](integrals.row(si), model); return pool[fi](integrals.row(si), model);
...@@ -323,13 +312,13 @@ float sft::FeaturePool::apply(int fi, int si, const Mat& integrals) const ...@@ -323,13 +312,13 @@ float sft::FeaturePool::apply(int fi, int si, const Mat& integrals) const
void sft::FeaturePool::fill(int desired) void sft::FeaturePool::fill(int desired)
{ {
int mw = model.width; int mw = model.width;
int mh = model.height; int mh = model.height;
int maxPoolSize = (mw -1) * mw / 2 * (mh - 1) * mh / 2 * N_CHANNELS; int maxPoolSize = (mw -1) * mw / 2 * (mh - 1) * mh / 2 * N_CHANNELS;
nfeatures = std::min(desired, maxPoolSize); nfeatures = std::min(desired, maxPoolSize);
dprintf("Requeste feature pool %d max %d suggested %d\n", desired, maxPoolSize, nfeatures);
pool.reserve(nfeatures); pool.reserve(nfeatures);
...@@ -363,10 +352,19 @@ void sft::FeaturePool::fill(int desired) ...@@ -363,10 +352,19 @@ void sft::FeaturePool::fill(int desired)
sft::ICF f(x, y, w, h, ch); sft::ICF f(x, y, w, h, ch);
if (std::find(pool.begin(), pool.end(),f) == pool.end()) if (std::find(pool.begin(), pool.end(),f) == pool.end())
{
// std::cout << f << std::endl;
pool.push_back(f); pool.push_back(f);
}
} }
} }
std::ostream& sft::operator<<(std::ostream& out, const sft::ICF& m)
{
out << m.channel << " " << m.bb;
return out;
}
// ============ Dataset ============ // // ============ Dataset ============ //
namespace { namespace {
using namespace sft; using namespace sft;
......
...@@ -106,47 +106,34 @@ int main(int argc, char** argv) ...@@ -106,47 +106,34 @@ int main(int argc, char** argv)
// 3. Train all octaves // 3. Train all octaves
for (ivector::const_iterator it = cfg.octaves.begin(); it != cfg.octaves.end(); ++it) for (ivector::const_iterator it = cfg.octaves.begin(); it != cfg.octaves.end(); ++it)
{ {
// a. create rangom feature pool
int nfeatures = cfg.poolSize; 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 npositives = cfg.positives;
int nnegatives = cfg.negatives; int nnegatives = cfg.negatives;
int shrinkage = cfg.shrinkage; int shrinkage = cfg.shrinkage;
int octave = *it; cv::Rect boundingBox = cfg.bbox(it);
std::cout << "Object bounding box" << boundingBox << std::endl;
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);
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); sft::Dataset dataset(path, boost.logScale);
if (boost.train(dataset, pool)) if (boost.train(dataset, pool, cfg.weaks, cfg.treeDepth))
{ {
} std::cout << "Octave " << *it << " was successfully trained..." << std::endl;
std::cout << "Octave " << octave << " was successfully trained..." << std::endl;
// // d. crain octave
// if (octave.train(pool, cfg.positives, cfg.negatives, cfg.weaks))
// {
// strong.push_back(octave); // strong.push_back(octave);
// } }
} }
// fso << "]" << "}"; // 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 // // // 6. Set thresolds
// // cascade.prune(); // // cascade.prune();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment