#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>
{
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
_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);
}
};
}
-// 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;
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);
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);
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;
}
}
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();
fill(nfeatures);
}
-sft::FeaturePool::~FeaturePool(){}
-
float sft::FeaturePool::apply(int fi, int si, const Mat& integrals) const
{
return pool[fi](integrals.row(si), model);
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);
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;
// 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();