From f6e3e3f049f51888b4d9f89df97f2e51b8e08222 Mon Sep 17 00:00:00 2001 From: "marina.kolpakova" Date: Thu, 6 Dec 2012 15:58:50 +0400 Subject: [PATCH] add negatives generation --- apps/sft/include/sft/octave.hpp | 4 ++- apps/sft/octave.cpp | 76 ++++++++++++++++++++++++++++++++++------- apps/sft/sft.cpp | 3 +- 3 files changed, 69 insertions(+), 14 deletions(-) diff --git a/apps/sft/include/sft/octave.hpp b/apps/sft/include/sft/octave.hpp index 51457c9..baaa642 100644 --- a/apps/sft/include/sft/octave.hpp +++ b/apps/sft/include/sft/octave.hpp @@ -102,7 +102,7 @@ private: class Octave : cv::Boost { public: - Octave(int npositives, int nnegatives, int logScale, int shrinkage); + Octave(cv::Rect boundingBox, int npositives, int nnegatives, int logScale, int shrinkage); virtual ~Octave(); virtual bool train(const Dataset& dataset, const FeaturePool& pool); @@ -114,7 +114,9 @@ protected: 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 generateNegatives(const Dataset& dataset); private: + cv::Rect boundingBox; int npositives; int nnegatives; diff --git a/apps/sft/octave.cpp b/apps/sft/octave.cpp index c0690b9..35348c6 100644 --- a/apps/sft/octave.cpp +++ b/apps/sft/octave.cpp @@ -58,8 +58,8 @@ #include // ============ Octave ============ // -sft::Octave::Octave(int np, int nn, int ls, int shr) -: logScale(ls), npositives(np), nnegatives(nn), shrinkage(shr) +sft::Octave::Octave(cv::Rect bb, int np, int nn, int ls, int shr) +: logScale(ls), boundingBox(bb), npositives(np), nnegatives(nn), shrinkage(shr) { int maxSample = npositives + nnegatives; responses.create(maxSample, 1, CV_32FC1); @@ -137,41 +137,93 @@ public: } // ToDo: parallelize it +// 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 cols = (64 * pow(2, logScale) + 1) * (128 * pow(2, logScale) + 1); - integrals.create(pool.size(), cols, CV_32SC1); + int w = 64 * pow(2, logScale) /shrinkage; + int h = 128 * pow(2, logScale) /shrinkage * 10; + + integrals.create(pool.size(), (w + 1) * (h + 1), CV_32SC1); int total = 0; - // float* responce = responce.ptr(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 channels = integrals.col(total).reshape(0, (128 * pow(2, logScale) + 1)); - - cv::Mat sample = cv::imread(curr); + cv::Mat sample = cv::imread(curr); + cv::Mat channels = integrals.col(total).reshape(0, h + 1); prepocessor.apply(sample, channels); + responses.ptr(total)[0] = 1.f; - ++total; - if (total >= npositives) break; + if (++total >= npositives) break; } dprintf("Processing positives finished:\n\trequested %d positives, collected %d samples.\n", npositives, total); - npositives = total; - nnegatives *= total / (float)npositives; + npositives = total; + nnegatives = cvRound(nnegatives * total / (double)npositives); +} + +void sft::Octave::generateNegatives(const Dataset& dataset) +{ + // ToDo: set seed, use offsets + sft::Random::engine eng; + sft::Random::engine idxEng; + + Preprocessor prepocessor(shrinkage); + + int nimages = (int)dataset.neg.size(); + sft::Random::uniform iRand(0, nimages - 1); + + int total = 0; + Mat sum; + for (int i = npositives; i < nnegatives + npositives; ++total) + { + int curr = iRand(idxEng); + + dprintf("View %d-th sample\n", curr); + dprintf("Process %s\n", dataset.neg[curr].c_str()); + + Mat frame = cv::imread(dataset.neg[curr]); + prepocessor.apply(frame, sum); + + int maxW = frame.cols - 2 * boundingBox.x - boundingBox.width; + int maxH = frame.rows - 2 * boundingBox.y - boundingBox.height; + + sft::Random::uniform wRand(0, maxW); + sft::Random::uniform hRand(0, maxH); + + int dx = wRand(eng); + int dy = hRand(eng); + + sum = sum(cv::Rect(dx, dy, boundingBox.width, boundingBox.height)); + + dprintf("generated %d %d\n", dx, dy); + + if (predict(sum)) + { + responses.ptr(i)[0] = 0.f; + sum = sum.reshape(0, 1); + sum.copyTo(integrals.col(i)); + ++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) { // 1. fill integrals and classes + processPositives(dataset, pool); + generateNegatives(dataset); + return false; } diff --git a/apps/sft/sft.cpp b/apps/sft/sft.cpp index 7c896bc..682a867 100644 --- a/apps/sft/sft.cpp +++ b/apps/sft/sft.cpp @@ -59,7 +59,8 @@ int main(int argc, char** argv) cv::Size model(64, 128); std::string path = "/home/kellan/cuda-dev/opencv_extra/testdata/sctrain/rescaled-train-2012-10-27-19-02-52"; - sft::Octave boost(npositives, nnegatives, octave, shrinkage); + cv::Rect boundingBox(5, 5 ,16, 32); + sft::Octave boost(boundingBox, npositives, nnegatives, octave, shrinkage); sft::FeaturePool pool(model, nfeatures); sft::Dataset dataset(path, boost.logScale); -- 2.7.4